Search in sources :

Example 16 with ATNState

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

the class LexerATNFactory method set.

@Override
public Handle set(GrammarAST associatedAST, List<GrammarAST> alts, boolean invert) {
    ATNState left = newState(associatedAST);
    ATNState right = newState(associatedAST);
    IntervalSet set = new IntervalSet();
    for (GrammarAST t : alts) {
        if (t.getType() == ANTLRParser.RANGE) {
            int a = CharSupport.getCharValueFromGrammarCharLiteral(t.getChild(0).getText());
            int b = CharSupport.getCharValueFromGrammarCharLiteral(t.getChild(1).getText());
            if (checkRange((GrammarAST) t.getChild(0), (GrammarAST) t.getChild(1), a, b)) {
                checkSetCollision(associatedAST, set, a, b);
                set.add(a, b);
            }
        } else if (t.getType() == ANTLRParser.LEXER_CHAR_SET) {
            set.addAll(getSetFromCharSetLiteral(t));
        } else if (t.getType() == ANTLRParser.STRING_LITERAL) {
            int c = CharSupport.getCharValueFromGrammarCharLiteral(t.getText());
            if (c != -1) {
                checkSetCollision(associatedAST, set, c);
                set.add(c);
            } else {
                g.tool.errMgr.grammarError(ErrorType.INVALID_LITERAL_IN_LEXER_SET, g.fileName, t.getToken(), t.getText());
            }
        } else if (t.getType() == ANTLRParser.TOKEN_REF) {
            g.tool.errMgr.grammarError(ErrorType.UNSUPPORTED_REFERENCE_IN_LEXER_SET, g.fileName, t.getToken(), t.getText());
        }
    }
    if (invert) {
        left.addTransition(new NotSetTransition(right, set));
    } else {
        Transition transition;
        if (set.getIntervals().size() == 1) {
            Interval interval = set.getIntervals().get(0);
            transition = CodePointTransitions.createWithCodePointRange(right, interval.a, interval.b);
        } else {
            transition = new SetTransition(right, set);
        }
        left.addTransition(transition);
    }
    associatedAST.atnState = left;
    return new Handle(left, right);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) 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) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) Interval(org.antlr.v4.runtime.misc.Interval)

Example 17 with ATNState

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

the class ParserATNFactory method set.

/**
 * From set build single edge graph {@code o->o-set->o}.  To conform to
 *  what an alt block looks like, must have extra state on left.
 *  This also handles {@code ~A}, converted to {@code ~{A}} set.
 */
@NotNull
@Override
public Handle set(@NotNull GrammarAST associatedAST, @NotNull List<GrammarAST> terminals, boolean invert) {
    ATNState left = newState(associatedAST);
    ATNState right = newState(associatedAST);
    IntervalSet set = new IntervalSet();
    for (GrammarAST t : terminals) {
        int ttype = g.getTokenType(t.getText());
        set.add(ttype);
    }
    if (invert) {
        left.addTransition(new NotSetTransition(right, set));
    } else {
        left.addTransition(new SetTransition(right, set));
    }
    associatedAST.atnState = left;
    return new Handle(left, right);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 18 with ATNState

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

the class ParserATNFactory method newState.

@NotNull
public ATNState newState(@Nullable GrammarAST node) {
    ATNState n = new BasicState();
    n.setRuleIndex(currentRule.index);
    atn.addState(n);
    return n;
}
Also used : BasicState(org.antlr.v4.runtime.atn.BasicState) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 19 with ATNState

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

the class ParserATNFactory method sempred.

/**
 * Build what amounts to an epsilon transition with a semantic
 *  predicate action.  The {@code pred} is a pointer into the AST of
 *  the {@link ANTLRParser#SEMPRED} token.
 */
@NotNull
@Override
public Handle sempred(@NotNull PredAST pred) {
    // System.out.println("sempred: "+ pred);
    ATNState left = newState(pred);
    ATNState right = newState(pred);
    AbstractPredicateTransition p;
    if (pred.getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME) != null) {
        int precedence = Integer.parseInt(pred.getOptionString(LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME));
        p = new PrecedencePredicateTransition(right, precedence);
    } else {
        boolean isCtxDependent = UseDefAnalyzer.actionIsContextDependent(pred);
        p = new PredicateTransition(right, currentRule.index, g.sempreds.get(pred), isCtxDependent);
    }
    left.addTransition(p);
    pred.atnState = left;
    return new Handle(left, right);
}
Also used : PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) PrecedencePredicateTransition(org.antlr.v4.runtime.atn.PrecedencePredicateTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) PredicateTransition(org.antlr.v4.runtime.atn.PredicateTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) ATNState(org.antlr.v4.runtime.atn.ATNState) NotNull(org.antlr.v4.runtime.misc.NotNull)

Example 20 with ATNState

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

the class ParserATNFactory method addRuleFollowLinks.

public void addRuleFollowLinks() {
    for (ATNState p : atn.states) {
        if (p != null && p.getStateType() == ATNState.BASIC && p.getNumberOfTransitions() == 1 && p.transition(0) instanceof RuleTransition) {
            RuleTransition rt = (RuleTransition) p.transition(0);
            addFollowLink(rt.ruleIndex, rt.followState);
        }
    }
}
Also used : RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) ATNState(org.antlr.v4.runtime.atn.ATNState)

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