ruby_parser 2.3.1 → 3.0.0.a1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ruby_parser might be problematic. Click here for more details.

@@ -3,6 +3,7 @@
3
3
  ENV['VERBOSE'] = "1"
4
4
 
5
5
  require 'rubygems'
6
+ gem "minitest"
6
7
  require 'minitest/autorun'
7
8
  require 'ruby_parser'
8
9
 
@@ -10,13 +11,21 @@ $: << File.expand_path('~/Work/p4/zss/src/ParseTree/dev/test')
10
11
 
11
12
  require 'pt_testcase'
12
13
 
13
- class RubyParser
14
+ class Ruby18Parser # FIX
15
+ def process input
16
+ parse input
17
+ end
18
+ end
19
+
20
+ class Ruby19Parser
14
21
  def process input
15
22
  parse input
16
23
  end
17
24
  end
18
25
 
19
26
  class RubyParserTestCase < ParseTreeTestCase
27
+ attr_accessor :result, :processor
28
+
20
29
  def self.previous key
21
30
  "Ruby"
22
31
  end
@@ -29,16 +38,6 @@ class RubyParserTestCase < ParseTreeTestCase
29
38
 
30
39
  super
31
40
  end
32
- end
33
-
34
- class TestRubyParser < RubyParserTestCase
35
- attr_accessor :result, :processor
36
-
37
- def setup
38
- super
39
-
40
- self.processor = RubyParser.new
41
- end
42
41
 
43
42
  def assert_parse rb, pt
44
43
  self.result = processor.parse rb
@@ -49,17 +48,18 @@ class TestRubyParser < RubyParserTestCase
49
48
  assert_parse rb, pt
50
49
  assert_equal line, result.line, "call should have line number"
51
50
  end
51
+ end
52
52
 
53
+ module TestRubyParser
53
54
  def test_attrasgn_array_lhs
54
55
  rb = '[1, 2, 3, 4][from .. to] = ["a", "b", "c"]'
55
56
  pt = s(:attrasgn,
56
57
  s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3), s(:lit, 4)),
57
58
  :[]=,
58
- s(:arglist,
59
- s(:dot2,
60
- s(:call, nil, :from, s(:arglist)),
61
- s(:call, nil, :to, s(:arglist))),
62
- s(:array, s(:str, "a"), s(:str, "b"), s(:str, "c"))))
59
+ s(:dot2,
60
+ s(:call, nil, :from),
61
+ s(:call, nil, :to)),
62
+ s(:array, s(:str, "a"), s(:str, "b"), s(:str, "c")))
63
63
 
64
64
  assert_parse rb, pt
65
65
  end
@@ -100,18 +100,25 @@ class TestRubyParser < RubyParserTestCase
100
100
  end
101
101
 
102
102
  def test_block_append_tail_block
103
- head = s(:call, nil, :f1, s(:arglist))
103
+ head = s(:call, nil, :f1)
104
104
  tail = s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y)))
105
105
  expected = s(:block,
106
- s(:call, nil, :f1, s(:arglist)),
106
+ s(:call, nil, :f1),
107
107
  s(:block, s(:undef, s(:lit, :x)), s(:undef, s(:lit, :y))))
108
108
  assert_equal expected, processor.block_append(head, tail)
109
109
  end
110
110
 
111
+ def test_call_array_arg
112
+ rb = "1 == [:b, :c]"
113
+ pt = s(:call, s(:lit, 1), :==, s(:array, s(:lit, :b), s(:lit, :c)))
114
+
115
+ assert_parse rb, pt
116
+ end
117
+
111
118
  def test_call_env
112
119
  processor.env[:a] = :lvar
113
120
  rb = "a.happy"
114
- pt = s(:call, s(:lvar, :a), :happy, s(:arglist))
121
+ pt = s(:call, s(:lvar, :a), :happy)
115
122
 
116
123
  assert_parse rb, pt
117
124
  end
@@ -119,7 +126,7 @@ class TestRubyParser < RubyParserTestCase
119
126
  def test_dasgn_icky2
120
127
  rb = "a do\n v = nil\n begin\n yield\n rescue Exception => v\n break\n end\nend"
121
128
  pt = s(:iter,
122
- s(:call, nil, :a, s(:arglist)),
129
+ s(:call, nil, :a),
123
130
  nil,
124
131
  s(:block,
125
132
  s(:lasgn, :v, s(:nil)),
@@ -135,29 +142,27 @@ class TestRubyParser < RubyParserTestCase
135
142
  def test_class_comments
136
143
  rb = "# blah 1\n# blah 2\n\nclass X\n # blah 3\n def blah\n # blah 4\n end\nend"
137
144
  pt = s(:class, :X, nil,
138
- s(:scope,
139
- s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
145
+ s(:defn, :blah, s(:args), s(:nil)))
140
146
 
141
147
  assert_parse rb, pt
142
148
 
143
149
  assert_equal "# blah 1\n# blah 2\n\n", result.comments
144
- assert_equal "# blah 3\n", result.scope.defn.comments
150
+ assert_equal "# blah 3\n", result.defn.comments
145
151
  end
146
152
 
147
153
  def test_module_comments
148
154
  rb = "# blah 1\n \n # blah 2\n\nmodule X\n # blah 3\n def blah\n # blah 4\n end\nend"
149
155
  pt = s(:module, :X,
150
- s(:scope,
151
- s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))))
156
+ s(:defn, :blah, s(:args), s(:nil)))
152
157
 
153
158
  assert_parse rb, pt
154
159
  assert_equal "# blah 1\n\n# blah 2\n\n", result.comments
155
- assert_equal "# blah 3\n", result.scope.defn.comments
160
+ assert_equal "# blah 3\n", result.defn.comments
156
161
  end
157
162
 
158
163
  def test_defn_comments
159
164
  rb = "# blah 1\n# blah 2\n\ndef blah\nend"
160
- pt = s(:defn, :blah, s(:args), s(:scope, s(:block, s(:nil))))
165
+ pt = s(:defn, :blah, s(:args), s(:nil))
161
166
 
162
167
  assert_parse rb, pt
163
168
  assert_equal "# blah 1\n# blah 2\n\n", result.comments
@@ -165,7 +170,7 @@ class TestRubyParser < RubyParserTestCase
165
170
 
166
171
  def test_defs_comments
167
172
  rb = "# blah 1\n# blah 2\n\ndef self.blah\nend"
168
- pt = s(:defs, s(:self), :blah, s(:args), s(:scope, s(:block)))
173
+ pt = s(:defs, s(:self), :blah, s(:args))
169
174
 
170
175
  assert_parse rb, pt
171
176
  assert_equal "# blah 1\n# blah 2\n\n", result.comments
@@ -174,9 +179,9 @@ class TestRubyParser < RubyParserTestCase
174
179
  def test_do_bug # TODO: rename
175
180
  rb = "a 1\na.b do |c|\n # do nothing\nend"
176
181
  pt = s(:block,
177
- s(:call, nil, :a, s(:arglist, s(:lit, 1))),
182
+ s(:call, nil, :a, s(:lit, 1)),
178
183
  s(:iter,
179
- s(:call, s(:call, nil, :a, s(:arglist)), :b, s(:arglist)),
184
+ s(:call, s(:call, nil, :a), :b),
180
185
  s(:lasgn, :c)))
181
186
 
182
187
  assert_parse rb, pt
@@ -193,7 +198,7 @@ class TestRubyParser < RubyParserTestCase
193
198
 
194
199
  def test_bug_call_arglist_parens
195
200
  rb = 'g ( 1), 2'
196
- pt = s(:call, nil, :g, s(:arglist, s(:lit, 1), s(:lit, 2)))
201
+ pt = s(:call, nil, :g, s(:lit, 1), s(:lit, 2))
197
202
 
198
203
  assert_parse rb, pt
199
204
 
@@ -204,11 +209,7 @@ class TestRubyParser < RubyParserTestCase
204
209
  CODE
205
210
 
206
211
  pt = s(:defn, :f, s(:args),
207
- s(:scope,
208
- s(:block,
209
- s(:call, nil, :g,
210
- s(:arglist,
211
- s(:lit, 1), s(:lit, 2))))))
212
+ s(:call, nil, :g, s(:lit, 1), s(:lit, 2)))
212
213
 
213
214
  assert_parse rb, pt
214
215
 
@@ -223,7 +224,7 @@ class TestRubyParser < RubyParserTestCase
223
224
 
224
225
  def test_dstr_evstr
225
226
  rb = "\"#\{'a'}#\{b}\""
226
- pt = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
227
+ pt = s(:dstr, "a", s(:evstr, s(:call, nil, :b)))
227
228
 
228
229
  assert_parse rb, pt
229
230
  end
@@ -244,14 +245,14 @@ class TestRubyParser < RubyParserTestCase
244
245
 
245
246
  def test_evstr_evstr
246
247
  rb = "\"#\{a}#\{b}\""
247
- pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:evstr, s(:call, nil, :b, s(:arglist))))
248
+ pt = s(:dstr, "", s(:evstr, s(:call, nil, :a)), s(:evstr, s(:call, nil, :b)))
248
249
 
249
250
  assert_parse rb, pt
250
251
  end
251
252
 
252
253
  def test_evstr_str
253
254
  rb = "\"#\{a} b\""
254
- pt = s(:dstr, "", s(:evstr, s(:call, nil, :a, s(:arglist))), s(:str, " b"))
255
+ pt = s(:dstr, "", s(:evstr, s(:call, nil, :a)), s(:str, " b"))
255
256
 
256
257
  assert_parse rb, pt
257
258
  end
@@ -308,16 +309,16 @@ class TestRubyParser < RubyParserTestCase
308
309
 
309
310
  def test_literal_concat_dstr_dstr
310
311
  lhs = s(:dstr, "Failed to download spec ",
311
- s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
312
+ s(:evstr, s(:call, nil, :spec_name)),
312
313
  s(:str, " from "),
313
- s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
314
+ s(:evstr, s(:call, nil, :source_uri)),
314
315
  s(:str, ":\n"))
315
316
  rhs = s(:dstr, "\t",
316
317
  s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
317
318
  expected = s(:dstr, "Failed to download spec ",
318
- s(:evstr, s(:call, nil, :spec_name, s(:arglist))),
319
+ s(:evstr, s(:call, nil, :spec_name)),
319
320
  s(:str, " from "),
320
- s(:evstr, s(:call, nil, :source_uri, s(:arglist))),
321
+ s(:evstr, s(:call, nil, :source_uri)),
321
322
  s(:str, ":\n"),
322
323
  s(:str, "\t"),
323
324
  s(:evstr, s(:call, s(:ivar, :@fetch_error), :message)))
@@ -326,8 +327,8 @@ class TestRubyParser < RubyParserTestCase
326
327
  end
327
328
 
328
329
  def test_literal_concat_dstr_evstr
329
- lhs, rhs = s(:dstr, "a"), s(:evstr, s(:call, nil, :b, s(:arglist)))
330
- expected = s(:dstr, "a", s(:evstr, s(:call, nil, :b, s(:arglist))))
330
+ lhs, rhs = s(:dstr, "a"), s(:evstr, s(:call, nil, :b))
331
+ expected = s(:dstr, "a", s(:evstr, s(:call, nil, :b)))
331
332
 
332
333
  assert_equal expected, processor.literal_concat(lhs, rhs)
333
334
  end
@@ -392,11 +393,11 @@ class TestRubyParser < RubyParserTestCase
392
393
  end
393
394
 
394
395
  def test_logop_nested_mix
395
- lhs = s(:or, s(:call, nil, :a, s(:arglist)), s(:call, nil, :b, s(:arglist)))
396
- rhs = s(:and, s(:call, nil, :c, s(:arglist)), s(:call, nil, :d, s(:arglist)))
396
+ lhs = s(:or, s(:call, nil, :a), s(:call, nil, :b))
397
+ rhs = s(:and, s(:call, nil, :c), s(:call, nil, :d))
397
398
  exp = s(:or,
398
- s(:or, s(:call, nil, :a, s(:arglist)), s(:call, nil, :b, s(:arglist))),
399
- s(:and, s(:call, nil, :c, s(:arglist)), s(:call, nil, :d, s(:arglist))))
399
+ s(:or, s(:call, nil, :a), s(:call, nil, :b)),
400
+ s(:and, s(:call, nil, :c), s(:call, nil, :d)))
400
401
 
401
402
  lhs.paren = true
402
403
  rhs.paren = true
@@ -406,7 +407,7 @@ class TestRubyParser < RubyParserTestCase
406
407
 
407
408
  def test_str_evstr
408
409
  rb = "\"a #\{b}\""
409
- pt = s(:dstr, "a ", s(:evstr, s(:call, nil, :b, s(:arglist))))
410
+ pt = s(:dstr, "a ", s(:evstr, s(:call, nil, :b)))
410
411
 
411
412
  assert_parse rb, pt
412
413
  end
@@ -439,7 +440,7 @@ class TestRubyParser < RubyParserTestCase
439
440
 
440
441
  def test_str_pct_Q_nested
441
442
  rb = "%Q[before [#\{nest}] after]"
442
- pt = s(:dstr, "before [", s(:evstr, s(:call, nil, :nest, s(:arglist))), s(:str, "] after"))
443
+ pt = s(:dstr, "before [", s(:evstr, s(:call, nil, :nest)), s(:str, "] after"))
443
444
 
444
445
  assert_parse rb, pt
445
446
  end
@@ -493,7 +494,7 @@ class TestRubyParser < RubyParserTestCase
493
494
  rb = "a = 42\np a"
494
495
  pt = s(:block,
495
496
  s(:lasgn, :a, s(:lit, 42)),
496
- s(:call, nil, :p, s(:arglist, s(:lvar, :a))))
497
+ s(:call, nil, :p, s(:lvar, :a)))
497
498
 
498
499
  assert_parse_line rb, pt, 1
499
500
  assert_equal 1, result.lasgn.line, "lasgn should have line number"
@@ -512,9 +513,9 @@ class TestRubyParser < RubyParserTestCase
512
513
  rb = "f do |x, y|\n x + y\nend"
513
514
 
514
515
  pt = s(:iter,
515
- s(:call, nil, :f, s(:arglist)),
516
+ s(:call, nil, :f),
516
517
  s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
517
- s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))
518
+ s(:call, s(:lvar, :x), :+, s(:lvar, :y)))
518
519
 
519
520
  assert_parse_line rb, pt, 1
520
521
  assert_equal 1, result[1].line, "call should have line number"
@@ -523,7 +524,7 @@ class TestRubyParser < RubyParserTestCase
523
524
  end
524
525
 
525
526
  def test_parse_line_defn_no_parens
526
- pt = s(:defn, :f, s(:args), s(:scope, s(:block, s(:nil))))
527
+ pt = s(:defn, :f, s(:args), s(:nil))
527
528
 
528
529
  rb = "def f\nend"
529
530
  assert_parse_line rb, pt, 1
@@ -535,16 +536,13 @@ class TestRubyParser < RubyParserTestCase
535
536
  def test_parse_line_defn_complex
536
537
  rb = "def x(y)\n p(y)\n y *= 2\n return y;\nend" # TODO: remove () & ;
537
538
  pt = s(:defn, :x, s(:args, :y),
538
- s(:scope,
539
- s(:block,
540
- s(:call, nil, :p, s(:arglist, s(:lvar, :y))),
541
- s(:lasgn, :y,
542
- s(:call, s(:lvar, :y), :*, s(:arglist, s(:lit, 2)))),
543
- s(:return, s(:lvar, :y)))))
539
+ s(:call, nil, :p, s(:lvar, :y)),
540
+ s(:lasgn, :y, s(:call, s(:lvar, :y), :*, s(:lit, 2))),
541
+ s(:return, s(:lvar, :y)))
544
542
 
545
543
  assert_parse_line rb, pt, 1
546
544
 
547
- body = result.scope.block
545
+ body = result
548
546
  assert_equal 2, body.call.line, "call should have line number"
549
547
  assert_equal 3, body.lasgn.line, "lasgn should have line number"
550
548
  assert_equal 4, body.return.line, "return should have line number"
@@ -554,9 +552,9 @@ class TestRubyParser < RubyParserTestCase
554
552
  rb = "f(a) do |x, y|\n x + y\nend"
555
553
 
556
554
  pt = s(:iter,
557
- s(:call, nil, :f, s(:arglist, s(:call, nil, :a, s(:arglist)))),
555
+ s(:call, nil, :f, s(:call, nil, :a)),
558
556
  s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
559
- s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))
557
+ s(:call, s(:lvar, :x), :+, s(:lvar, :y)))
560
558
 
561
559
  assert_parse_line rb, pt, 1
562
560
 
@@ -569,9 +567,9 @@ class TestRubyParser < RubyParserTestCase
569
567
  rb = "f a do |x, y|\n x + y\nend"
570
568
 
571
569
  pt = s(:iter,
572
- s(:call, nil, :f, s(:arglist, s(:call, nil, :a, s(:arglist)))),
570
+ s(:call, nil, :f, s(:call, nil, :a)),
573
571
  s(:masgn, s(:array, s(:lasgn, :x), s(:lasgn, :y))),
574
- s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))
572
+ s(:call, s(:lvar, :x), :+, s(:lvar, :y)))
575
573
 
576
574
  assert_parse_line rb, pt, 1
577
575
 
@@ -610,23 +608,20 @@ class TestRubyParser < RubyParserTestCase
610
608
  RUBY
611
609
 
612
610
  pt = s(:defn, :blah, s(:args),
613
- s(:scope,
614
- s(:block,
615
- s(:if,
616
- s(:true),
617
- s(:return, s(:lit, 42)),
618
- nil))))
611
+ s(:if, s(:true),
612
+ s(:return, s(:lit, 42)),
613
+ nil))
619
614
 
620
615
  assert_parse_line rb, pt, 1
621
616
 
622
- assert_equal 3, result.scope.block.if.return.line
623
- assert_equal 3, result.scope.block.if.return.lit.line
617
+ assert_equal 3, result.if.return.line
618
+ assert_equal 3, result.if.return.lit.line
624
619
  end
625
620
 
626
621
  def test_parse_if_not_canonical
627
622
  rb = "if not var.nil? then 'foo' else 'bar'\nend"
628
623
  pt = s(:if,
629
- s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)),
624
+ s(:call, s(:call, nil, :var), :nil?),
630
625
  s(:str, "bar"),
631
626
  s(:str, "foo"))
632
627
 
@@ -637,7 +632,7 @@ class TestRubyParser < RubyParserTestCase
637
632
  rb = "if not var.nil? then 'foo' else 'bar'\nend"
638
633
  pt = s(:if,
639
634
  s(:not,
640
- s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))),
635
+ s(:call, s(:call, nil, :var), :nil?)),
641
636
  s(:str, "foo"),
642
637
  s(:str, "bar"))
643
638
 
@@ -649,7 +644,7 @@ class TestRubyParser < RubyParserTestCase
649
644
  def test_parse_while_not_canonical
650
645
  rb = "while not var.nil?\n 'foo'\nend"
651
646
  pt = s(:until,
652
- s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)),
647
+ s(:call, s(:call, nil, :var), :nil?),
653
648
  s(:str, "foo"), true)
654
649
 
655
650
  assert_parse rb, pt
@@ -659,7 +654,7 @@ class TestRubyParser < RubyParserTestCase
659
654
  rb = "while not var.nil?\n 'foo'\nend"
660
655
  pt = s(:while,
661
656
  s(:not,
662
- s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))),
657
+ s(:call, s(:call, nil, :var), :nil?)),
663
658
  s(:str, "foo"), true)
664
659
 
665
660
  processor.canonicalize_conditions = false
@@ -671,7 +666,7 @@ class TestRubyParser < RubyParserTestCase
671
666
  rb = "until not var.nil?\n 'foo'\nend"
672
667
 
673
668
  pt = s(:while,
674
- s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist)),
669
+ s(:call, s(:call, nil, :var), :nil?),
675
670
  s(:str, "foo"), true)
676
671
 
677
672
  assert_parse rb, pt
@@ -681,7 +676,7 @@ class TestRubyParser < RubyParserTestCase
681
676
  rb = "until not var.nil?\n 'foo'\nend"
682
677
  pt = s(:until,
683
678
  s(:not,
684
- s(:call, s(:call, nil, :var, s(:arglist)), :nil?, s(:arglist))),
679
+ s(:call, s(:call, nil, :var), :nil?)),
685
680
  s(:str, "foo"), true)
686
681
 
687
682
  processor.canonicalize_conditions = false
@@ -689,3 +684,62 @@ class TestRubyParser < RubyParserTestCase
689
684
  assert_parse rb, pt
690
685
  end
691
686
  end
687
+
688
+ class TestRuby18Parser < RubyParserTestCase
689
+ include TestRubyParser
690
+
691
+ def setup
692
+ super
693
+
694
+ self.processor = Ruby18Parser.new
695
+ end
696
+
697
+ def test_flip2_env_lvar
698
+ rb = "if a..b then end"
699
+ pt = s(:if, s(:flip2, s(:call, nil, :a), s(:call, nil, :b)), nil, nil)
700
+
701
+ assert_parse rb, pt
702
+
703
+ top_env = processor.env.env.first
704
+
705
+ assert_kind_of Hash, top_env
706
+
707
+ flip = top_env.find { |k,v| k =~ /^flip/ }
708
+
709
+ assert flip
710
+ assert_equal :lvar, flip.last
711
+ end
712
+ end
713
+
714
+ class TestRuby19Parser < RubyParserTestCase
715
+ include TestRubyParser
716
+
717
+ def setup
718
+ super
719
+
720
+ self.processor = Ruby19Parser.new
721
+ end
722
+
723
+ # HACK: need to figure out the desired structure and get this working
724
+ # def test_wtf
725
+ # # lambda -> f_larglist lambda_body
726
+ # # f_larglist -> f_args opt_bv_decl
727
+ # # opt_bv_decl
728
+ # # bv_decls
729
+ # # bvar
730
+ #
731
+ # rb = "->(a, b=nil) { p [a, b] }"
732
+ # pt = s(:iter,
733
+ # s(:call, nil, :lambda),
734
+ # s(:args, :a, :b,
735
+ # s(:block, s(:lasgn, :b, s(nil)))),
736
+ # s(:call, nil, :p, s(:array, s(:lvar, :a), s(:lvar, :b))))
737
+ #
738
+ # assert_parse rb, pt
739
+ #
740
+ # rb = "->(a; b) { p [a, b] }"
741
+ #
742
+ # assert_parse rb, pt
743
+ # end
744
+ end
745
+