-
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
How do I attach callbacks to terminal nodes? #27
Comments
Parsimonious divides the problem into 2 phases: parsing and tree walking. They're decoupled for various reason (https://github.com/erikrose/parsimonious#why-no-streaming-tree-processing). Parsimonious does the parsing for you, emitting a syntax tree—a tree of Let's build on the example grammar I suggested in #26. I imagine there'd be more than one type of instruction: maybe a
You make that rule the top one in your grammar so it's the default entrypoint. Then you do something like this:
That would give you a syntax tree representing the user's input. If you were a masochist, you could use it directly, if-then-ing your way down its branches to figure out what in-game thing to do. To make it easier on yourself, you can first transform the tree a bit to make it easier to work with. First, tweak my #26 grammar a bit by breaking out the "additional directions" part:
Then you can write a fairly straightforward I plan to add some common tree transformations to the grammar proper so you don't have to write visitors at all in common cases: https://github.com/erikrose/parsimonious#niceties. #23 has a good discussion of these and also some ideas for weaving grammar and tree-walker together without losing decoupling. Does this help? |
Say, while we're on the subject, have you seen https://pypi.python.org/pypi/blessings/ ? It might make your output easier, if you're targeting the terminal. |
Continuing from the previous example - I'd like to be able to define some kind of callback which will be called any time the parser encounters a terminal.
So continuing on from my previous example, supposing the parser had successfully parsed the expression:
"go north, south"
And supposing that matches a particular rule for 'movement instruction' - I'd like a particular callback to be invoked any time we have one of those.
Can it be done?
The text was updated successfully, but these errors were encountered: