Search code examples
parsingantlr4grammar

Possible to get actual token value?


In Antlr4, let's say I have the following rule:

// grammar
root: TRUE eof;

// lexer
TRUE:       T R U E;
fragment T: [tT];
fragment R: [rR];
fragment U: [uU];
fragment E: [eE];

If the user enters in true the parse tree that it shows looks something like this:

  root
  /  \
TRUE EOF

But this gives me the token TRUE --

enter image description here

Is there a way to get the actual string value entered in, in this case true, respecting casing?


Solution

  • Each token comes with an index into the input stream. Use those to extract the original text. Note that token.end is inclusive , i.e. it points to the last character that belongs to that token.