use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.
the class ParserFactory method set.
@Override
public List<SrcOp> set(GrammarAST setAST, GrammarAST labelAST, boolean invert) {
MatchSet matchOp;
if (invert)
matchOp = new MatchNotSet(this, setAST);
else
matchOp = new MatchSet(this, setAST);
if (labelAST != null) {
String label = labelAST.getText();
RuleFunction rf = getCurrentRuleFunction();
if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
defineImplicitLabel(setAST, matchOp);
TokenListDecl l = getTokenListLabelDecl(label);
rf.addContextDecl(setAST.getAltLabel(), l);
} else {
Decl d = getTokenLabelDecl(label);
matchOp.labels.add(d);
rf.addContextDecl(setAST.getAltLabel(), d);
}
}
if (controller.needsImplicitLabel(setAST, matchOp))
defineImplicitLabel(setAST, matchOp);
AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
return list(matchOp, listLabelOp);
}
use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.
the class ParserFactory method wildcard.
@Override
public List<SrcOp> wildcard(GrammarAST ast, GrammarAST labelAST) {
Wildcard wild = new Wildcard(this, ast);
// TODO: dup with tokenRef
if (labelAST != null) {
String label = labelAST.getText();
Decl d = getTokenLabelDecl(label);
wild.labels.add(d);
getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), d);
if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
TokenListDecl l = getTokenListLabelDecl(label);
getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), l);
}
}
if (controller.needsImplicitLabel(ast, wild))
defineImplicitLabel(ast, wild);
AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(wild, labelAST);
return list(wild, listLabelOp);
}
use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.
the class ParserFactory method tokenRef.
@Override
public List<SrcOp> tokenRef(GrammarAST ID, GrammarAST labelAST, GrammarAST args) {
MatchToken matchOp = new MatchToken(this, (TerminalAST) ID);
if (labelAST != null) {
String label = labelAST.getText();
RuleFunction rf = getCurrentRuleFunction();
if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
// add Token _X and List<Token> X decls
// adds _X
defineImplicitLabel(ID, matchOp);
TokenListDecl l = getTokenListLabelDecl(label);
rf.addContextDecl(ID.getAltLabel(), l);
} else {
Decl d = getTokenLabelDecl(label);
matchOp.labels.add(d);
rf.addContextDecl(ID.getAltLabel(), d);
}
// Decl d = getTokenLabelDecl(label);
// ((MatchToken)matchOp).labels.add(d);
// getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), d);
// if ( labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN ) {
// TokenListDecl l = getTokenListLabelDecl(label);
// getCurrentRuleFunction().addContextDecl(ID.getAltLabel(), l);
// }
}
if (controller.needsImplicitLabel(ID, matchOp))
defineImplicitLabel(ID, matchOp);
AddToLabelList listLabelOp = getAddToListOpIfListLabelPresent(matchOp, labelAST);
return list(matchOp, listLabelOp);
}
use of org.antlr.v4.codegen.model.decl.Decl 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.Decl 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);
}
Aggregations