Skip to content

Commit f204c97

Browse files
committed
Make capture(_:) produce component's whole match as capture type.
1 parent 52da66c commit f204c97

File tree

2 files changed

+91
-92
lines changed

2 files changed

+91
-92
lines changed

Sources/VariadicsGenerator/VariadicsGenerator.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -529,23 +529,22 @@ struct VariadicsGenerator: ParsableCommand {
529529
let matchType = arity == 0
530530
? "W"
531531
: "(W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ") + ")"
532-
func newMatchType(transformed: Bool) -> String {
533-
let newCaptureType = transformed ? "NewCapture" : baseMatchTypeName
532+
func newMatchType(newCaptureType: String) -> String {
534533
return arity == 0
535-
? "(W, \(newCaptureType))"
536-
: "(W, \(newCaptureType), " + (0..<arity).map { "C\($0)" }.joined(separator: ", ") + ")"
534+
? "(\(baseMatchTypeName), \(newCaptureType))"
535+
: "(\(baseMatchTypeName), \(newCaptureType), " + (0..<arity).map { "C\($0)" }.joined(separator: ", ") + ")"
537536
}
538537
let whereClause = "where R.\(matchAssociatedTypeName) == \(matchType)"
539538
output("""
540539
// MARK: - Non-builder capture arity \(arity)
541540
542-
public func capture<\(genericParams)>(_ component: R) -> \(regexTypeName)<\(newMatchType(transformed: false))> \(whereClause) {
541+
public func capture<\(genericParams)>(_ component: R) -> \(regexTypeName)<\(newMatchType(newCaptureType: "W"))> \(whereClause) {
543542
.init(node: .group(.capture, component.regex.root))
544543
}
545544
546545
public func capture<\(genericParams), NewCapture>(
547546
_ component: R, transform: @escaping (Substring) -> NewCapture
548-
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
547+
) -> \(regexTypeName)<\(newMatchType(newCaptureType: "NewCapture"))> \(whereClause) {
549548
.init(node: .groupTransform(
550549
.capture,
551550
component.regex.root,
@@ -556,7 +555,7 @@ struct VariadicsGenerator: ParsableCommand {
556555
557556
public func tryCapture<\(genericParams), NewCapture>(
558557
_ component: R, transform: @escaping (Substring) throws -> NewCapture
559-
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
558+
) -> \(regexTypeName)<\(newMatchType(newCaptureType: "NewCapture"))> \(whereClause) {
560559
.init(node: .groupTransform(
561560
.capture,
562561
component.regex.root,
@@ -567,7 +566,7 @@ struct VariadicsGenerator: ParsableCommand {
567566
568567
public func tryCapture<\(genericParams), NewCapture>(
569568
_ component: R, transform: @escaping (Substring) -> NewCapture?
570-
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
569+
) -> \(regexTypeName)<\(newMatchType(newCaptureType: "NewCapture"))> \(whereClause) {
571570
.init(node: .groupTransform(
572571
.capture,
573572
component.regex.root,
@@ -580,14 +579,14 @@ struct VariadicsGenerator: ParsableCommand {
580579
581580
public func capture<\(genericParams)>(
582581
@RegexBuilder _ component: () -> R
583-
) -> \(regexTypeName)<\(newMatchType(transformed: false))> \(whereClause) {
582+
) -> \(regexTypeName)<\(newMatchType(newCaptureType: "W"))> \(whereClause) {
584583
.init(node: .group(.capture, component().regex.root))
585584
}
586585
587586
public func capture<\(genericParams), NewCapture>(
588587
@RegexBuilder _ component: () -> R,
589588
transform: @escaping (Substring) -> NewCapture
590-
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
589+
) -> \(regexTypeName)<\(newMatchType(newCaptureType: "NewCapture"))> \(whereClause) {
591590
.init(node: .groupTransform(
592591
.capture,
593592
component().regex.root,
@@ -599,7 +598,7 @@ struct VariadicsGenerator: ParsableCommand {
599598
public func tryCapture<\(genericParams), NewCapture>(
600599
@RegexBuilder _ component: () -> R,
601600
transform: @escaping (Substring) throws -> NewCapture
602-
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
601+
) -> \(regexTypeName)<\(newMatchType(newCaptureType: "NewCapture"))> \(whereClause) {
603602
.init(node: .groupTransform(
604603
.capture,
605604
component().regex.root,
@@ -611,7 +610,7 @@ struct VariadicsGenerator: ParsableCommand {
611610
public func tryCapture<\(genericParams), NewCapture>(
612611
@RegexBuilder _ component: () -> R,
613612
transform: @escaping (Substring) -> NewCapture?
614-
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
613+
) -> \(regexTypeName)<\(newMatchType(newCaptureType: "NewCapture"))> \(whereClause) {
615614
.init(node: .groupTransform(
616615
.capture,
617616
component().regex.root,

0 commit comments

Comments
 (0)