Skip to content

Commit 1b63cc3

Browse files
author
Ary Borenszweig
committed
Added $1?, $2?, etc. as a nilable alternative to$1, $2
1 parent e3d065c commit 1b63cc3

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

spec/compiler/lexer/lexer_spec.cr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ end
114114

115115
private def it_lexes_global_match_data_index(globals)
116116
globals.each do |global|
117-
it_lexes global, :GLOBAL_MATCH_DATA_INDEX, global[1, global.length - 1].to_i
117+
it_lexes global, :GLOBAL_MATCH_DATA_INDEX, global[1, global.length - 1]
118118
end
119119
end
120120

@@ -234,7 +234,7 @@ describe "Lexer" do
234234
it_lexes_symbols [":foo", ":foo!", ":foo?", ":\"foo\"", ":かたな", ":+", ":-", ":*", ":/",
235235
":==", ":<", ":<=", ":>", ":>=", ":!", ":!=", ":=~", ":!~", ":&", ":|",
236236
":^", ":~", ":**", ":>>", ":<<", ":%", ":[]", ":[]?", ":[]=", ":<=>", ":==="]
237-
it_lexes_global_match_data_index ["$1", "$10"]
237+
it_lexes_global_match_data_index ["$1", "$10", "$1?", "$23?"]
238238

239239
it_lexes "$~", :"$~"
240240
it_lexes "$?", :"$?"

spec/compiler/parser/parser_spec.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ describe "Parser" do
754754
it_parses "$~", Call.new("$~".var, "not_nil!")
755755
it_parses "$~.foo", Call.new(Call.new("$~".var, "not_nil!"), "foo")
756756
it_parses "$1", Call.new(Call.new("$~".var, "not_nil!"), "[]", 1.int32)
757+
it_parses "$1?", Call.new(Call.new("$~".var, "not_nil!"), "[]?", 1.int32)
757758
it_parses "foo $1", Call.new(nil, "foo", Call.new(Call.new("$~".var, "not_nil!"), "[]", 1.int32))
758759
it_parses "$~ = 1", Assign.new("$~".var, 1.int32)
759760

src/compiler/crystal/syntax/lexer.cr

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,18 @@ module Crystal
586586
next_char
587587
@token.type = :"$?"
588588
when .digit?
589-
number = current_char - '0'
590-
while (char = next_char).digit?
591-
number *= 10
592-
number += char - '0'
589+
start = current_pos
590+
char = next_char
591+
if char == '0'
592+
char = next_char
593+
else
594+
while char.digit?
595+
char = next_char
596+
end
597+
char = next_char if char == '?'
593598
end
594599
@token.type = :GLOBAL_MATCH_DATA_INDEX
595-
@token.value = number
600+
@token.value = string_range(start)
596601
else
597602
if ident_start?(current_char)
598603
while ident_part?(next_char)

src/compiler/crystal/syntax/parser.cr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,18 @@ module Crystal
808808
node_and_next_token Call.new(var, "not_nil!").at(location)
809809
end
810810
when :GLOBAL_MATCH_DATA_INDEX
811-
value = @token.value
812-
if value == 0
811+
value = @token.value.to_s
812+
if value == "0"
813813
node_and_next_token Path.global("PROGRAM_NAME")
814814
else
815+
if value.ends_with? '?'
816+
method = "[]?"
817+
value = value.chop
818+
else
819+
method = "[]"
820+
end
815821
location = @token.location
816-
node_and_next_token Call.new(Call.new(Var.new("$~").at(location), "not_nil!").at(location), "[]", NumberLiteral.new(value as Int32))
822+
node_and_next_token Call.new(Call.new(Var.new("$~").at(location), "not_nil!").at(location), method, NumberLiteral.new(value.to_i))
817823
end
818824
when :__LINE__
819825
node_and_next_token MagicConstant.expand_line_node(@token.location)

0 commit comments

Comments
 (0)