Class: RedParse::Node

Inherits:
Array
  • Object
show all
Extended by:
Stackable::Meta
Includes:
FlattenedIvars, Stackable
Defined in:
lib/redparse/node.rb,
lib/redparse/ripper.rb

Constant Summary collapse

@@data_warned =
nil

Constants included from FlattenedIvars

FlattenedIvars::EXCLUDED_IVARS

Instance Attribute Summary collapse

Attributes included from Stackable::Meta

#boolean_identity_params, #identity_params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Stackable::Meta

build_exemplars, enumerate_exemplars, identity_param

Methods included from FlattenedIvars

#flattened_ivars, #flattened_ivars_equal?

Methods included from Stackable

#identity_name

Constructor Details

#initialize(*data) ⇒ Node

Returns a new instance of Node.



382
383
384
385
386
387
388
389
390
# File 'lib/redparse/node.rb', line 382

def initialize(*data)
  if Hash===data.last
    options=data.pop
    options.each_pair{|name,val|
      instance_variable_set name,val
    }
  end
  replace data
end

Instance Attribute Details

#endlineObject

Returns the value of attribute endline.



449
450
451
# File 'lib/redparse/node.rb', line 449

def endline
  @endline
end

#errorsObject

Returns the value of attribute errors.



450
451
452
# File 'lib/redparse/node.rb', line 450

def errors
  @errors
end

#offsetObject (readonly)

Returns the value of attribute offset.



451
452
453
# File 'lib/redparse/node.rb', line 451

def offset
  @offset
end

#parentObject

Returns the value of attribute parent.



744
745
746
# File 'lib/redparse/node.rb', line 744

def parent
  @parent
end

#startlineObject



446
447
448
# File 'lib/redparse/node.rb', line 446

def startline
  @startline||=endline
end

Class Method Details

.[](*data) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/redparse/node.rb', line 461

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



398
399
400
# File 'lib/redparse/node.rb', line 398

def self.create(*args)
  new(*args)
end

.inline_symbols(data) ⇒ Object



453
454
455
456
457
458
459
# File 'lib/redparse/node.rb', line 453

def self.inline_symbols data
  data.map!{|datum| 
    Symbol===datum ? 
      CallNode[nil,datum.to_s,nil,nil,nil] : 
      datum 
  }
end

.namelistObject



632
633
634
635
636
637
638
# File 'lib/redparse/node.rb', line 632

def self.namelist
  #@namelist
  result=superclass.namelist||[] rescue []
  result.concat @namelist if defined? @namelist
  result.uniq!
  return result
end

.param_names(*names) ⇒ Object



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/redparse/node.rb', line 590

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



406
407
408
409
410
411
412
# File 'lib/redparse/node.rb', line 406

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.



1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'lib/redparse/node.rb', line 1084

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



402
403
404
# File 'lib/redparse/node.rb', line 402

def ==(other)
  super and flattened_ivars_equal?(other)
end

#[]=(*args) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
# File 'lib/redparse/node.rb', line 415

def []=(*args)
  val=args.pop
  #inline symbols as callnodes
  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



738
739
740
741
742
# File 'lib/redparse/node.rb', line 738

def add_parent_links!
  walk{|parent,i,subi,o|
    o.parent=parent if Node===o
  }
end

#args_rip(list, p) ⇒ Object



263
264
265
266
267
# File 'lib/redparse/ripper.rb', line 263

def args_rip list,p
  list.inject(p.on_args_new){|sum,param| 
    p.on_args_add(sum,param.rip(p)) 
  }
end

#begin_parsetree(o) ⇒ Object



667
# File 'lib/redparse/node.rb', line 667

def begin_parsetree(o); parsetree(o) end

#classic_inspectObject



479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/redparse/node.rb', line 479

def classic_inspect
  self.class.name.dup+'['+
    map{|elem| 
      if elem.respond_to? :classic_inspect
        elem.classic_inspect
      else
        elem.inspect
      end
    }.join(', ')+
  ']'
  #this skips ivars, tho...
end

#dataObject Also known as: unwrap



436
437
438
439
440
441
442
# File 'lib/redparse/node.rb', line 436

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?



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/redparse/node.rb', line 1009

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

#delete_extraneous_ivars!Object



1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
# File 'lib/redparse/node.rb', line 1041

def delete_extraneous_ivars!
  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



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'lib/redparse/node.rb', line 1063

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



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/redparse/node.rb', line 698

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



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'lib/redparse/node.rb', line 720

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

Returns:

  • (Boolean)


433
# File 'lib/redparse/node.rb', line 433

def error? x; false end

#evalable_inspectObject



550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/redparse/node.rb', line 550

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

dead code



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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'lib/redparse/node.rb', line 875

def fixup_multiple_assignments! #dead code
 result=self
 walk{|parent,i,subi,node|
  if CommaOpNode===node
    #there should be an assignnode within this node... find it
    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

    #re-hang the current node with = at the top
    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)

    #graft the new node back onto the old tree
    if parent
      if subi
        parent[i][subi]=node
      else
        parent[i]=node
      end
    else #replacement at top level
      result=node
    end

    #re-scan newly made node, since we tell caller not to scan our children
    node.fixup_multiple_assignments!

    false #skip (your old view of) my children, please
  else
    true
  end
 }
 
 return result

end

#fixup_rescue_assignments!Object

dead code



931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
# File 'lib/redparse/node.rb', line 931

def fixup_rescue_assignments! #dead code
  result=self
  walk{|parent,i,subi,node|
    #if a rescue op with a single assignment on the lhs
    if RescueOpNode===node and assign=node.first and #ick
         AssignNode===assign and assign.op.ident=="=" and 
         !(assign.multi? or 
           prohibit_fixup assign.right)


      #re-hang the node with = at the top instead of rescue
      node=AssignNode.new(assign.left, assign.op,
        RescueOpNode.new(assign.right,nil,node[1][0].action)
      )
      
      #graft the new node back onto the old tree
      if parent
        if subi
          parent[i][subi]=node
        else
          parent[i]=node
        end
      else #replacement at top level
        result=node
      end

      #re-scan newly made node, since we tell caller not to scan our children
      node.fixup_rescue_assignments!

      false #skip (your old view of) my children, please
    else
      true
    end
  }
  return result
end

#force_stmt_list_rip(expr, p) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/redparse/ripper.rb', line 256

def force_stmt_list_rip expr,p
  if SequenceNode===expr
    expr.rip(p)
  else
    stmts_rip [expr],p
  end
end

#imageObject



427
# File 'lib/redparse/node.rb', line 427

def image; "(#{inspect})" end

#initialize_ivarsObject



392
393
394
395
396
# File 'lib/redparse/node.rb', line 392

def initialize_ivars
  @offset||=0
  @startline||=0
  @endline||=0
end

#inspect(label = nil, indent = 0, verbose = false) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/redparse/node.rb', line 492

def inspect label=nil,indent=0,verbose=false
  ivarnames=instance_variables.map{|v| v.to_s }
  ivarnames-=FlattenedIvars::EXCLUDED_IVARS
  ivarnames-=noinspect_instance_variables if defined? noinspect_instance_variables
  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 and verbose
    end
  }.compact.join(' ')


  if verbose
    pos=@startline.to_s
    pos<<"..#@endline" if @endline!=@startline
    pos<<"@#@offset"
  end
  classname=self.class.name
  classname.sub!(/^(?:RedParse::)?(.*?)(?:Node)?$/){$1}
  result= [' '*indent,"+",(label.to_s+': ' if label),classname,]
  result+=[" pos=",pos,] if pos
  result+=[" ",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,verbose)
        when ListInNode 
          result.push ' '*indent,"+#{name}:\n",*val.map{|v| 
            v.inspect(nil,indent+2,verbose) 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,verbose) 
      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



640
# File 'lib/redparse/node.rb', line 640

def lhs_unparse o; unparse(o) end

#linerangeObject



863
864
865
866
867
868
869
870
871
872
873
# File 'lib/redparse/node.rb', line 863

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

#lvalueObject



1006
# File 'lib/redparse/node.rb', line 1006

def lvalue; nil end

#lvars_defined_inObject



968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
# File 'lib/redparse/node.rb', line 968

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



839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
# File 'lib/redparse/node.rb', line 839

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



673
674
675
676
677
678
679
# File 'lib/redparse/node.rb', line 673

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_assignObject

needed by LiteralNode



414
# File 'lib/redparse/node.rb', line 414

alias original_brackets_assign []=

#parsetree(o) ⇒ Object



662
663
664
# File 'lib/redparse/node.rb', line 662

def parsetree(o)
  "wrong(#{inspect})"
end

#parsetrees(list, o) ⇒ Object



669
670
671
# File 'lib/redparse/node.rb', line 669

def parsetrees list,o
  !list.empty? and list.map{|node| node.parsetree(o)}
end

#pretty_print(q) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/redparse/node.rb', line 574

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
    }
#          q.text ', '
#          q.pp_hash ivars          
  }
end

#prohibit_fixup(x) ⇒ Object



922
923
924
925
926
927
928
929
# File 'lib/redparse/node.rb', line 922

def prohibit_fixup x
  case x
  when UnaryStarNode; true
#        when ParenedNode; x.size>1
  when CallSiteNode; x.params and !x.real_parens
  else false
  end
end

#replace_ivars_and_self(o, session, &replace_self_action) ⇒ Object



818
819
820
821
822
823
824
825
826
827
# File 'lib/redparse/node.rb', line 818

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



829
830
831
832
833
834
835
836
837
# File 'lib/redparse/node.rb', line 829

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



666
# File 'lib/redparse/node.rb', line 666

def rescue_parsetree(o); parsetree(o) end

#rfind(ifnone = nil, &block) ⇒ Object



804
805
806
807
808
809
810
# File 'lib/redparse/node.rb', line 804

def rfind ifnone=nil, &block
  result=find(proc{
    find{|subnode| subnode.rfind(&block) if subnode.respond_to? :rfind}
  },&block)
  return result if result
  return ifnone[] if ifnone
end

#rfind_all(&block) ⇒ Object



812
813
814
815
816
# File 'lib/redparse/node.rb', line 812

def rfind_all &block
  result=find_all(&block)
  each{|subnode| result.concat subnode.find_all(&block) if subnode.respond_to? :rfind_all}
  return result
end

#rgrep(pattern) ⇒ Object



798
799
800
801
802
# File 'lib/redparse/node.rb', line 798

def rgrep pattern
  result=grep(pattern)
  each{|subnode| result.concat subnode.rgrep(pattern) if subnode.respond_to? :rgrep}
  return result
end

#rip_and_rescues(p) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/redparse/ripper.rb', line 237

def rip_and_rescues p
    unless rescues.empty?
      r=rescues.map{|resc| resc.rip(p)}
      r.each_with_index{|x,i| x<<r[i+1] unless i+1==r.size }
      r=r.first
    end
    p.on_bodystmt(
      force_stmt_list_rip(body,p),
      r,
      else_&&else_.rip(p),
      ensure_&&p.on_ensure(force_stmt_list_rip(ensure_,p))
    )
end

#rip_explode!(init, receiver = self, &block) ⇒ Object



250
251
252
# File 'lib/redparse/ripper.rb', line 250

def rip_explode! init,receiver=self,&block
  receiver.inject(init,&block)
end

#short_inspect(cutoff = 2) ⇒ Object



429
430
431
# File 'lib/redparse/node.rb', line 429

def short_inspect(cutoff=2)
  inspect.gsub(/\n {#{cutoff*2},}.*$/,'')
end

#stmts_rip(list, p) ⇒ Object



253
254
255
# File 'lib/redparse/ripper.rb', line 253

def stmts_rip list,p
  list.inject(p.on_stmts_new){|sum,expr| p.on_stmts_add(sum,expr.rip(p)) }
end

#to_parsetree(*options) ⇒ Object



644
645
646
647
648
649
650
651
652
653
654
655
# File 'lib/redparse/node.rb', line 644

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



657
658
659
660
# File 'lib/redparse/node.rb', line 657

def to_parsetree_and_warnings(*options)
  #for now, no warnings are ever output
  return to_parsetree(*options),[]
end

#to_ruby(o = {}) ⇒ Object



642
# File 'lib/redparse/node.rb', line 642

def to_ruby o={}; unparse(o) end

#to_sObject

mostly for form inside #{ }



475
476
477
# File 'lib/redparse/node.rb', line 475

def to_s #mostly for form inside #{ }
  unparse
end

#unaryObject



1005
# File 'lib/redparse/node.rb', line 1005

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.



684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'lib/redparse/node.rb', line 684

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

#xform_tree!(*xformers) ⇒ Object



746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/redparse/node.rb', line 746

def xform_tree!(*xformers)
  #search tree for patterns and store results of actions in session
  session={}
  depthwalk{|parent,i,subi,o|
    xformers.each{|xformer|
      if o
        tempsession={}
        xformer.xform!(o,tempsession)
        merge_replacement_session session, tempsession
      #elsif xformer===o and Reg::Transform===xformer
      #  new=xformer.right
      #  if Reg::Formula===right
      #    new=new.formula_value(o,session)
      #  end
      #  subi ? parent[i][subi]=new : parent[i]=new
      end
    }
  }
  session["final"]=true
  
  #apply saved-up actions stored in session, while making a copy of tree
  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 too
  finallys.each{|(action,arg)| action[old2new[arg.__id__],session] } if finallys

  return result
=begin was
  finallys=session["finally"]
  finallys.each{|(action,arg)| action[arg] } if finallys

  depthwalk{|parent,i,subi,o|
    next unless parent
    replace_ivars_and_self o, session do |new|
      subi ? parent[i][subi]=new : parent[i]=new
    end
  }
  replace_ivars_and_self self,session do |new|
    fail unless new
    return new
  end

  return self
=end
end