Skip to content

Commit 6f7ab96

Browse files
committed
Throw error if we encounter stray opening '('
This should be unreachable, let's make sure of that. Doing so requires generalizing the handling of LocatedError a bit.
1 parent 63ab0a9 commit 6f7ab96

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ API convention:
2121
- eat() and tryEat() is still used by the parser as a character-by-character interface
2222
*/
2323

24+
extension Error {
25+
func addingLocation(_ loc: Range<Source.Position>) -> Error {
26+
// If we're already a LocatedError, don't change the location.
27+
if self is _LocatedErrorProtocol {
28+
return self
29+
}
30+
return Source.LocatedError<Self>(self, loc)
31+
}
32+
}
33+
2434
extension Source {
2535
// MARK: - recordLoc
2636

@@ -51,12 +61,8 @@ extension Source {
5161
do {
5262
guard let result = try f(&self) else { return nil }
5363
return Located(result, start..<currentPosition)
54-
} catch let e as Source.LocatedError<ParseError> {
55-
throw e
56-
} catch let e as ParseError {
57-
throw LocatedError(e, start..<currentPosition)
58-
} catch {
59-
fatalError("FIXME: Let's not keep the boxed existential...")
64+
} catch let e {
65+
throw e.addingLocation(start..<currentPosition)
6066
}
6167
}
6268

@@ -1841,6 +1847,9 @@ extension Source {
18411847
}
18421848
throw Unreachable("TODO: reason")
18431849

1850+
case "(" where !customCC:
1851+
throw Unreachable("Should have lexed a group or group-like atom")
1852+
18441853
// (sometimes) special metacharacters
18451854
case ".": return customCC ? .char(".") : .any
18461855
case "^": return customCC ? .char("^") : .startOfLine

Sources/_RegexParser/Regex/Parse/SourceLocation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ extension Source {
5656
var currentPosition: Position { bounds.lowerBound }
5757
}
5858

59+
protocol _LocatedErrorProtocol: Error {}
60+
5961
extension Source {
6062
/// An error with source location info
61-
public struct LocatedError<E: Error>: Error {
63+
public struct LocatedError<E: Error>: Error, _LocatedErrorProtocol {
6264
public let error: E
6365
public let location: SourceLocation
6466

@@ -70,7 +72,6 @@ extension Source {
7072
self.error = v
7173
self.location = Location(r)
7274
}
73-
7475
}
7576

7677
/// Located value: a value wrapped with a source range

0 commit comments

Comments
 (0)