Module: Aurum::Engine::BasicParsingCapability

Defined in:
lib/aurum/engine/parsing_facility.rb

Instance Method Summary collapse

Instance Method Details

#parse(lexer) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/aurum/engine/parsing_facility.rb', line 4

def parse lexer
  @lookahead, @lookaheads = lexer.next_symbol, []
  @state_stack, @value_stack = [0], []
  loop do
    action = parsing_table.actions[current_state][@lookahead.to_s]
    error_recovery unless action
    if action.shift_action?
      @state_stack.push(action.value)
      if action.lookahead_shift?
        @lookaheads << @lookahead
      else
        @value_stack.push(@lookahead) unless @lookahead.is_a?(String)
      end
      @lookahead = lexer.next_symbol
    elsif action.reduce_action?
      pushback_lookaheads(lexer) unless @lookaheads.empty?
      prepare_read_reduce(lexer) if action.read_reduce?
      handle = action.value
      return @value_stack.pop if handle.nonterminal == Aurum::Builder::StartSymbol
      nonterminal = reduce_to(handle, lexer)
      goto = parsing_table.actions[current_state][nonterminal]
      if goto.shift_action?
        @state_stack.push(goto.value)
      elsif goto.reduce_action?          
        lexer.pushback(@lookahead)
        @lookahead = nonterminal
      end
    end
  end
end