Search in sources :

Example 16 with ErrorNode

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);
}
Also used : IntervalSet(org.antlr.v4.runtime.misc.IntervalSet) Token(org.antlr.v4.runtime.Token)

Example 17 with ErrorNode

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;
}
Also used : BlockContext(org.beetl.core.parser.BeetlParser.BlockContext) VarDefineNode(org.beetl.core.statement.VarDefineNode) TryCatchStatement(org.beetl.core.statement.TryCatchStatement) BlockStatement(org.beetl.core.statement.BlockStatement) Token(org.antlr.v4.runtime.Token) GrammarToken(org.beetl.core.statement.GrammarToken)

Example 18 with ErrorNode

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;
}
Also used : ErrorNodeImpl(org.antlr.v4.runtime.tree.ErrorNodeImpl)

Example 19 with ErrorNode

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());
}
Also used : ST(org.stringtemplate.v4.ST) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Example 20 with ErrorNode

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());
}
Also used : ST(org.stringtemplate.v4.ST) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Aggregations

Token (org.antlr.v4.runtime.Token)9 ErrorNode (org.antlr.v4.runtime.tree.ErrorNode)8 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)6 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)3 ErrorNodeImpl (org.antlr.v4.runtime.tree.ErrorNodeImpl)3 ParseTree (org.antlr.v4.runtime.tree.ParseTree)3 Rectangle2D (java.awt.geom.Rectangle2D)2 ArrayDeque (java.util.ArrayDeque)2 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 RuleContext (org.antlr.v4.runtime.RuleContext)2 IntegerStack (org.antlr.v4.runtime.misc.IntegerStack)2 ParseTreeListener (org.antlr.v4.runtime.tree.ParseTreeListener)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)2 ST (org.stringtemplate.v4.ST)2 AutocompleteCandidate (com.twosigma.beakerx.autocomplete.AutocompleteCandidate)1 BlockStatementContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.BlockStatementContext)1