1
- // swift run VariadicsGenerator --max-arity 7 > Sources/RegexDSL/Concatenation.swift
1
+ // swift run VariadicsGenerator --max-arity 7 > Sources/_StringProcessing/ RegexDSL/Concatenation.swift
2
2
3
3
import ArgumentParser
4
4
5
5
struct Permutation {
6
6
let arity : Int
7
7
// 1 -> no extra constraint
8
- // 0 -> where T.Capture : NoCaptureProtocol
8
+ // 0 -> where T.Match : NoCaptureProtocol
9
9
let bits : Int64
10
10
11
11
func isCaptureless( at index: Int ) -> Bool {
@@ -77,8 +77,6 @@ func outputForEach<C: Collection>(
77
77
if let lt = lineTerminator {
78
78
let indent = needsSep ? " " : " "
79
79
output ( " \( lt) \n \( indent) " )
80
- } else if needsSep {
81
- output ( " " )
82
80
}
83
81
}
84
82
}
@@ -87,12 +85,13 @@ typealias Counter = Int64
87
85
let patternProtocolName = " RegexProtocol "
88
86
let concatenationStructTypeBaseName = " Concatenate "
89
87
let capturingGroupTypeBaseName = " CapturingGroup "
88
+ let matchAssociatedTypeName = " Match "
90
89
let captureAssociatedTypeName = " Capture "
91
90
let patternBuilderTypeName = " RegexBuilder "
92
91
let patternProtocolRequirementName = " regex "
93
92
let PatternTypeBaseName = " Regex "
94
- let emptyProtocolName = " EmptyProtocol "
95
- let emptyStructName = " Empty "
93
+ let emptyProtocolName = " EmptyCaptureProtocol "
94
+ let baseMatchTypeName = " Substring "
96
95
97
96
@main
98
97
struct VariadicsGenerator : ParsableCommand {
@@ -114,6 +113,10 @@ struct VariadicsGenerator: ParsableCommand {
114
113
115
114
""" )
116
115
116
+ for arity in 2 ... maxArity+ 1 {
117
+ emitTupleStruct ( arity: arity)
118
+ }
119
+
117
120
for arity in minArity... maxArity {
118
121
for permutation in Permutations ( arity: arity) {
119
122
emitConcatenation ( permutation: permutation)
@@ -124,42 +127,123 @@ struct VariadicsGenerator: ParsableCommand {
124
127
output ( " // END AUTO-GENERATED CONTENT " )
125
128
}
126
129
130
+ func emitTupleStruct( arity: Int ) {
131
+ output ( " @frozen public struct Tuple \( arity) < " )
132
+ outputForEach ( 0 ..< arity, separator: " , " ) {
133
+ " _ \( $0) "
134
+ }
135
+ output ( " >: TupleProtocol { \n " )
136
+ outputForEach ( 0 ..< arity, separator: " " ) {
137
+ " public var _ \( $0) : _ \( $0) \n "
138
+ }
139
+ output ( " \n init(_coercing: [Any]) { \n " )
140
+ outputForEach ( 0 ..< arity, separator: " " ) {
141
+ " self._ \( $0) = _coercing[ \( $0) ] as! _ \( $0) \n "
142
+ }
143
+ output ( " } \n " )
144
+
145
+ output ( " } \n " )
146
+ output ( " extension Tuple \( arity) : \( emptyProtocolName) where " )
147
+ outputForEach ( 1 ..< arity, separator: " , " ) {
148
+ " _ \( $0) : \( emptyProtocolName) "
149
+ }
150
+ output ( " {} \n " )
151
+ output ( " extension Tuple \( arity) : MatchProtocol { \n " )
152
+ output ( " public typealias Capture = " )
153
+ if arity == 2 {
154
+ output ( " _1 " )
155
+ } else {
156
+ output ( " Tuple \( arity- 1 ) < " )
157
+ outputForEach ( 1 ..< arity, separator: " , " ) {
158
+ " _ \( $0) "
159
+ }
160
+ output ( " > " )
161
+ }
162
+ // `public var tuple: (_0, ...) { (_0, ...) }`
163
+ output ( " \n public var tuple: ( " )
164
+ outputForEach ( 0 ..< arity, separator: " , " ) {
165
+ " _ \( $0) "
166
+ }
167
+ output ( " ) { ( " )
168
+ outputForEach ( 0 ..< arity, separator: " , " ) {
169
+ " self._ \( $0) "
170
+ }
171
+ output ( " ) } \n " )
172
+ // `public init(_0: _0, ...) { ... }`
173
+ output ( " \n public init( " )
174
+ outputForEach ( 0 ..< arity, separator: " , " ) {
175
+ " _ _ \( $0) : _ \( $0) "
176
+ }
177
+ output ( " ) { \n " )
178
+ outputForEach ( 0 ..< arity, separator: " " ) {
179
+ " self._ \( $0) = _ \( $0) \n "
180
+ }
181
+ output ( " } " )
182
+ output ( " \n } \n " )
183
+ // Equatable
184
+ output ( " extension Tuple \( arity) : Equatable where " )
185
+ outputForEach ( 0 ..< arity, separator: " , " ) {
186
+ " _ \( $0) : Equatable "
187
+ }
188
+ output ( " { \n " )
189
+ output ( " public static func == (lhs: Self, rhs: Self) -> Bool { \n " )
190
+ output ( " " )
191
+ outputForEach ( 0 ..< arity, separator: " && " ) {
192
+ " lhs._ \( $0) == rhs._ \( $0) "
193
+ }
194
+ output ( " \n } " )
195
+ output ( " } \n " )
196
+ }
197
+
127
198
func emitConcatenation( permutation: Permutation ) {
128
199
let arity = permutation. arity
200
+
201
+ func emitGenericParameters( withConstraints: Bool ) {
202
+ outputForEach ( 0 ..< arity, separator: " , " ) {
203
+ var base = " T \( $0) "
204
+ if withConstraints {
205
+ base += " : \( patternProtocolName) "
206
+ }
207
+ return base
208
+ }
209
+ }
210
+
129
211
// Emit concatenation type declarations.
130
212
// public struct Concatenation{n}_{perm}<...>: RegexProtocol {
131
- // public typealias Capture = ...
132
- // public let regex: Regex
213
+ // public typealias Match = ...
214
+ // public let regex: Regex<Match>
133
215
// public init(...) { ... }
134
216
// }
135
- let typeName = " \( concatenationStructTypeBaseName) \( arity) _ \( permutation. identifier) "
217
+ let typeName =
218
+ " \( concatenationStructTypeBaseName) \( arity) _ \( permutation. identifier) "
136
219
output ( " public struct \( typeName) < \n " )
137
- outputForEach ( 0 ..< arity , separator : " , " ) { " T \( $0 ) : \( patternProtocolName ) " }
220
+ emitGenericParameters ( withConstraints : true )
138
221
output ( " \n >: \( patternProtocolName) " )
139
222
if permutation. hasCaptureless {
140
223
output ( " where " )
141
- outputForEach ( permutation. capturelessIndices, separator: " , " ) {
142
- " T \( $0) . \( captureAssociatedTypeName) : \( emptyProtocolName) "
224
+ outputForEach ( permutation. capturelessIndices, separator: " , " ) {
225
+ " T \( $0) . \( matchAssociatedTypeName ) . \( captureAssociatedTypeName) : \( emptyProtocolName) "
143
226
}
144
227
}
145
228
output ( " { \n " )
146
229
let captureIndices = permutation. captureIndices
147
- output ( " public typealias \( captureAssociatedTypeName ) = " )
230
+ output ( " public typealias \( matchAssociatedTypeName ) = " )
148
231
let captureElements = captureIndices
149
- . map { " T \( $0) . \( captureAssociatedTypeName) " }
232
+ . map { " T \( $0) . \( matchAssociatedTypeName ) . \( captureAssociatedTypeName) " }
150
233
if captureElements. isEmpty {
151
- output ( emptyStructName )
234
+ output ( baseMatchTypeName )
152
235
} else {
153
- output ( " ( \( captureElements. joined ( separator: " , " ) ) ) " )
236
+ let count = captureElements. count + 1
237
+ output ( " Tuple \( count) < \( baseMatchTypeName) , \( captureElements. joined ( separator: " , " ) ) > " )
154
238
}
155
239
output ( " \n " )
156
- output ( " public let \( patternProtocolRequirementName) : \( PatternTypeBaseName) < \( captureAssociatedTypeName ) > \n " )
240
+ output ( " public let \( patternProtocolRequirementName) : \( PatternTypeBaseName) < \( matchAssociatedTypeName ) > \n " )
157
241
output ( " init( " )
158
- outputForEach ( 0 ..< arity, separator: " , " ) { " _ x \( $0) : T \( $0) " }
242
+ outputForEach ( 0 ..< arity, separator: " , " ) { " _ x \( $0) : T \( $0) " }
159
243
output ( " ) { \n " )
160
244
output ( " \( patternProtocolRequirementName) = .init(ast: concat( \n " )
161
245
outputForEach (
162
- 0 ..< arity, separator: " , " , lineTerminator: " "
246
+ 0 ..< arity, separator: " , " , lineTerminator: " "
163
247
) { i in
164
248
" x \( i) . \( patternProtocolRequirementName) .ast "
165
249
}
@@ -169,14 +253,14 @@ struct VariadicsGenerator: ParsableCommand {
169
253
// Emit concatenation builders.
170
254
output ( " extension \( patternBuilderTypeName) { \n " )
171
255
output ( " public static func buildBlock< " )
172
- outputForEach ( 0 ..< arity , separator : " , " ) { " T \( $0 ) " }
256
+ emitGenericParameters ( withConstraints : true )
173
257
output ( " >( \n " )
174
- outputForEach ( 0 ..< arity, separator: " , " ) { " _ x \( $0) : T \( $0) " }
258
+ outputForEach ( 0 ..< arity, separator: " , " ) { " _ x \( $0) : T \( $0) " }
175
259
output ( " \n ) -> \( typeName) < " )
176
- outputForEach ( 0 ..< arity , separator : " , " ) { " T \( $0 ) " }
260
+ emitGenericParameters ( withConstraints : false )
177
261
output ( " > { \n " )
178
262
output ( " \( typeName) ( " )
179
- outputForEach ( 0 ..< arity, separator: " , " ) { " x \( $0) " }
263
+ outputForEach ( 0 ..< arity, separator: " , " ) { " x \( $0) " }
180
264
output ( " ) \n } \n } \n \n " )
181
265
}
182
266
}
0 commit comments