From a6283d4400751240a234d40dc315053996296ea8 Mon Sep 17 00:00:00 2001 From: TSUYUSATO Kitsune Date: Tue, 4 Jul 2017 00:11:06 +0900 Subject: [PATCH] Lexer: show helpful error message against invalid number literal like '.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. --- spec/compiler/lexer/lexer_spec.cr | 3 +++ src/compiler/crystal/syntax/lexer.cr | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/spec/compiler/lexer/lexer_spec.cr b/spec/compiler/lexer/lexer_spec.cr index 6a858baeaae8..612e16512da7 100644 --- a/spec/compiler/lexer/lexer_spec.cr +++ b/spec/compiler/lexer/lexer_spec.cr @@ -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 diff --git a/src/compiler/crystal/syntax/lexer.cr b/src/compiler/crystal/syntax/lexer.cr index 708a58b63e30..fbaf61bdb159 100644 --- a/src/compiler/crystal/syntax/lexer.cr +++ b/src/compiler/crystal/syntax/lexer.cr @@ -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