use of org.antlr.v4.codegen.model.RuleFunction 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.RuleFunction in project antlr4 by antlr.
the class ActionTranslator method translateAction.
public static List<ActionChunk> translateAction(OutputModelFactory factory, RuleFunction rf, Token tokenWithinAction, ActionAST node) {
String action = tokenWithinAction.getText();
if (action != null && action.length() > 0 && action.charAt(0) == '{') {
int firstCurly = action.indexOf('{');
int lastCurly = action.lastIndexOf('}');
if (firstCurly >= 0 && lastCurly >= 0) {
// trim {...}
action = action.substring(firstCurly + 1, lastCurly);
}
}
return translateActionChunk(factory, rf, action, node);
}
use of org.antlr.v4.codegen.model.RuleFunction 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.RuleFunction in project antlr4 by tunnelvisionlabs.
the class Target method getRuleFunctionContextStructName.
/**
* If we know which actual function, we can provide the actual ctx type.
* This will contain implicit labels etc... From outside, though, we
* see only ParserRuleContext unless there are externally visible stuff
* like args, locals, explicit labels, etc...
*/
public String getRuleFunctionContextStructName(RuleFunction function) {
Rule r = function.rule;
if (r.g.isLexer()) {
return getTemplates().getInstanceOf("LexerRuleContext").render();
}
String baseName = r.getBaseContext();
return Utils.capitalize(baseName) + getTemplates().getInstanceOf("RuleContextNameSuffix").render();
}
use of org.antlr.v4.codegen.model.RuleFunction in project antlr4 by tunnelvisionlabs.
the class DefaultOutputModelFactory method rulePostamble.
@Override
public List<SrcOp> rulePostamble(RuleFunction function, Rule r) {
if (r.namedActions.containsKey("after") || r.namedActions.containsKey("finally")) {
// See OutputModelController.buildLeftRecursiveRuleFunction
// and Parser.exitRule for other places which set stop.
CodeGenerator gen = getGenerator();
STGroup codegenTemplates = gen.getTemplates();
ST setStopTokenAST = codegenTemplates.getInstanceOf("recRuleSetStopToken");
Action setStopTokenAction = new Action(this, function.ruleCtx, setStopTokenAST);
List<SrcOp> ops = new ArrayList<SrcOp>(1);
ops.add(setStopTokenAction);
return ops;
}
return super.rulePostamble(function, r);
}
Aggregations