Class: ATP::AST::Node

Inherits:
AST::Node
  • Object
show all
Defined in:
lib/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
# 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

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/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/atp/ast/node.rb', line 5

def line_number
  @line_number
end

Class Method Details

.from_sexp(sexp) ⇒ Object

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



29
30
31
32
# File 'lib/atp/ast/node.rb', line 29

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



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?

Returns:

  • (Boolean)


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

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)



81
82
83
# File 'lib/atp/ast/node.rb', line 81

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

#has_source?Boolean

Returns true if the node carries source file data, retrieve it via the source method

Returns:

  • (Boolean)


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_flagsObject

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



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

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

#sourceObject



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

#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



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