The grammar already contains the syntactic structure of the language. And when you create a parser from the grammar, a list of terms is also created. These terms appear in the syntax tree that the parser produces.
I was thinking, what if I could request the possible next terms that the parser is looking for at the position of the cursor (basically, what is shiftable/reducable)? This may give me both Expression and Variable (just a Variable is one of the possible Expression options), but then I would just only do something with Variable, namely return a Completion with the list of all currently defined variables.
Because I am currently very stuck on writing the autocomplete, since just walking the syntax tree which also contains error nodes, I find very difficult. And whenever I try to come up with something, there’s a lot of node.firstChild, node.nextSibling, state.sliceDoc(), error nodes in the way, subsequent non-terminals being a child of the error node (instead of sibling of error node), which all seems not very robust and error-prone.