GrammarParserInterpreter.java 文件源码

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

项目:codebuff 作者:
/** Return a list of parse trees, one for each alternative in a decision
 *  given the same input.
 *
 *  Very similar to {@link #getAllPossibleParseTrees} except
 *  that it re-parses the input for every alternative in a decision,
 *  not just the ambiguous ones (there is no alts parameter here).
 *  This method also tries to reduce the size of the parse trees
 *  by stripping away children of the tree that are completely out of range
 *  of startIndex..stopIndex. Also, because errors are expected, we
 *  use a specialized error handler that more or less bails out
 *  but that also consumes the first erroneous token at least. This
 *  ensures that an error node will be in the parse tree for display.
 *
 *  NOTES:
   // we must parse the entire input now with decision overrides
// we cannot parse a subset because it could be that a decision
// above our decision of interest needs to read way past
// lookaheadInfo.stopIndex. It seems like there is no escaping
// the use of a full and complete token stream if we are
// resetting to token index 0 and re-parsing from the start symbol.
// It's not easy to restart parsing somewhere in the middle like a
// continuation because our call stack does not match the
// tree stack because of left recursive rule rewriting. grrrr!
 *
 * @since 4.5.1
 */
public static List<ParserRuleContext> getLookaheadParseTrees(Grammar g,
                                                             ParserInterpreter originalParser,
                                                             TokenStream tokens,
                                                             int startRuleIndex,
                                                             int decision,
                                                             int startIndex,
                                                             int stopIndex)
{
    List<ParserRuleContext> trees = new ArrayList<ParserRuleContext>();
    // Create a new parser interpreter to parse the ambiguous subphrase
    ParserInterpreter parser = deriveTempParserInterpreter(g, originalParser, tokens);
    BailButConsumeErrorStrategy errorHandler = new BailButConsumeErrorStrategy();
    parser.setErrorHandler(errorHandler);

    DecisionState decisionState = originalParser.getATN().decisionToState.get(decision);

    for (int alt=1; alt<=decisionState.getTransitions().length; alt++) {
        // re-parse entire input for all ambiguous alternatives
        // (don't have to do first as it's been parsed, but do again for simplicity
        //  using this temp parser.)
        parser.reset();
        parser.addDecisionOverride(decision, startIndex, alt);
        ParserRuleContext tt = parser.parse(startRuleIndex);
        int stopTreeAt = stopIndex;
        if ( errorHandler.firstErrorTokenIndex>=0 ) {
            stopTreeAt = errorHandler.firstErrorTokenIndex; // cut off rest at first error
        }
        ParserRuleContext subtree =
            Trees.getRootOfSubtreeEnclosingRegion(tt,
                                                  startIndex,
                                                  stopTreeAt);
        // Use higher of overridden decision tree or tree enclosing all tokens
        if ( Trees.isAncestorOf(parser.getOverrideDecisionRoot(), subtree) ) {
            subtree = parser.getOverrideDecisionRoot();
        }
        Trees.stripChildrenOutOfRange(subtree, parser.getOverrideDecisionRoot(), startIndex, stopTreeAt);
        trees.add(subtree);
    }

    return trees;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号