Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

+ 1.9: reworked mlhs and family. Now horrible things like 'a, *, c = …

…f' work

[git-p4: depot-paths = "//src/ruby_parser/dev/": change = 7584]
  • Loading branch information...
commit b207df1927f4289f06449862a93f30a8693faf6f 1 parent 5824824
@zenspider zenspider authored
Showing with 111 additions and 4 deletions.
  1. +37 −4 lib/ruby19_parser.y
  2. +74 −0 test/test_ruby_parser.rb
View
41 lib/ruby19_parser.y
@@ -322,13 +322,13 @@ rule
}
mlhs: mlhs_basic
- | tLPAREN mlhs_entry rparen
+ | tLPAREN mlhs_inner rparen
{
result = val[1]
}
- mlhs_entry: mlhs_basic
- | tLPAREN mlhs_entry rparen
+ mlhs_inner: mlhs_basic
+ | tLPAREN mlhs_inner rparen
{
result = s(:masgn, s(:array, val[1]))
}
@@ -345,21 +345,45 @@ rule
{
result = s(:masgn, val[0] << s(:splat, val[2]))
}
+ | mlhs_head tSTAR mlhs_node tCOMMA mlhs_post
+ {
+ ary = list_append val[0], s(:splat, val[2])
+ ary.concat val[4][1..-1]
+ result = s(:masgn, ary)
+ }
| mlhs_head tSTAR
{
result = s(:masgn, val[0] << s(:splat))
}
+ | mlhs_head tSTAR tCOMMA mlhs_post
+ {
+ ary = list_append val[0], s(:splat)
+ ary.concat val[3][1..-1]
+ result = s(:masgn, ary)
+ }
| tSTAR mlhs_node
{
result = s(:masgn, s(:array, s(:splat, val[1])))
}
+ | tSTAR mlhs_node tCOMMA mlhs_post
+ {
+ ary = s(:array, s(:splat, val[1]))
+ ary.concat val[3][1..-1]
+ result = s(:masgn, ary)
+ }
| tSTAR
{
result = s(:masgn, s(:array, s(:splat)))
}
+ | tSTAR tCOMMA mlhs_post
+ {
+ ary = s(:array, s(:splat))
+ ary.concat val[2][1..-1]
+ result = s(:masgn, ary)
+ }
mlhs_item: mlhs_node
- | tLPAREN mlhs_entry rparen
+ | tLPAREN mlhs_inner rparen
{
result = val[1]
}
@@ -373,6 +397,15 @@ rule
result = val[0] << val[1].compact
}
+ mlhs_post: mlhs_item
+ {
+ result = s(:array, val[0])
+ }
+ | mlhs_post tCOMMA mlhs_item
+ {
+ result = list_append val[0], val[2]
+ }
+
mlhs_node: user_variable
{
result = self.assignable val[0]
View
74 test/test_ruby_parser.rb
@@ -766,6 +766,80 @@ def setup
self.processor = Ruby19Parser.new
end
+ def test_mlhs_back_splat
+ rb = "a, b, c, *s = f"
+ pt = s(:masgn,
+ s(:array,
+ s(:lasgn, :a), s(:lasgn, :b), s(:lasgn, :c),
+ s(:splat, s(:lasgn, :s))),
+ s(:to_ary, s(:call, nil, :f)))
+
+ assert_parse rb, pt
+ end
+
+ def test_mlhs_back_anonsplat
+ rb = "a, b, c, * = f"
+ pt = s(:masgn,
+ s(:array,
+ s(:lasgn, :a), s(:lasgn, :b), s(:lasgn, :c),
+ s(:splat)),
+ s(:to_ary, s(:call, nil, :f)))
+
+ assert_parse rb, pt
+ end
+
+ def test_mlhs_mid_splat
+ rb = "a, b, c, *s, x, y, z = f"
+ pt = s(:masgn,
+ s(:array,
+ s(:lasgn, :a), s(:lasgn, :b), s(:lasgn, :c),
+ s(:splat, s(:lasgn, :s)),
+ s(:lasgn, :x), s(:lasgn, :y), s(:lasgn, :z)),
+ s(:to_ary, s(:call, nil, :f)))
+
+ assert_parse rb, pt
+ end
+
+ def test_mlhs_mid_anonsplat
+ rb = "a, b, c, *, x, y, z = f"
+ pt = s(:masgn,
+ s(:array, s(:lasgn, :a), s(:splat), s(:lasgn, :z)),
+ s(:to_ary, s(:call, nil, :f)))
+ pt = s(:masgn,
+ s(:array,
+ s(:lasgn, :a), s(:lasgn, :b), s(:lasgn, :c),
+ s(:splat),
+ s(:lasgn, :x), s(:lasgn, :y), s(:lasgn, :z)),
+ s(:to_ary, s(:call, nil, :f)))
+
+ assert_parse rb, pt
+ end
+
+ def test_mlhs_front_splat
+ rb = "*s, x, y, z = f"
+ pt = s(:masgn,
+ s(:array, s(:splat, s(:lasgn, :s)), s(:lasgn, :z)),
+ s(:to_ary, s(:call, nil, :f)))
+ pt = s(:masgn,
+ s(:array,
+ s(:splat, s(:lasgn, :s)),
+ s(:lasgn, :x), s(:lasgn, :y), s(:lasgn, :z)),
+ s(:to_ary, s(:call, nil, :f)))
+
+ assert_parse rb, pt
+ end
+
+ def test_mlhs_front_anonsplat
+ rb = "*, x, y, z = f"
+ pt = s(:masgn,
+ s(:array,
+ s(:splat),
+ s(:lasgn, :x), s(:lasgn, :y), s(:lasgn, :z)),
+ s(:to_ary, s(:call, nil, :f)))
+
+ assert_parse rb, pt
+ end
+
def test_expr_not
rb = "!(42)"
pt = s(:call, s(:lit, 42), :"!")
Please sign in to comment.
Something went wrong with that request. Please try again.