Sha256: fed40dfea049c09e5d811ad99adba23fafb88f9d9ddb61712a368682eaa8c562

Contents?: true

Size: 1.58 KB

Versions: 61

Compression:

Stored size: 1.58 KB

Contents

module Treetop
  module Compiler
    class ParsingRule < Runtime::SyntaxNode

      def compile(builder)
        compile_inline_module_declarations(builder)
        generate_method_definition(builder)
      end
      
      def compile_inline_module_declarations(builder)
        parsing_expression.inline_modules.each_with_index do |inline_module, i|
          inline_module.compile(i, builder, self)
          builder.newline
        end
      end
      
      def generate_method_definition(builder)
        builder.reset_addresses
        expression_address = builder.next_address
        result_var = "r#{expression_address}"
        
        builder.method_declaration(method_name) do
          builder.assign 'start_index', 'index'
          generate_cache_lookup(builder)
          builder.newline
          parsing_expression.compile(expression_address, builder)
          builder.newline
          generate_cache_storage(builder, result_var)
          builder.newline          
          builder << result_var
        end
      end
      
      def generate_cache_lookup(builder)
        builder.if_ "node_cache[:#{name}].has_key?(index)" do
          builder.assign 'cached', "node_cache[:#{name}][index]"
          builder << '@index = cached.interval.end if cached'
          builder << 'return cached'
        end
      end
      
      def generate_cache_storage(builder, result_var)
        builder.assign "node_cache[:#{name}][start_index]", result_var
      end
      
      def method_name
        "_nt_#{name}"
      end
      
      def name
        nonterminal.text_value
      end
    end
  end
end

Version data entries

61 entries across 61 versions & 7 rubygems

Version Path
cloudhead-less-1.1.5 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
cloudhead-less-1.1.6 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
cloudhead-less-1.1.7 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
cloudhead-less-1.1.8 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
cloudhead-less-1.1.9 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
cloudhead-less-1.2.0 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
cloudhead-less-1.2.1 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
cloudhead-less-1.2.2 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
cloudhead-less-1.2.3 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
nirvdrum-less-1.1.4 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
regex-treetop-1.4.8 lib/treetop/compiler/node_classes/parsing_rule.rb
middleman-0.10.17 vendor/gems/gems/treetop-1.4.2/lib/treetop/compiler/node_classes/parsing_rule.rb
middleman-0.10.16 vendor/gems/gems/treetop-1.4.2/lib/treetop/compiler/node_classes/parsing_rule.rb
middleman-0.10.15 vendor/gems/gems/treetop-1.4.2/lib/treetop/compiler/node_classes/parsing_rule.rb
rackjour-0.1.8 vendor/gems/gems/treetop-1.4.2/lib/treetop/compiler/node_classes/parsing_rule.rb
middleman-0.10.14 vendor/gems/gems/treetop-1.4.2/lib/treetop/compiler/node_classes/parsing_rule.rb
treetop-1.4.2 lib/treetop/compiler/node_classes/parsing_rule.rb
treetop-1.4.1 lib/treetop/compiler/node_classes/parsing_rule.rb
less-1.1.13 lib/vendor/treetop/lib/treetop/compiler/node_classes/parsing_rule.rb
treetop-1.3.0 lib/treetop/compiler/node_classes/parsing_rule.rb