Sha256: 1bfd2bec3644351468c598123d3e1df1057d4fc1f59cb77816970eaed830e273

Contents?: true

Size: 1.36 KB

Versions: 210

Compression:

Stored size: 1.36 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|
          target_file.write(AUTOGENERATED+"\n\n")
          target_file.write(ruby_source(source_path))
        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

210 entries across 169 versions & 16 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/treetop-1.4.10/lib/treetop/compiler/grammar_compiler.rb
classiccms-0.7.4 vendor/bundle/gems/treetop-1.4.10/lib/treetop/compiler/grammar_compiler.rb
classiccms-0.7.3 vendor/bundle/gems/treetop-1.4.10/lib/treetop/compiler/grammar_compiler.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/treetop-1.4.12/lib/treetop/compiler/grammar_compiler.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/treetop-1.4.12/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.31 etc/isolate/jruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.31 etc/isolate/ruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.30 etc/isolate/jruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.30 etc/isolate/ruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.29 etc/isolate/jruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.29 etc/isolate/ruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.28 etc/isolate/jruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.28 etc/isolate/ruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.27 etc/isolate/jruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.27 etc/isolate/ruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.26 etc/isolate/jruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.26 etc/isolate/ruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.25 etc/isolate/jruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.25 etc/isolate/ruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb
depengine-0.0.24 etc/isolate/ruby-1.8/gems/treetop-1.4.8/lib/treetop/compiler/grammar_compiler.rb