Pegged, a Parsing Expression Grammar (PEG) generator in D

Philippe Sigaud philippe.sigaud at gmail.com
Sat Mar 10 23:26:33 PST 2012


On Sun, Mar 11, 2012 at 00:56, Andrej Mitrovic
<andrej.mitrovich at gmail.com> wrote:
> I see you are not the only one who started writing string array
> literals like this:
>
> enum PEGCode = grammarCode!(
>     "Grammar <- S Definition+ EOI"
>    ,"Definition <- RuleName Arrow Expression"
>    ,"RuleName   <- Identifier>(ParamList?)"
>    ,"Expression <- Sequence (OR Sequence)*"
> );
>
> IOW comma on the left side. I know it's not a style preference but
> actually a (unfortunate but needed) technique for avoiding bugs. :)

Yes, I use what I call "Haskell-comma" (I saw it first on Haskell
code). But in the previous sample, that's old code. Now, that should
only be;

enum code = grammar("
    Grammar <- S Definition+ EOI
    Definition <- RuleName Arrow Expression
    RuleName   <- Identifier>(ParamList?)
    Expression <- Sequence (OR Sequence)*
");

I see I didn't update bootstrap.d, I'll do that.


> So can you use this to actually parse D code, extract syntax trees and
> stuff like that? I'm clueless about parsing but it looks very neat.

I can partially parse D code, because I only wrote part of the D
grammar (see the pegged.examples.dgrammar module), just enough to
parse most of template constraints. I intend to complete it and see if
that floats.

Yes, it extracts syntax trees, at compile-time or runtime.

> Nice work!

Thanks!


More information about the Digitalmars-d-announce mailing list