class BELParser::Parsers::AST::Node

BEL application-specific AST node.

All BEL AST nodes have a basic set of properties. Additional properties may be specified by subclasses. Each class in the hierarchy describes its type through the class variable ast_type. This is equivalent to its type instance variable but the former is not used by the AST library itself.

@see Node.ast_type @see Node.initialize

Attributes

ast_type[R]
has_semantics[R]
character_range[RW]

Get the character range enclosing this AST node. It is defined as the close interval containing all the characters of this AST node.

complete[RW]

Get/Set the complete property.

line_number[R]

Get the line number where this AST node originates.

Public Class Methods

new(type, children = [], properties = {}) click to toggle source

New BEL AST node.

@param [Symbol] type The node type symbol @param [Array] children Optional children of node @param [Hash] properties Optional supported node properties

Supported properties - line_number -> {#line_number} -

character_range -> {#character_range}, {#range_start},

{#range_end}
  • complete -> {#complete?}

@raise ArgumentError If children is not an Array or properties

is not a Hash
Calls superclass method AST::Node::new
# File lib/bel_parser/parsers/ast/node.rb, line 45
def initialize(type, children = [], properties = {})
  AST.assert_is_a(Array, children, 'children')
  AST.assert_is_a(Hash, properties, 'properties')
  super(type, children, properties)
end

Public Instance Methods

+(array)
Alias for: concat
<<(element)
Alias for: append
add_syntax_error(syntax_error) click to toggle source

Add a syntax error to thie AST node.

# File lib/bel_parser/parsers/ast/node.rb, line 75
def add_syntax_error(syntax_error)
  syntax_errors << syntax_error
end
append(element) click to toggle source

Appends `element` to `children` and returns the resulting node.

@return [Node]

# File lib/bel_parser/parsers/ast/node.rb, line 147
def append(element)
  updated(@children + [element])
end
Also aliased as: <<
child(index) click to toggle source

Get the child at the specified index.

@param [Fixnum] index the index to get @return [Node, nil] the child of this node at the indicated index

or nil if not present
# File lib/bel_parser/parsers/ast/node.rb, line 156
def child(index)
  @children[index]
end
children?() click to toggle source

Get whether this node has children.

@return [true, false]

# File lib/bel_parser/parsers/ast/node.rb, line 194
def children?
  !@children.empty?
end
complete?() click to toggle source

Get whether the AST node can be considered complete.

# File lib/bel_parser/parsers/ast/node.rb, line 96
def complete?
  @complete
end
concat(array) click to toggle source

Concatenates `array` with `children` and returns the resulting node.

@return [Node]

# File lib/bel_parser/parsers/ast/node.rb, line 138
def concat(array)
  updated(@children + array.to_a)
end
Also aliased as: +
first_child() click to toggle source

Get the child at index 0. @see child

# File lib/bel_parser/parsers/ast/node.rb, line 162
def first_child
  child 0
end
fourth_child() click to toggle source

Get the child at index 3. @see child

# File lib/bel_parser/parsers/ast/node.rb, line 180
def fourth_child
  child 3
end
freeze() click to toggle source
# File lib/bel_parser/parsers/ast/node.rb, line 116
def freeze
  # no freeze...nothing I want to be able to modify properties!
  self
end
incomplete?() click to toggle source

Get whether the AST node should be considered incomplete.

# File lib/bel_parser/parsers/ast/node.rb, line 101
def incomplete?
  !@complete
end
num_children() click to toggle source

Get the number of children.

@return [Fixnum] the number of children

# File lib/bel_parser/parsers/ast/node.rb, line 187
def num_children
  @children.size
end
range_a()

Get a in the character range [a, b].

Alias for: range_start
range_b()

Get b in the character range [a, b].

Alias for: range_end
range_end() click to toggle source

Get the end of the character range enclosing this AST node.

# File lib/bel_parser/parsers/ast/node.rb, line 88
def range_end
  @character_range[1]
end
Also aliased as: range_b
range_start() click to toggle source

Get the start of the character range enclosing this AST node.

# File lib/bel_parser/parsers/ast/node.rb, line 80
def range_start
  @character_range[0]
end
Also aliased as: range_a
second_child() click to toggle source

Get the child at index 1. @see child

# File lib/bel_parser/parsers/ast/node.rb, line 168
def second_child
  child 1
end
syntax_errors() click to toggle source

Get the syntax errors for this AST node.

# File lib/bel_parser/parsers/ast/node.rb, line 70
def syntax_errors
  (@syntax_errors ||= [])
end
third_child() click to toggle source

Get the child at index 2. @see child

# File lib/bel_parser/parsers/ast/node.rb, line 174
def third_child
  child 2
end
traverse() { |self| ... } click to toggle source
# File lib/bel_parser/parsers/ast/node.rb, line 105
def traverse(&block)
  if block_given?
    yield self
    children.each do |child_node|
      child_node.traverse(&block) if child_node.respond_to?(:traverse)
    end
  else
    enum_for(:traverse)
  end
end
updated(children = nil, properties = nil) click to toggle source
# File lib/bel_parser/parsers/ast/node.rb, line 121
def updated(children = nil, properties = nil)
  new_children   = children   || @children
  new_properties = properties || {}

  if @children == new_children && properties.nil?
    self
  elsif instance_of? Node
    original_dup.send :initialize, @type, new_children, new_properties
  else # self.is_a? Node
    original_dup.send :initialize, new_children, new_properties
  end
end