From bf7710d49a019497a8a67230ae048935f1c75cc9 Mon Sep 17 00:00:00 2001 From: Harlan Haskins Date: Fri, 25 Mar 2022 21:19:59 -0600 Subject: [PATCH 1/4] Avoid conforming String to Error in this module This seems like an overly-broad conformance to add to swift-experimental-string-processing, and it doesn't look like anyone is calling report(). Do we need this? --- Sources/_MatchingEngine/Regex/Parse/Parse.swift | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Sources/_MatchingEngine/Regex/Parse/Parse.swift b/Sources/_MatchingEngine/Regex/Parse/Parse.swift index e7b687912..296956fdc 100644 --- a/Sources/_MatchingEngine/Regex/Parse/Parse.swift +++ b/Sources/_MatchingEngine/Regex/Parse/Parse.swift @@ -122,15 +122,6 @@ extension ParsingContext { // Diagnostics extension Parser { - mutating func report( - _ str: String, _ function: String = #function, _ line: Int = #line - ) throws -> Never { - throw """ - ERROR: \(str) - (error detected in parser at \(function):\(line)) - """ - } - fileprivate func loc( _ start: Source.Position ) -> SourceLocation { @@ -529,5 +520,3 @@ public func parseWithDelimiters( let (contents, delim) = droppingRegexDelimiters(String(regex)) return try parse(contents, delim.defaultSyntaxOptions) } - -extension String: Error {} From bf8c1a92ad072e4bdfbcffb44a08813d5aeaea5d Mon Sep 17 00:00:00 2001 From: Harlan Haskins Date: Fri, 25 Mar 2022 22:12:29 -0600 Subject: [PATCH 2/4] Update tests as well --- Tests/RegexTests/MatchTests.swift | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Tests/RegexTests/MatchTests.swift b/Tests/RegexTests/MatchTests.swift index dba72820f..1d5b436ff 100644 --- a/Tests/RegexTests/MatchTests.swift +++ b/Tests/RegexTests/MatchTests.swift @@ -13,6 +13,13 @@ import XCTest @testable import _MatchingEngine @testable import _StringProcessing +struct MatchError: Error { + var message: String + init(_ message: String) { + self.message = message + } +} + extension Executor { func _firstMatch( _ regex: String, input: String, @@ -31,7 +38,7 @@ extension Executor { let caps = result.rawCaptures.slices(from: input) return (input[result.range], caps) } else if start == input.endIndex { - throw "match not found for \(regex) in \(input)" + throw MatchError("match not found for \(regex) in \(input)") } else { input.formIndex(after: &start) } @@ -76,27 +83,29 @@ func flatCaptureTest( if expect == nil { continue } else { - throw "Match failed" + throw MatchError("Match failed") } } guard let expect = expect else { - throw "Match of \(test) succeeded where failure expected in \(regex)" + throw MatchError(""" + Match of \(test) succeeded where failure expected in \(regex) + """) } let capStrs = caps.map { $0 == nil ? nil : String($0!) } guard expect.count == capStrs.count else { - throw """ + throw MatchError(""" Capture count mismatch: \(expect) \(capStrs) - """ + """) } guard expect.elementsEqual(capStrs) else { - throw """ + throw MatchError(""" Capture mismatch: \(expect) \(capStrs) - """ + """) } } catch { if !xfail { From 84311bb5644aae432eb3a76d399ea96ef5f9e58b Mon Sep 17 00:00:00 2001 From: Harlan Haskins Date: Fri, 25 Mar 2022 22:59:46 -0600 Subject: [PATCH 3/4] Remove error-conforming string from Participant --- Sources/Exercises/Participant.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/Exercises/Participant.swift b/Sources/Exercises/Participant.swift index 2f7ec183b..0654569b6 100644 --- a/Sources/Exercises/Participant.swift +++ b/Sources/Exercises/Participant.swift @@ -31,20 +31,22 @@ public protocol Participant { // Default impls extension Participant { - static var unsupported: Error { "Unsupported" } + enum Error: Swift.Error { + case unsupported + } // Produce a function that will parse a grapheme break entry from a line public static func graphemeBreakProperty() throws -> (String) -> GraphemeBreakEntry? { - throw unsupported + throw Error.unsupported } // Produce a function that will extract the bodies of C-style comments from its input public static func cComments() throws -> (String) -> [Substring] { - throw unsupported + throw Error.unsupported } // Produce a function that will extract the bodies of Swift-style comments from its input public static func swiftComments() throws -> (String) -> [Substring] { - throw unsupported + throw Error.unsupported } } From b400e3e536083628a532ceeac8da87eb983759d1 Mon Sep 17 00:00:00 2001 From: Harlan Haskins Date: Sat, 26 Mar 2022 10:03:14 -0600 Subject: [PATCH 4/4] Un-nest Error --- Sources/Exercises/Participant.swift | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Sources/Exercises/Participant.swift b/Sources/Exercises/Participant.swift index 0654569b6..ad20d7fbd 100644 --- a/Sources/Exercises/Participant.swift +++ b/Sources/Exercises/Participant.swift @@ -29,24 +29,25 @@ public protocol Participant { // ... } +// Errors that may be thrown from default implementations +private enum ParticipantError: Error { + case unsupported +} + // Default impls extension Participant { - enum Error: Swift.Error { - case unsupported - } - // Produce a function that will parse a grapheme break entry from a line public static func graphemeBreakProperty() throws -> (String) -> GraphemeBreakEntry? { - throw Error.unsupported + throw ParticipantError.unsupported } // Produce a function that will extract the bodies of C-style comments from its input public static func cComments() throws -> (String) -> [Substring] { - throw Error.unsupported + throw ParticipantError.unsupported } // Produce a function that will extract the bodies of Swift-style comments from its input public static func swiftComments() throws -> (String) -> [Substring] { - throw Error.unsupported + throw ParticipantError.unsupported } }