ANTLRWorks 2 (AW2) performs a similar operation, which I'll describe here. If you reference the source code for AW2, keep in mind that it is only released under an LGPL license.
Create a special token which represents the location of interest for code completion.
- In some ways, this token behaves like the
EOF
. In particular, theParserATNSimulator
never consumes this token; a decision is always made at or before it is reached. - In other ways, this token is very unique. In particular, if the token is located at an identifier or keyword, it is treated as though the token type was "fuzzy", and allowed to match any identifier or keyword for the language. For ANTLR 4 grammars, if the caret token is located at a location where the user has typed
g
, the parser will allow that token to match a rule name or the keywordgrammar
.
- In some ways, this token behaves like the
Create a specialized ATN interpreter that can return all possible parse trees which lead to the caret token, without looking past the caret for any decision, and without constraining the exact token type of the caret token.
For each possible parse tree, evaluate your code completion in the context of whatever the caret token matched in a parser rule.
The union of all the results found in step 3 is a superset of the complete set of valid code completion results, and can be presented in the IDE.
The following describes AW2's implementation of the above steps.
- In AW2, this is the
CaretToken
, and it always has the token typeCARET_TOKEN_TYPE
. - In AW2, this specialized operation is represented by the
ForestParser<TParser>
interface, with most of the reusable implementation inAbstractForestParser<TParser>
and specialized for parsing ANTLR 4 grammars for code completion inGrammarForestParser
. - In AW2, this analysis is performed primarily by
GrammarCompletionQuery.TaskImpl.runImpl(BaseDocument)
.