| |
| Posted by Philippe Sigaud | PermalinkReply |
|
Philippe Sigaud
| Hi,
Pegged is a parser generator based on Parsing Expression Grammars (PEG) written in D, that aims to be both simple to use and work at compile-time.
See: https://github.com/PhilippeSigaud/Pegged
To use Pegged, just call the grammar function with a PEG and mix it in your module. For example:
import pegged.grammar;
mixin(grammar(`
Arithmetic:
Term < Factor (Add / Sub)*
Add < "+" Factor
Sub < "-" Factor
Factor < Primary (Mul / Div)*
Mul < "*" Primary
Div < "/" Primary
Primary < Parens / Neg / Pos / Number / Variable
Parens < "(" Term ")"
Neg < "-" Primary
Pos < "+" Primary
Number < ~([0-9]+)
Variable <- identifier
`));
This will create the `Arithmetic` parser, that can parse your usual arithmetic expresion at runtime or compile-time.
// Parsing at compile-time:
enum parseTree1 = Arithmetic("1 + 2 - (3*x-5)*6");
// Runtime:
auto parseTree2 = Arithmetic("1 + 2 - (3*x-5)*6");
-----
This minor release v0.4 (v0.4.1 right now) is on dub (code.dlang.org) right now and introduces the following new features:
* A longest-match alternation operator, |, which will always choose the longest match during a parse. See [https://github.com/PhilippeSigaud/Pegged/wiki/Extended-PEG-Syntax] for more details and the interest of this operator for grammar writers.
* The left-recursion engine, introduced in version v0.3.0 is now fully documented in the wiki at [https://github.com/PhilippeSigaud/Pegged/wiki/Left-Recursion]. Pegged can deal with left recursion, hidden left recursion and indirect left recursion. Try it!
* These improvements allow Pegged to generate a parser that fully parses Extended Pascal files, based on the official ISO 10206:1990 grammar. Have a look at it in the [https://github.com/PhilippeSigaud/Pegged/tree/master/pegged/examples/extended_pascal] directory.
* The new toHTML function can be used to generate an HTML file containing an expandable tree view that can be manipulated with an HTML5-compliant browser. See [https://github.com/PhilippeSigaud/Pegged/wiki/Parse-Result] for more details on this new fun way to explore parse results (and parse failures!).
Pegged documentation can be found on the wiki [https://github.com/PhilippeSigaud/Pegged/wiki], along with a tutorial [https://github.com/PhilippeSigaud/Pegged/wiki/Pegged-Tutorial].
Thanks a lot for Bastiaan Veelo for these wonderful improvements to Pegged!
|