Class: Ast::Tree
- Inherits:
-
Array
- Object
- Array
- Ast::Tree
- Defined in:
- lib/ast_ast/tree.rb
Overview
Trees are similar to tokens, in that they have a pointer but trees are meant to be traversed. They can have branches (Trees within Tress).
Scanning/Checking/Skipping collapse
Scanning/Checking/Skipping collapse
- #check(type = nil) ⇒ Object
- #dec ⇒ Object
-
#eot? ⇒ boolean
Whether at end of tokens.
- #inc ⇒ Object
-
#pointer ⇒ Token
(also: #curr_item)
The current token being ‘pointed’ to.
- #rest ⇒ Object
- #scan(type = nil) ⇒ Object
- #skip(type = nil) ⇒ Object
Instance Method Summary collapse
-
#initialize(*args) ⇒ Tree
constructor
A new instance of Tree.
- #inspect ⇒ Object
Constructor Details
#initialize(*args) ⇒ Tree
Returns a new instance of Tree.
7 8 9 10 |
# File 'lib/ast_ast/tree.rb', line 7 def initialize(*args) @pos = 0 super end |
Instance Attribute Details
#pos ⇒ Object
19 20 21 |
# File 'lib/ast_ast/tree.rb', line 19 def pos @pos end |
Instance Method Details
#check(type = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ast_ast/tree.rb', line 52 def check(type=nil) if type.nil? self.pointer else if self.pointer.type == type self.pointer else raise Error, "wrong type: #{type} for #{self.pointer}" end end end |
#dec ⇒ Object
27 28 29 |
# File 'lib/ast_ast/tree.rb', line 27 def dec @pos -= 1 unless @pos == 1 end |
#eot? ⇒ boolean
Returns whether at end of tokens.
38 39 40 |
# File 'lib/ast_ast/tree.rb', line 38 def eot? pos >= self.size end |
#inc ⇒ Object
23 24 25 |
# File 'lib/ast_ast/tree.rb', line 23 def inc @pos += 1 unless self.eot? end |
#inspect ⇒ Object
12 13 14 |
# File 'lib/ast_ast/tree.rb', line 12 def inspect "{ #{self.to_s[1..-2]} }" end |
#pointer ⇒ Token Also known as: curr_item
Returns the current token being ‘pointed’ to.
32 33 34 |
# File 'lib/ast_ast/tree.rb', line 32 def pointer self[pos] end |
#rest ⇒ Object
48 49 50 |
# File 'lib/ast_ast/tree.rb', line 48 def rest self[@pos..-1] end |
#scan(type = nil) ⇒ Object
42 43 44 45 46 |
# File 'lib/ast_ast/tree.rb', line 42 def scan(type=nil) a = self.check(type) self.inc a end |
#skip(type = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ast_ast/tree.rb', line 64 def skip(type=nil) if type.nil? self.inc else if self.pointer.type == type self.inc else raise Error, "wrong type: #{type} for #{self.pointer}" end end end |