Class: RedParse::Node
Constant Summary
collapse
- @@data_warned =
nil
FlattenedIvars::EXCLUDED_IVARS
Instance Attribute Summary collapse
#boolean_identity_params, #identity_params
Class Method Summary
collapse
Instance Method Summary
collapse
-
#+(other) ⇒ Object
-
#+@ ⇒ Object
convert to a Reg::Array expression.
-
#==(other) ⇒ Object
-
#[]=(*args) ⇒ Object
-
#add_parent_links! ⇒ Object
-
#begin_parsetree(o) ⇒ Object
-
#data ⇒ Object
(also: #unwrap)
-
#deep_copy(transform = {}, &override) ⇒ Object
why not use Ron::GraphWalk.graph_copy instead here?.
-
#delete_extraneous_ivars! ⇒ Object
-
#delete_linenums! ⇒ Object
-
#depthwalk(parent = nil, index = nil, subindex = nil, &callback) ⇒ Object
-
#depthwalk_nodes(parent = nil, index = nil, subindex = nil, &callback) ⇒ Object
-
#error?(x) ⇒ Boolean
-
#evalable_inspect ⇒ Object
-
#fixup_multiple_assignments! ⇒ Object
-
#fixup_rescue_assignments! ⇒ Object
-
#image ⇒ Object
-
#initialize(*data) ⇒ Node
constructor
-
#initialize_ivars ⇒ Object
-
#inspect(label = nil, indent = 0) ⇒ Object
-
#lhs_unparse(o) ⇒ Object
-
#linerange ⇒ Object
-
#lvalue ⇒ Object
-
#lvars_defined_in ⇒ Object
-
#merge_replacement_session(session, tempsession) ⇒ Object
-
#negate(condition, offset = nil) ⇒ Object
-
#original_brackets_assign ⇒ Object
-
#parsetree(o) ⇒ Object
-
#parsetrees(list, o) ⇒ Object
-
#pretty_print(q) ⇒ Object
-
#prohibit_fixup(x) ⇒ Object
-
#replace_ivars_and_self(o, session, &replace_self_action) ⇒ Object
-
#replace_value(ovid, ov, session, &replace_action) ⇒ Object
-
#rescue_parsetree(o) ⇒ Object
-
#to_parsetree(*options) ⇒ Object
-
#to_parsetree_and_warnings(*options) ⇒ Object
-
#unary ⇒ Object
-
#walk(parent = nil, index = nil, subindex = nil, &callback) ⇒ Object
callback takes four parameters: parent of node currently being walked, index and subindex within that parent, and finally the actual node being walked.
-
#xform_tree!(*xformers) ⇒ Object
build_exemplars, enumerate_exemplars, identity_param
#flattened_ivars, #flattened_ivars_equal?
Methods included from Stackable
#identity_name
Constructor Details
#initialize(*data) ⇒ Node
Returns a new instance of Node.
376
377
378
|
# File 'lib/redparse/node.rb', line 376
def initialize(*data)
replace data
end
|
Instance Attribute Details
#endline ⇒ Object
Returns the value of attribute endline.
433
434
435
|
# File 'lib/redparse/node.rb', line 433
def endline
@endline
end
|
#errors ⇒ Object
Returns the value of attribute errors.
434
435
436
|
# File 'lib/redparse/node.rb', line 434
def errors
@errors
end
|
#offset ⇒ Object
Returns the value of attribute offset.
435
436
437
|
# File 'lib/redparse/node.rb', line 435
def offset
@offset
end
|
#parent ⇒ Object
Returns the value of attribute parent.
702
703
704
|
# File 'lib/redparse/node.rb', line 702
def parent
@parent
end
|
#startline ⇒ Object
430
431
432
|
# File 'lib/redparse/node.rb', line 430
def startline
@startline||=endline
end
|
Class Method Details
.[](*data) ⇒ Object
445
446
447
448
449
450
451
452
453
454
455
456
457
|
# File 'lib/redparse/node.rb', line 445
def self.[](*data)
options=data.pop if Hash===data.last
inline_symbols data
result=allocate
result.instance_eval{
replace data
options.each_pair{|name,val|
instance_variable_set name,val
} if options
}
result.initialize_ivars
return result
end
|
.create(*args) ⇒ Object
386
387
388
|
# File 'lib/redparse/node.rb', line 386
def self.create(*args)
new(*args)
end
|
.inline_symbols(data) ⇒ Object
437
438
439
440
441
442
443
|
# File 'lib/redparse/node.rb', line 437
def self.inline_symbols data
data.map!{|datum|
Symbol===datum ?
CallNode[nil,datum.to_s,nil,nil,nil] :
datum
}
end
|
.namelist ⇒ Object
593
594
595
596
597
598
|
# File 'lib/redparse/node.rb', line 593
def self.namelist
result=superclass.namelist||[] rescue []
result.concat @namelist if defined? @namelist
return result
end
|
.param_names(*names) ⇒ Object
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
|
# File 'lib/redparse/node.rb', line 551
def self.param_names(*names)
accessors=[]
namelist=[]
@namelist=[]
names.each{|name|
name=name.to_s
last=name[-1]
name.chomp! '!' and name << ?_
namelist << name
unless last==?_
accessors << "def #{name.chomp('_')}; self[#{@namelist.size}] end\n"
accessors << "def #{name.chomp('_')}=(newval); "+
"newval.extend ::RedParse::ListInNode if ::Array===newval and not RedParse::Node===newval;"+
"self[#{@namelist.size}]=newval "+
"end\n"
@namelist << name
end
}
init="
def initialize(#{namelist.join(', ')})
replace [#{@namelist.size==1 ?
@namelist.first :
@namelist.join(', ')
}]
end
alias init_data initialize
"
code= "class ::#{self}\n"+init+accessors.join+"\nend\n"
if defined? DEBUGGER__ or defined? Debugger
Tempfile.open("param_name_defs"){|f|
f.write code
f.flush
load f.path
}
else
eval code
end
@namelist.reject!{|name| /_\Z/===name }
end
|
Instance Method Details
#+(other) ⇒ Object
394
395
396
397
398
399
400
|
# File 'lib/redparse/node.rb', line 394
def +(other)
if SequenceNode===other
SequenceNode[self,*other]
else
SequenceNode[self,other]
end
end
|
#+@ ⇒ Object
convert to a Reg::Array expression. subnodes are also converted. if any matchers are present in the tree, they will be included directly into the enclosing Node’s matcher. this can be a nice way to turn a (possibly deeply nested) node tree into a matcher. note: anything stored in instance variables is ignored in the matcher.
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
|
# File 'lib/redparse/node.rb', line 1022
def +@
node2matcher=proc{|n|
case n
when Node; +n
when Array; +[*n.map(&node2matcher)]
else n
end
}
return +[*map(&node2matcher)] & self.class
end
|
#==(other) ⇒ Object
390
391
392
|
# File 'lib/redparse/node.rb', line 390
def ==(other)
super and flattened_ivars_equal?(other)
end
|
#[]=(*args) ⇒ Object
403
404
405
406
407
408
409
410
411
412
413
|
# File 'lib/redparse/node.rb', line 403
def []=(*args)
val=args.pop
case val
when Symbol
val=CallNode[nil,val.to_s]
when Integer,Float
val=LiteralNode[val]
end
super( *args<<val )
end
|
#add_parent_links! ⇒ Object
696
697
698
699
700
|
# File 'lib/redparse/node.rb', line 696
def add_parent_links!
walk{|parent,i,subi,o|
o.parent=parent if Node===o
}
end
|
#begin_parsetree(o) ⇒ Object
625
|
# File 'lib/redparse/node.rb', line 625
def begin_parsetree(o); parsetree(o) end
|
#data ⇒ Object
Also known as:
unwrap
420
421
422
423
424
425
426
|
# File 'lib/redparse/node.rb', line 420
def data
unless @@data_warned
warn "using obsolete Node#data from #{caller.first}"
@@data_warned=true
end
Array.new(self)
end
|
#deep_copy(transform = {}, &override) ⇒ Object
why not use Ron::GraphWalk.graph_copy instead here?
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
|
# File 'lib/redparse/node.rb', line 947
def deep_copy transform={},&override
handler=proc{|child|
if transform.has_key? child.__id__
transform[child.__id__]
else
case child
when Node
override&&override[child] or
child.deep_copy(transform,&override)
when Array
child.clone.map!(&handler)
when Integer,Symbol,Float,nil,false,true,Module
child
else
child.clone
end
end
}
newdata=map(&handler)
result=clone
instance_variables.each{|iv|
unless iv=="@data" or iv==:@data
val=instance_variable_get(iv)
result.instance_variable_set(iv,handler[val])
end
}
result.replace newdata
return result
end
|
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
|
# File 'lib/redparse/node.rb', line 979
def
walk{|parent,i,subi,node|
case node
when Node
node.remove_instance_variable :@offset rescue nil
node.remove_instance_variable :@loopword_offset rescue nil
node.remove_instance_variable :@iftok_offset rescue nil
node.remove_instance_variable :@endline rescue nil
node.remove_instance_variable :@lvalue rescue nil
if node.respond_to? :lvalue
node.lvalue or
node.remove_instance_variable :@lvalue rescue nil
end
when Token
print "#{node.inspect} in "; pp parent
fail "no tokens should be present in final parse tree (maybe except VarNameToken, ick)"
end
true
}
return self
end
|
#delete_linenums! ⇒ Object
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
|
# File 'lib/redparse/node.rb', line 1001
def delete_linenums!
walk{|parent,i,subi,node|
case node
when Node
node.remove_instance_variable :@endline rescue nil
node.remove_instance_variable :@startline rescue nil
end
true
}
return self
end
|
#depthwalk(parent = nil, index = nil, subindex = nil, &callback) ⇒ Object
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
# File 'lib/redparse/node.rb', line 656
def depthwalk(parent=nil,index=nil,subindex=nil,&callback)
(size-1).downto(0){|i|
datum=self[i]
case datum
when Node
datum.depthwalk(self,i,&callback)
when Array
(datum.size-1).downto(0){|j|
x=datum[j]
if Node===x
x.depthwalk(self,i,j,&callback)
else
callback[self,i,j,x]
end
}
else
callback[self, i, nil, datum]
end
}
callback[ parent,index,subindex,self ]
end
|
#depthwalk_nodes(parent = nil, index = nil, subindex = nil, &callback) ⇒ Object
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
|
# File 'lib/redparse/node.rb', line 678
def depthwalk_nodes(parent=nil,index=nil,subindex=nil,&callback)
(size-1).downto(0){|i|
datum=self[i]
case datum
when Node
datum.depthwalk_nodes(self,i,&callback)
when Array
(datum.size-1).downto(0){|j|
x=datum[j]
if Node===x
x.depthwalk_nodes(self,i,j,&callback)
end
}
end
}
callback[ parent,index,subindex,self ]
end
|
#error?(x) ⇒ Boolean
417
|
# File 'lib/redparse/node.rb', line 417
def error? x; false end
|
#evalable_inspect ⇒ Object
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
# File 'lib/redparse/node.rb', line 511
def evalable_inspect
ivarnames=instance_variables-["@data", :@data]
ivars=ivarnames.map{|ivarname|
val=instance_variable_get(ivarname)
if val.respond_to?(:evalable_inspect)
val=val.evalable_inspect
else
val=val.inspect
end
":"+ivarname+"=>"+val
}.join(', ')
bare="["+map{|val|
if val.respond_to?(:evalable_inspect)
val=val.evalable_inspect
else
val=val.inspect
end
}.join(", ")+"]"
bare.gsub!(/\]\Z/, ", {"+ivars+"}]") unless ivarnames.empty?
return self.class.name+bare
end
|
#fixup_multiple_assignments! ⇒ Object
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
|
# File 'lib/redparse/node.rb', line 813
def fixup_multiple_assignments!
result=self
walk{|parent,i,subi,node|
if CommaOpNode===node
j=nil
list=Array.new(node)
assignnode=nil
list.each_with_index{|assignnode2,jj| assignnode=assignnode2
AssignNode===assignnode and break(j=jj)
}
fail "CommaOpNode without any assignment in final parse tree" unless j
lhs=list[0...j]<<list[j].left
rhs=list[j+1..-1].unshift list[j].right
if lhs.size==1 and MultiAssign===lhs.first
lhs=lhs.first
else
lhs=MultiAssign.new(lhs)
end
node=AssignNode.new(lhs, assignnode.op, rhs)
if parent
if subi
parent[i][subi]=node
else
parent[i]=node
end
else
result=node
end
node.fixup_multiple_assignments!
false
else
true
end
}
return result
end
|
#fixup_rescue_assignments! ⇒ Object
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
|
# File 'lib/redparse/node.rb', line 869
def fixup_rescue_assignments!
result=self
walk{|parent,i,subi,node|
if RescueOpNode===node and assign=node.first and
AssignNode===assign and assign.op.ident=="=" and
!(assign.multi? or
prohibit_fixup assign.right)
node=AssignNode.new(assign.left, assign.op,
RescueOpNode.new(assign.right,nil,node[1][0].action)
)
if parent
if subi
parent[i][subi]=node
else
parent[i]=node
end
else
result=node
end
node.fixup_rescue_assignments!
false
else
true
end
}
return result
end
|
#image ⇒ Object
415
|
# File 'lib/redparse/node.rb', line 415
def image; "(#{inspect})" end
|
#initialize_ivars ⇒ Object
380
381
382
383
384
|
# File 'lib/redparse/node.rb', line 380
def initialize_ivars
@offset||=0
@startline||=0
@endline||=0
end
|
#inspect(label = nil, indent = 0) ⇒ Object
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
# File 'lib/redparse/node.rb', line 459
def inspect label=nil,indent=0
ivarnames=instance_variables-FlattenedIvars::EXCLUDED_IVARS
ivarnodes=[]
ivars=ivarnames.map{|ivarname|
ivar=instance_variable_get(ivarname)
if Node===ivar
ivarnodes.push [ivarname,ivar]
nil
else
ivarname[1..-1]+"="+ivar.inspect if ivar
end
}.compact.join(' ')
pos=@startline.to_s
pos<<"..#@endline" if @endline!=@startline
pos<<"@#@offset"
classname=self.class.name
classname.sub!(/^(?:RedParse::)?(.*?)(?:Node)?$/){$1}
result= [' '*indent,"+",(label+': ' if label),classname," pos=",pos," ",ivars,"\n"]
indent+=2
namelist=self.class.namelist
if namelist and !namelist.empty?
namelist.each{|name|
val=send name rescue "{{ERROR INSPECTING ATTR #{name}}}"
case val
when Node; result<< val.inspect(name,indent)
when ListInNode
result.push ' '*indent,"#{name}:\n",*val.map{|v|
v.inspect(nil,indent+2) rescue ' '*(indent+2)+"-#{v.inspect}\n"
}
when nil;
else ivars<< " #{name}=#{val.inspect}"
end
}
else
each{|val|
case val
when Node; result<< val.inspect(nil,indent)
else result<< ' '*indent+"-#{val.inspect}\n"
end
}
end
ivarnodes.each{|(name,val)|
result<< val.inspect(name,indent)
}
return result.join
end
|
#lhs_unparse(o) ⇒ Object
600
|
# File 'lib/redparse/node.rb', line 600
def lhs_unparse o; unparse(o) end
|
#linerange ⇒ Object
801
802
803
804
805
806
807
808
809
810
811
|
# File 'lib/redparse/node.rb', line 801
def linerange
min=9999999999999999999999999999999999999999999999999999
max=0
walk{|parent,i,subi,node|
if node.respond_to? :endline and line=node.endline
min=[min,line].min
max=[max,line].max
end
}
return min..max
end
|
#lvalue ⇒ Object
944
|
# File 'lib/redparse/node.rb', line 944
def lvalue; nil end
|
#lvars_defined_in ⇒ Object
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
|
# File 'lib/redparse/node.rb', line 906
def lvars_defined_in
result=[]
walk {|parent,i,subi,node|
case node
when MethodNode,ClassNode,ModuleNode,MetaClassNode; false
when CallSiteNode
Node===node.receiver and
result.concat node.receiver.lvars_defined_in
node.args.each{|arg|
result.concat arg.lvars_defined_in if Node===arg
} if node.args
false
when AssignNode
lvalue=node.left
lvalue.respond_to? :all_current_lvars and
result.concat lvalue.all_current_lvars
true
when ForNode
lvalue=node.for
lvalue.respond_to? :all_current_lvars and
result.concat lvalue.all_current_lvars
true
when RescueOpNode,BeginNode
rescues=node[1]
rescues.each{|resc|
name=resc.varname
name and result.push name.ident
}
true
else true
end
}
result.uniq!
return result
end
|
#merge_replacement_session(session, tempsession) ⇒ Object
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
|
# File 'lib/redparse/node.rb', line 777
def merge_replacement_session session,tempsession
ts_has_boundvars= !tempsession.keys.grep(::Symbol).empty?
tempsession.each_pair{|k,v|
if Integer===k
if true
v=Reg::WithBoundRefValues.new(v,tempsession) if ts_has_boundvars
else
v=Ron::GraphWalk.graphcopy(v){|cntr,o,i,ty,useit|
if Reg::BoundRef===o
useit[0]=true
tempsession[o.name]||o
end
}
end
if session.has_key? k
v=v.chain_to session[k]
end
session[k]=v
elsif "finally"==k
session["finally"]=Array(session["finally"]).concat v
end
}
end
|
#negate(condition, offset = nil) ⇒ Object
631
632
633
634
635
636
637
|
# File 'lib/redparse/node.rb', line 631
def negate(condition,offset=nil)
if UnOpNode===condition and condition.op.ident[/^(!|not)$/]
condition.val
else
UnOpNode.new(KeywordToken.new("not",offset),condition)
end
end
|
#original_brackets_assign ⇒ Object
402
|
# File 'lib/redparse/node.rb', line 402
alias original_brackets_assign []=
|
#parsetree(o) ⇒ Object
620
621
622
|
# File 'lib/redparse/node.rb', line 620
def parsetree(o)
"wrong(#{inspect})"
end
|
#parsetrees(list, o) ⇒ Object
627
628
629
|
# File 'lib/redparse/node.rb', line 627
def parsetrees list,o
!list.empty? and list.map{|node| node.parsetree(o)}
end
|
#pretty_print(q) ⇒ Object
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
|
# File 'lib/redparse/node.rb', line 535
def pretty_print(q)
ivarnames=instance_variables-["@data", :@data]
ivars={}
ivarnames.each{|ivarname|
ivars[ivarname.to_sym]=instance_variable_get(ivarname)
}
q.group(1, self.class.name+'[', ']') {
displaylist= ivars.empty? ? self : dup<<ivars
q.seplist(displaylist) {|v|
q.pp v
}
}
end
|
#prohibit_fixup(x) ⇒ Object
860
861
862
863
864
865
866
867
|
# File 'lib/redparse/node.rb', line 860
def prohibit_fixup x
case x
when UnaryStarNode; true
when CallSiteNode; x.params and !x.real_parens
else false
end
end
|
#replace_ivars_and_self(o, session, &replace_self_action) ⇒ Object
756
757
758
759
760
761
762
763
764
765
|
# File 'lib/redparse/node.rb', line 756
def replace_ivars_and_self o,session,&replace_self_action
o.instance_variables.each{|ovname|
ov=o.instance_variable_get(ovname)
replace_value ov.__id__,ov,session do |new|
o.instance_variable_set(ovname,new)
end
}
replace_value o.__id__,o,session, &replace_self_action
end
|
#replace_value(ovid, ov, session, &replace_action) ⇒ Object
767
768
769
770
771
772
773
774
775
|
# File 'lib/redparse/node.rb', line 767
def replace_value ovid,ov,session,&replace_action
if session.has_key? ovid
new= session[ovid]
if Reg::Formula===new
new=new.formula_value(ov,session)
end
replace_action[new]
end
end
|
#rescue_parsetree(o) ⇒ Object
624
|
# File 'lib/redparse/node.rb', line 624
def rescue_parsetree(o); parsetree(o) end
|
#to_parsetree(*options) ⇒ Object
602
603
604
605
606
607
608
609
610
611
612
613
|
# File 'lib/redparse/node.rb', line 602
def to_parsetree(*options)
o={}
[:newlines,:quirks,:ruby187].each{|opt|
o[opt]=true if options.include? opt
}
result=[parsetree(o)]
result=[] if result==[[]] || result==[nil]
return result
end
|
#to_parsetree_and_warnings(*options) ⇒ Object
615
616
617
618
|
# File 'lib/redparse/node.rb', line 615
def to_parsetree_and_warnings(*options)
return to_parsetree(*options),[]
end
|
#unary ⇒ Object
943
|
# File 'lib/redparse/node.rb', line 943
def unary; false end
|
#walk(parent = nil, index = nil, subindex = nil, &callback) ⇒ Object
callback takes four parameters: parent of node currently being walked, index and subindex within that parent, and finally the actual node being walked.
642
643
644
645
646
647
648
649
650
651
652
653
654
|
# File 'lib/redparse/node.rb', line 642
def walk(parent=nil,index=nil,subindex=nil,&callback)
callback[ parent,index,subindex,self ] and
each_with_index{|datum,i|
case datum
when Node; datum.walk(self,i,&callback)
when Array;
datum.each_with_index{|x,j|
Node===x ? x.walk(self,i,j,&callback) : callback[self,i,j,x]
}
else callback[self,i,nil,datum]
end
}
end
|
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
|
# File 'lib/redparse/node.rb', line 704
def xform_tree!(*xformers)
session={}
depthwalk{|parent,i,subi,o|
xformers.each{|xformer|
if o
tempsession={}
xformer.xform!(o,tempsession)
merge_replacement_session session, tempsession
end
}
}
session["final"]=true
result=::Ron::GraphWalk::graphcopy(self,old2new={}){|cntr,o,i,ty,useit|
newo=nil
replace_value o.__id__,o,session do |val|
newo=val
useit[0]=true
end
newo
}
finallys=session["finally"]
finallys.each{|(action,arg)| action[old2new[arg.__id__],session] } if finallys
return result
end
|