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;
}
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);
}
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;
}
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>());
}
}
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);
}
}
}
}
}
Aggregations