Skip to content

Commit

Permalink
Making Polyglot only load when required first. Updated docs and specs…
Browse files Browse the repository at this point in the history
… to reflect this
  • Loading branch information
mikel committed Dec 27, 2009
1 parent dfb3c54 commit 19cd021
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 7 additions & 0 deletions doc/using_in_ruby.markdown
Expand Up @@ -8,6 +8,13 @@ You can `.treetop` files into Ruby source code with the `tt` command line script
##Loading A Grammar Directly
The Polyglot gem makes it possible to load `.treetop` or `.tt` files directly with `require`. This will invoke `Treetop.load`, which automatically compiles the grammar to Ruby and then 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. A better solution to this issue is in the works.

In order to use Polyglot dynamic loading of `.treetop` or `.tt` files though, you need to require the Polyglot gem before you require the Treetop gem as Treetop will only create hooks into Polyglot for the treetop files if Polyglot is already loaded. So you need to use:

require 'polyglot'
require 'treetop'

in order to use Polyglot auto loading with Treetop in Ruby.

##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. The parser will return the syntax tree of the match or `nil` if there is a failure.

Expand Down
5 changes: 3 additions & 2 deletions lib/treetop.rb
Expand Up @@ -12,7 +12,8 @@ module Treetop
require File.join(TREETOP_ROOT, "runtime")
require File.join(TREETOP_ROOT, "compiler")

unless defined?(TREETOP_DISABLE_POLYGLOT)
require 'polyglot'
# To have Polyglot extensions loaded, you need to require 'polyglot'
# before you require 'treetop'
if defined?(Polyglot)
Polyglot.register(Treetop::VALID_GRAMMAR_EXT, Treetop)
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -2,6 +2,7 @@
require 'rubygems'
require 'benchmark'
require 'spec'
require 'polyglot'

unless $bootstrapped_gen_1_metagrammar
load File.join(dir, '..', 'lib', 'treetop', 'bootstrap_gen_1_metagrammar.rb')
Expand Down

0 comments on commit 19cd021

Please sign in to comment.