Skip to content

Commit

Permalink
Lexer: show helpful error message against invalid number literal like…
Browse files Browse the repository at this point in the history
… '.42'

Dot-started floating-point number like '.42' is not supported on
Crystal. However compiler does not tell us this, so this fix adds to
detect such a literal and to show helpful error message.
  • Loading branch information
makenowjust committed Aug 3, 2021
1 parent 4a04eba commit a6283d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions spec/compiler/lexer/lexer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ describe "Lexer" do
assert_syntax_error "4F64", "unexpected token: F64"
assert_syntax_error "0F32", "unexpected token: F32"

assert_syntax_error ".42", ".1 style number literal is not supported, put 0 before dot"
assert_syntax_error "-.42", ".1 style number literal is not supported, put 0 before dot"

it "lexes not instance var" do
lexer = Lexer.new "!@foo"
token = lexer.next_token
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@ module Crystal
when '~'
next_char :"~"
when '.'
line = @line_number
column = @column_number
case next_char
when '.'
case next_char
Expand All @@ -644,6 +646,8 @@ module Crystal
else
@token.type = :".."
end
when .ascii_number?
raise ".1 style number literal is not supported, put 0 before dot", line, column
else
@token.type = :"."
end
Expand Down

0 comments on commit a6283d4

Please sign in to comment.