-
Notifications
You must be signed in to change notification settings - Fork 120
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
Feasible to port grammar file from peg.js? Any caveats? #40
Comments
The only problem that I can see is my peg doesn't support The easiest way to implement import would be to add an export feature to On Mon, Jul 20, 2015 at 4:27 PM, Victor Hooi [email protected]
|
Aha, so for the capture groups, I can just use positional capture groups, rather than named capture groups, right? The README doesn't say too much on how to extract the values - but I gather it's shoved in a variable called "buffer", which I can then read? Do you think named capture groups be addd to peg at some point? |
For lack of named capture groups, it's default to process nested grammar structure, e.g. |
Do you mind providing an example of how to emulate named capture groups? Coming from peg.js, this has been a sticking point for me. I really want to use this library. Here's an example:
How would I parse |
You can use AST in which case everything is captured. Example code: https://github.com/pointlander/peg/tree/master/grammars/calculator_ast |
Thanks for your response! I did look at the calculator example and tried to figure out how to apply it. The part that I can't wrap my head around is how to handle capture groups that are not leaf nodes. In the example above, how to pull out |
Split it up into multiple rules? (untested)
|
Ahh that's interesting, thanks for sharing. I think the missing piece for me is an understanding of how to build a tree using a stack, specifically that Looking at https://github.com/pointlander/peg/blob/master/peg.peg it appears that nodes are getting pushed on, but it's not clear to me how to make sense of this. |
peg.peg looks like it makes heavy use of In my example above, (Also, since Does that make sense? HTH. |
So ... I just realized that the file I linked you to is the file you were already looking at. Sorry about that. But maybe with the rest of my comment, you can make sense of it. |
The suggestion to use Also I realized after staring at the syntax more that you can add multiple captures within a single line. For example: https://github.com/pointlander/peg/blob/master/peg.peg#L22-L26 I thought it was only for OR expressions (e.g. Thanks for helping me understand this better! |
Quick followup: This project finally clicked for me 🙂 I'm working on a route parser similar to similar to https://github.com/pillarjs/path-to-regexp in Express.js. The parser's state looks like this:
With the help of go-naturaldate, I realized that you need to treat those fields (segments, keys, etc.) as temporary scratch state. Then you rely on the execution order of the algorithm to fill them in before they're needed. The For anyone else that struggled to make the leap from peg.js like me, here's a full example of the route parser. This example includes a peg.js grammar that I used to first prototype the parser. Thanks for your patience and assistance. This is a very cool project! |
I'm wondering how feasible it would be to port a peg.js grammar file to this (peg)? Example:
https://raw.githubusercontent.com/mongodb-js/log/88daf5f4259652f95f2d4a7a01bd3f14751e4812/mongodb-log.pegjs
I'm new to PEGs, but just skimming through the docs, it seems like it should be easy just to translate each line 1:1 - is there anything we need to be wary of?
Curious if it would ever be possible to have a feature to automatically import a grammar file from peg.js? =) (i.e. if this is something that can easily be automated).
The text was updated successfully, but these errors were encountered: