-
Notifications
You must be signed in to change notification settings - Fork 125
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
JSON example. Some questions and thoughts #14
Comments
The best examples are probably the tests, especially in test_grammar.py.
Cool! I may even have to steal this, once you finish it, for benchmark trials. There's a ton of speed work to come.
Here's (off the top of my head, approximately, untestedly, etc., etc.) how I'd structure it:
You might want to break it up even further, depending on your aesthetic and the details of your tree-walker implementation:
I like that better. I moved some of the ws into
My intention is that unused nodes get ignored by the visitor methods, like you do in
…etc.; that is, you can reuse variable names in the argument list if you don't care about capturing.
Once you get your grammar refactored and working, let me know if this pattern still holds. I expect we'll discover lots of patterns to factor up as we write more grammars. I haven't ruled out adding a small number of tree transformations someplace. I especially like the ones PyPy chose: http://doc.pypy.org/en/latest/rlib.html#tree-transformations.
You'll be happy to hear I'm writing benchmarks as we speak. :-) Then I get to do fun speed work, which is what I've been really looking forward to! Thanks so much for trying Parsimonious! |
Feel free to re-open or just keep chatting. |
Thank you for detailed response!
Feel free to use this code, also I can make pull request once it will be fixed. I've modified example according to your suggestions. I like the second variant too.
That's what I meant by "mark some term or rule as excluded from tree". PyPy way looks very nice.
def visit_member(self, node, (name, value)): # no more noise in visitors Also, with this transformations the need of
👍 One more thing (maybe it should be in separate issue). |
I was interested in parsimonius because of readable grammars and separation of concerns, but didn't find any examples (except
rule_syntax
), so I've tried to write a simple JSON parser with demo and benchmark. https://gist.github.com/reclosedev/5222560I'm not sure that I've used correct way to express grammar. For example, comma separated
values
andmembers
. This grammar allows comma after the lastmember/value
(JSON doesn't). How should it be written?Can we mark some term or rule as excluded from tree? Example: whitespace, braces, commas. It would allow to reuse and to simplify some
visit_*
methods.Suggestion:
NodeVisitor.lift_child
could be more useful, if it accepted rules with more than one child, e.g.:Or it can be separate method.
I think it would be great to have more real grammar examples with benchmarks in parsimonius.
The text was updated successfully, but these errors were encountered: