Skip to content

Commit b1f16e2

Browse files
authored
Fix tokenization of numbers followed by .. (#143)
* `.1..` is `.1` followed by `..` * `0x01..` is `0x01` followed by `..` Also fixes these cases when followed by `...`
1 parent 7dfd7c2 commit b1f16e2

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/tokenize.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -763,12 +763,13 @@ function lex_digit(l::Lexer, kind)
763763
accept_number(l, isdigit)
764764
pc,ppc = dpeekchar(l)
765765
if pc == '.'
766-
if kind === K"Float"
766+
if ppc == '.'
767+
# Number followed by K".." or K"..."
768+
return emit(l, kind)
769+
elseif kind === K"Float"
767770
# If we enter the function with kind == K"Float" then a '.' has been parsed.
768771
readchar(l)
769772
return emit_error(l, K"ErrorInvalidNumericConstant")
770-
elseif ppc == '.'
771-
return emit(l, kind)
772773
elseif is_operator_start_char(ppc) && ppc !== ':'
773774
readchar(l)
774775
return emit_error(l)
@@ -838,7 +839,9 @@ function lex_digit(l::Lexer, kind)
838839
readchar(l)
839840
!(ishex(ppc) || ppc == '.') && return emit_error(l, K"ErrorInvalidNumericConstant")
840841
accept_number(l, ishex)
841-
if accept(l, '.')
842+
pc,ppc = dpeekchar(l)
843+
if pc == '.' && ppc != '.'
844+
readchar(l)
842845
accept_number(l, ishex)
843846
isfloat = true
844847
end

test/tokenize.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ tok(str, i = 1) = collect(tokenize(str))[i]
2121

2222
strtok(str) = untokenize.(collect(tokenize(str)), str)
2323

24+
function toks(str)
25+
ts = [untokenize(t, str)=>kind(t) for t in tokenize(str)]
26+
@test ts[end] == (""=>K"EndMarker")
27+
pop!(ts)
28+
ts
29+
end
30+
2431
@testset "tokens" begin
2532
for s in ["a", IOBuffer("a")]
2633
l = tokenize(s)
@@ -553,7 +560,7 @@ end
553560
@test kind(tok("1234x", 2)) == K"Identifier"
554561
end
555562

556-
@testset "floats with trailing `.` " begin
563+
@testset "numbers with trailing `.` " begin
557564
@test tok("1.0").kind == K"Float"
558565
@test tok("1.a").kind == K"Float"
559566
@test tok("1.(").kind == K"Float"
@@ -569,7 +576,10 @@ end
569576
@test tok("1.").kind == K"Float"
570577
@test tok("1.\"text\" ").kind == K"Float"
571578

572-
@test tok("1..").kind == K"Integer"
579+
@test toks("1..") == ["1"=>K"Integer", ".."=>K".."]
580+
@test toks(".1..") == [".1"=>K"Float", ".."=>K".."]
581+
@test toks("0x01..") == ["0x01"=>K"HexInt", ".."=>K".."]
582+
573583
@test kind.(collect(tokenize("1f0./1"))) == [K"Float", K"/", K"Integer", K"EndMarker"]
574584
end
575585

0 commit comments

Comments
 (0)