Skip to content

Work around compiler type checker issue #737

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 1 commit into from
Apr 23, 2024
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
29 changes: 15 additions & 14 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ let package = Package(
.executable(
name: "VariadicsGenerator",
targets: ["VariadicsGenerator"]),
.executable(
name: "RegexBenchmark",
targets: ["RegexBenchmark"])
// Disable to work around rdar://126877024
// .executable(
// name: "RegexBenchmark",
// targets: ["RegexBenchmark"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
Expand Down Expand Up @@ -142,17 +143,17 @@ let package = Package(
"_StringProcessing"
],
swiftSettings: [availabilityDefinition]),
.executableTarget(
name: "RegexBenchmark",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"_RegexParser",
"_StringProcessing",
"RegexBuilder"
],
swiftSettings: [
.unsafeFlags(["-Xfrontend", "-disable-availability-checking"]),
]),
// .executableTarget(
// name: "RegexBenchmark",
// dependencies: [
// .product(name: "ArgumentParser", package: "swift-argument-parser"),
// "_RegexParser",
// "_StringProcessing",
// "RegexBuilder"
// ],
// swiftSettings: [
// .unsafeFlags(["-Xfrontend", "-disable-availability-checking"]),
// ]),

// MARK: Exercises
.target(
Expand Down
257 changes: 121 additions & 136 deletions Tests/RegexBuilderTests/AnyRegexOutputTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,153 +5,137 @@ import RegexBuilder

private let enablePrinting = false

@available(SwiftStdlib 5.7, *)
extension RegexDSLTests {

func testContrivedAROExample() {
// Find and extract potential IDs. IDs are 8 bytes encoded as
// 16 hexadecimal numbers, with an optional `-` between every
// double-byte (i.e. 4 hex digits).
//
// AAAA-BBBB-CCCC-DDDD
// AAAABBBBCCCCDDDD
// AAAABBBBCCCC-DDDD
//
// IDs are converted to uppercase and hyphen separated
//
// The regex can have special capture names which affect replacement
// behavior
// - "salient": presented uppercase in square brackets after
// - "note": presented lowercase in parens
// - none: nothing
// - no captures: "<error>"
//
let input = """
Machine 1234-5678-90ab-CDEF connected
Session FEDCAB0987654321 ended
Artiface 0011deff-2231-abcd contrived
"""
let noCapOutput = """
Machine <error> connected
Session <error> ended
Artiface <error> contrived
"""
let unnamedOutput = """
Machine 1234-5678-90AB-CDEF connected
Session FEDC-AB09-8765-4321 ended
Artiface 0011-DEFF-2231-ABCD contrived
"""
let salientOutput = """
Machine 1234-5678-90AB-CDEF [5678] connected
Session FEDC-AB09-8765-4321 [AB09] ended
Artiface 0011-DEFF-2231-ABCD [DEFF] contrived
"""
let noteOutput = """
Machine 1234-5678-90AB-CDEF (5678) connected
Session FEDC-AB09-8765-4321 (ab09) ended
Artiface 0011-DEFF-2231-ABCD (deff) contrived
"""

enum Kind {
case none
case unnamed
case salient
case note

func contains(captureNamed s: String) -> Bool {
switch self {
case .none: return false
case .unnamed: return false
case .salient: return s == "salient"
case .note: return s == "note"
}
}

var expected: String {
switch self {
case .none: return """
Machine <error> connected
Session <error> ended
Artiface <error> contrived
"""
case .unnamed: return """
Machine 1234-5678-90AB-CDEF connected
Session FEDC-AB09-8765-4321 ended
Artiface 0011-DEFF-2231-ABCD contrived
"""
case .salient: return """
Machine 1234-5678-90AB-CDEF [5678] connected
Session FEDC-AB09-8765-4321 [AB09] ended
Artiface 0011-DEFF-2231-ABCD [DEFF] contrived
"""
case .note: return """
Machine 1234-5678-90AB-CDEF (5678) connected
Session FEDC-AB09-8765-4321 (ab09) ended
Artiface 0011-DEFF-2231-ABCD (deff) contrived
"""
}
}
// Find and extract potential IDs. IDs are 8 bytes encoded as
// 16 hexadecimal numbers, with an optional `-` between every
// double-byte (i.e. 4 hex digits).
//
// AAAA-BBBB-CCCC-DDDD
// AAAABBBBCCCCDDDD
// AAAABBBBCCCC-DDDD
//
// IDs are converted to uppercase and hyphen separated
//
// The regex can have special capture names which affect replacement
// behavior
// - "salient": presented uppercase in square brackets after
// - "note": presented lowercase in parens
// - none: nothing
// - no captures: "<error>"
fileprivate let input = """
Machine 1234-5678-90ab-CDEF connected
Session FEDCAB0987654321 ended
Artiface 0011deff-2231-abcd contrived
"""
fileprivate let noCapOutput = """
Machine <error> connected
Session <error> ended
Artiface <error> contrived
"""
fileprivate let unnamedOutput = """
Machine 1234-5678-90AB-CDEF connected
Session FEDC-AB09-8765-4321 ended
Artiface 0011-DEFF-2231-ABCD contrived
"""
fileprivate let salientOutput = """
Machine 1234-5678-90AB-CDEF [5678] connected
Session FEDC-AB09-8765-4321 [AB09] ended
Artiface 0011-DEFF-2231-ABCD [DEFF] contrived
"""
fileprivate let noteOutput = """
Machine 1234-5678-90AB-CDEF (5678) connected
Session FEDC-AB09-8765-4321 (ab09) ended
Artiface 0011-DEFF-2231-ABCD (deff) contrived
"""

fileprivate enum Kind {
case none
case unnamed
case salient
case note

func contains(captureNamed s: String) -> Bool {
switch self {
case .none: return false
case .unnamed: return false
case .salient: return s == "salient"
case .note: return s == "note"
}
}

func checkContains<Output>(
_ re: Regex<Output>, _ kind: Kind
) {
for name in ["", "salient", "note", "other"] {
XCTAssertEqual(
kind.contains(captureNamed: name), re.contains(captureNamed: name))
}
var expected: String {
switch self {
case .none: return noCapOutput
case .unnamed: return unnamedOutput
case .salient: return salientOutput
case .note: return noteOutput
}
func checkAROReplacing<Output>(
_ re: Regex<Output>, _ kind: Kind
) {
let aro = Regex<AnyRegexOutput>(re)
let output = input.replacing(aro) {
(match: Regex<AnyRegexOutput>.Match) -> String in

if match.count < 5 { return "<error>" }
}
}

let suffix: String
if re.contains(captureNamed: "salient") {
let body = match["salient"]!.substring?.uppercased() ?? "<no capture>"
suffix = " [\(body)]"
} else if re.contains(captureNamed: "note") {
let body = match["note"]!.substring?.lowercased() ?? "<no capture>"
suffix = " (\(body))"
} else {
suffix = ""
}
fileprivate func checkContains<Output>(
_ re: Regex<Output>, _ kind: Kind
) {
for name in ["", "salient", "note", "other"] {
XCTAssertEqual(
kind.contains(captureNamed: name), re.contains(captureNamed: name))
}
}

return match.output.dropFirst().lazy.map {
$0.substring!.uppercased()
}.joined(separator: "-") + suffix
}
fileprivate func checkAROReplacing<Output>(
_ re: Regex<Output>, _ kind: Kind
) {
let aro = Regex<AnyRegexOutput>(re)
let output = input.replacing(aro) {
(match: Regex<AnyRegexOutput>.Match) -> String in

if match.count < 5 { return "<error>" }

let suffix: String
if re.contains(captureNamed: "salient") {
let body = match["salient"]!.substring?.uppercased() ?? "<no capture>"
suffix = " [\(body)]"
} else if re.contains(captureNamed: "note") {
let body = match["note"]!.substring?.lowercased() ?? "<no capture>"
suffix = " (\(body))"
} else {
suffix = ""
}

XCTAssertEqual(output, kind.expected)
return match.output.dropFirst().lazy.map {
$0.substring!.uppercased()
}.joined(separator: "-") + suffix
}

if enablePrinting {
print("---")
print(output)
print(kind)
}
}
func check<Output>(
_ re: Regex<Output>, _ kind: Kind, _ expected: String
) {
let aro = Regex<AnyRegexOutput>(re)
XCTAssertEqual(output, kind.expected)

let casted = try! XCTUnwrap(Regex(aro, as: Output.self))
if enablePrinting {
print("---")
print(output)
print(kind)
}
}

// contains(captureNamed:)
checkContains(re, kind)
checkContains(aro, kind)
checkContains(casted, kind)
fileprivate func check<Output>(
_ re: Regex<Output>, _ kind: Kind, _ expected: String
) {
let aro = Regex<AnyRegexOutput>(re)
let casted = try! XCTUnwrap(Regex(aro, as: Output.self))

// contains(captureNamed:)
checkContains(re, kind)
checkContains(aro, kind)
checkContains(casted, kind)

// replacing
checkAROReplacing(re, kind)
checkAROReplacing(aro, kind)
checkAROReplacing(casted, kind)
}

// replacing
checkAROReplacing(re, kind)
checkAROReplacing(aro, kind)
checkAROReplacing(casted, kind)
}
@available(SwiftStdlib 5.7, *)
extension RegexDSLTests {

func testContrivedAROExample() {
// Literals (mocked up via explicit `as` types)
check(try! Regex(#"""
(?x)
Expand All @@ -161,6 +145,7 @@ extension RegexDSLTests {
.none,
noCapOutput
)

check(try! Regex(#"""
(?x)
(\p{hexdigit}{4}) -? (\p{hexdigit}{4}) -?
Expand Down