I have some ambiguous input. What I want to do is to skip one of the alternatives if my predicate evaluates to false inside of the rule(I want to check if my chain hasn't a whitespace inside, but don't want to actually produce Whitespace tokens and inject it to every rule).
I know I can catch exceptions with antlr, but it seems only for a global rule scope.
I guess I can try something with java code instead.
For example, I have some java code which antlr4 produces:
switch ( getInterpreter().adaptivePredict(_input,117,_ctx) ) {
...
case 4:
{
_localctx = new ChainExpressionContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(907);
chain();
}
break;
...
case 34:
{
_localctx = new FunctionExpressionContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(953);
functionCallNoParen();
}
break;
}
What I want to do is something like this
boolean flag = true;
int _myalt = getInterpreter().adaptivePredict(_input,117,_ctx);
while (flag) {
flag = false;
switch ( _myalt ) {
...
case 4:
{
_localctx = new ChainExpressionContext(_localctx);
_ctx = _localctx;
_prevctx = _localctx;
setState(907);
try {
chain();
} catch (FailedPredicateException) {
if (**Also adaptivePredict of this rule reported ambiguity**) {
flag = true;
_myalt = 34;
continue;
}
}
break;
...
}
}
Is it possible even(I mean may code like this break the whole antlr parsing somehow)? Or antlr have some better approaches for this like custom error handling?
EDIT
For example I have grammar
chain
: chainBase memberAccess*
;
expression
: ...
| chain
...
| functionCallNoParen
;
I would like to parse ambiguous phrases(For a parser with one channel, where HIDDEN tokens are ignored by default this input looks just identical)
put (123).abc
put(123).abc
differently depending on a whitespace characters inside(first is functionCallNoParen, second is chain), so I can try something like
chain
: chainBase {!isCurrentTokenAWhitespace()}? memberAccess*
;
and here comes the described problem