Class: ATP::AST::Node

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Factories

#n, #n0

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

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

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

.from_sexp(sexp) ⇒ Object

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



25
26
27
28
# File 'lib/atp/ast/node.rb', line 25

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

Instance Method Details

#add(*nodes) ⇒ Object

Add the given nodes to the children



62
63
64
# File 'lib/atp/ast/node.rb', line 62

def add(*nodes)
  updated(nil, children + nodes)
end

#ensure_node_present(type, child_nodes = nil) ⇒ 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
39
40
41
42
43
# File 'lib/atp/ast/node.rb', line 32

def ensure_node_present(type, child_nodes = nil)
  if children.any? { |n| n.type == type }
    self
  else
    if child_nodes
      node = n(type, *child_nodes)
    else
      node = n0(type)
    end
    updated(nil, children + [node])
  end
end

#find(type) ⇒ Object

Returns the first child node of the given type that is found



72
73
74
# File 'lib/atp/ast/node.rb', line 72

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)



77
78
79
# File 'lib/atp/ast/node.rb', line 77

def find_all(*types)
  children.select { |c| types.include?(c.try(:type)) }
end

#remove(*nodes) ⇒ Object

Remove the given nodes from the children



67
68
69
# File 'lib/atp/ast/node.rb', line 67

def remove(*nodes)
  updated(nil, children - nodes)
end

#sourceObject



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

#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



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

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