Class: ATP::AST::Node
- Inherits:
-
AST::Node
- Object
- AST::Node
- ATP::AST::Node
- Includes:
- Factories
- Defined in:
- lib/atp/ast/node.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
Class Method Summary collapse
-
.from_sexp(sexp) ⇒ Object
Create a new node from the given S-expression (a string).
Instance Method Summary collapse
-
#add(*nodes) ⇒ Object
Add the given nodes to the children.
-
#ensure_node_present(type) ⇒ Object
Adds an empty node of the given type to the children unless another node of the same type is already present.
-
#find(type) ⇒ Object
Returns the first child node of the given type that is found.
-
#find_all(*types) ⇒ Object
Returns an array containing all child nodes of the given type(s).
-
#initialize(type, children = [], properties = {}) ⇒ Node
constructor
A new instance of Node.
- #source ⇒ Object
-
#value ⇒ Object
Returns the value at the root of an AST node like this:.
Methods included from Factories
Constructor Details
#initialize(type, children = [], properties = {}) ⇒ Node
Returns a new instance of Node.
9 10 11 12 13 14 |
# File 'lib/atp/ast/node.rb', line 9 def initialize(type, children = [], properties = {}) # Always use strings instead of symbols in the AST, makes serializing # back and forward to a string easier children = children.map { |c| c.is_a?(Symbol) ? c.to_s : c } super type, children, properties end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
7 8 9 |
# File 'lib/atp/ast/node.rb', line 7 def description @description end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
7 8 9 |
# File 'lib/atp/ast/node.rb', line 7 def file @file end |
#line_number ⇒ Object (readonly)
Returns the value of attribute line_number.
7 8 9 |
# File 'lib/atp/ast/node.rb', line 7 def line_number @line_number end |
Class Method Details
Instance Method Details
#add(*nodes) ⇒ Object
Add the given nodes to the children
57 58 59 |
# File 'lib/atp/ast/node.rb', line 57 def add(*nodes) updated(nil, children + nodes) end |
#ensure_node_present(type) ⇒ Object
Adds an empty node of the given type to the children unless another node of the same type is already present
32 33 34 35 36 37 38 |
# File 'lib/atp/ast/node.rb', line 32 def ensure_node_present(type) if children.any? { |n| n.type == type } self else updated(nil, children + [n0(type)]) end end |
#find(type) ⇒ Object
Returns the first child node of the given type that is found
62 63 64 |
# File 'lib/atp/ast/node.rb', line 62 def find(type) children.find { |c| c.try(:type) == type } end |
#find_all(*types) ⇒ Object
Returns an array containing all child nodes of the given type(s)
67 68 69 |
# File 'lib/atp/ast/node.rb', line 67 def find_all(*types) children.select { |c| types.include?(c.try(:type)) } end |
#source ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/atp/ast/node.rb', line 16 def source if file "#{file}:#{line_number}" else '<Sorry, lost the source file info, please include an example if you report as a bug>' end end |
#value ⇒ Object
Returns the value at the root of an AST node like this:
node # => (module-def
(module-name
(SCALAR-ID "Instrument"))
node.value # => "Instrument"
No error checking is done and the caller is responsible for calling this only on compatible nodes
50 51 52 53 54 |
# File 'lib/atp/ast/node.rb', line 50 def value val = children.first val = val.children.first while val.respond_to?(:children) val end |