Skip to content

Commit

Permalink
+ ruby{18,19,20,21}.y, builders/default: lvar-injecting match (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Jun 16, 2013
1 parent 6b75ff0 commit ee46e7e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
22 changes: 22 additions & 0 deletions lib/parser/builders/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,28 @@ def binary_op(receiver, operator_t, arg)
source_map)
end

def match_op(receiver, match_t, arg)
source_map = send_binary_op_map(receiver, match_t, arg)

if receiver.type == :regexp &&
receiver.children.count == 2 &&
receiver.children.first.type == :str

regexp_str, _regopt = *receiver
regexp_body, = *regexp_str

Regexp.new(regexp_body).names.each do |name|
@parser.static_env.declare(name)
end

n(:match_with_lvasgn, [ receiver, arg ],
source_map)
else
n(:send, [ receiver, :=~, arg ],
source_map)
end
end

def unary_op(op_t, receiver)
case value(op_t)
when '+', '-'
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/ruby18.y
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ rule
}
| arg tMATCH arg
{
result = @builder.binary_op(val[0], val[1], val[2])
result = @builder.match_op(val[0], val[1], val[2])
}
| arg tNMATCH arg
{
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/ruby19.y
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ rule
}
| arg tMATCH arg
{
result = @builder.binary_op(val[0], val[1], val[2])
result = @builder.match_op(val[0], val[1], val[2])
}
| arg tNMATCH arg
{
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/ruby20.y
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ rule
}
| arg tMATCH arg
{
result = @builder.binary_op(val[0], val[1], val[2])
result = @builder.match_op(val[0], val[1], val[2])
}
| arg tNMATCH arg
{
Expand Down
2 changes: 1 addition & 1 deletion lib/parser/ruby21.y
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ rule
}
| arg tMATCH arg
{
result = @builder.binary_op(val[0], val[1], val[2])
result = @builder.match_op(val[0], val[1], val[2])
}
| arg tNMATCH arg
{
Expand Down
26 changes: 26 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2882,6 +2882,32 @@ def test_send_call
ALL_VERSIONS - %w(1.8))
end

def test_lvar_injecting_match
assert_parses(
s(:begin,
s(:match_with_lvasgn,
s(:regexp,
s(:str, '(?<match>bar)'),
s(:regopt)),
s(:str, 'bar')),
s(:lvar, :match)),
%q{/(?<match>bar)/ =~ 'bar'; match},
%q{ ~~ selector (match_with_lvasgn)
|~~~~~~~~~~~~~~~~~~~~~~~~ expression (match_with_lvasgn)})
end

# TODO not yet
# def test_non_lvar_injecting_match
# assert_parses(
# s(:send,
# s(:regexp,
# s(:str, '(?<match>bar)'),
# s(:regopt)),
# :=~,
# s(:str, 'bar')),
# %q{/#{'(?<match>bar)'}/ =~ 'bar'})
# end

# To superclass

def test_super
Expand Down

0 comments on commit ee46e7e

Please sign in to comment.