Class: BELParser::Parsers::AST::Node
- 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.
Direct Known Subclasses
AnnotationDefinition, Argument, BlankLine, Comment, CommentLine, DocumentProperty, Domain, Function, Identifier, Keyword, List, ListItem, MultiIdentifier, Name, NamespaceDefinition, NestedStatement, Object, ObservedTerm, Parameter, Pattern, Prefix, Relationship, Set, SimpleStatement, Statement, String, Subject, Term, Unset, Uri, Url, Value
Class Attribute Summary collapse
-
.ast_type ⇒ Object
readonly
Returns the value of attribute ast_type.
-
.has_semantics ⇒ Object
readonly
Returns the value of attribute has_semantics.
Instance Attribute Summary collapse
-
#character_range ⇒ Object
Get the character range enclosing this AST node.
-
#complete ⇒ Object
Get/Set the complete property.
-
#line_number ⇒ Object
readonly
Get the line number where this AST node originates.
Attributes inherited from AST::Node
Instance Method Summary collapse
-
#add_syntax_error(syntax_error) ⇒ Object
Add a syntax error to thie AST node.
-
#append(element) ⇒ Node
(also: #<<)
Appends ‘element` to `children` and returns the resulting node.
-
#child(index) ⇒ Node?
Get the child at the specified index.
-
#children? ⇒ true, false
Get whether this node has children.
-
#complete? ⇒ Boolean
Get whether the AST node can be considered complete.
-
#concat(array) ⇒ Node
(also: #+)
Concatenates ‘array` with `children` and returns the resulting node.
-
#first_child ⇒ Object
Get the child at index 0.
-
#fourth_child ⇒ Object
Get the child at index 3.
- #freeze ⇒ Object
-
#incomplete? ⇒ Boolean
Get whether the AST node should be considered incomplete.
-
#initialize(type, children = [], properties = {}) ⇒ Node
constructor
New BEL AST node.
-
#num_children ⇒ Fixnum
Get the number of children.
-
#range_end ⇒ Object
(also: #range_b)
Get the end of the character range enclosing this AST node.
-
#range_start ⇒ Object
(also: #range_a)
Get the start of the character range enclosing this AST node.
-
#second_child ⇒ Object
Get the child at index 1.
-
#syntax_errors ⇒ Object
Get the syntax errors for this AST node.
-
#third_child ⇒ Object
Get the child at index 2.
- #traverse(&block) ⇒ Object
- #updated(children = nil, properties = nil) ⇒ Object
Methods inherited from AST::Node
#==, #dup, #eql?, #inspect, #to_a, #to_ast, #to_bel, #to_s, #to_sexp
Methods included from BELParser::Parsers
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}
-
complete
-> #complete?
properties
is not a Hash
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_type ⇒ Object (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_semantics ⇒ Object (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_range ⇒ Object
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 |
#complete ⇒ Object
Get/Set the complete property.
67 68 69 |
# File 'lib/bel_parser/parsers/ast/node.rb', line 67 def complete @complete end |
#line_number ⇒ Object (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.
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.
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.
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.
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.
138 139 140 |
# File 'lib/bel_parser/parsers/ast/node.rb', line 138 def concat(array) updated(@children + array.to_a) end |
#first_child ⇒ Object
Get the child at index 0.
162 163 164 |
# File 'lib/bel_parser/parsers/ast/node.rb', line 162 def first_child child 0 end |
#fourth_child ⇒ Object
Get the child at index 3.
180 181 182 |
# File 'lib/bel_parser/parsers/ast/node.rb', line 180 def fourth_child child 3 end |
#freeze ⇒ Object
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.
101 102 103 |
# File 'lib/bel_parser/parsers/ast/node.rb', line 101 def incomplete? !@complete end |
#num_children ⇒ Fixnum
Get the number of children.
187 188 189 |
# File 'lib/bel_parser/parsers/ast/node.rb', line 187 def num_children @children.size end |
#range_end ⇒ Object 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_start ⇒ Object 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_child ⇒ Object
Get the child at index 1.
168 169 170 |
# File 'lib/bel_parser/parsers/ast/node.rb', line 168 def second_child child 1 end |
#syntax_errors ⇒ Object
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_child ⇒ Object
Get the child at index 2.
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 |