Skip to content

Provide literal pattern for convertible regexes #678

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

Closed
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
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ let availabilityDefinition = PackageDescription.SwiftSetting.unsafeFlags([
"-Xfrontend",
"-define-availability",
"-Xfrontend",
"SwiftStdlib 5.8:macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
"SwiftStdlib 5.8:macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4",
"-Xfrontend",
"-define-availability",
"-Xfrontend",
"SwiftStdlib 5.9:macOS 9999, iOS 9999, watchOS 9999, tvOS 9999",
])

/// Swift settings for building a private stdlib-like module that is to be used
Expand Down
6 changes: 3 additions & 3 deletions Sources/RegexBuilder/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ extension _RegexFactory {
_ left: some RegexComponent,
ignoringOutputTypeOf right: some RegexComponent
) -> Regex<Output> {
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
if #available(macOS 14.0, iOS 9999, watchOS 9999, tvOS 9999, *) {
return accumulate(left, ignoreCapturesInTypedOutput(right))
}
return accumulate(left, right)
Expand All @@ -549,7 +549,7 @@ extension _RegexFactory {
ignoringOutputTypeOf left: some RegexComponent,
_ right: some RegexComponent
) -> Regex<Output> {
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
if #available(macOS 14.0, iOS 9999, watchOS 9999, tvOS 9999, *) {
return accumulate(ignoreCapturesInTypedOutput(left), right)
}
return accumulate(left, right)
Expand All @@ -563,7 +563,7 @@ extension _RegexFactory {
ignoringOutputTypeOf left: some RegexComponent,
andAlso right: some RegexComponent
) -> Regex<Output> {
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
if #available(macOS 14.0, iOS 9999, watchOS 9999, tvOS 9999, *) {
return accumulate(
ignoreCapturesInTypedOutput(left), ignoreCapturesInTypedOutput(right))
}
Expand Down
4 changes: 1 addition & 3 deletions Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2091,9 +2091,7 @@ extension Parser {
// multiple scalars. These may be confusable for metacharacters, e.g
// `[\u{301}]` wouldn't be interpreted as a custom character class due
// to the combining accent (assuming it is literal, not `\u{...}`).
let scalars = char.unicodeScalars
if scalars.count > 1 && scalars.first!.isASCII && char != "\r\n" &&
!char.isLetter && !char.isNumber {
if char.isConfusable {
p.error(.confusableCharacter(char), at: charLoc.location)
}
break
Expand Down
13 changes: 13 additions & 0 deletions Sources/_RegexParser/Utility/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ extension Character {
let str = String(self)
return str._nfcCodeUnits.elementsEqual(str.utf8)
}

/// Whether this character could be confusable with a metacharacter in a
/// regex literal.
///
/// A "confusable" character is one that starts with a non-alphanumeric ASCII
/// character and includes other combining Unicode scalars. For example,
/// `"[́"` (aka `"[\u{301}"`) is confusable, since it looks just like the
/// `"["` metacharacter, but doesn't parse as one.
public var isConfusable: Bool {
let scalars = self.unicodeScalars
return scalars.count > 1 && scalars.first!.isASCII && self != "\r\n" &&
!self.isLetter && !self.isNumber
}
}

extension CustomStringConvertible {
Expand Down
1 change: 1 addition & 0 deletions Sources/_StringProcessing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ add_library(_StringProcessing
Compiler.swift
ConsumerInterface.swift
Executor.swift
LiteralPrinter.swift
MatchingOptions.swift
PrintAsPattern.swift)
target_compile_options(_StringProcessing PRIVATE
Expand Down
Loading