From d685e6f1409048fecc8f171f01c873c392923a74 Mon Sep 17 00:00:00 2001 From: Aaron Braunstein Date: Thu, 26 Oct 2023 03:22:32 -0700 Subject: [PATCH] Do not explore beyond rule stop states when computing possible next tokens during error recovery Fixes https://github.com/tunnelvisionlabs/antlr4/issues/84 --- contributors.txt | 3 +- .../runtime/templates/ParserErrors/Index.stg | 1 + .../templates/ParserErrors/TokenMismatch4.stg | 49 +++++++++++++++++++ .../org/antlr/v4/runtime/atn/LL1Analyzer.java | 3 ++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/ParserErrors/TokenMismatch4.stg diff --git a/contributors.txt b/contributors.txt index e57317c7d0..4acd825016 100644 --- a/contributors.txt +++ b/contributors.txt @@ -280,4 +280,5 @@ YYYY/MM/DD, github id, Full name, email 2020/10/16, adarshbhat, Adarsh Bhat, adarshbhat@users.noreply.github.com 2020/10/20, adamwojs, Adam Wójs, adam[at]wojs.pl 2020/10/24, cliid, Jiwu Jang, jiwujang@naver.com -2020/11/05, MichelHartmann, Michel Hartmann, MichelHartmann@users.noreply.github.com \ No newline at end of file +2020/11/05, MichelHartmann, Michel Hartmann, MichelHartmann@users.noreply.github.com +2023/10/26, br0nstein, Aaron Braunstein, aa(last name)@gmail.com \ No newline at end of file diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/ParserErrors/Index.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/ParserErrors/Index.stg index 88e25c6f75..c7520b016a 100644 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/ParserErrors/Index.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/ParserErrors/Index.stg @@ -32,5 +32,6 @@ TestTemplates ::= [ "NoViableAltAvoidance": [], "TokenMismatch2": [], "TokenMismatch3": [], + "TokenMismatch4": [], "ExtraneousInput": [] ] diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/ParserErrors/TokenMismatch4.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/ParserErrors/TokenMismatch4.stg new file mode 100644 index 0000000000..d184d90e7f --- /dev/null +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/ParserErrors/TokenMismatch4.stg @@ -0,0 +1,49 @@ +TestType() ::= "Parser" + +Options ::= [ + "Debug": false +] + +Grammar ::= [ + "T": {} +] + +Input() ::= "SELECT CONCAT() FROM tbl" + +Rule() ::= "s" + +Output() ::= << +(selectStatement (selectQuery SELECT (expr (func CONCAT ( (expr )))) (fromClause FROM tbl)) limitOpt) +Error encountered: [@3,14:14=')',\<7>,1:14]<\n> +>> + +Errors() ::= << +line 1:14 mismatched input ')' expecting {IDENTIFIER, NUMBER}<\n> +>> + +grammar(grammarName) ::= << +grammar ; + + + + +s +@init { + +} +@after { + + +} + : r=selectStatement ; +selectStatement : selectQuery limitOpt ; +selectQuery : 'SELECT' (expr | '*') fromClause? ; +limitOpt : ('LIMIT' NUMBER)? ; +expr : func | IDENTIFIER | NUMBER ; +fromClause : 'FROM' IDENTIFIER ; +func : IDENTIFIER '(' expr ',' expr (',' expr)* ')' ; +parenthesizedSelectQueryOrExpr : '(' (selectQuery limitOpt | expr) ')' ; +IDENTIFIER : [a-zA-Z]+ ; +NUMBER : [1-9][0-9]* ; +WS : [ \r\t\n]+ -> skip ; +>> diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java index b337dfe9bc..2287abc442 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LL1Analyzer.java @@ -190,6 +190,9 @@ protected void _LOOK(@NotNull ATNState s, calledRuleStack.set(s.ruleIndex); } } + if (!PredictionContext.isEmptyLocal(ctx)) { + return; + } } int n = s.getNumberOfTransitions();