@@ -90,10 +90,7 @@ var standardError = StandardErrorStream()
9090
9191typealias Counter = Int64
9292let regexProtocolName = " RegexProtocol "
93- let concatenationStructTypeBaseName = " Concatenate "
94- let capturingGroupTypeBaseName = " CapturingGroup "
9593let matchAssociatedTypeName = " Match "
96- let captureAssociatedTypeName = " Capture "
9794let patternBuilderTypeName = " RegexBuilder "
9895let patternProtocolRequirementName = " regex "
9996let regexTypeName = " Regex "
@@ -535,108 +532,169 @@ struct VariadicsGenerator: ParsableCommand {
535532 : " ( \( baseMatchTypeName) , \( newCaptureType) , " + ( 0 ..< arity) . map { " C \( $0) " } . joined ( separator: " , " ) + " ) "
536533 }
537534 let whereClause = " where R. \( matchAssociatedTypeName) == \( matchType) "
535+ let rawNewMatchType = newMatchType ( newCaptureType: " W " )
536+ let transformedNewMatchType = newMatchType ( newCaptureType: " NewCapture " )
538537 output ( """
539538 // MARK: - Non-builder capture arity \( arity)
540539
541540 public func capture< \( genericParams) >(
542- _ component: R, as reference: Reference? = nil
543- ) -> \( regexTypeName) < \( newMatchType ( newCaptureType: " W " ) ) > \( whereClause) {
544- .init(node: .group(.capture, component.regex.root, reference?.id))
541+ _ component: R
542+ ) -> \( regexTypeName) < \( rawNewMatchType) > \( whereClause) {
543+ .init(node: .group(.capture, component.regex.root))
544+ }
545+
546+ public func capture< \( genericParams) >(
547+ _ component: R, as reference: Reference<W>
548+ ) -> \( regexTypeName) < \( rawNewMatchType) > \( whereClause) {
549+ .init(node: .group(.capture, component.regex.root, reference.id))
550+ }
551+
552+ public func capture< \( genericParams) , NewCapture>(
553+ _ component: R,
554+ transform: @escaping (Substring) -> NewCapture
555+ ) -> \( regexTypeName) < \( transformedNewMatchType) > \( whereClause) {
556+ .init(node: .groupTransform(
557+ .capture,
558+ component.regex.root,
559+ CaptureTransform(resultType: NewCapture.self) {
560+ transform($0) as Any
561+ }))
545562 }
546563
547564 public func capture< \( genericParams) , NewCapture>(
548565 _ component: R,
549- as reference: Reference? = nil ,
566+ as reference: Reference<NewCapture> ,
550567 transform: @escaping (Substring) -> NewCapture
551- ) -> \( regexTypeName) < \( newMatchType ( newCaptureType : " NewCapture " ) ) > \( whereClause) {
568+ ) -> \( regexTypeName) < \( transformedNewMatchType ) > \( whereClause) {
552569 .init(node: .groupTransform(
553570 .capture,
554571 component.regex.root,
555572 CaptureTransform(resultType: NewCapture.self) {
556573 transform($0) as Any
557574 },
558- reference? .id))
575+ reference.id))
559576 }
560577
561578 public func tryCapture< \( genericParams) , NewCapture>(
562579 _ component: R,
563- as reference: Reference? = nil,
564580 transform: @escaping (Substring) throws -> NewCapture
565- ) -> \( regexTypeName) < \( newMatchType ( newCaptureType: " NewCapture " ) ) > \( whereClause) {
581+ ) -> \( regexTypeName) < \( transformedNewMatchType) > \( whereClause) {
582+ .init(node: .groupTransform(
583+ .capture,
584+ component.regex.root,
585+ CaptureTransform(resultType: NewCapture.self) {
586+ try transform($0) as Any
587+ }))
588+ }
589+
590+ public func tryCapture< \( genericParams) , NewCapture>(
591+ _ component: R,
592+ as reference: Reference<NewCapture>,
593+ transform: @escaping (Substring) throws -> NewCapture
594+ ) -> \( regexTypeName) < \( transformedNewMatchType) > \( whereClause) {
566595 .init(node: .groupTransform(
567596 .capture,
568597 component.regex.root,
569598 CaptureTransform(resultType: NewCapture.self) {
570599 try transform($0) as Any
571600 },
572- reference? .id))
601+ reference.id))
573602 }
574603
575604 public func tryCapture< \( genericParams) , NewCapture>(
576605 _ component: R,
577- as reference: Reference? = nil,
578606 transform: @escaping (Substring) -> NewCapture?
579- ) -> \( regexTypeName) < \( newMatchType ( newCaptureType: " NewCapture " ) ) > \( whereClause) {
607+ ) -> \( regexTypeName) < \( transformedNewMatchType) > \( whereClause) {
608+ .init(node: .groupTransform(
609+ .capture,
610+ component.regex.root,
611+ CaptureTransform(resultType: NewCapture.self) {
612+ transform($0) as Any?
613+ }))
614+ }
615+
616+ public func tryCapture< \( genericParams) , NewCapture>(
617+ _ component: R,
618+ as reference: Reference<NewCapture>,
619+ transform: @escaping (Substring) -> NewCapture?
620+ ) -> \( regexTypeName) < \( transformedNewMatchType) > \( whereClause) {
580621 .init(node: .groupTransform(
581622 .capture,
582623 component.regex.root,
583624 CaptureTransform(resultType: NewCapture.self) {
584625 transform($0) as Any?
585626 },
586- reference? .id))
627+ reference.id))
587628 }
588629
589630 // MARK: - Builder capture arity \( arity)
590631
591632 public func capture< \( genericParams) >(
592- as reference: Reference? = nil,
593633 @RegexBuilder _ component: () -> R
594- ) -> \( regexTypeName) < \( newMatchType ( newCaptureType: " W " ) ) > \( whereClause) {
595- .init(node: .group(.capture, component().regex.root, reference?.id))
634+ ) -> \( regexTypeName) < \( rawNewMatchType) > \( whereClause) {
635+ .init(node: .group(.capture, component().regex.root))
636+ }
637+
638+ public func capture< \( genericParams) >(
639+ as reference: Reference<W>,
640+ @RegexBuilder _ component: () -> R
641+ ) -> \( regexTypeName) < \( rawNewMatchType) > \( whereClause) {
642+ .init(node: .group(.capture, component().regex.root, reference.id))
596643 }
597644
598645 public func capture< \( genericParams) , NewCapture>(
599- as reference: Reference? = nil,
600646 @RegexBuilder _ component: () -> R,
601647 transform: @escaping (Substring) -> NewCapture
602- ) -> \( regexTypeName) < \( newMatchType ( newCaptureType : " NewCapture " ) ) > \( whereClause) {
648+ ) -> \( regexTypeName) < \( transformedNewMatchType ) > \( whereClause) {
603649 .init(node: .groupTransform(
604650 .capture,
605651 component().regex.root,
606652 CaptureTransform(resultType: NewCapture.self) {
607653 transform($0) as Any
608- },
609- reference?.id))
654+ }))
610655 }
611656
612657 public func tryCapture< \( genericParams) , NewCapture>(
613- as reference: Reference? = nil ,
658+ as reference: Reference<NewCapture> ,
614659 @RegexBuilder _ component: () -> R,
615660 transform: @escaping (Substring) throws -> NewCapture
616- ) -> \( regexTypeName) < \( newMatchType ( newCaptureType : " NewCapture " ) ) > \( whereClause) {
661+ ) -> \( regexTypeName) < \( transformedNewMatchType ) > \( whereClause) {
617662 .init(node: .groupTransform(
618663 .capture,
619664 component().regex.root,
620665 CaptureTransform(resultType: NewCapture.self) {
621666 try transform($0) as Any
622667 },
623- reference?.id))
668+ reference.id))
669+ }
670+
671+ public func tryCapture< \( genericParams) , NewCapture>(
672+ @RegexBuilder _ component: () -> R,
673+ transform: @escaping (Substring) -> NewCapture?
674+ ) -> \( regexTypeName) < \( transformedNewMatchType) > \( whereClause) {
675+ .init(node: .groupTransform(
676+ .capture,
677+ component().regex.root,
678+ CaptureTransform(resultType: NewCapture.self) {
679+ transform($0) as Any?
680+ }))
624681 }
625682
626683 public func tryCapture< \( genericParams) , NewCapture>(
627- as reference: Reference? = nil ,
684+ as reference: Reference<NewCapture> ,
628685 @RegexBuilder _ component: () -> R,
629686 transform: @escaping (Substring) -> NewCapture?
630- ) -> \( regexTypeName) < \( newMatchType ( newCaptureType : " NewCapture " ) ) > \( whereClause) {
687+ ) -> \( regexTypeName) < \( transformedNewMatchType ) > \( whereClause) {
631688 .init(node: .groupTransform(
632689 .capture,
633690 component().regex.root,
634691 CaptureTransform(resultType: NewCapture.self) {
635692 transform($0) as Any?
636693 },
637- reference? .id))
694+ reference.id))
638695 }
639696
697+
640698 """ )
641699 }
642700}
0 commit comments