Class: Aurum::Lexer

Inherits:
Object
  • Object
show all
Defined in:
lib/aurum/engine.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, accepts, lexical_states, input) ⇒ Lexer

Returns a new instance of Lexer.



32
33
34
35
# File 'lib/aurum/engine.rb', line 32

def initialize table, accepts, lexical_states, input
  @table, @accepts, @lexical_states, @input  = table, accepts, lexical_states, input      
  shift_to :initial 
end

Instance Method Details

#goto(state, input) ⇒ Object



63
64
65
66
67
# File 'lib/aurum/engine.rb', line 63

def goto state, input
  return nil unless input
  next_state = @table[state].find {|tran| tran.symbols.include?(input)} 
  next_state ? next_state.destination : nil
end

#next_symbolObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/aurum/engine.rb', line 37

def next_symbol
  return Aurum::EOF if @input.eof?
  @recognized, lexeme, @pushback_symbol = @pushback_symbol, '', nil
  until @recognized
    next_state, char = @start_state, nil
    while next_state
      lexeme << char if char
      state, char = next_state, @input.get_char
      next_state = goto state, char    
    end       
    @input.pushback char
    return Unknown unless actions = @accepts[state]
    if actions.first == IgnoreAction
      lexeme = ''
    else
      actions.first.execute self, lexeme
    end                 
  end
  @recognized.value = lexeme unless @recognized.value 
  @recognized      
end

#pushback(symbol) ⇒ Object



59
60
61
# File 'lib/aurum/engine.rb', line 59

def pushback symbol
  @pushback_symbol = symbol
end

#recognize(token) ⇒ Object



73
74
75
# File 'lib/aurum/engine.rb', line 73

def recognize token
  @recognized = Aurum::Symbol.new token, true
end

#shift_to(state) ⇒ Object



69
70
71
# File 'lib/aurum/engine.rb', line 69

def shift_to state
  @start_state = goto 0, -@lexical_states.index(state)-1
end