mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
With `-enable-experimental-string-processing`, start lexing `'` delimiters as regex literals (this is just a placeholder delimiter for now). The contents of which gets passed to the libswift library, which can return an error string to be emitted, or null for success. The libswift side isn't yet hooked up to the Swift regex parser, so for now just emit a dummy diagnostic for regexes starting with quantifiers. If successful, build an AST node which will be emitted as an implicit call to an `init(_regexString:)` initializer of an in-scope `Regex` decl (which will eventually be a known stdlib decl).
28 lines
784 B
Swift
28 lines
784 B
Swift
import ExperimentalRegexBridging
|
|
|
|
public func experimental_regex_strawperson(
|
|
_ ptr: UnsafePointer<CChar>?
|
|
) -> UnsafePointer<CChar>? {
|
|
guard let s = ptr else { return nil }
|
|
|
|
func error(_ str: String) -> UnsafePointer<CChar>? {
|
|
let count = str.utf8.count + 1
|
|
return str.withCString {
|
|
assert($0[count-1] == 0)
|
|
let ptr = UnsafeMutablePointer<CChar>.allocate(capacity: count)
|
|
ptr.initialize(from: $0, count: count)
|
|
return UnsafePointer(ptr)
|
|
}
|
|
}
|
|
|
|
let str = String(cString: s)
|
|
if str.starts(with: "*") || str.starts(with: "+") || str.starts(with: "?") {
|
|
return error("cannot start regex with quantifier")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
public func registerParser() {
|
|
Parser_registerParseRegexStrawperson({ experimental_regex_strawperson($0) })
|
|
}
|