Class: Treetop::Runtime::CompiledParser

Inherits:
Object
  • Object
show all
Includes:
Treetop::Runtime
Defined in:
lib/treetop/runtime/compiled_parser.rb

Direct Known Subclasses

Compiler::MetagrammarParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompiledParser

Returns a new instance of CompiledParser.



11
12
13
# File 'lib/treetop/runtime/compiled_parser.rb', line 11

def initialize
  self.consume_all_input = true
end

Instance Attribute Details

#consume_all_inputObject Also known as: consume_all_input?

Returns the value of attribute consume_all_input.



8
9
10
# File 'lib/treetop/runtime/compiled_parser.rb', line 8

def consume_all_input
  @consume_all_input
end

#indexObject

Returns the value of attribute index.



6
7
8
# File 'lib/treetop/runtime/compiled_parser.rb', line 6

def index
  @index
end

#inputObject (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/treetop/runtime/compiled_parser.rb', line 6

def input
  @input
end

#max_terminal_failure_indexObject (readonly)

Returns the value of attribute max_terminal_failure_index.



6
7
8
# File 'lib/treetop/runtime/compiled_parser.rb', line 6

def max_terminal_failure_index
  @max_terminal_failure_index
end

#root=(value) ⇒ Object (writeonly)

Sets the attribute root

Parameters:

  • value

    the value to set the attribute root to.



7
8
9
# File 'lib/treetop/runtime/compiled_parser.rb', line 7

def root=(value)
  @root = value
end

Instance Method Details

#failure_columnObject



33
34
35
# File 'lib/treetop/runtime/compiled_parser.rb', line 33

def failure_column
  @terminal_failures && input.column_of(failure_index)
end

#failure_indexObject



25
26
27
# File 'lib/treetop/runtime/compiled_parser.rb', line 25

def failure_index
  max_terminal_failure_index
end

#failure_lineObject



29
30
31
# File 'lib/treetop/runtime/compiled_parser.rb', line 29

def failure_line
  @terminal_failures && input.line_of(failure_index)
end

#failure_reasonObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/treetop/runtime/compiled_parser.rb', line 37

def failure_reason
  return nil unless (tf = terminal_failures) && tf.size > 0
  "Expected " +
    (tf.size == 1 ?
     tf[0].expected_string :
           "one of #{tf.map{|f| f.expected_string}.uniq*', '}"
    ) +
          " at line #{failure_line}, column #{failure_column} (byte #{failure_index+1})" +
          " after #{input[index...failure_index]}"
end

#parse(input, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/treetop/runtime/compiled_parser.rb', line 15

def parse(input, options = {})
  prepare_to_parse(input)
  @index = options[:index] if options[:index]
  result = send("_nt_#{options[:root] || root}")
  should_consume_all = options.include?(:consume_all_input) ? options[:consume_all_input] : consume_all_input?
  return nil if (should_consume_all && index != input.size)
  return SyntaxNode.new(input, index...(index + 1)) if result == true
  return result
end

#terminal_failuresObject



48
49
50
51
52
53
54
# File 'lib/treetop/runtime/compiled_parser.rb', line 48

def terminal_failures
  if @terminal_failures.empty? || @terminal_failures[0].is_a?(TerminalParseFailure)
    @terminal_failures
  else
    @terminal_failures.map! {|tf_ary| TerminalParseFailure.new(*tf_ary) }
  end
end