Class: Treetop::Compiler::GrammarCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/wildfly/gems/treetop-1.6.8/lib/treetop/compiler/grammar_compiler.rb

Instance Method Summary collapse

Instance Method Details

#compile(source_path, target_path = source_path.gsub(/\.(treetop|tt)\Z/, '.rb')) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/puppet_x/wildfly/gems/treetop-1.6.8/lib/treetop/compiler/grammar_compiler.rb', line 5

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

#ruby_source(source_path) ⇒ Object

compile a treetop file into ruby



18
19
20
# File 'lib/puppet_x/wildfly/gems/treetop-1.6.8/lib/treetop/compiler/grammar_compiler.rb', line 18

def ruby_source(source_path)
  ruby_source_from_string(File.read(source_path))
end

#ruby_source_from_string(s) ⇒ Object

compile a string containing treetop source into ruby



23
24
25
26
27
28
29
30
# File 'lib/puppet_x/wildfly/gems/treetop-1.6.8/lib/treetop/compiler/grammar_compiler.rb', line 23

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