Skip to content

Commit 4254dd8

Browse files
committed
Add Source.tryEatWithLoc
1 parent 7f3ee1f commit 4254dd8

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Sources/_MatchingEngine/Regex/Parse/LexicalAnalysis.swift

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ extension Source {
140140
return result
141141
}
142142

143+
/// Attempt to eat the given character, returning its source location if
144+
/// successful, `nil` otherwise.
145+
mutating func tryEatWithLoc(_ c: Character) -> SourceLocation? {
146+
let start = currentPosition
147+
guard tryEat(c) else { return nil }
148+
return .init(start ..< currentPosition)
149+
}
150+
143151
/// Throws an expected ASCII character error if not matched
144152
mutating func expectASCII() throws -> Located<Character> {
145153
try recordLoc { src in
@@ -1475,15 +1483,13 @@ extension Source {
14751483
) throws -> (dashLoc: SourceLocation, AST.Atom)? {
14761484
// Make sure we don't have a binary operator e.g '--', and the '-' is not
14771485
// ending the custom character class (in which case it is literal).
1478-
let start = currentPosition
1479-
guard peekCCBinOp() == nil && !starts(with: "-]") && tryEat("-") else {
1480-
return nil
1481-
}
1482-
let dashLoc = Location(start ..< currentPosition)
1483-
guard let end = try lexAtom(context: context) else {
1486+
guard peekCCBinOp() == nil, !starts(with: "-]"),
1487+
let dash = tryEatWithLoc("-"),
1488+
let end = try lexAtom(context: context)
1489+
else {
14841490
return nil
14851491
}
1486-
return (dashLoc, end)
1492+
return (dash, end)
14871493
}
14881494
}
14891495

0 commit comments

Comments
 (0)