Skip to content

Commit da59c30

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

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
@@ -111,7 +111,7 @@ extension DSLTree.Atom {
111111
: $0 == s
112112
}
113113

114-
case .any, .dot:
114+
case .any:
115115
// FIXME: Should this be a total ordering?
116116
if opts.semanticLevel == .graphemeCluster {
117117
return { input, bounds in
@@ -123,6 +123,9 @@ extension DSLTree.Atom {
123123
}
124124
}
125125

126+
case .dot:
127+
throw Unreachable(".atom(.dot) should be handled by emitDot")
128+
126129
case .assertion:
127130
// TODO: We could handle, should this be total?
128131
return nil

Sources/_StringProcessing/PrintAsPattern.swift

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

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

902902
case .startOfLine, .endOfLine:
903903
fatalError("unreachable")
@@ -1104,8 +1104,8 @@ extension DSLTree.Atom {
11041104
return (".any", true)
11051105

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

11101110
case let .char(c):
11111111
return (String(c)._quoted, false)

Tests/RegexBuilderTests/RegexDSLTests.swift

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

72+
let allNewlines = "\u{A}\u{B}\u{C}\u{D}\r\n\u{85}\u{2028}\u{2029}"
73+
let asciiNewlines = "\u{A}\u{B}\u{C}\u{D}\r\n"
74+
7275
func testCharacterClasses() throws {
7376
try _testDSLCaptures(
7477
("a c", ("a c", " ", "c")),
@@ -111,9 +114,6 @@ class RegexDSLTests: XCTestCase {
111114
}
112115
}
113116

114-
let allNewlines = "\u{A}\u{B}\u{C}\u{D}\r\n\u{85}\u{2028}\u{2029}"
115-
let asciiNewlines = "\u{A}\u{B}\u{C}\u{D}\r\n"
116-
117117
// `.newlineSequence` and `.verticalWhitespace` match the same set of
118118
// newlines in grapheme semantic mode, and scalar mode when applied with
119119
// OneOrMore.
@@ -243,6 +243,20 @@ class RegexDSLTests: XCTestCase {
243243
}
244244
}
245245

246+
func testAny() throws {
247+
// .any matches newlines regardless of matching options.
248+
for dotMatchesNewline in [true, false] {
249+
try _testDSLCaptures(
250+
("abc\(allNewlines)def", "abc\(allNewlines)def"),
251+
matchType: Substring.self, ==)
252+
{
253+
Regex {
254+
OneOrMore(.any)
255+
}.dotMatchesNewlines(dotMatchesNewline)
256+
}
257+
}
258+
}
259+
246260
func testMatchResultDotZeroWithoutCapture() throws {
247261
let match = try XCTUnwrap("aaa".wholeMatch { OneOrMore { "a" } })
248262
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)