Skip to content

Commit efe90d1

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

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

@@ -320,23 +323,31 @@ fileprivate extension Compiler.ByteCodeGen {
320323
builder.buildMatch(c, isCaseInsensitive: false)
321324
}
322325

323-
mutating func emitDot() {
324-
switch (options.semanticLevel, options.dotMatchesNewline) {
325-
case (.graphemeCluster, true):
326+
mutating func emitAny() {
327+
switch options.semanticLevel {
328+
case .graphemeCluster:
326329
builder.buildAdvance(1)
327-
case (.graphemeCluster, false):
330+
case .unicodeScalar:
331+
// TODO: builder.buildAdvanceUnicodeScalar(1)
328332
builder.buildConsume { input, bounds in
329-
input[bounds.lowerBound].isNewline
330-
? nil
331-
: input.index(after: bounds.lowerBound)
333+
input.unicodeScalars.index(after: bounds.lowerBound)
332334
}
335+
}
336+
}
333337

334-
case (.unicodeScalar, true):
335-
// TODO: builder.buildAdvanceUnicodeScalar(1)
338+
mutating func emitDot() {
339+
if options.dotMatchesNewline {
340+
emitAny()
341+
return
342+
}
343+
switch options.semanticLevel {
344+
case .graphemeCluster:
336345
builder.buildConsume { input, bounds in
337-
input.unicodeScalars.index(after: bounds.lowerBound)
346+
input[bounds.lowerBound].isNewline
347+
? nil
348+
: input.index(after: bounds.lowerBound)
338349
}
339-
case (.unicodeScalar, false):
350+
case .unicodeScalar:
340351
builder.buildConsume { input, bounds in
341352
input[bounds.lowerBound].isNewline
342353
? nil

Sources/_StringProcessing/ConsumerInterface.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ extension DSLTree.Atom {
128128
// can match a single scalar in scalar semantic mode.
129129
return try Character(s).generateConsumer(opts)
130130

131-
case .dot:
131+
case .any, .dot:
132132
// FIXME: Should this be a total ordering?
133133
if opts.semanticLevel == .graphemeCluster {
134134
return { input, bounds in

Sources/_StringProcessing/PrintAsPattern.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,9 @@ extension DSLTree.Atom {
11241124
_ printer: inout PrettyPrinter
11251125
) -> (String, canBeWrapped: Bool)? {
11261126
switch self {
1127+
case .any:
1128+
return (".any", true)
1129+
11271130
case .dot:
11281131
// FIXME: This is wrong, the DSL doesn't have an equivalent to .dot.
11291132
return (".any", true)
@@ -1167,6 +1170,9 @@ extension DSLTree.Atom {
11671170

11681171
var _regexBase: String {
11691172
switch self {
1173+
case .any:
1174+
return "(?s:.)"
1175+
11701176
case .dot:
11711177
return "."
11721178

Sources/_StringProcessing/Regex/DSLTree.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ extension DSLTree {
166166
case char(Character)
167167
case scalar(Unicode.Scalar)
168168

169+
/// Any character, including newlines.
170+
case any
171+
169172
/// The DSL representation of '.' in a regex literal. This does not match
170173
/// newlines unless single line mode is enabled.
171174
case dot
@@ -780,7 +783,8 @@ extension DSLTree.Atom {
780783
switch self {
781784
case .changeMatchingOptions, .assertion:
782785
return false
783-
case .char, .scalar, .dot, .backreference, .symbolicReference, .unconverted:
786+
case .char, .scalar, .any, .dot, .backreference, .symbolicReference,
787+
.unconverted:
784788
return true
785789
}
786790
}

0 commit comments

Comments
 (0)