Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to reuse grammars? #81

Open
taboege opened this issue Oct 10, 2020 · 1 comment
Open

How to reuse grammars? #81

taboege opened this issue Oct 10, 2020 · 1 comment

Comments

@taboege
Copy link

taboege commented Oct 10, 2020

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
Copy link
Collaborator

mohawk2 commented Oct 10, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants