public
Description: A Ruby-based parsing DSL based on parsing expression grammars.
Homepage: http://treetop.rubyforge.org
Clone URL: git://github.com/nathansobo/treetop.git
Inlined load_grammar to Treetop.load. No more polluting object. 
Treetop.load needs some
specs to deal with not overriding the default behavior of load for 
non-treetop files
when Treetop module is included.
Nathan Sobo (author)
Tue Dec 18 13:11:30 -0800 2007
commit  b81f53e06308dd4d16f18ac53a945500fe7e4b31
tree    887eed5f73a86b317c59359bccc08f632192ae86
parent  fd36a410fda94fdd0b0d3655889351b5223f7016
...
26
27
28
29
 
30
31
32
...
26
27
28
 
29
30
31
32
0
@@ -26,7 +26,7 @@ The first rule becomes the *root* of the grammar, causing its expression to be m
0
     # use_grammar.rb
0
     require 'rubygems'
0
     require 'treetop'
0
- load_grammar 'my_grammar'
0
+ Treetop.load 'my_grammar'
0
     
0
     parser = MyGrammarParser.new
0
     puts parser.parse('hello chomsky').success? # => true
...
15
16
17
18
 
19
20
21
...
15
16
17
 
18
19
20
21
0
@@ -15,7 +15,7 @@ Due to shortcomings in Ruby's semantics that scope constant definitions in a blo
0
 ##Small Stuff
0
 * Migrate the tests back to RSpec.
0
 * Improve the `tt` command line tool to allow `.treetop` extensions to be elided in its arguments.
0
-* Generate and load temp files with `load_grammar` rather than evaluating strings to improve stack trace readability.
0
+* Generate and load temp files with `Treetop.load` rather than evaluating strings to improve stack trace readability.
0
 * 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.
0
 * Allow the root of a grammar to be dynamically set for testing purposes.
0
 
...
6
7
8
9
 
10
11
12
13
14
 
15
16
17
...
6
7
8
 
9
10
11
12
13
 
14
15
16
17
0
@@ -6,12 +6,12 @@ You can `.treetop` files into Ruby source code with the `tt` command line script
0
     tt foo.treetop -o foogrammar.rb
0
 
0
 ##Loading A Grammar Directly
0
-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.
0
+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.
0
 
0
 ##Instantiating and Using Parsers
0
 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.
0
 
0
- load_grammar "arithmetic"
0
+ Treetop.load "arithmetic"
0
     
0
     parser = ArithmeticParser.new
0
     puts parser.parse('1+1').success?
...
2
3
4
5
 
6
7
8
...
2
3
4
 
5
6
7
8
0
@@ -2,7 +2,7 @@ dir = File.dirname(__FILE__)
0
 require File.expand_path("#{dir}/test_helper")
0
 
0
 require File.expand_path("#{dir}/arithmetic_node_classes")
0
-load_grammar File.expand_path("#{dir}/arithmetic")
0
+Treetop.load File.expand_path("#{dir}/arithmetic")
0
 
0
 class ArithmeticParserTest < Test::Unit::TestCase
0
   include ParserTestHelper
...
2
3
4
5
6
 
 
7
8
9
...
2
3
4
 
 
5
6
7
8
9
0
@@ -2,8 +2,8 @@ dir = File.dirname(__FILE__)
0
 require File.expand_path("#{dir}/test_helper")
0
 require File.expand_path("#{dir}/arithmetic_node_classes")
0
 require File.expand_path("#{dir}/lambda_calculus_node_classes")
0
-load_grammar File.expand_path("#{dir}/arithmetic")
0
-load_grammar File.expand_path("#{dir}/lambda_calculus")
0
+Treetop.load File.expand_path("#{dir}/arithmetic")
0
+Treetop.load File.expand_path("#{dir}/lambda_calculus")
0
 
0
 class Treetop::Runtime::SyntaxNode
0
   def method_missing(method, *args)
...
4
5
6
7
...
4
5
6
 
0
@@ -4,4 +4,3 @@ require File.join(dir, *%w[compiler ruby_builder])
0
 require File.join(dir, *%w[compiler node_classes])
0
 require File.join(dir, *%w[compiler metagrammar]) unless $exclude_metagrammar
0
 require File.join(dir, *%w[compiler grammar_compiler])
0
-require File.join(dir, *%w[compiler load_grammar])
...
20
21
22
23
24
 
 
 
 
25
26
...
20
21
22
 
 
23
24
25
26
27
28
0
@@ -20,7 +20,9 @@ module Treetop
0
     end
0
   end
0
 
0
- def self.load(file)
0
- load_grammar file
0
+ def self.load(path)
0
+ adjusted_path = path =~ /\.(treetop|tt)\Z/ ? path : path + '.treetop'
0
+ compiler = Treetop::Compiler::GrammarCompiler.new
0
+ Object.class_eval(compiler.ruby_source(adjusted_path))
0
   end
0
 end
...
40
41
42
43
44
 
 
45
46
47
48
 
49
50
 
51
52
53
54
55
 
56
57
 
58
59
60
...
40
41
42
 
 
43
44
45
46
47
 
48
49
 
50
51
52
53
54
 
55
56
 
57
58
59
60
0
@@ -40,21 +40,21 @@ describe Compiler::GrammarCompiler do
0
     compiler.ruby_source(source_path_with_treetop_extension).should_not be_nil
0
   end
0
 
0
- specify "load_grammar compiles and evaluates a source grammar with a .treetop extension" do
0
- load_grammar source_path_with_treetop_extension
0
+ specify "Treetop.load compiles and evaluates a source grammar with a .treetop extension" do
0
+ Treetop.load source_path_with_treetop_extension
0
     Test::GrammarParser.new.parse('foo').should_not be_nil
0
   end
0
   
0
- specify "load_grammar compiles and evaluates a source grammar with a .tt extension" do
0
+ specify "Treetop.load compiles and evaluates a source grammar with a .tt extension" do
0
     path_without_extension = source_path_with_tt_extension
0
- load_grammar path_without_extension
0
+ Treetop.load path_without_extension
0
     Test::GrammarParser.new.parse('foo').should_not be_nil
0
   end
0
 
0
 
0
- specify "load_grammar compiles and evaluates source grammar with no extension" do
0
+ specify "Treetop.load compiles and evaluates source grammar with no extension" do
0
     path_without_extension = source_path_with_treetop_extension.gsub(/\.treetop\Z/, '')
0
- load_grammar path_without_extension
0
+ Treetop.load path_without_extension
0
     Test::GrammarParser.new.parse('foo').should_not be_nil
0
   end
0
 
...
5
6
7
8
9
10
 
 
 
11
12
13
...
5
6
7
 
 
 
8
9
10
11
12
13
0
@@ -5,9 +5,9 @@ module GrammarCompositionSpec
0
   describe "several composed grammars" do
0
     before do
0
       dir = File.dirname(__FILE__)
0
- load_grammar File.join(dir, 'a')
0
- load_grammar File.join(dir, 'b')
0
- load_grammar File.join(dir, 'c')
0
+ Treetop.load File.join(dir, 'a')
0
+ Treetop.load File.join(dir, 'b')
0
+ Treetop.load File.join(dir, 'c')
0
       # Check that polyglot finds d.treetop and loads it:
0
       $: << dir
0
       require 'd'

Comments

    No one has commented yet.