Sha256: fbb34b6275a0424d7f39c3cb750824f3598457b4823f0d8210d6a38608030a4c

Contents?: true

Size: 1.49 KB

Versions: 80

Compression:

Stored size: 1.49 KB

Contents

module Treetop
  module Compiler
    AUTOGENERATED = "# Autogenerated from a Treetop grammar. Edits may be lost.\n"
    class GrammarCompiler
      def compile(source_path, target_path = source_path.gsub(/\.(treetop|tt)\Z/, '.rb'))
        File.open(target_path, 'w') do |target_file|
          ruby = ruby_source(source_path)
          if ruby =~ /\A#.*\n/
            ruby.sub!(/\n/, "\n"+AUTOGENERATED+"\n\n")
          else
            ruby = AUTOGENERATED+"\n\n"+ruby
          end
          target_file.write(ruby)
        end
      end

      # compile a treetop file into ruby
      def ruby_source(source_path)
        ruby_source_from_string(File.read(source_path))
      end

      # compile a string containing treetop source into ruby
      def ruby_source_from_string(s)
        parser = MetagrammarParser.new
        result = parser.parse(s)
        unless result
          raise RuntimeError.new(parser.failure_reason)
        end
        result.compile
      end
    end
  end

  # compile a treetop source file and load it
  def self.load(path)
    adjusted_path = path =~ /\.(treetop|tt)\Z/ ? path : path + '.treetop'
    File.open(adjusted_path) do |source_file|
      source = source_file.read
      source.gsub!(/\b__FILE__\b/, %Q{"#{adjusted_path}"})
      load_from_string(source)
    end
  end

  # compile a treetop source string and load it
  def self.load_from_string(s)
    compiler = Treetop::Compiler::GrammarCompiler.new
    Object.class_eval(compiler.ruby_source_from_string(s))
  end
end

Version data entries

80 entries across 76 versions & 21 rubygems

Version Path
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb
logstash-output-scalyr-0.1.10.beta vendor/bundle/jruby/2.5.0/gems/treetop-1.4.15/lib/treetop/compiler/grammar_compiler.rb