Sha256: eaf1c570347760f49acc5676ea25bfaef54eb26319ac0457c2b70e572655dc1d

Contents?: true

Size: 1004 Bytes

Versions: 1

Compression:

Stored size: 1004 Bytes

Contents

module Wool
  module SexpAnalysis
    # Visitor: a set of methods for visiting an AST. The
    # default implementations visit each child and do no
    # other processing. By including this module, and
    # implementing certain methods, you can do your own
    # processing on, say, every instance of a :rescue AST node.
    # The default implementation will go arbitrarily deep in the AST
    # tree until it hits a method you define.
    module Visitor
      def visit(node)
        case node
        when Sexp
          case node[0]
          when ::Symbol
            send("visit_#{node[0]}", node)
          when Array
            node.each {|x| visit(x)}
          end
        end
      end
      
      def default_visit(node)
        node.children.select {|x| Sexp === x}.each {|x| visit(x) }
      end
      
      def method_missing(meth, *args, &blk)
        if meth.to_s[0,6] == 'visit_'
          default_visit args.first
        else
          raise
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wool-0.5.1 lib/wool/analysis/visitor.rb