-
Notifications
You must be signed in to change notification settings - Fork 67
Extended PEG Syntax
PhilippeSigaud edited this page Mar 10, 2012
·
48 revisions
As we saw in PEG Basics and Declaring a Grammar, Pegged implements the entire PEG syntax, exactly as it was defined by its author.
Now, I felt the need to extend this a little bit. At that time, semantic actions were not implemented in Pegged so now that they are, these extensions are not strictly necessary, but they are useful shortcuts.
The first extensions act on the result of parsing expression. Given 'e' a parsing expression:
-
:e
will drope
's captures. And, due to the way sequences are implemented in Pegged, the mother expression will forgete
result (that's deliberate). It allows one to write:
mixin(grammar("
JSON <- :'{' (Pair (:',' Pair)*)? :'}'
Pair <- String :':' Value
# Rest of JSON grammar ...
"));
On the first rule, see the colon before the curly braces literals and the comma. That means that when called on `{"Hello":42, "World!":0}`, `JSON` parse tree will contain only the interesting parts, not the syntactic signs necessary to structure the JSON grammar:
ParseTree("JSON",
ParseTree("Pair",
ParseTree("String", ...)
ParseTree("Number", ...)),
ParseTree("Pair",
ParseTree("String", ...)
ParseTree("Number", ...))
)
Fusing Captures:
----------------
Rule-Level Extensions
---------------------