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
Get/Set the complete property.
Get the line number where this AST
node originates.
Public Class Methods
@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
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
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
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
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
Get whether this node has children.
@return [true, false]
# File lib/bel_parser/parsers/ast/node.rb, line 194 def children? !@children.empty? end
Get whether the AST
node can be considered complete.
# File lib/bel_parser/parsers/ast/node.rb, line 96 def complete? @complete end
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
Get the child at index 0. @see child
# File lib/bel_parser/parsers/ast/node.rb, line 162 def first_child child 0 end
Get the child at index 3. @see child
# File lib/bel_parser/parsers/ast/node.rb, line 180 def fourth_child child 3 end
# 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
Get whether the AST
node should be considered incomplete.
# File lib/bel_parser/parsers/ast/node.rb, line 101 def incomplete? !@complete end
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
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
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
Get the child at index 1. @see child
# File lib/bel_parser/parsers/ast/node.rb, line 168 def second_child child 1 end
Get the syntax errors for this AST
node.
# File lib/bel_parser/parsers/ast/node.rb, line 70 def syntax_errors (@syntax_errors ||= []) end
Get the child at index 2. @see child
# File lib/bel_parser/parsers/ast/node.rb, line 174 def third_child child 2 end
# 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
# 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