Skip to content

Commit d9dfeab

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

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

Sources/VariadicsGenerator/VariadicsGenerator.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,7 @@ 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
535534
? "(W, \(newCaptureType))"
536535
: "(W, \(newCaptureType), " + (0..<arity).map { "C\($0)" }.joined(separator: ", ") + ")"
@@ -539,13 +538,13 @@ struct VariadicsGenerator: ParsableCommand {
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,

Sources/_StringProcessing/RegexDSL/Variadics.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,7 @@ extension AlternationBuilder {
22652265
}
22662266
// MARK: - Non-builder capture arity 0
22672267

2268-
public func capture<R: RegexProtocol, W>(_ component: R) -> Regex<(W, Substring)> where R.Match == W {
2268+
public func capture<R: RegexProtocol, W>(_ component: R) -> Regex<(W, W)> where R.Match == W {
22692269
.init(node: .group(.capture, component.regex.root))
22702270
}
22712271

@@ -2306,7 +2306,7 @@ public func tryCapture<R: RegexProtocol, W, NewCapture>(
23062306

23072307
public func capture<R: RegexProtocol, W>(
23082308
@RegexBuilder _ component: () -> R
2309-
) -> Regex<(W, Substring)> where R.Match == W {
2309+
) -> Regex<(W, W)> where R.Match == W {
23102310
.init(node: .group(.capture, component().regex.root))
23112311
}
23122312

@@ -2347,7 +2347,7 @@ public func tryCapture<R: RegexProtocol, W, NewCapture>(
23472347
}
23482348
// MARK: - Non-builder capture arity 1
23492349

2350-
public func capture<R: RegexProtocol, W, C0>(_ component: R) -> Regex<(W, Substring, C0)> where R.Match == (W, C0) {
2350+
public func capture<R: RegexProtocol, W, C0>(_ component: R) -> Regex<(W, W, C0)> where R.Match == (W, C0) {
23512351
.init(node: .group(.capture, component.regex.root))
23522352
}
23532353

@@ -2388,7 +2388,7 @@ public func tryCapture<R: RegexProtocol, W, C0, NewCapture>(
23882388

23892389
public func capture<R: RegexProtocol, W, C0>(
23902390
@RegexBuilder _ component: () -> R
2391-
) -> Regex<(W, Substring, C0)> where R.Match == (W, C0) {
2391+
) -> Regex<(W, W, C0)> where R.Match == (W, C0) {
23922392
.init(node: .group(.capture, component().regex.root))
23932393
}
23942394

@@ -2429,7 +2429,7 @@ public func tryCapture<R: RegexProtocol, W, C0, NewCapture>(
24292429
}
24302430
// MARK: - Non-builder capture arity 2
24312431

2432-
public func capture<R: RegexProtocol, W, C0, C1>(_ component: R) -> Regex<(W, Substring, C0, C1)> where R.Match == (W, C0, C1) {
2432+
public func capture<R: RegexProtocol, W, C0, C1>(_ component: R) -> Regex<(W, W, C0, C1)> where R.Match == (W, C0, C1) {
24332433
.init(node: .group(.capture, component.regex.root))
24342434
}
24352435

@@ -2470,7 +2470,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, NewCapture>(
24702470

24712471
public func capture<R: RegexProtocol, W, C0, C1>(
24722472
@RegexBuilder _ component: () -> R
2473-
) -> Regex<(W, Substring, C0, C1)> where R.Match == (W, C0, C1) {
2473+
) -> Regex<(W, W, C0, C1)> where R.Match == (W, C0, C1) {
24742474
.init(node: .group(.capture, component().regex.root))
24752475
}
24762476

@@ -2511,7 +2511,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, NewCapture>(
25112511
}
25122512
// MARK: - Non-builder capture arity 3
25132513

2514-
public func capture<R: RegexProtocol, W, C0, C1, C2>(_ component: R) -> Regex<(W, Substring, C0, C1, C2)> where R.Match == (W, C0, C1, C2) {
2514+
public func capture<R: RegexProtocol, W, C0, C1, C2>(_ component: R) -> Regex<(W, W, C0, C1, C2)> where R.Match == (W, C0, C1, C2) {
25152515
.init(node: .group(.capture, component.regex.root))
25162516
}
25172517

@@ -2552,7 +2552,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, NewCapture>(
25522552

25532553
public func capture<R: RegexProtocol, W, C0, C1, C2>(
25542554
@RegexBuilder _ component: () -> R
2555-
) -> Regex<(W, Substring, C0, C1, C2)> where R.Match == (W, C0, C1, C2) {
2555+
) -> Regex<(W, W, C0, C1, C2)> where R.Match == (W, C0, C1, C2) {
25562556
.init(node: .group(.capture, component().regex.root))
25572557
}
25582558

@@ -2593,7 +2593,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, NewCapture>(
25932593
}
25942594
// MARK: - Non-builder capture arity 4
25952595

2596-
public func capture<R: RegexProtocol, W, C0, C1, C2, C3>(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) {
2596+
public func capture<R: RegexProtocol, W, C0, C1, C2, C3>(_ component: R) -> Regex<(W, W, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) {
25972597
.init(node: .group(.capture, component.regex.root))
25982598
}
25992599

@@ -2634,7 +2634,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, NewCapture>(
26342634

26352635
public func capture<R: RegexProtocol, W, C0, C1, C2, C3>(
26362636
@RegexBuilder _ component: () -> R
2637-
) -> Regex<(W, Substring, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) {
2637+
) -> Regex<(W, W, C0, C1, C2, C3)> where R.Match == (W, C0, C1, C2, C3) {
26382638
.init(node: .group(.capture, component().regex.root))
26392639
}
26402640

@@ -2675,7 +2675,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, NewCapture>(
26752675
}
26762676
// MARK: - Non-builder capture arity 5
26772677

2678-
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4>(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) {
2678+
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4>(_ component: R) -> Regex<(W, W, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) {
26792679
.init(node: .group(.capture, component.regex.root))
26802680
}
26812681

@@ -2716,7 +2716,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, C4, NewCapture>(
27162716

27172717
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4>(
27182718
@RegexBuilder _ component: () -> R
2719-
) -> Regex<(W, Substring, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) {
2719+
) -> Regex<(W, W, C0, C1, C2, C3, C4)> where R.Match == (W, C0, C1, C2, C3, C4) {
27202720
.init(node: .group(.capture, component().regex.root))
27212721
}
27222722

@@ -2757,7 +2757,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, C4, NewCapture>(
27572757
}
27582758
// MARK: - Non-builder capture arity 6
27592759

2760-
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5>(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) {
2760+
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5>(_ component: R) -> Regex<(W, W, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) {
27612761
.init(node: .group(.capture, component.regex.root))
27622762
}
27632763

@@ -2798,7 +2798,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, NewCapture>(
27982798

27992799
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5>(
28002800
@RegexBuilder _ component: () -> R
2801-
) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) {
2801+
) -> Regex<(W, W, C0, C1, C2, C3, C4, C5)> where R.Match == (W, C0, C1, C2, C3, C4, C5) {
28022802
.init(node: .group(.capture, component().regex.root))
28032803
}
28042804

@@ -2839,7 +2839,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, NewCapture>(
28392839
}
28402840
// MARK: - Non-builder capture arity 7
28412841

2842-
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6>(_ component: R) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) {
2842+
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6>(_ component: R) -> Regex<(W, W, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) {
28432843
.init(node: .group(.capture, component.regex.root))
28442844
}
28452845

@@ -2880,7 +2880,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, NewCaptu
28802880

28812881
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6>(
28822882
@RegexBuilder _ component: () -> R
2883-
) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) {
2883+
) -> Regex<(W, W, C0, C1, C2, C3, C4, C5, C6)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6) {
28842884
.init(node: .group(.capture, component().regex.root))
28852885
}
28862886

@@ -2921,7 +2921,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, NewCaptu
29212921
}
29222922
// MARK: - Non-builder capture arity 8
29232923

2924-
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, C7>(_ 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) {
2924+
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, C7>(_ component: R) -> Regex<(W, W, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) {
29252925
.init(node: .group(.capture, component.regex.root))
29262926
}
29272927

@@ -2962,7 +2962,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, C7, NewC
29622962

29632963
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, C7>(
29642964
@RegexBuilder _ component: () -> R
2965-
) -> Regex<(W, Substring, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) {
2965+
) -> Regex<(W, W, C0, C1, C2, C3, C4, C5, C6, C7)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7) {
29662966
.init(node: .group(.capture, component().regex.root))
29672967
}
29682968

@@ -3003,7 +3003,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, C7, NewC
30033003
}
30043004
// MARK: - Non-builder capture arity 9
30053005

3006-
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, C7, C8>(_ 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) {
3006+
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, C7, C8>(_ component: R) -> Regex<(W, W, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) {
30073007
.init(node: .group(.capture, component.regex.root))
30083008
}
30093009

@@ -3044,7 +3044,7 @@ public func tryCapture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, C7, C8,
30443044

30453045
public func capture<R: RegexProtocol, W, C0, C1, C2, C3, C4, C5, C6, C7, C8>(
30463046
@RegexBuilder _ component: () -> R
3047-
) -> 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) {
3047+
) -> Regex<(W, W, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R.Match == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) {
30483048
.init(node: .group(.capture, component().regex.root))
30493049
}
30503050

0 commit comments

Comments
 (0)