Search in sources :

Example 11 with Tuple2

use of org.antlr.v4.runtime.misc.Tuple2 in project antlr4 by tunnelvisionlabs.

the class SemanticPipeline method assignLexerTokenTypes.

void assignLexerTokenTypes(Grammar g, List<GrammarAST> tokensDefs) {
    // put in root, even if imported
    Grammar G = g.getOutermostGrammar();
    for (GrammarAST def : tokensDefs) {
        // tokens { id (',' id)* } so must check IDs not TOKEN_REF
        if (Grammar.isTokenName(def.getText())) {
            G.defineTokenName(def.getText());
        }
    }
    /* Define token types for nonfragment rules which do not include a 'type(...)'
		 * or 'more' lexer command.
		 */
    for (Rule r : g.rules.values()) {
        if (!r.isFragment() && !hasTypeOrMoreCommand(r)) {
            G.defineTokenName(r.name);
        }
    }
    // FOR ALL X : 'xxx'; RULES, DEFINE 'xxx' AS TYPE X
    List<Tuple2<GrammarAST, GrammarAST>> litAliases = Grammar.getStringLiteralAliasesFromLexerRules(g.ast);
    Set<String> conflictingLiterals = new HashSet<String>();
    if (litAliases != null) {
        for (Tuple2<GrammarAST, GrammarAST> pair : litAliases) {
            GrammarAST nameAST = pair.getItem1();
            GrammarAST litAST = pair.getItem2();
            if (!G.stringLiteralToTypeMap.containsKey(litAST.getText())) {
                G.defineTokenAlias(nameAST.getText(), litAST.getText());
            } else {
                // oops two literal defs in two rules (within or across modes).
                conflictingLiterals.add(litAST.getText());
            }
        }
        for (String lit : conflictingLiterals) {
            // Remove literal if repeated across rules so it's not
            // found by parser grammar.
            Integer value = G.stringLiteralToTypeMap.remove(lit);
            if (value != null && value > 0 && value < G.typeToStringLiteralList.size() && lit.equals(G.typeToStringLiteralList.get(value))) {
                G.typeToStringLiteralList.set(value, null);
            }
        }
    }
}
Also used : Tuple2(org.antlr.v4.runtime.misc.Tuple2) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Rule(org.antlr.v4.tool.Rule) HashSet(java.util.HashSet)

Example 12 with Tuple2

use of org.antlr.v4.runtime.misc.Tuple2 in project antlr4 by tunnelvisionlabs.

the class LeftRecursiveRule method getAltLabels.

/**
 * Get -&gt; labels from those alts we deleted for left-recursive rules.
 */
@Override
public Map<String, List<Tuple2<Integer, AltAST>>> getAltLabels() {
    Map<String, List<Tuple2<Integer, AltAST>>> labels = new HashMap<String, List<Tuple2<Integer, AltAST>>>();
    Map<String, List<Tuple2<Integer, AltAST>>> normalAltLabels = super.getAltLabels();
    if (normalAltLabels != null)
        labels.putAll(normalAltLabels);
    if (recPrimaryAlts != null) {
        for (LeftRecursiveRuleAltInfo altInfo : recPrimaryAlts) {
            if (altInfo.altLabel != null) {
                List<Tuple2<Integer, AltAST>> pairs = labels.get(altInfo.altLabel);
                if (pairs == null) {
                    pairs = new ArrayList<Tuple2<Integer, AltAST>>();
                    labels.put(altInfo.altLabel, pairs);
                }
                pairs.add(Tuple.create(altInfo.altNum, altInfo.originalAltAST));
            }
        }
    }
    if (recOpAlts != null) {
        for (int i = 0; i < recOpAlts.size(); i++) {
            LeftRecursiveRuleAltInfo altInfo = recOpAlts.getElement(i);
            if (altInfo.altLabel != null) {
                List<Tuple2<Integer, AltAST>> pairs = labels.get(altInfo.altLabel);
                if (pairs == null) {
                    pairs = new ArrayList<Tuple2<Integer, AltAST>>();
                    labels.put(altInfo.altLabel, pairs);
                }
                pairs.add(Tuple.create(altInfo.altNum, altInfo.originalAltAST));
            }
        }
    }
    if (labels.isEmpty())
        return null;
    return labels;
}
Also used : LeftRecursiveRuleAltInfo(org.antlr.v4.analysis.LeftRecursiveRuleAltInfo) OrderedHashMap(org.antlr.v4.misc.OrderedHashMap) HashMap(java.util.HashMap) Tuple2(org.antlr.v4.runtime.misc.Tuple2) List(java.util.List) ArrayList(java.util.ArrayList) AltAST(org.antlr.v4.tool.ast.AltAST)

Example 13 with Tuple2

use of org.antlr.v4.runtime.misc.Tuple2 in project antlr4 by tunnelvisionlabs.

the class Rule method getAltLabels.

/**
 * Get {@code #} labels. The keys of the map are the labels applied to outer
 * alternatives of a lexer rule, and the values are collections of pairs
 * (alternative number and {@link AltAST}) identifying the alternatives with
 * this label. Unlabeled alternatives are not included in the result.
 */
public Map<String, List<Tuple2<Integer, AltAST>>> getAltLabels() {
    Map<String, List<Tuple2<Integer, AltAST>>> labels = new LinkedHashMap<String, List<Tuple2<Integer, AltAST>>>();
    for (int i = 1; i <= numberOfAlts; i++) {
        GrammarAST altLabel = alt[i].ast.altLabel;
        if (altLabel != null) {
            List<Tuple2<Integer, AltAST>> list = labels.get(altLabel.getText());
            if (list == null) {
                list = new ArrayList<Tuple2<Integer, AltAST>>();
                labels.put(altLabel.getText(), list);
            }
            list.add(Tuple.create(i, alt[i].ast));
        }
    }
    if (labels.isEmpty())
        return null;
    return labels;
}
Also used : Tuple2(org.antlr.v4.runtime.misc.Tuple2) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ArrayList(java.util.ArrayList) List(java.util.List) AltAST(org.antlr.v4.tool.ast.AltAST) LinkedHashMap(java.util.LinkedHashMap)

Example 14 with Tuple2

use of org.antlr.v4.runtime.misc.Tuple2 in project antlr4 by tunnelvisionlabs.

the class ScopeParser method parse.

public static AttributeDict parse(@Nullable ActionAST action, String s, char separator, Grammar g) {
    AttributeDict dict = new AttributeDict();
    List<Tuple2<String, Integer>> decls = splitDecls(s, separator);
    for (Tuple2<String, Integer> decl : decls) {
        if (decl.getItem1().trim().length() > 0) {
            Attribute a = parseAttributeDef(action, decl, g);
            dict.add(a);
        }
    }
    return dict;
}
Also used : Attribute(org.antlr.v4.tool.Attribute) Tuple2(org.antlr.v4.runtime.misc.Tuple2) AttributeDict(org.antlr.v4.tool.AttributeDict)

Example 15 with Tuple2

use of org.antlr.v4.runtime.misc.Tuple2 in project antlr4 by tunnelvisionlabs.

the class RuleFunction method getDeclsForAllElements.

/**
 * for all alts, find which ref X or r needs List
 *	   Must see across alts.  If any alt needs X or r as list, then
 *	   define as list.
 */
public Set<Decl> getDeclsForAllElements(List<AltAST> altASTs) {
    Set<String> needsList = new HashSet<String>();
    Set<String> nonOptional = new HashSet<String>();
    Set<String> suppress = new HashSet<String>();
    List<GrammarAST> allRefs = new ArrayList<GrammarAST>();
    boolean firstAlt = true;
    for (AltAST ast : altASTs) {
        IntervalSet reftypes = new IntervalSet(RULE_REF, TOKEN_REF);
        List<GrammarAST> refs = ast.getNodesWithType(reftypes);
        allRefs.addAll(refs);
        Tuple2<FrequencySet<String>, FrequencySet<String>> minAndAltFreq = getElementFrequenciesForAlt(ast);
        FrequencySet<String> minFreq = minAndAltFreq.getItem1();
        FrequencySet<String> altFreq = minAndAltFreq.getItem2();
        for (GrammarAST t : refs) {
            String refLabelName = getLabelName(rule.g, t);
            if (altFreq.count(refLabelName) == 0) {
                suppress.add(refLabelName);
            }
            if (altFreq.count(refLabelName) > 1) {
                needsList.add(refLabelName);
            }
            if (firstAlt && minFreq.count(refLabelName) != 0) {
                nonOptional.add(refLabelName);
            }
        }
        for (String ref : nonOptional.toArray(new String[nonOptional.size()])) {
            if (minFreq.count(ref) == 0) {
                nonOptional.remove(ref);
            }
        }
        firstAlt = false;
    }
    Set<Decl> decls = new LinkedHashSet<Decl>();
    for (GrammarAST t : allRefs) {
        String refLabelName = getLabelName(rule.g, t);
        if (suppress.contains(refLabelName)) {
            continue;
        }
        List<Decl> d = getDeclForAltElement(t, refLabelName, needsList.contains(refLabelName), !nonOptional.contains(refLabelName));
        decls.addAll(d);
    }
    return decls;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ArrayList(java.util.ArrayList) ContextRuleListGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl) Decl(org.antlr.v4.codegen.model.decl.Decl) StructDecl(org.antlr.v4.codegen.model.decl.StructDecl) AttributeDecl(org.antlr.v4.codegen.model.decl.AttributeDecl) AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl) ContextTokenListGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl) ContextRuleListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl) ContextRuleGetterDecl(org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl) ContextTokenListIndexedGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl) ContextTokenGetterDecl(org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl) AltAST(org.antlr.v4.tool.ast.AltAST) IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) FrequencySet(org.antlr.v4.misc.FrequencySet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) OrderedHashSet(org.antlr.v4.runtime.misc.OrderedHashSet)

Aggregations

ArrayList (java.util.ArrayList)12 Tuple2 (org.antlr.v4.runtime.misc.Tuple2)7 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)7 RuleDependency (org.antlr.v4.runtime.RuleDependency)5 AltAST (org.antlr.v4.tool.ast.AltAST)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Lexer (org.antlr.v4.runtime.Lexer)4 Parser (org.antlr.v4.runtime.Parser)4 LinkedHashMap (java.util.LinkedHashMap)3 Element (javax.lang.model.element.Element)3 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeElement (javax.lang.model.element.TypeElement)3 VariableElement (javax.lang.model.element.VariableElement)3 RecognitionException (org.antlr.runtime.RecognitionException)3 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 CommonToken (org.antlr.runtime.CommonToken)2