Sha256: 958a140d3f3f30a72ffbd4a3121549819aaf25192aa6372dc7f91f832f162632

Contents?: true

Size: 1.83 KB

Versions: 6

Compression:

Stored size: 1.83 KB

Contents

module Treetop
  module Runtime
    class CompiledParser
      include Treetop::Runtime
      
      attr_reader :input, :index

      def parse(input)
        prepare_to_parse(input)
        return root
      end
      
      protected
      
      attr_reader :node_cache
      attr_writer :index
              
      def prepare_to_parse(input)
        @input = input
        reset_index
        @node_cache = Hash.new {|hash, key| hash[key] = Hash.new}
      end
      
      def reset_index
        @index = 0
      end
      
      def parse_char_class(char_class_re, char_class_string, node_class = SyntaxNode, inline_module = nil)
        if input.index(char_class_re, index) == index
          result = node_class.new(input, index...(index + 1))
          result.extend(inline_module) if inline_module
          @index += 1
          result
        else
          terminal_parse_failure("[#{char_class_string}]")
        end
      end
    
      def parse_terminal(terminal_string, node_class = SyntaxNode, inline_module = nil)
        if input.index(terminal_string, index) == index
          result = node_class.new(input, index...(index + terminal_string.length))
          result.extend(inline_module) if inline_module
          @index += terminal_string.length
          result
        else
          terminal_parse_failure(terminal_string)
        end
      end
    
      def parse_anything(node_class = SyntaxNode, inline_module = nil)
        if index < input.length
          result = node_class.new(input, index...(index + 1))
          result.extend(inline_module) if inline_module
          @index += 1
          result
        else
          terminal_parse_failure("any character")
        end
      end
    
      def terminal_parse_failure(expected_string)
        TerminalParseFailure.new(input, index, expected_string)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
treetop-1.0.2 lib/treetop/runtime/compiled_parser.rb
treetop-1.1.1 lib/treetop/runtime/compiled_parser.rb
treetop-1.0.0 lib/treetop/runtime/compiled_parser.rb
treetop-1.0.1 lib/treetop/runtime/compiled_parser.rb
treetop-1.1.0 lib/treetop/runtime/compiled_parser.rb
treetop-1.1.2 lib/treetop/runtime/compiled_parser.rb