Skip to content

Commit 58f2a03

Browse files
committed
Tweak libswiftLexRegexLiteral interface
Update so it would have the ability to better recover from lexing errors in the future.
1 parent b7b36cd commit 58f2a03

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

Sources/_MatchingEngine/Regex/Parse/Mocking.swift

+32-11
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,50 @@ private func copyCString(_ str: String) -> UnsafePointer<CChar> {
150150

151151
/// Interface for libswift.
152152
///
153-
/// Lex a regular expression literal starting at `inputPtr`, making sure not to
154-
/// lex past `bufferEndPtr`. The pointer at which to resume lexing is returned,
155-
/// or nil if this is not a regex literal. The `errOut` parameter will be set
156-
/// if an error is encountered.
153+
/// Attempt to lex a regex literal string.
154+
///
155+
/// - Parameters:
156+
/// - CurPtrPtr: A pointer to the current pointer of lexer, which should be
157+
/// the start of the literal. This will be advanced to the point
158+
/// at which the lexer should resume, or will remain the same if
159+
/// this is not a regex literal.
160+
/// - BufferEnd: A pointer to the end of the buffer, which should not be lexed
161+
/// past.
162+
/// - ErrorOut: If an error is encountered, this will be set to the error
163+
/// string.
164+
///
165+
/// - Returns: A bool indicating whether lexing was completely erroneous, and
166+
/// cannot be recovered from, or false if there either was no error,
167+
/// or there was a recoverable error.
157168
func libswiftLexRegexLiteral(
158-
_ inputPtr: UnsafePointer<CChar>?,
169+
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>?>?,
159170
_ bufferEndPtr: UnsafePointer<CChar>?,
160171
_ errOut: UnsafeMutablePointer<UnsafePointer<CChar>?>?
161-
) -> UnsafePointer<CChar>? {
162-
guard let inputPtr = inputPtr, let endPtr = bufferEndPtr else { return nil }
172+
) -> /*CompletelyErroneous*/ CBool {
173+
guard let curPtrPtr = curPtrPtr, let inputPtr = curPtrPtr.pointee,
174+
let bufferEndPtr = bufferEndPtr
175+
else {
176+
fatalError("Expected lexing pointers")
177+
}
163178
guard let errOut = errOut else { fatalError("Expected error out param") }
164179

165180
do {
166-
let (_, _, endPtr) = try lexRegex(start: inputPtr, end: endPtr)
167-
return endPtr.assumingMemoryBound(to: CChar.self)
181+
let (_, _, endPtr) = try lexRegex(start: inputPtr, end: bufferEndPtr)
182+
curPtrPtr.pointee = endPtr.assumingMemoryBound(to: CChar.self)
183+
return false
168184
} catch let error as LexError {
169185
if error.kind == .unknownDelimiter {
170186
// An unknown delimiter should be recovered from, as we may want to try
171187
// lex something else.
172-
return nil
188+
return false
173189
}
174190
errOut.pointee = copyCString("\(error)")
175-
return error.resumePtr.assumingMemoryBound(to: CChar.self)
191+
curPtrPtr.pointee = error.resumePtr.assumingMemoryBound(to: CChar.self)
192+
193+
// For now, treat every error as unrecoverable.
194+
// TODO: We should ideally be able to recover from a regex with missing
195+
// closing delimiters, which would help with code completion.
196+
return true
176197
} catch {
177198
fatalError("Should be a LexError")
178199
}

0 commit comments

Comments
 (0)