Antlr4 parser integrated code handling at the prediction time

风格不统一 提交于 2019-12-25 00:37:53

问题


I have some rules like this

@parser::header
{
import com.almondtools.antlr4multichannel.MultiChannelTokenStream;
}

@parser::members
{

private static final int HIDDEN = Token.HIDDEN_CHANNEL;

public void enableWs() {
    ((MultiChannelTokenStream) _input).enable(HIDDEN);
}

public void disableWs() {
    ((MultiChannelTokenStream) _input).disable(HIDDEN);
}
}

expression
    : functionCallNoParen
    | chain
    ;

functionCallNoParen
    : chain {enableWs();} Whitespace {disableWs();} expression
    ;

chain
    : chainBase memberAccess* {disableWs();}
    | constant
    ;

memberAccess
    : '.' identifier functionCallInsideMember?
    ;

chainBase
    : identifier {enableWs();} ('::' identifier)* functionCallInsideMember?
    | '(' expression ')'
    ;

functionCallInsideMember
    : '(' {disableWs();} expression? CloseRoundBracket {enableWs();}
    ;

constant
    : Decimal
    ;

identifier
    : VarName
    ;

// Lexer
Whitespace:         [ \t\r\u000B\f\b]+  -> channel(HIDDEN);
OpenRoundBracket:        '(';
CloseRoundBracket:       ')';
Decimal:            Digit+;
fragment Digit:                     [0-9];
VarName:            [a-zA-Z] [a-zA-Z0-9]*;

Where I use some control over HIDDEN tokens with this TokenStream https://stackoverflow.com/a/29115489/11630974

The problem is my parser can parse abc (123).qwe properly as (functionCallNoParen (chain (chainBase (identifier abc))) (expression (chain (chainBase ( (expression (chain (constant 123))) )) (memberAccess . (identifier qwe))))) if I specify start rule as functionCallNoParen.
But If I use expression, antlr4's adaptivePredict don't execute {enableWs();} code I guess, so it tries to find whitespace inside DEFAULT_CHANNEL and then choosing second alternative just as (expression (chain (chainBase (identifier abc)))).
Is my assumption right? And if yes, do I need to override some core prediction functions or is there some better solutions?

来源:https://stackoverflow.com/questions/57221977/antlr4-parser-integrated-code-handling-at-the-prediction-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!