How to define a global token in Lexer.g4?

Viewed 32

i'm using Antlr4 and trying to add a global token in lexer.g4 so i can use it inside any mode

//Lexer.g4
TEST: 'test'->pushMode(testMode);
mode testMode;
PRE: 'pre';
POST: 'post'->popMode;

//Parser.g4
testStatement: TEST PRE TEST POST;

i can't use TEST inside the testMode because it has been defined outside the mode. any solution to define this token so it can be global and i can use it inside any mode ?!

1 Answers

Use the type command to set the token type:

TEST: 'test'-> pushMode(testMode);

mode testMode;
    TEST_X: 'test'-> type(TEST);
    ....
Related