diff --git a/Sources/Exercises/Participants/RegexParticipant.swift b/Sources/Exercises/Participants/RegexParticipant.swift index d62ddc422..583a86d92 100644 --- a/Sources/Exercises/Participants/RegexParticipant.swift +++ b/Sources/Exercises/Participants/RegexParticipant.swift @@ -80,15 +80,15 @@ private func graphemeBreakPropertyData( forLine line: String ) -> GraphemeBreakEntry? { line.match { - oneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:)) + tryCapture(oneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) } optionally { ".." - oneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:)) + tryCapture(oneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) } } oneOrMore(.whitespace) ";" oneOrMore(.whitespace) - oneOrMore(.word).tryCapture(Unicode.GraphemeBreakProperty.init) + tryCapture(oneOrMore(.word)) { Unicode.GraphemeBreakProperty($0) } many(.any) }.map { let (_, lower, upper, property) = $0.match diff --git a/Sources/VariadicsGenerator/VariadicsGenerator.swift b/Sources/VariadicsGenerator/VariadicsGenerator.swift index b0907101e..30fc11598 100644 --- a/Sources/VariadicsGenerator/VariadicsGenerator.swift +++ b/Sources/VariadicsGenerator/VariadicsGenerator.swift @@ -174,6 +174,12 @@ struct VariadicsGenerator: ParsableCommand { emitUnaryAlternationBuildBlock(arity: arity) } + print("Generating 'capture' and 'tryCapture' overloads...", to: &standardError) + for arity in 0.. String { + let newCaptureType = transformed ? "NewCapture" : baseMatchTypeName + return arity == 0 + ? "(W, \(newCaptureType))" + : "(W, \(newCaptureType), " + (0..(_ component: R) -> \(regexTypeName)<\(newMatchType(transformed: false))> \(whereClause) { + .init(node: .group(.capture, component.regex.root)) + } + + public func capture<\(genericParams), NewCapture>( + _ component: R, transform: @escaping (Substring) -> NewCapture + ) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) + } + + public func tryCapture<\(genericParams), NewCapture>( + _ component: R, transform: @escaping (Substring) throws -> NewCapture + ) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) + } + + public func tryCapture<\(genericParams), NewCapture>( + _ component: R, transform: @escaping (Substring) -> NewCapture? + ) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) + } + + // MARK: - Builder capture arity \(arity) + + public func capture<\(genericParams)>( + @RegexBuilder _ component: () -> R + ) -> \(regexTypeName)<\(newMatchType(transformed: false))> \(whereClause) { + .init(node: .group(.capture, component().regex.root)) + } + + public func capture<\(genericParams), NewCapture>( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture + ) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) + } + + public func tryCapture<\(genericParams), NewCapture>( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture + ) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) + } + + public func tryCapture<\(genericParams), NewCapture>( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? + ) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) + } + + """) + } } diff --git a/Sources/_StringProcessing/RegexDSL/DSL.swift b/Sources/_StringProcessing/RegexDSL/DSL.swift index f6eb91fdb..33869b56a 100644 --- a/Sources/_StringProcessing/RegexDSL/DSL.swift +++ b/Sources/_StringProcessing/RegexDSL/DSL.swift @@ -150,59 +150,3 @@ public func oneOf( ) -> R { builder() } - -// MARK: - Capture - -public struct CapturingGroup: RegexProtocol { - public let regex: Regex - - init( - _ component: Component - ) { - self.regex = .init(node: .group( - .capture, component.regex.root)) - } - - init( - _ component: Component, - transform: CaptureTransform - ) { - self.regex = .init(node: .groupTransform( - .capture, - component.regex.root, - transform)) - } - - init( - _ component: Component, - transform: @escaping (Substring) -> NewCapture - ) { - self.init( - component, - transform: CaptureTransform(resultType: NewCapture.self) { - transform($0) as Any - }) - } - - init( - _ component: Component, - transform: @escaping (Substring) throws -> NewCapture - ) { - self.init( - component, - transform: CaptureTransform(resultType: NewCapture.self) { - try transform($0) as Any - }) - } - - init( - _ component: Component, - transform: @escaping (Substring) -> NewCapture? - ) { - self.init( - component, - transform: CaptureTransform(resultType: NewCapture.self) { - transform($0) as Any? - }) - } -} diff --git a/Sources/_StringProcessing/RegexDSL/DSLCapture.swift b/Sources/_StringProcessing/RegexDSL/DSLCapture.swift deleted file mode 100644 index 05d974239..000000000 --- a/Sources/_StringProcessing/RegexDSL/DSLCapture.swift +++ /dev/null @@ -1,120 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors -// Licensed under Apache License v2.0 with Runtime Library Exception -// -// See https://swift.org/LICENSE.txt for license information -// -//===----------------------------------------------------------------------===// - -extension RegexProtocol { - @_disfavoredOverload - public func capture() -> CapturingGroup<(Substring, Substring)> { - .init(self) - } - - @_disfavoredOverload - public func capture( - _ transform: @escaping (Substring) -> NewCapture - ) -> CapturingGroup<(Substring, NewCapture)> { - .init(self, transform: transform) - } - - @_disfavoredOverload - public func tryCapture( - _ transform: @escaping (Substring) throws -> NewCapture - ) -> CapturingGroup<(Substring, NewCapture)> { - .init(self, transform: transform) - } - - @_disfavoredOverload - public func tryCapture( - _ transform: @escaping (Substring) -> NewCapture? - ) -> CapturingGroup<(Substring, NewCapture)> { - .init(self, transform: transform) - } - - public func capture() -> CapturingGroup<(Substring, Substring, C0)> - where Match == (W, C0) { - .init(self) - } - - public func capture( - _ transform: @escaping (Substring) -> NewCapture - ) -> CapturingGroup<(Substring, NewCapture, C0)> where Match == (W, C0) { - .init(self, transform: transform) - } - - public func tryCapture( - _ transform: @escaping (Substring) throws -> NewCapture - ) -> CapturingGroup<(Substring, NewCapture, C0)> where Match == (W, C0) { - .init(self, transform: transform) - } - - public func tryCapture( - _ transform: @escaping (Substring) -> NewCapture? - ) -> CapturingGroup<(Substring, NewCapture, C0)> where Match == (W, C0) { - .init(self, transform: transform) - } - - public func capture() -> CapturingGroup<(Substring, Substring, C0, C1)> where Match == (W, C0, C1) { - .init(self) - } - - public func capture( - _ transform: @escaping (Substring) -> NewCapture - ) -> CapturingGroup<(Substring, NewCapture, C0, C1)> where Match == (W, C0, C1) { - .init(self, transform: transform) - } - - public func tryCapture( - _ transform: @escaping (Substring) throws -> NewCapture - ) -> CapturingGroup<(Substring, NewCapture, C0, C1)> where Match == (W, C0, C1) { - .init(self, transform: transform) - } - - public func tryCapture( - _ transform: @escaping (Substring) -> NewCapture? - ) -> CapturingGroup<(Substring, NewCapture, C0, C1)> where Match == (W, C0, C1) { - .init(self, transform: transform) - } - - public func capture() -> CapturingGroup<(Substring, Substring, C0, C1, C2)> - where Match == (W, C0, C1, C2) { - .init(self) - } - - public func capture( - _ transform: @escaping (Substring) -> NewCapture - ) -> CapturingGroup<(Substring, NewCapture, C0, C1, C2)> where Match == (W, C0, C1, C2) { - .init(self, transform: transform) - } - - public func tryCapture( - _ transform: @escaping (Substring) throws -> NewCapture - ) -> CapturingGroup<(Substring, NewCapture, C0, C1, C2)> where Match == (W, C0, C1, C2) { - .init(self, transform: transform) - } - - public func tryCapture( - _ transform: @escaping (Substring) -> NewCapture? - ) -> CapturingGroup<(Substring, NewCapture, C0, C1, C2)> where Match == (W, C0, C1, C2) { - .init(self, transform: transform) - } -} - -/* Or using parameterized extensions and variadic generics. -extension RegexProtocol where Match == (T...) { - public func capture() -> CapturingGroup<(Substring, T...)> { - .init(self) - } - - public func capture( - _ transform: @escaping (Substring) -> NewCapture - ) -> CapturingGroup<(NewCapture, T...)> { - .init(self, transform: transform) - } -} -*/ diff --git a/Sources/_StringProcessing/RegexDSL/Variadics.swift b/Sources/_StringProcessing/RegexDSL/Variadics.swift index 6289bb6f8..d73b9f043 100644 --- a/Sources/_StringProcessing/RegexDSL/Variadics.swift +++ b/Sources/_StringProcessing/RegexDSL/Variadics.swift @@ -84,7 +84,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -92,7 +92,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -100,7 +100,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -108,7 +108,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -116,7 +116,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -124,7 +124,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -132,7 +132,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -140,7 +140,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -148,7 +148,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -156,7 +156,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -164,7 +164,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -172,7 +172,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -180,7 +180,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -188,7 +188,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -196,7 +196,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -204,7 +204,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -212,7 +212,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -220,7 +220,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -228,7 +228,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -236,7 +236,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -244,7 +244,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -252,7 +252,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -260,7 +260,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -268,7 +268,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -276,7 +276,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -284,7 +284,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -292,7 +292,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -300,7 +300,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -308,7 +308,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -316,7 +316,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -324,7 +324,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -332,7 +332,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -340,7 +340,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -348,7 +348,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -356,7 +356,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -364,7 +364,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -372,7 +372,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -380,7 +380,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -388,7 +388,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -396,7 +396,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -404,7 +404,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -412,7 +412,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -420,7 +420,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -428,7 +428,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -436,7 +436,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -444,7 +444,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -452,7 +452,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -460,7 +460,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -468,7 +468,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -476,7 +476,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -484,7 +484,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -492,7 +492,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -500,7 +500,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -508,7 +508,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -516,7 +516,7 @@ extension RegexBuilder { .init(node: combined.regex.root.appending(next.regex.root)) } } - extension RegexBuilder { +extension RegexBuilder { @_disfavoredOverload public static func buildBlock( combining next: R1, into combined: R0 @@ -2078,6 +2078,826 @@ extension AlternationBuilder { .init(node: .alternation([regex.regex.root])) } } +// MARK: - Non-builder capture arity 0 + +public func capture(_ component: R) -> Regex<(W, Substring)> where R.Match == W { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture)> where R.Match == W { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture)> where R.Match == W { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture)> where R.Match == W { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 0 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring)> where R.Match == W { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture)> where R.Match == W { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture)> where R.Match == W { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture)> where R.Match == W { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} +// MARK: - Non-builder capture arity 1 + +public func capture(_ component: R) -> Regex<(W, Substring, C0)> where R.Match == (W, C0) { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0)> where R.Match == (W, C0) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0)> where R.Match == (W, C0) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0)> where R.Match == (W, C0) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 1 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring, C0)> where R.Match == (W, C0) { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0)> where R.Match == (W, C0) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0)> where R.Match == (W, C0) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0)> where R.Match == (W, C0) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} +// MARK: - Non-builder capture arity 2 + +public func capture(_ component: R) -> Regex<(W, Substring, C0, C1)> where R.Match == (W, C0, C1) { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1)> where R.Match == (W, C0, C1) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1)> where R.Match == (W, C0, C1) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1)> where R.Match == (W, C0, C1) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 2 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring, C0, C1)> where R.Match == (W, C0, C1) { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1)> where R.Match == (W, C0, C1) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1)> where R.Match == (W, C0, C1) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1)> where R.Match == (W, C0, C1) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} +// MARK: - Non-builder capture arity 3 + +public func capture(_ component: R) -> Regex<(W, Substring, C0, C1, C2)> where R.Match == (W, C0, C1, C2) { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2)> where R.Match == (W, C0, C1, C2) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2)> where R.Match == (W, C0, C1, C2) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2)> where R.Match == (W, C0, C1, C2) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 3 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring, C0, C1, C2)> where R.Match == (W, C0, C1, C2) { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2)> where R.Match == (W, C0, C1, C2) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2)> where R.Match == (W, C0, C1, C2) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2)> where R.Match == (W, C0, C1, C2) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} +// MARK: - Non-builder capture arity 4 + +public func capture(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 4 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} +// MARK: - Non-builder capture arity 5 + +public func capture(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 5 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} +// MARK: - Non-builder capture arity 6 + +public func capture(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 6 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} +// MARK: - Non-builder capture arity 7 + +public func capture(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 7 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} +// MARK: - Non-builder capture arity 8 + +public func capture(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 8 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} +// MARK: - Non-builder capture arity 9 + +public func capture(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { + .init(node: .group(.capture, component.regex.root)) +} + +public func capture( + _ component: R, transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + _ component: R, transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { + .init(node: .groupTransform( + .capture, + component.regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} + +// MARK: - Builder capture arity 9 + +public func capture( + @RegexBuilder _ component: () -> R +) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { + .init(node: .group(.capture, component().regex.root)) +} + +public func capture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) throws -> NewCapture +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + try transform($0) as Any + })) +} + +public func tryCapture( + @RegexBuilder _ component: () -> R, + transform: @escaping (Substring) -> NewCapture? +) -> Regex<(W, NewCapture, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { + .init(node: .groupTransform( + .capture, + component().regex.root, + CaptureTransform(resultType: NewCapture.self) { + transform($0) as Any? + })) +} // END AUTO-GENERATED CONTENT diff --git a/Tests/RegexTests/AlgorithmsTests.swift b/Tests/RegexTests/AlgorithmsTests.swift index 8041214b0..1454daaf5 100644 --- a/Tests/RegexTests/AlgorithmsTests.swift +++ b/Tests/RegexTests/AlgorithmsTests.swift @@ -116,7 +116,7 @@ class RegexConsumerTests: XCTestCase { } func testMatches() { - let regex = Regex(oneOrMore(.digit).capture { 2 * Int($0)! }) + let regex = capture(oneOrMore(.digit)) { 2 * Int($0)! } let str = "foo 160 bar 99 baz" XCTAssertEqual(str.matches(of: regex).map(\.result.1), [320, 198]) } @@ -133,7 +133,7 @@ class RegexConsumerTests: XCTestCase { XCTAssertEqual(input.replacing(regex, with: replace), result) } - let int = oneOrMore(.digit).capture { Int($0)! } + let int = capture(oneOrMore(.digit)) { Int($0)! } replaceTest( int, diff --git a/Tests/RegexTests/RegexDSLTests.swift b/Tests/RegexTests/RegexDSLTests.swift index 4fa8f7030..3dd660187 100644 --- a/Tests/RegexTests/RegexDSLTests.swift +++ b/Tests/RegexTests/RegexDSLTests.swift @@ -16,8 +16,8 @@ class RegexDSLTests: XCTestCase { func testSimpleStrings() throws { let regex = Regex { "a" - Character("b").capture() // Character - "1".tryCapture { Int($0) } // Int + capture(Character("b")) // Character + tryCapture("1") { Int($0) } // Int } // Assert the inferred capture type. let _: (Substring, Substring, Int).Type = type(of: regex).Match.self @@ -33,8 +33,8 @@ class RegexDSLTests: XCTestCase { func testCharacterClasses() throws { let regex = Regex { CharacterClass.any - CharacterClass.whitespace.capture() // Character - "c".capture() // Substring + capture(CharacterClass.whitespace) // Character + capture("c") // Substring } // Assert the inferred capture type. let _: (Substring, Substring, Substring).Type = type(of: regex).Match.self @@ -65,10 +65,12 @@ class RegexDSLTests: XCTestCase { do { let regex = Regex { "ab" - oneOf { - "c" - "def" - }.capture().+ + capture { + oneOf { + "c" + "def" + } + }.+ } XCTAssertTrue( try XCTUnwrap("abc".match(regex)?.match) == ("abc", ["c"])) @@ -86,7 +88,7 @@ class RegexDSLTests: XCTestCase { } do { let regex = oneOf { - "aaa".capture() + capture("aaa") } XCTAssertTrue( try XCTUnwrap("aaa".match(regex)?.match) == ("aaa", "aaa")) @@ -94,9 +96,9 @@ class RegexDSLTests: XCTestCase { } do { let regex = oneOf { - "aaa".capture() - "bbb".capture() - "ccc".capture() + capture("aaa") + capture("bbb") + capture("ccc") } XCTAssertTrue( try XCTUnwrap("aaa".match(regex)?.match) == ("aaa", "aaa", nil, nil)) @@ -111,12 +113,12 @@ class RegexDSLTests: XCTestCase { func testCombinators() throws { let regex = Regex { "a".+ - oneOrMore(Character("b")).capture() // Substring - many("c").capture() // Substring - CharacterClass.hexDigit.capture().* // [Substring] + capture(oneOrMore(Character("b"))) // Substring + capture(many("c")) // Substring + capture(CharacterClass.hexDigit).* // [Substring] "e".? - ("t" | "k").capture() // Substring - oneOf { "k".capture(); "j".capture() } // (Substring?, Substring?) + capture("t" | "k") // Substring + oneOf { capture("k"); capture("j") } // (Substring?, Substring?) } // Assert the inferred capture type. let _: (Substring, Substring, Substring, [Substring], Substring, Substring?, Substring?).Type @@ -136,9 +138,9 @@ class RegexDSLTests: XCTestCase { let regex = Regex { "a".+ oneOrMore { - oneOrMore("b").capture() - many("c").capture() - "d".capture().* + capture(oneOrMore("b")) + capture(many("c")) + capture("d").* "e".? } } @@ -188,11 +190,11 @@ class RegexDSLTests: XCTestCase { "a".+ oneOrMore(.whitespace) optionally { - oneOrMore(.digit).capture { Int($0)! } + capture(oneOrMore(.digit)) { Int($0)! } } many { oneOrMore(.whitespace) - oneOrMore(.word).capture { Word($0)! } + capture(oneOrMore(.word)) { Word($0)! } } } // Assert the inferred capture type. @@ -218,40 +220,44 @@ class RegexDSLTests: XCTestCase { func testNestedCaptureTypes() throws { let regex1 = Regex { "a".+ - Regex { - oneOrMore("b").capture() + capture { + capture(oneOrMore("b")) "e".? - }.capture() + } } let _: (Substring, Substring, Substring).Type = type(of: regex1).Match.self let regex2 = Regex { "a".+ - Regex { - "b".tryCapture { Int($0) }.* + capture { + tryCapture("b") { Int($0) }.* "e".? - }.capture() + } } let _: (Substring, Substring, [Int]).Type = type(of: regex2).Match.self let regex3 = Regex { "a".+ - Regex { - "b".tryCapture { Int($0) } - "c".tryCapture { Double($0) }.* + capture { + tryCapture("b") { Int($0) } + many { + tryCapture("c") { Double($0) } + } "e".? - }.capture() + } } let _: (Substring, Substring, Int, [Double]).Type = type(of: regex3).Match.self let regex4 = Regex { "a".+ - oneOrMore { - oneOrMore("b").capture() - many("c").capture() - "d".capture().* - "e".? - }.capture() + capture { + oneOrMore { + capture(oneOrMore("b")) + capture(many("c")) + capture("d").* + "e".? + } + } } let _: ( Substring, Substring, [(Substring, Substring, [Substring])]).Type @@ -282,9 +288,11 @@ class RegexDSLTests: XCTestCase { ";" spaces - oneOrMore { - CharacterClass.word - }.capture() + capture { + oneOrMore { + CharacterClass.word + } + } many { CharacterClass.any @@ -307,15 +315,19 @@ class RegexDSLTests: XCTestCase { """ let regexWithCapture = Regex { - oneOrMore(CharacterClass.hexDigit).capture(Unicode.Scalar.init(hex:)) + capture { + oneOrMore(CharacterClass.hexDigit) + } transform: { Unicode.Scalar(hex: $0) } optionally { ".." - oneOrMore(CharacterClass.hexDigit).capture(Unicode.Scalar.init(hex:)) + capture { + oneOrMore(CharacterClass.hexDigit) + } transform: { Unicode.Scalar(hex: $0) } } oneOrMore(CharacterClass.whitespace) ";" oneOrMore(CharacterClass.whitespace) - oneOrMore(CharacterClass.word).capture() + capture(oneOrMore(CharacterClass.word)) many(CharacterClass.any) } // Regex<(Substring, Unicode.Scalar?, Unicode.Scalar??, Substring)> do { @@ -334,15 +346,23 @@ class RegexDSLTests: XCTestCase { } let regexWithTryCapture = Regex { - oneOrMore(CharacterClass.hexDigit).tryCapture(Unicode.Scalar.init(hex:)) + tryCapture { + oneOrMore(CharacterClass.hexDigit) + } transform: { + Unicode.Scalar(hex: $0) + } optionally { ".." - oneOrMore(CharacterClass.hexDigit).tryCapture(Unicode.Scalar.init(hex:)) + tryCapture { + oneOrMore(CharacterClass.hexDigit) + } transform: { + Unicode.Scalar(hex: $0) + } } oneOrMore(CharacterClass.whitespace) ";" oneOrMore(CharacterClass.whitespace) - oneOrMore(CharacterClass.word).capture() + capture(oneOrMore(CharacterClass.word)) many(CharacterClass.any) } // Regex<(Substring, Unicode.Scalar, Unicode.Scalar?, Substring)> do {