From eb9124fff8ea8f1c31733dd4898d71d4804fc029 Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Fri, 10 Nov 2017 23:49:00 -0800 Subject: [PATCH] [Swift] Fix parse-cancellation in BailErrorStrategy. BailErrorStrategy is supposed to throw an error that's different from the ordinary recognition error, specifically so that it can be handled differently by client code. This was not ported over from Java correctly. Fix this by moving parseCancellation from ANTLRError to ANTLRException, adding its RecognitionException argument, and throwing it from the two handlers in BailErrorStrategy. Also remove ANTLRException.cannotInvokeStartRule, which is unused. (The Java runtime uses it when ParseTreePatternMatcher throws a generic exception, but we don't have that.) --- .../test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java | 4 ---- runtime/Swift/Sources/Antlr4/BailErrorStrategy.swift | 4 ++-- runtime/Swift/Sources/Antlr4/misc/exception/ANTLRError.swift | 1 - .../Swift/Sources/Antlr4/misc/exception/ANTLRException.swift | 2 +- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java index 90dc05245..530782e86 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java @@ -290,8 +290,6 @@ public class BaseSwiftTest implements RuntimeTestSupport { "let tree = try parser.()\n" + "print(profiler.getDecisionInfo().description)\n" + "try ParseTreeWalker.DEFAULT.walk(TreeShapeListener(), tree)\n" + - "}catch ANTLRException.cannotInvokeStartRule {\n" + - " print(\"error occur: cannotInvokeStartRule\")\n" + "}catch ANTLRException.recognition(let e ) {\n" + " print(\"error occur\\(e)\")\n" + "}catch {\n" + @@ -333,8 +331,6 @@ public class BaseSwiftTest implements RuntimeTestSupport { "do {\n" + " try tokens.fill()\n" + - "} catch ANTLRException.cannotInvokeStartRule {\n" + - " print(\"error occur: cannotInvokeStartRule\")\n" + "} catch ANTLRException.recognition(let e ) {\n" + " print(\"error occur\\(e)\")\n" + "} catch {\n" + diff --git a/runtime/Swift/Sources/Antlr4/BailErrorStrategy.swift b/runtime/Swift/Sources/Antlr4/BailErrorStrategy.swift index d1e81140c..cb4db3a49 100644 --- a/runtime/Swift/Sources/Antlr4/BailErrorStrategy.swift +++ b/runtime/Swift/Sources/Antlr4/BailErrorStrategy.swift @@ -47,7 +47,7 @@ public class BailErrorStrategy: DefaultErrorStrategy { context = (contextWrap.getParent() as? ParserRuleContext) } - throw ANTLRException.recognition(e: e) + throw ANTLRException.parseCancellation(e: e) } /// @@ -63,7 +63,7 @@ public class BailErrorStrategy: DefaultErrorStrategy { context = (contextWrap.getParent() as? ParserRuleContext) } - throw ANTLRException.recognition(e: e) + throw ANTLRException.parseCancellation(e: e) } /// diff --git a/runtime/Swift/Sources/Antlr4/misc/exception/ANTLRError.swift b/runtime/Swift/Sources/Antlr4/misc/exception/ANTLRError.swift index c9df3022e..729e2e939 100644 --- a/runtime/Swift/Sources/Antlr4/misc/exception/ANTLRError.swift +++ b/runtime/Swift/Sources/Antlr4/misc/exception/ANTLRError.swift @@ -19,5 +19,4 @@ public enum ANTLRError: Error { case illegalState(msg:String) case illegalArgument(msg:String) case negativeArraySize(msg:String) - case parseCancellation } diff --git a/runtime/Swift/Sources/Antlr4/misc/exception/ANTLRException.swift b/runtime/Swift/Sources/Antlr4/misc/exception/ANTLRException.swift index c4453384d..739c2fc8f 100644 --- a/runtime/Swift/Sources/Antlr4/misc/exception/ANTLRException.swift +++ b/runtime/Swift/Sources/Antlr4/misc/exception/ANTLRException.swift @@ -15,6 +15,6 @@ import Foundation public enum ANTLRException: Error { - case cannotInvokeStartRule + case parseCancellation(e: RecognitionException) case recognition(e: RecognitionException) }