Class: Lrama::Parser::TokenScanner

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

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ TokenScanner

Returns a new instance of TokenScanner.



4
5
6
7
# File 'lib/lrama/parser/token_scanner.rb', line 4

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

Instance Method Details

#consume(*token_types) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/lrama/parser/token_scanner.rb', line 27

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

  return nil
end

#consume!(*token_types) ⇒ Object



35
36
37
# File 'lib/lrama/parser/token_scanner.rb', line 35

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

#consume_multi(*token_types) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lrama/parser/token_scanner.rb', line 39

def consume_multi(*token_types)
  a = []

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

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

  return a
end

#current_tokenObject



9
10
11
# File 'lib/lrama/parser/token_scanner.rb', line 9

def current_token
  @tokens[@index]
end

#current_typeObject



13
14
15
# File 'lib/lrama/parser/token_scanner.rb', line 13

def current_type
  current_token&.type
end

#eots?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/lrama/parser/token_scanner.rb', line 51

def eots?
  current_token.nil?
end

#nextObject



21
22
23
24
25
# File 'lib/lrama/parser/token_scanner.rb', line 21

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

#previous_tokenObject



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

def previous_token
  @tokens[@index - 1]
end