Class: BELParser::Parsers::AST::Node

Inherits:
AST::Node
  • Object
show all
Defined in:
lib/bel_parser/parsers/ast/node.rb

Overview

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.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from AST::Node

#children, #hash, #type

Instance Method Summary collapse

Methods inherited from AST::Node

#==, #dup, #eql?, #inspect, #to_a, #to_ast, #to_bel, #to_s, #to_sexp

Methods included from BELParser::Parsers

#serialize

Constructor Details

#initialize(type, children = [], properties = {}) ⇒ Node

New BEL AST node.

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}

properties

is not a Hash

Parameters:

  • type (Symbol)

    The node type symbol @param [Array]

Raises:

  • ArgumentError If children is not an Array or



45
46
47
48
49
# 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

Class Attribute Details

.ast_typeObject (readonly)

Returns the value of attribute ast_type.



52
53
54
# File 'lib/bel_parser/parsers/ast/node.rb', line 52

def ast_type
  @ast_type
end

.has_semanticsObject (readonly)

Returns the value of attribute has_semantics.



56
57
58
# File 'lib/bel_parser/parsers/ast/node.rb', line 56

def has_semantics
  @has_semantics
end

Instance Attribute Details

#character_rangeObject

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



64
65
66
# File 'lib/bel_parser/parsers/ast/node.rb', line 64

def character_range
  @character_range
end

#completeObject

Get/Set the complete property.



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

def complete
  @complete
end

#line_numberObject (readonly)

Get the line number where this AST node originates.



59
60
61
# File 'lib/bel_parser/parsers/ast/node.rb', line 59

def line_number
  @line_number
end

Instance Method Details

#add_syntax_error(syntax_error) ⇒ Object

Add a syntax error to thie AST node.



75
76
77
# File 'lib/bel_parser/parsers/ast/node.rb', line 75

def add_syntax_error(syntax_error)
  syntax_errors << syntax_error
end

#append(element) ⇒ Node Also known as: <<

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

Returns:



147
148
149
# File 'lib/bel_parser/parsers/ast/node.rb', line 147

def append(element)
  updated(@children + [element])
end

#child(index) ⇒ Node?

Get the child at the specified index.

Parameters:

  • index (Fixnum)

    the index to get

Returns:

  • (Node, nil)

    the child of this node at the indicated index or nil if not present



156
157
158
# File 'lib/bel_parser/parsers/ast/node.rb', line 156

def child(index)
  @children[index]
end

#children?true, false

Get whether this node has children.

Returns:

  • (true, false)


194
195
196
# File 'lib/bel_parser/parsers/ast/node.rb', line 194

def children?
  !@children.empty?
end

#complete?Boolean

Get whether the AST node can be considered complete.

Returns:

  • (Boolean)


96
97
98
# File 'lib/bel_parser/parsers/ast/node.rb', line 96

def complete?
  @complete
end

#concat(array) ⇒ Node Also known as: +

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

Returns:



138
139
140
# File 'lib/bel_parser/parsers/ast/node.rb', line 138

def concat(array)
  updated(@children + array.to_a)
end

#first_childObject

Get the child at index 0.

See Also:



162
163
164
# File 'lib/bel_parser/parsers/ast/node.rb', line 162

def first_child
  child 0
end

#fourth_childObject

Get the child at index 3.

See Also:



180
181
182
# File 'lib/bel_parser/parsers/ast/node.rb', line 180

def fourth_child
  child 3
end

#freezeObject



116
117
118
119
# 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?Boolean

Get whether the AST node should be considered incomplete.

Returns:

  • (Boolean)


101
102
103
# File 'lib/bel_parser/parsers/ast/node.rb', line 101

def incomplete?
  !@complete
end

#num_childrenFixnum

Get the number of children.

Returns:

  • (Fixnum)

    the number of children



187
188
189
# File 'lib/bel_parser/parsers/ast/node.rb', line 187

def num_children
  @children.size
end

#range_endObject Also known as: range_b

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



88
89
90
# File 'lib/bel_parser/parsers/ast/node.rb', line 88

def range_end
  @character_range[1]
end

#range_startObject Also known as: range_a

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



80
81
82
# File 'lib/bel_parser/parsers/ast/node.rb', line 80

def range_start
  @character_range[0]
end

#second_childObject

Get the child at index 1.

See Also:



168
169
170
# File 'lib/bel_parser/parsers/ast/node.rb', line 168

def second_child
  child 1
end

#syntax_errorsObject

Get the syntax errors for this AST node.



70
71
72
# File 'lib/bel_parser/parsers/ast/node.rb', line 70

def syntax_errors
  (@syntax_errors ||= [])
end

#third_childObject

Get the child at index 2.

See Also:



174
175
176
# File 'lib/bel_parser/parsers/ast/node.rb', line 174

def third_child
  child 2
end

#traverse(&block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# 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