Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Keep magic comments at the top of generated files #30

Merged
merged 1 commit into from

2 participants

@kemitchell

At present the comments are preserved, but the auto-generation notice bumps them down below line 1 where they've no effect.

The documentation comment on the syntax of magic comments is:

https://github.com/ruby/ruby/blob/7ea675732ac1dac72f07756498706678d8725719/encoding.c#L1697

The added regex test tracks the doc comment, except I don't bother checking the encoding string (say, with Encoding.find($2)), as I figure that's Ruby's business and users will find out soon enough if they've botched an encoding string.

@cjheath cjheath merged commit 5ecfa5e into nathansobo:master
@cjheath
Collaborator

Apparently I pushed this to nathansobo's repo, not to mine - which is where I release from. Sorry. By today I'd forgotten that your patch existed, needed it, wrote it, republished the gem, and only found your pull request when I went to the mailing list archive to notify interested parties that 1.4.14 now has a fox for this. D'oh! But anyhow, it's fixed, and the new gem is released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 1, 2013
  1. @kemitchell
This page is out of date. Refresh to see the latest.
View
13 lib/treetop/compiler/grammar_compiler.rb
@@ -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
View
22 spec/compiler/grammar_compiler_spec.rb
@@ -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)
View
8 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
View
8 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
Something went wrong with that request. Please try again.