Skip to content

Commit

Permalink
+ ruby27.y: allow newlines inside braced pattern. (#664)
Browse files Browse the repository at this point in the history
This commit tracks upstream commits ruby/ruby@f5c904c and ruby/ruby@c8d0bf0
that have been cherry-picked in 2.7.1 (ruby/ruby@004c298 and ruby/ruby@44f7e38)
  • Loading branch information
iliabylich committed Apr 3, 2020
1 parent 900a652 commit 0544dd3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/parser/ruby27.y
Expand Up @@ -1877,13 +1877,16 @@ opt_block_args_tail:
| tLBRACE
{
@pattern_hash_keys.push
result = @lexer.in_kwarg
@lexer.in_kwarg = false
}
p_kwargs tRCURLY
p_kwargs rbrace
{
@pattern_hash_keys.pop
@lexer.in_kwarg = val[1]
result = @builder.hash_pattern(val[0], val[2], val[3])
}
| tLBRACE tRCURLY
| tLBRACE rbrace
{
result = @builder.hash_pattern(val[0], [], val[1])
}
Expand Down Expand Up @@ -2902,6 +2905,10 @@ keyword_variable: kNIL
{
result = val[1]
}
rbrace: opt_nl tRCURLY
{
result = val[1]
}
trailer: | tNL | tCOMMA
term: tSEMI
Expand Down
61 changes: 61 additions & 0 deletions test/test_parser.rb
Expand Up @@ -8758,6 +8758,67 @@ def test_pattern_matching_hash
%q{in a: 1, _a:, ** then true},
%q{ ~~~~~~~~~~~~~ expression (in_pattern.hash_pattern)}
)

assert_parses_pattern_match(
s(:in_pattern,
s(:hash_pattern,
s(:pair,
s(:sym, :a),
s(:int, 1))), nil,
s(:false)),
%q{
in {a: 1
}
false
},
%q{}
)


assert_parses_pattern_match(
s(:in_pattern,
s(:hash_pattern,
s(:pair,
s(:sym, :a),
s(:int, 2))), nil,
s(:false)),
%q{
in {a:
2}
false
},
%q{}
)

assert_parses_pattern_match(
s(:in_pattern,
s(:hash_pattern,
s(:pair,
s(:sym, :a),
s(:hash_pattern,
s(:match_var, :b))),
s(:match_var, :c)), nil,
s(:send, nil, :p,
s(:lvar, :c))),
%q{
in a: {b:}, c:
p c
},
%q{}
)

assert_parses_pattern_match(
s(:in_pattern,
s(:hash_pattern,
s(:match_var, :a)), nil,
s(:true)),
%q{
in {a:
}
true
},
%q{}
)
end

def test_pattern_matching_hash_with_string_keys
Expand Down

0 comments on commit 0544dd3

Please sign in to comment.