diff --git a/lib/treetop/compiler/grammar_compiler.rb b/lib/treetop/compiler/grammar_compiler.rb index 7c5ae91..2683e6a 100755 --- a/lib/treetop/compiler/grammar_compiler.rb +++ b/lib/treetop/compiler/grammar_compiler.rb @@ -7,7 +7,12 @@ def compile(source_path, target_path = source_path.gsub(/\.(treetop|tt)\Z/, '.rb end end - # compile a string containing treetop source + # compile a treetop file into ruby + def ruby_source(source_path) + ruby_source_string(File.read(source_path)) + end + + # compile a string containing treetop source into ruby def ruby_source_string(s) parser = MetagrammarParser.new result = parser.parse(s) diff --git a/spec/compiler/grammar_compiler_spec.rb b/spec/compiler/grammar_compiler_spec.rb index b9b591f..10f5070 100755 --- a/spec/compiler/grammar_compiler_spec.rb +++ b/spec/compiler/grammar_compiler_spec.rb @@ -39,6 +39,15 @@ specify "compilation of a single file without writing it to an output file" do compiler.ruby_source(source_path_with_treetop_extension).should_not be_nil end + + specify "ruby_source_string compiles a grammar stored in string" do + compiler.ruby_source_string(File.read(source_path_with_treetop_extension)).should_not be_nil + end + + specify "Treetop.load_string compiles and evaluates a source grammar stored in string" do + Treetop.load_string File.read(source_path_with_treetop_extension) + Test::GrammarParser.new.parse('foo').should_not be_nil + end specify "Treetop.load compiles and evaluates a source grammar with a .treetop extension" do Treetop.load source_path_with_treetop_extension