Search in sources :

Example 21 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method tokenRef.

/**
 * From label {@code A} build graph {@code o-A->o}.
 */
@NotNull
@Override
public Handle tokenRef(@NotNull TerminalAST node) {
    ATNState left = newState(node);
    ATNState right = newState(node);
    int ttype = g.getTokenType(node.getText());
    left.addTransition(new AtomTransition(right, ttype));
    node.atnState = left;
    return new Handle(left, right);
}
Also used : AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 22 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method action.

/**
 * Build what amounts to an epsilon transition with an action.
 *  The action goes into ATN though it is ignored during prediction
 *  if {@link ActionTransition#actionIndex actionIndex}{@code <0}.
 */
@NotNull
@Override
public Handle action(@NotNull ActionAST action) {
    // System.out.println("action: "+action);
    ATNState left = newState(action);
    ATNState right = newState(action);
    ActionTransition a = new ActionTransition(right, currentRule.index);
    left.addTransition(a);
    action.atnState = left;
    return new Handle(left, right);
}
Also used : ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 23 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method _ruleRef.

@NotNull
public Handle _ruleRef(@NotNull GrammarAST node) {
    Rule r = g.getRule(node.getText());
    if (r == null) {
        g.tool.errMgr.grammarError(ErrorType.INTERNAL_ERROR, g.fileName, node.getToken(), "Rule " + node.getText() + " undefined");
        return null;
    }
    RuleStartState start = atn.ruleToStartState[r.index];
    ATNState left = newState(node);
    ATNState right = newState(node);
    int precedence = 0;
    if (((GrammarASTWithOptions) node).getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME) != null) {
        precedence = Integer.parseInt(((GrammarASTWithOptions) node).getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME));
    }
    RuleTransition call = new RuleTransition(start, r.index, precedence, right);
    left.addTransition(call);
    node.atnState = left;
    return new Handle(left, right);
}
Also used : RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) GrammarASTWithOptions(org.antlr.v4.tool.ast.GrammarASTWithOptions) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 24 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method optional.

/**
 * From {@code (A)?} build either:
 *
 * <pre>
 *  o--A-&gt;o
 *  |     ^
 *  o----&gt;|
 * </pre>
 *
 * or, if {@code A} is a block, just add an empty alt to the end of the
 * block
 */
@NotNull
@Override
public Handle optional(@NotNull GrammarAST optAST, @NotNull Handle blk) {
    BlockStartState blkStart = (BlockStartState) blk.left;
    ATNState blkEnd = blk.right;
    preventEpsilonOptionalBlocks.add(Tuple.create(currentRule, blkStart, blkEnd));
    boolean greedy = ((QuantifierAST) optAST).isGreedy();
    // no way to express SLL restriction
    blkStart.sll = false;
    blkStart.nonGreedy = !greedy;
    epsilon(blkStart, blk.right, !greedy);
    optAST.atnState = blk.left;
    return blk;
}
Also used : QuantifierAST(org.antlr.v4.tool.ast.QuantifierAST) PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) StarBlockStartState(org.antlr.v4.runtime.atn.StarBlockStartState) BasicBlockStartState(org.antlr.v4.runtime.atn.BasicBlockStartState) BlockStartState(org.antlr.v4.runtime.atn.BlockStartState) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 25 with ATNState

use of org.antlr.v4.runtime.atn.ATNState in project antlr4 by tunnelvisionlabs.

the class ParserATNFactory method newState.

@NotNull
public <T extends ATNState> T newState(@NotNull Class<T> nodeType, GrammarAST node) {
    Exception cause;
    try {
        Constructor<T> ctor = nodeType.getConstructor();
        T s = ctor.newInstance();
        if (currentRule == null)
            s.setRuleIndex(-1);
        else
            s.setRuleIndex(currentRule.index);
        atn.addState(s);
        return s;
    } catch (InstantiationException ex) {
        cause = ex;
    } catch (IllegalAccessException ex) {
        cause = ex;
    } catch (IllegalArgumentException ex) {
        cause = ex;
    } catch (InvocationTargetException ex) {
        cause = ex;
    } catch (NoSuchMethodException ex) {
        cause = ex;
    } catch (SecurityException ex) {
        cause = ex;
    }
    String message = String.format("Could not create %s of type %s.", ATNState.class.getName(), nodeType.getName());
    throw new UnsupportedOperationException(message, cause);
}
Also used : QuantifierAST(org.antlr.v4.tool.ast.QuantifierAST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ActionAST(org.antlr.v4.tool.ast.ActionAST) TerminalAST(org.antlr.v4.tool.ast.TerminalAST) BlockAST(org.antlr.v4.tool.ast.BlockAST) AltAST(org.antlr.v4.tool.ast.AltAST) PredAST(org.antlr.v4.tool.ast.PredAST) RecognitionException(org.antlr.runtime.RecognitionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Aggregations

ATNState (org.antlr.v4.runtime.atn.ATNState)94 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)34 ATN (org.antlr.v4.runtime.atn.ATN)25 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)23 Rule (org.antlr.v4.tool.Rule)22 Transition (org.antlr.v4.runtime.atn.Transition)21 NotNull (org.antlr.v4.runtime.misc.NotNull)19 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)18 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)17 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)16 SetTransition (org.antlr.v4.runtime.atn.SetTransition)16 NotSetTransition (org.antlr.v4.runtime.atn.NotSetTransition)15 DecisionState (org.antlr.v4.runtime.atn.DecisionState)11 DOTGenerator (org.antlr.v4.tool.DOTGenerator)10 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)10 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)9 ArrayList (java.util.ArrayList)8 EpsilonTransition (org.antlr.v4.runtime.atn.EpsilonTransition)8 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)8