Search in sources :

Example 66 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class Trees method getRootOfSubtreeEnclosingRegion.

/**
 * Find smallest subtree of t enclosing range startTokenIndex..stopTokenIndex
 *  inclusively using postorder traversal.  Recursive depth-first-search.
 *
 *  @since 4.5
 */
@Nullable
public static ParserRuleContext getRootOfSubtreeEnclosingRegion(@NotNull ParseTree t, // inclusive
int startTokenIndex, // inclusive
int stopTokenIndex) {
    int n = t.getChildCount();
    for (int i = 0; i < n; i++) {
        ParseTree child = t.getChild(i);
        ParserRuleContext r = getRootOfSubtreeEnclosingRegion(child, startTokenIndex, stopTokenIndex);
        if (r != null)
            return r;
    }
    if (t instanceof ParserRuleContext) {
        ParserRuleContext r = (ParserRuleContext) t;
        if (// is range fully contained in t?
        startTokenIndex >= r.getStart().getTokenIndex() && (r.getStop() == null || stopTokenIndex <= r.getStop().getTokenIndex())) {
            // note: r.getStop()==null likely implies that we bailed out of parser and there's nothing to the right
            return r;
        }
    }
    return null;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) Nullable(org.antlr.v4.runtime.misc.Nullable)

Example 67 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class ParseTreePatternMatcher method matchImpl.

// ---- SUPPORT CODE ----
/**
 * Recursively walk {@code tree} against {@code patternTree}, filling
 * {@code match.}{@link ParseTreeMatch#labels labels}.
 *
 * @return the first node encountered in {@code tree} which does not match
 * a corresponding node in {@code patternTree}, or {@code null} if the match
 * was successful. The specific node returned depends on the matching
 * algorithm used by the implementation, and may be overridden.
 */
@Nullable
protected ParseTree matchImpl(@NotNull ParseTree tree, @NotNull ParseTree patternTree, @NotNull MultiMap<String, ParseTree> labels) {
    if (tree == null) {
        throw new IllegalArgumentException("tree cannot be null");
    }
    if (patternTree == null) {
        throw new IllegalArgumentException("patternTree cannot be null");
    }
    // x and <ID>, x and y, or x and x; or could be mismatched types
    if (tree instanceof TerminalNode && patternTree instanceof TerminalNode) {
        TerminalNode t1 = (TerminalNode) tree;
        TerminalNode t2 = (TerminalNode) patternTree;
        ParseTree mismatchedNode = null;
        // both are tokens and they have same type
        if (t1.getSymbol().getType() == t2.getSymbol().getType()) {
            if (t2.getSymbol() instanceof TokenTagToken) {
                // x and <ID>
                TokenTagToken tokenTagToken = (TokenTagToken) t2.getSymbol();
                // track label->list-of-nodes for both token name and label (if any)
                labels.map(tokenTagToken.getTokenName(), tree);
                if (tokenTagToken.getLabel() != null) {
                    labels.map(tokenTagToken.getLabel(), tree);
                }
            } else if (t1.getText().equals(t2.getText())) {
            // x and x
            } else {
                // x and y
                if (mismatchedNode == null) {
                    mismatchedNode = t1;
                }
            }
        } else {
            if (mismatchedNode == null) {
                mismatchedNode = t1;
            }
        }
        return mismatchedNode;
    }
    if (tree instanceof ParserRuleContext && patternTree instanceof ParserRuleContext) {
        ParserRuleContext r1 = (ParserRuleContext) tree;
        ParserRuleContext r2 = (ParserRuleContext) patternTree;
        ParseTree mismatchedNode = null;
        // (expr ...) and <expr>
        RuleTagToken ruleTagToken = getRuleTagToken(r2);
        if (ruleTagToken != null) {
            ParseTreeMatch m = null;
            if (r1.getRuleContext().getRuleIndex() == r2.getRuleContext().getRuleIndex()) {
                // track label->list-of-nodes for both rule name and label (if any)
                labels.map(ruleTagToken.getRuleName(), tree);
                if (ruleTagToken.getLabel() != null) {
                    labels.map(ruleTagToken.getLabel(), tree);
                }
            } else {
                if (mismatchedNode == null) {
                    mismatchedNode = r1;
                }
            }
            return mismatchedNode;
        }
        // (expr ...) and (expr ...)
        if (r1.getChildCount() != r2.getChildCount()) {
            if (mismatchedNode == null) {
                mismatchedNode = r1;
            }
            return mismatchedNode;
        }
        int n = r1.getChildCount();
        for (int i = 0; i < n; i++) {
            ParseTree childMatch = matchImpl(r1.getChild(i), patternTree.getChild(i), labels);
            if (childMatch != null) {
                return childMatch;
            }
        }
        return mismatchedNode;
    }
    // if nodes aren't both tokens or both rule nodes, can't match
    return tree;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Nullable(org.antlr.v4.runtime.misc.Nullable)

Example 68 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class LL1Analyzer method LOOK.

/**
 * Compute set of tokens that can follow {@code s} in the ATN in the
 * specified {@code ctx}.
 *
 * <p>If {@code ctx} is {@code null} and the end of the rule containing
 * {@code s} is reached, {@link Token#EPSILON} is added to the result set.
 * If {@code ctx} is not {@code PredictionContext#EMPTY_LOCAL} and the end of the outermost rule is
 * reached, {@link Token#EOF} is added to the result set.</p>
 *
 * @param s the ATN state
 * @param stopState the ATN state to stop at. This can be a
 * {@link BlockEndState} to detect epsilon paths through a closure.
 * @param ctx the complete parser context, or {@code null} if the context
 * should be ignored
 *
 * @return The set of tokens that can follow {@code s} in the ATN in the
 * specified {@code ctx}.
 */
@NotNull
public IntervalSet LOOK(@NotNull ATNState s, @Nullable ATNState stopState, @NotNull PredictionContext ctx) {
    IntervalSet r = new IntervalSet();
    // ignore preds; get all lookahead
    final boolean seeThruPreds = true;
    final boolean addEOF = true;
    _LOOK(s, stopState, ctx, r, new HashSet<ATNConfig>(), new BitSet(), seeThruPreds, addEOF);
    return r;
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) BitSet(java.util.BitSet) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 69 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class LexerATNSimulator method addDFAState.

/**
 * Add a new DFA state if there isn't one with this set of
 *		configurations already. This method also detects the first
 *		configuration containing an ATN rule stop state. Later, when
 *		traversing the DFA, we will know which rule to accept.
 */
@NotNull
protected DFAState addDFAState(@NotNull ATNConfigSet configs) {
    /* the lexer evaluates predicates on-the-fly; by this point configs
		 * should not contain any configurations with unevaluated predicates.
		 */
    assert !configs.hasSemanticContext();
    DFAState proposed = new DFAState(atn.modeToDFA[mode], configs);
    DFAState existing = atn.modeToDFA[mode].states.get(proposed);
    if (existing != null)
        return existing;
    configs.optimizeConfigs(this);
    DFAState newState = new DFAState(atn.modeToDFA[mode], configs.clone(true));
    ATNConfig firstConfigWithRuleStopState = null;
    for (ATNConfig c : configs) {
        if (c.getState() instanceof RuleStopState) {
            firstConfigWithRuleStopState = c;
            break;
        }
    }
    if (firstConfigWithRuleStopState != null) {
        int prediction = atn.ruleToTokenType[firstConfigWithRuleStopState.getState().ruleIndex];
        LexerActionExecutor lexerActionExecutor = firstConfigWithRuleStopState.getLexerActionExecutor();
        newState.setAcceptState(new AcceptStateInfo(prediction, lexerActionExecutor));
    }
    return atn.modeToDFA[mode].addState(newState);
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState) AcceptStateInfo(org.antlr.v4.runtime.dfa.AcceptStateInfo) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 70 with NotNull

use of org.antlr.v4.runtime.misc.NotNull in project antlr4 by tunnelvisionlabs.

the class LexerATNSimulator method matchATN.

protected int matchATN(@NotNull CharStream input) {
    ATNState startState = atn.modeToStartState.get(mode);
    if (debug) {
        System.out.format(Locale.getDefault(), "matchATN mode %d start: %s\n", mode, startState);
    }
    int old_mode = mode;
    ATNConfigSet s0_closure = computeStartState(input, startState);
    boolean suppressEdge = s0_closure.hasSemanticContext();
    if (suppressEdge) {
        s0_closure.clearExplicitSemanticContext();
    }
    DFAState next = addDFAState(s0_closure);
    if (!suppressEdge) {
        if (!atn.modeToDFA[mode].s0.compareAndSet(null, next)) {
            next = atn.modeToDFA[mode].s0.get();
        }
    }
    int predict = execATN(input, next);
    if (debug) {
        System.out.format(Locale.getDefault(), "DFA after matchATN: %s\n", atn.modeToDFA[old_mode].toLexerString());
    }
    return predict;
}
Also used : DFAState(org.antlr.v4.runtime.dfa.DFAState)

Aggregations

NotNull (org.antlr.v4.runtime.misc.NotNull)40 ATNState (org.antlr.v4.runtime.atn.ATNState)14 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)14 DFAState (org.antlr.v4.runtime.dfa.DFAState)12 ParseTree (org.antlr.v4.runtime.tree.ParseTree)9 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)8 ArrayList (java.util.ArrayList)7 Token (org.antlr.v4.runtime.Token)7 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)4 DFA (org.antlr.v4.runtime.dfa.DFA)4 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 QuantifierAST (org.antlr.v4.tool.ast.QuantifierAST)4 List (java.util.List)3 ATN (org.antlr.v4.runtime.atn.ATN)3 BlockEndState (org.antlr.v4.runtime.atn.BlockEndState)3 Transition (org.antlr.v4.runtime.atn.Transition)3 WildcardTransition (org.antlr.v4.runtime.atn.WildcardTransition)3 Interval (org.antlr.v4.runtime.misc.Interval)3 Nullable (org.antlr.v4.runtime.misc.Nullable)3