Skip to content

Commit ec7f487

Browse files
authored
Merge pull request swiftlang#327 from itingliu/revised_algorithm_5_7
Revised algorithm 5 7
2 parents 29bc5da + 978cce1 commit ec7f487

File tree

5 files changed

+47
-39
lines changed

5 files changed

+47
-39
lines changed

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ let package = Package(
2929
],
3030
dependencies: [
3131
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
32-
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
3332
],
3433
targets: [
3534
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
@available(SwiftStdlib 5.7, *)
13+
/// A protocol allowing custom types to function as regex components by
14+
/// providing the raw functionality backing `prefixMatch`.
15+
public protocol CustomConsumingRegexComponent: RegexComponent {
16+
/// Process the input string within the specified bounds, beginning at the given index, and return
17+
/// the end position (upper bound) of the match and the produced output.
18+
/// - Parameters:
19+
/// - input: The string in which the match is performed.
20+
/// - index: An index of `input` at which to begin matching.
21+
/// - bounds: The bounds in `input` in which the match is performed.
22+
/// - Returns: The upper bound where the match terminates and a matched instance, or `nil` if
23+
/// there isn't a match.
24+
func consuming(
25+
_ input: String,
26+
startingAt index: String.Index,
27+
in bounds: Range<String.Index>
28+
) throws -> (upperBound: String.Index, output: RegexOutput)?
29+
}
30+
31+
@available(SwiftStdlib 5.7, *)
32+
extension CustomConsumingRegexComponent {
33+
public var regex: Regex<RegexOutput> {
34+
let node: DSLTree.Node = .matcher(.init(RegexOutput.self), { input, index, bounds in
35+
try consuming(input, startingAt: index, in: bounds)
36+
})
37+
return Regex(node: node)
38+
}
39+
}

Sources/_StringProcessing/Regex/DSLConsumers.swift

Lines changed: 0 additions & 30 deletions
This file was deleted.

Tests/RegexBuilderTests/CustomTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import _StringProcessing
1414
@testable import RegexBuilder
1515

1616
// A nibbler processes a single character from a string
17-
private protocol Nibbler: CustomMatchingRegexComponent {
17+
private protocol Nibbler: CustomConsumingRegexComponent {
1818
func nibble(_: Character) -> RegexOutput?
1919
}
2020

2121
extension Nibbler {
2222
// Default implementation, just feed the character in
23-
func match(
23+
func consuming(
2424
_ input: String,
2525
startingAt index: String.Index,
2626
in bounds: Range<String.Index>
@@ -49,10 +49,10 @@ private struct Asciibbler: Nibbler {
4949
}
5050
}
5151

52-
private struct IntParser: CustomMatchingRegexComponent {
52+
private struct IntParser: CustomConsumingRegexComponent {
5353
struct ParseError: Error, Hashable {}
5454
typealias RegexOutput = Int
55-
func match(_ input: String,
55+
func consuming(_ input: String,
5656
startingAt index: String.Index,
5757
in bounds: Range<String.Index>
5858
) throws -> (upperBound: String.Index, output: Int)? {
@@ -71,7 +71,7 @@ private struct IntParser: CustomMatchingRegexComponent {
7171
}
7272
}
7373

74-
private struct CurrencyParser: CustomMatchingRegexComponent {
74+
private struct CurrencyParser: CustomConsumingRegexComponent {
7575
enum Currency: String, Hashable {
7676
case usd = "USD"
7777
case ntd = "NTD"
@@ -84,7 +84,7 @@ private struct CurrencyParser: CustomMatchingRegexComponent {
8484
}
8585

8686
typealias RegexOutput = Currency
87-
func match(_ input: String,
87+
func consuming(_ input: String,
8888
startingAt index: String.Index,
8989
in bounds: Range<String.Index>
9090
) throws -> (upperBound: String.Index, output: Currency)? {

Tests/RegexBuilderTests/RegexDSLTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,9 +850,9 @@ class RegexDSLTests: XCTestCase {
850850
var patch: Int
851851
var dev: String?
852852
}
853-
struct SemanticVersionParser: CustomMatchingRegexComponent {
853+
struct SemanticVersionParser: CustomConsumingRegexComponent {
854854
typealias RegexOutput = SemanticVersion
855-
func match(
855+
func consuming(
856856
_ input: String,
857857
startingAt index: String.Index,
858858
in bounds: Range<String.Index>

0 commit comments

Comments
 (0)