Search in sources :

Example 6 with Pair

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

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<Pair<Integer, AltAST>>> getAltLabels() {
    Map<String, List<Pair<Integer, AltAST>>> labels = new LinkedHashMap<String, List<Pair<Integer, AltAST>>>();
    for (int i = 1; i <= numberOfAlts; i++) {
        GrammarAST altLabel = alt[i].ast.altLabel;
        if (altLabel != null) {
            List<Pair<Integer, AltAST>> list = labels.get(altLabel.getText());
            if (list == null) {
                list = new ArrayList<Pair<Integer, AltAST>>();
                labels.put(altLabel.getText(), list);
            }
            list.add(new Pair<Integer, AltAST>(i, alt[i].ast));
        }
    }
    if (labels.isEmpty())
        return null;
    return labels;
}
Also used : 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) Pair(org.antlr.v4.runtime.misc.Pair)

Example 7 with Pair

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

the class BaseJavaTest method execParser.

public ParseTree execParser(String startRuleName, String input, String parserName, String lexerName) throws Exception {
    Pair<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
    Parser parser = pl.a;
    return execStartRule(startRuleName, parser);
}
Also used : Lexer(org.antlr.v4.runtime.Lexer) Parser(org.antlr.v4.runtime.Parser)

Example 8 with Pair

use of org.antlr.v4.runtime.misc.Pair 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;
    for (AltAST ast : altASTs) {
        IntervalSet reftypes = new IntervalSet(RULE_REF, TOKEN_REF);
        List<GrammarAST> refs = 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 = t.getText();
            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 = t.getText();
        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 9 with Pair

use of org.antlr.v4.runtime.misc.Pair 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 10 with Pair

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

the class RuleFunction method addContextGetters.

public void addContextGetters(OutputModelFactory factory, Rule r) {
    // Add ctx labels for elements in alts with no -> label
    List<AltAST> altsNoLabels = r.getUnlabeledAltASTs();
    if (altsNoLabels != null) {
        Set<Decl> decls = getDeclsForAllElements(altsNoLabels);
        // we know to put in rule ctx, so do it directly
        for (Decl d : decls) ruleCtx.addDecl(d);
    }
    // make structs for -> labeled alts, define ctx labels for elements
    altLabelCtxs = new HashMap<String, AltLabelStructDecl>();
    Map<String, List<Pair<Integer, AltAST>>> labels = r.getAltLabels();
    if (labels != null) {
        for (Map.Entry<String, List<Pair<Integer, AltAST>>> entry : labels.entrySet()) {
            String label = entry.getKey();
            List<AltAST> alts = new ArrayList<AltAST>();
            for (Pair<Integer, AltAST> pair : entry.getValue()) {
                alts.add(pair.b);
            }
            Set<Decl> decls = getDeclsForAllElements(alts);
            for (Pair<Integer, AltAST> pair : entry.getValue()) {
                Integer altNum = pair.a;
                altToContext[altNum] = new AltLabelStructDecl(factory, r, altNum, label);
                if (!altLabelCtxs.containsKey(label)) {
                    altLabelCtxs.put(label, altToContext[altNum]);
                }
                // we know which ctx to put in, so do it directly
                for (Decl d : decls) {
                    altToContext[altNum].addDecl(d);
                }
            }
        }
    }
}
Also used : 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) ArrayList(java.util.ArrayList) List(java.util.List) AltLabelStructDecl(org.antlr.v4.codegen.model.decl.AltLabelStructDecl) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Pair (org.antlr.v4.runtime.misc.Pair)15 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)10 ArrayList (java.util.ArrayList)8 AltAST (org.antlr.v4.tool.ast.AltAST)7 Lexer (org.antlr.v4.runtime.Lexer)4 Parser (org.antlr.v4.runtime.Parser)4 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)4 HashMap (java.util.HashMap)3 List (java.util.List)3 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)3 URL (java.net.URL)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 CommonToken (org.antlr.runtime.CommonToken)2 RecognitionException (org.antlr.runtime.RecognitionException)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