Search in sources :

Example 1 with FrequencySet

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

the class ElementFrequenciesVisitor method combineMax.

/*
	 * Common
	 */
/**
 * Generate a frequency set as the union of two input sets. If an
 * element is contained in both sets, the value for the output will be
 * the maximum of the two input values.
 *
 * @param a The first set.
 * @param b The second set.
 * @return The union of the two sets, with the maximum value chosen
 * whenever both sets contain the same key.
 */
protected static FrequencySet<String> combineMax(FrequencySet<String> a, FrequencySet<String> b) {
    FrequencySet<String> result = combineAndClip(a, b, 1);
    for (Map.Entry<String, MutableInt> entry : a.entrySet()) {
        result.get(entry.getKey()).v = entry.getValue().v;
    }
    for (Map.Entry<String, MutableInt> entry : b.entrySet()) {
        MutableInt slot = result.get(entry.getKey());
        slot.v = Math.max(slot.v, entry.getValue().v);
    }
    return result;
}
Also used : MutableInt(org.antlr.v4.misc.MutableInt) Map(java.util.Map)

Example 2 with FrequencySet

use of org.antlr.v4.misc.FrequencySet in project antlr4 by antlr.

the class RuleFunction method getElementFrequenciesForAlt.

/**
 * Given list of X and r refs in alt, compute how many of each there are
 */
protected Pair<FrequencySet<String>, FrequencySet<String>> getElementFrequenciesForAlt(AltAST ast) {
    try {
        ElementFrequenciesVisitor visitor = new ElementFrequenciesVisitor(new CommonTreeNodeStream(new GrammarASTAdaptor(), ast));
        visitor.outerAlternative();
        if (visitor.frequencies.size() != 1) {
            factory.getGrammar().tool.errMgr.toolError(ErrorType.INTERNAL_ERROR);
            return new Pair<>(new FrequencySet<String>(), new FrequencySet<String>());
        }
        return new Pair<>(visitor.getMinFrequencies(), visitor.frequencies.peek());
    } catch (RecognitionException ex) {
        factory.getGrammar().tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, ex);
        return new Pair<>(new FrequencySet<String>(), new FrequencySet<String>());
    }
}
Also used : GrammarASTAdaptor(org.antlr.v4.parse.GrammarASTAdaptor) RecognitionException(org.antlr.runtime.RecognitionException) FrequencySet(org.antlr.v4.misc.FrequencySet) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream) Pair(org.antlr.v4.runtime.misc.Pair)

Example 3 with FrequencySet

use of org.antlr.v4.misc.FrequencySet in project antlr4 by antlr.

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>();
    List<GrammarAST> allRefs = new ArrayList<GrammarAST>();
    boolean firstAlt = true;
    IntervalSet reftypes = new IntervalSet(RULE_REF, TOKEN_REF, STRING_LITERAL);
    for (AltAST ast : altASTs) {
        List<GrammarAST> refs = getRuleTokens(ast.getNodesWithType(reftypes));
        allRefs.addAll(refs);
        Pair<FrequencySet<String>, FrequencySet<String>> minAndAltFreq = getElementFrequenciesForAlt(ast);
        FrequencySet<String> minFreq = minAndAltFreq.a;
        FrequencySet<String> altFreq = minAndAltFreq.b;
        for (GrammarAST t : refs) {
            String refLabelName = getName(t);
            if (refLabelName != null) {
                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 = getName(t);
        if (refLabelName == null) {
            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)

Example 4 with FrequencySet

use of org.antlr.v4.misc.FrequencySet in project antlr4 by antlr.

the class ElementFrequenciesVisitor method combineMax.

/*
	 * Common
	 */
/**
 * Generate a frequency set as the union of two input sets. If an
 * element is contained in both sets, the value for the output will be
 * the maximum of the two input values.
 *
 * @param a The first set.
 * @param b The second set.
 * @return The union of the two sets, with the maximum value chosen
 * whenever both sets contain the same key.
 */
protected static FrequencySet<String> combineMax(FrequencySet<String> a, FrequencySet<String> b) {
    FrequencySet<String> result = combineAndClip(a, b, 1);
    for (Map.Entry<String, MutableInt> entry : a.entrySet()) {
        result.get(entry.getKey()).v = entry.getValue().v;
    }
    for (Map.Entry<String, MutableInt> entry : b.entrySet()) {
        MutableInt slot = result.get(entry.getKey());
        slot.v = Math.max(slot.v, entry.getValue().v);
    }
    return result;
}
Also used : MutableInt(org.antlr.v4.misc.MutableInt) Map(java.util.Map)

Example 5 with FrequencySet

use of org.antlr.v4.misc.FrequencySet 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

FrequencySet (org.antlr.v4.misc.FrequencySet)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 RecognitionException (org.antlr.runtime.RecognitionException)2 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)2 AltLabelStructDecl (org.antlr.v4.codegen.model.decl.AltLabelStructDecl)2 AttributeDecl (org.antlr.v4.codegen.model.decl.AttributeDecl)2 ContextRuleGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleGetterDecl)2 ContextRuleListGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleListGetterDecl)2 ContextRuleListIndexedGetterDecl (org.antlr.v4.codegen.model.decl.ContextRuleListIndexedGetterDecl)2 ContextTokenGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenGetterDecl)2 ContextTokenListGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenListGetterDecl)2 ContextTokenListIndexedGetterDecl (org.antlr.v4.codegen.model.decl.ContextTokenListIndexedGetterDecl)2 Decl (org.antlr.v4.codegen.model.decl.Decl)2 StructDecl (org.antlr.v4.codegen.model.decl.StructDecl)2 MutableInt (org.antlr.v4.misc.MutableInt)2 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)2 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)2