use of org.antlr.v4.codegen.model.RuleFunction in project antlr4 by antlr.
the class OutputModelController method buildNormalRuleFunction.
public void buildNormalRuleFunction(Rule r, RuleFunction function) {
CodeGenerator gen = delegate.getGenerator();
// TRIGGER factory functions for rule alts, elements
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(r.ast.token.getInputStream());
GrammarAST blk = (GrammarAST) r.ast.getFirstChildWithType(ANTLRParser.BLOCK);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, blk);
walker = new SourceGenTriggers(nodes, this);
try {
// walk AST of rule alts/elements
function.code = DefaultOutputModelFactory.list(walker.block(null, null));
function.hasLookaheadBlock = walker.hasLookaheadBlock;
} catch (org.antlr.runtime.RecognitionException e) {
e.printStackTrace(System.err);
}
function.ctxType = gen.getTarget().getRuleFunctionContextStructName(function);
function.postamble = rulePostamble(function, r);
}
use of org.antlr.v4.codegen.model.RuleFunction in project antlr4 by antlr.
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);
}
use of org.antlr.v4.codegen.model.RuleFunction in project antlr4 by antlr.
the class OutputModelController method buildRuleFunction.
/**
* Create RuleFunction per rule and update sempreds,actions of parser
* output object with stuff found in r.
*/
public void buildRuleFunction(Parser parser, Rule r) {
RuleFunction function = rule(r);
parser.funcs.add(function);
pushCurrentRule(function);
function.fillNamedActions(delegate, r);
if (r instanceof LeftRecursiveRule) {
buildLeftRecursiveRuleFunction((LeftRecursiveRule) r, (LeftRecursiveRuleFunction) function);
} else {
buildNormalRuleFunction(r, function);
}
Grammar g = getGrammar();
for (ActionAST a : r.actions) {
if (a instanceof PredAST) {
PredAST p = (PredAST) a;
RuleSempredFunction rsf = parser.sempredFuncs.get(r);
if (rsf == null) {
rsf = new RuleSempredFunction(delegate, r, function.ctxType);
parser.sempredFuncs.put(r, rsf);
}
rsf.actions.put(g.sempreds.get(p), new Action(delegate, p));
}
}
popCurrentRule();
}
use of org.antlr.v4.codegen.model.RuleFunction in project antlr4 by antlr.
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.RuleFunction in project antlr4 by antlr.
the class ActionTranslator method translateActionChunk.
public static List<ActionChunk> translateActionChunk(OutputModelFactory factory, RuleFunction rf, String action, ActionAST node) {
Token tokenWithinAction = node.token;
ActionTranslator translator = new ActionTranslator(factory, node);
translator.rf = rf;
factory.getGrammar().tool.log("action-translator", "translate " + action);
String altLabel = node.getAltLabel();
if (rf != null) {
translator.nodeContext = rf.ruleCtx;
if (altLabel != null)
translator.nodeContext = rf.altLabelCtxs.get(altLabel);
}
ANTLRStringStream in = new ANTLRStringStream(action);
in.setLine(tokenWithinAction.getLine());
in.setCharPositionInLine(tokenWithinAction.getCharPositionInLine());
ActionSplitter trigger = new ActionSplitter(in, translator);
// forces eval, triggers listener methods
trigger.getActionTokens();
return translator.chunks;
}
Aggregations