Class: Treetop::Runtime::CompiledParser
- Inherits:
-
Object
- Object
- Treetop::Runtime::CompiledParser
- Includes:
- Treetop::Runtime
- Defined in:
- lib/treetop/runtime/compiled_parser.rb
Direct Known Subclasses
Constant Summary collapse
- OtherThan =
'something other than '
Instance Attribute Summary collapse
-
#consume_all_input ⇒ Object
(also: #consume_all_input?)
Returns the value of attribute consume_all_input.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#max_terminal_failure_index ⇒ Object
readonly
Returns the value of attribute max_terminal_failure_index.
-
#root ⇒ Object
writeonly
Sets the attribute root.
Instance Method Summary collapse
- #failure_column ⇒ Object
- #failure_index ⇒ Object
- #failure_line ⇒ Object
- #failure_reason ⇒ Object
-
#initialize ⇒ CompiledParser
constructor
A new instance of CompiledParser.
- #parse(input, options = {}) ⇒ Object
- #terminal_failures ⇒ Object
Constructor Details
#initialize ⇒ CompiledParser
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_input ⇒ Object 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 |
#index ⇒ Object
Returns the value of attribute index.
6 7 8 |
# File 'lib/treetop/runtime/compiled_parser.rb', line 6 def index @index end |
#input ⇒ Object (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_index ⇒ Object (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
7 8 9 |
# File 'lib/treetop/runtime/compiled_parser.rb', line 7 def root=(value) @root = value end |
Instance Method Details
#failure_column ⇒ Object
38 39 40 |
# File 'lib/treetop/runtime/compiled_parser.rb', line 38 def failure_column @terminal_failures && input.column_of(failure_index) end |
#failure_index ⇒ Object
30 31 32 |
# File 'lib/treetop/runtime/compiled_parser.rb', line 30 def failure_index max_terminal_failure_index end |
#failure_line ⇒ Object
34 35 36 |
# File 'lib/treetop/runtime/compiled_parser.rb', line 34 def failure_line @terminal_failures && input.line_of(failure_index) end |
#failure_reason ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/treetop/runtime/compiled_parser.rb', line 43 def failure_reason return nil unless (tf = terminal_failures) && tf.size > 0 "Expected " + (tf.size == 1 ? (tf[0].unexpected ? OtherThan : '')+tf[0].expected_string : "one of #{tf.map{|f| (f.unexpected ? OtherThan : '')+f.expected_string}.uniq*', '}" ) + " at line #{failure_line}, column #{failure_column} (byte #{failure_index+1})" + (failure_index > 0 ? " after #{input[index...failure_index]}" : '') end |
#parse(input, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/treetop/runtime/compiled_parser.rb', line 15 def parse(input, = {}) prepare_to_parse(input) @index = [:index] if [:index] result = send("_nt_#{[:root] || root}") should_consume_all = .include?(:consume_all_input) ? [:consume_all_input] : consume_all_input? if (should_consume_all && index != input.size) if index > max_terminal_failure_index # Otherwise the failure is already explained terminal_parse_failure('<END OF INPUT>', true) end return nil end return SyntaxNode.new(input, index...(index + 1)) if result == true return result end |
#terminal_failures ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/treetop/runtime/compiled_parser.rb', line 54 def terminal_failures if @terminal_failures.empty? || @terminal_failures[-1].is_a?(TerminalParseFailure) @terminal_failures else @terminal_failures.map! {|tf_ary| tf_ary.is_a?(TerminalParseFailure) ? tf_ary : TerminalParseFailure.new(*tf_ary) } end end |