use of org.antlr.v4.codegen.model.decl.AltLabelStructDecl in project antlr4 by tunnelvisionlabs.
the class RuleFunction method addContextGetters.
public void addContextGetters(OutputModelFactory factory, Collection<RuleAST> contextASTs) {
List<AltAST> unlabeledAlternatives = new ArrayList<AltAST>();
Map<String, List<AltAST>> labeledAlternatives = new LinkedHashMap<String, List<AltAST>>();
for (RuleAST ast : contextASTs) {
try {
unlabeledAlternatives.addAll(rule.g.getUnlabeledAlternatives(ast));
for (Map.Entry<String, List<Tuple2<Integer, AltAST>>> entry : rule.g.getLabeledAlternatives(ast).entrySet()) {
List<AltAST> list = labeledAlternatives.get(entry.getKey());
if (list == null) {
list = new ArrayList<AltAST>();
labeledAlternatives.put(entry.getKey(), list);
}
for (Tuple2<Integer, AltAST> tuple : entry.getValue()) {
list.add(tuple.getItem2());
}
}
} catch (RecognitionException ex) {
}
}
// Add ctx labels for elements in alts with no '#' label
if (!unlabeledAlternatives.isEmpty()) {
Set<Decl> decls = getDeclsForAllElements(unlabeledAlternatives);
// put directly in base context
for (Decl decl : decls) {
ruleCtx.addDecl(decl);
}
}
// make structs for '#' labeled alts, define ctx labels for elements
altLabelCtxs = new LinkedHashMap<String, AltLabelStructDecl>();
if (!labeledAlternatives.isEmpty()) {
for (Map.Entry<String, List<AltAST>> entry : labeledAlternatives.entrySet()) {
AltLabelStructDecl labelDecl = new AltLabelStructDecl(factory, rule, entry.getKey());
altLabelCtxs.put(entry.getKey(), labelDecl);
Set<Decl> decls = getDeclsForAllElements(entry.getValue());
for (Decl decl : decls) {
labelDecl.addDecl(decl);
}
}
}
}
use of org.antlr.v4.codegen.model.decl.AltLabelStructDecl in project antlr4 by tunnelvisionlabs.
the class RuleFunction method addContextDecl.
/**
* Add decl to struct ctx for rule or alt if labeled
*/
public void addContextDecl(String altLabel, Decl d) {
CodeBlockForOuterMostAlt alt = d.getOuterMostAltCodeBlock();
// if we found code blk and might be alt label, try to add to that label ctx
if (alt != null && altLabelCtxs != null) {
// System.out.println(d.name+" lives in alt "+alt.alt.altNum);
AltLabelStructDecl altCtx = altLabelCtxs.get(altLabel);
if (altCtx != null) {
// we have an alt ctx
// System.out.println("ctx is "+ altCtx.name);
altCtx.addDecl(d);
return;
}
}
// stick in overall rule's ctx
ruleCtx.addDecl(d);
}
use of org.antlr.v4.codegen.model.decl.AltLabelStructDecl 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);
}
}
}
}
}
use of org.antlr.v4.codegen.model.decl.AltLabelStructDecl in project antlr4 by antlr.
the class RuleFunction method addContextDecl.
/**
* Add decl to struct ctx for rule or alt if labeled
*/
public void addContextDecl(String altLabel, Decl d) {
CodeBlockForOuterMostAlt alt = d.getOuterMostAltCodeBlock();
// if we found code blk and might be alt label, try to add to that label ctx
if (alt != null && altLabelCtxs != null) {
// System.out.println(d.name+" lives in alt "+alt.alt.altNum);
AltLabelStructDecl altCtx = altLabelCtxs.get(altLabel);
if (altCtx != null) {
// we have an alt ctx
// System.out.println("ctx is "+ altCtx.name);
altCtx.addDecl(d);
return;
}
}
// stick in overall rule's ctx
ruleCtx.addDecl(d);
}
Aggregations