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

+ ruby31.y: pin operator with expression #797

Merged
merged 1 commit into from Apr 20, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions doc/AST_FORMAT.md
Expand Up @@ -2112,6 +2112,24 @@ Format:
~~~~ expression
~~~

### Pin operator with expression

Format:

~~~
(pin
(begin
(send
(int 2) :+
(int 2))))
"in ^(2 + 2)"
~ selector
~~~~~~~~ expression
~ begin (begin)
~ end (begin)
~~~~~~~ expression (begin)
~~~

### Match alternative

Format:
Expand Down
9 changes: 8 additions & 1 deletion lib/parser/ruby31.y
Expand Up @@ -1946,6 +1946,7 @@ opt_block_args_tail:
}

p_expr_basic: p_value
| p_variable
| p_const p_lparen p_args rparen
{
@pattern_hash_keys.pop
Expand Down Expand Up @@ -2191,8 +2192,8 @@ opt_block_args_tail:
{
result = @builder.range_exclusive(val[0], val[1], nil)
}
| p_variable
| p_var_ref
| p_expr_ref
| p_const
| tBDOT2 p_primitive
{
Expand Down Expand Up @@ -2233,6 +2234,12 @@ opt_block_args_tail:
result = @builder.pin(val[0], lvar)
}

p_expr_ref: tCARET tLPAREN expr_value tRPAREN
{
expr = @builder.begin(val[1], val[2], val[3])
result = @builder.pin(val[0], expr)
}

p_const: tCOLON3 cname
{
result = @builder.const_global(val[0], val[1])
Expand Down
49 changes: 49 additions & 0 deletions test/test_parser.rb
Expand Up @@ -10171,4 +10171,53 @@ def test_parser_drops_truncated_parts_of_squiggly_heredoc
%q{},
SINCE_2_3)
end

def test_pin_expr
assert_parses_pattern_match(
s(:in_pattern,
s(:pin,
s(:begin,
s(:int, 42))), nil,
s(:nil)),
%q{in ^(42) then nil},
%q{ ~ selector (in_pattern.pin)
| ~~~~~ expression (in_pattern.pin)
| ~ begin (in_pattern.pin.begin)
| ~ end (in_pattern.pin.begin)
| ~~~~ expression (in_pattern.pin.begin)},
SINCE_3_1)

assert_parses_pattern_match(
s(:in_pattern,
s(:hash_pattern,
s(:pair,
s(:sym, :foo),
s(:pin,
s(:begin,
s(:int, 42))))), nil,
s(:nil)),
%q{in { foo: ^(42) } then nil},
%q{ ~ selector (in_pattern.hash_pattern.pair.pin)
| ~~~~~ expression (in_pattern.hash_pattern.pair.pin)
| ~ begin (in_pattern.hash_pattern.pair.pin.begin)
| ~ end (in_pattern.hash_pattern.pair.pin.begin)
| ~~~~ expression (in_pattern.hash_pattern.pair.pin.begin)},
SINCE_3_1)

assert_parses_pattern_match(
s(:in_pattern,
s(:pin,
s(:begin,
s(:send,
s(:int, 0), :+,
s(:int, 0)))), nil,
s(:nil)),
%q{in ^(0+0) then nil},
%q{ ~ selector (in_pattern.pin)
| ~~~~~~ expression (in_pattern.pin)
| ~ begin (in_pattern.pin.begin)
| ~ end (in_pattern.pin.begin)
| ~~~~~ expression (in_pattern.pin.begin)},
SINCE_3_1)
end
end