changeset 48743:c969fffbfc9b switch

Improving parser error recovery.
author jlahoda
date Mon, 15 Jan 2018 20:52:48 +0100
parents 9278248c6c71
children cc4fec682911
files src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java test/langtools/tools/javac/switchexpr/ParserRecovery.java test/langtools/tools/javac/switchexpr/ParserRecovery.out
diffstat 3 files changed, 45 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Mon Jan 15 11:12:51 2018 +0100
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java	Mon Jan 15 20:52:48 2018 +0100
@@ -1384,8 +1384,8 @@
                     accept(CASE);
                     while (true) {
                         pat = term(EXPR | NOLAMBDA);
-                        if (token.kind == COLON || token.kind == ARROW) break;
-                        accept(COMMA);
+                        if (token.kind != COMMA) break;
+                        nextToken();
                         caseExprs.append(toP(F.at(pos).CaseExpression(pat, List.nil(), null)));
                     };
                 }
@@ -1402,8 +1402,8 @@
                             accept(SEMI);
                         }
                         break;
-                    case COLON:
-                        nextToken();
+                    default:
+                        accept(COLON);
                         stats = blockStatements();
                         break;
                 }
@@ -2718,8 +2718,8 @@
             JCExpression pat;
             while (true) {
                 pat = parseExpression();
-                if (token.kind == COLON) break;
-                accept(COMMA);
+                if (token.kind != COMMA) break;
+                nextToken();
                 c = F.at(pos).Case(pat, List.nil());
                 storeEnd(c, S.prevToken().endPos);
                 cases.append(c);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchexpr/ParserRecovery.java	Mon Jan 15 20:52:48 2018 +0100
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @compile/fail/ref=ParserRecovery.out -XDdev -XDrawDiagnostics ParserRecovery.java
+ */
+
+public class ParserRecovery {
+    void t1(int e) {
+         int i = switch (e) { case any; };
+    }
+    void t2(int e) {
+         switch (e) { case any; }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchexpr/ParserRecovery.out	Mon Jan 15 20:52:48 2018 +0100
@@ -0,0 +1,3 @@
+ParserRecovery.java:31:39: compiler.err.expected: :
+ParserRecovery.java:34:31: compiler.err.expected: :
+2 errors