Class: OrigenTesters::ATP::AST::Node

Inherits:
AST::Node
  • Object
show all
Defined in:
lib/origen_testers/atp/ast/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#descriptionObject (readonly)

Returns the value of attribute description.



5
6
7
# File 'lib/origen_testers/atp/ast/node.rb', line 5

def description
  @description
end

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/origen_testers/atp/ast/node.rb', line 5

def file
  @file
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/origen_testers/atp/ast/node.rb', line 6

def id
  @id
end

#line_numberObject (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

#propertiesObject (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



30
31
32
33
34
35
36
37
38
39
# File 'lib/origen_testers/atp/ast/node.rb', line 30

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

.from_sexp(sexp) ⇒ Object

Create a new node from the given S-expression (a string)



55
56
57
58
# File 'lib/origen_testers/atp/ast/node.rb', line 55

def self.from_sexp(sexp)
  @parser ||= Parser.new
  @parser.string_to_ast(sexp)
end

Instance Method Details

#_dump(depth) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# 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



92
93
94
# File 'lib/origen_testers/atp/ast/node.rb', line 92

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?

Returns:

  • (Boolean)


141
142
143
# File 'lib/origen_testers/atp/ast/node.rb', line 141

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/origen_testers/atp/ast/node.rb', line 62

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_flowsObject

Returns a copy of node with any sub-flow nodes removed



134
135
136
# File 'lib/origen_testers/atp/ast/node.rb', line 134

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



109
110
111
# File 'lib/origen_testers/atp/ast/node.rb', line 109

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.



118
119
120
121
122
123
124
125
126
# File 'lib/origen_testers/atp/ast/node.rb', line 118

def find_all(*types)
  options = types.pop if types.last.is_a?(Hash)
  options ||= {}
  if options[: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

Returns:

  • (Boolean)


50
51
52
# File 'lib/origen_testers/atp/ast/node.rb', line 50

def has_source?
  !!file
end

#remove(*nodes) ⇒ Object

Remove the given nodes (or types) from the children



97
98
99
100
101
102
103
104
105
106
# File 'lib/origen_testers/atp/ast/node.rb', line 97

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_flagsObject

Returns an array containing all flags which are set within the given node



129
130
131
# File 'lib/origen_testers/atp/ast/node.rb', line 129

def set_flags
  Processors::ExtractSetFlags.new.run(self)
end

#sourceObject



41
42
43
44
45
46
47
# File 'lib/origen_testers/atp/ast/node.rb', line 41

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

#valueObject

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



85
86
87
88
89
# File 'lib/origen_testers/atp/ast/node.rb', line 85

def value
  val = children.first
  val = val.children.first while val.respond_to?(:children)
  val
end