Search in sources :

Example 16 with Alternative

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

the class SymbolChecks method checkForLabelConflicts.

/** Make sure a label doesn't conflict with another symbol.
     *  Labels must not conflict with: rules, tokens, scope names,
     *  return values, parameters, and rule-scope dynamic attributes
     *  defined in surrounding rule.  Also they must have same type
     *  for repeated defs.
     */
public void checkForLabelConflicts(Collection<Rule> rules) {
    for (Rule r : rules) {
        checkForAttributeConflicts(r);
        Map<String, LabelElementPair> labelNameSpace = new HashMap<>();
        for (int i = 1; i <= r.numberOfAlts; i++) {
            Alternative a = r.alt[i];
            for (List<LabelElementPair> pairs : a.labelDefs.values()) {
                if (r.hasAltSpecificContexts()) {
                    // Collect labelName-labeledRules map for rule with alternative labels.
                    Map<String, List<LabelElementPair>> labelPairs = new HashMap<>();
                    for (LabelElementPair p : pairs) {
                        String labelName = findAltLabelName(p.label);
                        if (labelName != null) {
                            List<LabelElementPair> list;
                            if (labelPairs.containsKey(labelName)) {
                                list = labelPairs.get(labelName);
                            } else {
                                list = new ArrayList<>();
                                labelPairs.put(labelName, list);
                            }
                            list.add(p);
                        }
                    }
                    for (List<LabelElementPair> internalPairs : labelPairs.values()) {
                        labelNameSpace.clear();
                        checkLabelPairs(r, labelNameSpace, internalPairs);
                    }
                } else {
                    checkLabelPairs(r, labelNameSpace, pairs);
                }
            }
        }
    }
}
Also used : Alternative(org.antlr.v4.tool.Alternative) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) LabelElementPair(org.antlr.v4.tool.LabelElementPair)

Example 17 with Alternative

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

the class ParserATNFactory method epsilon.

/** From an empty alternative build {@code o-e->o}. */
@Override
public Handle epsilon(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)

Aggregations

ArrayList (java.util.ArrayList)4 Alternative (org.antlr.v4.tool.Alternative)4 Rule (org.antlr.v4.tool.Rule)4 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)4 BitSet (java.util.BitSet)3 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)3 ATNState (org.antlr.v4.runtime.atn.ATNState)3 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 DFAState (org.antlr.v4.runtime.dfa.DFAState)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 ActionAST (org.antlr.v4.tool.ast.ActionAST)2 Map (java.util.Map)1 CommonToken (org.antlr.runtime.CommonToken)1 Token (org.antlr.runtime.Token)1 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)1