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: Allow omission of parentheses in one line pattern matching #816

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
4 changes: 2 additions & 2 deletions lib/parser/ruby31.y
Expand Up @@ -371,7 +371,7 @@ rule
result = @lexer.in_kwarg
@lexer.in_kwarg = true
}
p_expr
p_top_expr_body
{
@pattern_variables.pop
@lexer.in_kwarg = val[2]
Expand All @@ -386,7 +386,7 @@ rule
result = @lexer.in_kwarg
@lexer.in_kwarg = true
}
p_expr
p_top_expr_body
{
@pattern_variables.pop
@lexer.in_kwarg = val[2]
Expand Down
66 changes: 64 additions & 2 deletions test/test_parser.rb
Expand Up @@ -9614,6 +9614,68 @@ def test_pattern_matching_single_line
SINCE_3_0)
end

def test_pattern_matching_single_line_allowed_omission_of_parentheses
assert_parses(
s(:begin,
s(:match_pattern,
s(:array,
s(:int, 1),
s(:int, 2)),
s(:array_pattern,
s(:match_var, :a),
s(:match_var, :b))),
s(:lvar, :a)),
%q{[1, 2] => a, b; a},
%q{~~~~~~~~~~~~~~ expression (match_pattern)
| ~~ operator (match_pattern)},
SINCE_3_1)

assert_parses(
s(:begin,
s(:match_pattern,
s(:hash,
s(:pair,
s(:sym, :a),
s(:int, 1))),
s(:hash_pattern,
s(:match_var, :a))),
s(:lvar, :a)),
%q{{a: 1} => a:; a},
%q{~~~~~~~~~~~~ expression (match_pattern)
| ~~ operator (match_pattern)},
SINCE_3_1)

assert_parses(
s(:begin,
s(:match_pattern_p,
s(:array,
s(:int, 1),
s(:int, 2)),
s(:array_pattern,
s(:match_var, :a),
s(:match_var, :b))),
s(:lvar, :a)),
%q{[1, 2] in a, b; a},
%q{~~~~~~~~~~~~~~ expression (match_pattern_p)
| ~~ operator (match_pattern_p)},
SINCE_3_1)

assert_parses(
s(:begin,
s(:match_pattern_p,
s(:hash,
s(:pair,
s(:sym, :a),
s(:int, 1))),
s(:hash_pattern,
s(:match_var, :a))),
s(:lvar, :a)),
%q{{a: 1} in a:; a},
%q{~~~~~~~~~~~~ expression (match_pattern_p)
| ~~ operator (match_pattern_p)},
SINCE_3_1)
end

def test_ruby_bug_pattern_matching_restore_in_kwarg_flag
refute_diagnoses(
"p(({} in {a:}), a:\n 1)",
Expand Down Expand Up @@ -9703,7 +9765,7 @@ def test_pattern_matching_required_parentheses_for_in_match
[:error, :unexpected_token, { :token => 'tCOMMA' }],
%{1 => a, b},
%{ ^ location},
SINCE_3_0)
%w(3.0))

assert_diagnoses(
[:error, :unexpected_token, { :token => 'tASSOC' }],
Expand All @@ -9715,7 +9777,7 @@ def test_pattern_matching_required_parentheses_for_in_match
[:error, :unexpected_token, { :token => 'tLABEL' }],
%{1 => a:},
%{ ^^ location},
SINCE_3_0)
%w(3.0))
end

def test_pattern_matching_required_bound_variable_before_pin
Expand Down