class RKelly::Nodes::Node

Attributes

comments[RW]
filename[RW]
range[RW]
value[RW]

Public Class Methods

new(value) click to toggle source
# File lib/rkelly/nodes/node.rb, line 11
def initialize(value)
  @value = value
  @range = CharRange::EMPTY
end

Public Instance Methods

/(pattern)
Alias for: pointcut
==(other) click to toggle source
# File lib/rkelly/nodes/node.rb, line 23
def ==(other)
  other.is_a?(self.class) && @value == other.value
end
Also aliased as: =~
===(other) click to toggle source
# File lib/rkelly/nodes/node.rb, line 28
def ===(other)
  other.is_a?(self.class) && @value === other.value
end
=~(other)
Alias for: ==
each(&block) click to toggle source

Loops through all the syntax nodes.

# File lib/rkelly/nodes/node.rb, line 97
def each(&block)
  EnumerableVisitor.new(block).accept(self)
end
line() click to toggle source

For backwards compatibility

# File lib/rkelly/nodes/node.rb, line 19
def line
  @range.from.line
end
pointcut(pattern) click to toggle source

Matches nodes with the given pattern (usually a class name of the node) and returns an instance of PointcutVisitor on which matches can be invoked to get the list of AST nodes that matched.

ast.pointcut(RKelly::Nodes::IfNode).matches --> array of nodes
# File lib/rkelly/nodes/node.rb, line 39
def pointcut(pattern)
  case pattern
  when String
    ast = RKelly::Parser.new.parse(pattern)
    # Only take the first statement
    finder = ast.value.first.class.name.end_with?("StatementNode") ?
      ast.value.first.value : ast.value.first
    visitor = PointcutVisitor.new(finder)
  else
    visitor = PointcutVisitor.new(pattern)
  end

  visitor.accept(self)
  visitor
end
Also aliased as: /
to_dots() click to toggle source

Generates a graph description in DOT language. This can be fed into the dot program to generate a graph of the AST:

$ dot -Tpng generated-graph.dot -o graph.png
# File lib/rkelly/nodes/node.rb, line 74
      def to_dots
        visitor = DotVisitor.new
        visitor.accept(self)
        header = <<-END
digraph g {
graph [ rankdir = "TB" ];
node [
  fontsize = "16"
  shape = "ellipse"
];
edge [ ];
        END
        nodes = visitor.nodes.map { |x| x.to_s }.join("\n")
        counter = 0
        arrows = visitor.arrows.map { |x|
          s = "#{x} [\nid = #{counter}\n];"
          counter += 1
          s
        }.join("\n")
        "#{header}\n#{nodes}\n#{arrows}\n}"
      end
to_ecma() click to toggle source

Generates formatted and intented JavaScript source code.

# File lib/rkelly/nodes/node.rb, line 65
def to_ecma
  ECMAVisitor.new.accept(self)
end
to_real_sexp() click to toggle source

This CRASHES! It calls method s which is nowhere to be found.

# File lib/rkelly/nodes/node.rb, line 103
def to_real_sexp
  RealSexpVisitor.new.accept(self)
end
to_sexp() click to toggle source

Generates an s-expression data structure like so:

"var x = 10;" --> [:var, [[:var_decl, :x, [:assign, [:lit, 10]]]]]]
# File lib/rkelly/nodes/node.rb, line 60
def to_sexp
  SexpVisitor.new.accept(self)
end