Skip to content

Commit 7d5e86d

Browse files
committed
Internalize _CharacterClassModel
`makeDSLTreeCharacterClass` was the last API that required it to be public. Remove it, and replace it with some static members on `_AST.Atom`.
1 parent 297a69d commit 7d5e86d

File tree

3 files changed

+50
-91
lines changed

3 files changed

+50
-91
lines changed

Sources/RegexBuilder/CharacterClass.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@ public struct CharacterClass {
2020
self.ccc = ccc
2121
}
2222

23-
init(unconverted model: _CharacterClassModel) {
24-
guard let ccc = model.makeDSLTreeCharacterClass() else {
25-
fatalError("Unsupported character class")
26-
}
27-
self.ccc = ccc
23+
init(unconverted atom: DSLTree._AST.Atom) {
24+
self.ccc = .init(members: [.atom(.unconverted(atom))])
2825
}
2926
}
3027

@@ -49,15 +46,15 @@ extension RegexComponent where Self == CharacterClass {
4946
}
5047

5148
public static var anyGraphemeCluster: CharacterClass {
52-
.init(unconverted: .anyGrapheme)
49+
.init(unconverted: ._anyGrapheme)
5350
}
5451

5552
public static var whitespace: CharacterClass {
56-
.init(unconverted: .whitespace)
53+
.init(unconverted: ._whitespace)
5754
}
5855

5956
public static var digit: CharacterClass {
60-
.init(unconverted: .digit)
57+
.init(unconverted: ._digit)
6158
}
6259

6360
public static var hexDigit: CharacterClass {
@@ -69,19 +66,19 @@ extension RegexComponent where Self == CharacterClass {
6966
}
7067

7168
public static var horizontalWhitespace: CharacterClass {
72-
.init(unconverted: .horizontalWhitespace)
69+
.init(unconverted: ._horizontalWhitespace)
7370
}
7471

7572
public static var newlineSequence: CharacterClass {
76-
.init(unconverted: .newlineSequence)
73+
.init(unconverted: ._newlineSequence)
7774
}
7875

7976
public static var verticalWhitespace: CharacterClass {
80-
.init(unconverted: .verticalWhitespace)
77+
.init(unconverted: ._verticalWhitespace)
8178
}
8279

8380
public static var word: CharacterClass {
84-
.init(unconverted: .word)
81+
.init(unconverted: ._word)
8582
}
8683
}
8784

Sources/_StringProcessing/Regex/DSLTree.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,32 @@ extension DSLTree {
740740
@_spi(RegexBuilder)
741741
public struct Atom {
742742
internal var ast: AST.Atom
743+
744+
// FIXME: The below APIs should be removed once the DSL tree has been
745+
// migrated to use proper DSL atoms for them.
746+
747+
public static var _anyGrapheme: Self {
748+
.init(ast: .init(.escaped(.graphemeCluster), .fake))
749+
}
750+
public static var _whitespace: Self {
751+
.init(ast: .init(.escaped(.whitespace), .fake))
752+
}
753+
public static var _digit: Self {
754+
.init(ast: .init(.escaped(.decimalDigit), .fake))
755+
}
756+
public static var _horizontalWhitespace: Self {
757+
.init(ast: .init(.escaped(.horizontalWhitespace), .fake))
758+
}
759+
public static var _newlineSequence: Self {
760+
// FIXME: newline sequence is not same as \n
761+
.init(ast: .init(.escaped(.newline), .fake))
762+
}
763+
public static var _verticalWhitespace: Self {
764+
.init(ast: .init(.escaped(.verticalTab), .fake))
765+
}
766+
public static var _word: Self {
767+
.init(ast: .init(.escaped(.wordCharacter), .fake))
768+
}
743769
}
744770
}
745771
}

Sources/_StringProcessing/_CharacterClassModel.swift

Lines changed: 15 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
// an AST, but this isn't a natural thing to produce in the context
1616
// of parsing or to store in an AST
1717

18-
@_spi(RegexBuilder)
19-
public struct _CharacterClassModel: Hashable {
18+
struct _CharacterClassModel: Hashable {
2019
/// The actual character class to match.
2120
var cc: Representation
2221

@@ -28,7 +27,7 @@ public struct _CharacterClassModel: Hashable {
2827
var isInverted: Bool = false
2928

3029
// TODO: Split out builtin character classes into their own type?
31-
public enum Representation: Hashable {
30+
enum Representation: Hashable {
3231
/// Any character
3332
case any
3433
/// Any grapheme cluster
@@ -85,7 +84,7 @@ public struct _CharacterClassModel: Hashable {
8584
}
8685

8786
/// Inverts a character class.
88-
public var inverted: Self {
87+
var inverted: Self {
8988
return withInversion(true)
9089
}
9190

@@ -161,51 +160,50 @@ public struct _CharacterClassModel: Hashable {
161160
}
162161
}
163162

164-
@_spi(RegexBuilder)
165163
extension _CharacterClassModel {
166-
public static var any: _CharacterClassModel {
164+
static var any: _CharacterClassModel {
167165
.init(cc: .any, matchLevel: .graphemeCluster)
168166
}
169167

170-
public static var anyGrapheme: _CharacterClassModel {
168+
static var anyGrapheme: _CharacterClassModel {
171169
.init(cc: .anyGrapheme, matchLevel: .graphemeCluster)
172170
}
173171

174-
public static var anyUnicodeScalar: _CharacterClassModel {
172+
static var anyUnicodeScalar: _CharacterClassModel {
175173
.init(cc: .any, matchLevel: .unicodeScalar)
176174
}
177175

178-
public static var whitespace: _CharacterClassModel {
176+
static var whitespace: _CharacterClassModel {
179177
.init(cc: .whitespace, matchLevel: .graphemeCluster)
180178
}
181179

182-
public static var digit: _CharacterClassModel {
180+
static var digit: _CharacterClassModel {
183181
.init(cc: .digit, matchLevel: .graphemeCluster)
184182
}
185183

186-
public static var hexDigit: _CharacterClassModel {
184+
static var hexDigit: _CharacterClassModel {
187185
.init(cc: .hexDigit, matchLevel: .graphemeCluster)
188186
}
189187

190-
public static var horizontalWhitespace: _CharacterClassModel {
188+
static var horizontalWhitespace: _CharacterClassModel {
191189
.init(cc: .horizontalWhitespace, matchLevel: .graphemeCluster)
192190
}
193191

194-
public static var newlineSequence: _CharacterClassModel {
192+
static var newlineSequence: _CharacterClassModel {
195193
.init(cc: .newlineSequence, matchLevel: .graphemeCluster)
196194
}
197195

198-
public static var verticalWhitespace: _CharacterClassModel {
196+
static var verticalWhitespace: _CharacterClassModel {
199197
.init(cc: .verticalWhitespace, matchLevel: .graphemeCluster)
200198
}
201199

202-
public static var word: _CharacterClassModel {
200+
static var word: _CharacterClassModel {
203201
.init(cc: .word, matchLevel: .graphemeCluster)
204202
}
205203
}
206204

207205
extension _CharacterClassModel.Representation: CustomStringConvertible {
208-
public var description: String {
206+
var description: String {
209207
switch self {
210208
case .any: return "<any>"
211209
case .anyGrapheme: return "<any grapheme>"
@@ -222,73 +220,11 @@ extension _CharacterClassModel.Representation: CustomStringConvertible {
222220
}
223221

224222
extension _CharacterClassModel: CustomStringConvertible {
225-
public var description: String {
223+
var description: String {
226224
return "\(isInverted ? "not " : "")\(cc)"
227225
}
228226
}
229227

230-
extension _CharacterClassModel {
231-
public func makeDSLTreeCharacterClass() -> DSLTree.CustomCharacterClass? {
232-
// FIXME: Implement in DSLTree instead of wrapping an AST atom
233-
switch makeAST() {
234-
case .atom(let atom):
235-
return .init(members: [.atom(.unconverted(.init(ast: atom)))])
236-
default:
237-
return nil
238-
}
239-
}
240-
241-
internal func makeAST() -> AST.Node? {
242-
let inv = isInverted
243-
244-
func esc(_ b: AST.Atom.EscapedBuiltin) -> AST.Node {
245-
escaped(b)
246-
}
247-
248-
switch cc {
249-
case .any: return atom(.any)
250-
251-
case .digit:
252-
return esc(inv ? .notDecimalDigit : .decimalDigit)
253-
254-
case .horizontalWhitespace:
255-
return esc(
256-
inv ? .notHorizontalWhitespace : .horizontalWhitespace)
257-
258-
// FIXME: newline sequence is not same as \n
259-
case .newlineSequence:
260-
return esc(inv ? .notNewline : .newline)
261-
262-
case .whitespace:
263-
return esc(inv ? .notWhitespace : .whitespace)
264-
265-
case .verticalWhitespace:
266-
return esc(inv ? .notVerticalTab : .verticalTab)
267-
268-
case .word:
269-
return esc(inv ? .notWordCharacter : .wordCharacter)
270-
271-
case .anyGrapheme:
272-
return esc(.graphemeCluster)
273-
274-
case .hexDigit:
275-
let members: [AST.CustomCharacterClass.Member] = [
276-
range_m(.char("a"), .char("f")),
277-
range_m(.char("A"), .char("F")),
278-
range_m(.char("0"), .char("9")),
279-
]
280-
let ccc = AST.CustomCharacterClass(
281-
.init(faking: inv ? .inverted : .normal),
282-
members,
283-
.fake)
284-
285-
return .customCharacterClass(ccc)
286-
287-
default: return nil
288-
}
289-
}
290-
}
291-
292228
extension _CharacterClassModel {
293229
func withMatchLevel(
294230
_ level: _CharacterClassModel.MatchLevel

0 commit comments

Comments
 (0)