use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class ParserATNFactory method plus.
/**
* From {@code (blk)+} build
*
* <pre>
* |---------|
* v |
* [o-blk-o]->o->o
* </pre>
*
* We add a decision for loop back node to the existing one at {@code blk}
* start.
*/
@Override
public Handle plus(GrammarAST plusAST, Handle blk) {
PlusBlockStartState blkStart = (PlusBlockStartState) blk.left;
BlockEndState blkEnd = (BlockEndState) blk.right;
preventEpsilonClosureBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));
PlusLoopbackState loop = newState(PlusLoopbackState.class, plusAST);
loop.nonGreedy = !((QuantifierAST) plusAST).isGreedy();
atn.defineDecisionState(loop);
LoopEndState end = newState(LoopEndState.class, plusAST);
blkStart.loopBackState = loop;
end.loopBackState = loop;
plusAST.atnState = loop;
// blk can see loop back
epsilon(blkEnd, loop);
BlockAST blkAST = (BlockAST) plusAST.getChild(0);
if (((QuantifierAST) plusAST).isGreedy()) {
if (expectNonGreedy(blkAST)) {
g.tool.errMgr.grammarError(ErrorType.EXPECTED_NON_GREEDY_WILDCARD_BLOCK, g.fileName, plusAST.getToken(), plusAST.getToken().getText());
}
// loop back to start
epsilon(loop, blkStart);
// or exit
epsilon(loop, end);
} else {
// if not greedy, priority to exit branch; make it first
// exit
epsilon(loop, end);
// loop back to start
epsilon(loop, blkStart);
}
return new Handle(blkStart, end);
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class ParserATNFactory method wildcard.
/** Build an atom with all possible values in its label. */
@Override
public Handle wildcard(GrammarAST node) {
ATNState left = newState(node);
ATNState right = newState(node);
left.addTransition(new WildcardTransition(right));
node.atnState = left;
return new Handle(left, right);
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class ParserATNFactory method optional.
/**
* From {@code (A)?} build either:
*
* <pre>
* o--A->o
* | ^
* o---->|
* </pre>
*
* or, if {@code A} is a block, just add an empty alt to the end of the
* block
*/
@Override
public Handle optional(GrammarAST optAST, Handle blk) {
BlockStartState blkStart = (BlockStartState) blk.left;
ATNState blkEnd = blk.right;
preventEpsilonOptionalBlocks.add(new Triple<Rule, ATNState, ATNState>(currentRule, blkStart, blkEnd));
boolean greedy = ((QuantifierAST) optAST).isGreedy();
blkStart.nonGreedy = !greedy;
epsilon(blkStart, blk.right, !greedy);
optAST.atnState = blk.left;
return blk;
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class CodeGenPipeline method process.
public void process() {
if (!CodeGenerator.targetExists(g.getOptionString("language")))
return;
CodeGenerator gen = new CodeGenerator(g);
IntervalSet idTypes = new IntervalSet();
idTypes.add(ANTLRParser.ID);
idTypes.add(ANTLRParser.RULE_REF);
idTypes.add(ANTLRParser.TOKEN_REF);
List<GrammarAST> idNodes = g.ast.getNodesWithType(idTypes);
for (GrammarAST idNode : idNodes) {
if (gen.getTarget().grammarSymbolCausesIssueInGeneratedCode(idNode)) {
g.tool.errMgr.grammarError(ErrorType.USE_OF_BAD_WORD, g.fileName, idNode.getToken(), idNode.getText());
}
}
// all templates are generated in memory to report the most complete
// error information possible, but actually writing output files stops
// after the first error is reported
int errorCount = g.tool.errMgr.getNumErrors();
if (g.isLexer()) {
if (gen.getTarget().needsHeader()) {
// Header file if needed.
ST lexer = gen.generateLexer(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
writeRecognizer(lexer, gen, true);
}
}
ST lexer = gen.generateLexer(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
writeRecognizer(lexer, gen, false);
}
} else {
if (gen.getTarget().needsHeader()) {
ST parser = gen.generateParser(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
writeRecognizer(parser, gen, true);
}
}
ST parser = gen.generateParser(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
writeRecognizer(parser, gen, false);
}
if (g.tool.gen_listener) {
if (gen.getTarget().needsHeader()) {
ST listener = gen.generateListener(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeListener(listener, true);
}
}
ST listener = gen.generateListener(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeListener(listener, false);
}
if (gen.getTarget().needsHeader()) {
ST baseListener = gen.generateBaseListener(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeBaseListener(baseListener, true);
}
}
if (gen.getTarget().wantsBaseListener()) {
ST baseListener = gen.generateBaseListener(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeBaseListener(baseListener, false);
}
}
}
if (g.tool.gen_visitor) {
if (gen.getTarget().needsHeader()) {
ST visitor = gen.generateVisitor(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeVisitor(visitor, true);
}
}
ST visitor = gen.generateVisitor(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeVisitor(visitor, false);
}
if (gen.getTarget().needsHeader()) {
ST baseVisitor = gen.generateBaseVisitor(true);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeBaseVisitor(baseVisitor, true);
}
}
if (gen.getTarget().wantsBaseVisitor()) {
ST baseVisitor = gen.generateBaseVisitor(false);
if (g.tool.errMgr.getNumErrors() == errorCount) {
gen.writeBaseVisitor(baseVisitor, false);
}
}
}
}
gen.writeVocabFile();
}
use of org.antlr.v4.tool.ast.GrammarAST in project antlr4 by antlr.
the class RuleFunction method getDeclForAltElement.
public List<Decl> getDeclForAltElement(GrammarAST t, String refLabelName, boolean needList, boolean optional) {
List<Decl> decls = new ArrayList<Decl>();
if (t.getType() == RULE_REF) {
Rule rref = factory.getGrammar().getRule(t.getText());
String ctxName = factory.getGenerator().getTarget().getRuleFunctionContextStructName(rref);
if (needList) {
if (factory.getGenerator().getTarget().supportsOverloadedMethods())
decls.add(new ContextRuleListGetterDecl(factory, refLabelName, ctxName));
decls.add(new ContextRuleListIndexedGetterDecl(factory, refLabelName, ctxName));
} else {
decls.add(new ContextRuleGetterDecl(factory, refLabelName, ctxName, optional));
}
} else {
if (needList) {
if (factory.getGenerator().getTarget().supportsOverloadedMethods())
decls.add(new ContextTokenListGetterDecl(factory, refLabelName));
decls.add(new ContextTokenListIndexedGetterDecl(factory, refLabelName));
} else {
decls.add(new ContextTokenGetterDecl(factory, refLabelName, optional));
}
}
return decls;
}
Aggregations