diff --git a/Sources/_RegexParser/Regex/Parse/CompilerInterface.swift b/Sources/_RegexParser/Regex/Parse/CompilerInterface.swift index 0856361d8..f1916993d 100644 --- a/Sources/_RegexParser/Regex/Parse/CompilerInterface.swift +++ b/Sources/_RegexParser/Regex/Parse/CompilerInterface.swift @@ -107,8 +107,16 @@ public func swiftCompilerParseRegexLiteral( // however that it will need plumbing through on the compiler side. return (regexToEmit: input, version: currentRegexLiteralFormatVersion) } catch { + let message: String + if + let located = error as? Source.LocatedError, + let errorDiag = located.error as? Diagnostics.ErrorDiagnostic { + message = errorDiag.description + } else { + message = String(describing: error) + } throw CompilerParseError( - message: "cannot parse regular expression: \(String(describing: error))", + message: "cannot parse regular expression: \(message)", location: (error as? LocatedErrorProtocol)?.location.start ) } diff --git a/Sources/_RegexParser/Regex/Parse/Diagnostics.swift b/Sources/_RegexParser/Regex/Parse/Diagnostics.swift index f5c0d7075..010613d55 100644 --- a/Sources/_RegexParser/Regex/Parse/Diagnostics.swift +++ b/Sources/_RegexParser/Regex/Parse/Diagnostics.swift @@ -337,13 +337,14 @@ public struct Diagnostics: Hashable { diags.contains(where: { $0.behavior == .fatalError }) } + struct ErrorDiagnostic: Error, CustomStringConvertible { + var diag: Diagnostic + var description: String { diag.message } + } + /// If any error diagnostic has been added, throw it as an Error. func throwAnyError() throws { for diag in diags where diag.isAnyError { - struct ErrorDiagnostic: Error, CustomStringConvertible { - var diag: Diagnostic - var description: String { diag.message } - } throw Source.LocatedError(ErrorDiagnostic(diag: diag), diag.location) } } diff --git a/Sources/_RegexParser/Regex/Parse/SourceLocation.swift b/Sources/_RegexParser/Regex/Parse/SourceLocation.swift index 6f6928d2f..d97c0a257 100644 --- a/Sources/_RegexParser/Regex/Parse/SourceLocation.swift +++ b/Sources/_RegexParser/Regex/Parse/SourceLocation.swift @@ -63,15 +63,15 @@ public protocol LocatedErrorProtocol: Error { extension Source { /// An error that includes information about the location in source code. - public struct LocatedError: Error, LocatedErrorProtocol { - public let error: E + public struct LocatedError: Error, LocatedErrorProtocol { + public let error: Error public let location: SourceLocation - init(_ e: E, _ r: SourceLocation) { + init(_ e: Error, _ r: SourceLocation) { self.error = e self.location = r } - public init(_ v: E, _ r: Range) { + public init(_ v: Error, _ r: Range) { self.error = v self.location = Location(r) } @@ -133,6 +133,6 @@ extension Error { if self is LocatedErrorProtocol { return self } - return Source.LocatedError(self, loc) + return Source.LocatedError(self, loc) } }