Skip to content

Commit

Permalink
Merge pull request #30 from kemitchell/magic_comments
Browse files Browse the repository at this point in the history
Keep magic comments at the top of generated files
  • Loading branch information
cjheath committed Feb 3, 2013
2 parents 16d2857 + 6551d54 commit 5ecfa5e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/treetop/compiler/grammar_compiler.rb
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions spec/compiler/grammar_compiler_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions spec/compiler/test_grammar_magic_coding.treetop
@@ -0,0 +1,8 @@
# coding: UTF-8
module Test
grammar Grammar do
rule foo do
'foo'
end
end
end
8 changes: 8 additions & 0 deletions spec/compiler/test_grammar_magic_encoding.treetop
@@ -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.