Search in sources :

Example 41 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class AttributeChecks method checkAllAttributeExpressions.

public static void checkAllAttributeExpressions(Grammar g) {
    for (ActionAST act : g.namedActions.values()) {
        AttributeChecks checker = new AttributeChecks(g, null, null, act, act.token);
        checker.examineAction();
    }
    for (Rule r : g.rules.values()) {
        for (ActionAST a : r.namedActions.values()) {
            AttributeChecks checker = new AttributeChecks(g, r, null, a, a.token);
            checker.examineAction();
        }
        for (int i = 1; i <= r.numberOfAlts; i++) {
            Alternative alt = r.alt[i];
            for (ActionAST a : alt.actions) {
                AttributeChecks checker = new AttributeChecks(g, r, alt, a, a.token);
                checker.examineAction();
            }
        }
        for (GrammarAST e : r.exceptions) {
            ActionAST a = (ActionAST) e.getChild(1);
            AttributeChecks checker = new AttributeChecks(g, r, null, a, a.token);
            checker.examineAction();
        }
        if (r.finallyAction != null) {
            AttributeChecks checker = new AttributeChecks(g, r, null, r.finallyAction, r.finallyAction.token);
            checker.examineAction();
        }
    }
}
Also used : Alternative(org.antlr.v4.tool.Alternative) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Rule(org.antlr.v4.tool.Rule) ActionAST(org.antlr.v4.tool.ast.ActionAST)

Example 42 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class BasicSemanticChecks method checkElementIsOuterMostInSingleAlt.

/**
	 Make sure that action is last element in outer alt; here action,
	 a2, z, and zz are bad, but a3 is ok:
	 (RULE A (BLOCK (ALT {action} 'a')))
	 (RULE B (BLOCK (ALT (BLOCK (ALT {a2} 'x') (ALT 'y')) {a3})))
	 (RULE C (BLOCK (ALT 'd' {z}) (ALT 'e' {zz})))
	 */
protected void checkElementIsOuterMostInSingleAlt(GrammarAST tree) {
    CommonTree alt = tree.parent;
    CommonTree blk = alt.parent;
    boolean outerMostAlt = blk.parent.getType() == RULE;
    Tree rule = tree.getAncestor(RULE);
    String fileName = tree.getToken().getInputStream().getSourceName();
    if (!outerMostAlt || blk.getChildCount() > 1) {
        ErrorType e = ErrorType.LEXER_COMMAND_PLACEMENT_ISSUE;
        g.tool.errMgr.grammarError(e, fileName, tree.getToken(), rule.getChild(0).getText());
    }
}
Also used : ErrorType(org.antlr.v4.tool.ErrorType) CommonTree(org.antlr.runtime.tree.CommonTree) CommonTree(org.antlr.runtime.tree.CommonTree) Tree(org.antlr.runtime.tree.Tree)

Example 43 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class BasicSemanticChecks method finishRule.

@Override
public void finishRule(RuleAST rule, GrammarAST ID, GrammarAST block) {
    if (rule.isLexerRule())
        return;
    BlockAST blk = (BlockAST) rule.getFirstChildWithType(BLOCK);
    int nalts = blk.getChildCount();
    GrammarAST idAST = (GrammarAST) rule.getChild(0);
    for (int i = 0; i < nalts; i++) {
        AltAST altAST = (AltAST) blk.getChild(i);
        if (altAST.altLabel != null) {
            String altLabel = altAST.altLabel.getText();
            // first check that label doesn't conflict with a rule
            // label X or x can't be rule x.
            Rule r = ruleCollector.rules.get(Utils.decapitalize(altLabel));
            if (r != null) {
                g.tool.errMgr.grammarError(ErrorType.ALT_LABEL_CONFLICTS_WITH_RULE, g.fileName, altAST.altLabel.token, altLabel, r.name);
            }
            // Now verify that label X or x doesn't conflict with label
            // in another rule. altLabelToRuleName has both X and x mapped.
            String prevRuleForLabel = ruleCollector.altLabelToRuleName.get(altLabel);
            if (prevRuleForLabel != null && !prevRuleForLabel.equals(rule.getRuleName())) {
                g.tool.errMgr.grammarError(ErrorType.ALT_LABEL_REDEF, g.fileName, altAST.altLabel.token, altLabel, rule.getRuleName(), prevRuleForLabel);
            }
        }
    }
    List<GrammarAST> altLabels = ruleCollector.ruleToAltLabels.get(rule.getRuleName());
    int numAltLabels = 0;
    if (altLabels != null)
        numAltLabels = altLabels.size();
    if (numAltLabels > 0 && nalts != numAltLabels) {
        g.tool.errMgr.grammarError(ErrorType.RULE_WITH_TOO_FEW_ALT_LABELS, g.fileName, idAST.token, rule.getRuleName());
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) BlockAST(org.antlr.v4.tool.ast.BlockAST) AltAST(org.antlr.v4.tool.ast.AltAST) Rule(org.antlr.v4.tool.Rule)

Example 44 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class Target method getImplicitTokenLabel.

// should be same for all refs to same token like ctx.ID within single rule function
// for literals like 'while', we gen _s<ttype>
public String getImplicitTokenLabel(String tokenName) {
    ST st = getTemplates().getInstanceOf("ImplicitTokenLabel");
    int ttype = getCodeGenerator().g.getTokenType(tokenName);
    if (tokenName.startsWith("'")) {
        return "s" + ttype;
    }
    String text = getTokenTypeAsTargetLabel(getCodeGenerator().g, ttype);
    st.add("tokenName", text);
    return st.render();
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ST(org.stringtemplate.v4.ST)

Example 45 with Rule

use of org.antlr.v4.tool.Rule in project antlr4 by antlr.

the class ATNSerializer method serialize.

/** Serialize state descriptors, edge descriptors, and decision&rarr;state map
	 *  into list of ints:
	 *
	 * 		grammar-type, (ANTLRParser.LEXER, ...)
	 *  	max token type,
	 *  	num states,
	 *  	state-0-type ruleIndex, state-1-type ruleIndex, ... state-i-type ruleIndex optional-arg ...
	 *  	num rules,
	 *  	rule-1-start-state rule-1-args, rule-2-start-state  rule-2-args, ...
	 *  	(args are token type,actionIndex in lexer else 0,0)
	 *      num modes,
	 *      mode-0-start-state, mode-1-start-state, ... (parser has 0 modes)
	 *      num unicode-bmp-sets
	 *      bmp-set-0-interval-count intervals, bmp-set-1-interval-count intervals, ...
	 *      num unicode-smp-sets
	 *      smp-set-0-interval-count intervals, smp-set-1-interval-count intervals, ...
	 *	num total edges,
	 *      src, trg, edge-type, edge arg1, optional edge arg2 (present always), ...
	 *      num decisions,
	 *      decision-0-start-state, decision-1-start-state, ...
	 *
	 *  Convenient to pack into unsigned shorts to make as Java string.
	 */
public IntegerList serialize() {
    IntegerList data = new IntegerList();
    data.add(ATNDeserializer.SERIALIZED_VERSION);
    serializeUUID(data, ATNDeserializer.SERIALIZED_UUID);
    // convert grammar type to ATN const to avoid dependence on ANTLRParser
    data.add(atn.grammarType.ordinal());
    data.add(atn.maxTokenType);
    int nedges = 0;
    // Note that we use a LinkedHashMap as a set to
    // maintain insertion order while deduplicating
    // entries with the same key.
    Map<IntervalSet, Boolean> sets = new LinkedHashMap<>();
    // dump states, count edges and collect sets while doing so
    IntegerList nonGreedyStates = new IntegerList();
    IntegerList precedenceStates = new IntegerList();
    data.add(atn.states.size());
    for (ATNState s : atn.states) {
        if (s == null) {
            // might be optimized away
            data.add(ATNState.INVALID_TYPE);
            continue;
        }
        int stateType = s.getStateType();
        if (s instanceof DecisionState && ((DecisionState) s).nonGreedy) {
            nonGreedyStates.add(s.stateNumber);
        }
        if (s instanceof RuleStartState && ((RuleStartState) s).isLeftRecursiveRule) {
            precedenceStates.add(s.stateNumber);
        }
        data.add(stateType);
        if (s.ruleIndex == -1) {
            data.add(Character.MAX_VALUE);
        } else {
            data.add(s.ruleIndex);
        }
        if (s.getStateType() == ATNState.LOOP_END) {
            data.add(((LoopEndState) s).loopBackState.stateNumber);
        } else if (s instanceof BlockStartState) {
            data.add(((BlockStartState) s).endState.stateNumber);
        }
        if (s.getStateType() != ATNState.RULE_STOP) {
            // the deserializer can trivially derive these edges, so there's no need to serialize them
            nedges += s.getNumberOfTransitions();
        }
        for (int i = 0; i < s.getNumberOfTransitions(); i++) {
            Transition t = s.transition(i);
            int edgeType = Transition.serializationTypes.get(t.getClass());
            if (edgeType == Transition.SET || edgeType == Transition.NOT_SET) {
                SetTransition st = (SetTransition) t;
                sets.put(st.set, true);
            }
        }
    }
    // non-greedy states
    data.add(nonGreedyStates.size());
    for (int i = 0; i < nonGreedyStates.size(); i++) {
        data.add(nonGreedyStates.get(i));
    }
    // precedence states
    data.add(precedenceStates.size());
    for (int i = 0; i < precedenceStates.size(); i++) {
        data.add(precedenceStates.get(i));
    }
    int nrules = atn.ruleToStartState.length;
    data.add(nrules);
    for (int r = 0; r < nrules; r++) {
        ATNState ruleStartState = atn.ruleToStartState[r];
        data.add(ruleStartState.stateNumber);
        if (atn.grammarType == ATNType.LEXER) {
            if (atn.ruleToTokenType[r] == Token.EOF) {
                data.add(Character.MAX_VALUE);
            } else {
                data.add(atn.ruleToTokenType[r]);
            }
        }
    }
    int nmodes = atn.modeToStartState.size();
    data.add(nmodes);
    if (nmodes > 0) {
        for (ATNState modeStartState : atn.modeToStartState) {
            data.add(modeStartState.stateNumber);
        }
    }
    List<IntervalSet> bmpSets = new ArrayList<>();
    List<IntervalSet> smpSets = new ArrayList<>();
    for (IntervalSet set : sets.keySet()) {
        if (set.getMaxElement() <= Character.MAX_VALUE) {
            bmpSets.add(set);
        } else {
            smpSets.add(set);
        }
    }
    serializeSets(data, bmpSets, new CodePointSerializer() {

        @Override
        public void serializeCodePoint(IntegerList data, int cp) {
            data.add(cp);
        }
    });
    serializeSets(data, smpSets, new CodePointSerializer() {

        @Override
        public void serializeCodePoint(IntegerList data, int cp) {
            serializeInt(data, cp);
        }
    });
    Map<IntervalSet, Integer> setIndices = new HashMap<>();
    int setIndex = 0;
    for (IntervalSet bmpSet : bmpSets) {
        setIndices.put(bmpSet, setIndex++);
    }
    for (IntervalSet smpSet : smpSets) {
        setIndices.put(smpSet, setIndex++);
    }
    data.add(nedges);
    for (ATNState s : atn.states) {
        if (s == null) {
            // might be optimized away
            continue;
        }
        if (s.getStateType() == ATNState.RULE_STOP) {
            continue;
        }
        for (int i = 0; i < s.getNumberOfTransitions(); i++) {
            Transition t = s.transition(i);
            if (atn.states.get(t.target.stateNumber) == null) {
                throw new IllegalStateException("Cannot serialize a transition to a removed state.");
            }
            int src = s.stateNumber;
            int trg = t.target.stateNumber;
            int edgeType = Transition.serializationTypes.get(t.getClass());
            int arg1 = 0;
            int arg2 = 0;
            int arg3 = 0;
            switch(edgeType) {
                case Transition.RULE:
                    trg = ((RuleTransition) t).followState.stateNumber;
                    arg1 = ((RuleTransition) t).target.stateNumber;
                    arg2 = ((RuleTransition) t).ruleIndex;
                    arg3 = ((RuleTransition) t).precedence;
                    break;
                case Transition.PRECEDENCE:
                    PrecedencePredicateTransition ppt = (PrecedencePredicateTransition) t;
                    arg1 = ppt.precedence;
                    break;
                case Transition.PREDICATE:
                    PredicateTransition pt = (PredicateTransition) t;
                    arg1 = pt.ruleIndex;
                    arg2 = pt.predIndex;
                    arg3 = pt.isCtxDependent ? 1 : 0;
                    break;
                case Transition.RANGE:
                    arg1 = ((RangeTransition) t).from;
                    arg2 = ((RangeTransition) t).to;
                    if (arg1 == Token.EOF) {
                        arg1 = 0;
                        arg3 = 1;
                    }
                    break;
                case Transition.ATOM:
                    arg1 = ((AtomTransition) t).label;
                    if (arg1 == Token.EOF) {
                        arg1 = 0;
                        arg3 = 1;
                    }
                    break;
                case Transition.ACTION:
                    ActionTransition at = (ActionTransition) t;
                    arg1 = at.ruleIndex;
                    arg2 = at.actionIndex;
                    if (arg2 == -1) {
                        arg2 = 0xFFFF;
                    }
                    arg3 = at.isCtxDependent ? 1 : 0;
                    break;
                case Transition.SET:
                    arg1 = setIndices.get(((SetTransition) t).set);
                    break;
                case Transition.NOT_SET:
                    arg1 = setIndices.get(((SetTransition) t).set);
                    break;
                case Transition.WILDCARD:
                    break;
            }
            data.add(src);
            data.add(trg);
            data.add(edgeType);
            data.add(arg1);
            data.add(arg2);
            data.add(arg3);
        }
    }
    int ndecisions = atn.decisionToState.size();
    data.add(ndecisions);
    for (DecisionState decStartState : atn.decisionToState) {
        data.add(decStartState.stateNumber);
    }
    //
    if (atn.grammarType == ATNType.LEXER) {
        data.add(atn.lexerActions.length);
        for (LexerAction action : atn.lexerActions) {
            data.add(action.getActionType().ordinal());
            switch(action.getActionType()) {
                case CHANNEL:
                    int channel = ((LexerChannelAction) action).getChannel();
                    data.add(channel != -1 ? channel : 0xFFFF);
                    data.add(0);
                    break;
                case CUSTOM:
                    int ruleIndex = ((LexerCustomAction) action).getRuleIndex();
                    int actionIndex = ((LexerCustomAction) action).getActionIndex();
                    data.add(ruleIndex != -1 ? ruleIndex : 0xFFFF);
                    data.add(actionIndex != -1 ? actionIndex : 0xFFFF);
                    break;
                case MODE:
                    int mode = ((LexerModeAction) action).getMode();
                    data.add(mode != -1 ? mode : 0xFFFF);
                    data.add(0);
                    break;
                case MORE:
                    data.add(0);
                    data.add(0);
                    break;
                case POP_MODE:
                    data.add(0);
                    data.add(0);
                    break;
                case PUSH_MODE:
                    mode = ((LexerPushModeAction) action).getMode();
                    data.add(mode != -1 ? mode : 0xFFFF);
                    data.add(0);
                    break;
                case SKIP:
                    data.add(0);
                    data.add(0);
                    break;
                case TYPE:
                    int type = ((LexerTypeAction) action).getType();
                    data.add(type != -1 ? type : 0xFFFF);
                    data.add(0);
                    break;
                default:
                    String message = String.format(Locale.getDefault(), "The specified lexer action type %s is not valid.", action.getActionType());
                    throw new IllegalArgumentException(message);
            }
        }
    }
    // don't adjust the first value since that's the version number
    for (int i = 1; i < data.size(); i++) {
        if (data.get(i) < Character.MIN_VALUE || data.get(i) > Character.MAX_VALUE) {
            throw new UnsupportedOperationException("Serialized ATN data element " + data.get(i) + " element " + i + " out of range " + (int) Character.MIN_VALUE + ".." + (int) Character.MAX_VALUE);
        }
        int value = (data.get(i) + 2) & 0xFFFF;
        data.set(i, value);
    }
    return data;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) IntegerList(org.antlr.v4.runtime.misc.IntegerList) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet)

Aggregations

LexerGrammar (org.antlr.v4.tool.LexerGrammar)50 ATN (org.antlr.v4.runtime.atn.ATN)47 Rule (org.antlr.v4.tool.Rule)47 Test (org.junit.Test)46 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)31 Grammar (org.antlr.v4.tool.Grammar)30 ATNState (org.antlr.v4.runtime.atn.ATNState)26 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)24 ArrayList (java.util.ArrayList)20 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)17 DOTGenerator (org.antlr.v4.tool.DOTGenerator)9 HashMap (java.util.HashMap)8 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)8 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)8 ActionAST (org.antlr.v4.tool.ast.ActionAST)8 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)7 DecisionState (org.antlr.v4.runtime.atn.DecisionState)7 RuleAST (org.antlr.v4.tool.ast.RuleAST)7 LinkedHashMap (java.util.LinkedHashMap)6