Skip to content

Tweak libswiftLexRegexLiteral interface #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions Sources/_MatchingEngine/Regex/Parse/Mocking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,50 @@ private func copyCString(_ str: String) -> UnsafePointer<CChar> {

/// Interface for libswift.
///
/// Lex a regular expression literal starting at `inputPtr`, making sure not to
/// lex past `bufferEndPtr`. The pointer at which to resume lexing is returned,
/// or nil if this is not a regex literal. The `errOut` parameter will be set
/// if an error is encountered.
/// Attempt to lex a regex literal string.
///
/// - Parameters:
/// - CurPtrPtr: A pointer to the current pointer of lexer, which should be
/// the start of the literal. This will be advanced to the point
/// at which the lexer should resume, or will remain the same if
/// this is not a regex literal.
/// - BufferEnd: A pointer to the end of the buffer, which should not be lexed
/// past.
/// - ErrorOut: If an error is encountered, this will be set to the error
/// string.
///
/// - Returns: A bool indicating whether lexing was completely erroneous, and
/// cannot be recovered from, or false if there either was no error,
/// or there was a recoverable error.
func libswiftLexRegexLiteral(
_ inputPtr: UnsafePointer<CChar>?,
_ curPtrPtr: UnsafeMutablePointer<UnsafePointer<CChar>?>?,
_ bufferEndPtr: UnsafePointer<CChar>?,
_ errOut: UnsafeMutablePointer<UnsafePointer<CChar>?>?
) -> UnsafePointer<CChar>? {
guard let inputPtr = inputPtr, let endPtr = bufferEndPtr else { return nil }
) -> /*CompletelyErroneous*/ CBool {
guard let curPtrPtr = curPtrPtr, let inputPtr = curPtrPtr.pointee,
let bufferEndPtr = bufferEndPtr
else {
fatalError("Expected lexing pointers")
}
guard let errOut = errOut else { fatalError("Expected error out param") }

do {
let (_, _, endPtr) = try lexRegex(start: inputPtr, end: endPtr)
return endPtr.assumingMemoryBound(to: CChar.self)
let (_, _, endPtr) = try lexRegex(start: inputPtr, end: bufferEndPtr)
curPtrPtr.pointee = endPtr.assumingMemoryBound(to: CChar.self)
return false
} catch let error as LexError {
if error.kind == .unknownDelimiter {
// An unknown delimiter should be recovered from, as we may want to try
// lex something else.
return nil
return false
}
errOut.pointee = copyCString("\(error)")
return error.resumePtr.assumingMemoryBound(to: CChar.self)
curPtrPtr.pointee = error.resumePtr.assumingMemoryBound(to: CChar.self)

// For now, treat every error as unrecoverable.
// TODO: We should ideally be able to recover from a regex with missing
// closing delimiters, which would help with code completion.
return true
} catch {
fatalError("Should be a LexError")
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/_StringProcessing/ConsumerInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,27 +354,27 @@ extension Unicode.BinaryProperty {
case .diacratic: // spelling?
return consumeScalarProp(\.isDiacritic)
case .emojiModifierBase:
if #available(macOS 10.12.2, *) {
if #available(macOS 10.12.2, iOS 10.2, tvOS 10.1, watchOS 3.1.1, *) {
return consumeScalarProp(\.isEmojiModifierBase)
} else {
throw unsupported("isEmojiModifierBase on old OSes")
}
case .emojiComponent:
break
case .emojiModifier:
if #available(macOS 10.12.2, *) {
if #available(macOS 10.12.2, iOS 10.2, tvOS 10.1, watchOS 3.1.1, *) {
return consumeScalarProp(\.isEmojiModifier)
} else {
throw unsupported("isEmojiModifier on old OSes")
}
case .emoji:
if #available(macOS 10.12.2, *) {
if #available(macOS 10.12.2, iOS 10.2, tvOS 10.1, watchOS 3.1.1, *) {
return consumeScalarProp(\.isEmoji)
} else {
throw unsupported("isEmoji on old OSes")
}
case .emojiPresentation:
if #available(macOS 10.12.2, *) {
if #available(macOS 10.12.2, iOS 10.2, tvOS 10.1, watchOS 3.1.1, *) {
return consumeScalarProp(\.isEmojiPresentation)
} else {
throw unsupported("isEmojiPresentation on old OSes")
Expand Down