Sha256: 8e5915bb7e041f4f17646372c5704b3a3a39f4c997e050db3e7b5773517459cc

Contents?: true

Size: 1.41 KB

Versions: 26

Compression:

Stored size: 1.41 KB

Contents

#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

Version data entries

26 entries across 26 versions & 9 rubygems

Version Path
treetop-1.2.4 doc/using_in_ruby.markdown
treetop-1.2.1 doc/using_in_ruby.markdown
treetop-1.2.2 doc/using_in_ruby.markdown
treetop-1.2.3 doc/using_in_ruby.markdown
treetop-1.2.6 doc/using_in_ruby.markdown
treetop-1.2.5 doc/using_in_ruby.markdown