pegex-parser / pegex-pm

Pegex Parser for Perl
62 stars 22 forks source link

How to reuse grammars? #81

Open taboege opened 3 years ago

taboege commented 3 years ago

The current pegex parser accepts meta directives %include and %extends but these do not seem to do anything yet -- at least I couldn't find it. Is this a planned but not yet implemented feature? If not, what is the meaning of %include / %extends's arguments? How would one share rules from one pegex grammar with another one?

As I understand, a grammar may be given as a .pgx file or a Pegex::Grammar-type package, or, if you only care about the rules, the return value from make_tree would suffice. I could imagine that extensibility is either specified in the pegex language or completely delegated to the implementation (where you can juggle with the grammar package or the tree).

One possible way is to provide a method which "merges" two pegex grammar trees but, in any case, the sharing mechanism should be compatible with how receiver classes are merged, too, which maybe points at delegating all of this to the implementation and not specify a resolution process in the language-agnostic pegex? What are your thoughts on this?

In Perl, I think receiver classes have a natural way of being extended via inheritance, but that is not the only way to share rules. As a final impulse, I can't help but mention Raku. At least there, grammars are very class-like and sharing is accomplished either by inheritance or by mixing roles into grammars and doing the parallel thing with the receivers (action classes). All that is a consequence of its OO system and then simply method dispatch, so the rule and receive sharing is consistent when conflicts happen.

mohawk2 commented 3 years ago

I think that an "include" functionality would operate exactly the same as roles, but without the baggage of an OO approach. I believe that include and extends are indeed planned but not yet implemented.

As discussed on IRC, I think that more introspectability of grammars is an important feature. This is the intent behind https://github.com/pegex-parser/pegex-pm/tree/literals, to make literals easy to pull out of the grammar to make producing "canonical" versions of constructs much easier to reconstruct.