简体   繁体   中英

Antlr4 Listener subtree check condition

I'm new to Antlr, pardon me for basic question, I'm trying to validate the below statement, like if while_condition contains f_lastmove.. do something The while_condition can have other conditions as well. How can I drill down the while_condition? I'm using Listener Pattern with Golang. ESQL While 语句

I don't know Go, but in Java you could do something like this:

// In this example, the grammar is called `T.g4`
class WhileLastMoveListener extends TBaseListener {

    private boolean insideWhileCondition = false;
    
    @Override 
    public void enterWhile_condition(TParser.While_conditionContext ctx) { 
        this.insideWhileCondition = true;
    }

    @Override 
    public void exitWhile_condition(TParser.While_conditionContext ctx) {
        this.insideWhileCondition = false;
    }

    @Override 
    public void enterF_lastmove(TParser.F_lastmoveContext ctx) {
        if (this.insideWhileCondition) {
            // Found a `f_lastmove` rule inside a while `while_condition`
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM