Skip to content

Commit

Permalink
Lexer: fix global capture vars ending with zero, e.g. $10? (#12701)
Browse files Browse the repository at this point in the history
  • Loading branch information
FnControlOption committed Nov 1, 2022
1 parent 42a3f91 commit 47ae3d7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 3 additions & 1 deletion spec/compiler/lexer/lexer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ describe "Lexer" do
":^", ":~", ":**", ":>>", ":<<", ":%", ":[]", ":[]?", ":[]=", ":<=>", ":===",
":&+", ":&-", ":&*", ":&**"]

it_lexes_global_match_data_index ["$1", "$10", "$1?", "$23?"]
it_lexes_global_match_data_index ["$1", "$10", "$1?", "$10?", "$23?"]
assert_syntax_error "$01", %(unexpected token: "1")
assert_syntax_error "$0?"

it_lexes "$~", :OP_DOLLAR_TILDE
it_lexes "$?", :OP_DOLLAR_QUESTION
Expand Down
11 changes: 5 additions & 6 deletions src/compiler/crystal/syntax/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -827,14 +827,13 @@ module Crystal
@token.type = :OP_DOLLAR_QUESTION
when .ascii_number?
start = current_pos
char = next_char
if char == '0'
char = next_char
if current_char == '0'
next_char
else
while char.ascii_number?
char = next_char
while next_char.ascii_number?
# Nothing to do
end
char = next_char if char == '?'
next_char if current_char == '?'
end
@token.type = :GLOBAL_MATCH_DATA_INDEX
@token.value = string_range_from_pool(start)
Expand Down

0 comments on commit 47ae3d7

Please sign in to comment.