Skip to content

Commit

Permalink
Add helpful error message for invalid number literal like '.42' (#4665)
Browse files Browse the repository at this point in the history
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 authored Aug 4, 2021
1 parent 606ea3b commit fe57059
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
@@ -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
4 changes: 4 additions & 0 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
@@ -636,6 +636,8 @@ module Crystal
when '~'
next_char :"~"
when '.'
line = @line_number
column = @column_number
case next_char
when '.'
case next_char
@@ -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

0 comments on commit fe57059

Please sign in to comment.