Permalink
Browse files

Merge pull request #30 from kemitchell/magic_comments

Keep magic comments at the top of generated files
  • Loading branch information...
2 parents 16d2857 + 6551d54 commit 5ecfa5ed0fb4644916d757aa9abf2c30fb914f98 @cjheath cjheath committed Feb 3, 2013
@@ -4,8 +4,17 @@ 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(AUTOGENERATED+"\n\n")
- target_file.write(ruby_source(source_path))
+ generated_source = ruby_source(source_path)
+ first_line_break = generated_source.index("\n")
+ first_line = generated_source.slice(0..first_line_break)
+ if /(coding|encoding): (\S+)/.match(first_line)
+ target_file.write(first_line)
+ target_file.write(AUTOGENERATED+"\n\n")
+ target_file.write(generated_source.slice((first_line_break + 1)..-1))
+ else
+ target_file.write(AUTOGENERATED+"\n\n")
+ target_file.write(generated_source)
+ end
end
end
@@ -12,8 +12,12 @@
@source_path_with_treetop_extension = "#{dir}/test_grammar.treetop"
@source_path_with_do = "#{dir}/test_grammar_do.treetop"
@source_path_with_tt_extension = "#{dir}/test_grammar.tt"
+ @source_path_with_magic_coding = "#{dir}/test_grammar_magic_coding.treetop"
+ @source_path_with_magic_encoding = "#{dir}/test_grammar_magic_encoding.treetop"
@target_path = "#{@tmpdir}/test_grammar.rb"
@target_path_with_do = "#{@tmpdir}/test_grammar_do.rb"
+ @target_path_with_magic_coding = "#{@tmpdir}/test_grammar_magic_coding.rb"
+ @target_path_with_magic_encoding = "#{@tmpdir}/test_grammar_magic_encoding.rb"
@alternate_target_path = "#{@tmpdir}/test_grammar_alt.rb"
delete_target_files
end
@@ -82,6 +86,24 @@
Test::GrammarParser.new.parse('foo').should_not be_nil
end
+ specify "grammars with magic 'encoding' comments keep those comments at the top" do
+ src_copy = "#{@tmpdir}/test_grammar_magic_encoding.treetop"
+ File.open(@source_path_with_magic_encoding) do |f|
+ File.open(src_copy,'w'){|o|o.write(f.read)}
+ end
+ compiler.compile(src_copy)
+ File.open(@target_path_with_magic_encoding).readline.should == "# encoding: UTF-8\n"
+ end
+
+ specify "grammars with magic 'coding' comments keep those comments at the top" do
+ src_copy = "#{@tmpdir}/test_grammar_magic_coding.treetop"
+ File.open(@source_path_with_magic_coding) do |f|
+ File.open(src_copy,'w'){|o|o.write(f.read)}
+ end
+ compiler.compile(src_copy)
+ File.open(@target_path_with_magic_coding).readline.should == "# coding: UTF-8\n"
+ end
+
def delete_target_files
File.delete(target_path) if File.exists?(target_path)
File.delete(@target_path_with_do) if File.exists?(@target_path_with_do)
@@ -0,0 +1,8 @@
+# coding: UTF-8
+module Test
+ grammar Grammar do
+ rule foo do
+ 'foo'
+ end
+ end
+end
@@ -0,0 +1,8 @@
+# encoding: UTF-8
+module Test
+ grammar Grammar do
+ rule foo do
+ 'foo'
+ end
+ end
+end

0 comments on commit 5ecfa5e

Please sign in to comment.