Unverified Commit 6044391c authored by 张亮's avatar 张亮 Committed by GitHub
Browse files

Merge pull request #2257 from codefairy08/dev_bug_fixs_2211

add method findNodeOnlyFromFirstDescendant
parents 0671030f bdfc0f92
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -93,6 +93,27 @@ public final class ExtractorUtils {
        return Optional.absent();
    }
    
    /**
     * Find node only from first descendant which only has one child.
     *
     * @param node start node
     * @param ruleName rule name
     * @return matched node
     */
    public static Optional<ParserRuleContext> findNodeOnlyFromFirstDescendant(final ParserRuleContext node, final RuleName ruleName) {
        ParserRuleContext nextNode = node;
        do {
            if (isMatchedNode(nextNode, ruleName)) {
                return Optional.of(nextNode);
            }
            if (1 != nextNode.getChildCount() || !(nextNode.getChild(0) instanceof ParserRuleContext)) {
                return Optional.absent();
            }
            nextNode = (ParserRuleContext) nextNode.getChild(0);
        } while (null != nextNode);
        return Optional.absent();
    }
    
    /**
     * Get all descendant nodes.
     *