Autocompletion has a fatal flaw. If there is a syntax error somewhere before the cursor, the machine will fail and never reach it, even if it has more than enough context to guess the next tokens.
But what if we could define recovery rules? A way for it to reset when it fails and keep going.
At first glance, rules might seem like a odd and complex mechanism, specially since they allow recursion. But once looked in detail, it's just like any other type of transition, with a few quirks.
The autocompleter works by traversing the ATN, which is ANTLR4's state machine. To do so, we begin by focusing on the three most common types of transitions: Epsilon, Atomic and Set.
ANTLR4 grammars are commonly used to build parsers and syntax analyzers. However, if you dive deeper you'll find out they also be used to predict what the user is going to type next.