Pegged is a parsing expression grammar (PEG) generator implemented in the D programming language.
The idea is to give the generator a PEG, with the syntax presented in the reference article . From this grammar definition a set of related parsers will be created, to be used at runtime or compile time.
To use Pegged, just call the
grammar function with a PEG and mix it in. For example:
import pegged.grammar; mixin(grammar( "Expr <- Factor AddExpr* AddExpr <- ('+'/'-') Factor Factor <- Primary MulExpr* MulExpr <- ('*'/'/') Primary Primary <- Parens / Number / Variable / '-' Primary Parens <- '(' Expr ')' Number <~ [0-9]+ Variable <- Identifier"));
This creates the
Factor (and so on) parsers for basic arithmetic expressions with operator precedence ('*' and '/' bind stronger than '+' or '-').
Identifier is a pre-defined parser recognizing your basic C-style identifier (first a letter or underscore, then digits, letters or underscores). In the rest of this document, I'll call 'rule' a
Parser <- Parsing Expression expression and I'll use 'grammar' to designate the entire group of rules given to
To use a parser, use the
.parse method. It will return a parse tree containing the calls to the different rules:
// Parsing at compile-time: enum parseTree1 = Expr.parse("1 + 2 - (3*x-5)*6"); pragma(msg, parseTree1.capture); writeln(parseTree1); // And at runtime too: auto parseTree2 = Expr.parse(" 0 + 123 - 456 "); assert(parseTree2.capture == ["0", "+", "123", "-", "456"]);
By default, the grammars are not space-sensitive, because I found it to be what I want most of the time. There is an opt-out, though. This may change in the future.
Here is a little Pegged Tutorial
mixin(grammar(rules));in a module and then grammars and rules can refer to one another. That way, you can have utility grammars providing their functionalities to other grammars. Grammar Composition
More advanced features, outside the standard PEG perimeter are there to bring more power in the mix:
"List(E, Sep) <- E (Sep E)*"is possible. The previous rule defines a parameterized parser taking two other parsers (namely,
Sep) to match a
Sep-separated list of
E's. See Parameterized Rules to see what's possible.
=operator. The parse tree generated by the parser (so, also its matches) is delivered to the user in the output. Other parsers in the grammar see the named captures too. See Named Captures for more explanations on this.
o => o). Too bad.
Pegged is herein released with the Boost licence (like most D projects). See here for more details.
Author: Philippe Sigaud.