Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tokenization of numbers followed by .. #207

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/lexer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -629,12 +629,13 @@ function lex_digit(l::Lexer, kind)
accept_number(l, isdigit)
pc,ppc = dpeekchar(l)
if pc == '.'
if kind === Tokens.FLOAT
if ppc == '.'
# Number followed by .. or ...
return emit(l, kind)
elseif kind === Tokens.FLOAT
# If we enter the function with kind == FLOAT then a '.' has been parsed.
readchar(l)
return emit_error(l, Tokens.INVALID_NUMERIC_CONSTANT)
elseif ppc == '.'
return emit(l, kind)
elseif is_operator_start_char(ppc) && ppc !== ':'
readchar(l)
return emit_error(l)
Expand Down Expand Up @@ -703,7 +704,9 @@ function lex_digit(l::Lexer, kind)
readchar(l)
!(ishex(ppc) || ppc == '.') && return emit_error(l, Tokens.INVALID_NUMERIC_CONSTANT)
accept_number(l, ishex)
if accept(l, '.')
pc,ppc = dpeekchar(l)
if pc == '.' && ppc != '.'
readchar(l)
accept_number(l, ishex)
isfloat = true
end
Expand Down
7 changes: 5 additions & 2 deletions test/lexer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ end
@test tok("1234x", 2).kind == T.IDENTIFIER
end

@testset "floats with trailing `.` " begin
@testset "numbers with trailing `.` " begin
@test tok("1.0").kind == Tokens.FLOAT
@test tok("1.a").kind == Tokens.FLOAT
@test tok("1.(").kind == Tokens.FLOAT
Expand All @@ -373,7 +373,10 @@ end
@test tok("1.").kind == Tokens.FLOAT
@test tok("1.\"text\" ").kind == Tokens.FLOAT

@test tok("1..").kind == Tokens.INTEGER
@test toks("1..") == ["1"=>Tokens.INTEGER, ".."=>Tokens.DDOT]
@test toks(".1..") == [".1"=>Tokens.FLOAT, ".."=>Tokens.DDOT]
@test toks("0x01..") == ["0x01"=>Tokens.HEX_INT, ".."=>Tokens.DDOT]

@test T.kind.(collect(tokenize("1f0./1"))) == [T.FLOAT, T.OP, T.INTEGER, T.ENDMARKER]
end

Expand Down