@@ -174,6 +174,12 @@ struct VariadicsGenerator: ParsableCommand {
174
174
emitUnaryAlternationBuildBlock ( arity: arity)
175
175
}
176
176
177
+ print ( " Generating 'capture' and 'tryCapture' overloads... " , to: & standardError)
178
+ for arity in 0 ..< maxArity {
179
+ print ( " Capture arity: \( arity) " , to: & standardError)
180
+ emitCapture ( arity: arity)
181
+ }
182
+
177
183
output ( " \n \n " )
178
184
179
185
output ( " // END AUTO-GENERATED CONTENT \n " )
@@ -471,4 +477,105 @@ struct VariadicsGenerator: ParsableCommand {
471
477
472
478
""" )
473
479
}
480
+
481
+ func emitCapture( arity: Int ) {
482
+ let genericParams = arity == 0
483
+ ? " R: \( regexProtocolName) , W "
484
+ : " R: \( regexProtocolName) , W, " + ( 0 ..< arity) . map { " C \( $0) " } . joined ( separator: " , " )
485
+ let matchType = arity == 0
486
+ ? " W "
487
+ : " (W, " + ( 0 ..< arity) . map { " C \( $0) " } . joined ( separator: " , " ) + " ) "
488
+ func newMatchType( transformed: Bool ) -> String {
489
+ let newCaptureType = transformed ? " NewCapture " : baseMatchTypeName
490
+ return arity == 0
491
+ ? " (W, \( newCaptureType) ) "
492
+ : " (W, \( newCaptureType) , " + ( 0 ..< arity) . map { " C \( $0) " } . joined ( separator: " , " ) + " ) "
493
+ }
494
+ let whereClause = " where R. \( matchAssociatedTypeName) == \( matchType) "
495
+ output ( """
496
+ // MARK: - Non-builder capture arity \( arity)
497
+
498
+ public func capture< \( genericParams) >(_ component: R) -> \( regexTypeName) < \( newMatchType ( transformed: false ) ) > \( whereClause) {
499
+ .init(node: .group(.capture, component.regex.root))
500
+ }
501
+
502
+ public func capture< \( genericParams) , NewCapture>(
503
+ _ component: R, transform: @escaping (Substring) -> NewCapture
504
+ ) -> \( regexTypeName) < \( newMatchType ( transformed: true ) ) > \( whereClause) {
505
+ .init(node: .groupTransform(
506
+ .capture,
507
+ component.regex.root,
508
+ CaptureTransform(resultType: NewCapture.self) {
509
+ transform($0) as Any
510
+ }))
511
+ }
512
+
513
+ public func tryCapture< \( genericParams) , NewCapture>(
514
+ _ component: R, transform: @escaping (Substring) throws -> NewCapture
515
+ ) -> \( regexTypeName) < \( newMatchType ( transformed: true ) ) > \( whereClause) {
516
+ .init(node: .groupTransform(
517
+ .capture,
518
+ component.regex.root,
519
+ CaptureTransform(resultType: NewCapture.self) {
520
+ try transform($0) as Any
521
+ }))
522
+ }
523
+
524
+ public func tryCapture< \( genericParams) , NewCapture>(
525
+ _ component: R, transform: @escaping (Substring) -> NewCapture?
526
+ ) -> \( regexTypeName) < \( newMatchType ( transformed: true ) ) > \( whereClause) {
527
+ .init(node: .groupTransform(
528
+ .capture,
529
+ component.regex.root,
530
+ CaptureTransform(resultType: NewCapture.self) {
531
+ transform($0) as Any?
532
+ }))
533
+ }
534
+
535
+ // MARK: - Builder capture arity \( arity)
536
+
537
+ public func capture< \( genericParams) >(
538
+ @RegexBuilder _ component: () -> R
539
+ ) -> \( regexTypeName) < \( newMatchType ( transformed: false ) ) > \( whereClause) {
540
+ .init(node: .group(.capture, component().regex.root))
541
+ }
542
+
543
+ public func capture< \( genericParams) , NewCapture>(
544
+ @RegexBuilder _ component: () -> R,
545
+ transform: @escaping (Substring) -> NewCapture
546
+ ) -> \( regexTypeName) < \( newMatchType ( transformed: true ) ) > \( whereClause) {
547
+ .init(node: .groupTransform(
548
+ .capture,
549
+ component().regex.root,
550
+ CaptureTransform(resultType: NewCapture.self) {
551
+ transform($0) as Any
552
+ }))
553
+ }
554
+
555
+ public func tryCapture< \( genericParams) , NewCapture>(
556
+ @RegexBuilder _ component: () -> R,
557
+ transform: @escaping (Substring) throws -> NewCapture
558
+ ) -> \( regexTypeName) < \( newMatchType ( transformed: true ) ) > \( whereClause) {
559
+ .init(node: .groupTransform(
560
+ .capture,
561
+ component().regex.root,
562
+ CaptureTransform(resultType: NewCapture.self) {
563
+ try transform($0) as Any
564
+ }))
565
+ }
566
+
567
+ public func tryCapture< \( genericParams) , NewCapture>(
568
+ @RegexBuilder _ component: () -> R,
569
+ transform: @escaping (Substring) -> NewCapture?
570
+ ) -> \( regexTypeName) < \( newMatchType ( transformed: true ) ) > \( whereClause) {
571
+ .init(node: .groupTransform(
572
+ .capture,
573
+ component().regex.root,
574
+ CaptureTransform(resultType: NewCapture.self) {
575
+ transform($0) as Any?
576
+ }))
577
+ }
578
+
579
+ """ )
580
+ }
474
581
}
0 commit comments