Sha256: f2e67bb1dc2779ca981772e24d3cf30b71d6256cd674e5bf3ade4cac61229112

Contents?: true

Size: 1.15 KB

Versions: 11

Compression:

Stored size: 1.15 KB

Contents

module Treetop
  module Compiler
    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(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|
      load_from_string(source_file.read)
    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

11 entries across 11 versions & 6 rubygems

Version Path
cognita-treetop-1.2.4 lib/treetop/compiler/grammar_compiler.rb
skylinecms-3.1.0 vendor/digitpaint/personify/vendor/treetop/lib/treetop/compiler/grammar_compiler.rb
personify-1.0.0 vendor/treetop/lib/treetop/compiler/grammar_compiler.rb
westarete-skylinecms-3.0.8.20100329 vendor/digitpaint/personify/vendor/treetop/lib/treetop/compiler/grammar_compiler.rb
westarete-skylinecms-3.0.8.20100330 vendor/digitpaint/personify/vendor/treetop/lib/treetop/compiler/grammar_compiler.rb
skylinecms-3.0.8 vendor/digitpaint/personify/vendor/treetop/lib/treetop/compiler/grammar_compiler.rb
skylinecms-3.0.7 vendor/digitpaint/personify/vendor/treetop/lib/treetop/compiler/grammar_compiler.rb
mack-0.8.3 lib/gems/treetop-1.2.4/lib/treetop/compiler/grammar_compiler.rb
mack-0.8.3.1 lib/gems/treetop-1.2.4/lib/treetop/compiler/grammar_compiler.rb
treetop-1.2.5 lib/treetop/compiler/grammar_compiler.rb
treetop-1.2.4 lib/treetop/compiler/grammar_compiler.rb