ruby_parser 3.9.0 → 3.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ require "rp_extensions"
7
7
  require "rp_stringscanner"
8
8
 
9
9
  module RubyParserStuff
10
- VERSION = "3.9.0"
10
+ VERSION = "3.10.0"
11
11
 
12
12
  attr_accessor :lexer, :in_def, :in_single, :file
13
13
  attr_reader :env, :comments
@@ -52,7 +52,7 @@ module RubyParserStuff
52
52
  end
53
53
 
54
54
  def arg_blk_pass node1, node2 # TODO: nuke
55
- node1 = s(:arglist, node1) unless [:arglist, :call_args, :array, :args].include? node1.first
55
+ node1 = s(:arglist, node1) unless [:arglist, :call_args, :array, :args].include? node1.sexp_type
56
56
  node1 << node2 if node2
57
57
  node1
58
58
  end
@@ -67,7 +67,7 @@ module RubyParserStuff
67
67
  case sexp.sexp_type
68
68
  when :masgn then
69
69
  if sexp.size == 2 and sexp[1].sexp_type == :array then
70
- s(:masgn, *sexp[1][1..-1].map { |sub| clean_mlhs sub })
70
+ s(:masgn, *sexp[1].sexp_body.map { |sub| clean_mlhs sub })
71
71
  else
72
72
  debug20 5
73
73
  sexp
@@ -86,7 +86,7 @@ module RubyParserStuff
86
86
 
87
87
  def block_var *args
88
88
  result = self.args args
89
- result[0] = :masgn
89
+ result.sexp_type = :masgn
90
90
  result
91
91
  end
92
92
 
@@ -101,7 +101,7 @@ module RubyParserStuff
101
101
  ary << "&#{block[1]}".to_sym if block
102
102
 
103
103
  if ary.length > 2 or ary.splat then # HACK
104
- s(:masgn, *ary[1..-1])
104
+ s(:masgn, *ary.sexp_body)
105
105
  else
106
106
  ary.last
107
107
  end
@@ -112,7 +112,7 @@ module RubyParserStuff
112
112
  when :kwsplat then
113
113
  array
114
114
  else
115
- s(:hash, *array[1..-1])
115
+ s(:hash, *array.sexp_body)
116
116
  end
117
117
  end
118
118
 
@@ -124,7 +124,7 @@ module RubyParserStuff
124
124
  when Sexp then
125
125
  case arg.sexp_type
126
126
  when :array, :args, :call_args then # HACK? remove array at some point
127
- result.concat arg[1..-1]
127
+ result.concat arg.sexp_body
128
128
  else
129
129
  result << arg
130
130
  end
@@ -148,7 +148,7 @@ module RubyParserStuff
148
148
  when Sexp then
149
149
  case arg.sexp_type
150
150
  when :args, :block, :array, :call_args then # HACK call_args mismatch
151
- result.concat arg[1..-1]
151
+ result.concat arg.sexp_body
152
152
  when :block_arg then
153
153
  result << :"&#{arg.last}"
154
154
  when :shadow then
@@ -177,8 +177,8 @@ module RubyParserStuff
177
177
  end
178
178
 
179
179
  def aryset receiver, index
180
- index ||= []
181
- s(:attrasgn, receiver, :"[]=", *index[1..-1]).compact # [][1..-1] => nil
180
+ index ||= s()
181
+ s(:attrasgn, receiver, :"[]=", *index.sexp_body).compact # [].sexp_body => nil
182
182
  end
183
183
 
184
184
  def assignable(lhs, value = nil)
@@ -233,7 +233,7 @@ module RubyParserStuff
233
233
  return nil if node.nil?
234
234
  node = value_expr node
235
235
 
236
- case node.first
236
+ case node.sexp_type
237
237
  when :lit then
238
238
  if Regexp === node.last then
239
239
  return s(:match, node)
@@ -247,11 +247,13 @@ module RubyParserStuff
247
247
  when :dot2 then
248
248
  label = "flip#{node.hash}"
249
249
  env[label] = :lvar
250
- return s(:flip2, node[1], node[2])
250
+ _, lhs, rhs = node
251
+ return s(:flip2, lhs, rhs)
251
252
  when :dot3 then
252
253
  label = "flip#{node.hash}"
253
254
  env[label] = :lvar
254
- return s(:flip3, node[1], node[2])
255
+ _, lhs, rhs = node
256
+ return s(:flip3, lhs, rhs)
255
257
  else
256
258
  return node
257
259
  end
@@ -266,7 +268,7 @@ module RubyParserStuff
266
268
 
267
269
  def new_match lhs, rhs
268
270
  if lhs then
269
- case lhs[0]
271
+ case lhs.sexp_type
270
272
  when :dregx, :dregx_once then
271
273
  return s(:match2, lhs, rhs).line(lhs.line)
272
274
  when :lit then
@@ -275,7 +277,7 @@ module RubyParserStuff
275
277
  end
276
278
 
277
279
  if rhs then
278
- case rhs[0]
280
+ case rhs.sexp_type
279
281
  when :dregx, :dregx_once then
280
282
  return s(:match3, rhs, lhs).line(lhs.line)
281
283
  when :lit then
@@ -347,12 +349,12 @@ module RubyParserStuff
347
349
 
348
350
  def list_append list, item # TODO: nuke me *sigh*
349
351
  return s(:array, item) unless list
350
- list = s(:array, list) unless Sexp === list && list.first == :array
352
+ list = s(:array, list) unless Sexp === list && list.sexp_type == :array
351
353
  list << item
352
354
  end
353
355
 
354
356
  def list_prepend item, list # TODO: nuke me *sigh*
355
- list = s(:array, list) unless Sexp === list && list[0] == :array
357
+ list = s(:array, list) unless Sexp === list && list.sexp_type == :array
356
358
  list.insert 1, item
357
359
  list
358
360
  end
@@ -361,37 +363,43 @@ module RubyParserStuff
361
363
  return tail unless head
362
364
  return head unless tail
363
365
 
364
- htype, ttype = head[0], tail[0]
366
+ htype, ttype = head.sexp_type, tail.sexp_type
365
367
 
366
368
  head = s(:dstr, '', head) if htype == :evstr
367
369
 
368
370
  case ttype
369
371
  when :str then
370
372
  if htype == :str
371
- head[-1] << tail[-1]
373
+ head.last << tail.last
372
374
  elsif htype == :dstr and head.size == 2 then
373
- head[-1] << tail[-1]
375
+ head.last << tail.last
374
376
  else
375
377
  head << tail
376
378
  end
377
379
  when :dstr then
378
380
  if htype == :str then
379
381
  lineno = head.line
380
- tail[1] = head[-1] + tail[1]
382
+ tail[1] = head.last + tail[1]
381
383
  head = tail
382
384
  head.line = lineno
383
385
  else
384
- tail[0] = :array
386
+ tail.sexp_type = :array
385
387
  tail[1] = s(:str, tail[1])
386
388
  tail.delete_at 1 if tail[1] == s(:str, '')
387
389
 
388
- head.push(*tail[1..-1])
390
+ head.push(*tail.sexp_body)
389
391
  end
390
392
  when :evstr then
391
- head[0] = :dstr if htype == :str
392
- if head.size == 2 and tail.size > 1 and tail[1][0] == :str then
393
- head[-1] << tail[1][-1]
394
- head[0] = :str if head.size == 2 # HACK ?
393
+ if htype == :str then
394
+ f, l = head.file, head.line
395
+ head = s(:dstr, *head.sexp_body)
396
+ head.file = f
397
+ head.line = l
398
+ end
399
+
400
+ if head.size == 2 and tail.size > 1 and tail[1].sexp_type == :str then
401
+ head.last << tail[1].last
402
+ head.sexp_type = :str if head.size == 2 # HACK ?
395
403
  else
396
404
  head.push(tail)
397
405
  end
@@ -406,14 +414,16 @@ module RubyParserStuff
406
414
  def logical_op type, left, right
407
415
  left = value_expr left
408
416
 
409
- if left and left[0] == type and not left.paren then
410
- node, second = left, nil
417
+ if left and left.sexp_type == type and not left.paren then
418
+ node, rhs = left, nil
411
419
 
412
- while (second = node[2]) && second[0] == type and not second.paren do
413
- node = second
420
+ loop do
421
+ _, _lhs, rhs = node
422
+ break unless rhs && rhs.sexp_type == type and not rhs.paren
423
+ node = rhs
414
424
  end
415
425
 
416
- node[2] = s(type, second, right)
426
+ node[2] = s(type, rhs, right)
417
427
 
418
428
  return left
419
429
  end
@@ -426,7 +436,7 @@ module RubyParserStuff
426
436
 
427
437
  def new_aref val
428
438
  val[2] ||= s(:arglist)
429
- val[2][0] = :arglist if val[2][0] == :array # REFACTOR
439
+ val[2].sexp_type = :arglist if val[2].sexp_type == :array # REFACTOR
430
440
  if val[0].node_type == :self then
431
441
  result = new_call nil, :"[]", val[2]
432
442
  else
@@ -468,13 +478,13 @@ module RubyParserStuff
468
478
  end
469
479
 
470
480
  def argl x
471
- x = s(:arglist, x) if x and x[0] == :array
481
+ x = s(:arglist, x) if x and x.sexp_type == :array
472
482
  x
473
483
  end
474
484
 
475
485
  def backref_assign_error ref
476
486
  # TODO: need a test for this... obviously
477
- case ref.first
487
+ case ref.sexp_type
478
488
  when :nth_ref then
479
489
  raise "write a test 2"
480
490
  raise SyntaxError, "Can't set variable %p" % ref.last
@@ -500,7 +510,7 @@ module RubyParserStuff
500
510
  # TODO: need a test with f(&b) { } to produce warning
501
511
 
502
512
  if args
503
- if [:arglist, :args, :array, :call_args].include? args.first
513
+ if [:arglist, :args, :array, :call_args].include? args.sexp_type
504
514
  result.concat args.sexp_body
505
515
  else
506
516
  result << args
@@ -539,7 +549,7 @@ module RubyParserStuff
539
549
 
540
550
  result[2..-1].each do |node|
541
551
  block = node.block(:delete)
542
- node.concat block[1..-1] if block
552
+ node.concat block.sexp_body if block
543
553
  end
544
554
 
545
555
  # else
@@ -556,8 +566,8 @@ module RubyParserStuff
556
566
  result = s(:class, path, superclass)
557
567
 
558
568
  if body then
559
- if body.first == :block then
560
- result.push(*body[1..-1])
569
+ if body.sexp_type == :block then
570
+ result.push(*body.sexp_body)
561
571
  else
562
572
  result.push body
563
573
  end
@@ -581,8 +591,8 @@ module RubyParserStuff
581
591
  result = s(:defn, name.to_sym, args)
582
592
 
583
593
  if body then
584
- if body.first == :block then
585
- result.push(*body[1..-1])
594
+ if body.sexp_type == :block then
595
+ result.push(*body.sexp_body)
586
596
  else
587
597
  result.push body
588
598
  end
@@ -602,8 +612,8 @@ module RubyParserStuff
602
612
  result = s(:defs, recv, name.to_sym, args)
603
613
 
604
614
  if body then
605
- if body.first == :block then
606
- result.push(*body[1..-1])
615
+ if body.sexp_type == :block then
616
+ result.push(*body.sexp_body)
607
617
  else
608
618
  result.push body
609
619
  end
@@ -627,7 +637,7 @@ module RubyParserStuff
627
637
  def new_if c, t, f
628
638
  l = [c.line, t && t.line, f && f.line].compact.min
629
639
  c = cond c
630
- c, t, f = c.last, f, t if c[0] == :not and canonicalize_conditions
640
+ c, t, f = c.last, f, t if c.sexp_type == :not and canonicalize_conditions
631
641
  s(:if, c, t, f).line(l)
632
642
  end
633
643
 
@@ -642,7 +652,7 @@ module RubyParserStuff
642
652
  result << args
643
653
  result << body if body
644
654
 
645
- args[0] = :args unless args == 0
655
+ args.sexp_type = :args unless args == 0
646
656
 
647
657
  result
648
658
  end
@@ -654,10 +664,12 @@ module RubyParserStuff
654
664
  end
655
665
 
656
666
  def new_masgn lhs, rhs, wrap = false
667
+ _, ary = lhs
668
+
657
669
  rhs = value_expr(rhs)
658
- rhs = lhs[1] ? s(:to_ary, rhs) : s(:array, rhs) if wrap
670
+ rhs = ary ? s(:to_ary, rhs) : s(:array, rhs) if wrap
659
671
 
660
- lhs.delete_at 1 if lhs[1].nil?
672
+ lhs.delete_at 1 if ary.nil?
661
673
  lhs << rhs
662
674
 
663
675
  lhs
@@ -669,8 +681,8 @@ module RubyParserStuff
669
681
  result = s(:module, path)
670
682
 
671
683
  if body then # REFACTOR?
672
- if body.first == :block then
673
- result.push(*body[1..-1])
684
+ if body.sexp_type == :block then
685
+ result.push(*body.sexp_body)
674
686
  else
675
687
  result.push body
676
688
  end
@@ -741,9 +753,9 @@ module RubyParserStuff
741
753
  k = c if c =~ /[esu]/ if RUBY_VERSION < "1.9"
742
754
  end
743
755
 
744
- case node[0]
756
+ case node.sexp_type
745
757
  when :str then
746
- node[0] = :lit
758
+ node.sexp_type = :lit
747
759
  node[1] = if k then
748
760
  Regexp.new(node[1], o, k)
749
761
  else
@@ -762,14 +774,14 @@ module RubyParserStuff
762
774
  end
763
775
  when :dstr then
764
776
  if options =~ /o/ then
765
- node[0] = :dregx_once
777
+ node.sexp_type = :dregx_once
766
778
  else
767
- node[0] = :dregx
779
+ node.sexp_type = :dregx
768
780
  end
769
781
  node << o if o and o != 0
770
782
  else
771
783
  node = s(:dregx, '', node);
772
- node[0] = :dregx_once if options =~ /o/
784
+ node.sexp_type = :dregx_once if options =~ /o/
773
785
  node << o if o and o != 0
774
786
  end
775
787
 
@@ -777,12 +789,12 @@ module RubyParserStuff
777
789
  end
778
790
 
779
791
  def new_resbody cond, body
780
- if body && body.first == :block then
792
+ if body && body.sexp_type == :block then
781
793
  body.shift # remove block and splat it in directly
782
794
  else
783
795
  body = [body]
784
796
  end
785
- s(:resbody, cond, *body)
797
+ s(:resbody, cond, *body).line cond.line
786
798
  end
787
799
 
788
800
  def new_sclass val
@@ -791,8 +803,8 @@ module RubyParserStuff
791
803
  result = s(:sclass, recv)
792
804
 
793
805
  if body then
794
- if body.first == :block then
795
- result.push(*body[1..-1])
806
+ if body.sexp_type == :block then
807
+ result.push(*body.sexp_body)
796
808
  else
797
809
  result.push body
798
810
  end
@@ -833,7 +845,7 @@ module RubyParserStuff
833
845
  end
834
846
 
835
847
  def new_word_list_entry val
836
- result = val[1][0] == :evstr ? s(:dstr, "", val[1]) : val[1]
848
+ result = val[1].sexp_type == :evstr ? s(:dstr, "", val[1]) : val[1]
837
849
  self.lexer.fixup_lineno
838
850
  result
839
851
  end
@@ -862,9 +874,9 @@ module RubyParserStuff
862
874
 
863
875
  result ||= s(:str, "")
864
876
 
865
- case sym[0]
877
+ case sym.sexp_type
866
878
  when :dstr then
867
- sym[0] = :dsym
879
+ sym.sexp_type = :dsym
868
880
  when :str then
869
881
  sym = s(:lit, sym.last.to_sym)
870
882
  else
@@ -880,7 +892,7 @@ module RubyParserStuff
880
892
  s(:super, args)
881
893
  else
882
894
  args ||= s(:arglist)
883
- s(:super, *args[1..-1])
895
+ s(:super, *args.sexp_body)
884
896
  end
885
897
  end
886
898
 
@@ -899,11 +911,11 @@ module RubyParserStuff
899
911
  def new_until_or_while type, block, expr, pre
900
912
  other = type == :until ? :while : :until
901
913
  line = [block && block.line, expr.line].compact.min
902
- block, pre = block.last, false if block && block[0] == :begin
914
+ block, pre = block.last, false if block && block.sexp_type == :begin
903
915
 
904
916
  expr = cond expr
905
917
 
906
- result = unless expr.first == :not and canonicalize_conditions then
918
+ result = unless expr.sexp_type == :not and canonicalize_conditions then
907
919
  s(type, expr, block, pre)
908
920
  else
909
921
  s(other, expr.last, block, pre)
@@ -923,11 +935,11 @@ module RubyParserStuff
923
935
 
924
936
  def new_xstring str
925
937
  if str then
926
- case str[0]
938
+ case str.sexp_type
927
939
  when :str
928
- str[0] = :xstr
940
+ str.sexp_type = :xstr
929
941
  when :dstr
930
- str[0] = :dxstr
942
+ str.sexp_type = :dxstr
931
943
  else
932
944
  str = s(:dxstr, '', str)
933
945
  end
@@ -945,10 +957,10 @@ module RubyParserStuff
945
957
 
946
958
  args ||= s(:arglist)
947
959
 
948
- args[0] = :arglist if [:call_args, :array].include?(args[0])
949
- args = s(:arglist, args) unless args.first == :arglist
960
+ args.sexp_type = :arglist if [:call_args, :array].include? args.sexp_type
961
+ args = s(:arglist, args) unless args.sexp_type == :arglist
950
962
 
951
- return s(:yield, *args[1..-1])
963
+ return s(:yield, *args.sexp_body)
952
964
  end
953
965
 
954
966
  def next_token
@@ -966,11 +978,11 @@ module RubyParserStuff
966
978
 
967
979
  rhs = value_expr rhs
968
980
 
969
- case lhs[0]
981
+ case lhs.sexp_type
970
982
  when :lasgn, :iasgn, :cdecl, :cvdecl, :gasgn, :cvasgn, :attrasgn, :safe_attrasgn then
971
983
  lhs << rhs
972
984
  when :const then
973
- lhs[0] = :cdecl
985
+ lhs.sexp_type = :cdecl
974
986
  lhs << rhs
975
987
  else
976
988
  raise "unknown lhs #{lhs.inspect} w/ #{rhs.inspect}"
@@ -1080,8 +1092,8 @@ module RubyParserStuff
1080
1092
 
1081
1093
  def remove_begin node
1082
1094
  oldnode = node
1083
- if node and :begin == node[0] and node.size == 2 then
1084
- node = node[-1]
1095
+ if node and node.sexp_type == :begin and node.size == 2 then
1096
+ node = node.last
1085
1097
  node.line = oldnode.line
1086
1098
  end
1087
1099
  node
@@ -1114,18 +1126,18 @@ module RubyParserStuff
1114
1126
 
1115
1127
  def ret_args node
1116
1128
  if node then
1117
- raise "write a test 5" if node[0] == :block_pass
1129
+ raise "write a test 5" if node.sexp_type == :block_pass
1118
1130
 
1119
1131
  raise SyntaxError, "block argument should not be given" if
1120
- node[0] == :block_pass
1132
+ node.sexp_type == :block_pass
1121
1133
 
1122
- node[0] = :array if node[0] == :call_args
1123
- node = node.last if node[0] == :array && node.size == 2
1134
+ node.sexp_type = :array if node.sexp_type == :call_args
1135
+ node = node.last if node.sexp_type == :array && node.size == 2
1124
1136
 
1125
1137
  # HACK matz wraps ONE of the FOUR splats in a newline to
1126
1138
  # distinguish. I use paren for now. ugh
1127
- node = s(:svalue, node) if node[0] == :splat and not node.paren
1128
- node[0] = :svalue if node[0] == :arglist && node[1][0] == :splat
1139
+ node = s(:svalue, node) if node.sexp_type == :splat and not node.paren
1140
+ node.sexp_type = :svalue if node.sexp_type == :arglist && node[1].sexp_type == :splat
1129
1141
  end
1130
1142
 
1131
1143
  node
@@ -1141,15 +1153,20 @@ module RubyParserStuff
1141
1153
  def value_expr oldnode # HACK
1142
1154
  node = remove_begin oldnode
1143
1155
  node.line = oldnode.line if oldnode
1144
- node[2] = value_expr(node[2]) if node and node[0] == :if
1156
+ node[2] = value_expr node[2] if node and node.sexp_type == :if
1145
1157
  node
1146
1158
  end
1147
1159
 
1148
1160
  def void_stmts node
1149
1161
  return nil unless node
1150
- return node unless node[0] == :block
1162
+ return node unless node.sexp_type == :block
1163
+
1164
+ if node.respond_to? :sexp_body= then
1165
+ node.sexp_body = node.sexp_body.map { |n| remove_begin n }
1166
+ else
1167
+ node[1..-1] = node[1..-1].map { |n| remove_begin(n) }
1168
+ end
1151
1169
 
1152
- node[1..-1] = node[1..-1].map { |n| remove_begin(n) }
1153
1170
  node
1154
1171
  end
1155
1172
 
@@ -1310,10 +1327,15 @@ module RubyParserStuff
1310
1327
  attr_reader :stack
1311
1328
  attr_accessor :debug
1312
1329
 
1313
- def initialize(name)
1330
+ def initialize name, debug=false
1314
1331
  @name = name
1315
1332
  @stack = [false]
1316
- @debug = false
1333
+ @debug = debug
1334
+ end
1335
+
1336
+ def reset
1337
+ @stack = [false]
1338
+ warn "#{name}_stack(set): 0" if debug
1317
1339
  end
1318
1340
 
1319
1341
  def inspect
@@ -1321,12 +1343,11 @@ module RubyParserStuff
1321
1343
  end
1322
1344
 
1323
1345
  def is_in_state
1324
- p :stack_is_in_state => [name, @stack.last, caller.first] if debug
1325
1346
  @stack.last
1326
1347
  end
1327
1348
 
1328
1349
  def lexpop
1329
- p :stack_lexpop => caller.first if debug
1350
+ warn "#{name}_stack.lexpop" if debug
1330
1351
  raise if @stack.size == 0
1331
1352
  a = @stack.pop
1332
1353
  b = @stack.pop
@@ -1335,14 +1356,16 @@ module RubyParserStuff
1335
1356
 
1336
1357
  def pop
1337
1358
  r = @stack.pop
1338
- p :stack_pop => [name, r, @stack, caller.first] if debug
1359
+ warn "#{name}_stack.pop" if debug
1339
1360
  @stack.push false if @stack.size == 0
1340
1361
  r
1341
1362
  end
1342
1363
 
1343
1364
  def push val
1344
1365
  @stack.push val
1345
- p :stack_push => [name, @stack, caller.first] if debug
1366
+ c = caller.first
1367
+ c = caller[1] if c =~ /expr_result/
1368
+ warn "#{name}_stack(push): #{val} at line #{c.clean_caller}" if debug
1346
1369
  nil
1347
1370
  end
1348
1371