Class: OrigenTesters::ATP::AST::Node
- Inherits:
-
AST::Node
- Object
- AST::Node
- OrigenTesters::ATP::AST::Node
- Defined in:
- lib/origen_testers/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.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
Class Method Summary collapse
- ._load(str) ⇒ Object
-
.from_sexp(sexp) ⇒ Object
Create a new node from the given S-expression (a string).
Instance Method Summary collapse
- #_dump(depth) ⇒ Object
-
#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.
-
#excluding_sub_flows ⇒ Object
Returns a copy of node with any sub-flow nodes removed.
-
#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 (or types) 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 14 |
# File 'lib/origen_testers/atp/ast/node.rb', line 8 def initialize(type, children = [], properties = {}) @properties = 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/origen_testers/atp/ast/node.rb', line 5 def description @description end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
5 6 7 |
# File 'lib/origen_testers/atp/ast/node.rb', line 5 def file @file end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/origen_testers/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/origen_testers/atp/ast/node.rb', line 5 def line_number @line_number end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
5 6 7 |
# File 'lib/origen_testers/atp/ast/node.rb', line 5 def properties @properties end |
Class Method Details
._load(str) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/origen_testers/atp/ast/node.rb', line 31 def self._load(str) d = Marshal.load(str) p = d[:properties] p[:id] = d[:id] p[:file] = d[:file] p[:line_number] = d[:line_number] p[:description] = d[:description] n = d[:klass].new(d[:type], d[:children], p) n end |
Instance Method Details
#_dump(depth) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/origen_testers/atp/ast/node.rb', line 16 def _dump(depth) # this strips the @strip information from the instance # d = { type: type, children: children, properties: properties } d = { klass: self.class, id: id, file: file, line_number: line_number, description: description, type: type, children: Processors::Marshal.new.process_all(children), properties: properties } Marshal.dump(d, depth) end |
#add(*nodes) ⇒ Object
Add the given nodes to the children
93 94 95 |
# File 'lib/origen_testers/atp/ast/node.rb', line 93 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?
142 143 144 |
# File 'lib/origen_testers/atp/ast/node.rb', line 142 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
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/origen_testers/atp/ast/node.rb', line 63 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 |
#excluding_sub_flows ⇒ Object
Returns a copy of node with any sub-flow nodes removed
135 136 137 |
# File 'lib/origen_testers/atp/ast/node.rb', line 135 def excluding_sub_flows Processors::SubFlowRemover.new.run(self) end |
#find(*types) ⇒ Object
Returns the first child node of the given type(s) that is found
110 111 112 |
# File 'lib/origen_testers/atp/ast/node.rb', line 110 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.
119 120 121 122 123 124 125 126 127 |
# File 'lib/origen_testers/atp/ast/node.rb', line 119 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
51 52 53 |
# File 'lib/origen_testers/atp/ast/node.rb', line 51 def has_source? !!file end |
#remove(*nodes) ⇒ Object
Remove the given nodes (or types) from the children
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/origen_testers/atp/ast/node.rb', line 98 def remove(*nodes) nodes = nodes.map do |node| if node.is_a?(Symbol) find_all(node) else node end end.flatten.compact updated(nil, children - nodes) end |
#set_flags ⇒ Object
Returns an array containing all flags which are set within the given node
130 131 132 |
# File 'lib/origen_testers/atp/ast/node.rb', line 130 def set_flags Processors::ExtractSetFlags.new.run(self) end |
#source ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/origen_testers/atp/ast/node.rb', line 42 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
86 87 88 89 90 |
# File 'lib/origen_testers/atp/ast/node.rb', line 86 def value val = children.first val = val.children.first while val.respond_to?(:children) val end |