Skip to content

Avoid conforming String to Error in this module #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Sources/Exercises/Participant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,25 @@ public protocol Participant {
// ...
}

// Errors that may be thrown from default implementations
private enum ParticipantError: Error {
case unsupported
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hamishknight can you take a look?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used for testing AFAIK


// Default impls
extension Participant {
static var unsupported: Error { "Unsupported" }

// Produce a function that will parse a grapheme break entry from a line
public static func graphemeBreakProperty() throws -> (String) -> GraphemeBreakEntry? {
throw 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 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 unsupported
throw ParticipantError.unsupported
}
}
11 changes: 0 additions & 11 deletions Sources/_MatchingEngine/Regex/Parse/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -529,5 +520,3 @@ public func parseWithDelimiters<S: StringProtocol>(
let (contents, delim) = droppingRegexDelimiters(String(regex))
return try parse(contents, delim.defaultSyntaxOptions)
}

extension String: Error {}
23 changes: 16 additions & 7 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down