use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class ActionTranslator method nonLocalAttr.
@Override
public void nonLocalAttr(String expr, Token x, Token y) {
gen.g.tool.log("action-translator", "nonLocalAttr " + x + "::" + y);
Rule r = factory.getGrammar().getRule(x.getText());
chunks.add(new NonLocalAttrRef(nodeContext, x.getText(), y.getText(), r.index));
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class ActionTranslator method qualifiedAttr.
@Override
public void qualifiedAttr(String expr, Token x, Token y) {
gen.g.tool.log("action-translator", "qattr " + x + "." + y);
if (node.resolver.resolveToAttribute(x.getText(), node) != null) {
// must be a member access to a predefined attribute like $ctx.foo
attr(expr, x);
chunks.add(new ActionText(nodeContext, "." + y.getText()));
return;
}
Attribute a = node.resolver.resolveToAttribute(x.getText(), y.getText(), node);
if (a == null) {
// Added in response to https://github.com/antlr/antlr4/issues/1211
gen.g.tool.errMgr.grammarError(ErrorType.UNKNOWN_SIMPLE_ATTRIBUTE, gen.g.fileName, x, x.getText(), "rule");
return;
}
switch(a.dict.type) {
// has to be current rule
case ARG:
chunks.add(new ArgRef(nodeContext, y.getText()));
break;
case RET:
chunks.add(new QRetValueRef(nodeContext, getRuleLabel(x.getText()), y.getText()));
break;
case PREDEFINED_RULE:
chunks.add(getRulePropertyRef(x, y));
break;
case TOKEN:
chunks.add(getTokenPropertyRef(x, y));
break;
}
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
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 = delegate.getTarget().getRuleFunctionContextStructName(function);
function.postamble = rulePostamble(function, r);
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class OutputModelController method buildParserOutputModel.
/**
* Build a file with a parser containing rule functions. Use the
* controller as factory in SourceGenTriggers so it triggers codegen
* extensions too, not just the factory functions in this factory.
*/
public OutputModelObject buildParserOutputModel(boolean header) {
CodeGenerator gen = delegate.getGenerator();
ParserFile file = parserFile(gen.getRecognizerFileName(header));
setRoot(file);
file.parser = parser(file);
Grammar g = delegate.getGrammar();
for (Rule r : g.rules.values()) {
buildRuleFunction(file.parser, r);
}
return file;
}
use of org.antlr.v4.tool.Rule in project antlr4 by tunnelvisionlabs.
the class OutputModelController method buildLexerRuleActions.
public void buildLexerRuleActions(Lexer lexer, final Rule r) {
if (r.actions.isEmpty()) {
return;
}
CodeGenerator gen = delegate.getGenerator();
Grammar g = delegate.getGrammar();
String ctxType = delegate.getTarget().getRuleFunctionContextStructName(r);
RuleActionFunction raf = lexer.actionFuncs.get(r);
if (raf == null) {
raf = new RuleActionFunction(delegate, r, ctxType);
}
for (ActionAST a : r.actions) {
if (a instanceof PredAST) {
PredAST p = (PredAST) a;
RuleSempredFunction rsf = lexer.sempredFuncs.get(r);
if (rsf == null) {
rsf = new RuleSempredFunction(delegate, r, ctxType);
lexer.sempredFuncs.put(r, rsf);
}
rsf.actions.put(g.sempreds.get(p), new Action(delegate, p));
} else if (a.getType() == ANTLRParser.ACTION) {
raf.actions.put(g.lexerActions.get(a), new Action(delegate, a));
}
}
if (!raf.actions.isEmpty() && !lexer.actionFuncs.containsKey(r)) {
// only add to lexer if the function actually contains actions
lexer.actionFuncs.put(r, raf);
}
}
Aggregations