Trainer.java 文件源码

java
阅读 27 收藏 0 点赞 0 评论 0

项目:codebuff 作者:
/** Walk upwards from node until we find a child of p at t's char position.
 *  Don't see alignment with self, t, or element *after* us.
 *  return null if there is no such ancestor p.
 */
public static Pair<ParserRuleContext,Integer> earliestAncestorWithChildStartingAtCharPos(ParserRuleContext node, Token t, int charpos) {
    ParserRuleContext p = node;
    while ( p!=null ) {
        // check all children of p to see if one of them starts at charpos
        for (int i = 0; i<p.getChildCount(); i++) {
            ParseTree child = p.getChild(i);
            Token start;
            if ( child instanceof ParserRuleContext ) {
                start = ((ParserRuleContext) child).getStart();
            }
            else { // must be token
                start = ((TerminalNode)child).getSymbol();
            }
            // check that we don't see alignment with self or element *after* us
            if ( start.getTokenIndex()<t.getTokenIndex() && start.getCharPositionInLine()==charpos ) {
                return new Pair<>(p,i);
            }
        }
        p = p.getParent();
    }
    return null;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号