Skip to content

Revised algorithm 5 7 #327

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 2 commits into from
Apr 22, 2022
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
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
39 changes: 39 additions & 0 deletions Sources/_StringProcessing/Regex/CustomComponents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

@available(SwiftStdlib 5.7, *)
/// A protocol allowing custom types to function as regex components by
/// providing the raw functionality backing `prefixMatch`.
public protocol CustomConsumingRegexComponent: RegexComponent {
/// Process the input string within the specified bounds, beginning at the given index, and return
/// the end position (upper bound) of the match and the produced output.
/// - Parameters:
/// - input: The string in which the match is performed.
/// - index: An index of `input` at which to begin matching.
/// - bounds: The bounds in `input` in which the match is performed.
/// - Returns: The upper bound where the match terminates and a matched instance, or `nil` if
/// there isn't a match.
func consuming(
_ input: String,
startingAt index: String.Index,
in bounds: Range<String.Index>
) throws -> (upperBound: String.Index, output: RegexOutput)?
}

@available(SwiftStdlib 5.7, *)
extension CustomConsumingRegexComponent {
public var regex: Regex<RegexOutput> {
let node: DSLTree.Node = .matcher(.init(RegexOutput.self), { input, index, bounds in
try consuming(input, startingAt: index, in: bounds)
})
return Regex(node: node)
}
}
30 changes: 0 additions & 30 deletions Sources/_StringProcessing/Regex/DSLConsumers.swift

This file was deleted.

12 changes: 6 additions & 6 deletions Tests/RegexBuilderTests/CustomTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import _StringProcessing
@testable import RegexBuilder

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

extension Nibbler {
// Default implementation, just feed the character in
func match(
func consuming(
_ input: String,
startingAt index: String.Index,
in bounds: Range<String.Index>
Expand Down Expand Up @@ -49,10 +49,10 @@ private struct Asciibbler: Nibbler {
}
}

private struct IntParser: CustomMatchingRegexComponent {
private struct IntParser: CustomConsumingRegexComponent {
struct ParseError: Error, Hashable {}
typealias RegexOutput = Int
func match(_ input: String,
func consuming(_ input: String,
startingAt index: String.Index,
in bounds: Range<String.Index>
) throws -> (upperBound: String.Index, output: Int)? {
Expand All @@ -71,7 +71,7 @@ private struct IntParser: CustomMatchingRegexComponent {
}
}

private struct CurrencyParser: CustomMatchingRegexComponent {
private struct CurrencyParser: CustomConsumingRegexComponent {
enum Currency: String, Hashable {
case usd = "USD"
case ntd = "NTD"
Expand All @@ -84,7 +84,7 @@ private struct CurrencyParser: CustomMatchingRegexComponent {
}

typealias RegexOutput = Currency
func match(_ input: String,
func consuming(_ input: String,
startingAt index: String.Index,
in bounds: Range<String.Index>
) throws -> (upperBound: String.Index, output: Currency)? {
Expand Down
4 changes: 2 additions & 2 deletions Tests/RegexBuilderTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,9 @@ class RegexDSLTests: XCTestCase {
var patch: Int
var dev: String?
}
struct SemanticVersionParser: CustomMatchingRegexComponent {
struct SemanticVersionParser: CustomConsumingRegexComponent {
typealias RegexOutput = SemanticVersion
func match(
func consuming(
_ input: String,
startingAt index: String.Index,
in bounds: Range<String.Index>
Expand Down