From e08732075ddb5895164f08b38314a7cae36b8de0 Mon Sep 17 00:00:00 2001 From: Tina Liu <49205802+itingliu@users.noreply.github.com> Date: Tue, 22 Mar 2022 14:33:09 -0700 Subject: [PATCH 01/10] Pitch: String processing algorithms (#188) * Add string processing algorithms pitch Co-authored-by: Tim Vermeulen Co-authored-by: Michael Ilseman --- .../Evolution/StringProcessingAlgorithms.md | 612 ++++++++++++++++++ 1 file changed, 612 insertions(+) create mode 100644 Documentation/Evolution/StringProcessingAlgorithms.md diff --git a/Documentation/Evolution/StringProcessingAlgorithms.md b/Documentation/Evolution/StringProcessingAlgorithms.md new file mode 100644 index 000000000..9454396ce --- /dev/null +++ b/Documentation/Evolution/StringProcessingAlgorithms.md @@ -0,0 +1,612 @@ +# String processing algorithms + +## Introduction + +The Swift standard library's string processing algorithms are underpowered compared to other popular programming and scripting languages. Some of these omissions can be found in `NSString`, but these fundamental algorithms should have a place in the standard library. + +We propose: + +1. New regex-powered algorithms over strings, bringing the standard library up to parity with scripting languages +2. Generic `Collection` equivalents of these algorithms in terms of subsequences +3. `protocol CustomMatchingRegexComponent`, which allows 3rd party libraries to provide their industrial-strength parsers as intermixable components of regexes + +This proposal is part of a larger [regex-powered string processing initiative](https://forums.swift.org/t/declarative-string-processing-overview/52459). Throughout the document, we will reference the still-in-progress [`RegexProtocol`, `Regex`](https://github.com/apple/swift-experimental-string-processing/blob/main/Documentation/Evolution/StronglyTypedCaptures.md), and result builder DSL, but these are in flux and not formally part of this proposal. Further discussion of regex specifics is out of scope of this proposal and better discussed in another thread (see [Pitch and Proposal Status](https://github.com/apple/swift-experimental-string-processing/issues/107) for links to relevant threads). + +## Motivation + +A number of common string processing APIs are missing from the Swift standard library. While most of the desired functionalities can be accomplished through a series of API calls, every gap adds a burden to developers doing frequent or complex string processing. For example, here's one approach to find the number of occurrences a substring ("banana") within a string: + +```swift +let str = "A banana a day keeps the doctor away. I love bananas; banana are my favorite fruit." + +var idx = str.startIndex +var ranges = [Range]() +while let r = str.range(of: "banana", options: [], range: idx.. + Comparison of how Swift's APIs stack up with Python's. + +Note: Only a subset of Python's string processing API are included in this table for the following reasons: + +- Functions to query if all characters in the string are of a specified category, such as `isalnum()` and `isalpha()`, are omitted. These are achievable in Swift by passing in the corresponding character set to `allSatisfy(_:)`, so they're omitted in this table for simplicity. +- String formatting functions such as `center(length, character)` and `ljust(width, fillchar)` are also excluded here as this proposal focuses on matching and searching functionalities. + +##### Search and replace + +|Python |Swift | +|--- |--- | +| `count(sub, start, end)` | | +| `find(sub, start, end)`, `index(sub, start, end)` | `firstIndex(where:)` | +| `rfind(sub, start, end)`, `rindex(sub, start, end)` | `lastIndex(where:)` | +| `expandtabs(tabsize)`, `replace(old, new, count)` | `Foundation.replacingOccurrences(of:with:)` | +| `maketrans(x, y, z)` + `translate(table)` | + +##### Prefix and suffix matching + +|Python |Swift | +|--- |--- | +| `startswith(prefix, start, end)` | `starts(with:)` or `hasPrefix(:)`| +| `endswith(suffix, start, end)` | `hasSuffix(:)` | +| `removeprefix(prefix)` | Test if string has prefix with `hasPrefix(:)`, then drop the prefix with `dropFirst(:)`| +| `removesuffix(suffix)` | Test if string has suffix with `hasSuffix(:)`, then drop the suffix with `dropLast(:)` | + +##### Strip / trim + +|Python |Swift | +|--- |--- | +| `strip([chars])`| `Foundation.trimmingCharacters(in:)` | +| `lstrip([chars])` | `drop(while:)` | +| `rstrip([chars])` | Test character equality, then `dropLast()` iteratively | + +##### Split + +|Python |Swift | +|--- |--- | +| `partition(sep)` | `Foundation.components(separatedBy:)` | +| `rpartition(sep)` | | +| `split(sep, maxsplit)` | `split(separator:maxSplits:...)` | +| `splitlines(keepends)` | `split(separator:maxSplits:...)` | +| `rsplit(sep, maxsplit)` | | + + + + + +### Complex string processing + +Even with the API additions, more complex string processing quickly becomes unwieldy. Up-coming support for authoring regexes in Swift help alleviate this somewhat, but string processing in the modern world involves dealing with localization, standards-conforming validation, and other concerns for which a dedicated parser is required. + +Consider parsing the date field `"Date: Wed, 16 Feb 2022 23:53:19 GMT"` in an HTTP header as a `Date` type. The naive approach is to search for a substring that looks like a date string (`16 Feb 2022`), and attempt to post-process it as a `Date` with a date parser: + +```swift +let regex = Regex { + capture { + oneOrMore(.digit) + " " + oneOrMore(.word) + " " + oneOrMore(.digit) + } +} + +let dateParser = Date.ParseStrategy(format: "\(day: .twoDigits) \(month: .abbreviated) \(year: .padded(4))" +if let dateMatch = header.firstMatch(of: regex)?.0 { + let date = try? Date(dateMatch, strategy: dateParser) +} +``` + +This requires writing a simplistic pre-parser before invoking the real parser. The pre-parser will suffer from being out-of-sync and less featureful than what the real parser can do. + +Or consider parsing a bank statement to record all the monetary values in the last column: + +```swift +let statement = """ +CREDIT 04/06/2020 Paypal transfer $4.99 +CREDIT 04/03/2020 Payroll $69.73 +DEBIT 04/02/2020 ACH transfer ($38.25) +DEBIT 03/24/2020 IRX tax payment ($52,249.98) +""" +``` + +Parsing a currency string such as `$3,020.85` with regex is also tricky, as it can contain localized and currency symbols in addition to accounting conventions. This is why Foundation provides industrial-strength parsers for localized strings. + + +## Proposed solution + +### Complex string processing + +We propose a `CustomMatchingRegexComponent` protocol which allows types from outside the standard library participate in regex builders and `RegexComponent` algorithms. This allows types, such as `Date.ParseStrategy` and `FloatingPointFormatStyle.Currency`, to be used directly within a regex: + +```swift +let dateRegex = Regex { + capture(dateParser) +} + +let date: Date = header.firstMatch(of: dateRegex).map(\.result.1) + +let currencyRegex = Regex { + capture(.localizedCurrency(code: "USD").sign(strategy: .accounting)) +} + +let amount: [Decimal] = statement.matches(of: currencyRegex).map(\.result.1) +``` + +### String algorithm additions + +We also propose the following regex-powered algorithms as well as their generic `Collection` equivalents. See the Detailed design section for a complete list of variation and overloads . + +|Function | Description | +|--- |--- | +|`contains(_:) -> Bool` | Returns whether the collection contains the given sequence or `RegexComponent` | +|`starts(with:) -> Bool` | Returns whether the collection contains the same prefix as the specified `RegexComponent` | +|`trimPrefix(_:)`| Removes the prefix if it matches the given `RegexComponent` or collection | +|`firstRange(of:) -> Range?` | Finds the range of the first occurrence of a given sequence or `RegexComponent`| +|`ranges(of:) -> some Collection` | Finds the ranges of the all occurrences of a given sequence or `RegexComponent` within the collection | +|`replace(:with:subrange:maxReplacements)`| Replaces all occurrences of the sequence matching the given `RegexComponent` or sequence with a given collection | +|`split(by:)`| Returns the longest possible subsequences of the collection around elements equal to the given separator | +|`firstMatch(of:)`| Returns the first match of the specified `RegexComponent` within the collection | +|`matches(of:)`| Returns a collection containing all matches of the specified `RegexComponent` | + + + +## Detailed design + +### `CustomMatchingRegexComponent` + +`CustomMatchingRegexComponent` inherits from `RegexComponent` and satisfies its sole requirement; Conformers can be used with all of the string algorithms generic over `RegexComponent`. + +```swift +/// A protocol for custom match functionality. +public protocol CustomMatchingRegexComponent : RegexComponent { + /// Match the input string within the specified bounds, beginning at the given index, and return + /// the end position (upper bound) of the match and the matched instance. + /// - 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 match( + _ input: String, + startingAt index: String.Index, + in bounds: Range + ) -> (upperBound: String.Index, match: Match)? +} +``` + +
+Example for protocol conformance + +We use Foundation `FloatingPointFormatStyle.Currency` as an example for protocol conformance. It would implement the `match` function with `Match` being a `Decimal`. It could also add a static function `.localizedCurrency(code:)` as a member of `RegexComponent`, so it can be referred as `.localizedCurrency(code:)` in the `Regex` result builder: + +```swift +extension FloatingPointFormatStyle.Currency : CustomMatchingRegexComponent { + public func match( + _ input: String, + startingAt index: String.Index, + in bounds: Range + ) -> (upperBound: String.Index, match: Decimal)? +} + +extension RegexComponent where Self == FloatingPointFormatStyle.Currency { + public static func localizedCurrency(code: Locale.Currency) -> Self +} +``` + +Matching and extracting a localized currency amount, such as `"$3,020.85"`, can be done directly within a regex: + +```swift +let regex = Regex { + capture(.localizedCurreny(code: "USD")) +} +``` + +
+ + +### String algorithm additions + +#### Contains + +```swift +extension Collection where Element: Equatable { + /// Returns a Boolean value indicating whether the collection contains the + /// given sequence. + /// - Parameter other: A sequence to search for within this collection. + /// - Returns: `true` if the collection contains the specified sequence, + /// otherwise `false`. + public func contains(_ other: S) -> Bool + where S.Element == Element +} + +extension BidirectionalCollection where SubSequence == Substring { + /// Returns a Boolean value indicating whether the collection contains the + /// given regex. + /// - Parameter regex: A regex to search for within this collection. + /// - Returns: `true` if the regex was found in the collection, otherwise + /// `false`. + public func contains(_ regex: R) -> Bool +} +``` + +#### Starts with + +```swift +extension BidirectionalCollection where SubSequence == Substring { + /// Returns a Boolean value indicating whether the initial elements of the + /// sequence are the same as the elements in the specified regex. + /// - Parameter regex: A regex to compare to this sequence. + /// - Returns: `true` if the initial elements of the sequence matches the + /// beginning of `regex`; otherwise, `false`. + public func starts(with regex: R) -> Bool +} +``` + +#### Trim prefix + +```swift +extension Collection { + /// Returns a new collection of the same type by removing initial elements + /// that satisfy the given predicate from the start. + /// - Parameter predicate: A closure that takes an element of the sequence + /// as its argument and returns a Boolean value indicating whether the + /// element should be removed from the collection. + /// - Returns: A collection containing the elements of the collection that are + /// not removed by `predicate`. + public func trimmingPrefix(while predicate: (Element) throws -> Bool) rethrows -> SubSequence +} + +extension Collection where SubSequence == Self { + /// Removes the initial elements that satisfy the given predicate from the + /// start of the sequence. + /// - Parameter predicate: A closure that takes an element of the sequence + /// as its argument and returns a Boolean value indicating whether the + /// element should be removed from the collection. + public mutating func trimPrefix(while predicate: (Element) throws -> Bool) +} + +extension RangeReplaceableCollection { + /// Removes the initial elements that satisfy the given predicate from the + /// start of the sequence. + /// - Parameter predicate: A closure that takes an element of the sequence + /// as its argument and returns a Boolean value indicating whether the + /// element should be removed from the collection. + public mutating func trimPrefix(while predicate: (Element) throws -> Bool) +} + +extension Collection where Element: Equatable { + /// Returns a new collection of the same type by removing `prefix` from the + /// start. + /// - Parameter prefix: The collection to remove from this collection. + /// - Returns: A collection containing the elements that does not match + /// `prefix` from the start. + public func trimmingPrefix(_ prefix: Prefix) -> SubSequence + where Prefix.Element == Element +} + +extension Collection where SubSequence == Self, Element: Equatable { + /// Removes the initial elements that matches `prefix` from the start. + /// - Parameter prefix: The collection to remove from this collection. + public mutating func trimPrefix(_ prefix: Prefix) + where Prefix.Element == Element +} + +extension RangeReplaceableCollection where Element: Equatable { + /// Removes the initial elements that matches `prefix` from the start. + /// - Parameter prefix: The collection to remove from this collection. + public mutating func trimPrefix(_ prefix: Prefix) + where Prefix.Element == Element +} + +extension BidirectionalCollection where SubSequence == Substring { + /// Returns a new subsequence by removing the initial elements that matches + /// the given regex. + /// - Parameter regex: The regex to remove from this collection. + /// - Returns: A new subsequence containing the elements of the collection + /// that does not match `prefix` from the start. + public func trimmingPrefix(_ regex: R) -> SubSequence +} + +extension RangeReplaceableCollection + where Self: BidirectionalCollection, SubSequence == Substring +{ + /// Removes the initial elements that matches the given regex. + /// - Parameter regex: The regex to remove from this collection. + public mutating func trimPrefix(_ regex: R) +} +``` + +#### First range + +```swift +extension Collection where Element: Equatable { + /// Finds and returns the range of the first occurrence of a given sequence + /// within the collection. + /// - Parameter sequence: The sequence to search for. + /// - Returns: A range in the collection of the first occurrence of `sequence`. + /// Returns nil if `sequence` is not found. + public func firstRange(of sequence: S) -> Range? + where S.Element == Element +} + +extension BidirectionalCollection where Element: Comparable { + /// Finds and returns the range of the first occurrence of a given sequence + /// within the collection. + /// - Parameter other: The sequence to search for. + /// - Returns: A range in the collection of the first occurrence of `sequence`. + /// Returns `nil` if `sequence` is not found. + public func firstRange(of other: S) -> Range? + where S.Element == Element +} + +extension BidirectionalCollection where SubSequence == Substring { + /// Finds and returns the range of the first occurrence of a given regex + /// within the collection. + /// - Parameter regex: The regex to search for. + /// - Returns: A range in the collection of the first occurrence of `regex`. + /// Returns `nil` if `regex` is not found. + public func firstRange(of regex: R) -> Range? +} +``` + +#### Ranges + +```swift +extension Collection where Element: Equatable { + /// Finds and returns the ranges of the all occurrences of a given sequence + /// within the collection. + /// - Parameter other: The sequence to search for. + /// - Returns: A collection of ranges of all occurrences of `other`. Returns + /// an empty collection if `other` is not found. + public func ranges(of other: S) -> some Collection> + where S.Element == Element +} + +extension BidirectionalCollection where SubSequence == Substring { + /// Finds and returns the ranges of the all occurrences of a given sequence + /// within the collection. + /// - Parameter regex: The regex to search for. + /// - Returns: A collection or ranges in the receiver of all occurrences of + /// `regex`. Returns an empty collection if `regex` is not found. + public func ranges(of regex: R) -> some Collection> +} +``` + +#### First match + +```swift +extension BidirectionalCollection where SubSequence == Substring { + /// Returns the first match of the specified regex within the collection. + /// - Parameter regex: The regex to search for. + /// - Returns: The first match of `regex` in the collection, or `nil` if + /// there isn't a match. + public func firstMatch(of regex: R) -> RegexMatch? +} +``` + +#### Matches + +```swift +extension BidirectionalCollection where SubSequence == Substring { + /// Returns a collection containing all matches of the specified regex. + /// - Parameter regex: The regex to search for. + /// - Returns: A collection of matches of `regex`. + public func matches(of regex: R) -> some Collection> +} +``` + +#### Replace + +```swift +extension RangeReplaceableCollection where Element: Equatable { + /// Returns a new collection in which all occurrences of a target sequence + /// are replaced by another collection. + /// - Parameters: + /// - other: The sequence to replace. + /// - replacement: The new elements to add to the collection. + /// - subrange: The range in the collection in which to search for `other`. + /// - maxReplacements: A number specifying how many occurrences of `other` + /// to replace. Default is `Int.max`. + /// - Returns: A new collection in which all occurrences of `other` in + /// `subrange` of the collection are replaced by `replacement`. + public func replacing( + _ other: S, + with replacement: Replacement, + subrange: Range, + maxReplacements: Int = .max + ) -> Self where S.Element == Element, Replacement.Element == Element + + /// Returns a new collection in which all occurrences of a target sequence + /// are replaced by another collection. + /// - Parameters: + /// - other: The sequence to replace. + /// - replacement: The new elements to add to the collection. + /// - maxReplacements: A number specifying how many occurrences of `other` + /// to replace. Default is `Int.max`. + /// - Returns: A new collection in which all occurrences of `other` in + /// `subrange` of the collection are replaced by `replacement`. + public func replacing( + _ other: S, + with replacement: Replacement, + maxReplacements: Int = .max + ) -> Self where S.Element == Element, Replacement.Element == Element + + /// Replaces all occurrences of a target sequence with a given collection + /// - Parameters: + /// - other: The sequence to replace. + /// - replacement: The new elements to add to the collection. + /// - maxReplacements: A number specifying how many occurrences of `other` + /// to replace. Default is `Int.max`. + public mutating func replace( + _ other: S, + with replacement: Replacement, + maxReplacements: Int = .max + ) where S.Element == Element, Replacement.Element == Element +} + +extension RangeReplaceableCollection where SubSequence == Substring { + /// Returns a new collection in which all occurrences of a sequence matching + /// the given regex are replaced by another collection. + /// - Parameters: + /// - regex: A regex describing the sequence to replace. + /// - replacement: The new elements to add to the collection. + /// - subrange: The range in the collection in which to search for `regex`. + /// - maxReplacements: A number specifying how many occurrences of the + /// sequence matching `regex` to replace. Default is `Int.max`. + /// - Returns: A new collection in which all occurrences of subsequence + /// matching `regex` in `subrange` are replaced by `replacement`. + public func replacing( + _ regex: R, + with replacement: Replacement, + subrange: Range, + maxReplacements: Int = .max + ) -> Self where Replacement.Element == Element + + /// Returns a new collection in which all occurrences of a sequence matching + /// the given regex are replaced by another collection. + /// - Parameters: + /// - regex: A regex describing the sequence to replace. + /// - replacement: The new elements to add to the collection. + /// - maxReplacements: A number specifying how many occurrences of the + /// sequence matching `regex` to replace. Default is `Int.max`. + /// - Returns: A new collection in which all occurrences of subsequence + /// matching `regex` are replaced by `replacement`. + public func replacing( + _ regex: R, + with replacement: Replacement, + maxReplacements: Int = .max + ) -> Self where Replacement.Element == Element + + /// Replaces all occurrences of the sequence matching the given regex with + /// a given collection. + /// - Parameters: + /// - regex: A regex describing the sequence to replace. + /// - replacement: The new elements to add to the collection. + /// - maxReplacements: A number specifying how many occurrences of the + /// sequence matching `regex` to replace. Default is `Int.max`. + public mutating func replace( + _ regex: R, + with replacement: Replacement, + maxReplacements: Int = .max + ) where Replacement.Element == Element + + /// Returns a new collection in which all occurrences of a sequence matching + /// the given regex are replaced by another regex match. + /// - Parameters: + /// - regex: A regex describing the sequence to replace. + /// - replacement: A closure that receives the full match information, + /// including captures, and returns a replacement collection. + /// - subrange: The range in the collection in which to search for `regex`. + /// - maxReplacements: A number specifying how many occurrences of the + /// sequence matching `regex` to replace. Default is `Int.max`. + /// - Returns: A new collection in which all occurrences of subsequence + /// matching `regex` are replaced by `replacement`. + public func replacing( + _ regex: R, + with replacement: (RegexMatch) throws -> Replacement, + subrange: Range, + maxReplacements: Int = .max + ) rethrows -> Self where Replacement.Element == Element + + /// Returns a new collection in which all occurrences of a sequence matching + /// the given regex are replaced by another collection. + /// - Parameters: + /// - regex: A regex describing the sequence to replace. + /// - replacement: A closure that receives the full match information, + /// including captures, and returns a replacement collection. + /// - maxReplacements: A number specifying how many occurrences of the + /// sequence matching `regex` to replace. Default is `Int.max`. + /// - Returns: A new collection in which all occurrences of subsequence + /// matching `regex` are replaced by `replacement`. + public func replacing( + _ regex: R, + with replacement: (RegexMatch) throws -> Replacement, + maxReplacements: Int = .max + ) rethrows -> Self where Replacement.Element == Element + + /// Replaces all occurrences of the sequence matching the given regex with + /// a given collection. + /// - Parameters: + /// - regex: A regex describing the sequence to replace. + /// - replacement: A closure that receives the full match information, + /// including captures, and returns a replacement collection. + /// - maxReplacements: A number specifying how many occurrences of the + /// sequence matching `regex` to replace. Default is `Int.max`. + public mutating func replace( + _ regex: R, + with replacement: (RegexMatch) throws -> Replacement, + maxReplacements: Int = .max + ) rethrows where Replacement.Element == Element +} +``` + +#### Split + +```swift +extension Collection where Element: Equatable { + /// Returns the longest possible subsequences of the collection, in order, + /// around elements equal to the given separator. + /// - Parameter separator: The element to be split upon. + /// - Returns: A collection of subsequences, split from this collection's + /// elements. + public func split(by separator: S) -> some Collection + where S.Element == Element +} + +extension BidirectionalCollection where SubSequence == Substring { + /// Returns the longest possible subsequences of the collection, in order, + /// around elements equal to the given separator. + /// - Parameter separator: A regex describing elements to be split upon. + /// - Returns: A collection of substrings, split from this collection's + /// elements. + public func split(by separator: R) -> some Collection +} +``` + + + + + +## Alternatives considered + +### Extend `Sequence` instead of `Collection` + +Most of the proposed algorithms are necessarily on `Collection` due to the use of indices or mutation. `Sequence` does not support multi-pass iteration, so even `trimPrefix` would problematic on `Sequence` because it needs to look 1 `Element` ahead to know when to stop trimming. + +## Future directions + +### Backward algorithms + +It would be useful to have algorithms that operate from the back of a collection, including ability to find the last non-overlapping range of a pattern in a string, and/or that to find the first range of a pattern when searching from the back, and trimming a string from both sides. They are deferred from this proposal as the API that could clarify the nuances of backward algorithms are still being explored. + +
+ Nuances of backward algorithms + +There is a subtle difference between finding the last non-overlapping range of a pattern in a string, and finding the first range of this pattern when searching from the back. + +The currently proposed algorithm that finds a pattern from the front, e.g. `"aaaaa".ranges(of: "aa")`, produces two non-overlapping ranges, splitting the string in the chunks `aa|aa|a`. It would not be completely unreasonable to expect to introduce a counterpart, such as `"aaaaa".lastRange(of: "aa")`, to return the range that contains the third and fourth characters of the string. This would be a shorthand for `"aaaaa".ranges(of: "aa").last`. Yet, it would also be reasonable to expect the function to return the first range of `"aa"` when searching from the back of the string, i.e. the range that contains the fourth and fifth characters. + +Trimming a string from both sides shares a similar story. For example, `"ababa".trimming("aba")` can return either `"ba"` or `"ab"`, depending on whether the prefix or the suffix was trimmed first. +
+ + +### Future API + +Some Python functions are not currently included in this proposal, such as trimming the suffix from a string/collection. This pitch aims to establish a pattern for using `RegexComponent` with string processing algorithms, so that further enhancement can to be introduced to the standard library easily in the future, and eventually close the gap between Swift and other popular scripting languages. From b2b35df28404be04b561d630a65bf0218df1ad9a Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Wed, 23 Mar 2022 23:07:31 -0700 Subject: [PATCH 02/10] RegexBuilder module Move the regex builder DSL (except `RegexComponent`) to a new module named RegexBuilder. The DSL depends on `DSLTree` and a few other supporting types, so those types have been made `_spi(RegexBuilder) public`. The SPI establishes an ABI between `_StringProcessing` and `RegexBuilder`, but I don't think it's a concern because the two modules will co-evolve and both will be rebuilt for every release. --- Package.swift | 13 ++- .../Participants/RegexParticipant.swift | 1 + .../RegexDSL => RegexBuilder}/Anchor.swift | 1 + .../RegexDSL => RegexBuilder}/Builder.swift | 2 + .../RegexDSL => RegexBuilder}/DSL.swift | 110 ++++++++---------- Sources/RegexBuilder/Match.swift | 28 +++++ .../RegexDSL => RegexBuilder}/Variadics.swift | 1 + .../VariadicsGenerator.swift | 3 +- .../_StringProcessing/CharacterClass.swift | 11 ++ .../{RegexDSL => Regex}/ASTConversion.swift | 0 .../{RegexDSL => Regex}/AnyRegexOutput.swift | 0 .../{RegexDSL => Regex}/Core.swift | 46 ++++++-- .../{RegexDSL => Regex}/DSLConsumers.swift | 0 .../{RegexDSL => Regex}/DSLTree.swift | 70 +++++------ .../{RegexDSL => Regex}/Match.swift | 17 +-- .../{RegexDSL => Regex}/Options.swift | 0 Tests/RegexBuilderTests/AlgorithmsTests.swift | 62 ++++++++++ .../CustomTests.swift | 18 ++- .../RegexDSLTests.swift | 3 +- Tests/RegexTests/AlgorithmsTests.swift | 46 -------- Tests/RegexTests/CaptureTests.swift | 12 +- 21 files changed, 270 insertions(+), 174 deletions(-) rename Sources/{_StringProcessing/RegexDSL => RegexBuilder}/Anchor.swift (98%) rename Sources/{_StringProcessing/RegexDSL => RegexBuilder}/Builder.swift (95%) rename Sources/{_StringProcessing/RegexDSL => RegexBuilder}/DSL.swift (75%) create mode 100644 Sources/RegexBuilder/Match.swift rename Sources/{_StringProcessing/RegexDSL => RegexBuilder}/Variadics.swift (99%) rename Sources/_StringProcessing/{RegexDSL => Regex}/ASTConversion.swift (100%) rename Sources/_StringProcessing/{RegexDSL => Regex}/AnyRegexOutput.swift (100%) rename Sources/_StringProcessing/{RegexDSL => Regex}/Core.swift (78%) rename Sources/_StringProcessing/{RegexDSL => Regex}/DSLConsumers.swift (100%) rename Sources/_StringProcessing/{RegexDSL => Regex}/DSLTree.swift (81%) rename Sources/_StringProcessing/{RegexDSL => Regex}/Match.swift (87%) rename Sources/_StringProcessing/{RegexDSL => Regex}/Options.swift (100%) create mode 100644 Tests/RegexBuilderTests/AlgorithmsTests.swift rename Tests/{RegexTests => RegexBuilderTests}/CustomTests.swift (84%) rename Tests/{RegexTests => RegexBuilderTests}/RegexDSLTests.swift (99%) diff --git a/Package.swift b/Package.swift index 72e17e362..71641ae28 100644 --- a/Package.swift +++ b/Package.swift @@ -42,13 +42,22 @@ let package = Package( .target( name: "_StringProcessing", dependencies: ["_MatchingEngine", "_CUnicode"], + swiftSettings: [ + .unsafeFlags(["-enable-library-evolution"]), + ]), + .target( + name: "RegexBuilder", + dependencies: ["_StringProcessing", "_MatchingEngine"], swiftSettings: [ .unsafeFlags(["-enable-library-evolution"]), .unsafeFlags(["-Xfrontend", "-enable-experimental-pairwise-build-block"]) ]), .testTarget( name: "RegexTests", - dependencies: ["_StringProcessing"], + dependencies: ["_StringProcessing"]), + .testTarget( + name: "RegexBuilderTests", + dependencies: ["_StringProcessing", "RegexBuilder"], swiftSettings: [ .unsafeFlags(["-Xfrontend", "-enable-experimental-pairwise-build-block"]) ]), @@ -73,7 +82,7 @@ let package = Package( // MARK: Exercises .target( name: "Exercises", - dependencies: ["_MatchingEngine", "Prototypes", "_StringProcessing"], + dependencies: ["_MatchingEngine", "Prototypes", "_StringProcessing", "RegexBuilder"], swiftSettings: [ .unsafeFlags(["-Xfrontend", "-enable-experimental-pairwise-build-block"]) ]), diff --git a/Sources/Exercises/Participants/RegexParticipant.swift b/Sources/Exercises/Participants/RegexParticipant.swift index bae3aed42..731b9b6f6 100644 --- a/Sources/Exercises/Participants/RegexParticipant.swift +++ b/Sources/Exercises/Participants/RegexParticipant.swift @@ -10,6 +10,7 @@ //===----------------------------------------------------------------------===// import _StringProcessing +import RegexBuilder /* diff --git a/Sources/_StringProcessing/RegexDSL/Anchor.swift b/Sources/RegexBuilder/Anchor.swift similarity index 98% rename from Sources/_StringProcessing/RegexDSL/Anchor.swift rename to Sources/RegexBuilder/Anchor.swift index 57d8f2ffa..ea2dde382 100644 --- a/Sources/_StringProcessing/RegexDSL/Anchor.swift +++ b/Sources/RegexBuilder/Anchor.swift @@ -10,6 +10,7 @@ //===----------------------------------------------------------------------===// import _MatchingEngine +@_spi(RegexBuilder) import _StringProcessing public struct Anchor { internal enum Kind { diff --git a/Sources/_StringProcessing/RegexDSL/Builder.swift b/Sources/RegexBuilder/Builder.swift similarity index 95% rename from Sources/_StringProcessing/RegexDSL/Builder.swift rename to Sources/RegexBuilder/Builder.swift index 78c122828..8921c8f25 100644 --- a/Sources/_StringProcessing/RegexDSL/Builder.swift +++ b/Sources/RegexBuilder/Builder.swift @@ -9,6 +9,8 @@ // //===----------------------------------------------------------------------===// +@_spi(RegexBuilder) import _StringProcessing + @resultBuilder public enum RegexComponentBuilder { public static func buildBlock() -> Regex { diff --git a/Sources/_StringProcessing/RegexDSL/DSL.swift b/Sources/RegexBuilder/DSL.swift similarity index 75% rename from Sources/_StringProcessing/RegexDSL/DSL.swift rename to Sources/RegexBuilder/DSL.swift index 4c3c382cc..816668b67 100644 --- a/Sources/_StringProcessing/RegexDSL/DSL.swift +++ b/Sources/RegexBuilder/DSL.swift @@ -10,6 +10,15 @@ //===----------------------------------------------------------------------===// import _MatchingEngine +@_spi(RegexBuilder) import _StringProcessing + +extension Regex { + public init( + @RegexComponentBuilder _ content: () -> Content + ) where Content.Output == Output { + self.init(content()) + } +} // A convenience protocol for builtin regex components that are initialized with // a `DSLTree` node. @@ -23,51 +32,6 @@ extension _BuiltinRegexComponent { } } -// MARK: - Primitives - -extension String: RegexComponent { - public typealias Output = Substring - - public var regex: Regex { - .init(node: .quotedLiteral(self)) - } -} - -extension Substring: RegexComponent { - public typealias Output = Substring - - public var regex: Regex { - .init(node: .quotedLiteral(String(self))) - } -} - -extension Character: RegexComponent { - public typealias Output = Substring - - public var regex: Regex { - .init(node: .atom(.char(self))) - } -} - -extension UnicodeScalar: RegexComponent { - public typealias Output = Substring - - public var regex: Regex { - .init(node: .atom(.scalar(self))) - } -} - -extension CharacterClass: RegexComponent { - public typealias Output = Substring - - public var regex: Regex { - guard let ast = self.makeAST() else { - fatalError("FIXME: extended AST?") - } - return Regex(ast: ast) - } -} - // MARK: - Combinators // MARK: Concatenation @@ -96,9 +60,9 @@ public struct QuantificationBehavior { case reluctantly case possessively } - + var kind: Kind - + internal var astKind: AST.Quantification.Kind { switch kind { case .eagerly: return .eager @@ -108,19 +72,49 @@ public struct QuantificationBehavior { } } +extension DSLTree.Node { + /// Generates a DSLTree node for a repeated range of the given DSLTree node. + /// Individual public API functions are in the generated Variadics.swift file. + static func repeating( + _ range: Range, + _ behavior: QuantificationBehavior, + _ node: DSLTree.Node + ) -> DSLTree.Node { + // TODO: Throw these as errors + assert(range.lowerBound >= 0, "Cannot specify a negative lower bound") + assert(!range.isEmpty, "Cannot specify an empty range") + + switch (range.lowerBound, range.upperBound) { + case (0, Int.max): // 0... + return .quantification(.zeroOrMore, behavior.astKind, node) + case (1, Int.max): // 1... + return .quantification(.oneOrMore, behavior.astKind, node) + case _ where range.count == 1: // ..<1 or ...0 or any range with count == 1 + // Note: `behavior` is ignored in this case + return .quantification(.exactly(.init(faking: range.lowerBound)), .eager, node) + case (0, _): // 0..: _BuiltinRegexComponent { // MARK: - Backreference -struct ReferenceID: Hashable, Equatable { - private static var counter: Int = 0 - var base: Int - - init() { - base = Self.counter - Self.counter += 1 - } -} - public struct Reference: RegexComponent { let id = ReferenceID() - + public init(_ captureType: Capture.Type = Capture.self) {} public var regex: Regex { .init(node: .atom(.symbolicReference(id))) } } + +extension Regex.Match { + public subscript(_ reference: Reference) -> Capture { + self[reference.id] + } +} diff --git a/Sources/RegexBuilder/Match.swift b/Sources/RegexBuilder/Match.swift new file mode 100644 index 000000000..3f86f9498 --- /dev/null +++ b/Sources/RegexBuilder/Match.swift @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// 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 _StringProcessing + +extension String { + public func match( + @RegexComponentBuilder _ content: () -> R + ) -> Regex.Match? { + match(content()) + } +} + +extension Substring { + public func match( + @RegexComponentBuilder _ content: () -> R + ) -> Regex.Match? { + match(content()) + } +} diff --git a/Sources/_StringProcessing/RegexDSL/Variadics.swift b/Sources/RegexBuilder/Variadics.swift similarity index 99% rename from Sources/_StringProcessing/RegexDSL/Variadics.swift rename to Sources/RegexBuilder/Variadics.swift index c81f8b555..60292252a 100644 --- a/Sources/_StringProcessing/RegexDSL/Variadics.swift +++ b/Sources/RegexBuilder/Variadics.swift @@ -12,6 +12,7 @@ // BEGIN AUTO-GENERATED CONTENT import _MatchingEngine +@_spi(RegexBuilder) import _StringProcessing extension RegexComponentBuilder { public static func buildPartialBlock( diff --git a/Sources/VariadicsGenerator/VariadicsGenerator.swift b/Sources/VariadicsGenerator/VariadicsGenerator.swift index 683d45e6e..1f41e68d6 100644 --- a/Sources/VariadicsGenerator/VariadicsGenerator.swift +++ b/Sources/VariadicsGenerator/VariadicsGenerator.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -// swift run VariadicsGenerator --max-arity 10 > Sources/_StringProcessing/RegexDSL/Variadics.swift +// swift run VariadicsGenerator --max-arity 10 > Sources/RegexBuilder/Variadics.swift import ArgumentParser #if os(macOS) @@ -121,6 +121,7 @@ struct VariadicsGenerator: ParsableCommand { // BEGIN AUTO-GENERATED CONTENT import _MatchingEngine + @_spi(RegexBuilder) import _StringProcessing """) diff --git a/Sources/_StringProcessing/CharacterClass.swift b/Sources/_StringProcessing/CharacterClass.swift index d72ecf06c..7989c0943 100644 --- a/Sources/_StringProcessing/CharacterClass.swift +++ b/Sources/_StringProcessing/CharacterClass.swift @@ -178,6 +178,17 @@ public struct CharacterClass: Hashable { } } +extension CharacterClass: RegexComponent { + public typealias Output = Substring + + public var regex: Regex { + guard let ast = self.makeAST() else { + fatalError("FIXME: extended AST?") + } + return Regex(ast: ast) + } +} + extension RegexComponent where Self == CharacterClass { public static var any: CharacterClass { .init(cc: .any, matchLevel: .graphemeCluster) diff --git a/Sources/_StringProcessing/RegexDSL/ASTConversion.swift b/Sources/_StringProcessing/Regex/ASTConversion.swift similarity index 100% rename from Sources/_StringProcessing/RegexDSL/ASTConversion.swift rename to Sources/_StringProcessing/Regex/ASTConversion.swift diff --git a/Sources/_StringProcessing/RegexDSL/AnyRegexOutput.swift b/Sources/_StringProcessing/Regex/AnyRegexOutput.swift similarity index 100% rename from Sources/_StringProcessing/RegexDSL/AnyRegexOutput.swift rename to Sources/_StringProcessing/Regex/AnyRegexOutput.swift diff --git a/Sources/_StringProcessing/RegexDSL/Core.swift b/Sources/_StringProcessing/Regex/Core.swift similarity index 78% rename from Sources/_StringProcessing/RegexDSL/Core.swift rename to Sources/_StringProcessing/Regex/Core.swift index 236888c77..c6433ef3e 100644 --- a/Sources/_StringProcessing/RegexDSL/Core.swift +++ b/Sources/_StringProcessing/Regex/Core.swift @@ -36,6 +36,7 @@ public struct Regex: RegexComponent { init(ast: AST) { self.tree = ast.dslTree } + init(tree: DSLTree) { self.tree = tree } @@ -44,7 +45,8 @@ public struct Regex: RegexComponent { let program: Program // var ast: AST { program.ast } - var root: DSLTree.Node { + @_spi(RegexBuilder) + public var root: DSLTree.Node { program.tree.root } @@ -59,7 +61,8 @@ public struct Regex: RegexComponent { self.program = Program(ast: .init(ast, globalOptions: nil)) } - init(node: DSLTree.Node) { + @_spi(RegexBuilder) + public init(node: DSLTree.Node) { self.program = Program(tree: .init(node, options: nil)) } @@ -84,17 +87,46 @@ public struct Regex: RegexComponent { self = content.regex } - public init( - @RegexComponentBuilder _ content: () -> Content - ) where Content.Output == Output { - self.init(content()) + public var regex: Regex { + self } +} + +// MARK: - Primitive regex components + +extension String: RegexComponent { + public typealias Output = Substring public var regex: Regex { - self + .init(node: .quotedLiteral(self)) + } +} + +extension Substring: RegexComponent { + public typealias Output = Substring + + public var regex: Regex { + .init(node: .quotedLiteral(String(self))) + } +} + +extension Character: RegexComponent { + public typealias Output = Substring + + public var regex: Regex { + .init(node: .atom(.char(self))) + } +} + +extension UnicodeScalar: RegexComponent { + public typealias Output = Substring + + public var regex: Regex { + .init(node: .atom(.scalar(self))) } } +// MARK: - Testing public struct MockRegexLiteral: RegexComponent { public typealias MatchValue = Substring diff --git a/Sources/_StringProcessing/RegexDSL/DSLConsumers.swift b/Sources/_StringProcessing/Regex/DSLConsumers.swift similarity index 100% rename from Sources/_StringProcessing/RegexDSL/DSLConsumers.swift rename to Sources/_StringProcessing/Regex/DSLConsumers.swift diff --git a/Sources/_StringProcessing/RegexDSL/DSLTree.swift b/Sources/_StringProcessing/Regex/DSLTree.swift similarity index 81% rename from Sources/_StringProcessing/RegexDSL/DSLTree.swift rename to Sources/_StringProcessing/Regex/DSLTree.swift index 25a5943c0..e579828d2 100644 --- a/Sources/_StringProcessing/RegexDSL/DSLTree.swift +++ b/Sources/_StringProcessing/Regex/DSLTree.swift @@ -11,7 +11,8 @@ import _MatchingEngine -struct DSLTree { +@_spi(RegexBuilder) +public struct DSLTree { var root: Node var options: Options? @@ -22,7 +23,8 @@ struct DSLTree { } extension DSLTree { - indirect enum Node: _TreeNode { + @_spi(RegexBuilder) + public indirect enum Node: _TreeNode { /// Try to match each node in order /// /// ... | ... | ... @@ -101,7 +103,8 @@ extension DSLTree { } extension DSLTree { - struct CustomCharacterClass { + @_spi(RegexBuilder) + public struct CustomCharacterClass { var members: [Member] var isInverted: Bool @@ -120,7 +123,8 @@ extension DSLTree { } } - enum Atom { + @_spi(RegexBuilder) + public enum Atom { case char(Character) case scalar(Unicode.Scalar) case any @@ -134,18 +138,21 @@ extension DSLTree { } // CollectionConsumer -typealias _ConsumerInterface = ( +@_spi(RegexBuilder) +public typealias _ConsumerInterface = ( String, Range ) -> String.Index? // Type producing consume // TODO: better name -typealias _MatcherInterface = ( +@_spi(RegexBuilder) +public typealias _MatcherInterface = ( String, String.Index, Range ) -> (String.Index, Any)? // Character-set (post grapheme segmentation) -typealias _CharacterPredicateInterface = ( +@_spi(RegexBuilder) +public typealias _CharacterPredicateInterface = ( (Character) -> Bool ) @@ -161,7 +168,8 @@ typealias _CharacterPredicateInterface = ( */ extension DSLTree.Node { - var children: [DSLTree.Node]? { + @_spi(RegexBuilder) + public var children: [DSLTree.Node]? { switch self { case let .orderedChoice(v): return v @@ -256,7 +264,8 @@ extension DSLTree { } } extension DSLTree.Node { - func _captureStructure( + @_spi(RegexBuilder) + public func _captureStructure( _ constructor: inout CaptureStructure.Constructor ) -> CaptureStructure { switch self { @@ -323,14 +332,18 @@ extension DSLTree.Node { } extension DSLTree.Node { - func appending(_ newNode: DSLTree.Node) -> DSLTree.Node { + @_spi(RegexBuilder) + public func appending(_ newNode: DSLTree.Node) -> DSLTree.Node { if case .concatenation(let components) = self { return .concatenation(components + [newNode]) } return .concatenation([self, newNode]) } - func appendingAlternationCase(_ newNode: DSLTree.Node) -> DSLTree.Node { + @_spi(RegexBuilder) + public func appendingAlternationCase( + _ newNode: DSLTree.Node + ) -> DSLTree.Node { if case .orderedChoice(let components) = self { return .orderedChoice(components + [newNode]) } @@ -338,32 +351,13 @@ extension DSLTree.Node { } } -extension DSLTree.Node { - /// Generates a DSLTree node for a repeated range of the given DSLTree node. - /// Individual public API functions are in the generated Variadics.swift file. - static func repeating( - _ range: Range, - _ behavior: QuantificationBehavior, - _ node: DSLTree.Node - ) -> DSLTree.Node { - // TODO: Throw these as errors - assert(range.lowerBound >= 0, "Cannot specify a negative lower bound") - assert(!range.isEmpty, "Cannot specify an empty range") - - switch (range.lowerBound, range.upperBound) { - case (0, Int.max): // 0... - return .quantification(.zeroOrMore, behavior.astKind, node) - case (1, Int.max): // 1... - return .quantification(.oneOrMore, behavior.astKind, node) - case _ where range.count == 1: // ..<1 or ...0 or any range with count == 1 - // Note: `behavior` is ignored in this case - return .quantification(.exactly(.init(faking: range.lowerBound)), .eager, node) - case (0, _): // 0..(_ reference: Reference) -> Capture { - guard let offset = referencedCaptureOffsets[reference.id] else { + @_spi(RegexBuilder) + public subscript(_ id: ReferenceID) -> Capture { + guard let offset = referencedCaptureOffsets[id] else { preconditionFailure( "Reference did not capture any match in the regex") } @@ -98,21 +99,9 @@ extension String { public func match(_ regex: R) -> Regex.Match? { regex.match(in: self) } - - public func match( - @RegexComponentBuilder _ content: () -> R - ) -> Regex.Match? { - match(content()) - } } extension Substring { public func match(_ regex: R) -> Regex.Match? { regex.match(in: self) } - - public func match( - @RegexComponentBuilder _ content: () -> R - ) -> Regex.Match? { - match(content()) - } } diff --git a/Sources/_StringProcessing/RegexDSL/Options.swift b/Sources/_StringProcessing/Regex/Options.swift similarity index 100% rename from Sources/_StringProcessing/RegexDSL/Options.swift rename to Sources/_StringProcessing/Regex/Options.swift diff --git a/Tests/RegexBuilderTests/AlgorithmsTests.swift b/Tests/RegexBuilderTests/AlgorithmsTests.swift new file mode 100644 index 000000000..183d247a7 --- /dev/null +++ b/Tests/RegexBuilderTests/AlgorithmsTests.swift @@ -0,0 +1,62 @@ +//===----------------------------------------------------------------------===// +// +// 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 +import _StringProcessing +@testable import RegexBuilder + +class RegexConsumerTests: XCTestCase { + func testMatches() { + let regex = Capture(OneOrMore(.digit)) { 2 * Int($0)! } + let str = "foo 160 bar 99 baz" + XCTAssertEqual(str.matches(of: regex).map(\.result.1), [320, 198]) + } + + func testMatchReplace() { + func replaceTest( + _ regex: R, + input: String, + result: String, + _ replace: (_MatchResult>) -> String, + file: StaticString = #file, + line: UInt = #line + ) { + XCTAssertEqual(input.replacing(regex, with: replace), result) + } + + let int = Capture(OneOrMore(.digit)) { Int($0)! } + + replaceTest( + int, + input: "foo 160 bar 99 baz", + result: "foo 240 bar 143 baz", + { match in String(match.result.1, radix: 8) }) + + replaceTest( + Regex { int; "+"; int }, + input: "9+16, 0+3, 5+5, 99+1", + result: "25, 3, 10, 100", + { match in "\(match.result.1 + match.result.2)" }) + + // TODO: Need to support capture history + // replaceTest( + // OneOrMore { int; "," }, + // input: "3,5,8,0, 1,0,2,-5,x8,8,", + // result: "16 3-5x16", + // { match in "\(match.result.1.reduce(0, +))" }) + + replaceTest( + Regex { int; "x"; int; Optionally { "x"; int } }, + input: "2x3 5x4x3 6x0 1x2x3x4", + result: "6 60 0 6x4", + { match in "\(match.result.1 * match.result.2 * (match.result.3 ?? 1))" }) + } +} diff --git a/Tests/RegexTests/CustomTests.swift b/Tests/RegexBuilderTests/CustomTests.swift similarity index 84% rename from Tests/RegexTests/CustomTests.swift rename to Tests/RegexBuilderTests/CustomTests.swift index 12d4ad6cd..b405a5399 100644 --- a/Tests/RegexTests/CustomTests.swift +++ b/Tests/RegexBuilderTests/CustomTests.swift @@ -1,5 +1,17 @@ -import _StringProcessing +//===----------------------------------------------------------------------===// +// +// 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 +import _StringProcessing +@testable import RegexBuilder // A nibbler processes a single character from a string private protocol Nibbler: CustomRegexComponent { @@ -58,8 +70,7 @@ func customTest( } } -extension RegexTests { - +class CustomRegexComponentTests: XCTestCase { // TODO: Refactor below into more exhaustive, declarative // tests. func testCustomRegexComponents() { @@ -127,5 +138,4 @@ extension RegexTests { XCTAssertEqual(res4.result.0, "123") XCTAssertEqual(res4.result.1, 3) } - } diff --git a/Tests/RegexTests/RegexDSLTests.swift b/Tests/RegexBuilderTests/RegexDSLTests.swift similarity index 99% rename from Tests/RegexTests/RegexDSLTests.swift rename to Tests/RegexBuilderTests/RegexDSLTests.swift index 4ca446b32..a7d307b9b 100644 --- a/Tests/RegexTests/RegexDSLTests.swift +++ b/Tests/RegexBuilderTests/RegexDSLTests.swift @@ -10,7 +10,8 @@ //===----------------------------------------------------------------------===// import XCTest -@testable import _StringProcessing +import _StringProcessing +@testable import RegexBuilder class RegexDSLTests: XCTestCase { func _testDSLCaptures( diff --git a/Tests/RegexTests/AlgorithmsTests.swift b/Tests/RegexTests/AlgorithmsTests.swift index 6a7bf646b..b51f12100 100644 --- a/Tests/RegexTests/AlgorithmsTests.swift +++ b/Tests/RegexTests/AlgorithmsTests.swift @@ -114,52 +114,6 @@ class RegexConsumerTests: XCTestCase { expectReplace("aab", "a+", "X", "Xb") expectReplace("aab", "a*", "X", "XXbX") } - - func testMatches() { - let regex = Capture(OneOrMore(.digit)) { 2 * Int($0)! } - let str = "foo 160 bar 99 baz" - XCTAssertEqual(str.matches(of: regex).map(\.result.1), [320, 198]) - } - - func testMatchReplace() { - func replaceTest( - _ regex: R, - input: String, - result: String, - _ replace: (_MatchResult>) -> String, - file: StaticString = #file, - line: UInt = #line - ) { - XCTAssertEqual(input.replacing(regex, with: replace), result) - } - - let int = Capture(OneOrMore(.digit)) { Int($0)! } - - replaceTest( - int, - input: "foo 160 bar 99 baz", - result: "foo 240 bar 143 baz", - { match in String(match.result.1, radix: 8) }) - - replaceTest( - Regex { int; "+"; int }, - input: "9+16, 0+3, 5+5, 99+1", - result: "25, 3, 10, 100", - { match in "\(match.result.1 + match.result.2)" }) - - // TODO: Need to support capture history - // replaceTest( - // OneOrMore { int; "," }, - // input: "3,5,8,0, 1,0,2,-5,x8,8,", - // result: "16 3-5x16", - // { match in "\(match.result.1.reduce(0, +))" }) - - replaceTest( - Regex { int; "x"; int; Optionally { "x"; int } }, - input: "2x3 5x4x3 6x0 1x2x3x4", - result: "6 60 0 6x4", - { match in "\(match.result.1 * match.result.2 * (match.result.3 ?? 1))" }) - } func testAdHoc() { let r = try! Regex("a|b+") diff --git a/Tests/RegexTests/CaptureTests.swift b/Tests/RegexTests/CaptureTests.swift index cc3568c1d..258aea86d 100644 --- a/Tests/RegexTests/CaptureTests.swift +++ b/Tests/RegexTests/CaptureTests.swift @@ -1,6 +1,16 @@ +//===----------------------------------------------------------------------===// +// +// 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 +@testable @_spi(RegexBuilder) import _StringProcessing import _MatchingEngine extension StructuredCapture { From bf7710d49a019497a8a67230ae048935f1c75cc9 Mon Sep 17 00:00:00 2001 From: Harlan Haskins Date: Fri, 25 Mar 2022 21:19:59 -0600 Subject: [PATCH 03/10] Avoid conforming String to Error in this module This seems like an overly-broad conformance to add to swift-experimental-string-processing, and it doesn't look like anyone is calling report(). Do we need this? --- Sources/_MatchingEngine/Regex/Parse/Parse.swift | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Sources/_MatchingEngine/Regex/Parse/Parse.swift b/Sources/_MatchingEngine/Regex/Parse/Parse.swift index e7b687912..296956fdc 100644 --- a/Sources/_MatchingEngine/Regex/Parse/Parse.swift +++ b/Sources/_MatchingEngine/Regex/Parse/Parse.swift @@ -122,15 +122,6 @@ extension ParsingContext { // Diagnostics extension Parser { - mutating func report( - _ str: String, _ function: String = #function, _ line: Int = #line - ) throws -> Never { - throw """ - ERROR: \(str) - (error detected in parser at \(function):\(line)) - """ - } - fileprivate func loc( _ start: Source.Position ) -> SourceLocation { @@ -529,5 +520,3 @@ public func parseWithDelimiters( let (contents, delim) = droppingRegexDelimiters(String(regex)) return try parse(contents, delim.defaultSyntaxOptions) } - -extension String: Error {} From bf8c1a92ad072e4bdfbcffb44a08813d5aeaea5d Mon Sep 17 00:00:00 2001 From: Harlan Haskins Date: Fri, 25 Mar 2022 22:12:29 -0600 Subject: [PATCH 04/10] Update tests as well --- Tests/RegexTests/MatchTests.swift | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Tests/RegexTests/MatchTests.swift b/Tests/RegexTests/MatchTests.swift index dba72820f..1d5b436ff 100644 --- a/Tests/RegexTests/MatchTests.swift +++ b/Tests/RegexTests/MatchTests.swift @@ -13,6 +13,13 @@ import XCTest @testable import _MatchingEngine @testable import _StringProcessing +struct MatchError: Error { + var message: String + init(_ message: String) { + self.message = message + } +} + extension Executor { func _firstMatch( _ regex: String, input: String, @@ -31,7 +38,7 @@ extension Executor { let caps = result.rawCaptures.slices(from: input) return (input[result.range], caps) } else if start == input.endIndex { - throw "match not found for \(regex) in \(input)" + throw MatchError("match not found for \(regex) in \(input)") } else { input.formIndex(after: &start) } @@ -76,27 +83,29 @@ func flatCaptureTest( if expect == nil { continue } else { - throw "Match failed" + throw MatchError("Match failed") } } guard let expect = expect else { - throw "Match of \(test) succeeded where failure expected in \(regex)" + throw MatchError(""" + Match of \(test) succeeded where failure expected in \(regex) + """) } let capStrs = caps.map { $0 == nil ? nil : String($0!) } guard expect.count == capStrs.count else { - throw """ + throw MatchError(""" Capture count mismatch: \(expect) \(capStrs) - """ + """) } guard expect.elementsEqual(capStrs) else { - throw """ + throw MatchError(""" Capture mismatch: \(expect) \(capStrs) - """ + """) } } catch { if !xfail { From 84311bb5644aae432eb3a76d399ea96ef5f9e58b Mon Sep 17 00:00:00 2001 From: Harlan Haskins Date: Fri, 25 Mar 2022 22:59:46 -0600 Subject: [PATCH 05/10] Remove error-conforming string from Participant --- Sources/Exercises/Participant.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/Exercises/Participant.swift b/Sources/Exercises/Participant.swift index 2f7ec183b..0654569b6 100644 --- a/Sources/Exercises/Participant.swift +++ b/Sources/Exercises/Participant.swift @@ -31,20 +31,22 @@ public protocol Participant { // Default impls extension Participant { - static var unsupported: Error { "Unsupported" } + enum Error: Swift.Error { + case unsupported + } // Produce a function that will parse a grapheme break entry from a line public static func graphemeBreakProperty() throws -> (String) -> GraphemeBreakEntry? { - throw unsupported + throw Error.unsupported } // Produce a function that will extract the bodies of C-style comments from its input public static func cComments() throws -> (String) -> [Substring] { - throw unsupported + throw Error.unsupported } // Produce a function that will extract the bodies of Swift-style comments from its input public static func swiftComments() throws -> (String) -> [Substring] { - throw unsupported + throw Error.unsupported } } From b400e3e536083628a532ceeac8da87eb983759d1 Mon Sep 17 00:00:00 2001 From: Harlan Haskins Date: Sat, 26 Mar 2022 10:03:14 -0600 Subject: [PATCH 06/10] Un-nest Error --- Sources/Exercises/Participant.swift | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/Sources/Exercises/Participant.swift b/Sources/Exercises/Participant.swift index 0654569b6..ad20d7fbd 100644 --- a/Sources/Exercises/Participant.swift +++ b/Sources/Exercises/Participant.swift @@ -29,24 +29,25 @@ public protocol Participant { // ... } +// Errors that may be thrown from default implementations +private enum ParticipantError: Error { + case unsupported +} + // Default impls extension Participant { - enum Error: Swift.Error { - case unsupported - } - // Produce a function that will parse a grapheme break entry from a line public static func graphemeBreakProperty() throws -> (String) -> GraphemeBreakEntry? { - throw Error.unsupported + throw ParticipantError.unsupported } // Produce a function that will extract the bodies of C-style comments from its input public static func cComments() throws -> (String) -> [Substring] { - throw Error.unsupported + throw ParticipantError.unsupported } // Produce a function that will extract the bodies of Swift-style comments from its input public static func swiftComments() throws -> (String) -> [Substring] { - throw Error.unsupported + throw ParticipantError.unsupported } } From 656f24b46c6776932d777f04d3f628ee0f74c553 Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Mon, 28 Mar 2022 11:53:25 -0700 Subject: [PATCH 07/10] Remove operators from regex builder. https://github.com/apple/swift-experimental-string-processing/blob/main/Documentation/Evolution/RegexBuilderDSL.md#operators-for-quantification-and-alternation --- Sources/RegexBuilder/DSL.swift | 4 - Sources/RegexBuilder/Variadics.swift | 461 ------------------ .../VariadicsGenerator.swift | 11 - Tests/RegexBuilderTests/RegexDSLTests.swift | 53 +- 4 files changed, 31 insertions(+), 498 deletions(-) diff --git a/Sources/RegexBuilder/DSL.swift b/Sources/RegexBuilder/DSL.swift index 816668b67..9107a9719 100644 --- a/Sources/RegexBuilder/DSL.swift +++ b/Sources/RegexBuilder/DSL.swift @@ -165,10 +165,6 @@ public struct Repeat: _BuiltinRegexComponent { // Variadics.swift. } -postfix operator .? -postfix operator .* -postfix operator .+ - // MARK: Alternation // TODO: Variadic generics diff --git a/Sources/RegexBuilder/Variadics.swift b/Sources/RegexBuilder/Variadics.swift index 60292252a..3cf154627 100644 --- a/Sources/RegexBuilder/Variadics.swift +++ b/Sources/RegexBuilder/Variadics.swift @@ -498,13 +498,6 @@ extension Optionally { } } -@_disfavoredOverload -public postfix func .?( - _ component: Component -) -> Optionally { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -532,13 +525,6 @@ extension ZeroOrMore { } } -@_disfavoredOverload -public postfix func .*( - _ component: Component -) -> ZeroOrMore { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { @_disfavoredOverload @@ -560,13 +546,6 @@ extension OneOrMore { } } -@_disfavoredOverload -public postfix func .+( - _ component: Component -) -> OneOrMore { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { @_disfavoredOverload @@ -625,12 +604,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?)> where Component.Output == (W, C0) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -656,12 +629,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?)> where Component.Output == (W, C0) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -681,12 +648,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0)> where Component.Output == (W, C0) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -741,12 +702,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?, C1?)> where Component.Output == (W, C0, C1) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -772,12 +727,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?, C1?)> where Component.Output == (W, C0, C1) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -797,12 +746,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0, C1)> where Component.Output == (W, C0, C1) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -857,12 +800,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?, C1?, C2?)> where Component.Output == (W, C0, C1, C2) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -888,12 +825,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?, C1?, C2?)> where Component.Output == (W, C0, C1, C2) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -913,12 +844,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0, C1, C2)> where Component.Output == (W, C0, C1, C2) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -973,12 +898,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?, C1?, C2?, C3?)> where Component.Output == (W, C0, C1, C2, C3) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -1004,12 +923,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?, C1?, C2?, C3?)> where Component.Output == (W, C0, C1, C2, C3) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -1029,12 +942,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0, C1, C2, C3)> where Component.Output == (W, C0, C1, C2, C3) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -1089,12 +996,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?, C1?, C2?, C3?, C4?)> where Component.Output == (W, C0, C1, C2, C3, C4) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -1120,12 +1021,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?, C1?, C2?, C3?, C4?)> where Component.Output == (W, C0, C1, C2, C3, C4) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -1145,12 +1040,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0, C1, C2, C3, C4)> where Component.Output == (W, C0, C1, C2, C3, C4) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -1205,12 +1094,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?, C1?, C2?, C3?, C4?, C5?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -1236,12 +1119,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?, C1?, C2?, C3?, C4?, C5?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -1261,12 +1138,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0, C1, C2, C3, C4, C5)> where Component.Output == (W, C0, C1, C2, C3, C4, C5) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -1321,12 +1192,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -1352,12 +1217,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -1377,12 +1236,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0, C1, C2, C3, C4, C5, C6)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -1437,12 +1290,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6, C7) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -1468,12 +1315,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6, C7) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -1493,12 +1334,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0, C1, C2, C3, C4, C5, C6, C7)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6, C7) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -1553,12 +1388,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -1584,12 +1413,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -1609,12 +1432,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -1669,12 +1486,6 @@ extension Optionally { } } -public postfix func .?( - _ component: Component -) -> Optionally<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9) { - .init(node: .quantification(.zeroOrOne, .eager, component.regex.root)) -} - extension RegexComponentBuilder { public static func buildLimitedAvailability( _ component: Component @@ -1700,12 +1511,6 @@ extension ZeroOrMore { } } -public postfix func .*( - _ component: Component -) -> ZeroOrMore<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9) { - .init(node: .quantification(.zeroOrMore, .eager, component.regex.root)) -} - extension OneOrMore { public init( @@ -1725,12 +1530,6 @@ extension OneOrMore { } } -public postfix func .+( - _ component: Component -) -> OneOrMore<(Substring, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9)> where Component.Output == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9) { - .init(node: .quantification(.oneOrMore, .eager, component.regex.root)) -} - extension Repeat { public init( @@ -1774,10 +1573,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf where R0: RegexComponent, R1: RegexComponent { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1785,10 +1580,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1796,10 +1587,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?, C1?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0, C1) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1807,10 +1594,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?, C1?, C2?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0, C1, C2) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1818,10 +1601,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?, C1?, C2?, C3?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0, C1, C2, C3) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1829,10 +1608,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?, C1?, C2?, C3?, C4?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0, C1, C2, C3, C4) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1840,10 +1615,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?, C1?, C2?, C3?, C4?, C5?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0, C1, C2, C3, C4, C5) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1851,10 +1622,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0, C1, C2, C3, C4, C5, C6) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1862,10 +1629,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0, C1, C2, C3, C4, C5, C6, C7) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1873,10 +1636,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0, C1, C2, C3, C4, C5, C6, C7, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1884,10 +1643,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?)> where R0: RegexComponent, R1: RegexComponent, R1.Output == (W1, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1895,10 +1650,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1906,10 +1657,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0), R1.Output == (W1, C1) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1917,10 +1664,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1?, C2?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0), R1.Output == (W1, C1, C2) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1928,10 +1671,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1?, C2?, C3?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0), R1.Output == (W1, C1, C2, C3) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1939,10 +1678,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1?, C2?, C3?, C4?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0), R1.Output == (W1, C1, C2, C3, C4) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1950,10 +1685,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1?, C2?, C3?, C4?, C5?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0), R1.Output == (W1, C1, C2, C3, C4, C5) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1961,10 +1692,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1?, C2?, C3?, C4?, C5?, C6?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0), R1.Output == (W1, C1, C2, C3, C4, C5, C6) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1972,10 +1699,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1?, C2?, C3?, C4?, C5?, C6?, C7?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0), R1.Output == (W1, C1, C2, C3, C4, C5, C6, C7) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1983,10 +1706,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0), R1.Output == (W1, C1, C2, C3, C4, C5, C6, C7, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -1994,10 +1713,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0), R1.Output == (W1, C1, C2, C3, C4, C5, C6, C7, C8, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2005,10 +1720,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2016,10 +1727,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1), R1.Output == (W1, C2) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2027,10 +1734,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2?, C3?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1), R1.Output == (W1, C2, C3) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2038,10 +1741,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2?, C3?, C4?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1), R1.Output == (W1, C2, C3, C4) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2049,10 +1748,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2?, C3?, C4?, C5?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1), R1.Output == (W1, C2, C3, C4, C5) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2060,10 +1755,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2?, C3?, C4?, C5?, C6?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1), R1.Output == (W1, C2, C3, C4, C5, C6) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2071,10 +1762,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2?, C3?, C4?, C5?, C6?, C7?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1), R1.Output == (W1, C2, C3, C4, C5, C6, C7) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2082,10 +1769,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2?, C3?, C4?, C5?, C6?, C7?, C8?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1), R1.Output == (W1, C2, C3, C4, C5, C6, C7, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2093,10 +1776,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1), R1.Output == (W1, C2, C3, C4, C5, C6, C7, C8, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2104,10 +1783,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2115,10 +1790,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2), R1.Output == (W1, C3) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2126,10 +1797,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3?, C4?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2), R1.Output == (W1, C3, C4) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2137,10 +1804,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3?, C4?, C5?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2), R1.Output == (W1, C3, C4, C5) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2148,10 +1811,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3?, C4?, C5?, C6?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2), R1.Output == (W1, C3, C4, C5, C6) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2159,10 +1818,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3?, C4?, C5?, C6?, C7?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2), R1.Output == (W1, C3, C4, C5, C6, C7) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2170,10 +1825,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3?, C4?, C5?, C6?, C7?, C8?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2), R1.Output == (W1, C3, C4, C5, C6, C7, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2181,10 +1832,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3?, C4?, C5?, C6?, C7?, C8?, C9?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2), R1.Output == (W1, C3, C4, C5, C6, C7, C8, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2192,10 +1839,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2203,10 +1846,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3), R1.Output == (W1, C4) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2214,10 +1853,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4?, C5?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3), R1.Output == (W1, C4, C5) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2225,10 +1860,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4?, C5?, C6?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3), R1.Output == (W1, C4, C5, C6) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2236,10 +1867,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4?, C5?, C6?, C7?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3), R1.Output == (W1, C4, C5, C6, C7) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2247,10 +1874,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4?, C5?, C6?, C7?, C8?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3), R1.Output == (W1, C4, C5, C6, C7, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2258,10 +1881,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4?, C5?, C6?, C7?, C8?, C9?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3), R1.Output == (W1, C4, C5, C6, C7, C8, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2269,10 +1888,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2280,10 +1895,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4), R1.Output == (W1, C5) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2291,10 +1902,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5?, C6?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4), R1.Output == (W1, C5, C6) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2302,10 +1909,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5?, C6?, C7?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4), R1.Output == (W1, C5, C6, C7) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2313,10 +1916,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5?, C6?, C7?, C8?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4), R1.Output == (W1, C5, C6, C7, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2324,10 +1923,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5?, C6?, C7?, C8?, C9?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4), R1.Output == (W1, C5, C6, C7, C8, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2335,10 +1930,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2346,10 +1937,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5), R1.Output == (W1, C6) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2357,10 +1944,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6?, C7?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5), R1.Output == (W1, C6, C7) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2368,10 +1951,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6?, C7?, C8?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5), R1.Output == (W1, C6, C7, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2379,10 +1958,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6?, C7?, C8?, C9?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5), R1.Output == (W1, C6, C7, C8, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2390,10 +1965,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5, C6) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2401,10 +1972,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6, C7?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5, C6), R1.Output == (W1, C7) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2412,10 +1979,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6, C7?, C8?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5, C6), R1.Output == (W1, C7, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2423,10 +1986,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6, C7?, C8?, C9?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5, C6), R1.Output == (W1, C7, C8, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2434,10 +1993,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6, C7)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5, C6, C7) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2445,10 +2000,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6, C7, C8?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5, C6, C7), R1.Output == (W1, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2456,10 +2007,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6, C7, C8?, C9?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5, C6, C7), R1.Output == (W1, C8, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2467,10 +2014,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6, C7, C8)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5, C6, C7, C8) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock( accumulated: R0, next: R1 @@ -2478,10 +2021,6 @@ extension AlternationBuilder { .init(node: accumulated.regex.root.appendingAlternationCase(next.regex.root)) } } - -public func | (lhs: R0, rhs: R1) -> ChoiceOf<(Substring, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9?)> where R0: RegexComponent, R1: RegexComponent, R0.Output == (W0, C0, C1, C2, C3, C4, C5, C6, C7, C8), R1.Output == (W1, C9) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) -} extension AlternationBuilder { public static func buildPartialBlock(first regex: R) -> ChoiceOf<(W, C0?)> where R: RegexComponent, R.Output == (W, C0) { .init(node: .orderedChoice([regex.regex.root])) diff --git a/Sources/VariadicsGenerator/VariadicsGenerator.swift b/Sources/VariadicsGenerator/VariadicsGenerator.swift index 1f41e68d6..e03f81831 100644 --- a/Sources/VariadicsGenerator/VariadicsGenerator.swift +++ b/Sources/VariadicsGenerator/VariadicsGenerator.swift @@ -380,13 +380,6 @@ struct VariadicsGenerator: ParsableCommand { } } - \(params.disfavored)\ - public postfix func \(kind.operatorName)<\(params.genericParams)>( - _ component: Component - ) -> \(kind.rawValue)<\(params.matchType)> \(params.whereClause) { - .init(node: .quantification(.\(kind.astQuantifierAmount), .eager, component.regex.root)) - } - \(kind == .zeroOrOne ? """ extension \(concatBuilderName) { @@ -499,10 +492,6 @@ struct VariadicsGenerator: ParsableCommand { } } - public func | <\(genericParams)>(lhs: R0, rhs: R1) -> ChoiceOf<\(matchType)> \(whereClause) { - .init(node: lhs.regex.root.appendingAlternationCase(rhs.regex.root)) - } - """) } diff --git a/Tests/RegexBuilderTests/RegexDSLTests.swift b/Tests/RegexBuilderTests/RegexDSLTests.swift index a7d307b9b..42f9be1b4 100644 --- a/Tests/RegexBuilderTests/RegexDSLTests.swift +++ b/Tests/RegexBuilderTests/RegexDSLTests.swift @@ -99,12 +99,14 @@ class RegexDSLTests: XCTestCase { do { let regex = Regex { "ab" - Capture { - ChoiceOf { - "c" - "def" + OneOrMore { + Capture { + ChoiceOf { + "c" + "def" + } } - }.+ + } } XCTAssertTrue( try XCTUnwrap("abc".match(regex)?.output) == ("abc", "c")) @@ -149,12 +151,17 @@ class RegexDSLTests: XCTestCase { ("aaaabccccdddkj", ("aaaabccccdddkj", "b", "cccc", "d", "k", nil, "j")), matchType: (Substring, Substring, Substring, Substring?, Substring, Substring?, Substring?).self, ==) { - "a".+ + OneOrMore("a") Capture(OneOrMore(Character("b"))) // Substring Capture(ZeroOrMore("c")) // Substring - Capture(.hexDigit).* // Substring? - "e".? - Capture("t" | "k") // Substring + ZeroOrMore(Capture(.hexDigit)) // Substring? + Optionally("e") + Capture { + ChoiceOf { + "t" + "k" + } + } // Substring ChoiceOf { Capture("k"); Capture("j") } // (Substring?, Substring?) } } @@ -276,7 +283,7 @@ class RegexDSLTests: XCTestCase { matchType: Substring.self, ==) { Anchor.startOfLine - "a".+ + OneOrMore("a") "b" Anchor.endOfLine } @@ -298,7 +305,7 @@ class RegexDSLTests: XCTestCase { ("aaaaab", nil), matchType: Substring.self, ==) { - "a".+ + OneOrMore("a") lookahead(CharacterClass.digit) lookahead("2", negative: true) CharacterClass.word @@ -361,7 +368,7 @@ class RegexDSLTests: XCTestCase { ("aaa ", ("aaa ", nil, nil)), matchType: (Substring, Int?, Word?).self, ==) { - "a".+ + OneOrMore("a") OneOrMore(.whitespace) Optionally { Capture(OneOrMore(.digit)) { Int($0)! } @@ -375,43 +382,45 @@ class RegexDSLTests: XCTestCase { func testNestedCaptureTypes() throws { let regex1 = Regex { - "a".+ + OneOrMore("a") Capture { Capture(OneOrMore("b")) - "e".? + Optionally("e") } } let _: (Substring, Substring, Substring).Type = type(of: regex1).Output.self let regex2 = Regex { - "a".+ + OneOrMore("a") Capture { - TryCapture("b") { Int($0) }.* - "e".? + ZeroOrMore { + TryCapture("b") { Int($0) } + } + Optionally("e") } } let _: (Substring, Substring, Int?).Type = type(of: regex2).Output.self let regex3 = Regex { - "a".+ + OneOrMore("a") Capture { TryCapture("b") { Int($0) } ZeroOrMore { TryCapture("c") { Double($0) } } - "e".? + Optionally("e") } } let _: (Substring, Substring, Int, Double?).Type = type(of: regex3).Output.self let regex4 = Regex { - "a".+ + OneOrMore("a") Capture { OneOrMore { Capture(OneOrMore("b")) Capture(ZeroOrMore("c")) - Capture("d").* - "e".? + ZeroOrMore(Capture("d")) + Optionally("e") } } } From 58005fd0703eb6e3b9d13e8c42248cd42b218252 Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Tue, 29 Mar 2022 13:58:12 -0700 Subject: [PATCH 08/10] Change `lookahead` function to `Lookahead` type --- Sources/RegexBuilder/Anchor.swift | 34 +++++++++++++-------- Tests/RegexBuilderTests/RegexDSLTests.swift | 4 +-- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Sources/RegexBuilder/Anchor.swift b/Sources/RegexBuilder/Anchor.swift index ea2dde382..51854ab70 100644 --- a/Sources/RegexBuilder/Anchor.swift +++ b/Sources/RegexBuilder/Anchor.swift @@ -107,16 +107,26 @@ extension Anchor { } } -public func lookahead( - negative: Bool = false, - @RegexComponentBuilder _ content: () -> R -) -> Regex { - Regex(node: .nonCapturingGroup(negative ? .negativeLookahead : .lookahead, content().regex.root)) -} - -public func lookahead( - _ component: R, - negative: Bool = false -) -> Regex { - Regex(node: .nonCapturingGroup(negative ? .negativeLookahead : .lookahead, component.regex.root)) +public struct Lookahead: _BuiltinRegexComponent { + public var regex: Regex + + init(_ regex: Regex) { + self.regex = regex + } + + public init( + _ component: R, + negative: Bool = false + ) where R.Output == Output { + self.init(node: .nonCapturingGroup( + negative ? .negativeLookahead : .lookahead, component.regex.root)) + } + + public init( + negative: Bool = false, + @RegexComponentBuilder _ component: () -> R + ) where R.Output == Output { + self.init(node: .nonCapturingGroup( + negative ? .negativeLookahead : .lookahead, component().regex.root)) + } } diff --git a/Tests/RegexBuilderTests/RegexDSLTests.swift b/Tests/RegexBuilderTests/RegexDSLTests.swift index 42f9be1b4..628d9cfdd 100644 --- a/Tests/RegexBuilderTests/RegexDSLTests.swift +++ b/Tests/RegexBuilderTests/RegexDSLTests.swift @@ -306,8 +306,8 @@ class RegexDSLTests: XCTestCase { matchType: Substring.self, ==) { OneOrMore("a") - lookahead(CharacterClass.digit) - lookahead("2", negative: true) + Lookahead(CharacterClass.digit) + Lookahead("2", negative: true) CharacterClass.word } } From 3e3654e3141efa5ce8bf02051b0c25e0f16b50cc Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Tue, 29 Mar 2022 14:07:32 -0700 Subject: [PATCH 09/10] Remove `Regex.init(_: R)`. `Regex.init(_: R)` has existed as a way to convert any `RegexComponent` to a `Regex`. However, it is not super useful for the end user. More importantly, its existence seems to cause bad error messages in regex builder closures. When this initializer is deleted, error messages become better and more localized. The user can still obtain a `Regex` from any `RegexComponent` just by calling the `regex` property. --- Sources/RegexBuilder/DSL.swift | 2 +- Sources/_StringProcessing/Regex/Core.swift | 6 ------ Tests/RegexBuilderTests/RegexDSLTests.swift | 2 +- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/Sources/RegexBuilder/DSL.swift b/Sources/RegexBuilder/DSL.swift index 9107a9719..e6abf2ff0 100644 --- a/Sources/RegexBuilder/DSL.swift +++ b/Sources/RegexBuilder/DSL.swift @@ -16,7 +16,7 @@ extension Regex { public init( @RegexComponentBuilder _ content: () -> Content ) where Content.Output == Output { - self.init(content()) + self = content().regex } } diff --git a/Sources/_StringProcessing/Regex/Core.swift b/Sources/_StringProcessing/Regex/Core.swift index c6433ef3e..d51b72749 100644 --- a/Sources/_StringProcessing/Regex/Core.swift +++ b/Sources/_StringProcessing/Regex/Core.swift @@ -81,12 +81,6 @@ public struct Regex: RegexComponent { self.init(ast: try! parseWithDelimiters(pattern)) } - public init( - _ content: Content - ) where Content.Output == Output { - self = content.regex - } - public var regex: Regex { self } diff --git a/Tests/RegexBuilderTests/RegexDSLTests.swift b/Tests/RegexBuilderTests/RegexDSLTests.swift index 42f9be1b4..7523b5496 100644 --- a/Tests/RegexBuilderTests/RegexDSLTests.swift +++ b/Tests/RegexBuilderTests/RegexDSLTests.swift @@ -22,7 +22,7 @@ class RegexDSLTests: XCTestCase { line: UInt = #line, @RegexComponentBuilder _ content: () -> Content ) throws { - let regex = Regex(content()) + let regex = content() for (input, maybeExpectedCaptures) in tests { let maybeMatch = input.match(regex) if let expectedCaptures = maybeExpectedCaptures { From 96da785d50a6063c4835b3081c1457f544344442 Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Mon, 28 Mar 2022 13:09:23 -0700 Subject: [PATCH 10/10] Rename _MatchingEngine to _RegexParser and fix CMake. The _MatchingEngine module no longer contains the matching engine. It has the regex AST and parser. This patch renames it to `_RegexParser`. Also fix the CMake build which has been broken for a while. --- Package.swift | 18 +++--- README.md | 9 ++- Sources/CMakeLists.txt | 4 +- .../PatternConverter/PatternConverter.swift | 4 +- Sources/Prototypes/CMakeLists.txt | 2 +- .../Prototypes/Combinators/Combinators.swift | 2 +- Sources/RegexBuilder/Anchor.swift | 2 +- Sources/RegexBuilder/CMakeLists.txt | 10 +++ Sources/RegexBuilder/DSL.swift | 2 +- Sources/RegexBuilder/Variadics.swift | 2 +- .../VariadicsGenerator.swift | 2 +- Sources/_MatchingEngine/CMakeLists.txt | 46 -------------- Sources/_RegexParser/CMakeLists.txt | 28 +++++++++ .../Regex/AST/AST.swift | 0 .../Regex/AST/ASTAction.swift | 0 .../Regex/AST/ASTProtocols.swift | 0 .../Regex/AST/Atom.swift | 0 .../Regex/AST/Conditional.swift | 0 .../Regex/AST/CustomCharClass.swift | 0 .../Regex/AST/Group.swift | 0 .../Regex/AST/MatchingOptions.swift | 0 .../Regex/AST/Quantification.swift | 0 .../Regex/Parse/CaptureStructure.swift | 0 .../CharacterPropertyClassification.swift | 0 .../Regex/Parse/DelimiterLexing.swift | 0 .../Regex/Parse/Diagnostics.swift | 0 .../Regex/Parse/LexicalAnalysis.swift | 0 .../Regex/Parse/Mocking.swift | 0 .../Regex/Parse/Parse.swift | 0 .../Regex/Parse/Source.swift | 0 .../Regex/Parse/SourceLocation.swift | 0 .../Regex/Parse/SyntaxOptions.swift | 0 .../Regex/Printing/DumpAST.swift | 0 .../Regex/Printing/PrettyPrinter.swift | 0 .../Regex/Printing/PrintAsCanonical.swift | 0 .../Regex/Printing/RenderRanges.swift | 0 .../Regex/TreeProtocols.swift | 0 .../Utility/AllScalars.swift | 0 .../Utility/Errors.swift | 0 .../Utility/Misc.swift | 0 .../Utility/MissingUnicode.swift | 0 .../Utility/TypeConstruction.swift | 0 Sources/_StringProcessing/ByteCodeGen.swift | 2 +- Sources/_StringProcessing/CMakeLists.txt | 62 +++++++++++++++---- Sources/_StringProcessing/Capture.swift | 2 +- .../_StringProcessing/CharacterClass.swift | 2 +- Sources/_StringProcessing/Compiler.swift | 2 +- .../_StringProcessing/ConsumerInterface.swift | 2 +- .../_StringProcessing/Engine/MEBuilder.swift | 2 +- .../_StringProcessing/Engine/MECapture.swift | 2 +- .../_StringProcessing/Engine/MEProgram.swift | 2 +- .../_StringProcessing/Engine/Registers.swift | 2 +- .../Engine/Structuralize.swift | 2 +- Sources/_StringProcessing/Executor.swift | 2 +- .../_StringProcessing/MatchingOptions.swift | 2 +- .../_StringProcessing/PrintAsPattern.swift | 2 +- .../Regex/ASTConversion.swift | 2 +- .../Regex/AnyRegexOutput.swift | 2 +- Sources/_StringProcessing/Regex/Core.swift | 2 +- Sources/_StringProcessing/Regex/DSLTree.swift | 2 +- Sources/_StringProcessing/Regex/Options.swift | 2 +- .../Utility/ASTBuilder.swift | 2 +- Sources/_Unicode/CMakeLists.txt | 16 ----- Tests/MatchingEngineTests/UtilTests.swift | 2 +- Tests/PrototypesTests/CombinatorsTests.swift | 2 +- Tests/PrototypesTests/PEGTests.swift | 2 +- Tests/PrototypesTests/PTCaRetTests.swift | 2 +- Tests/RegexTests/CaptureTests.swift | 2 +- Tests/RegexTests/CompileTests.swift | 2 +- Tests/RegexTests/DiagnosticTests.swift | 2 +- Tests/RegexTests/LexTests.swift | 2 +- Tests/RegexTests/MatchTests.swift | 2 +- Tests/RegexTests/ParseTests.swift | 2 +- Tests/RegexTests/SyntaxOptionsTests.swift | 2 +- 74 files changed, 140 insertions(+), 129 deletions(-) create mode 100644 Sources/RegexBuilder/CMakeLists.txt delete mode 100644 Sources/_MatchingEngine/CMakeLists.txt create mode 100644 Sources/_RegexParser/CMakeLists.txt rename Sources/{_MatchingEngine => _RegexParser}/Regex/AST/AST.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/AST/ASTAction.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/AST/ASTProtocols.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/AST/Atom.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/AST/Conditional.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/AST/CustomCharClass.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/AST/Group.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/AST/MatchingOptions.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/AST/Quantification.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/CaptureStructure.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/CharacterPropertyClassification.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/DelimiterLexing.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/Diagnostics.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/LexicalAnalysis.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/Mocking.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/Parse.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/Source.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/SourceLocation.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Parse/SyntaxOptions.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Printing/DumpAST.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Printing/PrettyPrinter.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Printing/PrintAsCanonical.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/Printing/RenderRanges.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Regex/TreeProtocols.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Utility/AllScalars.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Utility/Errors.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Utility/Misc.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Utility/MissingUnicode.swift (100%) rename Sources/{_MatchingEngine => _RegexParser}/Utility/TypeConstruction.swift (100%) delete mode 100644 Sources/_Unicode/CMakeLists.txt diff --git a/Package.swift b/Package.swift index 71641ae28..e95d98b67 100644 --- a/Package.swift +++ b/Package.swift @@ -14,8 +14,8 @@ let package = Package( name: "Prototypes", targets: ["Prototypes"]), .library( - name: "_MatchingEngine", - targets: ["_MatchingEngine"]), + name: "_RegexParser", + targets: ["_RegexParser"]), .executable( name: "VariadicsGenerator", targets: ["VariadicsGenerator"]) @@ -27,7 +27,7 @@ let package = Package( // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages which this package depends on. .target( - name: "_MatchingEngine", + name: "_RegexParser", dependencies: [], swiftSettings: [ .unsafeFlags(["-enable-library-evolution"]) @@ -35,19 +35,19 @@ let package = Package( .testTarget( name: "MatchingEngineTests", dependencies: [ - "_MatchingEngine", "_StringProcessing"]), + "_RegexParser", "_StringProcessing"]), .target( name: "_CUnicode", dependencies: []), .target( name: "_StringProcessing", - dependencies: ["_MatchingEngine", "_CUnicode"], + dependencies: ["_RegexParser", "_CUnicode"], swiftSettings: [ .unsafeFlags(["-enable-library-evolution"]), ]), .target( name: "RegexBuilder", - dependencies: ["_StringProcessing", "_MatchingEngine"], + dependencies: ["_StringProcessing", "_RegexParser"], swiftSettings: [ .unsafeFlags(["-enable-library-evolution"]), .unsafeFlags(["-Xfrontend", "-enable-experimental-pairwise-build-block"]) @@ -63,7 +63,7 @@ let package = Package( ]), .target( name: "Prototypes", - dependencies: ["_MatchingEngine", "_StringProcessing"]), + dependencies: ["_RegexParser", "_StringProcessing"]), // MARK: Scripts .executableTarget( @@ -75,14 +75,14 @@ let package = Package( name: "PatternConverter", dependencies: [ .product(name: "ArgumentParser", package: "swift-argument-parser"), - "_MatchingEngine", + "_RegexParser", "_StringProcessing" ]), // MARK: Exercises .target( name: "Exercises", - dependencies: ["_MatchingEngine", "Prototypes", "_StringProcessing", "RegexBuilder"], + dependencies: ["_RegexParser", "Prototypes", "_StringProcessing", "RegexBuilder"], swiftSettings: [ .unsafeFlags(["-Xfrontend", "-enable-experimental-pairwise-build-block"]) ]), diff --git a/README.md b/README.md index 69c545243..e8a6e387e 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,13 @@ See [Declarative String Processing Overview][decl-string] ## Integration with Swift -`_MatchingEngine`, `_CUnicode` and `_StringProcessing` are specially integrated modules that are built as part of apple/swift. +`_RegexParser` and `_StringProcessing` are specially integrated modules that are built as part of apple/swift. -Specifically, `_MatchingEngine` contains the parser for regular expression literals and is built both as part of the compiler and as a core library. `_CUnicode` and `_StringProcessing` are built together as a core library named `_StringProcessing`. +Specifically, `_RegexParser` contains the parser for regular expression literals and is built both as part of the compiler and as a core library. `_CUnicode` and `_StringProcessing` are built together as a core library named `_StringProcessing`. | Module | Swift toolchain component | | ------------------- | ------------------------------------------------------------------------------------ | -| `_MatchingEngine` | `SwiftCompilerSources/Sources/ExperimentalRegex` and `stdlib/public/_MatchingEngine` | +| `_RegexParser` | `SwiftCompilerSources/Sources/_RegexParser` and `stdlib/public/_RegexParser` | | `_CUnicode` | `stdlib/public/_StringProcessing` | | `_StringProcessing` | `stdlib/public/_StringProcessing` | @@ -65,10 +65,9 @@ To integrate the latest changes in apple/swift-experimental-string-processing to ### Development notes -Compiler integration can be tricky. Use special caution when developing `_MatchingEngine`, `_CUnicode` and `_StringProcessing` modules. +Compiler integration can be tricky. Use special caution when developing `_RegexParser` and `_StringProcessing` modules. - Do not change the names of these modules without due approval from compiler and infrastructure teams. - Do not modify the existing ABI (e.g. C API, serialization format) between the regular expression parser and the Swift compiler unless absolutely necessary. - Always minimize the number of lockstep integrations, i.e. when apple/swift-experimental-string-processing and apple/swift have to change together. Whenever possible, introduce new API first, migrate Swift compiler onto it, and then deprecate old API. Use versioning if helpful. - In `_StringProcessing`, do not write fully qualified references to symbols in `_CUnicode`, and always wrap `import _CUnicode` in a `#if canImport(_CUnicode)`. This is because `_CUnicode` is built as part of `_StringProcessing` with CMake. -- In `_MatchingEngine`, do not write fully qualified references to `_MatchingEngine` itself. This is because `_MatchingEngine` is built as `ExperimentalRegex` in `SwiftCompilerSources/` with CMake. diff --git a/Sources/CMakeLists.txt b/Sources/CMakeLists.txt index 19feadbd9..7b4e5e3ed 100644 --- a/Sources/CMakeLists.txt +++ b/Sources/CMakeLists.txt @@ -1,6 +1,6 @@ -add_subdirectory(_Unicode) -add_subdirectory(_MatchingEngine) +add_subdirectory(RegexBuilder) +add_subdirectory(_RegexParser) add_subdirectory(_StringProcessing) add_subdirectory(Prototypes) add_subdirectory(VariadicsGenerator) diff --git a/Sources/PatternConverter/PatternConverter.swift b/Sources/PatternConverter/PatternConverter.swift index 32a4bd8e9..ff47e4be2 100644 --- a/Sources/PatternConverter/PatternConverter.swift +++ b/Sources/PatternConverter/PatternConverter.swift @@ -12,7 +12,7 @@ // swift run PatternConverter import ArgumentParser -import _MatchingEngine +import _RegexParser import _StringProcessing @main @@ -52,7 +52,7 @@ struct PatternConverter: ParsableCommand { let delim = experimentalSyntax ? "|" : "/" print("Converting '\(delim)\(regex)\(delim)'") - let ast = try _MatchingEngine.parse( + let ast = try _RegexParser.parse( regex, experimentalSyntax ? .experimental : .traditional) diff --git a/Sources/Prototypes/CMakeLists.txt b/Sources/Prototypes/CMakeLists.txt index 60768f5a3..f9532c54d 100644 --- a/Sources/Prototypes/CMakeLists.txt +++ b/Sources/Prototypes/CMakeLists.txt @@ -15,4 +15,4 @@ add_library(Prototypes TourOfTypes/CharacterClass.swift TourOfTypes/Literal.swift) target_link_libraries(Prototypes PUBLIC - _MatchingEngine) + _RegexParser) diff --git a/Sources/Prototypes/Combinators/Combinators.swift b/Sources/Prototypes/Combinators/Combinators.swift index eddef70b0..448110c95 100644 --- a/Sources/Prototypes/Combinators/Combinators.swift +++ b/Sources/Prototypes/Combinators/Combinators.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser /* diff --git a/Sources/RegexBuilder/Anchor.swift b/Sources/RegexBuilder/Anchor.swift index ea2dde382..a32d4a8a9 100644 --- a/Sources/RegexBuilder/Anchor.swift +++ b/Sources/RegexBuilder/Anchor.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser @_spi(RegexBuilder) import _StringProcessing public struct Anchor { diff --git a/Sources/RegexBuilder/CMakeLists.txt b/Sources/RegexBuilder/CMakeLists.txt new file mode 100644 index 000000000..c2a0d9738 --- /dev/null +++ b/Sources/RegexBuilder/CMakeLists.txt @@ -0,0 +1,10 @@ + +add_library(RegexBuilder + Anchor.swift + Builder.swift + DSL.swift + Match.swift + Variadics.swift) +target_compile_options(RegexBuilder PRIVATE + -enable-library-evolution + -Xfrontend -enable-experimental-pairwise-build-block) diff --git a/Sources/RegexBuilder/DSL.swift b/Sources/RegexBuilder/DSL.swift index 9107a9719..5efbaa4d9 100644 --- a/Sources/RegexBuilder/DSL.swift +++ b/Sources/RegexBuilder/DSL.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser @_spi(RegexBuilder) import _StringProcessing extension Regex { diff --git a/Sources/RegexBuilder/Variadics.swift b/Sources/RegexBuilder/Variadics.swift index 3cf154627..f59b1f13a 100644 --- a/Sources/RegexBuilder/Variadics.swift +++ b/Sources/RegexBuilder/Variadics.swift @@ -11,7 +11,7 @@ // BEGIN AUTO-GENERATED CONTENT -import _MatchingEngine +import _RegexParser @_spi(RegexBuilder) import _StringProcessing extension RegexComponentBuilder { diff --git a/Sources/VariadicsGenerator/VariadicsGenerator.swift b/Sources/VariadicsGenerator/VariadicsGenerator.swift index e03f81831..ff406e9fb 100644 --- a/Sources/VariadicsGenerator/VariadicsGenerator.swift +++ b/Sources/VariadicsGenerator/VariadicsGenerator.swift @@ -120,7 +120,7 @@ struct VariadicsGenerator: ParsableCommand { // BEGIN AUTO-GENERATED CONTENT - import _MatchingEngine + import _RegexParser @_spi(RegexBuilder) import _StringProcessing diff --git a/Sources/_MatchingEngine/CMakeLists.txt b/Sources/_MatchingEngine/CMakeLists.txt deleted file mode 100644 index f7cb97ce3..000000000 --- a/Sources/_MatchingEngine/CMakeLists.txt +++ /dev/null @@ -1,46 +0,0 @@ - -add_library(_MatchingEngine - Engine/Backtracking.swift - Engine/Builder.swift - Engine/Capture.swift - Engine/Consume.swift - Engine/Engine.swift - Engine/InstPayload.swift - Engine/Instruction.swift - Engine/Processor.swift - Engine/Program.swift - Engine/Registers.swift - Engine/Tracing.swift - Regex/AST/AST.swift - Regex/AST/ASTAction.swift - Regex/AST/ASTProtocols.swift - Regex/AST/Atom.swift - Regex/AST/Conditional.swift - Regex/AST/CustomCharClass.swift - Regex/AST/Group.swift - Regex/AST/MatchingOptions.swift - Regex/AST/Quantification.swift - Regex/Parse/CaptureStructure.swift - Regex/Parse/CharacterPropertyClassification.swift - Regex/Parse/Diagnostics.swift - Regex/Parse/LexicalAnalysis.swift - Regex/Parse/Mocking.swift - Regex/Parse/Parse.swift - Regex/Parse/Source.swift - Regex/Parse/SourceLocation.swift - Regex/Parse/SyntaxOptions.swift - Regex/Printing/DumpAST.swift - Regex/Printing/PrettyPrinter.swift - Regex/Printing/PrintAsCanonical.swift - Regex/Printing/PrintAsPattern.swift - Regex/Printing/RenderRanges.swift - Utility/AllScalars.swift - Utility/Formatting.swift - Utility/Misc.swift - Utility/MissingUnicode.swift - Utility/Protocols.swift - Utility/TypeConstruction.swift - Utility/TypedIndex.swift - Utility/TypedInt.swift) -target_compile_options(_MatchingEngine PRIVATE - -enable-library-evolution) diff --git a/Sources/_RegexParser/CMakeLists.txt b/Sources/_RegexParser/CMakeLists.txt new file mode 100644 index 000000000..48856b453 --- /dev/null +++ b/Sources/_RegexParser/CMakeLists.txt @@ -0,0 +1,28 @@ + +add_library(_RegexParser + AST/AST.swift + AST/ASTAction.swift + AST/ASTProtocols.swift + AST/Atom.swift + AST/Conditional.swift + AST/CustomCharClass.swift + AST/Group.swift + AST/MatchingOptions.swift + AST/Quantification.swift + Parse/CaptureStructure.swift + Parse/CharacterPropertyClassification.swift + Parse/DelimiterLexing.swift + Parse/Diagnostics.swift + Parse/LexicalAnalysis.swift + Parse/Mocking.swift + Parse/Parse.swift + Parse/Source.swift + Parse/SourceLocation.swift + Parse/SyntaxOptions.swift + Printing/DumpAST.swift + Printing/PrettyPrinter.swift + Printing/PrintAsCanonical.swift + Printing/RenderRanges.swift + TreeProtocols.swift) +target_compile_options(_RegexParser PRIVATE + -enable-library-evolution) diff --git a/Sources/_MatchingEngine/Regex/AST/AST.swift b/Sources/_RegexParser/Regex/AST/AST.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/AST/AST.swift rename to Sources/_RegexParser/Regex/AST/AST.swift diff --git a/Sources/_MatchingEngine/Regex/AST/ASTAction.swift b/Sources/_RegexParser/Regex/AST/ASTAction.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/AST/ASTAction.swift rename to Sources/_RegexParser/Regex/AST/ASTAction.swift diff --git a/Sources/_MatchingEngine/Regex/AST/ASTProtocols.swift b/Sources/_RegexParser/Regex/AST/ASTProtocols.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/AST/ASTProtocols.swift rename to Sources/_RegexParser/Regex/AST/ASTProtocols.swift diff --git a/Sources/_MatchingEngine/Regex/AST/Atom.swift b/Sources/_RegexParser/Regex/AST/Atom.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/AST/Atom.swift rename to Sources/_RegexParser/Regex/AST/Atom.swift diff --git a/Sources/_MatchingEngine/Regex/AST/Conditional.swift b/Sources/_RegexParser/Regex/AST/Conditional.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/AST/Conditional.swift rename to Sources/_RegexParser/Regex/AST/Conditional.swift diff --git a/Sources/_MatchingEngine/Regex/AST/CustomCharClass.swift b/Sources/_RegexParser/Regex/AST/CustomCharClass.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/AST/CustomCharClass.swift rename to Sources/_RegexParser/Regex/AST/CustomCharClass.swift diff --git a/Sources/_MatchingEngine/Regex/AST/Group.swift b/Sources/_RegexParser/Regex/AST/Group.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/AST/Group.swift rename to Sources/_RegexParser/Regex/AST/Group.swift diff --git a/Sources/_MatchingEngine/Regex/AST/MatchingOptions.swift b/Sources/_RegexParser/Regex/AST/MatchingOptions.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/AST/MatchingOptions.swift rename to Sources/_RegexParser/Regex/AST/MatchingOptions.swift diff --git a/Sources/_MatchingEngine/Regex/AST/Quantification.swift b/Sources/_RegexParser/Regex/AST/Quantification.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/AST/Quantification.swift rename to Sources/_RegexParser/Regex/AST/Quantification.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/CaptureStructure.swift b/Sources/_RegexParser/Regex/Parse/CaptureStructure.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/CaptureStructure.swift rename to Sources/_RegexParser/Regex/Parse/CaptureStructure.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/CharacterPropertyClassification.swift b/Sources/_RegexParser/Regex/Parse/CharacterPropertyClassification.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/CharacterPropertyClassification.swift rename to Sources/_RegexParser/Regex/Parse/CharacterPropertyClassification.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/DelimiterLexing.swift b/Sources/_RegexParser/Regex/Parse/DelimiterLexing.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/DelimiterLexing.swift rename to Sources/_RegexParser/Regex/Parse/DelimiterLexing.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/Diagnostics.swift b/Sources/_RegexParser/Regex/Parse/Diagnostics.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/Diagnostics.swift rename to Sources/_RegexParser/Regex/Parse/Diagnostics.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/LexicalAnalysis.swift b/Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/LexicalAnalysis.swift rename to Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/Mocking.swift b/Sources/_RegexParser/Regex/Parse/Mocking.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/Mocking.swift rename to Sources/_RegexParser/Regex/Parse/Mocking.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/Parse.swift b/Sources/_RegexParser/Regex/Parse/Parse.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/Parse.swift rename to Sources/_RegexParser/Regex/Parse/Parse.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/Source.swift b/Sources/_RegexParser/Regex/Parse/Source.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/Source.swift rename to Sources/_RegexParser/Regex/Parse/Source.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/SourceLocation.swift b/Sources/_RegexParser/Regex/Parse/SourceLocation.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/SourceLocation.swift rename to Sources/_RegexParser/Regex/Parse/SourceLocation.swift diff --git a/Sources/_MatchingEngine/Regex/Parse/SyntaxOptions.swift b/Sources/_RegexParser/Regex/Parse/SyntaxOptions.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Parse/SyntaxOptions.swift rename to Sources/_RegexParser/Regex/Parse/SyntaxOptions.swift diff --git a/Sources/_MatchingEngine/Regex/Printing/DumpAST.swift b/Sources/_RegexParser/Regex/Printing/DumpAST.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Printing/DumpAST.swift rename to Sources/_RegexParser/Regex/Printing/DumpAST.swift diff --git a/Sources/_MatchingEngine/Regex/Printing/PrettyPrinter.swift b/Sources/_RegexParser/Regex/Printing/PrettyPrinter.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Printing/PrettyPrinter.swift rename to Sources/_RegexParser/Regex/Printing/PrettyPrinter.swift diff --git a/Sources/_MatchingEngine/Regex/Printing/PrintAsCanonical.swift b/Sources/_RegexParser/Regex/Printing/PrintAsCanonical.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Printing/PrintAsCanonical.swift rename to Sources/_RegexParser/Regex/Printing/PrintAsCanonical.swift diff --git a/Sources/_MatchingEngine/Regex/Printing/RenderRanges.swift b/Sources/_RegexParser/Regex/Printing/RenderRanges.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/Printing/RenderRanges.swift rename to Sources/_RegexParser/Regex/Printing/RenderRanges.swift diff --git a/Sources/_MatchingEngine/Regex/TreeProtocols.swift b/Sources/_RegexParser/Regex/TreeProtocols.swift similarity index 100% rename from Sources/_MatchingEngine/Regex/TreeProtocols.swift rename to Sources/_RegexParser/Regex/TreeProtocols.swift diff --git a/Sources/_MatchingEngine/Utility/AllScalars.swift b/Sources/_RegexParser/Utility/AllScalars.swift similarity index 100% rename from Sources/_MatchingEngine/Utility/AllScalars.swift rename to Sources/_RegexParser/Utility/AllScalars.swift diff --git a/Sources/_MatchingEngine/Utility/Errors.swift b/Sources/_RegexParser/Utility/Errors.swift similarity index 100% rename from Sources/_MatchingEngine/Utility/Errors.swift rename to Sources/_RegexParser/Utility/Errors.swift diff --git a/Sources/_MatchingEngine/Utility/Misc.swift b/Sources/_RegexParser/Utility/Misc.swift similarity index 100% rename from Sources/_MatchingEngine/Utility/Misc.swift rename to Sources/_RegexParser/Utility/Misc.swift diff --git a/Sources/_MatchingEngine/Utility/MissingUnicode.swift b/Sources/_RegexParser/Utility/MissingUnicode.swift similarity index 100% rename from Sources/_MatchingEngine/Utility/MissingUnicode.swift rename to Sources/_RegexParser/Utility/MissingUnicode.swift diff --git a/Sources/_MatchingEngine/Utility/TypeConstruction.swift b/Sources/_RegexParser/Utility/TypeConstruction.swift similarity index 100% rename from Sources/_MatchingEngine/Utility/TypeConstruction.swift rename to Sources/_RegexParser/Utility/TypeConstruction.swift diff --git a/Sources/_StringProcessing/ByteCodeGen.swift b/Sources/_StringProcessing/ByteCodeGen.swift index c9599b925..d03a1e382 100644 --- a/Sources/_StringProcessing/ByteCodeGen.swift +++ b/Sources/_StringProcessing/ByteCodeGen.swift @@ -1,4 +1,4 @@ -import _MatchingEngine +import _RegexParser extension Compiler { struct ByteCodeGen { diff --git a/Sources/_StringProcessing/CMakeLists.txt b/Sources/_StringProcessing/CMakeLists.txt index c20dcc240..963b6074c 100644 --- a/Sources/_StringProcessing/CMakeLists.txt +++ b/Sources/_StringProcessing/CMakeLists.txt @@ -12,6 +12,12 @@ add_library(_StringProcessing Algorithms/Consumers/ManyConsumer.swift Algorithms/Consumers/PredicateConsumer.swift Algorithms/Consumers/RegexConsumer.swift + Algorithms/Matching/FirstMatch.swift + Algorithms/Matching/Matches.swift + Algorithms/Matching/MatchingCollectionConsumer.swift + Algorithms/Matching/MatchingCollectionSearcher.swift + Algorithms/Matching/MatchReplace.swift + Algorithms/Matching/MatchResult.swift Algorithms/Searchers/CollectionSearcher.swift Algorithms/Searchers/ConsumerSearcher.swift Algorithms/Searchers/NaivePatternSearcher.swift @@ -19,24 +25,54 @@ add_library(_StringProcessing Algorithms/Searchers/PredicateSearcher.swift Algorithms/Searchers/TwoWaySearcher.swift Algorithms/Searchers/ZSearcher.swift - ASTBuilder.swift + Engine/Backtracking.swift + Engine/Consume.swift + Engine/Engine.swift + Engine/InstPayload.swift + Engine/Instruction.swift + Engine/MEBuilder.swift + Engine/MECapture.swift + Engine/MEProgram.swift + Engine/Processor.swift + Engine/Register.swift + Engine/Structuralize.swift + Engine/Tracing.swift + Regex/AnyRegexOutput.swift + Regex/ASTConversion.swift + Regex/Core.swift + Regex/DSLConsumers.swift + Regex/DSLTree.swift + Regex/Match.swift + Regex/Options.swift + Unicode/CaseConversion.swift + Unicode/CharacterProps.swift + Unicode/Comparison.swift + Unicode/Data.swift + Unicode/Decoding.swift + Unicode/Encodings.swift + Unicode/Formatting.swift + Unicode/Graphemes.swift + Unicode/NecessaryEvils.swift + Unicode/Normalization.swift + Unicode/NumberParsing.swift + Unicode/ScalarProps.swift + Unicode/Transcoding.swift + Unicode/UCD.swift + Unicode/Validation.swift + Utility/ASTBuilder.swift + Utility/Protocols.swift + Utility/Traced.swift + Utility/TypedIndex.swift + Utility/TypedInt.swift + ByteCodeGen.swift Capture.swift CharacterClass.swift Compiler.swift ConsumerInterface.swift Executor.swift - Legacy/HareVM.swift - Legacy/LegacyCompile.swift - Legacy/RECode.swift - Legacy/TortoiseVM.swift - Legacy/VirtualMachine.swift - RegexDSL/Builder.swift - RegexDSL/Concatenation.swift - RegexDSL/Core.swift - RegexDSL/DSL.swift - RegexDSL/DSLCapture.swift - RegexDSL/DynamicCaptures.swift) + MatchingOptions.swift + PrintAsPattern.swift) target_compile_options(_StringProcessing PRIVATE -enable-library-evolution) target_link_libraries(_StringProcessing PUBLIC - _MatchingEngine) + _RegexParser) diff --git a/Sources/_StringProcessing/Capture.swift b/Sources/_StringProcessing/Capture.swift index 55e74684c..f7dff424e 100644 --- a/Sources/_StringProcessing/Capture.swift +++ b/Sources/_StringProcessing/Capture.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser /// A structured capture struct StructuredCapture { diff --git a/Sources/_StringProcessing/CharacterClass.swift b/Sources/_StringProcessing/CharacterClass.swift index 7989c0943..0b95e08b4 100644 --- a/Sources/_StringProcessing/CharacterClass.swift +++ b/Sources/_StringProcessing/CharacterClass.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser // NOTE: This is a model type. We want to be able to get one from // an AST, but this isn't a natural thing to produce in the context diff --git a/Sources/_StringProcessing/Compiler.swift b/Sources/_StringProcessing/Compiler.swift index 1d72a8d27..90192bdaf 100644 --- a/Sources/_StringProcessing/Compiler.swift +++ b/Sources/_StringProcessing/Compiler.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser class Compiler { let tree: DSLTree diff --git a/Sources/_StringProcessing/ConsumerInterface.swift b/Sources/_StringProcessing/ConsumerInterface.swift index 8bddb3a87..0a2d93ff1 100644 --- a/Sources/_StringProcessing/ConsumerInterface.swift +++ b/Sources/_StringProcessing/ConsumerInterface.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser extension DSLTree.Node { /// Attempt to generate a consumer from this AST node diff --git a/Sources/_StringProcessing/Engine/MEBuilder.swift b/Sources/_StringProcessing/Engine/MEBuilder.swift index 78171a001..2b849874b 100644 --- a/Sources/_StringProcessing/Engine/MEBuilder.swift +++ b/Sources/_StringProcessing/Engine/MEBuilder.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine // For errors +import _RegexParser // For errors extension MEProgram where Input.Element: Hashable { struct Builder { diff --git a/Sources/_StringProcessing/Engine/MECapture.swift b/Sources/_StringProcessing/Engine/MECapture.swift index bac632e9e..301212736 100644 --- a/Sources/_StringProcessing/Engine/MECapture.swift +++ b/Sources/_StringProcessing/Engine/MECapture.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser /* diff --git a/Sources/_StringProcessing/Engine/MEProgram.swift b/Sources/_StringProcessing/Engine/MEProgram.swift index 1e58ddf54..a31134cc9 100644 --- a/Sources/_StringProcessing/Engine/MEProgram.swift +++ b/Sources/_StringProcessing/Engine/MEProgram.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser struct MEProgram where Input.Element: Equatable { typealias ConsumeFunction = (Input, Range) -> Input.Index? diff --git a/Sources/_StringProcessing/Engine/Registers.swift b/Sources/_StringProcessing/Engine/Registers.swift index 2cc3cddf5..a2a5be104 100644 --- a/Sources/_StringProcessing/Engine/Registers.swift +++ b/Sources/_StringProcessing/Engine/Registers.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser struct SentinelValue: Hashable, CustomStringConvertible { var description: String { "" } diff --git a/Sources/_StringProcessing/Engine/Structuralize.swift b/Sources/_StringProcessing/Engine/Structuralize.swift index 30e7b9efe..02a03c09c 100644 --- a/Sources/_StringProcessing/Engine/Structuralize.swift +++ b/Sources/_StringProcessing/Engine/Structuralize.swift @@ -1,4 +1,4 @@ -import _MatchingEngine +import _RegexParser extension CaptureStructure { var optionalCount: Int { diff --git a/Sources/_StringProcessing/Executor.swift b/Sources/_StringProcessing/Executor.swift index 15c1fe434..148ddf468 100644 --- a/Sources/_StringProcessing/Executor.swift +++ b/Sources/_StringProcessing/Executor.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser struct Executor { // TODO: consider let, for now lets us toggle tracing diff --git a/Sources/_StringProcessing/MatchingOptions.swift b/Sources/_StringProcessing/MatchingOptions.swift index 073c78b2f..899891184 100644 --- a/Sources/_StringProcessing/MatchingOptions.swift +++ b/Sources/_StringProcessing/MatchingOptions.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser /// A type that represents the current state of regex matching options, with /// stack-based scoping. diff --git a/Sources/_StringProcessing/PrintAsPattern.swift b/Sources/_StringProcessing/PrintAsPattern.swift index f30d8d54b..0cf16ab05 100644 --- a/Sources/_StringProcessing/PrintAsPattern.swift +++ b/Sources/_StringProcessing/PrintAsPattern.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser // TODO: Add an expansion level, both from top to bottom. // After `printAsCanonical` is fleshed out, these two diff --git a/Sources/_StringProcessing/Regex/ASTConversion.swift b/Sources/_StringProcessing/Regex/ASTConversion.swift index 72dd11cdf..5336a1892 100644 --- a/Sources/_StringProcessing/Regex/ASTConversion.swift +++ b/Sources/_StringProcessing/Regex/ASTConversion.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser extension AST { var dslTree: DSLTree { diff --git a/Sources/_StringProcessing/Regex/AnyRegexOutput.swift b/Sources/_StringProcessing/Regex/AnyRegexOutput.swift index b1d9bfafa..2f99470fc 100644 --- a/Sources/_StringProcessing/Regex/AnyRegexOutput.swift +++ b/Sources/_StringProcessing/Regex/AnyRegexOutput.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser extension Regex where Output == AnyRegexOutput { public init(_ pattern: String) throws { diff --git a/Sources/_StringProcessing/Regex/Core.swift b/Sources/_StringProcessing/Regex/Core.swift index c6433ef3e..fb536b28a 100644 --- a/Sources/_StringProcessing/Regex/Core.swift +++ b/Sources/_StringProcessing/Regex/Core.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser /// A type that represents a regular expression. diff --git a/Sources/_StringProcessing/Regex/DSLTree.swift b/Sources/_StringProcessing/Regex/DSLTree.swift index e579828d2..bd3b37a3d 100644 --- a/Sources/_StringProcessing/Regex/DSLTree.swift +++ b/Sources/_StringProcessing/Regex/DSLTree.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser @_spi(RegexBuilder) public struct DSLTree { diff --git a/Sources/_StringProcessing/Regex/Options.swift b/Sources/_StringProcessing/Regex/Options.swift index 7876ae35c..04be79c6e 100644 --- a/Sources/_StringProcessing/Regex/Options.swift +++ b/Sources/_StringProcessing/Regex/Options.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -import _MatchingEngine +import _RegexParser extension RegexComponent { public func caseSensitive(_ isCaseSensitive: Bool) -> Regex { diff --git a/Sources/_StringProcessing/Utility/ASTBuilder.swift b/Sources/_StringProcessing/Utility/ASTBuilder.swift index bbc199d27..8a9af8111 100644 --- a/Sources/_StringProcessing/Utility/ASTBuilder.swift +++ b/Sources/_StringProcessing/Utility/ASTBuilder.swift @@ -25,7 +25,7 @@ AST. */ -import _MatchingEngine +import _RegexParser func alt(_ asts: [AST.Node]) -> AST.Node { return .alternation( diff --git a/Sources/_Unicode/CMakeLists.txt b/Sources/_Unicode/CMakeLists.txt deleted file mode 100644 index 7fdb44628..000000000 --- a/Sources/_Unicode/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ - -add_library(_Unicode - CaseConversion.swift - CharacterProps.swift - Comparison.swift - Decoding.swift - Encodings.swift - Formatting.swift - Graphemes.swift - NecessaryEvils.swift - Normaliation.swift - NumberParsing.swift - ScalarProps.swift - Transcoding.swift - UCD.swift - Validation.swift) diff --git a/Tests/MatchingEngineTests/UtilTests.swift b/Tests/MatchingEngineTests/UtilTests.swift index cb9e22e97..d0b2698a9 100644 --- a/Tests/MatchingEngineTests/UtilTests.swift +++ b/Tests/MatchingEngineTests/UtilTests.swift @@ -10,7 +10,7 @@ //===----------------------------------------------------------------------===// import XCTest -@testable import _MatchingEngine +@testable import _RegexParser class UtilTests: XCTestCase { func testTupleTypeConstruction() { diff --git a/Tests/PrototypesTests/CombinatorsTests.swift b/Tests/PrototypesTests/CombinatorsTests.swift index e34d0f832..bf82b65a2 100644 --- a/Tests/PrototypesTests/CombinatorsTests.swift +++ b/Tests/PrototypesTests/CombinatorsTests.swift @@ -11,7 +11,7 @@ import XCTest @testable import Combinators -import _MatchingEngine +import _RegexParser class CombinatorTests: XCTestCase { func testAdHoc() { diff --git a/Tests/PrototypesTests/PEGTests.swift b/Tests/PrototypesTests/PEGTests.swift index 0fac674f3..5d7516a50 100644 --- a/Tests/PrototypesTests/PEGTests.swift +++ b/Tests/PrototypesTests/PEGTests.swift @@ -11,7 +11,7 @@ import XCTest @testable import Prototypes -import _MatchingEngine +import _RegexParser // Make examples more sane. Need builder typealias Pattern = PEG.Pattern diff --git a/Tests/PrototypesTests/PTCaRetTests.swift b/Tests/PrototypesTests/PTCaRetTests.swift index 0e05e1231..752f13b39 100644 --- a/Tests/PrototypesTests/PTCaRetTests.swift +++ b/Tests/PrototypesTests/PTCaRetTests.swift @@ -11,7 +11,7 @@ import XCTest @testable import Prototypes -import _MatchingEngine +import _RegexParser enum Event: UInt64, Hashable { case authenticate = 0 diff --git a/Tests/RegexTests/CaptureTests.swift b/Tests/RegexTests/CaptureTests.swift index 258aea86d..7d4266071 100644 --- a/Tests/RegexTests/CaptureTests.swift +++ b/Tests/RegexTests/CaptureTests.swift @@ -11,7 +11,7 @@ import XCTest @testable @_spi(RegexBuilder) import _StringProcessing -import _MatchingEngine +import _RegexParser extension StructuredCapture { func formatStringCapture(input: String) -> String { diff --git a/Tests/RegexTests/CompileTests.swift b/Tests/RegexTests/CompileTests.swift index 63e48fa61..bc545d02e 100644 --- a/Tests/RegexTests/CompileTests.swift +++ b/Tests/RegexTests/CompileTests.swift @@ -10,7 +10,7 @@ //===----------------------------------------------------------------------===// -@testable import _MatchingEngine +@testable import _RegexParser @testable import _StringProcessing import XCTest diff --git a/Tests/RegexTests/DiagnosticTests.swift b/Tests/RegexTests/DiagnosticTests.swift index 3b86363cc..428020b80 100644 --- a/Tests/RegexTests/DiagnosticTests.swift +++ b/Tests/RegexTests/DiagnosticTests.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -@testable import _MatchingEngine +@testable import _RegexParser @testable import _StringProcessing import XCTest diff --git a/Tests/RegexTests/LexTests.swift b/Tests/RegexTests/LexTests.swift index 25ced92c2..c50191d05 100644 --- a/Tests/RegexTests/LexTests.swift +++ b/Tests/RegexTests/LexTests.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -@testable import _MatchingEngine +@testable import _RegexParser import XCTest @testable import _StringProcessing diff --git a/Tests/RegexTests/MatchTests.swift b/Tests/RegexTests/MatchTests.swift index 1d5b436ff..52db17aa7 100644 --- a/Tests/RegexTests/MatchTests.swift +++ b/Tests/RegexTests/MatchTests.swift @@ -10,7 +10,7 @@ //===----------------------------------------------------------------------===// import XCTest -@testable import _MatchingEngine +@testable import _RegexParser @testable import _StringProcessing struct MatchError: Error { diff --git a/Tests/RegexTests/ParseTests.swift b/Tests/RegexTests/ParseTests.swift index ced69bb9d..6e511767a 100644 --- a/Tests/RegexTests/ParseTests.swift +++ b/Tests/RegexTests/ParseTests.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -@testable import _MatchingEngine +@testable import _RegexParser import XCTest @testable import _StringProcessing diff --git a/Tests/RegexTests/SyntaxOptionsTests.swift b/Tests/RegexTests/SyntaxOptionsTests.swift index 881eb0cbc..13618488c 100644 --- a/Tests/RegexTests/SyntaxOptionsTests.swift +++ b/Tests/RegexTests/SyntaxOptionsTests.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -@testable import _MatchingEngine +@testable import _RegexParser @testable import _StringProcessing import XCTest