Module: ANTLR3::AST::Tree
- Included in:
- BaseTree
- Defined in:
- lib/antlr3/tree.rb
Overview
ANTLR Abstract Syntax Trees
As ANTLR is concerned, an Abstract Syntax Tree (AST) node is an object that wraps a token, a list of child trees, and some information about the collective source text embodied within the tree and its children.
The Tree module, like the Token and Stream modules, emulates an abstract base class for AST classes; it specifies the attributes that are expected of basic tree nodes as well as the methods trees need to implement.
Terminology
While much of this terminology is probably familiar to most developers, the following brief glossary is intended to clarify terminology used in code throughout the AST library:
- payload
-
either a token value contained within a node or
nil
- flat list (nil tree)
-
a tree node without a token payload, but with more than one children – functions like an array of tree nodes
- root
-
a top-level tree node, i.e. a node that does not have a parent
- leaf
-
a node that does not have any children
- siblings
-
all other nodes sharing the same parent as some node
- ancestors
-
the list of successive parents from a tree node to the root node
- error node
-
a special node used to represent an erroneous series of tokens from an input stream
Instance Attribute Summary collapse
-
#child_index ⇒ Object
Returns the value of attribute child_index.
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#line ⇒ Object
readonly
Returns the value of attribute line.
-
#start_index ⇒ Object
attr_accessor :parent.
-
#stop_index ⇒ Object
Returns the value of attribute stop_index.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#token ⇒ Object
readonly
attr_reader :children.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #ancestors ⇒ Object
- #depth ⇒ Object
- #each_ancestor ⇒ Object
- #has_child?(node) ⇒ Boolean
- #leaf? ⇒ Boolean
- #root ⇒ Object
- #root? ⇒ Boolean (also: #detached?)
- #siblings ⇒ Object
- #walk ⇒ Object
Instance Attribute Details
#child_index ⇒ Object
Returns the value of attribute child_index.
226 227 228 |
# File 'lib/antlr3/tree.rb', line 226 def child_index @child_index end |
#column ⇒ Object (readonly)
Returns the value of attribute column.
230 231 232 |
# File 'lib/antlr3/tree.rb', line 230 def column @column end |
#line ⇒ Object (readonly)
Returns the value of attribute line.
229 230 231 |
# File 'lib/antlr3/tree.rb', line 229 def line @line end |
#start_index ⇒ Object
attr_accessor :parent
224 225 226 |
# File 'lib/antlr3/tree.rb', line 224 def start_index @start_index end |
#stop_index ⇒ Object
Returns the value of attribute stop_index.
225 226 227 |
# File 'lib/antlr3/tree.rb', line 225 def stop_index @stop_index end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
228 229 230 |
# File 'lib/antlr3/tree.rb', line 228 def text @text end |
#token ⇒ Object (readonly)
attr_reader :children
232 233 234 |
# File 'lib/antlr3/tree.rb', line 232 def token @token end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
227 228 229 |
# File 'lib/antlr3/tree.rb', line 227 def type @type end |
Instance Method Details
#ancestors ⇒ Object
277 278 279 |
# File 'lib/antlr3/tree.rb', line 277 def ancestors each_ancestor.to_a end |
#depth ⇒ Object
258 259 260 |
# File 'lib/antlr3/tree.rb', line 258 def depth root? ? 0 : parent.depth + 1 end |
#each_ancestor ⇒ Object
267 268 269 270 271 272 273 274 275 |
# File 'lib/antlr3/tree.rb', line 267 def each_ancestor block_given? or return( enum_for( :each_ancestor ) ) cursor = self until cursor.root? yield( parent_node = cursor.parent ) cursor = parent_node end return( self ) end |
#has_child?(node) ⇒ Boolean
254 255 256 |
# File 'lib/antlr3/tree.rb', line 254 def has_child?( node ) children and children.include?( node ) end |
#leaf? ⇒ Boolean
250 251 252 |
# File 'lib/antlr3/tree.rb', line 250 def leaf? children.nil? or children.empty? end |
#root ⇒ Object
240 241 242 243 244 245 246 247 |
# File 'lib/antlr3/tree.rb', line 240 def root cursor = self until cursor.root? yield( parent_node = cursor.parent ) cursor = parent_node end return( cursor ) end |
#root? ⇒ Boolean Also known as: detached?
235 236 237 |
# File 'lib/antlr3/tree.rb', line 235 def root? parent.nil? end |
#siblings ⇒ Object
262 263 264 265 |
# File 'lib/antlr3/tree.rb', line 262 def siblings root? and return [] parent.children.reject { | c | c.equal?( self ) } end |
#walk ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/antlr3/tree.rb', line 281 def walk block_given? or return( enum_for( :walk ) ) stack = [] cursor = self while true begin yield( cursor ) stack.push( cursor.children.dup ) unless cursor.empty? rescue StopIteration # skips adding children to prune the node ensure break if stack.empty? cursor = stack.last.shift stack.pop if stack.last.empty? end end return self end |