Class: Shuwar::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/shuwar/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(tok) ⇒ Parser

Returns a new instance of Parser.



5
6
7
# File 'lib/shuwar/parser.rb', line 5

def initialize(tok)
  @enum = tok.enum_for :each_token
end

Instance Method Details

#each_object(&block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/shuwar/parser.rb', line 44

def each_object(&block)
  if block
    each_object! &block
  else
    enum_for :each_object!
  end
end

#each_object!Object



38
39
40
41
42
# File 'lib/shuwar/parser.rb', line 38

def each_object!
  until input_end?
    yield get_object
  end
end

#get_objectObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/shuwar/parser.rb', line 9

def get_object
  case @enum.peek
    when Tokenizer::OpenParen
      @enum.next # Eat OpenParen
      tmp = []
      until @enum.peek.is_a? Tokenizer::CloseParen
        tmp.push get_object
      end
      @enum.next # Eat CloseParen
      tmp
    when Tokenizer::Quote
      @enum.next # Eat Quote
      [:quote, get_object]
    when Tokenizer::CloseParen
      raise "Got some close paren here. Why?"
    else
      @enum.next
  end
end

#input_end?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/shuwar/parser.rb', line 29

def input_end?
  begin
    @enum.peek
    false
  rescue StopIteration
    true
  end
end