Class: RKelly::Nodes::Node
- Inherits:
-
Object
- Object
- RKelly::Nodes::Node
- Defined in:
- lib/rkelly/nodes/node.rb
Direct Known Subclasses
BinaryNode, BracketAccessorNode, CommaNode, DotAccessorNode, ForInNode, ForNode, FunctionCallNode, FunctionExprNode, IfNode, LabelNode, NewExprNode, OpEqualNode, PostfixNode, PropertyNode, ResolveNode, TryNode, VarDeclNode
Instance Attribute Summary collapse
-
#comments ⇒ Object
Returns the value of attribute comments.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#range ⇒ Object
Returns the value of attribute range.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #=~)
- #===(other) ⇒ Object
-
#each(&block) ⇒ Object
Loops through all the syntax nodes.
-
#initialize(value) ⇒ Node
constructor
A new instance of Node.
-
#line ⇒ Object
For backwards compatibility.
-
#pointcut(pattern) ⇒ Object
(also: #/)
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.
-
#to_dots ⇒ Object
Generates a graph description in DOT language.
-
#to_ecma ⇒ Object
Generates formatted and intented JavaScript source code.
-
#to_real_sexp ⇒ Object
This CRASHES! It calls method #s which is nowhere to be found.
-
#to_sexp ⇒ Object
Generates an s-expression data structure like so:.
Methods included from Visitable
Constructor Details
Instance Attribute Details
#comments ⇒ Object
Returns the value of attribute comments.
10 11 12 |
# File 'lib/rkelly/nodes/node.rb', line 10 def comments @comments end |
#filename ⇒ Object
Returns the value of attribute filename.
10 11 12 |
# File 'lib/rkelly/nodes/node.rb', line 10 def filename @filename end |
#range ⇒ Object
Returns the value of attribute range.
10 11 12 |
# File 'lib/rkelly/nodes/node.rb', line 10 def range @range end |
#value ⇒ Object
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 |
#line ⇒ Object
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_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
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_ecma ⇒ Object
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_sexp ⇒ Object
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_sexp ⇒ Object
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 |