Class: Ast::Tree

Inherits:
Array
  • Object
show all
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

Instance Method Summary collapse

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

#posObject



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

#decObject



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.

Returns:

  • (boolean)

    whether at end of tokens



38
39
40
# File 'lib/ast_ast/tree.rb', line 38

def eot?
  pos >= self.size
end

#incObject



23
24
25
# File 'lib/ast_ast/tree.rb', line 23

def inc
  @pos += 1 unless self.eot?
end

#inspectObject



12
13
14
# File 'lib/ast_ast/tree.rb', line 12

def inspect
  "{ #{self.to_s[1..-2]} }"
end

#pointerToken Also known as: curr_item

Returns the current token being ‘pointed’ to.

Returns:

  • (Token)

    the current token being ‘pointed’ to



32
33
34
# File 'lib/ast_ast/tree.rb', line 32

def pointer
  self[pos]
end

#restObject



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