Console TAB-Autocomplete Support? (get list of legal tokens following a partial input string) #1277
-
Hi all, Has anyone implemented TAB-completion in Lark-based REPLs? I'm building a Lark-based program with a REPL console interface. With this, I'm really appreciating Lark's very straightforward usage pattern and flexibility. In the program usage:
My front end supports command history, but I'd really like to be able to support tab-autocomplete as well. What this would mean is a user being able to enter part of a command, then hit TAB, then see a list of possible tokens which would be legal from that point. For instance, if one of the CLI's commands was
To accomplish that with a Lark-based parser, is there any way of feeding in a partial string of text, and getting a list of tokens which would be legal from that point? Cheers |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi @davidmcnabnz , The way I would implement this is by trying to parse the incomplete string, as usual. It will of course throw an exception, for example UnexpectedToken, which you can then catch, and then access its Documentation for parsing exceptions: https://lark-parser.readthedocs.io/en/latest/classes.html#unexpectedinput P.S. You could also use UnexpectedInput's |
Beta Was this translation helpful? Give feedback.
-
@MegaIng huge appreciation for your demo. It totally satisfies the need, to the extent I've dropped you a PR with an interactive demo script. Compliments too on your design of both the Together with interrogating the Thanks again |
Beta Was this translation helpful? Give feedback.
I created a small example: https://github.com/MegaIng/lark-autocomplete . This works for your example grammar and the python grammar that lark comes with, so it should be quite powerful. I want to extend that to an at least somewhat complete library.