Skip to content

Commit

Permalink
Inlined load_grammar to Treetop.load. No more polluting object. Treet…
Browse files Browse the repository at this point in the history
…op.load needs some

specs to deal with not overriding the default behavior of load for non-treetop files
when Treetop module is included.
  • Loading branch information
Nathan Sobo committed Dec 18, 2007
1 parent fd36a41 commit b81f53e
Show file tree
Hide file tree
Showing 11 changed files with 20 additions and 59 deletions.
2 changes: 1 addition & 1 deletion trunk/README
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The first rule becomes the *root* of the grammar, causing its expression to be m
# use_grammar.rb
require 'rubygems'
require 'treetop'
load_grammar 'my_grammar'
Treetop.load 'my_grammar'

parser = MyGrammarParser.new
puts parser.parse('hello chomsky').success? # => true
Expand Down
2 changes: 1 addition & 1 deletion trunk/doc/contributing_and_planned_features.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Due to shortcomings in Ruby's semantics that scope constant definitions in a blo
##Small Stuff
* Migrate the tests back to RSpec.
* Improve the `tt` command line tool to allow `.treetop` extensions to be elided in its arguments.
* Generate and load temp files with `load_grammar` rather than evaluating strings to improve stack trace readability.
* Generate and load temp files with `Treetop.load` rather than evaluating strings to improve stack trace readability.
* Allow `do/end` style blocks as well as curly brace blocks. This was originally omitted because I thought it would be confusing. It probably isn't.
* Allow the root of a grammar to be dynamically set for testing purposes.

Expand Down
4 changes: 2 additions & 2 deletions trunk/doc/using_in_ruby.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ You can `.treetop` files into Ruby source code with the `tt` command line script
tt foo.treetop -o foogrammar.rb

##Loading A Grammar Directly
The `load_grammar` method takes the path to a `.treetop` file (where the extension is optional), and automatically compiles and evaluates the Ruby source. If you are getting errors in methods you define on the syntax tree, try using the command line compiler for better stack trace feedback. The need to do this is being addressed.
The `Treetop.load` method takes the path to a `.treetop` file (where the extension is optional), and automatically compiles and evaluates the Ruby source. If you are getting errors in methods you define on the syntax tree, try using the command line compiler for better stack trace feedback. The need to do this is being addressed.

##Instantiating and Using Parsers
If a grammar by the name of `Foo` is defined, the compiled Ruby source will define a `FooParser` class. To parse input, create an instance and call its `parse` method with a string.

load_grammar "arithmetic"
Treetop.load "arithmetic"

parser = ArithmeticParser.new
puts parser.parse('1+1').success?
33 changes: 0 additions & 33 deletions trunk/examples/TALK

This file was deleted.

2 changes: 1 addition & 1 deletion trunk/examples/lambda_calculus/arithmetic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require File.expand_path("#{dir}/test_helper")

require File.expand_path("#{dir}/arithmetic_node_classes")
load_grammar File.expand_path("#{dir}/arithmetic")
Treetop.load File.expand_path("#{dir}/arithmetic")

class ArithmeticParserTest < Test::Unit::TestCase
include ParserTestHelper
Expand Down
4 changes: 2 additions & 2 deletions trunk/examples/lambda_calculus/lambda_calculus_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
require File.expand_path("#{dir}/test_helper")
require File.expand_path("#{dir}/arithmetic_node_classes")
require File.expand_path("#{dir}/lambda_calculus_node_classes")
load_grammar File.expand_path("#{dir}/arithmetic")
load_grammar File.expand_path("#{dir}/lambda_calculus")
Treetop.load File.expand_path("#{dir}/arithmetic")
Treetop.load File.expand_path("#{dir}/lambda_calculus")

class Treetop::Runtime::SyntaxNode
def method_missing(method, *args)
Expand Down
1 change: 0 additions & 1 deletion trunk/lib/treetop/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
require File.join(dir, *%w[compiler node_classes])
require File.join(dir, *%w[compiler metagrammar]) unless $exclude_metagrammar
require File.join(dir, *%w[compiler grammar_compiler])
require File.join(dir, *%w[compiler load_grammar])
6 changes: 4 additions & 2 deletions trunk/lib/treetop/compiler/grammar_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def ruby_source(source_path)
end
end

def self.load(file)
load_grammar file
def self.load(path)
adjusted_path = path =~ /\.(treetop|tt)\Z/ ? path : path + '.treetop'
compiler = Treetop::Compiler::GrammarCompiler.new
Object.class_eval(compiler.ruby_source(adjusted_path))
end
end
7 changes: 0 additions & 7 deletions trunk/lib/treetop/compiler/load_grammar.rb

This file was deleted.

12 changes: 6 additions & 6 deletions trunk/spec/compiler/grammar_compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@
compiler.ruby_source(source_path_with_treetop_extension).should_not be_nil
end

specify "load_grammar compiles and evaluates a source grammar with a .treetop extension" do
load_grammar source_path_with_treetop_extension
specify "Treetop.load compiles and evaluates a source grammar with a .treetop extension" do
Treetop.load source_path_with_treetop_extension
Test::GrammarParser.new.parse('foo').should_not be_nil
end

specify "load_grammar compiles and evaluates a source grammar with a .tt extension" do
specify "Treetop.load compiles and evaluates a source grammar with a .tt extension" do
path_without_extension = source_path_with_tt_extension
load_grammar path_without_extension
Treetop.load path_without_extension
Test::GrammarParser.new.parse('foo').should_not be_nil
end


specify "load_grammar compiles and evaluates source grammar with no extension" do
specify "Treetop.load compiles and evaluates source grammar with no extension" do
path_without_extension = source_path_with_treetop_extension.gsub(/\.treetop\Z/, '')
load_grammar path_without_extension
Treetop.load path_without_extension
Test::GrammarParser.new.parse('foo').should_not be_nil
end

Expand Down
6 changes: 3 additions & 3 deletions trunk/spec/composition/grammar_composition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module GrammarCompositionSpec
describe "several composed grammars" do
before do
dir = File.dirname(__FILE__)
load_grammar File.join(dir, 'a')
load_grammar File.join(dir, 'b')
load_grammar File.join(dir, 'c')
Treetop.load File.join(dir, 'a')
Treetop.load File.join(dir, 'b')
Treetop.load File.join(dir, 'c')
# Check that polyglot finds d.treetop and loads it:
$: << dir
require 'd'
Expand Down

0 comments on commit b81f53e

Please sign in to comment.