Class: Lrama::Parser::TokenScanner

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

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ TokenScanner

Returns a new instance of TokenScanner.



11
12
13
14
# File 'lib/lrama/parser.rb', line 11

def initialize(tokens)
  @tokens = tokens
  @index = 0
end

Instance Method Details

#consume(*token_types) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/lrama/parser.rb', line 30

def consume(*token_types)
  if token_types.include?(current_type)
    token = current_token
    self.next
    return token
  end

  return nil
end

#consume!(*token_types) ⇒ Object



40
41
42
# File 'lib/lrama/parser.rb', line 40

def consume!(*token_types)
  consume(*token_types) || (raise "#{token_types} is expected but #{current_type}. #{current_token}")
end

#consume_multi(*token_types) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lrama/parser.rb', line 44

def consume_multi(*token_types)
  a = []

  while token_types.include?(current_type)
    a << current_token
    self.next
  end

  raise "No token is consumed. #{token_types}" if a.empty?

  return a
end

#current_tokenObject



16
17
18
# File 'lib/lrama/parser.rb', line 16

def current_token
  @tokens[@index]
end

#current_typeObject



20
21
22
# File 'lib/lrama/parser.rb', line 20

def current_type
  current_token && current_token.type
end

#eots?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/lrama/parser.rb', line 57

def eots?
  current_token.nil?
end

#nextObject



24
25
26
27
28
# File 'lib/lrama/parser.rb', line 24

def next
  token = current_token
  @index += 1
  return token
end