ThirstyMonkey
ThirstyMonkey

Reputation: 344

Convert Token Numbers to Strings in ANTLR4

I'm trying to use ANTLR4 to build a sort of autocomplete system using the getExpectedTokens() function that can be called when the parser experiences an error. getExpectedTokens() returns an IntervalSet, containing all the token numbers of the acceptable tokens at that point in the parse. Is there some mapping from the token numbers back to the actual Tokens themselves? (So, for example, if one of the expected tokens is a keyword that keyword can be displayed to the user in some way).

Upvotes: 2

Views: 843

Answers (2)

Ron7
Ron7

Reputation: 395

Using getSymbolicName() worked for me.

So you could do parser.getVocabulary().getSymbolicName(tokenType) where tokenType is an int.

Upvotes: 0

ThirstyMonkey
ThirstyMonkey

Reputation: 344

These tokens names are accessible through the parser's vocabulary.

parser.getVocabulary().getLiteralName(token_num) will return the string of the literal tokens.

Upvotes: 3

Related Questions