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, included

Constructor Details

#initialize(value) ⇒ Node

Returns a new instance of Node.



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

def initialize(value)
  @value = value
  @range = CharRange::EMPTY
end

Instance Attribute Details

#commentsObject

Returns the value of attribute comments.



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

def comments
  @comments
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

#rangeObject

Returns the value of attribute range.



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

def range
  @range
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

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



23
24
25
# File 'lib/rkelly/nodes/node.rb', line 23

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

#===(other) ⇒ Object



28
29
30
# File 'lib/rkelly/nodes/node.rb', line 28

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

#each(&block) ⇒ Object

Loops through all the syntax nodes.



97
98
99
# File 'lib/rkelly/nodes/node.rb', line 97

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

#lineObject

For backwards compatibility



19
20
21
# File 'lib/rkelly/nodes/node.rb', line 19

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


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

#to_dotsObject

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


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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_ecmaObject

Generates formatted and intented JavaScript source code.



65
66
67
# File 'lib/rkelly/nodes/node.rb', line 65

def to_ecma
  ECMAVisitor.new.accept(self)
end

#to_real_sexpObject

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



103
104
105
# File 'lib/rkelly/nodes/node.rb', line 103

def to_real_sexp
  RealSexpVisitor.new.accept(self)
end

#to_sexpObject

Generates an s-expression data structure like so:

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


60
61
62
# File 'lib/rkelly/nodes/node.rb', line 60

def to_sexp
  SexpVisitor.new.accept(self)
end