Search in sources :

Example 51 with NotNull

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

the class LexerATNFactory method createLexerAction.

@Nullable
private LexerAction createLexerAction(@NotNull GrammarAST ID, @Nullable GrammarAST arg) {
    String command = ID.getText();
    checkCommands(command, ID.getToken());
    if ("skip".equals(command) && arg == null) {
        return LexerSkipAction.INSTANCE;
    } else if ("more".equals(command) && arg == null) {
        return LexerMoreAction.INSTANCE;
    } else if ("popMode".equals(command) && arg == null) {
        return LexerPopModeAction.INSTANCE;
    } else if ("mode".equals(command) && arg != null) {
        String modeName = arg.getText();
        Integer mode = getModeConstantValue(modeName, arg.getToken());
        if (mode == null) {
            return null;
        }
        return new LexerModeAction(mode);
    } else if ("pushMode".equals(command) && arg != null) {
        String modeName = arg.getText();
        Integer mode = getModeConstantValue(modeName, arg.getToken());
        if (mode == null) {
            return null;
        }
        return new LexerPushModeAction(mode);
    } else if ("type".equals(command) && arg != null) {
        String typeName = arg.getText();
        Integer type = getTokenConstantValue(typeName, arg.getToken());
        if (type == null) {
            return null;
        }
        return new LexerTypeAction(type);
    } else if ("channel".equals(command) && arg != null) {
        String channelName = arg.getText();
        Integer channel = getChannelConstantValue(channelName, arg.getToken());
        if (channel == null) {
            return null;
        }
        return new LexerChannelAction(channel);
    } else {
        return null;
    }
}
Also used : LexerPushModeAction(org.antlr.v4.runtime.atn.LexerPushModeAction) LexerChannelAction(org.antlr.v4.runtime.atn.LexerChannelAction) LexerTypeAction(org.antlr.v4.runtime.atn.LexerTypeAction) LexerModeAction(org.antlr.v4.runtime.atn.LexerModeAction) Nullable(org.antlr.v4.runtime.misc.Nullable)

Example 52 with NotNull

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

the class ParserATNFactory method plus.

/**
 * From {@code (blk)+} build
 *
 * <pre>
 *   |---------|
 *   v         |
 *  [o-blk-o]-&gt;o-&gt;o
 * </pre>
 *
 * We add a decision for loop back node to the existing one at {@code blk}
 * start.
 */
@NotNull
@Override
public Handle plus(@NotNull GrammarAST plusAST, @NotNull Handle blk) {
    PlusBlockStartState blkStart = (PlusBlockStartState) blk.left;
    BlockEndState blkEnd = (BlockEndState) blk.right;
    preventEpsilonClosureBlocks.add(Tuple.create(currentRule, blkStart, blkEnd));
    PlusLoopbackState loop = newState(PlusLoopbackState.class, plusAST);
    loop.nonGreedy = !((QuantifierAST) plusAST).isGreedy();
    // no way to express SLL restriction
    loop.sll = false;
    atn.defineDecisionState(loop);
    LoopEndState end = newState(LoopEndState.class, plusAST);
    blkStart.loopBackState = loop;
    end.loopBackState = loop;
    plusAST.atnState = loop;
    // blk can see loop back
    epsilon(blkEnd, loop);
    BlockAST blkAST = (BlockAST) plusAST.getChild(0);
    if (((QuantifierAST) plusAST).isGreedy()) {
        if (expectNonGreedy(blkAST)) {
            g.tool.errMgr.grammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, plusAST.getToken(), plusAST.getToken().getText());
        }
        // loop back to start
        epsilon(loop, blkStart);
        // or exit
        epsilon(loop, end);
    } else {
        // if not greedy, priority to exit branch; make it first
        // exit
        epsilon(loop, end);
        // loop back to start
        epsilon(loop, blkStart);
    }
    return new Handle(blkStart, end);
}
Also used : LoopEndState(org.antlr.v4.runtime.atn.LoopEndState) PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) PlusLoopbackState(org.antlr.v4.runtime.atn.PlusLoopbackState) BlockAST(org.antlr.v4.tool.ast.BlockAST) QuantifierAST(org.antlr.v4.tool.ast.QuantifierAST) BlockEndState(org.antlr.v4.runtime.atn.BlockEndState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 53 with NotNull

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

the class ParserATNFactory method wildcard.

/**
 * Build an atom with all possible values in its label.
 */
@NotNull
@Override
public Handle wildcard(@NotNull GrammarAST node) {
    ATNState left = newState(node);
    ATNState right = newState(node);
    left.addTransition(new WildcardTransition(right));
    node.atnState = left;
    return new Handle(left, right);
}
Also used : WildcardTransition(org.antlr.v4.runtime.atn.WildcardTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 54 with NotNull

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

the class ParserATNFactory method epsilon.

/**
 * From an empty alternative build {@code o-e->o}.
 */
@NotNull
@Override
public Handle epsilon(@NotNull GrammarAST node) {
    ATNState left = newState(node);
    ATNState right = newState(node);
    epsilon(left, right);
    node.atnState = left;
    return new Handle(left, right);
}
Also used : ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 55 with NotNull

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

the class ParserATNFactory method epsilon.

protected void epsilon(ATNState a, @NotNull ATNState b, boolean prepend) {
    for (Transition t : a.getTransitions()) {
        if (t.getSerializationType() != Transition.EPSILON) {
            continue;
        }
        if (t.target == b && ((EpsilonTransition) t).outermostPrecedenceReturn() == -1) {
            // This transition was already added
            return;
        }
    }
    if (a != null) {
        int index = prepend ? 0 : a.getNumberOfTransitions();
        a.addTransition(index, new EpsilonTransition(b));
    }
}
Also used : NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) WildcardTransition(org.antlr.v4.runtime.atn.WildcardTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) PredicateTransition(org.antlr.v4.runtime.atn.PredicateTransition) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) Transition(org.antlr.v4.runtime.atn.Transition) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) EpsilonTransition(org.antlr.v4.runtime.atn.EpsilonTransition)

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