ParseTree 1.6.1 → 1.6.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ *** 1.6.2 / 2006-12-19
2
+
3
+ + 2 minor enhancements:
4
+ + ParseTree::translate can now take :"self.method".
5
+ * Improved PT tests for [], []=
6
+ + 1 bug fixes:
7
+ * Fixed a bug in ParseTree where cases w/o exprs were malformed.
8
+
1
9
  *** 1.6.1 / 2006-11-11
2
10
 
3
11
  + 2 minor enhancements:
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ Hoe.new("ParseTree", ParseTree::VERSION) do |p|
12
12
  p.changes = p.paragraphs_of("History.txt", 1).join("\n\n")
13
13
  p.clean_globs << File.expand_path("~/.ruby_inline")
14
14
  p.extra_deps << ['RubyInline', '>= 3.2.0']
15
- p.spec_extras[:require_paths] = proc { |paths| paths << 'test'; p paths }
15
+ p.spec_extras[:require_paths] = proc { |paths| paths << 'test' }
16
16
  end
17
17
 
18
18
  desc 'Run against ruby 1.9 (from a multiruby install) with -d.'
data/lib/parse_tree.rb CHANGED
@@ -25,10 +25,10 @@ require 'inline'
25
25
 
26
26
  class ParseTree
27
27
 
28
- VERSION = '1.6.1'
28
+ VERSION = '1.6.2'
29
29
 
30
30
  ##
31
- # Front end translation method.
31
+ # Front end translation method.
32
32
 
33
33
  def self.translate(klass_or_str, method=nil)
34
34
  pt = self.new(false)
@@ -37,7 +37,12 @@ class ParseTree
37
37
  pt.parse_tree_for_string(klass_or_str).first
38
38
  else
39
39
  unless method.nil? then
40
- pt.parse_tree_for_method(klass_or_str, method)
40
+ if method.to_s =~ /^self\./ then
41
+ method = method.to_s[5..-1].intern
42
+ pt.parse_tree_for_method(klass_or_str, method, true)
43
+ else
44
+ pt.parse_tree_for_method(klass_or_str, method)
45
+ end
41
46
  else
42
47
  pt.parse_tree(klass_or_str).first
43
48
  end
@@ -245,6 +250,7 @@ class ParseTree
245
250
 
246
251
  builder.prefix %{
247
252
  #define nd_3rd u3.node
253
+ static VALUE current_case = Qnil;
248
254
  }
249
255
 
250
256
  builder.prefix %{
@@ -372,12 +378,17 @@ again_no_block:
372
378
  break;
373
379
 
374
380
  case NODE_CASE:
375
- add_to_parse_tree(current, node->nd_head, newlines, locals); /* expr */
381
+ current_case = current;
382
+ if (node->nd_head != NULL) {
383
+ add_to_parse_tree(current, node->nd_head, newlines, locals); /* expr */
384
+ } else {
385
+ rb_ary_push(current, Qnil);
386
+ }
376
387
  node = node->nd_body;
377
388
  while (node) {
378
389
  add_to_parse_tree(current, node, newlines, locals);
379
390
  if (nd_type(node) == NODE_WHEN) { /* when */
380
- node = node->nd_next;
391
+ node = node->nd_next;
381
392
  } else {
382
393
  break; /* else */
383
394
  }
@@ -385,9 +396,15 @@ again_no_block:
385
396
  rb_ary_push(current, Qnil); /* no else */
386
397
  }
387
398
  }
399
+ current_case = Qnil;
388
400
  break;
389
401
 
390
402
  case NODE_WHEN:
403
+ if (current_case == Qnil) { /* when without case, ie, no expr in case */
404
+ rb_ary_pop(ary); /* reset what current is pointing at */
405
+ node = NEW_CASE(0, node);
406
+ goto again;
407
+ }
391
408
  add_to_parse_tree(current, node->nd_head, newlines, locals); /* args */
392
409
  if (node->nd_body) {
393
410
  add_to_parse_tree(current, node->nd_body, newlines, locals); /* body */
@@ -400,7 +417,7 @@ again_no_block:
400
417
  case NODE_UNTIL:
401
418
  add_to_parse_tree(current, node->nd_cond, newlines, locals);
402
419
  if (node->nd_body) {
403
- add_to_parse_tree(current, node->nd_body, newlines, locals);
420
+ add_to_parse_tree(current, node->nd_body, newlines, locals);
404
421
  } else {
405
422
  rb_ary_push(current, Qnil);
406
423
  }
@@ -439,7 +456,7 @@ again_no_block:
439
456
  break;
440
457
 
441
458
  // rescue body:
442
- // begin stmt rescue exception => var; stmt; [rescue e2 => v2; s2;]* end
459
+ // begin stmt rescue exception => var; stmt; [rescue e2 => v2; s2;]* end
443
460
  // stmt rescue stmt
444
461
  // a = b rescue c
445
462
 
@@ -452,7 +469,7 @@ again_no_block:
452
469
  add_to_parse_tree(current, node->nd_2nd, newlines, locals);
453
470
  add_to_parse_tree(current, node->nd_1st, newlines, locals);
454
471
  break;
455
-
472
+
456
473
  case NODE_ENSURE:
457
474
  add_to_parse_tree(current, node->nd_head, newlines, locals);
458
475
  if (node->nd_ensr) {
@@ -575,7 +592,7 @@ again_no_block:
575
592
  add_to_parse_tree(current, node->nd_head, newlines, locals);
576
593
  if (node->nd_args) {
577
594
  if (node->nd_args != (NODE *)-1) {
578
- add_to_parse_tree(current, node->nd_args, newlines, locals);
595
+ add_to_parse_tree(current, node->nd_args, newlines, locals);
579
596
  }
580
597
  }
581
598
  add_to_parse_tree(current, node->nd_value, newlines, locals);
@@ -613,22 +630,22 @@ again_no_block:
613
630
  case NODE_HASH:
614
631
  {
615
632
  NODE *list;
616
-
633
+
617
634
  list = node->nd_head;
618
635
  while (list) {
619
- add_to_parse_tree(current, list->nd_head, newlines, locals);
620
- list = list->nd_next;
621
- if (list == 0)
622
- rb_bug("odd number list for Hash");
623
- add_to_parse_tree(current, list->nd_head, newlines, locals);
624
- list = list->nd_next;
636
+ add_to_parse_tree(current, list->nd_head, newlines, locals);
637
+ list = list->nd_next;
638
+ if (list == 0)
639
+ rb_bug("odd number list for Hash");
640
+ add_to_parse_tree(current, list->nd_head, newlines, locals);
641
+ list = list->nd_next;
625
642
  }
626
643
  }
627
644
  break;
628
645
 
629
646
  case NODE_ARRAY:
630
647
  while (node) {
631
- add_to_parse_tree(current, node->nd_head, newlines, locals);
648
+ add_to_parse_tree(current, node->nd_head, newlines, locals);
632
649
  node = node->nd_next;
633
650
  }
634
651
  break;
@@ -642,20 +659,20 @@ again_no_block:
642
659
  NODE *list = node->nd_next;
643
660
  rb_ary_push(current, rb_str_new3(node->nd_lit));
644
661
  while (list) {
645
- if (list->nd_head) {
646
- switch (nd_type(list->nd_head)) {
647
- case NODE_STR:
648
- add_to_parse_tree(current, list->nd_head, newlines, locals);
649
- break;
650
- case NODE_EVSTR:
651
- add_to_parse_tree(current, list->nd_head->nd_body, newlines, locals);
652
- break;
653
- default:
654
- add_to_parse_tree(current, list->nd_head, newlines, locals);
655
- break;
656
- }
657
- }
658
- list = list->nd_next;
662
+ if (list->nd_head) {
663
+ switch (nd_type(list->nd_head)) {
664
+ case NODE_STR:
665
+ add_to_parse_tree(current, list->nd_head, newlines, locals);
666
+ break;
667
+ case NODE_EVSTR:
668
+ add_to_parse_tree(current, list->nd_head->nd_body, newlines, locals);
669
+ break;
670
+ default:
671
+ add_to_parse_tree(current, list->nd_head, newlines, locals);
672
+ break;
673
+ }
674
+ }
675
+ list = list->nd_next;
659
676
  }
660
677
  }
661
678
  break;
@@ -664,7 +681,7 @@ again_no_block:
664
681
  case NODE_DEFS:
665
682
  if (node->nd_defn) {
666
683
  if (nd_type(node) == NODE_DEFS)
667
- add_to_parse_tree(current, node->nd_recv, newlines, locals);
684
+ add_to_parse_tree(current, node->nd_recv, newlines, locals);
668
685
  rb_ary_push(current, ID2SYM(node->nd_mid));
669
686
  add_to_parse_tree(current, node->nd_defn, newlines, locals);
670
687
  }
@@ -705,8 +722,8 @@ again_no_block:
705
722
  while (optnode) {
706
723
  // optional arg names
707
724
  rb_ary_push(current, ID2SYM(locals[i + 3]));
708
- i++;
709
- optnode = optnode->nd_next;
725
+ i++;
726
+ optnode = optnode->nd_next;
710
727
  }
711
728
 
712
729
  if (arg_count > 0) {
@@ -727,11 +744,11 @@ again_no_block:
727
744
  optnode = node->nd_opt;
728
745
  // block?
729
746
  if (optnode) {
730
- add_to_parse_tree(current, node->nd_opt, newlines, locals);
747
+ add_to_parse_tree(current, node->nd_opt, newlines, locals);
731
748
  }
732
749
  }
733
750
  } break;
734
-
751
+
735
752
  case NODE_LVAR:
736
753
  case NODE_DVAR:
737
754
  case NODE_IVAR:
data/test/pt_testcase.rb CHANGED
@@ -14,11 +14,11 @@ class Examples
14
14
  x + 1
15
15
  end
16
16
  end
17
-
17
+
18
18
  def self.dmethod_maker
19
19
  define_method :dmethod_added, self.method(:bmethod_maker)
20
20
  end if RUBY_VERSION < "1.9"
21
-
21
+
22
22
  bmethod_maker
23
23
  dmethod_maker if RUBY_VERSION < "1.9"
24
24
  end
@@ -59,7 +59,7 @@ class ParseTreeTestCase < Test::Unit::TestCase
59
59
  @@testcase_order = %w(Ruby ParseTree)
60
60
 
61
61
  @@testcases = {
62
-
62
+
63
63
  "alias" => {
64
64
  "Ruby" => "class X\n alias :y :x\nend",
65
65
  "ParseTree" => [:class, :X, nil,
@@ -87,7 +87,6 @@ class ParseTreeTestCase < Test::Unit::TestCase
87
87
  [:vcall, :a],
88
88
  :[]=,
89
89
  [:argspush, [:splat, [:vcall, :b]], [:vcall, :c]]],
90
- "Ruby2Ruby" => "a.[]=(*b, c)" # HACK until we can get things ironed out
91
90
  },
92
91
 
93
92
  "array" => {
@@ -96,18 +95,23 @@ class ParseTreeTestCase < Test::Unit::TestCase
96
95
  },
97
96
 
98
97
  "attrasgn" => {
99
- "Ruby" => "y = 0\n42.method=(y)\n",
98
+ "Ruby" => "y = 0\n42.method = y\n",
100
99
  "ParseTree" => [:block,
101
100
  [:lasgn, :y, [:lit, 0]],
102
101
  [:attrasgn, [:lit, 42], :method=, [:array, [:lvar, :y]]]],
103
102
  },
104
103
 
104
+ "attrasgn_index_equals" => {
105
+ "Ruby" => "a[42] = 24",
106
+ "ParseTree" => [:attrasgn, [:vcall, :a], :[]=, [:array, [:lit, 42], [:lit, 24]]],
107
+ },
108
+
105
109
  "attrset" => {
106
110
  "Ruby" => [Examples, :writer=],
107
111
  "ParseTree" => [:defn, :writer=, [:attrset, :@writer]],
108
112
  "Ruby2Ruby" => "attr_writer :writer"
109
113
  },
110
-
114
+
111
115
  "back_ref" => {
112
116
  "Ruby" => "[$&, $`, $', $+]",
113
117
  "ParseTree" => [:array,
@@ -116,7 +120,7 @@ class ParseTreeTestCase < Test::Unit::TestCase
116
120
  [:back_ref, "'".intern], # s->e
117
121
  [:back_ref, :+]],
118
122
  },
119
-
123
+
120
124
  "begin" => {
121
125
  "Ruby" => "begin\n (1 + 1)\nend",
122
126
  "ParseTree" => [:begin, [:call, [:lit, 1], :+, [:array, [:lit, 1]]]],
@@ -142,14 +146,14 @@ class ParseTreeTestCase < Test::Unit::TestCase
142
146
  "ParseTree" => [:iter,
143
147
  [:fcall, :loop], nil, [:if, [:true], [:break], nil]],
144
148
  },
145
-
149
+
146
150
  "break_arg" => {
147
151
  "Ruby" => "loop do\n if true then\n break 42\n end\nend",
148
152
  "ParseTree" => [:iter,
149
153
  [:fcall, :loop], nil,
150
154
  [:if, [:true], [:break, [:lit, 42]], nil]],
151
155
  },
152
-
156
+
153
157
  "call" => {
154
158
  "Ruby" => "self.method",
155
159
  "ParseTree" => [:call, [:self], :method],
@@ -160,6 +164,11 @@ class ParseTreeTestCase < Test::Unit::TestCase
160
164
  "ParseTree" => [:fcall, :puts, [:array, [:lit, 42]]],
161
165
  },
162
166
 
167
+ "call_index" => { # see attrasgn_index_equals for opposite
168
+ "Ruby" => "a[42]",
169
+ "ParseTree" => [:call, [:vcall, :a], :[], [:array, [:lit, 42]]],
170
+ },
171
+
163
172
  "case" => {
164
173
  "Ruby" => "var = 2\nresult = \"\"\ncase var\nwhen 1 then\n puts(\"something\")\n result = \"red\"\nwhen 2, 3 then\n result = \"yellow\"\nwhen 4 then\n # do nothing\nelse\n result = \"green\"\nend\ncase result\nwhen \"red\" then\n var = 1\nwhen \"yellow\" then\n var = 2\nwhen \"green\" then\n var = 3\nelse\n # do nothing\nend\n",
165
174
  "ParseTree" => [:block,
@@ -188,6 +197,17 @@ class ParseTreeTestCase < Test::Unit::TestCase
188
197
  nil]]
189
198
  },
190
199
 
200
+ "case_no_expr" => { # TODO: nested
201
+ "Ruby" => "case\nwhen 1 then\n :a\nwhen 2 then\n :b\nelse\n :c\nend",
202
+ "ParseTree" => [:case, nil,
203
+ [:when,
204
+ [:array, [:lit, 1]],
205
+ [:lit, :a]],
206
+ [:when,
207
+ [:array, [:lit, 2]], [:lit, :b]],
208
+ [:lit, :c]],
209
+ },
210
+
191
211
  "cdecl" => {
192
212
  "Ruby" => "X = 42",
193
213
  "ParseTree" => [:cdecl, :X, [:lit, 42]],
@@ -335,7 +355,7 @@ class ParseTreeTestCase < Test::Unit::TestCase
335
355
  [:array, [:lvar, :a], [:lvar, :b],
336
356
  [:lvar, :c], [:lvar, :d]]]]]]
337
357
  },
338
-
358
+
339
359
  "defn_empty" => {
340
360
  "Ruby" => "def empty\n # do nothing\nend",
341
361
  "ParseTree" => [:defn, :empty, [:scope, [:block, [:args], [:nil]]]],
@@ -359,7 +379,7 @@ class ParseTreeTestCase < Test::Unit::TestCase
359
379
  "Ruby" => "def |(o)\n # do nothing\nend",
360
380
  "ParseTree" => [:defn, :|, [:scope, [:block, [:args, :o], [:nil]]]],
361
381
  },
362
-
382
+
363
383
  "defn_zarray" => { # tests memory allocation for returns
364
384
  "Ruby" => "def zarray\n a = []\n return a\nend",
365
385
  "ParseTree" => [:defn, :zarray,
@@ -396,12 +416,12 @@ class ParseTreeTestCase < Test::Unit::TestCase
396
416
 
397
417
  "dot2" => {
398
418
  "Ruby" => "(a..b)",
399
- "ParseTree" => [:dot2, [:vcall, :a], [:vcall, :b]],
419
+ "ParseTree" => [:dot2, [:vcall, :a], [:vcall, :b]],
400
420
  },
401
421
 
402
422
  "dot3" => {
403
423
  "Ruby" => "(a...b)",
404
- "ParseTree" => [:dot3, [:vcall, :a], [:vcall, :b]],
424
+ "ParseTree" => [:dot3, [:vcall, :a], [:vcall, :b]],
405
425
  },
406
426
 
407
427
  "dregx" => {
@@ -436,7 +456,7 @@ class ParseTreeTestCase < Test::Unit::TestCase
436
456
  [:lasgn, :t, [:lit, 5]],
437
457
  [:dxstr, 'touch ', [:lvar, :t]]],
438
458
  },
439
-
459
+
440
460
  "ensure" => {
441
461
  "Ruby" => "begin
442
462
  (1 + 1)
@@ -686,7 +706,7 @@ end",
686
706
  "Ruby" => "a, b = c, d",
687
707
  "ParseTree" => [:masgn,
688
708
  [:array, [:lasgn, :a], [:lasgn, :b]],
689
- [:array, [:vcall, :c], [:vcall, :d]]],
709
+ [:array, [:vcall, :c], [:vcall, :d]]],
690
710
  },
691
711
 
692
712
  "match" => {
@@ -749,13 +769,13 @@ end",
749
769
  :new, [:array, [:lit, :var]]]],
750
770
  [:lasgn, :c,
751
771
  [:call, [:lvar, :s], :new, [:array, [:nil]]]],
752
-
772
+
753
773
  [:op_asgn2, [:lvar, :c], :var=, "||".intern, # s->e
754
774
  [:lit, 20]],
755
775
  [:op_asgn2, [:lvar, :c], :var=, "&&".intern, # s->e
756
776
  [:lit, 21]],
757
777
  [:op_asgn2, [:lvar, :c], :var=, :+, [:lit, 22]],
758
-
778
+
759
779
  [:op_asgn2,
760
780
  [:call,
761
781
  [:call, [:lvar, :c], :d], :e], :f=, "||".intern,
@@ -786,6 +806,22 @@ end",
786
806
  "ParseTree" => [:iter, [:postexe], nil, [:lit, 1]],
787
807
  },
788
808
 
809
+ "proc_no_args" => {
810
+ "Ruby" => "proc do\n (x + 1)\nend",
811
+ "ParseTree" => [:iter,
812
+ [:fcall, :proc],
813
+ nil,
814
+ [:call, [:vcall, :x], :+, [:array, [:lit, 1]]]],
815
+ },
816
+
817
+ "proc_args" => {
818
+ "Ruby" => "proc do |x|\n (x + 1)\nend",
819
+ "ParseTree" => [:iter,
820
+ [:fcall, :proc],
821
+ [:dasgn_curr, :x],
822
+ [:call, [:dvar, :x], :+, [:array, [:lit, 1]]]],
823
+ },
824
+
789
825
  "redo" => {
790
826
  "Ruby" => "loop do\n if false then\n redo\n end\nend",
791
827
  "ParseTree" => [:iter,
@@ -963,7 +999,7 @@ end",
963
999
  # # HACK
964
1000
  # nodes -= [:alloca, :argspush, :cfunc, :cref, :evstr, :ifunc, :last, :memo, :newline, :opt_n, :method].map { |s| s.to_s }
965
1001
  # end
966
-
1002
+
967
1003
  # untested = nodes-tested
968
1004
 
969
1005
  # puts
@@ -977,7 +1013,7 @@ end",
977
1013
  # },
978
1014
  # )
979
1015
  # end
980
-
1016
+
981
1017
  # flunk
982
1018
  # end
983
1019
 
@@ -1038,7 +1074,7 @@ end",
1038
1074
 
1039
1075
  _, expected, extra_expected = *expected if Array === expected and expected.first == :defx
1040
1076
  _, input, extra_input = *input if Array === input and input.first == :defx
1041
-
1077
+
1042
1078
  debug = input.deep_clone
1043
1079
  assert_equal expected, processor.process(input), "failed on input: #{debug.inspect}"
1044
1080
  extra_input.each do |input| processor.process(input) end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: ParseTree
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.6.1
7
- date: 2006-11-13 00:00:00 -05:00
6
+ version: 1.6.2
7
+ date: 2006-12-19 00:00:00 -08:00
8
8
  summary: Extract and enumerate ruby parse trees.
9
9
  require_paths:
10
10
  - lib
@@ -75,7 +75,7 @@ dependencies:
75
75
  requirements:
76
76
  - - ">="
77
77
  - !ruby/object:Gem::Version
78
- version: 1.1.3
78
+ version: 1.1.4
79
79
  version:
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: RubyInline