Class: RKelly::Nodes::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Visitable, Visitors
Defined in:
lib/rkelly/nodes/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Visitable

#accept

Constructor Details

#initialize(value) ⇒ Node

Returns a new instance of Node.



9
10
11
12
13
14
# File 'lib/rkelly/nodes/node.rb', line 9

def initialize(value)
  @value = value
  @comments = []
  @range = CharRange::EMPTY
  @filename = nil
end

Instance Attribute Details

#comments ⇒ Object

Returns the value of attribute comments.



8
9
10
# File 'lib/rkelly/nodes/node.rb', line 8

def comments
  @comments
end

#filename ⇒ Object

Returns the value of attribute filename.



8
9
10
# File 'lib/rkelly/nodes/node.rb', line 8

def filename
  @filename
end

#range ⇒ Object

Returns the value of attribute range.



8
9
10
# File 'lib/rkelly/nodes/node.rb', line 8

def range
  @range
end

#value ⇒ Object

Returns the value of attribute value.



8
9
10
# File 'lib/rkelly/nodes/node.rb', line 8

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object Also known as: =~



21
22
23
# File 'lib/rkelly/nodes/node.rb', line 21

def ==(other)
  other.is_a?(self.class) && @value == other.value
end

#===(other) ⇒ Object



26
27
28
# File 'lib/rkelly/nodes/node.rb', line 26

def ===(other)
  other.is_a?(self.class) && @value === other.value
end

#each(&block) ⇒ Object

Loops through all the syntax nodes.



95
96
97
# File 'lib/rkelly/nodes/node.rb', line 95

def each(&block)
  EnumerableVisitor.new(block).accept(self)
end

#line ⇒ Object

For backwards compatibility



17
18
19
# File 'lib/rkelly/nodes/node.rb', line 17

def line
  @range.from.line
end

#pointcut(pattern) ⇒ Object Also known as: /

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


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rkelly/nodes/node.rb', line 37

def pointcut(pattern)
  case pattern
  when String
    ast = RKelly::Parser.new.parse(pattern)
    # Only take the first statement
    finder = ast.value.first.class.to_s =~ /StatementNode$/ ?
      ast.value.first.value : ast.value.first
    visitor = PointcutVisitor.new(finder)
  else
    visitor = PointcutVisitor.new(pattern)
  end

  visitor.accept(self)
  visitor
end

#to_dots ⇒ Object

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


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rkelly/nodes/node.rb', line 72

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 ⇒ Object

Generates formatted and intented JavaScript source code.



63
64
65
# File 'lib/rkelly/nodes/node.rb', line 63

def to_ecma
  ECMAVisitor.new.accept(self)
end

#to_real_sexp ⇒ Object

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



101
102
103
# File 'lib/rkelly/nodes/node.rb', line 101

def to_real_sexp
  RealSexpVisitor.new.accept(self)
end

#to_sexp ⇒ Object

Generates an s-expression data structure like so:

"var x = 10;" --> [:var, [[:var_decl, :x, [:assign, [:lit, 10]]]]]]


58
59
60
# File 'lib/rkelly/nodes/node.rb', line 58

def to_sexp
  SexpVisitor.new.accept(self)
end