37560

How To Terminate a Lexer in ANTLR4

Question:

Is there a simple way to terminate a lexer?

There are some tokens that I do not want to handle yet. But I also want the Lexer to sound an alarm if the input does contain those tokens. My simple strategy is throwing a RuntimeException in an action:

CHARIZING: '#@' {throw new RuntimeException("charizing op not supported yet");};

But the action produces compilation error since the generated Lexer has a break command after the action and the Java compiler complains the break is an unreachable statement.

CPPDefineLexer.java:118: error: unreachable statement case 1: throw new RuntimeException("charizing op not supported y et"); break;

Is there a simple strategy to terminate a Lexer?

Answer1:

You can fool the compiler:

CHARIZING : '#@' { if (true) { throw new RuntimeException("charizing op not supported yet"); } } ;

Recommend