use of org.antlr.v4.codegen.model.decl.Decl 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.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.
the class ParserFactory method defineImplicitLabel.
// support
public void defineImplicitLabel(GrammarAST ast, LabeledOp op) {
Decl d;
if (ast.getType() == ANTLRParser.SET || ast.getType() == ANTLRParser.WILDCARD) {
String implLabel = getTarget().getImplicitSetLabel(String.valueOf(ast.token.getTokenIndex()));
d = getTokenLabelDecl(implLabel);
((TokenDecl) d).isImplicit = true;
} else if (ast.getType() == ANTLRParser.RULE_REF) {
// a rule reference?
Rule r = g.getRule(ast.getText());
String implLabel = getTarget().getImplicitRuleLabel(ast.getText());
String ctxName = getTarget().getRuleFunctionContextStructName(r);
d = new RuleContextDecl(this, implLabel, ctxName);
((RuleContextDecl) d).isImplicit = true;
} else {
String implLabel = getTarget().getImplicitTokenLabel(ast.getText());
d = getTokenLabelDecl(implLabel);
((TokenDecl) d).isImplicit = true;
}
op.getLabels().add(d);
// all labels must be in scope struct in case we exec action out of context
getCurrentRuleFunction().addContextDecl(ast.getAltLabel(), d);
}
use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.
the class ParserFactory method getChoiceBlock.
@Override
public Choice getChoiceBlock(BlockAST blkAST, List<CodeBlockForAlt> alts, GrammarAST labelAST) {
int decision = ((DecisionState) blkAST.atnState).decision;
Choice c;
if (!g.tool.force_atn && AnalysisPipeline.disjoint(g.decisionLOOK.get(decision))) {
c = getLL1ChoiceBlock(blkAST, alts);
} else {
c = getComplexChoiceBlock(blkAST, alts);
}
if (labelAST != null) {
// for x=(...), define x or x_list
String label = labelAST.getText();
Decl d = getTokenLabelDecl(label);
c.label = d;
getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), d);
if (labelAST.parent.getType() == ANTLRParser.PLUS_ASSIGN) {
String listLabel = getTarget().getListLabel(label);
TokenListDecl l = new TokenListDecl(this, listLabel);
getCurrentRuleFunction().addContextDecl(labelAST.getAltLabel(), l);
}
}
return c;
}
use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.
the class RuleFunction method getDeclForAltElement.
public List<Decl> getDeclForAltElement(GrammarAST t, String refLabelName, boolean needList, boolean optional) {
int lfIndex = refLabelName.indexOf(ATNSimulator.RULE_VARIANT_DELIMITER);
if (lfIndex >= 0) {
refLabelName = refLabelName.substring(0, lfIndex);
}
List<Decl> decls = new ArrayList<Decl>();
if (t.getType() == RULE_REF) {
Rule rref = factory.getGrammar().getRule(t.getText());
String ctxName = factory.getTarget().getRuleFunctionContextStructName(rref);
if (needList) {
if (factory.getTarget().supportsOverloadedMethods())
decls.add(new ContextRuleListGetterDecl(factory, refLabelName, ctxName));
decls.add(new ContextRuleListIndexedGetterDecl(factory, refLabelName, ctxName));
} else {
decls.add(new ContextRuleGetterDecl(factory, refLabelName, ctxName, optional));
}
} else {
if (needList) {
if (factory.getTarget().supportsOverloadedMethods())
decls.add(new ContextTokenListGetterDecl(factory, refLabelName));
decls.add(new ContextTokenListIndexedGetterDecl(factory, refLabelName));
} else {
decls.add(new ContextTokenGetterDecl(factory, refLabelName, optional));
}
}
return decls;
}
use of org.antlr.v4.codegen.model.decl.Decl 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