Skip to content

Commit 8f8c7d0

Browse files
committed
Fix CharacterClass.any
This should map to `.any`, not `.dot`. rdar://96509234
1 parent efe90d1 commit 8f8c7d0

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

Sources/RegexBuilder/CharacterClass.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extension CharacterClass {
4242
@available(SwiftStdlib 5.7, *)
4343
extension RegexComponent where Self == CharacterClass {
4444
public static var any: CharacterClass {
45-
.init(DSLTree.CustomCharacterClass(members: [.atom(.dot)]))
45+
.init(DSLTree.CustomCharacterClass(members: [.atom(.any)]))
4646
}
4747

4848
public static var anyGraphemeCluster: CharacterClass {

Sources/_StringProcessing/ConsumerInterface.swift

Lines changed: 4 additions & 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 .any, .dot:
131+
case .any:
132132
// FIXME: Should this be a total ordering?
133133
if opts.semanticLevel == .graphemeCluster {
134134
return { input, bounds in
@@ -140,6 +140,9 @@ extension DSLTree.Atom {
140140
}
141141
}
142142

143+
case .dot:
144+
throw Unreachable(".atom(.dot) should be handled by emitDot")
145+
143146
case .assertion:
144147
// TODO: We could handle, should this be total?
145148
return nil

Sources/_StringProcessing/PrintAsPattern.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,8 @@ extension AST.Atom {
920920
return (" /* TODO: named character */", false)
921921

922922
case .dot:
923-
// FIXME: This is wrong, the DSL doesn't have an equivalent to .dot.
924-
return (".any", true)
923+
// The DSL does not have an equivalent to '.', print as a regex.
924+
return ("/./", false)
925925

926926
case .startOfLine, .endOfLine:
927927
fatalError("unreachable")
@@ -1128,8 +1128,8 @@ extension DSLTree.Atom {
11281128
return (".any", true)
11291129

11301130
case .dot:
1131-
// FIXME: This is wrong, the DSL doesn't have an equivalent to .dot.
1132-
return (".any", true)
1131+
// The DSL does not have an equivalent to '.', print as a regex.
1132+
return ("/./", false)
11331133

11341134
case let .char(c):
11351135
return (String(c)._quoted, false)

Tests/RegexBuilderTests/RegexDSLTests.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class RegexDSLTests: XCTestCase {
7373
XCTAssertTrue(match.output == substringMatch.output)
7474
}
7575

76+
let allNewlines = "\u{A}\u{B}\u{C}\u{D}\r\n\u{85}\u{2028}\u{2029}"
77+
let asciiNewlines = "\u{A}\u{B}\u{C}\u{D}\r\n"
78+
7679
func testCharacterClasses() throws {
7780
try _testDSLCaptures(
7881
("a c", ("a c", " ", "c")),
@@ -115,9 +118,6 @@ class RegexDSLTests: XCTestCase {
115118
}
116119
}
117120

118-
let allNewlines = "\u{A}\u{B}\u{C}\u{D}\r\n\u{85}\u{2028}\u{2029}"
119-
let asciiNewlines = "\u{A}\u{B}\u{C}\u{D}\r\n"
120-
121121
// `.newlineSequence` and `.verticalWhitespace` match the same set of
122122
// newlines in grapheme semantic mode, and scalar mode when applied with
123123
// OneOrMore.
@@ -247,6 +247,20 @@ class RegexDSLTests: XCTestCase {
247247
}
248248
}
249249

250+
func testAny() throws {
251+
// .any matches newlines regardless of matching options.
252+
for dotMatchesNewline in [true, false] {
253+
try _testDSLCaptures(
254+
("abc\(allNewlines)def", "abc\(allNewlines)def"),
255+
matchType: Substring.self, ==)
256+
{
257+
Regex {
258+
OneOrMore(.any)
259+
}.dotMatchesNewlines(dotMatchesNewline)
260+
}
261+
}
262+
}
263+
250264
func testMatchResultDotZeroWithoutCapture() throws {
251265
let match = try XCTUnwrap("aaa".wholeMatch { OneOrMore { "a" } })
252266
XCTAssertEqual(match.0, "aaa")

Tests/RegexTests/RenderDSLTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ extension RenderDSLTests {
6868
}
6969
""")
7070
}
71+
72+
func testDot() throws {
73+
try testConversion(#".+"#, #"""
74+
Regex {
75+
OneOrMore {
76+
/./
77+
}
78+
}
79+
"""#)
80+
try testConversion(#"a.c"#, #"""
81+
Regex {
82+
"a"
83+
/./
84+
"c"
85+
}
86+
"""#)
87+
}
7188

7289
func testOptions() throws {
7390
try XCTExpectFailure("Options like '(?i)' aren't converted") {

0 commit comments

Comments
 (0)