Class: LL::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/ll/driver.rb

Overview

Parser driver for generated parsers.

Direct Known Subclasses

Parser

Instance Method Summary (collapse)

Instance Method Details

- (Symbol) id_to_terminal(id)

Returns the Symbol of the terminal index.

Parameters:

  • id (Fixnum)

Returns:

  • (Symbol)


63
64
65
# File 'lib/ll/driver.rb', line 63

def id_to_terminal(id)
  return self.class::CONFIG.terminals[id]
end

- (Symbol) id_to_type(id)

Returns the Symbol that belongs to the stack type number.

Examples:

id_to_type(1) # => :terminal

Parameters:

  • id (Fixnum)

Returns:

  • (Symbol)


53
54
55
# File 'lib/ll/driver.rb', line 53

def id_to_type(id)
  return ConfigurationCompiler::TYPES.invert[id]
end

- (Object) parser_error(stack_type, stack_value, token_type, token_value)

Parameters:

  • stack_type (Fixnum)
  • stack_value (Fixnum)
  • token_type (Symbol)
  • token_value (Mixed)

Raises:



12
13
14
15
16
# File 'lib/ll/driver.rb', line 12

def parser_error(stack_type, stack_value, token_type, token_value)
  message = parser_error_message(stack_type, stack_value, token_type)

  raise ParserError, message
end

- (String) parser_error_message(stack_type, stack_value, token_type)

Parameters:

  • stack_type (Fixnum)
  • stack_value (Fixnum)
  • token_type (Symbol)

Returns:

  • (String)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ll/driver.rb', line 24

def parser_error_message(stack_type, stack_value, token_type)
  case id_to_type(stack_type)
  when :rule
    message = "Unexpected #{token_type} for rule #{stack_value}"
  when :terminal
    expected = id_to_terminal(stack_value)
    message  = "Unexpected #{token_type}, expected #{expected} instead"
  when :eof
    message = "Received #{token_type} but there's nothing left to parse"
  when :star
    message = %Q{Unexpected #{token_type} for a "*" operator}
  when :plus
    message = %Q{Unexpected #{token_type} for a "+" operator}
  when :question
    message = %Q{Unexpected #{token_type} for a "?" operator}
  end

  return message
end