-
Notifications
You must be signed in to change notification settings - Fork 128
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
Tree transforms #29
Comments
It's coming! It'll probably look something like this: #23 (comment). |
Suppress and Select are two pieces of sugar that would be nice. Perhaps a recursive flattener like in PyPy would help as well. I'll do a survey of Parsimonious grammars in the wild and see what pops out. Spelling strawman:
|
Transforms should also (do I even need to mention it?) be heritable, so we can suppress whitespace once and have it stick elsewhere:
|
However, I probably won't implement flattening until I see some compelling use cases. RPython's page gives a right-recursive grammar as an example, and their example, at least, is much simpler represented using a quantifier. |
In summary, I can think of 4 transforms sufficiently useful to include:
|
Writing and testing grammars in Parsimonious has been gloriously simple, but transforming the parse tree has so far been difficult. For example, I'm trying to write a general JSON-izer for Parsimonious parse trees that just spits out node info for the named rules (not anonymous literal matches, for example), and I'm having trouble doing it by implementing It would be a huge help to have more explicit support for transforms like lifting and ignoring (#29, #36), or some documentation/examples about how to implement them myself (#99 , #48). Are there plans to visit (no pun intended) these issues in the near-future? Alternately, could you link to any snippets of Parsimonious code that do things like the ignoring or lifting? |
Thanks for asking! First, I'm going to be talking about Parsimonious on Podcast.__init__ in the next month or two, so I'm going to try to get some real docs in place before then. Second, I haven't had much time to hack on Parsimonious lately, but tree transforms are the next thing on the roadmap after switching the precedence of Finally, there's lot of ignoring going on in my grammar in DXR: see https://github.com/mozilla/dxr/blob/master/dxr/query.py#L309, which takes a term like "+foo:bar". It looks at Does that help? |
Sweet! I enjoyed listening to you on Talk Python to Me, and I'm looking forward to your next interview. I think I understand the ignoring happening at your link; thank you. What do you do if you look at your node or its children, and decide you wanted to ignore everything? Simply |
You want the node to not exist in the tree? Arrange to lop it off in the visitor for the parent of that node. For instance, when it sees |
(I didn't mean to hijack this issue. It seemed like the right place for this.) I think I could JSON-ize any parse tree, regardless of rule structure, so I'm only using Here's a gist illustrating the visitor using your "(( bold stuff ))" example. I think the
Ah, I should "de- If this visitor encounters an anonymous parent with named children, it returns the children (a list of dictionaries), when I really just intend to lift the children to the level of the parent, which sounds like a simple lifting operation. Still not sure how to implement this one... Thanks for your responsiveness. |
I think you're almost there. It looks like you now have "uninteresting" nodes returning None. Now if you simply filter out the Nones from |
That was it. And, as far as lifting children, that was as simple as flattening a |
Hooray! :-D
|
It would be better if parsimonious had something like Supress in Pyparsing, this way we would be able to check for a keyword for existence, but don't represent it as a node in the tree.
The text was updated successfully, but these errors were encountered: