lib/treetop/compiler/grammar_compiler.rb in treetop-1.4.12 vs lib/treetop/compiler/grammar_compiler.rb in treetop-1.4.14
- old
+ new
@@ -2,11 +2,16 @@
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))
+ 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)