Skip to content

Commit cebf4a6

Browse files
committed
Fix crash on lone backslash
1 parent 4dab8d8 commit cebf4a6

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Sources/_MatchingEngine/Regex/Parse/LexicalAnalysis.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,9 @@ extension Source {
14721472
return ref
14731473
}
14741474

1475-
let char = src.eat()
1475+
guard let char = src.tryEat() else {
1476+
throw ParseError.expectedEscape
1477+
}
14761478

14771479
// Single-character builtins.
14781480
if let builtin = AST.Atom.EscapedBuiltin(

Sources/_MatchingEngine/Regex/Parse/Source.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ extension Source {
8686
tryEat(anyOf: set)
8787
}
8888

89+
/// Try to eat any character, returning `nil` if the input has been exhausted.
90+
mutating func tryEat() -> Char? {
91+
guard !isEmpty else { return nil }
92+
return eat()
93+
}
94+
8995
mutating func eat(asserting c: Char) {
9096
assert(peek() == c)
9197
advance()

Tests/RegexTests/ParseTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,10 @@ extension RegexTests {
17531753
diagnosticTest("(?<a-b", .expected(">"))
17541754
diagnosticTest("(?<a-b>", .expected(")"))
17551755

1756+
// MARK: Bad escapes
1757+
1758+
diagnosticTest("\\", .expectedEscape)
1759+
17561760
// MARK: Text Segment options
17571761

17581762
diagnosticTest("(?-y{g})", .cannotRemoveTextSegmentOptions)

0 commit comments

Comments
 (0)