Files
swift-mirror/libswift/Sources/ExperimentalRegex/Regex.swift
Hamish Knight 37f16520e6 Prototype regex literal AST and emission
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).
2021-12-06 21:16:14 +00:00

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) })
}