0

I'd like to customize auto-completion in bash (such as is described at https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Programmable-Completion) such that a parameter can use auto-completion, where the parameter is an expression that should conform to a defined grammar (LALR, PEG or other).

Assuming the grammar definition already exists, is there any way to automate the generation of the resources needed to add this autocomplete capability to bash?

As an example, let's say I have sql.l and sql.y which I can compile with flex and bison to generate a check_sql executable that takes a single SQL "select" query as a parameter, and then prints "Valid" or "Invalid" depending on whether the query is valid SQL syntax. Further assume the command syntax is:

check_sql <sql query>

Then, if I typed in:

check_sql "sel

and then hit Tab, I would like autocomplete to propose to add "ect" to finish the word "select".

Similarly, if I typed in:

check_sql "select my_column from my_table

and then hit Tab, I would like autocomplete to show options such as "where", "inner", or "having".

Finally, I would like to generate the scripts and resources that implement this behavior, using sql.l, sql.y or both as inputs, so that if/as my grammar definition files change, it is easy to also update the bash completion resources.

Is this possible and/or feasible? I have used this approach to implement autocomplete using Monaco (with a PEG grammar rather than an LALR one)-- and it seems in theory that it shouldn't be too hard to do with bash autocomplete, but I'm not finding any relevant documentation or examples.

mwag
  • 3,557
  • 31
  • 38
  • Bash autocomplete gives you the context (what the user typed) and expects a list of possible completions. How you come up with that list is up to you; bash certainly doesn't know anything about parsers. It should be possible to get bison to give you a list of possible tokens using the custom error feature which was recently introduced. That won't help with autocompleting a table or field name, but it should get you keywords. I wouldn't expect flex to be of much help here. – rici Aug 25 '21 at 04:23
  • So it's probably doable but major grammar changes are likely to require some manual adjustments. Perhaps you could flesh out your question by focusing on one aspect of the task. – rici Aug 25 '21 at 04:26

0 Answers0