From 9e061ca8884de5ae3936bfdad00b7205d15ecad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fn=20=E2=8C=83=20=E2=8C=A5?= <70830482+FnControlOption@users.noreply.github.com> Date: Sun, 30 Oct 2022 12:59:32 -0700 Subject: [PATCH] Lexer: fix global capture vars ending with zero, e.g. `$10?` --- spec/compiler/lexer/lexer_spec.cr | 4 +++- src/compiler/crystal/syntax/lexer.cr | 11 +++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/spec/compiler/lexer/lexer_spec.cr b/spec/compiler/lexer/lexer_spec.cr index d6d7868c52c1..7cc0478a89f5 100644 --- a/spec/compiler/lexer/lexer_spec.cr +++ b/spec/compiler/lexer/lexer_spec.cr @@ -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 diff --git a/src/compiler/crystal/syntax/lexer.cr b/src/compiler/crystal/syntax/lexer.cr index 733aa6d775ac..95bc4089f03e 100644 --- a/src/compiler/crystal/syntax/lexer.cr +++ b/src/compiler/crystal/syntax/lexer.cr @@ -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)