use of org.antlr.v4.runtime.tree.ErrorNode in project batfish by batfish.
the class BatfishANTLRErrorStrategy method consumeBlocksUntilWanted.
/**
* Consume all tokens a whole line at a time until the next token is one expected by the current
* rule. Each line (as delimited by supplied separator token) starting from the current line up to
* the last line consumed is placed in an {@link ErrorNode} and inserted as a child of the current
* rule.
*
* @param recognizer The {@link Parser} to whom to delegate creation of each {@link ErrorNode}
*/
private void consumeBlocksUntilWanted(Parser recognizer) {
IntervalSet expecting = recognizer.getExpectedTokens();
IntervalSet whatFollowsLoopIterationOrRule = expecting.or(getErrorRecoverySet(recognizer));
int nextToken;
do {
// Eat tokens until we are at the end of the line
consumeUntilEndOfLine(recognizer);
// Get the line number and separator text from the separator token
Token separatorToken = recognizer.getCurrentToken();
// Insert the current line as an {@link ErrorNode} as a child of the current rule
createErrorNode(recognizer, recognizer.getContext(), separatorToken);
// Eat the separator token
recognizer.consume();
nextToken = recognizer.getInputStream().LA(1);
} while (!whatFollowsLoopIterationOrRule.contains(nextToken) && nextToken != Lexer.EOF);
}
use of org.antlr.v4.runtime.tree.ErrorNode in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseTryCatch.
protected TryCatchStatement parseTryCatch(TryStContext tryStCtx) {
BlockContext tryBlockCtx = tryStCtx.block(0);
BlockStatement tryPart = (BlockStatement) this.parseBlock(tryBlockCtx.statement(), tryBlockCtx);
BlockStatement catchPart = null;
VarDefineNode errorNode = null;
if (tryStCtx.Catch() != null) {
this.pbCtx.enterBlock();
if (tryStCtx.Identifier() != null) {
Token errorToken = tryStCtx.Identifier().getSymbol();
errorNode = new VarDefineNode(this.getBTToken(errorToken));
this.pbCtx.addVarAndPostion(errorNode);
}
BlockContext catchBlockCtx = tryStCtx.block(1);
catchPart = (BlockStatement) this.parseBlock(catchBlockCtx.statement(), catchBlockCtx);
this.pbCtx.exitBlock();
}
TryCatchStatement statement = new TryCatchStatement(tryPart, catchPart, errorNode, this.getBTToken(tryStCtx.Try().getSymbol()));
return statement;
}
use of org.antlr.v4.runtime.tree.ErrorNode in project antlr4 by tunnelvisionlabs.
the class ParserRuleContext method addErrorNode.
/**
* Add a child to this node based upon badToken. It
* creates a ErrorNodeImpl rather than using
* {@link Parser#createErrorNode(ParserRuleContext, Token)}. I'm leaving this
* in for compatibility but the parser doesn't use this anymore.
*/
@Deprecated
public ErrorNode addErrorNode(Token badToken) {
ErrorNodeImpl t = new ErrorNodeImpl(badToken);
addAnyChild(t);
t.setParent(this);
return t;
}
use of org.antlr.v4.runtime.tree.ErrorNode in project antlr4 by antlr.
the class BaseCppTest method writeParserTestFile.
protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug, boolean trace) {
if (!parserStartRuleName.endsWith(")"))
parserStartRuleName += "()";
ST outputFileST = new ST("#include \\<iostream>\n" + "\n" + "#include \"antlr4-runtime.h\"\n" + "#include \"<lexerName>.h\"\n" + "#include \"<parserName>.h\"\n" + "\n" + "using namespace antlr4;\n" + "\n" + "class TreeShapeListener : public tree::ParseTreeListener {\n" + "public:\n" + " void visitTerminal(tree::TerminalNode *) override {}\n" + " void visitErrorNode(tree::ErrorNode *) override {}\n" + " void exitEveryRule(ParserRuleContext *) override {}\n" + " void enterEveryRule(ParserRuleContext *ctx) override {\n" + " for (auto child : ctx->children) {\n" + " tree::ParseTree *parent = child->parent;\n" + " ParserRuleContext *rule = dynamic_cast\\<ParserRuleContext *>(parent);\n" + " if (rule != ctx) {\n" + " throw \"Invalid parse tree shape detected.\";\n" + " }\n" + " }\n" + " }\n" + "};\n" + "\n" + "\n" + "int main(int argc, const char* argv[]) {\n" + " ANTLRFileStream input;\n" + " input.loadFromFile(argv[1]);\n" + " <lexerName> lexer(&input);\n" + " CommonTokenStream tokens(&lexer);\n" + "<createParser>" + "\n" + " tree::ParseTree *tree = parser.<parserStartRuleName>;\n" + " TreeShapeListener listener;\n" + " tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);\n" + "\n" + " return 0;\n" + "}\n");
String stSource = " <parserName> parser(&tokens);\n";
if (debug) {
stSource += " DiagnosticErrorListener errorListener;\n";
stSource += " parser.addErrorListener(&errorListener);\n";
}
if (trace)
stSource += " parser.setTrace(true);\n";
ST createParserST = new ST(stSource);
outputFileST.add("createParser", createParserST);
outputFileST.add("parserName", parserName);
outputFileST.add("lexerName", lexerName);
outputFileST.add("listenerName", listenerName);
outputFileST.add("visitorName", visitorName);
outputFileST.add("parserStartRuleName", parserStartRuleName);
writeFile(getTempDirPath(), "Test.cpp", outputFileST.render());
}
use of org.antlr.v4.runtime.tree.ErrorNode in project antlr4 by antlr.
the class BasePHPTest method writeParserTestFile.
protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug, boolean trace) {
if (!parserStartRuleName.endsWith(")")) {
parserStartRuleName += "()";
}
ST outputFileST = new ST("\\<?php\n" + "\n" + "declare(strict_types=1);\n" + "\n" + "use Antlr\\Antlr4\\Runtime\\CommonTokenStream;\n" + "use Antlr\\Antlr4\\Runtime\\Error\\Listeners\\DiagnosticErrorListener;\n" + "use Antlr\\Antlr4\\Runtime\\Error\\Listeners\\ConsoleErrorListener;\n" + "use Antlr\\Antlr4\\Runtime\\InputStream;\n" + "use Antlr\\Antlr4\\Runtime\\ParserRuleContext;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\ErrorNode;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\ParseTreeListener;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\ParseTreeWalker;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\RuleNode;\n" + "use Antlr\\Antlr4\\Runtime\\Tree\\TerminalNode;\n" + "\n" + "$runtime = \\getenv('RUNTIME');\n" + "\n" + "\\spl_autoload_register(function (string $class) use ($runtime) : void {\n" + " $file = \\str_replace('\\\\\\', \\DIRECTORY_SEPARATOR, \\str_replace('Antlr\\Antlr4\\Runtime\\\\\\', $runtime . '\\\\\\src\\\\\\', $class)) . '.php';\n" + "\n" + " if (\\file_exists($file)) {\n" + " require_once $file; \n" + " }\n" + "});\n" + "\n" + "final class TreeShapeListener implements ParseTreeListener {\n" + " public function visitTerminal(TerminalNode $node) : void {}\n" + " public function visitErrorNode(ErrorNode $node) : void {}\n" + " public function exitEveryRule(ParserRuleContext $ctx) : void {}\n" + "\n" + " public function enterEveryRule(ParserRuleContext $ctx) : void {\n" + " for ($i = 0, $count = $ctx->getChildCount(); $i \\< $count; $i++) {\n" + " $parent = $ctx->getChild($i)->getParent();\n" + "\n" + " if (!($parent instanceof RuleNode) || $parent->getRuleContext() !== $ctx) {\n" + " throw new RuntimeException('Invalid parse tree shape detected.');\n" + " }\n" + " }\n" + " }\n" + "}" + "\n" + "$input = InputStream::fromPath($argv[1]);\n" + "$lexer = new <lexerName>($input);\n" + "$lexer->addErrorListener(new ConsoleErrorListener());" + "$tokens = new CommonTokenStream($lexer);\n" + "<createParser>" + "$parser->addErrorListener(new ConsoleErrorListener());" + "$parser->setBuildParseTree(true);\n" + "$tree = $parser-><parserStartRuleName>;\n\n" + "ParseTreeWalker::default()->walk(new TreeShapeListener(), $tree);\n");
String stSource = "$parser = new <parserName>($tokens);\n";
if (debug) {
stSource += "$parser->addErrorListener(new DiagnosticErrorListener());\n";
}
if (trace) {
stSource += "$parser->setTrace(true);\n";
}
ST createParserST = new ST(stSource);
outputFileST.add("createParser", createParserST);
outputFileST.add("parserName", parserName);
outputFileST.add("lexerName", lexerName);
outputFileST.add("listenerName", listenerName);
outputFileST.add("visitorName", visitorName);
outputFileST.add("parserStartRuleName", parserStartRuleName);
writeFile(getTempDirPath(), "Test.php", outputFileST.render());
}
Aggregations