Skip to content

Define primary associated type for RegexComponent. #207

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
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
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ let package = Package(
dependencies: ["_RegexParser", "_CUnicode"],
swiftSettings: [
.unsafeFlags(["-enable-library-evolution"]),
.unsafeFlags(["-Xfrontend", "-enable-parameterized-protocol-types"])
]),
.target(
name: "RegexBuilder",
Expand Down
3 changes: 1 addition & 2 deletions Sources/_StringProcessing/Regex/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import _RegexParser


/// A type that represents a regular expression.
public protocol RegexComponent {
public protocol RegexComponent<Output> {
associatedtype Output
var regex: Regex<Output> { get }
}
Expand Down Expand Up @@ -93,7 +93,6 @@ extension Regex {
public init(node: DSLTree.Node) {
self.program = Program(tree: .init(node, options: nil))
}

}

// MARK: - Primitive regex components
Expand Down
32 changes: 0 additions & 32 deletions Tests/RegexBuilderTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -624,38 +624,6 @@ class RegexDSLTests: XCTestCase {
}
}

func testDynamicCaptures() throws {
do {
let regex = try Regex(compiling: "aabcc.")
let line = "aabccd"
let match = try XCTUnwrap(line.wholeMatch(of: regex))
XCTAssertEqual(match.0, line[...])
let output = match.output
XCTAssertEqual(output[0].substring, line[...])
}
do {
let regex = try Regex(
compiling: #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#)
let line = """
A6F0..A6F1 ; Extend # Mn [2] BAMUM COMBINING MARK KOQNDON..BAMUM \
COMBINING MARK TUKWENTIS
"""
let match = try XCTUnwrap(line.wholeMatch(of: regex))
XCTAssertEqual(match.0, line[...])
let output = match.output
XCTAssertEqual(output[0].substring, line[...])
XCTAssertTrue(output[1].substring == "A6F0")
XCTAssertTrue(output[2].substring == "A6F1")
XCTAssertTrue(output[3].substring == "Extend")
let typedOutput = try XCTUnwrap(output.as(
(Substring, Substring, Substring?, Substring).self))
XCTAssertEqual(typedOutput.0, line[...])
XCTAssertTrue(typedOutput.1 == "A6F0")
XCTAssertTrue(typedOutput.2 == "A6F1")
XCTAssertTrue(typedOutput.3 == "Extend")
}
}

func testBackreference() throws {
try _testDSLCaptures(
("abc#41#42abcabcabc", ("abc#41#42abcabcabc", "abc", 42, "abc", nil)),
Expand Down
57 changes: 57 additions & 0 deletions Tests/RegexTests/RegexAPITests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

import XCTest
@testable import _StringProcessing

class RegexAPITests: XCTestCase {
func testDynamicConstruction() throws {
do {
let regex = try Regex(compiling: "aabcc.")
let line = "aabccd"
let match = try XCTUnwrap(line.wholeMatch(of: regex))
XCTAssertEqual(match.0, line[...])
let output = match.output
XCTAssertEqual(output[0].substring, line[...])
}
do {
let regex = try Regex(
compiling: #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#)
let line = """
A6F0..A6F1 ; Extend # Mn [2] BAMUM COMBINING MARK KOQNDON..BAMUM \
COMBINING MARK TUKWENTIS
"""
let match = try XCTUnwrap(line.wholeMatch(of: regex))
XCTAssertEqual(match.0, line[...])
let output = match.output
XCTAssertEqual(output[0].substring, line[...])
XCTAssertTrue(output[1].substring == "A6F0")
XCTAssertTrue(output[2].substring == "A6F1")
XCTAssertTrue(output[3].substring == "Extend")
let typedOutput = try XCTUnwrap(output.as(
(Substring, Substring, Substring?, Substring).self))
XCTAssertEqual(typedOutput.0, line[...])
XCTAssertTrue(typedOutput.1 == "A6F0")
XCTAssertTrue(typedOutput.2 == "A6F1")
XCTAssertTrue(typedOutput.3 == "Extend")
}
}

func testPrimaryAssociatedType() throws {
let originalRegex = try Regex(compiling: "aabcc.")
let regex = originalRegex as any RegexComponent<AnyRegexOutput>
let line = "aabccd"
let match = try XCTUnwrap(line.wholeMatch(of: regex))
XCTAssertEqual(match.0, line[...])
let output = match.output
XCTAssertEqual(output[0].substring, line[...])
}
}