Skip to content
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

yacc: Publish the current state to the error callback #65

Closed
bazsi opened this issue May 13, 2015 · 2 comments
Closed

yacc: Publish the current state to the error callback #65

bazsi opened this issue May 13, 2015 · 2 comments

Comments

@bazsi
Copy link

bazsi commented May 13, 2015

I have a use-case where I am using ply generated grammar for a programmable tab-completion infrastructure.

This is the one: https://github.com/balabit/syslog-ng/blob/master/modules/python/pylib/syslogng/debuggercli/completerlang.py

Basically, whenever the user presses tab to request completion, I parse the currently edited string according to a grammar and upon the occurence of the injected TAB token I inspect the current state of the parser.

This way I get all the terminals and rules that apply at the specific point where the user pressed TAB. This information can then be used collect the potential completions, making it pretty easy to implement tab completion for LALR(1) grammars.

Right now, "state" is not published anywhere, it is a local variable within the parse() function, which I look up using sys._getframe(). This is of course fragile and was recently broken due as the part that called the error handler function was extracted into a separate function in ply 3.6.

I see that statestack is published in the parser and its last element should be equal to the current state, however I am not entirely sure.

If I got some help I would be willing to contribute a patch.
Thanks.

@dabeaz
Copy link
Owner

dabeaz commented Aug 30, 2016

Modified the parser to set a "state" attribute before calling any production or error function. You'll need to keep a reference to the parser to get it.

@dabeaz dabeaz closed this as completed Aug 30, 2016
@bazsi
Copy link
Author

bazsi commented May 29, 2018

Thanks a lot, it seems to work for me, so I am removing all the ugly getframe() calls from syslog-ng right now :)

bazsi added a commit to syslog-ng/syslog-ng that referenced this issue May 29, 2018
The yacc implementation we are using (ply), didn't publish its state
variable, requiring us to look that up using sys._getframe() calls, which
was pretty fragile.

@dabeaz was nice enough to add this function to ply in 2016 (version 3.9),
which has now trickled into various distributions, so the hack can be removed.

	dabeaz/ply#65

Signed-off-by: Balazs Scheidler <balazs.scheidler@balabit.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants