Skip to content

Commit 50e1f64

Browse files
authored
Merge pull request #207 from BenPH/fix-ddot-lexing
Fix tokenization of numbers followed by `..`
2 parents 13396b8 + 22d1e17 commit 50e1f64

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/lexer.jl

+7-4
Original file line numberDiff line numberDiff line change
@@ -629,12 +629,13 @@ function lex_digit(l::Lexer, kind)
629629
accept_number(l, isdigit)
630630
pc,ppc = dpeekchar(l)
631631
if pc == '.'
632-
if kind === Tokens.FLOAT
632+
if ppc == '.'
633+
# Number followed by .. or ...
634+
return emit(l, kind)
635+
elseif kind === Tokens.FLOAT
633636
# If we enter the function with kind == FLOAT then a '.' has been parsed.
634637
readchar(l)
635638
return emit_error(l, Tokens.INVALID_NUMERIC_CONSTANT)
636-
elseif ppc == '.'
637-
return emit(l, kind)
638639
elseif is_operator_start_char(ppc) && ppc !== ':'
639640
readchar(l)
640641
return emit_error(l)
@@ -703,7 +704,9 @@ function lex_digit(l::Lexer, kind)
703704
readchar(l)
704705
!(ishex(ppc) || ppc == '.') && return emit_error(l, Tokens.INVALID_NUMERIC_CONSTANT)
705706
accept_number(l, ishex)
706-
if accept(l, '.')
707+
pc,ppc = dpeekchar(l)
708+
if pc == '.' && ppc != '.'
709+
readchar(l)
707710
accept_number(l, ishex)
708711
isfloat = true
709712
end

test/lexer.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ end
358358
@test tok("1234x", 2).kind == T.IDENTIFIER
359359
end
360360

361-
@testset "floats with trailing `.` " begin
361+
@testset "numbers with trailing `.` " begin
362362
@test tok("1.0").kind == Tokens.FLOAT
363363
@test tok("1.a").kind == Tokens.FLOAT
364364
@test tok("1.(").kind == Tokens.FLOAT
@@ -373,7 +373,10 @@ end
373373
@test tok("1.").kind == Tokens.FLOAT
374374
@test tok("1.\"text\" ").kind == Tokens.FLOAT
375375

376-
@test tok("1..").kind == Tokens.INTEGER
376+
@test toks("1..") == ["1"=>Tokens.INTEGER, ".."=>Tokens.DDOT]
377+
@test toks(".1..") == [".1"=>Tokens.FLOAT, ".."=>Tokens.DDOT]
378+
@test toks("0x01..") == ["0x01"=>Tokens.HEX_INT, ".."=>Tokens.DDOT]
379+
377380
@test T.kind.(collect(tokenize("1f0./1"))) == [T.FLOAT, T.OP, T.INTEGER, T.ENDMARKER]
378381
end
379382

0 commit comments

Comments
 (0)