Skip to content

Commit fce8f9a

Browse files
committed
Merge branch 'main' into scalar-optimizations-clean
2 parents 6a1f6e9 + 33856e7 commit fce8f9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+4734
-3177
lines changed

Sources/PatternConverter/PatternConverter.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ struct PatternConverter: ParsableCommand {
5050
print("Converting '\(delim)\(regex)\(delim)'")
5151

5252
let ast = try _RegexParser.parse(
53-
regex, .semantic,
54-
experimentalSyntax ? .experimental : .traditional)
53+
regex, experimentalSyntax ? .experimental : .traditional)
5554

5655
// Show rendered source ranges
5756
if renderSourceRanges {

Sources/RegexBuilder/Anchor.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension Anchor: RegexComponent {
5151
}
5252

5353
public var regex: Regex<Substring> {
54-
Regex(node: .atom(.assertion(baseAssertion)))
54+
_RegexFactory().assertion(baseAssertion)
5555
}
5656
}
5757

@@ -159,14 +159,14 @@ public struct Lookahead<Output>: _BuiltinRegexComponent {
159159
public init<R: RegexComponent>(
160160
_ component: R
161161
) where R.RegexOutput == Output {
162-
self.init(node: .nonCapturingGroup(.lookahead, component.regex.root))
162+
self.init(_RegexFactory().lookaheadNonCapturing(component))
163163
}
164164

165165
/// Creates a lookahead from the regex generated by the given builder closure.
166166
public init<R: RegexComponent>(
167167
@RegexComponentBuilder _ component: () -> R
168168
) where R.RegexOutput == Output {
169-
self.init(node: .nonCapturingGroup(.lookahead, component().regex.root))
169+
self.init(_RegexFactory().lookaheadNonCapturing(component()))
170170
}
171171
}
172172

@@ -189,14 +189,14 @@ public struct NegativeLookahead<Output>: _BuiltinRegexComponent {
189189
public init<R: RegexComponent>(
190190
_ component: R
191191
) where R.RegexOutput == Output {
192-
self.init(node: .nonCapturingGroup(.negativeLookahead, component.regex.root))
192+
self.init(_RegexFactory().negativeLookaheadNonCapturing(component))
193193
}
194194

195195
/// Creates a negative lookahead from the regex generated by the given builder
196196
/// closure.
197197
public init<R: RegexComponent>(
198198
@RegexComponentBuilder _ component: () -> R
199199
) where R.RegexOutput == Output {
200-
self.init(node: .nonCapturingGroup(.negativeLookahead, component().regex.root))
200+
self.init(_RegexFactory().negativeLookaheadNonCapturing(component()))
201201
}
202202
}

Sources/RegexBuilder/Builder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
@resultBuilder
1616
public enum RegexComponentBuilder {
1717
public static func buildBlock() -> Regex<Substring> {
18-
.init(node: .empty)
18+
_RegexFactory().empty()
1919
}
2020

2121
public static func buildPartialBlock<R: RegexComponent>(

Sources/RegexBuilder/CharacterClass.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct CharacterClass {
3131
@available(SwiftStdlib 5.7, *)
3232
extension CharacterClass: RegexComponent {
3333
public var regex: Regex<Substring> {
34-
return Regex(node: DSLTree.Node.customCharacterClass(ccc))
34+
_RegexFactory().customCharacterClass(ccc)
3535
}
3636
}
3737

@@ -48,14 +48,10 @@ extension RegexComponent where Self == CharacterClass {
4848
.init(DSLTree.CustomCharacterClass(members: [.atom(.any)]))
4949
}
5050

51-
public static var anyGrapheme: CharacterClass {
51+
public static var anyGraphemeCluster: CharacterClass {
5252
.init(unconverted: .anyGrapheme)
5353
}
5454

55-
public static var anyUnicodeScalar: CharacterClass {
56-
.init(unconverted: .anyUnicodeScalar)
57-
}
58-
5955
public static var whitespace: CharacterClass {
6056
.init(unconverted: .whitespace)
6157
}

Sources/RegexBuilder/DSL.swift

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ internal protocol _BuiltinRegexComponent: RegexComponent {
2828
init(_ regex: Regex<RegexOutput>)
2929
}
3030

31-
@available(SwiftStdlib 5.7, *)
32-
extension _BuiltinRegexComponent {
33-
init(node: DSLTree.Node) {
34-
self.init(Regex(node: node))
35-
}
36-
}
37-
3831
// MARK: - Primitive regex components
3932

4033
@available(SwiftStdlib 5.7, *)
@@ -56,7 +49,7 @@ extension Character: RegexComponent {
5649
public typealias Output = Substring
5750

5851
public var regex: Regex<Output> {
59-
.init(node: .atom(.char(self)))
52+
_RegexFactory().char(self)
6053
}
6154
}
6255

@@ -65,7 +58,7 @@ extension UnicodeScalar: RegexComponent {
6558
public typealias Output = Substring
6659

6760
public var regex: Regex<Output> {
68-
.init(node: .atom(.scalar(self)))
61+
_RegexFactory().scalar(self)
6962
}
7063
}
7164

@@ -90,39 +83,6 @@ extension UnicodeScalar: RegexComponent {
9083

9184
// Note: Quantifiers are currently gyb'd.
9285

93-
extension DSLTree.Node {
94-
// Individual public API functions are in the generated Variadics.swift file.
95-
/// Generates a DSL tree node for a repeated range of the given node.
96-
@available(SwiftStdlib 5.7, *)
97-
static func repeating(
98-
_ range: Range<Int>,
99-
_ behavior: RegexRepetitionBehavior?,
100-
_ node: DSLTree.Node
101-
) -> DSLTree.Node {
102-
// TODO: Throw these as errors
103-
assert(range.lowerBound >= 0, "Cannot specify a negative lower bound")
104-
assert(!range.isEmpty, "Cannot specify an empty range")
105-
106-
let kind: DSLTree.QuantificationKind = behavior.map { .explicit($0.dslTreeKind) } ?? .default
107-
108-
switch (range.lowerBound, range.upperBound) {
109-
case (0, Int.max): // 0...
110-
return .quantification(.zeroOrMore, kind, node)
111-
case (1, Int.max): // 1...
112-
return .quantification(.oneOrMore, kind, node)
113-
case _ where range.count == 1: // ..<1 or ...0 or any range with count == 1
114-
// Note: `behavior` is ignored in this case
115-
return .quantification(.exactly(range.lowerBound), .default, node)
116-
case (0, _): // 0..<n or 0...n or ..<n or ...n
117-
return .quantification(.upToN(range.upperBound), kind, node)
118-
case (_, Int.max): // n...
119-
return .quantification(.nOrMore(range.lowerBound), kind, node)
120-
default: // any other range
121-
return .quantification(.range(range.lowerBound, range.upperBound), kind, node)
122-
}
123-
}
124-
}
125-
12686
/// A regex component that matches exactly one occurrence of its underlying
12787
/// component.
12888
@available(SwiftStdlib 5.7, *)
@@ -140,6 +100,7 @@ public struct One<Output>: RegexComponent {
140100
public struct OneOrMore<Output>: _BuiltinRegexComponent {
141101
public var regex: Regex<Output>
142102

103+
@usableFromInline
143104
internal init(_ regex: Regex<Output>) {
144105
self.regex = regex
145106
}
@@ -152,6 +113,7 @@ public struct OneOrMore<Output>: _BuiltinRegexComponent {
152113
public struct ZeroOrMore<Output>: _BuiltinRegexComponent {
153114
public var regex: Regex<Output>
154115

116+
@usableFromInline
155117
internal init(_ regex: Regex<Output>) {
156118
self.regex = regex
157119
}
@@ -164,6 +126,7 @@ public struct ZeroOrMore<Output>: _BuiltinRegexComponent {
164126
public struct Optionally<Output>: _BuiltinRegexComponent {
165127
public var regex: Regex<Output>
166128

129+
@usableFromInline
167130
internal init(_ regex: Regex<Output>) {
168131
self.regex = regex
169132
}
@@ -176,6 +139,7 @@ public struct Optionally<Output>: _BuiltinRegexComponent {
176139
public struct Repeat<Output>: _BuiltinRegexComponent {
177140
public var regex: Regex<Output>
178141

142+
@usableFromInline
179143
internal init(_ regex: Regex<Output>) {
180144
self.regex = regex
181145
}
@@ -217,6 +181,7 @@ public struct AlternationBuilder {
217181
public struct ChoiceOf<Output>: _BuiltinRegexComponent {
218182
public var regex: Regex<Output>
219183

184+
@usableFromInline
220185
internal init(_ regex: Regex<Output>) {
221186
self.regex = regex
222187
}
@@ -232,6 +197,7 @@ public struct ChoiceOf<Output>: _BuiltinRegexComponent {
232197
public struct Capture<Output>: _BuiltinRegexComponent {
233198
public var regex: Regex<Output>
234199

200+
@usableFromInline
235201
internal init(_ regex: Regex<Output>) {
236202
self.regex = regex
237203
}
@@ -243,6 +209,7 @@ public struct Capture<Output>: _BuiltinRegexComponent {
243209
public struct TryCapture<Output>: _BuiltinRegexComponent {
244210
public var regex: Regex<Output>
245211

212+
@usableFromInline
246213
internal init(_ regex: Regex<Output>) {
247214
self.regex = regex
248215
}
@@ -260,6 +227,7 @@ public struct TryCapture<Output>: _BuiltinRegexComponent {
260227
public struct Local<Output>: _BuiltinRegexComponent {
261228
public var regex: Regex<Output>
262229

230+
@usableFromInline
263231
internal init(_ regex: Regex<Output>) {
264232
self.regex = regex
265233
}
@@ -274,8 +242,13 @@ public struct Reference<Capture>: RegexComponent {
274242

275243
public init(_ captureType: Capture.Type = Capture.self) {}
276244

245+
@usableFromInline
246+
var _raw: Int {
247+
id._raw
248+
}
249+
277250
public var regex: Regex<Capture> {
278-
.init(node: .atom(.symbolicReference(id)))
251+
_RegexFactory().symbolicReference(id)
279252
}
280253
}
281254

@@ -285,3 +258,12 @@ extension Regex.Match {
285258
self[reference.id]
286259
}
287260
}
261+
262+
// RegexFactory's init is SPI, so we can't make an instance of one in AEIC, but
263+
// if we hide it behind a resilience barrier we can call this function instead
264+
// to get our instance of it.
265+
@available(SwiftStdlib 5.7, *)
266+
@usableFromInline
267+
internal func makeFactory() -> _RegexFactory {
268+
_RegexFactory()
269+
}

0 commit comments

Comments
 (0)