use of org.antlr.v4.parse.GrammarASTAdaptor in project antlr4 by antlr.
the class GrammarAST method toTokenString.
public String toTokenString() {
CharStream input = this.token.getInputStream();
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(input);
CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, this);
StringBuilder buf = new StringBuilder();
GrammarAST o = (GrammarAST) nodes.LT(1);
int type = adaptor.getType(o);
while (type != Token.EOF) {
buf.append(" ");
buf.append(o.getText());
nodes.consume();
o = (GrammarAST) nodes.LT(1);
type = adaptor.getType(o);
}
return buf.toString();
}
use of org.antlr.v4.parse.GrammarASTAdaptor 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.parse.GrammarASTAdaptor in project antlr4 by antlr.
the class LeftRecursiveRuleTransformer method translateLeftRecursiveRules.
public void translateLeftRecursiveRules() {
String language = g.getOptionString("language");
// translate all recursive rules
List<String> leftRecursiveRuleNames = new ArrayList<String>();
for (Rule r : rules) {
if (!Grammar.isTokenName(r.name)) {
if (LeftRecursiveRuleAnalyzer.hasImmediateRecursiveRuleRefs(r.ast, r.name)) {
boolean fitsPattern = translateLeftRecursiveRule(ast, (LeftRecursiveRule) r, language);
if (fitsPattern) {
leftRecursiveRuleNames.add(r.name);
} else {
// better given an error that non-conforming left-recursion exists
tool.errMgr.grammarError(ErrorType.NONCONFORMING_LR_RULE, g.fileName, ((GrammarAST) r.ast.getChild(0)).token, r.name);
}
}
}
}
// update all refs to recursive rules to have [0] argument
for (GrammarAST r : ast.getNodesWithType(ANTLRParser.RULE_REF)) {
// must be rule def
if (r.getParent().getType() == ANTLRParser.RULE)
continue;
// already has arg; must be in rewritten rule
if (((GrammarASTWithOptions) r).getOptionString(PRECEDENCE_OPTION_NAME) != null)
continue;
if (leftRecursiveRuleNames.contains(r.getText())) {
// found ref to recursive rule not already rewritten with arg
((GrammarASTWithOptions) r).setOption(PRECEDENCE_OPTION_NAME, (GrammarAST) new GrammarASTAdaptor().create(ANTLRParser.INT, "0"));
}
}
}
Aggregations