Path: | lib/parslet.rb |
Last Update: | Thu Apr 26 16:22:26 -0400 2012 |
A simple parser generator library. Typical usage would look like this:
require 'parslet' class MyParser < Parslet::Parser rule(:a) { str('a').repeat } root(:a) end pp MyParser.new.parse('aaaa') # => 'aaaa'@0 pp MyParser.new.parse('bbbb') # => Parslet::Atoms::ParseFailed: # Don't know what to do with bbbb at line 1 char 1.
The simple DSL allows you to define grammars in PEG-style. This kind of grammar construction does away with the ambiguities that usually comes with parsers; instead, it allows you to construct grammars that are easier to debug, since less magic is involved.
Parslet is typically used in stages:
The first stage is traditionally intermingled with the second stage; output from the second stage is usually called the ‘Abstract Syntax Tree’ or AST.
The stages are completely decoupled; You can change your grammar around and use the second stage to isolate the rest of your code from the changes you‘ve effected.
All parslet atoms are subclasses of Parslet::Atoms::Base. You might want to look at all of those: Parslet::Atoms::Re, Parslet::Atoms::Str, Parslet::Atoms::Repetition, Parslet::Atoms::Sequence, Parslet::Atoms::Alternative.
A parse that fails will raise Parslet::ParseFailed. A detailed explanation of what went wrong can be obtained from the parslet involved or the root of the parser instance.