Search in sources :

Example 16 with SetTransition

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

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)

Aggregations

ATNState (org.antlr.v4.runtime.atn.ATNState)13 SetTransition (org.antlr.v4.runtime.atn.SetTransition)13 NotSetTransition (org.antlr.v4.runtime.atn.NotSetTransition)12 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)12 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)9 Transition (org.antlr.v4.runtime.atn.Transition)9 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)7 Interval (org.antlr.v4.runtime.misc.Interval)6 RangeTransition (org.antlr.v4.runtime.atn.RangeTransition)5 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)5 ArrayList (java.util.ArrayList)4 EpsilonTransition (org.antlr.v4.runtime.atn.EpsilonTransition)4 RuleStopState (org.antlr.v4.runtime.atn.RuleStopState)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 HashMap (java.util.HashMap)3 DecisionState (org.antlr.v4.runtime.atn.DecisionState)3 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)3 IntegerList (org.antlr.v4.runtime.misc.IntegerList)3 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2