use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by tunnelvisionlabs.
the class ScopeParser method parseAttributeDef.
/**
* For decls like "String foo" or "char *foo32[]" compute the ID
* and type declarations. Also handle "int x=3" and 'T t = new T("foo")'
* but if the separator is ',' you cannot use ',' in the initvalue
* unless you escape use "\," escape.
*/
public static Attribute parseAttributeDef(@Nullable ActionAST action, @NotNull Tuple2<String, Integer> decl, Grammar g) {
if (decl.getItem1() == null)
return null;
Attribute attr = new Attribute();
int rightEdgeOfDeclarator = decl.getItem1().length() - 1;
int equalsIndex = decl.getItem1().indexOf('=');
if (equalsIndex > 0) {
// everything after the '=' is the init value
attr.initValue = decl.getItem1().substring(equalsIndex + 1, decl.getItem1().length()).trim();
rightEdgeOfDeclarator = equalsIndex - 1;
}
String declarator = decl.getItem1().substring(0, rightEdgeOfDeclarator + 1);
Tuple2<Integer, Integer> p;
String text = decl.getItem1();
text = text.replaceAll("::", "");
if (text.contains(":")) {
// declarator has type appearing after the name like "x:T"
p = _parsePostfixDecl(attr, declarator, action, g);
} else {
// declarator has type appearing before the name like "T x"
p = _parsePrefixDecl(attr, declarator, action, g);
}
int idStart = p.getItem1();
int idStop = p.getItem2();
attr.decl = decl.getItem1();
if (action != null) {
String actionText = action.getText();
int[] lines = new int[actionText.length()];
int[] charPositionInLines = new int[actionText.length()];
for (int i = 0, line = 0, col = 0; i < actionText.length(); i++, col++) {
lines[i] = line;
charPositionInLines[i] = col;
if (actionText.charAt(i) == '\n') {
line++;
col = -1;
}
}
int[] charIndexes = new int[actionText.length()];
for (int i = 0, j = 0; i < actionText.length(); i++, j++) {
charIndexes[j] = i;
// skip comments
if (i < actionText.length() - 1 && actionText.charAt(i) == '/' && actionText.charAt(i + 1) == '/') {
while (i < actionText.length() && actionText.charAt(i) != '\n') {
i++;
}
}
}
int declOffset = charIndexes[decl.getItem2()];
int declLine = lines[declOffset + idStart];
int line = action.getToken().getLine() + declLine;
int charPositionInLine = charPositionInLines[declOffset + idStart];
if (declLine == 0) {
/* offset for the start position of the ARG_ACTION token, plus 1
* since the ARG_ACTION text had the leading '[' stripped before
* reaching the scope parser.
*/
charPositionInLine += action.getToken().getCharPositionInLine() + 1;
}
int offset = ((CommonToken) action.getToken()).getStartIndex();
attr.token = new CommonToken(action.getToken().getInputStream(), ANTLRParser.ID, BaseRecognizer.DEFAULT_TOKEN_CHANNEL, offset + declOffset + idStart + 1, offset + declOffset + idStop);
attr.token.setLine(line);
attr.token.setCharPositionInLine(charPositionInLine);
assert attr.name.equals(attr.token.getText()) : "Attribute text should match the pseudo-token text at this point.";
}
return attr;
}
use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by antlr.
the class Choice method addCodeForLookaheadTempVar.
public TestSetInline addCodeForLookaheadTempVar(IntervalSet look) {
List<SrcOp> testOps = factory.getLL1Test(look, ast);
TestSetInline expr = Utils.find(testOps, TestSetInline.class);
if (expr != null) {
Decl d = new TokenTypeDecl(factory, expr.varName);
factory.getCurrentRuleFunction().addLocalDecl(d);
CaptureNextTokenType nextType = new CaptureNextTokenType(factory, expr.varName);
addPreambleOp(nextType);
}
return expr;
}
use of org.antlr.v4.codegen.model.decl.Decl in project antlr4 by antlr.
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 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.Decl 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;
IntervalSet reftypes = new IntervalSet(RULE_REF, TOKEN_REF, STRING_LITERAL);
for (AltAST ast : altASTs) {
List<GrammarAST> refs = getRuleTokens(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 = getName(t);
if (refLabelName != null) {
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 = getName(t);
if (refLabelName == null) {
continue;
}
List<Decl> d = getDeclForAltElement(t, refLabelName, needsList.contains(refLabelName), !nonOptional.contains(refLabelName));
decls.addAll(d);
}
return decls;
}
Aggregations