-
Notifications
You must be signed in to change notification settings - Fork 48
Description
Hi.
I have a use case where I am using gocc for a simple query language for my application. However, the end user of the application does not have in depth knowledge on the syntax defined in the bnf file.
I thought it might be useful to try and use the parser in order to help the end user with the query language by capturing the output when the parser errors and just providing the list of expected tokens it generates to my end user.
I created a small example here for demonstration:
autosuggest example
The additional code I added to the parser is just this bit:
SuggestParse
As you can see, I just wrap the scanner and force a INVALID token at the end. This forces the parser to generate an error and supply a list of suggested tokens.
There is a small demo you can build for the example:
go build github.com/goccmack/gocc/example/autosuggest/cmd/demo
./demoYou can submit your text with enter and it will supply a list of suggested token the parser is expecting.
One obvious issue is the string representation of the token might not be the same as the actual characters it will expect. For example, running the demo with:
find author with "test" at timerange
ExpectedTokens:
[0] int_lit
[1] dayMonth
[2] number
[3] spaceIn this case the dayMonth token actually expects '-' _decimal_digit _decimal_digit.
In order to present this to an end user I would have to translate the suggested tokens to their character representation. An additional table that could contain the regex expression for a token might be a bit more human friendly.
dayMonth -> "-[0-9][0-9]"
This can then be presented to an end user.
Would this be a feature you would want to include in gocc?
I have no problem finishing the work. I just want to hear your thoughts before I continue.