Method: CodeWeb::CodeParser#traverse

Defined in:
lib/code_web/code_parser.rb

#traverse(ast, has_yield = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/code_web/code_parser.rb', line 25

def traverse(ast, has_yield=false)
  puts "#{spaces}||#{collapse_ast(ast,1)}||" if verbose?
  puts src if ast.nil?
  case ast.node_type
  #dstr = define string ("abc#{here}"),
  #evstr evaluate string (#{HERE})
  #attrasgn = attribute assignment
  when :block, :if, :ensure, :rescue, :case, :when, :begin,
       :while, :until, :defined, :resbody, :match2, :match3, :dot2, :dot3,
       :dstr, :evstr, :dsym, :dregx, :hash, :array, :return, :and, :or,
       :next, :to_ary, :splat, :block_pass, :until, :yield,
       /asgn/, :ivar, :arglist, :args, :kwarg, :kwargs, :kwsplat, :zsuper, :not, #statements[]
       :super, :xstr, :for, :until, :dxstr, 
  #these end up being no-ops:
       :lit, :lvar, :const, :str, :nil, :gvar, :back_ref,
       :true, :false, :colon2, :colon3, :next, :alias,
       :nth_ref, :sclass, :cvdecl, :break, :retry, :undef,
  #random
       :svalue, :cvar
    traverse_nodes(ast, 1..-1)
  when :self
    traverse_nodes(ast, 1..-1)
  when :module, #name, statements[]
       :class #name, parent, statements[]
    in_context ast[1], true, true do
      traverse_nodes(ast, 2..-1)
    end
  when :cdecl, #name, statements[]
       :defn #name, args[], call[]
    in_context ast[1], true do
      traverse_nodes(ast, 2..-1)
    end
  when :defs #self[], name, args[], call[] # static method
    in_context ast[2], :static do
      traverse_nodes(ast, 2..-1)
    end
  when :iter #call[], yield_args[], yield_{block|call}[]
    traverse(ast[1], :has_yield)
    in_context 'yield', true do
      traverse_nodes(ast, 2..-1)
    end
  when :call # object, statement? || const symbol, args
    handle_method_call(ast, has_yield)
    traverse_nodes(ast, 1..-1)
  else
    STDERR.puts "#{src}\n  unknown node: #{ast.node_type} #{collapse_ast(ast,1)}"
    if exit_on_error
      if defined?(Pry)
        binding.pry
      elsif defined?(Byebug)
        byebug
      end
      raise "error"
    end
    traverse_nodes(ast, 1..-1)
  end
end