-
Notifications
You must be signed in to change notification settings - Fork 131
Completion support #101
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
Completion support #101
Conversation
Thanks for working on this @jchapuis. That looks like a great addition to parser-combinators! I'll try and review and comment soon :) |
…was causing missing completions with repeated parsers on nested alternatives with common prefixes, added a test to examplify the case
I asked on Gitter (scala/contributors) if anyone else would like to help review this. |
Hi all, if there is any way I can help in making this move forward, let me know! This extension has proven very valuable for us, and I find that it is an ideal use case for parser combinators. Indeed, when developing interactive and dynamic grammars, flexibility is much more important than performance - and flexibility is where combinators shine. Completions with dynamic grammars and fuzzy string matching allow for slick natural language search experiences for instance (which is what we use it for). |
@Test | ||
def intoParserWithSuccessCompletesResultingParser(): Unit = { | ||
val completions = TestParser.completeString(TestParser.test, animal) | ||
Assert.assertEquals(Seq(bear, lion), completions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is cool! Would it be possible to use completions to generate helpful parse error messages? For example,
Parse error!
animal plane
^
found: plane
expected: bear | lion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@olafurpg thanks! This is a great idea, I will look into this. It should be indeed feasible to "augment" error messages for parsers with CompletionSupport.
For information, I've made this available in a separate library: https://github.com/jchapuis/scala-parser-combinators-completion. It works in the same way, mixing-in the So I'm closing this pull request for now (let's see if that library gains some traction and interest). |
This PR contains a
CompletionSupport
trait which can be mixed-in to enable completion for a grammar, i.e. parsers which are 'augmented' with acompletions
method returning possible entry completions for a certain input. This can be used to elaborate as-you-type completions menus or tab-completion experiences, and is e.g. easy to plug with readline to implement a console application. This is ideal to develop e.g. search query grammars, since the flexibility granted by parser-combinators is very valuable in such a context.All completion-related types, combinator overloads and tests are located in a separate
completions
package and there no impact to existing code. As a result, a small amount of duplication was required for some combinators (but it's very limited).Note that in addition to this base set of definitions, I could also add a fuzzy completion mix-in to this PR, which allows defining as-you-type experiences supporting fuzzy string matching in an easy way.
Also, let me know of any required adaptation to the copyright notice for inclusion consideration. Thanks!