Skip to content

[DRAFT] Printable regex #667

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
wants to merge 1 commit into from
Closed
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
127 changes: 127 additions & 0 deletions Sources/_StringProcessing/Regex/TextualRegex.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

@_implementationOnly import _RegexParser

@available(macOS 9999, *) // TODO: 5.9?
public struct PrintableRegex<Output>: RegexComponent, @unchecked Sendable
{
public var regex: Regex<Output>

internal var text: String

public init?(_ re: Regex<Output>) {
guard case .convertedRegexLiteral(_, let ast) = re.program.tree.root
else {
return nil
}
self.regex = re
let str = ast.ast.renderAsCanonical()
self.text = str
}
}


@available(macOS 9999, *) // TODO: 5.9?
extension PrintableRegex: Hashable {
public static func == (
lhs: PrintableRegex<Output>, rhs: PrintableRegex<Output>
) -> Bool {
lhs.text == rhs.text
}
public func hash(into hasher: inout Hasher) {
text.hash(into: &hasher)
}
}

// FIXME: Do we only work with ARO as the output type?

@available(macOS 9999, *) // TODO: 5.9?
extension PrintableRegex: Codable {
private init(coding re: Regex<Output>) throws {
guard case .convertedRegexLiteral = re.program.tree.root else {
throw DecodingError.error
}
self.regex = re
fatalError()
}

private enum DecodingError: Error {
case error
}
enum CodingKeys: CodingKey {
case string
}

public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let str = try values.decode(String.self, forKey: .string)
guard let pre = try PrintableRegex(Regex(str)) else {
throw DecodingError.error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on my recent proposal experience, this should probably just be the standard DecodingError.typeMismatch. That gets used when a value doesn't decode into the expected type (e.g. an invalid URL).

}
self = pre
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(text, forKey: .string)
}
}

// MARK: - mock API

@available(macOS 9999, *) // TODO: 5.9?
extension Regex {
public var printable: PrintableRegex<Output>? { .init(self) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to kill two birds with one stone and also provide something like this at the same time?

public var stringRepresentation: String? { PrintableRegex(self).text }

}

@available(macOS 9999, *) // TODO: 5.9?
public enum RegexSyntax {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be one of those enum-wrapping structs, since it definitely isn't a closed set.

case swift
case pcre2
case sql
case icu
// ...
}

@available(macOS 9999, *) // TODO: 5.9?
extension PrintableRegex {
public func print(using syntax: RegexSyntax = .swift) -> String {
switch syntax {
case .swift: return text
default: fatalError()
}
}
}

@available(macOS 9999, *) // TODO: 5.9?
extension PrintableRegex: CustomStringConvertible, CustomDebugStringConvertible {
public var description: String {
text
}

public var debugDescription: String {
"/\(text)/"
}
}


@available(macOS 9999, *) // TODO: 5.9?
extension PrintableRegex: LosslessStringConvertible {
public init?(_ description: String) {
guard let re = try? Regex<Output>(description) else {
return nil
}
self.init(re)
}


}