Skip to content

Commit eacc954

Browse files
committed
Better diagnostic for expected escape
1 parent eefda78 commit eacc954

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

Sources/_MatchingEngine/Regex/Parse/Diagnostics.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum ParseError: Error, Hashable {
3131
case expectedASCII(Character)
3232

3333
case expectedNonEmptyContents
34+
case expectedEscape
3435

3536
case unknownGroupKind(String)
3637

@@ -70,6 +71,8 @@ extension ParseError: CustomStringConvertible {
7071
return s
7172
case .expectedNonEmptyContents:
7273
return "expected non-empty contents"
74+
case .expectedEscape:
75+
return "expected escape sequence"
7376
case let .unknownGroupKind(str):
7477
return "unknown group kind '(\(str)'"
7578
case let .invalidMatchingOption(c):

Sources/_MatchingEngine/Regex/Parse/LexicalAnalysis.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,18 @@ extension Source {
105105
/// Throws an unexpected end of input error if not matched
106106
///
107107
/// Note: much of the time, but not always, we can vend a more specific error.
108-
mutating func expectNonEmpty() throws {
108+
mutating func expectNonEmpty(
109+
_ error: ParseError = .unexpectedEndOfInput
110+
) throws {
109111
_ = try recordLoc { src in
110-
if src.isEmpty { throw ParseError.unexpectedEndOfInput }
112+
if src.isEmpty { throw error }
111113
}
112114
}
113115

114116
mutating func tryEatNonEmpty<C: Collection>(sequence c: C) throws -> Bool
115117
where C.Element == Char
116118
{
117-
_ = try recordLoc { src in
118-
guard !src.isEmpty else { throw ParseError.expected(String(c)) }
119-
}
119+
try expectNonEmpty(.expected(String(c)))
120120
return tryEat(sequence: c)
121121
}
122122

@@ -430,7 +430,7 @@ extension Source {
430430
// Ignore escapes if we're allowed to. lexUntil will consume the next
431431
// character.
432432
if ignoreEscaped, src.tryEat("\\") {
433-
try src.expectNonEmpty()
433+
try src.expectNonEmpty(.expectedEscape)
434434
}
435435
return false
436436
}.value

Tests/RegexTests/ParseTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,7 @@ extension RegexTests {
10931093
diagnosticTest("\\Qab\\", .expected("\\E"))
10941094
diagnosticTest(#""ab"#, .expected("\""), syntax: .experimental)
10951095
diagnosticTest(#""ab\""#, .expected("\""), syntax: .experimental)
1096+
diagnosticTest("\"ab\\", .expectedEscape, syntax: .experimental)
10961097

10971098
// MARK: Text Segment options
10981099

0 commit comments

Comments
 (0)