Gru
Gru

Reputation: 480

ANTLR4 how to separate Lexer subrule

Let's say I have Lexer rules like this:

EMPTY_LITERAL: '\'' '\'';
LITERAL: '\'' (ESCAPED_SEQ|.)*? '\'' ;
fragment ESCAPED_SEQ: '\\\'' | '\\\\'

and a parser rule like this:

literal:  EMPTY_LITERAL #EmptyLiteral | LITERAL #LiteralWithContent;

I want to get the content of LITERAL without quotes in the parser. I can strip the quotes, of course, but I am interesting in getting that string without quotes.

If I move the inner rule in the LITERAL the rule will not match properly (will match only 1 char). If I move LITERAL as a parser rule, I can match ESCAPED_SEQ but this is not what I want. Is there a way to name the inner rule in the lexer?

Upvotes: 0

Views: 135

Answers (1)

sepp2k
sepp2k

Reputation: 370102

Is there a way to name the inner rule in the lexer?

No, there is not. It's not possible to name or access specific parts of a token in ANTLR 4, nor is there a sensible way to turn LITERAL into a parser rule.

So stripping the quotes from the token's text yourself is your only option.

Upvotes: 2

Related Questions