Home > database >  Possible to get actual token value?
Possible to get actual token value?

Time:10-07

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?

CodePudding user response:

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.

  • Related