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);
}
}
}
}
use of org.antlr.v4.runtime.misc.Tuple2 in project antlr4 by tunnelvisionlabs.
the class LeftRecursiveRule method getAltLabels.
/**
* Get -> 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;
}
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;
}
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;
}
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;
}
Aggregations