Skip to content

Commit 29f9954

Browse files
committed
Rename Match and MatchResult to Output and Regex.Match.
- Rename the `Match` associatedtype in `RegexComponent` to `Output`. - Rename `MatchResult` to `Regex.Match`. The new names have been pitched as part of the [regex type](https://forums.swift.org/t/pitch-regex-type-and-overview/56029) and the [regex builder DSL](https://forums.swift.org/t/pitch-regex-builder-dsl/56007).
1 parent 279847c commit 29f9954

File tree

13 files changed

+675
-669
lines changed

13 files changed

+675
-669
lines changed

Sources/Exercises/Participants/RegexParticipant.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ private func extractFromCaptures(
6161
private func graphemeBreakPropertyData<RP: RegexComponent>(
6262
forLine line: String,
6363
using regex: RP
64-
) -> GraphemeBreakEntry? where RP.Match == (Substring, Substring, Substring?, Substring) {
65-
line.match(regex).map(\.match).flatMap(extractFromCaptures)
64+
) -> GraphemeBreakEntry? where RP.Output == (Substring, Substring, Substring?, Substring) {
65+
line.match(regex).map(\.output).flatMap(extractFromCaptures)
6666
}
6767

6868
private func graphemeBreakPropertyDataLiteral(
@@ -91,7 +91,7 @@ private func graphemeBreakPropertyData(
9191
TryCapture(OneOrMore(.word)) { Unicode.GraphemeBreakProperty($0) }
9292
ZeroOrMore(.any)
9393
}.map {
94-
let (_, lower, upper, property) = $0.match
94+
let (_, lower, upper, property) = $0.output
9595
return GraphemeBreakEntry(lower...(upper ?? lower), property)
9696
}
9797
}

Sources/VariadicsGenerator/VariadicsGenerator.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ var standardError = StandardErrorStream()
9090

9191
typealias Counter = Int64
9292
let regexComponentProtocolName = "RegexComponent"
93-
let matchAssociatedTypeName = "Match"
93+
let outputAssociatedTypeName = "Output"
9494
let patternProtocolRequirementName = "regex"
9595
let regexTypeName = "Regex"
9696
let baseMatchTypeName = "Substring"
@@ -202,15 +202,15 @@ struct VariadicsGenerator: ParsableCommand {
202202
// Emit concatenation type declaration.
203203

204204
let whereClause: String = {
205-
var result = " where R0.Match == "
205+
var result = " where R0.\(outputAssociatedTypeName) == "
206206
if leftArity == 0 {
207207
result += "W0"
208208
} else {
209209
result += "(W0"
210210
result += (0..<leftArity).map { ", C\($0)" }.joined()
211211
result += ")"
212212
}
213-
result += ", R1.Match == "
213+
result += ", R1.\(outputAssociatedTypeName) == "
214214
if rightArity == 0 {
215215
result += "W1"
216216
} else {
@@ -267,7 +267,7 @@ struct VariadicsGenerator: ParsableCommand {
267267
}
268268
output(")")
269269
}
270-
output("> where R0.\(matchAssociatedTypeName) == ")
270+
output("> where R0.\(outputAssociatedTypeName) == ")
271271
if leftArity == 0 {
272272
output("W0")
273273
} else {
@@ -348,10 +348,10 @@ struct VariadicsGenerator: ParsableCommand {
348348
self.matchType = arity == 0
349349
? baseMatchTypeName
350350
: "(\(baseMatchTypeName), \(quantifiedCaptures))"
351-
self.whereClauseForInit = "where \(matchAssociatedTypeName) == \(matchType)" +
352-
(arity == 0 ? "" : ", Component.Match == (W, \(capturesJoined))")
351+
self.whereClauseForInit = "where \(outputAssociatedTypeName) == \(matchType)" +
352+
(arity == 0 ? "" : ", Component.\(outputAssociatedTypeName) == (W, \(capturesJoined))")
353353
self.whereClause = arity == 0 ? "" :
354-
"where Component.Match == (W, \(capturesJoined))"
354+
"where Component.\(outputAssociatedTypeName) == (W, \(capturesJoined))"
355355
}
356356
}
357357

@@ -468,10 +468,10 @@ struct VariadicsGenerator: ParsableCommand {
468468
let whereClause: String = {
469469
var result = "where R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)"
470470
if leftArity > 0 {
471-
result += ", R0.\(matchAssociatedTypeName) == (W0, \((0..<leftArity).map { "C\($0)" }.joined(separator: ", ")))"
471+
result += ", R0.\(outputAssociatedTypeName) == (W0, \((0..<leftArity).map { "C\($0)" }.joined(separator: ", ")))"
472472
}
473473
if rightArity > 0 {
474-
result += ", R1.\(matchAssociatedTypeName) == (W1, \((leftArity..<leftArity+rightArity).map { "C\($0)" }.joined(separator: ", ")))"
474+
result += ", R1.\(outputAssociatedTypeName) == (W1, \((leftArity..<leftArity+rightArity).map { "C\($0)" }.joined(separator: ", ")))"
475475
}
476476
return result
477477
}()
@@ -516,7 +516,7 @@ struct VariadicsGenerator: ParsableCommand {
516516
}()
517517
let whereClause: String = """
518518
where R: \(regexComponentProtocolName), \
519-
R.\(matchAssociatedTypeName) == (W, \(captures))
519+
R.\(outputAssociatedTypeName) == (W, \(captures))
520520
"""
521521
let resultCaptures = (0..<arity).map { "C\($0)?" }.joined(separator: ", ")
522522
output("""
@@ -544,8 +544,8 @@ struct VariadicsGenerator: ParsableCommand {
544544
}
545545
let rawNewMatchType = newMatchType(newCaptureType: "W")
546546
let transformedNewMatchType = newMatchType(newCaptureType: "NewCapture")
547-
let whereClauseRaw = "where \(matchAssociatedTypeName) == \(rawNewMatchType), R.\(matchAssociatedTypeName) == \(matchType)"
548-
let whereClauseTransformed = "where \(matchAssociatedTypeName) == \(transformedNewMatchType), R.\(matchAssociatedTypeName) == \(matchType)"
547+
let whereClauseRaw = "where \(outputAssociatedTypeName) == \(rawNewMatchType), R.\(outputAssociatedTypeName) == \(matchType)"
548+
let whereClauseTransformed = "where \(outputAssociatedTypeName) == \(transformedNewMatchType), R.\(outputAssociatedTypeName) == \(matchType)"
549549
output("""
550550
// MARK: - Non-builder capture arity \(arity)
551551

Sources/_StringProcessing/Algorithms/Consumers/RegexConsumer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ extension RegexConsumer {
2828
consumed.base,
2929
in: range, mode: .partialFromFront
3030
) else { return nil }
31-
return (result.range.upperBound, result.match)
31+
return (result.range.upperBound, result.output)
3232
}
3333
}
3434

3535
// TODO: Explicitly implement the non-matching consumer/searcher protocols as
3636
// well, taking advantage of the fact that the captures can be ignored
3737

3838
extension RegexConsumer: MatchingCollectionConsumer {
39-
public typealias Match = R.Match
39+
public typealias Match = R.Output
4040

4141
public func matchingConsuming(
4242
_ consumed: Consumed, in range: Range<Consumed.Index>

Sources/_StringProcessing/RegexDSL/Anchor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ extension Anchor {
109109
public func lookahead<R: RegexComponent>(
110110
negative: Bool = false,
111111
@RegexComponentBuilder _ content: () -> R
112-
) -> Regex<R.Match> {
112+
) -> Regex<R.Output> {
113113
Regex(node: .nonCapturingGroup(negative ? .negativeLookahead : .lookahead, content().regex.root))
114114
}
115115

116116
public func lookahead<R: RegexComponent>(
117117
_ component: R,
118118
negative: Bool = false
119-
) -> Regex<R.Match> {
119+
) -> Regex<R.Output> {
120120
Regex(node: .nonCapturingGroup(negative ? .negativeLookahead : .lookahead, component.regex.root))
121121
}

Sources/_StringProcessing/RegexDSL/Core.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import _MatchingEngine
1414

1515
/// A type that represents a regular expression.
1616
public protocol RegexComponent {
17-
associatedtype Match
18-
var regex: Regex<Match> { get }
17+
associatedtype Output
18+
var regex: Regex<Output> { get }
1919
}
2020

2121
/// A regular expression.
22-
public struct Regex<Match>: RegexComponent {
22+
public struct Regex<Output>: RegexComponent {
2323
/// A program representation that caches any lowered representation for
2424
/// execution.
2525
internal class Program {
@@ -80,37 +80,37 @@ public struct Regex<Match>: RegexComponent {
8080

8181
public init<Content: RegexComponent>(
8282
_ content: Content
83-
) where Content.Match == Match {
83+
) where Content.Output == Output {
8484
self = content.regex
8585
}
8686

8787
public init<Content: RegexComponent>(
8888
@RegexComponentBuilder _ content: () -> Content
89-
) where Content.Match == Match {
89+
) where Content.Output == Output {
9090
self.init(content())
9191
}
9292

93-
public var regex: Regex<Match> {
93+
public var regex: Regex<Output> {
9494
self
9595
}
9696
}
9797

9898

99-
public struct MockRegexLiteral<Match>: RegexComponent {
99+
public struct MockRegexLiteral<Output>: RegexComponent {
100100
public typealias MatchValue = Substring
101-
public let regex: Regex<Match>
101+
public let regex: Regex<Output>
102102

103103
public init(
104104
_ string: String,
105105
_ syntax: SyntaxOptions = .traditional,
106-
matching: Match.Type = Match.self
106+
matching: Output.Type = Output.self
107107
) throws {
108108
regex = Regex(ast: try parse(string, syntax))
109109
}
110110
}
111111

112-
public func r<Match>(
113-
_ s: String, matching matchType: Match.Type = Match.self
114-
) -> MockRegexLiteral<Match> {
112+
public func r<Output>(
113+
_ s: String, matching matchType: Output.Type = Output.self
114+
) -> MockRegexLiteral<Output> {
115115
try! MockRegexLiteral(s, matching: matchType)
116116
}

Sources/_StringProcessing/RegexDSL/DSL.swift

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import _MatchingEngine
1414
// A convenience protocol for builtin regex components that are initialized with
1515
// a `DSLTree` node.
1616
internal protocol _BuiltinRegexComponent: RegexComponent {
17-
init(_ regex: Regex<Match>)
17+
init(_ regex: Regex<Output>)
1818
}
1919

2020
extension _BuiltinRegexComponent {
@@ -26,41 +26,41 @@ extension _BuiltinRegexComponent {
2626
// MARK: - Primitives
2727

2828
extension String: RegexComponent {
29-
public typealias Match = Substring
29+
public typealias Output = Substring
3030

31-
public var regex: Regex<Match> {
31+
public var regex: Regex<Output> {
3232
.init(node: .quotedLiteral(self))
3333
}
3434
}
3535

3636
extension Substring: RegexComponent {
37-
public typealias Match = Substring
37+
public typealias Output = Substring
3838

39-
public var regex: Regex<Match> {
39+
public var regex: Regex<Output> {
4040
.init(node: .quotedLiteral(String(self)))
4141
}
4242
}
4343

4444
extension Character: RegexComponent {
45-
public typealias Match = Substring
45+
public typealias Output = Substring
4646

47-
public var regex: Regex<Match> {
47+
public var regex: Regex<Output> {
4848
.init(node: .atom(.char(self)))
4949
}
5050
}
5151

5252
extension UnicodeScalar: RegexComponent {
53-
public typealias Match = Substring
53+
public typealias Output = Substring
5454

55-
public var regex: Regex<Match> {
55+
public var regex: Regex<Output> {
5656
.init(node: .atom(.scalar(self)))
5757
}
5858
}
5959

6060
extension CharacterClass: RegexComponent {
61-
public typealias Match = Substring
61+
public typealias Output = Substring
6262

63-
public var regex: Regex<Match> {
63+
public var regex: Regex<Output> {
6464
guard let ast = self.makeAST() else {
6565
fatalError("FIXME: extended AST?")
6666
}
@@ -79,7 +79,7 @@ extension CharacterClass: RegexComponent {
7979
// where R0.Match == (W0, C0...), R1.Match == (W1, C1...)
8080
// {
8181
// typealias Match = (Substring, C0..., C1...)
82-
// let regex: Regex<Match>
82+
// let regex: Regex<Output>
8383
// init(_ first: R0, _ second: R1) {
8484
// regex = .init(concat(r0, r1))
8585
// }
@@ -127,43 +127,43 @@ extension QuantificationBehavior {
127127
}
128128
}
129129

130-
public struct OneOrMore<Match>: _BuiltinRegexComponent {
131-
public var regex: Regex<Match>
130+
public struct OneOrMore<Output>: _BuiltinRegexComponent {
131+
public var regex: Regex<Output>
132132

133-
internal init(_ regex: Regex<Match>) {
133+
internal init(_ regex: Regex<Output>) {
134134
self.regex = regex
135135
}
136136

137137
// Note: Public initializers and operators are currently gyb'd. See
138138
// Variadics.swift.
139139
}
140140

141-
public struct ZeroOrMore<Match>: _BuiltinRegexComponent {
142-
public var regex: Regex<Match>
141+
public struct ZeroOrMore<Output>: _BuiltinRegexComponent {
142+
public var regex: Regex<Output>
143143

144-
internal init(_ regex: Regex<Match>) {
144+
internal init(_ regex: Regex<Output>) {
145145
self.regex = regex
146146
}
147147

148148
// Note: Public initializers and operators are currently gyb'd. See
149149
// Variadics.swift.
150150
}
151151

152-
public struct Optionally<Match>: _BuiltinRegexComponent {
153-
public var regex: Regex<Match>
152+
public struct Optionally<Output>: _BuiltinRegexComponent {
153+
public var regex: Regex<Output>
154154

155-
internal init(_ regex: Regex<Match>) {
155+
internal init(_ regex: Regex<Output>) {
156156
self.regex = regex
157157
}
158158

159159
// Note: Public initializers and operators are currently gyb'd. See
160160
// Variadics.swift.
161161
}
162162

163-
public struct Repeat<Match>: _BuiltinRegexComponent {
164-
public var regex: Regex<Match>
163+
public struct Repeat<Output>: _BuiltinRegexComponent {
164+
public var regex: Regex<Output>
165165

166-
internal init(_ regex: Regex<Match>) {
166+
internal init(_ regex: Regex<Output>) {
167167
self.regex = regex
168168
}
169169

@@ -194,7 +194,7 @@ public struct AlternationBuilder {
194194
@_disfavoredOverload
195195
public static func buildPartialBlock<R: RegexComponent>(
196196
first component: R
197-
) -> ChoiceOf<R.Match> {
197+
) -> ChoiceOf<R.Output> {
198198
.init(component.regex)
199199
}
200200

@@ -211,10 +211,10 @@ public struct AlternationBuilder {
211211
}
212212
}
213213

214-
public struct ChoiceOf<Match>: _BuiltinRegexComponent {
215-
public var regex: Regex<Match>
214+
public struct ChoiceOf<Output>: _BuiltinRegexComponent {
215+
public var regex: Regex<Output>
216216

217-
internal init(_ regex: Regex<Match>) {
217+
internal init(_ regex: Regex<Output>) {
218218
self.regex = regex
219219
}
220220

@@ -225,20 +225,20 @@ public struct ChoiceOf<Match>: _BuiltinRegexComponent {
225225

226226
// MARK: - Capture
227227

228-
public struct Capture<Match>: _BuiltinRegexComponent {
229-
public var regex: Regex<Match>
228+
public struct Capture<Output>: _BuiltinRegexComponent {
229+
public var regex: Regex<Output>
230230

231-
internal init(_ regex: Regex<Match>) {
231+
internal init(_ regex: Regex<Output>) {
232232
self.regex = regex
233233
}
234234

235235
// Note: Public initializers are currently gyb'd. See Variadics.swift.
236236
}
237237

238-
public struct TryCapture<Match>: _BuiltinRegexComponent {
239-
public var regex: Regex<Match>
238+
public struct TryCapture<Output>: _BuiltinRegexComponent {
239+
public var regex: Regex<Output>
240240

241-
internal init(_ regex: Regex<Match>) {
241+
internal init(_ regex: Regex<Output>) {
242242
self.regex = regex
243243
}
244244

Sources/_StringProcessing/RegexDSL/DSLConsumers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public protocol CustomRegexComponent: RegexComponent {
1414
_ input: String,
1515
startingAt index: String.Index,
1616
in bounds: Range<String.Index>
17-
) -> (upperBound: String.Index, match: Match)?
17+
) -> (upperBound: String.Index, output: Output)?
1818
}
1919

2020
extension CustomRegexComponent {
21-
public var regex: Regex<Match> {
22-
Regex(node: .matcher(.init(Match.self), { input, index, bounds in
21+
public var regex: Regex<Output> {
22+
Regex(node: .matcher(.init(Output.self), { input, index, bounds in
2323
match(input, startingAt: index, in: bounds)
2424
}))
2525
}

0 commit comments

Comments
 (0)