www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - PEG lib

Hello,


I have a text processing, matching, parsing, toolkit based on PEG (see belo=
w short description). It should be close to usable, and has some documentat=
ion already. I would be pleased to publish it as a library for others to us=
e eventually (with a free licence).
In the meantime, any kind of review, trial use, argumented critics, would b=
e very useful to help & improve it, esp. to make it good D code -- because =
I'm very new to the language.
Module and doc available at: http://spir.wikidot.com/deematch.


Some characteristics:
* Hopefully, as a tool, it is clear & friendly.
* Grammars/parsers are written in plain D, using a set of typical pattern t=
ypes.
* Produces a tree of rather simple nodes.
* A handful of helper pattern types allow producing an AST directly.
* Custom match actions performed on nodes ("bottom-up processing").
* Testing tools (match trials, specific assertions, test suites).
* Readable feedback (patterns, nodes, tests, errors).


    auto digit      =3D klass("0-9");
    auto natural    =3D charString(digit);
    auto SPACE      =3D drop(optionString(character(' ')));
    auto PLUS       =3D Composition(SPACE, literal("+"), SPACE);
    auto addition   =3D list(natural, PLUS, 2);
    addition.trial("98 + 765  +   4");	// common testing method
=3D=3D>
    ------------------------------------------------------------------
    pattern   : List([0-9]+, Composition(Drop(' '*) "+" Drop(' '*)), 2)
    text      : "98 + 765  +   4"
    outcome   : ["98" "765" "4"]
    ------------------------------------------------------------------


    auto nodes =3D natural.findAll("~~1-23__456=3D=3D=3D");
    writeln(nodes);
=3D=3D>
    ["1", "23", "456"]


    auto node =3D addition.match("98 + foo");
=3D=3D>
******************************************************************
Match Failure: cannot match expected pattern.
   pattern : List([0-9]+,Extract(Drop(' '*) "+" Drop(' '*)),2)
   index   : 5
   found   : "foo"
Cannot find at least 2 elements for List pattern [...]
******************************************************************


Denis
-- -- -- -- -- -- --
vit esse estrany =E2=98=A3

spir.wikidot.com
Nov 21 2010