-
Notifications
You must be signed in to change notification settings - Fork 64
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
Implemented mapping syntax in peg grammars #433
Conversation
What's wrong with: {{
// Nothing to see here
}}
start
= "l"i { return "left"; }
/ "r"i { return "right"; } ? |
Generally nothing, but it's so verbose that you tend to use a |
I'm not terribly excited about this one. I don't think it's generally-useful enough to add syntax for. I'm willing to think about it some more, though. |
I'm all in for exploring if/how a syntax for mapping can be so useful that it justifies an addition to the grammar. Or maybe there's a way to implement this as a plugin? Don't have a lot of experience with the whole parsing shebang, so I'm going to trust your judgment on this. Currently, I am working on a Tailwind-like CSS creation mechanism, which requires a lot of expanding letters to keywords and mapping in general. In some of your example grammars, and also the core peggy one, I spotted similar patterns. Mapping characters like So the ideas I am now playing around with are:
Generally, I'm just fiddling and learning about the inner workings of Peggy right now. If something useful comes out of it, even better. Settled on |
/ "n"%"\n" | ||
/ "r"%"\r" | ||
/ "t"%"\t" | ||
/ "v"%"\v" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't find this easier to read or maintain. Are there other examples you think might benefit from this approach? Maybe add a new example that shows the full power?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. In any case it is better to use ->
or =>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, if it was =>
I might be able to get excited about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Particularly if we also added &=>
and !=>
at the same time...
I'm going to close this for now. If we come up with better use cases, I'm open to further discussion. |
This PR enables direct mapping of string literals without using const maps, removing duplication in the grammar files.
Before:
After:
I chose the
:
as separator so you can translate your existing maps with minimal effort, another one that might make sense for reducing ambiguity might be%
or even->
. Awaiting your feedback on possible improvements :)