VisitSiblingLists.java 文件源码

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

项目:codebuff 作者:
public void enterEveryRule(ParserRuleContext ctx) {
    // Find sibling lists that are children of this parent node
    Set<Class> completed = new HashSet<>(); // only count sibling list for each subtree type once
    for (int i = 0; i<ctx.getChildCount(); i++) {
        ParseTree child = ctx.getChild(i);

        if ( completed.contains(child.getClass()) ) continue; // avoid counting repeatedly
        completed.add(child.getClass());
        if ( child instanceof TerminalNode ) continue; // tokens are separators at most not siblings

        // found subtree child
        List<? extends ParserRuleContext> siblings =
            ctx.getRuleContexts(((ParserRuleContext) child).getClass());
        if ( siblings.size()>1 ) { // we found a list
            // check for separator by looking between first two siblings (assume all are same)
            ParserRuleContext first = siblings.get(0);
            ParserRuleContext second = siblings.get(1);
            List<Tree> children = Trees.getChildren(ctx);

            int firstIndex = children.indexOf(first);
            int secondIndex = children.indexOf(second);

            if ( firstIndex+1 == secondIndex ) continue; // nothing between first and second so no separator

            ParseTree between = ctx.getChild(firstIndex+1);
            if ( between instanceof TerminalNode ) { // is it a token?
                Token separator = ((TerminalNode) between).getSymbol();
                visitNonSingletonWithSeparator(ctx, siblings, separator);
            }
        }
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号