Class: ATP::AST::Node
- Inherits:
-
AST::Node
- Object
- AST::Node
- ATP::AST::Node
- 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.
-
#id ⇒ Object
Returns the value of attribute id.
-
#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.
-
#contains?(*types) ⇒ Boolean
Returns true if the node contains any nodes of the given type(s) or if any of its children do.
-
#ensure_node_present(type, *child_nodes) ⇒ Object
Adds an empty node of the given type to the children unless another node of the same type is already present.
-
#find(*types) ⇒ Object
Returns the first child node of the given type(s) that is found.
-
#find_all(*types) ⇒ Object
Returns an array containing all child nodes of the given type(s), by default only considering the immediate children of the node on which this was called.
-
#has_source? ⇒ Boolean
Returns true if the node carries source file data, retrieve it via the source method.
-
#initialize(type, children = [], properties = {}) ⇒ Node
constructor
A new instance of Node.
-
#remove(*nodes) ⇒ Object
Remove the given nodes from the children.
-
#set_flags ⇒ Object
Returns an array containing all flags which are set within the given node.
- #source ⇒ Object
-
#value ⇒ Object
Returns the value at the root of an AST node like this:.
Constructor Details
#initialize(type, children = [], properties = {}) ⇒ Node
Returns a new instance of Node.
8 9 10 11 12 13 |
# File 'lib/atp/ast/node.rb', line 8 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.
5 6 7 |
# File 'lib/atp/ast/node.rb', line 5 def description @description end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
5 6 7 |
# File 'lib/atp/ast/node.rb', line 5 def file @file end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/atp/ast/node.rb', line 6 def id @id end |
#line_number ⇒ Object (readonly)
Returns the value of attribute line_number.
5 6 7 |
# File 'lib/atp/ast/node.rb', line 5 def line_number @line_number end |
Class Method Details
Instance Method Details
#add(*nodes) ⇒ Object
Add the given nodes to the children
66 67 68 |
# File 'lib/atp/ast/node.rb', line 66 def add(*nodes) updated(nil, children + nodes) end |
#contains?(*types) ⇒ Boolean
Returns true if the node contains any nodes of the given type(s) or if any of its children do. To consider only direct children of this node use: node.find_all(*types).empty?
103 104 105 |
# File 'lib/atp/ast/node.rb', line 103 def contains?(*types) !Extractor.new.process(self, types).empty? end |
#ensure_node_present(type, *child_nodes) ⇒ Object
Adds an empty node of the given type to the children unless another node of the same type is already present
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/atp/ast/node.rb', line 36 def ensure_node_present(type, *child_nodes) if children.any? { |n| n.type == type } self else if !child_nodes.empty? node = updated(type, child_nodes) else node = updated(type, []) end updated(nil, children + [node]) end end |
#find(*types) ⇒ Object
Returns the first child node of the given type(s) that is found
76 77 78 |
# File 'lib/atp/ast/node.rb', line 76 def find(*types) children.find { |c| types.include?(c.try(:type)) } end |
#find_all(*types) ⇒ Object
Returns an array containing all child nodes of the given type(s), by default only considering the immediate children of the node on which this was called.
To find all children of the given type by recursively searching through all child nodes, pass recursive: true when calling this method.
85 86 87 88 89 90 91 92 93 |
# File 'lib/atp/ast/node.rb', line 85 def find_all(*types) = types.pop if types.last.is_a?(Hash) ||= {} if [:recursive] Extractor.new.process(self, types) else children.select { |c| types.include?(c.try(:type)) } end end |
#has_source? ⇒ Boolean
Returns true if the node carries source file data, retrieve it via the source method
24 25 26 |
# File 'lib/atp/ast/node.rb', line 24 def has_source? !!file end |
#remove(*nodes) ⇒ Object
Remove the given nodes from the children
71 72 73 |
# File 'lib/atp/ast/node.rb', line 71 def remove(*nodes) updated(nil, children - nodes) end |
#set_flags ⇒ Object
Returns an array containing all flags which are set within the given node
96 97 98 |
# File 'lib/atp/ast/node.rb', line 96 def set_flags Processors::ExtractSetFlags.new.run(self) end |
#source ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/atp/ast/node.rb', line 15 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
59 60 61 62 63 |
# File 'lib/atp/ast/node.rb', line 59 def value val = children.first val = val.children.first while val.respond_to?(:children) val end |