Skip to content

Commit ed92d3b

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 ed92d3b

File tree

14 files changed

+679
-675
lines changed

14 files changed

+679
-675
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/Executor.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ struct Executor {
1919
self.engine = Engine(program, enableTracing: enablesTracing)
2020
}
2121

22-
func match<Match>(
22+
func match<Output>(
2323
_ input: String,
2424
in inputRange: Range<String.Index>,
2525
_ mode: MatchMode
26-
) throws -> MatchResult<Match>? {
26+
) throws -> Regex<Output>.Match? {
2727
var cpu = engine.makeProcessor(
2828
input: input, bounds: inputRange, matchMode: mode)
2929

@@ -43,8 +43,8 @@ struct Executor {
4343
// FIXME: This is a workaround for not tracking (or
4444
// specially compiling) whole-match values.
4545
let value: Any?
46-
if Match.self != Substring.self,
47-
Match.self != (Substring, DynamicCaptures).self,
46+
if Output.self != Substring.self,
47+
Output.self != (Substring, DynamicCaptures).self,
4848
caps.isEmpty
4949
{
5050
value = cpu.registers.values.first
@@ -53,7 +53,7 @@ struct Executor {
5353
value = nil
5454
}
5555

56-
return MatchResult(
56+
return .init(
5757
input: input,
5858
range: range,
5959
rawCaptures: caps,
@@ -65,7 +65,7 @@ struct Executor {
6565
_ input: String,
6666
in inputRange: Range<String.Index>,
6767
_ mode: MatchMode
68-
) throws -> MatchResult<(Substring, DynamicCaptures)>? {
68+
) throws -> Regex<(Substring, DynamicCaptures)>.Match? {
6969
try match(input, in: inputRange, mode)
7070
}
7171
}

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
}

0 commit comments

Comments
 (0)