Skip to content
Closed
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
10 changes: 9 additions & 1 deletion Sources/_RegexParser/Regex/Parse/CompilerInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/_RegexParser/Regex/Parse/Diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/_RegexParser/Regex/Parse/SourceLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ public protocol LocatedErrorProtocol: Error {

extension Source {
/// An error that includes information about the location in source code.
public struct LocatedError<E: Error>: 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<Source.Position>) {
public init(_ v: Error, _ r: Range<Source.Position>) {
self.error = v
self.location = Location(r)
}
Expand Down Expand Up @@ -133,6 +133,6 @@ extension Error {
if self is LocatedErrorProtocol {
return self
}
return Source.LocatedError<Self>(self, loc)
return Source.LocatedError(self, loc)
}
}