11
11
12
12
import _MatchingEngine
13
13
14
+ // A convenience protocol for builtin regex components that are initialized with
15
+ // a `DSLTree` node.
16
+ internal protocol _BuiltinRegexComponent : RegexComponent {
17
+ init ( _ regex: Regex < Match > )
18
+ }
19
+
20
+ extension _BuiltinRegexComponent {
21
+ init ( node: DSLTree . Node ) {
22
+ self . init ( Regex ( node: node) )
23
+ }
24
+ }
25
+
14
26
// MARK: - Primitives
15
27
16
28
extension String : RegexComponent {
@@ -115,37 +127,49 @@ extension QuantificationBehavior {
115
127
}
116
128
}
117
129
118
- // TODO: Variadic generics
119
- // struct _OneOrMore<W, C..., Component: RegexComponent>
120
- // where R.Match == (W, C...)
121
- // {
122
- // typealias Match = (Substring, [(C...)])
123
- // let regex: Regex<Match>
124
- // init(_ component: Component) {
125
- // regex = .init(oneOrMore(r0))
126
- // }
127
- // }
128
- //
129
- // struct _OneOrMoreNonCapturing<Component: RegexComponent> {
130
- // typealias Match = Substring
131
- // let regex: Regex<Match>
132
- // init(_ component: Component) {
133
- // regex = .init(oneOrMore(r0))
134
- // }
135
- // }
136
- //
137
- // func oneOrMore<W, C..., Component: RegexComponent>(
138
- // _ component: Component
139
- // ) -> <R: RegexComponent where R.Match == (Substring, [(C...)])> R {
140
- // _OneOrMore(component)
141
- // }
142
- //
143
- // @_disfavoredOverload
144
- // func oneOrMore<Component: RegexComponent>(
145
- // _ component: Component
146
- // ) -> <R: RegexComponent where R.Match == Substring> R {
147
- // _OneOrMoreNonCapturing(component)
148
- // }
130
+ public struct OneOrMore < Match> : _BuiltinRegexComponent {
131
+ public var regex : Regex < Match >
132
+
133
+ internal init ( _ regex: Regex < Match > ) {
134
+ self . regex = regex
135
+ }
136
+
137
+ // Note: Public initializers and operators are currently gyb'd. See
138
+ // Variadics.swift.
139
+ }
140
+
141
+ public struct ZeroOrMore < Match> : _BuiltinRegexComponent {
142
+ public var regex : Regex < Match >
143
+
144
+ internal init ( _ regex: Regex < Match > ) {
145
+ self . regex = regex
146
+ }
147
+
148
+ // Note: Public initializers and operators are currently gyb'd. See
149
+ // Variadics.swift.
150
+ }
151
+
152
+ public struct Optionally < Match> : _BuiltinRegexComponent {
153
+ public var regex : Regex < Match >
154
+
155
+ internal init ( _ regex: Regex < Match > ) {
156
+ self . regex = regex
157
+ }
158
+
159
+ // Note: Public initializers and operators are currently gyb'd. See
160
+ // Variadics.swift.
161
+ }
162
+
163
+ public struct Repeat < Match> : _BuiltinRegexComponent {
164
+ public var regex : Regex < Match >
165
+
166
+ internal init ( _ regex: Regex < Match > ) {
167
+ self . regex = regex
168
+ }
169
+
170
+ // Note: Public initializers and operators are currently gyb'd. See
171
+ // Variadics.swift.
172
+ }
149
173
150
174
postfix operator .?
151
175
postfix operator .*
@@ -168,8 +192,8 @@ postfix operator .+
168
192
@resultBuilder
169
193
public struct AlternationBuilder {
170
194
@_disfavoredOverload
171
- public static func buildBlock< R: RegexComponent > ( _ regex : R ) -> R {
172
- regex
195
+ public static func buildBlock< R: RegexComponent > ( _ component : R ) -> ChoiceOf < R . Match > {
196
+ . init ( component . regex)
173
197
}
174
198
175
199
public static func buildExpression< R: RegexComponent > ( _ regex: R ) -> R {
@@ -185,10 +209,38 @@ public struct AlternationBuilder {
185
209
}
186
210
}
187
211
188
- public func choiceOf< R: RegexComponent > (
189
- @AlternationBuilder builder: ( ) -> R
190
- ) -> R {
191
- builder ( )
212
+ public struct ChoiceOf < Match> : _BuiltinRegexComponent {
213
+ public var regex : Regex < Match >
214
+
215
+ internal init ( _ regex: Regex < Match > ) {
216
+ self . regex = regex
217
+ }
218
+
219
+ public init ( @AlternationBuilder _ builder: ( ) -> Self ) {
220
+ self = builder ( )
221
+ }
222
+ }
223
+
224
+ // MARK: - Capture
225
+
226
+ public struct Capture < Match> : _BuiltinRegexComponent {
227
+ public var regex : Regex < Match >
228
+
229
+ internal init ( _ regex: Regex < Match > ) {
230
+ self . regex = regex
231
+ }
232
+
233
+ // Note: Public initializers are currently gyb'd. See Variadics.swift.
234
+ }
235
+
236
+ public struct TryCapture < Match> : _BuiltinRegexComponent {
237
+ public var regex : Regex < Match >
238
+
239
+ internal init ( _ regex: Regex < Match > ) {
240
+ self . regex = regex
241
+ }
242
+
243
+ // Note: Public initializers are currently gyb'd. See Variadics.swift.
192
244
}
193
245
194
246
// MARK: - Backreference
0 commit comments