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


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

.from_sexp(sexp) ⇒ Object

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


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

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
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?

Returns:

  • (Boolean)

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_flowsObject

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)
  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)

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_flagsObject

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

#sourceObject


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

#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


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