Skip to content

Commit cd3ad6d

Browse files
authored
Rename RegexProtocol to RegexComponent and RegexBuilder to RegexComponentBuilder. (#200)
We've decided to use `RegexComponent` for the protocol, and use `RegexComponentBuilder` for the builder type since we want the user to import a module named `RegexBuilder` to get the DSL.
1 parent 39c54d3 commit cd3ad6d

25 files changed

+782
-781
lines changed

Sources/Exercises/Participants/RegexParticipant.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private func extractFromCaptures(
5858
}
5959

6060
@inline(__always) // get rid of generic please
61-
private func graphemeBreakPropertyData<RP: RegexProtocol>(
61+
private func graphemeBreakPropertyData<RP: RegexComponent>(
6262
forLine line: String,
6363
using regex: RP
6464
) -> GraphemeBreakEntry? where RP.Match == (Substring, Substring, Substring?, Substring) {

Sources/Prototypes/TourOfTypes/Literal.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ enum SemanticsLevel {
5555
}
5656

5757
/// Conformers can be ran as a regex / pattern
58-
protocol RegexProtocol {
58+
protocol RegexComponent {
5959
var level: SemanticsLevel? { get }
6060
}
6161

6262
/// Provide the option to encode semantic level statically
6363
protocol RegexLiteralProtocol: ExpressibleByRegexLiteral {
64-
associatedtype ScalarSemanticRegex: RegexProtocol
65-
associatedtype GraphemeSemanticRegex: RegexProtocol
66-
associatedtype POSIXSemanticRegex: RegexProtocol
67-
associatedtype UnspecifiedSemanticRegex: RegexProtocol = RegexLiteral
64+
associatedtype ScalarSemanticRegex: RegexComponent
65+
associatedtype GraphemeSemanticRegex: RegexComponent
66+
associatedtype POSIXSemanticRegex: RegexComponent
67+
associatedtype UnspecifiedSemanticRegex: RegexComponent = RegexLiteral
6868

6969
var scalarSemantic: ScalarSemanticRegex { get }
7070
var graphemeSemantic: GraphemeSemanticRegex { get }
@@ -84,16 +84,16 @@ struct StaticSemanticRegexLiteral: RegexLiteralProtocol {
8484
*/
8585

8686
/// A regex that has statically bound its semantic level
87-
struct ScalarSemanticRegex: RegexProtocol {
87+
struct ScalarSemanticRegex: RegexComponent {
8888
var level: SemanticsLevel? { .scalar }
8989
}
90-
struct GraphemeSemanticRegex: RegexProtocol {
90+
struct GraphemeSemanticRegex: RegexComponent {
9191
var level: SemanticsLevel? { .graphemeCluster }
9292
}
93-
struct POSIXSemanticRegex: RegexProtocol {
93+
struct POSIXSemanticRegex: RegexComponent {
9494
var level: SemanticsLevel? { .posix }
9595
}
96-
struct UnspecifiedSemanticRegex: RegexProtocol {
96+
struct UnspecifiedSemanticRegex: RegexComponent {
9797
var level: SemanticsLevel? { nil }
9898
}
9999

@@ -132,9 +132,9 @@ struct RegexLiteral: ExpressibleByRegexLiteral {
132132
}
133133
}
134134

135-
extension RegexLiteral: RegexProtocol, RegexLiteralProtocol {
135+
extension RegexLiteral: RegexComponent, RegexLiteralProtocol {
136136
/// A regex that has finally bound its semantic level (dynamically)
137-
struct BoundSemantic: RegexProtocol {
137+
struct BoundSemantic: RegexComponent {
138138
var _level: SemanticsLevel // Bound semantic level
139139
var level: SemanticsLevel? { _level }
140140
}

Sources/VariadicsGenerator/VariadicsGenerator.swift

+24-23
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,13 @@ struct StandardErrorStream: TextOutputStream {
8989
var standardError = StandardErrorStream()
9090

9191
typealias Counter = Int64
92-
let regexProtocolName = "RegexProtocol"
92+
let regexComponentProtocolName = "RegexComponent"
9393
let matchAssociatedTypeName = "Match"
94-
let patternBuilderTypeName = "RegexBuilder"
9594
let patternProtocolRequirementName = "regex"
9695
let regexTypeName = "Regex"
9796
let baseMatchTypeName = "Substring"
97+
let concatBuilderName = "RegexComponentBuilder"
98+
let altBuilderName = "AlternationBuilder"
9899

99100
@main
100101
struct VariadicsGenerator: ParsableCommand {
@@ -194,7 +195,7 @@ struct VariadicsGenerator: ParsableCommand {
194195
result += (0..<leftArity+rightArity).map {
195196
", C\($0)"
196197
}.joined()
197-
result += ", R0: \(regexProtocolName), R1: \(regexProtocolName)"
198+
result += ", R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)"
198199
return result
199200
}()
200201

@@ -231,7 +232,7 @@ struct VariadicsGenerator: ParsableCommand {
231232
}()
232233

233234
// Emit concatenation builder.
234-
output("extension \(patternBuilderTypeName) {\n")
235+
output("extension \(concatBuilderName) {\n")
235236
output("""
236237
public static func buildBlock<\(genericParams)>(
237238
combining next: R1, into combined: R0
@@ -246,14 +247,14 @@ struct VariadicsGenerator: ParsableCommand {
246247
func emitConcatenationWithEmpty(leftArity: Int) {
247248
// T + () = T
248249
output("""
249-
extension RegexBuilder {
250+
extension \(concatBuilderName) {
250251
public static func buildBlock<W0
251252
""")
252253
outputForEach(0..<leftArity) {
253254
", C\($0)"
254255
}
255256
output("""
256-
, R0: \(regexProtocolName), R1: \(regexProtocolName)>(
257+
, R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)>(
257258
combining next: R1, into combined: R0
258259
) -> \(regexTypeName)<
259260
""")
@@ -329,7 +330,7 @@ struct VariadicsGenerator: ParsableCommand {
329330
result += (0..<arity).map { ", C\($0)" }.joined()
330331
result += ", "
331332
}
332-
result += "Component: \(regexProtocolName)"
333+
result += "Component: \(regexComponentProtocolName)"
333334
return result
334335
}()
335336

@@ -366,7 +367,7 @@ struct VariadicsGenerator: ParsableCommand {
366367
\(params.disfavored)\
367368
public func \(kind.rawValue)<\(params.genericParams)>(
368369
_ behavior: QuantificationBehavior = .eagerly,
369-
@RegexBuilder _ component: () -> Component
370+
@\(concatBuilderName) _ component: () -> Component
370371
) -> \(regexTypeName)<\(params.matchType)> \(params.whereClause) {
371372
.init(node: .quantification(.\(kind.astQuantifierAmount), behavior.astKind, component().regex.root))
372373
}
@@ -380,7 +381,7 @@ struct VariadicsGenerator: ParsableCommand {
380381
381382
\(kind == .zeroOrOne ?
382383
"""
383-
extension RegexBuilder {
384+
extension \(concatBuilderName) {
384385
public static func buildLimitedAvailability<\(params.genericParams)>(
385386
_ component: Component
386387
) -> \(regexTypeName)<\(params.matchType)> \(params.whereClause) {
@@ -413,7 +414,7 @@ struct VariadicsGenerator: ParsableCommand {
413414
\(params.disfavored)\
414415
public func repeating<\(params.genericParams)>(
415416
count: Int,
416-
@RegexBuilder _ component: () -> Component
417+
@\(concatBuilderName) _ component: () -> Component
417418
) -> \(regexTypeName)<\(params.matchType)> \(params.whereClause) {
418419
assert(count > 0, "Must specify a positive count")
419420
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
@@ -433,7 +434,7 @@ struct VariadicsGenerator: ParsableCommand {
433434
public func repeating<\(params.genericParams), R: RangeExpression>(
434435
_ expression: R,
435436
_ behavior: QuantificationBehavior = .eagerly,
436-
@RegexBuilder _ component: () -> Component
437+
@\(concatBuilderName) _ component: () -> Component
437438
) -> \(regexTypeName)<\(params.matchType)> \(params.repeatingWhereClause) {
438439
.init(node: .repeating(expression.relative(to: 0..<Int.max), behavior, component().regex.root))
439440
}
@@ -456,7 +457,7 @@ struct VariadicsGenerator: ParsableCommand {
456457
}()
457458
let genericParams = leftGenParams + ", " + rightGenParams
458459
let whereClause: String = {
459-
var result = "where R0: \(regexProtocolName), R1: \(regexProtocolName)"
460+
var result = "where R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)"
460461
if leftArity > 0 {
461462
result += ", R0.\(matchAssociatedTypeName) == (W0, \((0..<leftArity).map { "C\($0)" }.joined(separator: ", ")))"
462463
}
@@ -480,7 +481,7 @@ struct VariadicsGenerator: ParsableCommand {
480481
return "(\(baseMatchTypeName), \(resultCaptures))"
481482
}()
482483
output("""
483-
extension AlternationBuilder {
484+
extension \(altBuilderName) {
484485
public static func buildBlock<\(genericParams)>(
485486
combining next: R1, into combined: R0
486487
) -> \(regexTypeName)<\(matchType)> \(whereClause) {
@@ -505,12 +506,12 @@ struct VariadicsGenerator: ParsableCommand {
505506
return "R, W, " + captures
506507
}()
507508
let whereClause: String = """
508-
where R: \(regexProtocolName), \
509+
where R: \(regexComponentProtocolName), \
509510
R.\(matchAssociatedTypeName) == (W, \(captures))
510511
"""
511512
let resultCaptures = (0..<arity).map { "C\($0)?" }.joined(separator: ", ")
512513
output("""
513-
extension AlternationBuilder {
514+
extension \(altBuilderName) {
514515
public static func buildBlock<\(genericParams)>(_ regex: R) -> \(regexTypeName)<(W, \(resultCaptures))> \(whereClause) {
515516
.init(node: .alternation([regex.regex.root]))
516517
}
@@ -521,8 +522,8 @@ struct VariadicsGenerator: ParsableCommand {
521522

522523
func emitCapture(arity: Int) {
523524
let genericParams = arity == 0
524-
? "R: \(regexProtocolName), W"
525-
: "R: \(regexProtocolName), W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ")
525+
? "R: \(regexComponentProtocolName), W"
526+
: "R: \(regexComponentProtocolName), W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ")
526527
let matchType = arity == 0
527528
? "W"
528529
: "(W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ") + ")"
@@ -630,20 +631,20 @@ struct VariadicsGenerator: ParsableCommand {
630631
// MARK: - Builder capture arity \(arity)
631632
632633
public func capture<\(genericParams)>(
633-
@RegexBuilder _ component: () -> R
634+
@\(concatBuilderName) _ component: () -> R
634635
) -> \(regexTypeName)<\(rawNewMatchType)> \(whereClause) {
635636
.init(node: .group(.capture, component().regex.root))
636637
}
637638
638639
public func capture<\(genericParams)>(
639640
as reference: Reference<W>,
640-
@RegexBuilder _ component: () -> R
641+
@\(concatBuilderName) _ component: () -> R
641642
) -> \(regexTypeName)<\(rawNewMatchType)> \(whereClause) {
642643
.init(node: .group(.capture, component().regex.root, reference.id))
643644
}
644645
645646
public func capture<\(genericParams), NewCapture>(
646-
@RegexBuilder _ component: () -> R,
647+
@\(concatBuilderName) _ component: () -> R,
647648
transform: @escaping (Substring) -> NewCapture
648649
) -> \(regexTypeName)<\(transformedNewMatchType)> \(whereClause) {
649650
.init(node: .groupTransform(
@@ -656,7 +657,7 @@ struct VariadicsGenerator: ParsableCommand {
656657
657658
public func tryCapture<\(genericParams), NewCapture>(
658659
as reference: Reference<NewCapture>,
659-
@RegexBuilder _ component: () -> R,
660+
@\(concatBuilderName) _ component: () -> R,
660661
transform: @escaping (Substring) throws -> NewCapture
661662
) -> \(regexTypeName)<\(transformedNewMatchType)> \(whereClause) {
662663
.init(node: .groupTransform(
@@ -669,7 +670,7 @@ struct VariadicsGenerator: ParsableCommand {
669670
}
670671
671672
public func tryCapture<\(genericParams), NewCapture>(
672-
@RegexBuilder _ component: () -> R,
673+
@\(concatBuilderName) _ component: () -> R,
673674
transform: @escaping (Substring) -> NewCapture?
674675
) -> \(regexTypeName)<\(transformedNewMatchType)> \(whereClause) {
675676
.init(node: .groupTransform(
@@ -682,7 +683,7 @@ struct VariadicsGenerator: ParsableCommand {
682683
683684
public func tryCapture<\(genericParams), NewCapture>(
684685
as reference: Reference<NewCapture>,
685-
@RegexBuilder _ component: () -> R,
686+
@\(concatBuilderName) _ component: () -> R,
686687
transform: @escaping (Substring) -> NewCapture?
687688
) -> \(regexTypeName)<\(transformedNewMatchType)> \(whereClause) {
688689
.init(node: .groupTransform(

Sources/_StringProcessing/Algorithms/Algorithms/Contains.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension BidirectionalCollection where Element: Comparable {
4040
// MARK: Regex algorithms
4141

4242
extension BidirectionalCollection where SubSequence == Substring {
43-
public func contains<R: RegexProtocol>(_ regex: R) -> Bool {
43+
public func contains<R: RegexComponent>(_ regex: R) -> Bool {
4444
contains(RegexConsumer(regex))
4545
}
4646
}

Sources/_StringProcessing/Algorithms/Algorithms/FirstRange.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ extension BidirectionalCollection where Element: Comparable {
5656
// MARK: Regex algorithms
5757

5858
extension BidirectionalCollection where SubSequence == Substring {
59-
public func firstRange<R: RegexProtocol>(of regex: R) -> Range<Index>? {
59+
public func firstRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
6060
firstRange(of: RegexConsumer(regex))
6161
}
6262

63-
public func lastRange<R: RegexProtocol>(of regex: R) -> Range<Index>? {
63+
public func lastRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
6464
lastRange(of: RegexConsumer(regex))
6565
}
6666
}

Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ extension BidirectionalCollection where Element: Comparable {
216216
// MARK: Regex algorithms
217217

218218
extension BidirectionalCollection where SubSequence == Substring {
219-
public func ranges<R: RegexProtocol>(
219+
public func ranges<R: RegexComponent>(
220220
of regex: R
221221
) -> RangesCollection<RegexConsumer<R, Self>> {
222222
ranges(of: RegexConsumer(regex))
223223
}
224224

225-
public func rangesFromBack<R: RegexProtocol>(
225+
public func rangesFromBack<R: RegexComponent>(
226226
of regex: R
227227
) -> ReversedRangesCollection<RegexConsumer<R, Self>> {
228228
rangesFromBack(of: RegexConsumer(regex))

Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ extension RangeReplaceableCollection
149149
// MARK: Regex algorithms
150150

151151
extension RangeReplaceableCollection where SubSequence == Substring {
152-
public func replacing<R: RegexProtocol, Replacement: Collection>(
152+
public func replacing<R: RegexComponent, Replacement: Collection>(
153153
_ regex: R,
154154
with replacement: Replacement,
155155
subrange: Range<Index>,
@@ -162,7 +162,7 @@ extension RangeReplaceableCollection where SubSequence == Substring {
162162
maxReplacements: maxReplacements)
163163
}
164164

165-
public func replacing<R: RegexProtocol, Replacement: Collection>(
165+
public func replacing<R: RegexComponent, Replacement: Collection>(
166166
_ regex: R,
167167
with replacement: Replacement,
168168
maxReplacements: Int = .max
@@ -174,7 +174,7 @@ extension RangeReplaceableCollection where SubSequence == Substring {
174174
maxReplacements: maxReplacements)
175175
}
176176

177-
public mutating func replace<R: RegexProtocol, Replacement: Collection>(
177+
public mutating func replace<R: RegexComponent, Replacement: Collection>(
178178
_ regex: R,
179179
with replacement: Replacement,
180180
maxReplacements: Int = .max

Sources/_StringProcessing/Algorithms/Algorithms/Split.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ extension BidirectionalCollection where Element: Comparable {
275275
// MARK: Regex algorithms
276276

277277
extension BidirectionalCollection where SubSequence == Substring {
278-
public func split<R: RegexProtocol>(
278+
public func split<R: RegexComponent>(
279279
by separator: R
280280
) -> SplitCollection<RegexConsumer<R, Self>> {
281281
split(by: RegexConsumer(separator))
282282
}
283283

284-
public func splitFromBack<R: RegexProtocol>(
284+
public func splitFromBack<R: RegexComponent>(
285285
by separator: R
286286
) -> ReversedSplitCollection<RegexConsumer<R, Self>> {
287287
splitFromBack(by: RegexConsumer(separator))

Sources/_StringProcessing/Algorithms/Algorithms/StartsWith.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ extension BidirectionalCollection where Element: Equatable {
4848
// MARK: Regex algorithms
4949

5050
extension BidirectionalCollection where SubSequence == Substring {
51-
public func starts<R: RegexProtocol>(with regex: R) -> Bool {
51+
public func starts<R: RegexComponent>(with regex: R) -> Bool {
5252
starts(with: RegexConsumer(regex))
5353
}
5454

55-
public func ends<R: RegexProtocol>(with regex: R) -> Bool {
55+
public func ends<R: RegexComponent>(with regex: R) -> Bool {
5656
ends(with: RegexConsumer(regex))
5757
}
5858
}

0 commit comments

Comments
 (0)