Skip to content

Commit 4bea391

Browse files
committed
Define primary associated type for RegexComponent.
1 parent 39cb22d commit 4bea391

File tree

4 files changed

+59
-34
lines changed

4 files changed

+59
-34
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ let package = Package(
4444
dependencies: ["_RegexParser", "_CUnicode"],
4545
swiftSettings: [
4646
.unsafeFlags(["-enable-library-evolution"]),
47+
.unsafeFlags(["-Xfrontend", "-enable-parameterized-protocol-types"])
4748
]),
4849
.target(
4950
name: "RegexBuilder",

Sources/_StringProcessing/Regex/Core.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import _RegexParser
1313

1414

1515
/// A type that represents a regular expression.
16-
public protocol RegexComponent {
16+
public protocol RegexComponent<Output> {
1717
associatedtype Output
1818
var regex: Regex<Output> { get }
1919
}
@@ -93,7 +93,6 @@ extension Regex {
9393
public init(node: DSLTree.Node) {
9494
self.program = Program(tree: .init(node, options: nil))
9595
}
96-
9796
}
9897

9998
// MARK: - Primitive regex components

Tests/RegexBuilderTests/RegexDSLTests.swift

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -624,38 +624,6 @@ class RegexDSLTests: XCTestCase {
624624
}
625625
}
626626

627-
func testDynamicCaptures() throws {
628-
do {
629-
let regex = try Regex(compiling: "aabcc.")
630-
let line = "aabccd"
631-
let match = try XCTUnwrap(line.wholeMatch(of: regex))
632-
XCTAssertEqual(match.0, line[...])
633-
let output = match.output
634-
XCTAssertEqual(output[0].substring, line[...])
635-
}
636-
do {
637-
let regex = try Regex(
638-
compiling: #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#)
639-
let line = """
640-
A6F0..A6F1 ; Extend # Mn [2] BAMUM COMBINING MARK KOQNDON..BAMUM \
641-
COMBINING MARK TUKWENTIS
642-
"""
643-
let match = try XCTUnwrap(line.wholeMatch(of: regex))
644-
XCTAssertEqual(match.0, line[...])
645-
let output = match.output
646-
XCTAssertEqual(output[0].substring, line[...])
647-
XCTAssertTrue(output[1].substring == "A6F0")
648-
XCTAssertTrue(output[2].substring == "A6F1")
649-
XCTAssertTrue(output[3].substring == "Extend")
650-
let typedOutput = try XCTUnwrap(output.as(
651-
(Substring, Substring, Substring?, Substring).self))
652-
XCTAssertEqual(typedOutput.0, line[...])
653-
XCTAssertTrue(typedOutput.1 == "A6F0")
654-
XCTAssertTrue(typedOutput.2 == "A6F1")
655-
XCTAssertTrue(typedOutput.3 == "Extend")
656-
}
657-
}
658-
659627
func testBackreference() throws {
660628
try _testDSLCaptures(
661629
("abc#41#42abcabcabc", ("abc#41#42abcabcabc", "abc", 42, "abc", nil)),

Tests/RegexTests/RegexAPITests.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
import XCTest
13+
@testable import _StringProcessing
14+
15+
class RegexAPITests: XCTestCase {
16+
func testDynamicConstruction() throws {
17+
do {
18+
let regex = try Regex(compiling: "aabcc.")
19+
let line = "aabccd"
20+
let match = try XCTUnwrap(line.wholeMatch(of: regex))
21+
XCTAssertEqual(match.0, line[...])
22+
let output = match.output
23+
XCTAssertEqual(output[0].substring, line[...])
24+
}
25+
do {
26+
let regex = try Regex(
27+
compiling: #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#)
28+
let line = """
29+
A6F0..A6F1 ; Extend # Mn [2] BAMUM COMBINING MARK KOQNDON..BAMUM \
30+
COMBINING MARK TUKWENTIS
31+
"""
32+
let match = try XCTUnwrap(line.wholeMatch(of: regex))
33+
XCTAssertEqual(match.0, line[...])
34+
let output = match.output
35+
XCTAssertEqual(output[0].substring, line[...])
36+
XCTAssertTrue(output[1].substring == "A6F0")
37+
XCTAssertTrue(output[2].substring == "A6F1")
38+
XCTAssertTrue(output[3].substring == "Extend")
39+
let typedOutput = try XCTUnwrap(output.as(
40+
(Substring, Substring, Substring?, Substring).self))
41+
XCTAssertEqual(typedOutput.0, line[...])
42+
XCTAssertTrue(typedOutput.1 == "A6F0")
43+
XCTAssertTrue(typedOutput.2 == "A6F1")
44+
XCTAssertTrue(typedOutput.3 == "Extend")
45+
}
46+
}
47+
48+
func testPrimaryAssociatedType() throws {
49+
let originalRegex = try Regex(compiling: "aabcc.")
50+
let regex = originalRegex as any RegexComponent<AnyRegexOutput>
51+
let line = "aabccd"
52+
let match = try XCTUnwrap(line.wholeMatch(of: regex))
53+
XCTAssertEqual(match.0, line[...])
54+
let output = match.output
55+
XCTAssertEqual(output[0].substring, line[...])
56+
}
57+
}

0 commit comments

Comments
 (0)