public
Fork of nathansobo/treetop
Description: A Ruby-based parsing DSL based on parsing expression grammars.
Homepage: http://treetop.rubyforge.org
Clone URL: git://github.com/juretta/treetop.git
Petteri Räty (author)
Thu Mar 13 07:46:17 -0700 2008
Nathan Sobo (committer)
Thu Mar 13 21:31:39 -0700 2008
commit  28f98a7c3c4bccfc3f0de0db7930240698aec8ba
tree    fac482c5693e82454788b35c3da83c2864527944
parent  be4848611026d224ce106ff3249214f957dcb63c
treetop / doc / using_in_ruby.markdown
100644 22 lines (16 sloc) 1.44 kb

Using Treetop Grammars in Ruby

Using the Command Line Compiler

You can .treetop files into Ruby source code with the tt command line script. tt takes an list of files with a .treetop extension and compiles them into .rb files of the same name. You can then require these files like any other Ruby script. Alternately, you can supply just one .treetop file and a -o flag to name specify the name of the output file. Improvements to this compilation script are welcome.

tt foo.treetop bar.treetop
tt foo.treetop -o foogrammar.rb

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.

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.

Treetop.load "arithmetic"

parser = ArithmeticParser.new
if parser.parse('1+1')
  puts 'success'
else
  puts 'failure'
end