dlang-community / Pegged

A Parsing Expression Grammar (PEG) module, using the D programming language.
534 stars 66 forks source link

How to fold a rule? #136

Open timotheecour opened 10 years ago

timotheecour commented 10 years ago

Here's an example of what I want to have: A -> fold(B) C B -> B1 / B2

In the parse tree, the operation fold(B) replaces B by it's children so that B doesn't appear (only B1 or B2) This makes for shorted parse trees. eg:

A(B(B1),C) would become after applying the 'fold': A(B1,C)

of course it wouldn't be called fold() but maybe %B or some other yet unused symbol

PhilippeSigaud commented 10 years ago

There is indeed no predefined operator for that.

Maybe with a semantic action? I'm not sure if that can be done, since an operator or a semantic action must return a parse tree. And an array of children is not a parse tree. Inside the semantic action, you do not have access to the parent tree...

A possible, more intrusive change would be to modify the sequence code to recognize a folded node and 'pull it upward', keeping only its children. Hmm...