Sha256: 930f356def26d444ec81637882b504a5b4cf1eea2a1af09a0a80a6d1184609fb

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module Treetop
  module Compiler
    class Choice < ParsingExpression
      def compile(address, builder, parent_expression = nil)
        super
        begin_comment(self)
        use_vars :result, :start_index
        compile_alternatives(alternatives)
        end_comment(self)
      end
      
      def compile_alternatives(alternatives)
        obtain_new_subexpression_address
        alternatives.first.compile(subexpression_address, builder)
        builder.if__ subexpression_success? do
	  # Undo lazy instantiation:
	  builder << "#{subexpression_result_var} = SyntaxNode.new(input, (index-1)...index) if #{subexpression_result_var} == true"
          assign_result subexpression_result_var
          extend_result_with_declared_module
          extend_result_with_inline_module
        end
        builder.else_ do
          if alternatives.size == 1
            reset_index
            assign_failure start_index_var
          else
            compile_alternatives(alternatives[1..-1])
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
treetop-1.5.3 lib/treetop/compiler/node_classes/choice.rb
treetop-1.5.1 lib/treetop/compiler/node_classes/choice.rb