Skip to content

Commit d6a03a0

Browse files
committed
Re-introduce DSLTree.Atom.any
This time as a "true any" that matches any character, including newlines.
1 parent 8e920c9 commit d6a03a0

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

Sources/_StringProcessing/ByteCodeGen.swift

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ fileprivate extension Compiler.ByteCodeGen {
5555
}
5656
}
5757
switch a {
58+
case .any:
59+
emitAny()
60+
5861
case .dot:
5962
emitDot()
6063

@@ -282,23 +285,31 @@ fileprivate extension Compiler.ByteCodeGen {
282285
}
283286
}
284287

285-
mutating func emitDot() {
286-
switch (options.semanticLevel, options.dotMatchesNewline) {
287-
case (.graphemeCluster, true):
288+
mutating func emitAny() {
289+
switch options.semanticLevel {
290+
case .graphemeCluster:
288291
builder.buildAdvance(1)
289-
case (.graphemeCluster, false):
292+
case .unicodeScalar:
293+
// TODO: builder.buildAdvanceUnicodeScalar(1)
290294
builder.buildConsume { input, bounds in
291-
input[bounds.lowerBound].isNewline
292-
? nil
293-
: input.index(after: bounds.lowerBound)
295+
input.unicodeScalars.index(after: bounds.lowerBound)
294296
}
297+
}
298+
}
295299

296-
case (.unicodeScalar, true):
297-
// TODO: builder.buildAdvanceUnicodeScalar(1)
300+
mutating func emitDot() {
301+
if options.dotMatchesNewline {
302+
emitAny()
303+
return
304+
}
305+
switch options.semanticLevel {
306+
case .graphemeCluster:
298307
builder.buildConsume { input, bounds in
299-
input.unicodeScalars.index(after: bounds.lowerBound)
308+
input[bounds.lowerBound].isNewline
309+
? nil
310+
: input.index(after: bounds.lowerBound)
300311
}
301-
case (.unicodeScalar, false):
312+
case .unicodeScalar:
302313
builder.buildConsume { input, bounds in
303314
input[bounds.lowerBound].isNewline
304315
? nil

Sources/_StringProcessing/ConsumerInterface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ extension DSLTree.Atom {
111111
: $0 == s
112112
}
113113

114-
case .dot:
114+
case .any, .dot:
115115
// FIXME: Should this be a total ordering?
116116
if opts.semanticLevel == .graphemeCluster {
117117
return { input, bounds in

Sources/_StringProcessing/PrintAsPattern.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,9 @@ extension DSLTree.Atom {
11001100
_ printer: inout PrettyPrinter
11011101
) -> (String, canBeWrapped: Bool)? {
11021102
switch self {
1103+
case .any:
1104+
return (".any", true)
1105+
11031106
case .dot:
11041107
// FIXME: This is wrong, the DSL doesn't have an equivalent to .dot.
11051108
return (".any", true)
@@ -1143,6 +1146,9 @@ extension DSLTree.Atom {
11431146

11441147
var _regexBase: String {
11451148
switch self {
1149+
case .any:
1150+
return "(?s:.)"
1151+
11461152
case .dot:
11471153
return "."
11481154

Sources/_StringProcessing/Regex/DSLTree.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ extension DSLTree {
246246
case char(Character)
247247
case scalar(Unicode.Scalar)
248248

249+
/// Any character, including newlines.
250+
case any
251+
249252
/// The DSL representation of '.' in a regex literal. This does not match
250253
/// newlines unless single line mode is enabled.
251254
case dot
@@ -860,7 +863,8 @@ extension DSLTree.Atom {
860863
switch self {
861864
case .changeMatchingOptions, .assertion:
862865
return false
863-
case .char, .scalar, .dot, .backreference, .symbolicReference, .unconverted:
866+
case .char, .scalar, .any, .dot, .backreference, .symbolicReference,
867+
.unconverted:
864868
return true
865869
}
866870
}

0 commit comments

Comments
 (0)