Skip to content

Throw error when Output is wrong type #541

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 1 commit into from
Jul 5, 2022
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
4 changes: 4 additions & 0 deletions Sources/_StringProcessing/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ enum RegexCompilationError: Error, CustomStringConvertible {
// TODO: Source location?
case uncapturedReference

case incorrectOutputType(incorrect: Any.Type, correct: Any.Type)

var description: String {
switch self {
case .uncapturedReference:
return "Found a reference used before it captured any match."
case .incorrectOutputType(let incorrect, let correct):
return "Cast to incorrect type 'Regex<\(incorrect)>', expected 'Regex<\(correct)>'"
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions Sources/_StringProcessing/Regex/AnyRegexOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,18 @@ extension Regex {
_ pattern: String,
as: Output.Type = Output.self
) throws {
self.init(ast: try parse(pattern, .traditional))
let regex = Regex(ast: try parse(pattern, .traditional))

let (isSuccess, correctType) = regex._verifyType()

guard isSuccess else {
throw RegexCompilationError.incorrectOutputType(
incorrect: Output.self,
correct: correctType
)
}

self = regex
}

/// Produces a regex that matches `verbatim` exactly, as though every
Expand Down Expand Up @@ -217,7 +228,8 @@ extension Regex {
as: Output.Type = Output.self
) {
self.init(node: erased.root)
guard self._verifyType() else {

guard _verifyType().0 else {
return nil
}
}
Expand Down
11 changes: 6 additions & 5 deletions Sources/_StringProcessing/Utility/TypeVerification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@

@available(SwiftStdlib 5.7, *)
extension Regex {
internal func _verifyType() -> Bool {
internal func _verifyType() -> (Bool, Any.Type) {
guard Output.self != AnyRegexOutput.self else {
return true
return (true, Output.self)
}

var tupleElements: [Any.Type] = []
var labels = ""

for capture in program.tree.captureList.captures {
var captureType: Any.Type = capture.type ?? Substring.self
var captureType = capture.type
var i = capture.optionalDepth

while i != 0 {
Expand All @@ -41,7 +41,8 @@ extension Regex {

// If we have no captures, then our Regex must be Regex<Substring>.
if tupleElements.count == 1 {
return Output.self == program.tree.root.wholeMatchType
let wholeMatchType = program.tree.root.wholeMatchType
return (Output.self == wholeMatchType, wholeMatchType)
}

let createdType = TypeConstruction.tupleType(
Expand All @@ -52,6 +53,6 @@ extension Regex {
labels: labels.all { $0 == " " } ? nil : labels
)

return Output.self == createdType
return (Output.self == createdType, createdType)
}
}
11 changes: 11 additions & 0 deletions Tests/RegexBuilderTests/AnyRegexOutputTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ extension RegexDSLTests {
noteOutput
)

// Run-time strings (errors)
XCTAssertThrowsError(try Regex("abc", as: (Substring, Substring).self))
XCTAssertThrowsError(try Regex("(abc)", as: Substring.self))
XCTAssertThrowsError(try Regex("(?<test>abc)", as: (Substring, Substring).self))
XCTAssertThrowsError(try Regex("(?<test>abc)?", as: (Substring, test: Substring).self))

XCTAssertNoThrow(try Regex("abc", as: Substring.self))
XCTAssertNoThrow(try Regex("(abc)", as: (Substring, Substring).self))
XCTAssertNoThrow(try Regex("(?<test>abc)", as: (Substring, test: Substring).self))
XCTAssertNoThrow(try Regex("(?<test>abc)?", as: (Substring, test: Substring?).self))

// Builders
check(
Regex {
Expand Down