Skip to content

Commit 15ff235

Browse files
committed
Track which filename a token was defined in
1 parent b4f871e commit 15ff235

File tree

8 files changed

+24
-13
lines changed

8 files changed

+24
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '../../fixtures/runtime_error.lit' # error: [runtime_error.lit:2] Runtime error: Undefined variable 'variable'.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import '../../fixtures/function.lit'
2+
3+
open_file("unknown-file.foobar") # error: [function.lit:4] Runtime error: File not found: unknown-file.foobar

spec/fixtures/function.lit

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# a comment
2+
3+
fn open_file { |path|
4+
open!(path)
5+
}

spec/support/create.cr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ module Create
3737
def token(type : Symbol, value = nil) : Lit::Token
3838
case type
3939
when :number
40-
Lit::Token.new(Lit::TokenType.parse(type.to_s), "1", 1.0, 1)
40+
Lit::Token.new(Lit::TokenType.parse(type.to_s), "1", 1.0, 1, ::Lit.current_file_name)
4141
when :string
42-
Lit::Token.new(Lit::TokenType.parse(type.to_s), %("some text"), "some text", 1)
42+
Lit::Token.new(Lit::TokenType.parse(type.to_s), %("some text"), "some text", 1, ::Lit.current_file_name)
4343
when :identifier
44-
Lit::Token.new(Lit::TokenType.parse(type.to_s), value || "my_var", nil, 1)
44+
Lit::Token.new(Lit::TokenType.parse(type.to_s), value || "my_var", nil, 1, ::Lit.current_file_name)
4545
when :string_interpolation
46-
Lit::Token.new(Lit::TokenType.parse(type.to_s), "#{value}", value, 1)
46+
Lit::Token.new(Lit::TokenType.parse(type.to_s), "#{value}", value, 1, ::Lit.current_file_name)
4747
else
4848
if TOKENS.has_key?(type)
49-
return Lit::Token.new(Lit::TokenType.parse(type.to_s), TOKENS[type], nil, 1)
49+
return Lit::Token.new(Lit::TokenType.parse(type.to_s), TOKENS[type], nil, 1, ::Lit.current_file_name)
5050
elsif type.to_s.starts_with?("number_")
5151
number = type.to_s.lchop("number_")
5252

53-
return Lit::Token.new(token_type(NUMBER), number, number.to_f, 1)
53+
return Lit::Token.new(token_type(NUMBER), number, number.to_f, 1, ::Lit.current_file_name)
5454
end
5555

5656
raise "Don't know hot to build token with type '#{type}'"

src/lit/error_reporter.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Lit
2525
end
2626

2727
def report_runtime_error(error)
28-
STDERR.puts Text.error("[#{::Lit.current_file_name}:#{error.token.line}] Runtime error: #{error.message}")
28+
STDERR.puts Text.error("[#{error.token.filename}:#{error.token.line}] Runtime error: #{error.message}")
2929

3030
@had_runtime_error = true
3131
end

src/lit/parser.cr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,10 @@ module Lit
121121
private def until_expr
122122
condition = expression
123123
body = block_expr("I was expecting a block after the until condition.")
124+
filename = previous.filename
124125

125126
# desugar until to while
126-
Expr::While.new(Expr::Unary.new(Token.new(TokenType::BANG, "!", nil, 0), condition), body)
127+
Expr::While.new(Expr::Unary.new(Token.new(TokenType::BANG, "!", nil, 0, filename), condition), body)
127128
end
128129

129130
private def loop_expr
@@ -216,7 +217,7 @@ module Lit
216217
raise error(@default_param.not_nil!, "Default parameter can't be used when explicit parameters are defined.")
217218
elsif @default_param && !@block_has_explicit_params
218219
# Inject implicit `it`
219-
return {block_body, [Token.new(TokenType::IDENTIFIER, "it", nil, peek.line)]}
220+
return {block_body, [Token.new(TokenType::IDENTIFIER, "it", nil, peek.line, peek.filename)]}
220221
end
221222

222223
{block_body, params}

src/lit/scanner.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,11 @@ module Lit
339339
private def add_token(type, literal)
340340
# Make line tokens appear on the line containing the "\n".
341341
line = type == TokenType::NEWLINE ? @line - 1 : @line
342-
@tokens << Token.new(type, current_token_string, literal, line)
342+
@tokens << Token.new(type, current_token_string, literal, line, ::Lit.current_file_name)
343343
end
344344

345345
private def add_eof_token
346-
@tokens << Token.new(TokenType::EOF, "", nil, @line)
346+
@tokens << Token.new(TokenType::EOF, "", nil, @line, ::Lit.current_file_name)
347347
end
348348

349349
private def digit?(c : Char) : Bool

src/lit/token.cr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ module Lit
66
getter lexeme : String
77
getter literal : Int64 | Float64 | String | Nil
88
getter line : Int32
9+
getter filename : String
910

10-
def initialize(@type, @lexeme, @literal, @line); end
11+
def initialize(@type, @lexeme, @literal, @line, @filename); end
1112

1213
def inspect
1314
%(<#{type} lexeme: #{lexeme.inspect} literal: #{literal ? literal.inspect : "nothing"}>)
1415
end
1516

1617
def with_lexeme(lexeme)
17-
self.class.new(type, lexeme, literal, line)
18+
self.class.new(type, lexeme, literal, line, filename)
1819
end
1920

2021
delegate :to_s, to: :inspect

0 commit comments

Comments
 (0)