From 88c25e42cdde71a13dd8eeb9f0d9bdfd302ea6f2 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Thu, 7 Apr 2022 21:01:18 -0500 Subject: [PATCH 1/3] Eliminate extra public API These were mostly leftover bits from testing :wave: --- .../Participants/RegexParticipant.swift | 8 +++---- .../PatternConverter/PatternConverter.swift | 2 +- .../_StringProcessing/PrintAsPattern.swift | 1 + Sources/_StringProcessing/Regex/Core.swift | 21 ------------------- .../Utility/ASTBuilder.swift | 8 +++---- Tests/RegexBuilderTests/RegexDSLTests.swift | 6 +++--- 6 files changed, 13 insertions(+), 33 deletions(-) diff --git a/Sources/Exercises/Participants/RegexParticipant.swift b/Sources/Exercises/Participants/RegexParticipant.swift index a40de3953..71018d8a7 100644 --- a/Sources/Exercises/Participants/RegexParticipant.swift +++ b/Sources/Exercises/Participants/RegexParticipant.swift @@ -69,10 +69,10 @@ private func graphemeBreakPropertyData( private func graphemeBreakPropertyDataLiteral( forLine line: String ) -> GraphemeBreakEntry? { - return graphemeBreakPropertyData( - forLine: line, - using: r(#"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#, - matching: (Substring, Substring, Substring?, Substring).self)) + let regex = try! Regex( + compiling: #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#, + as: (Substring, Substring, Substring?, Substring).self) + return graphemeBreakPropertyData(forLine: line, using: regex) } // MARK: - Builder DSL diff --git a/Sources/PatternConverter/PatternConverter.swift b/Sources/PatternConverter/PatternConverter.swift index ff47e4be2..f66204884 100644 --- a/Sources/PatternConverter/PatternConverter.swift +++ b/Sources/PatternConverter/PatternConverter.swift @@ -13,7 +13,7 @@ import ArgumentParser import _RegexParser -import _StringProcessing +@_spi(PatternConverter) import _StringProcessing @main struct PatternConverter: ParsableCommand { diff --git a/Sources/_StringProcessing/PrintAsPattern.swift b/Sources/_StringProcessing/PrintAsPattern.swift index 0cf16ab05..1998aa75b 100644 --- a/Sources/_StringProcessing/PrintAsPattern.swift +++ b/Sources/_StringProcessing/PrintAsPattern.swift @@ -19,6 +19,7 @@ import _RegexParser extension AST { /// Render as a Pattern DSL + @_spi(PatternConverter) public func renderAsBuilderDSL( maxTopDownLevels: Int? = nil, minBottomUpLevels: Int? = nil diff --git a/Sources/_StringProcessing/Regex/Core.swift b/Sources/_StringProcessing/Regex/Core.swift index 265a7868c..96d0f3a68 100644 --- a/Sources/_StringProcessing/Regex/Core.swift +++ b/Sources/_StringProcessing/Regex/Core.swift @@ -129,24 +129,3 @@ extension UnicodeScalar: RegexComponent { .init(node: .atom(.scalar(self))) } } - -// MARK: - Testing - -public struct MockRegexLiteral: RegexComponent { - public typealias MatchValue = Substring - public let regex: Regex - - public init( - _ string: String, - _ syntax: SyntaxOptions = .traditional, - matching: Output.Type = Output.self - ) throws { - regex = Regex(ast: try parse(string, syntax)) - } -} - -public func r( - _ s: String, matching matchType: Output.Type = Output.self -) -> MockRegexLiteral { - try! MockRegexLiteral(s, matching: matchType) -} diff --git a/Sources/_StringProcessing/Utility/ASTBuilder.swift b/Sources/_StringProcessing/Utility/ASTBuilder.swift index 8a9af8111..7a8edcd24 100644 --- a/Sources/_StringProcessing/Utility/ASTBuilder.swift +++ b/Sources/_StringProcessing/Utility/ASTBuilder.swift @@ -106,16 +106,16 @@ func negativeLookahead(_ child: AST.Node) -> AST.Node { func negativeLookbehind(_ child: AST.Node) -> AST.Node { group(.negativeLookbehind, child) } -public func nonAtomicLookahead(_ child: AST.Node) -> AST.Node { +func nonAtomicLookahead(_ child: AST.Node) -> AST.Node { group(.nonAtomicLookahead, child) } -public func nonAtomicLookbehind(_ child: AST.Node) -> AST.Node { +func nonAtomicLookbehind(_ child: AST.Node) -> AST.Node { group(.nonAtomicLookbehind, child) } -public func scriptRun(_ child: AST.Node) -> AST.Node { +func scriptRun(_ child: AST.Node) -> AST.Node { group(.scriptRun, child) } -public func atomicScriptRun(_ child: AST.Node) -> AST.Node { +func atomicScriptRun(_ child: AST.Node) -> AST.Node { group(.atomicScriptRun, child) } func changeMatchingOptions( diff --git a/Tests/RegexBuilderTests/RegexDSLTests.swift b/Tests/RegexBuilderTests/RegexDSLTests.swift index aaa3f6886..1f5305c59 100644 --- a/Tests/RegexBuilderTests/RegexDSLTests.swift +++ b/Tests/RegexBuilderTests/RegexDSLTests.swift @@ -611,9 +611,9 @@ class RegexDSLTests: XCTestCase { } do { - let regexLiteral = try MockRegexLiteral( - #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#, - matching: (Substring, Substring, Substring?, Substring).self) + let regexLiteral = try Regex( + compiling: #"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#, + as: (Substring, Substring, Substring?, Substring).self) let maybeMatchResult = line.matchWhole(regexLiteral) let matchResult = try XCTUnwrap(maybeMatchResult) let (wholeMatch, lower, upper, propertyString) = matchResult.output From e5aba75b0b8a5e474ca3fd2640b1d726d890cfb4 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Thu, 7 Apr 2022 21:48:02 -0500 Subject: [PATCH 2/3] Make some types internal --- .../Consumers/PredicateConsumer.swift | 8 ++++---- .../Algorithms/Consumers/RegexConsumer.swift | 18 +++++++++--------- .../Algorithms/Searchers/TwoWaySearcher.swift | 8 ++++---- .../Algorithms/Searchers/ZSearcher.swift | 6 +++--- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Sources/_StringProcessing/Algorithms/Consumers/PredicateConsumer.swift b/Sources/_StringProcessing/Algorithms/Consumers/PredicateConsumer.swift index c9b92b9ec..caf523d1c 100644 --- a/Sources/_StringProcessing/Algorithms/Consumers/PredicateConsumer.swift +++ b/Sources/_StringProcessing/Algorithms/Consumers/PredicateConsumer.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -public struct PredicateConsumer { +struct PredicateConsumer { let predicate: (Consumed.Element) -> Bool } @@ -29,7 +29,7 @@ extension PredicateConsumer: CollectionConsumer { extension PredicateConsumer: BidirectionalCollectionConsumer where Consumed: BidirectionalCollection { - public func consumingBack( + func consumingBack( _ consumed: Consumed, in range: Range ) -> Consumed.Index? { @@ -59,9 +59,9 @@ extension PredicateConsumer: BackwardCollectionSearcher, BackwardStatelessCollectionSearcher where Searched: BidirectionalCollection { - public typealias BackwardSearched = Consumed + typealias BackwardSearched = Consumed - public func searchBack( + func searchBack( _ searched: BackwardSearched, in range: Range ) -> Range? { diff --git a/Sources/_StringProcessing/Algorithms/Consumers/RegexConsumer.swift b/Sources/_StringProcessing/Algorithms/Consumers/RegexConsumer.swift index 3ab1e579d..dcc5d9f2b 100644 --- a/Sources/_StringProcessing/Algorithms/Consumers/RegexConsumer.swift +++ b/Sources/_StringProcessing/Algorithms/Consumers/RegexConsumer.swift @@ -9,13 +9,13 @@ // //===----------------------------------------------------------------------===// -public struct RegexConsumer< +struct RegexConsumer< R: RegexComponent, Consumed: BidirectionalCollection > where Consumed.SubSequence == Substring { // TODO: Should `Regex` itself implement these protocols? let regex: R - public init(_ regex: R) { + init(_ regex: R) { self.regex = regex } } @@ -36,9 +36,9 @@ extension RegexConsumer { // well, taking advantage of the fact that the captures can be ignored extension RegexConsumer: MatchingCollectionConsumer { - public typealias Match = R.Output + typealias Match = R.Output - public func matchingConsuming( + func matchingConsuming( _ consumed: Consumed, in range: Range ) -> (upperBound: String.Index, match: Match)? { _matchingConsuming(consumed[...], in: range) @@ -47,7 +47,7 @@ extension RegexConsumer: MatchingCollectionConsumer { // TODO: We'll want to bake backwards into the engine extension RegexConsumer: BidirectionalMatchingCollectionConsumer { - public func matchingConsumingBack( + func matchingConsumingBack( _ consumed: Consumed, in range: Range ) -> (lowerBound: String.Index, match: Match)? { var i = range.lowerBound @@ -67,12 +67,12 @@ extension RegexConsumer: BidirectionalMatchingCollectionConsumer { } extension RegexConsumer: MatchingStatelessCollectionSearcher { - public typealias Searched = Consumed + typealias Searched = Consumed // TODO: We'll want to bake search into the engine so it can // take advantage of the structure of the regex itself and // its own internal state - public func matchingSearch( + func matchingSearch( _ searched: Searched, in range: Range ) -> (range: Range, match: Match)? { ConsumerSearcher(consumer: self).matchingSearch(searched, in: range) @@ -81,9 +81,9 @@ extension RegexConsumer: MatchingStatelessCollectionSearcher { // TODO: Bake in search-back to engine too extension RegexConsumer: BackwardMatchingStatelessCollectionSearcher { - public typealias BackwardSearched = Consumed + typealias BackwardSearched = Consumed - public func matchingSearchBack( + func matchingSearchBack( _ searched: BackwardSearched, in range: Range ) -> (range: Range, match: Match)? { ConsumerSearcher(consumer: self).matchingSearchBack(searched, in: range) diff --git a/Sources/_StringProcessing/Algorithms/Searchers/TwoWaySearcher.swift b/Sources/_StringProcessing/Algorithms/Searchers/TwoWaySearcher.swift index 11184f856..5530b4421 100644 --- a/Sources/_StringProcessing/Algorithms/Searchers/TwoWaySearcher.swift +++ b/Sources/_StringProcessing/Algorithms/Searchers/TwoWaySearcher.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -public struct TwoWaySearcher +struct TwoWaySearcher where Searched.Element: Comparable { // TODO: Be generic over the pattern? @@ -36,14 +36,14 @@ public struct TwoWaySearcher } extension TwoWaySearcher: CollectionSearcher { - public struct State { + struct State { let end: Searched.Index var index: Searched.Index var criticalIndex: Searched.Index var memory: (offset: Int, index: Searched.Index)? } - public func state( + func state( for searched: Searched, in range: Range ) -> State { @@ -57,7 +57,7 @@ extension TwoWaySearcher: CollectionSearcher { memory: nil) } - public func search( + func search( _ searched: Searched, _ state: inout State ) -> Range? { diff --git a/Sources/_StringProcessing/Algorithms/Searchers/ZSearcher.swift b/Sources/_StringProcessing/Algorithms/Searchers/ZSearcher.swift index a541dcb7d..e06796e72 100644 --- a/Sources/_StringProcessing/Algorithms/Searchers/ZSearcher.swift +++ b/Sources/_StringProcessing/Algorithms/Searchers/ZSearcher.swift @@ -9,12 +9,12 @@ // //===----------------------------------------------------------------------===// -public struct ZSearcher { +struct ZSearcher { let pattern: [Searched.Element] let z: [Int] let areEquivalent: (Searched.Element, Searched.Element) -> Bool - public init( + init( pattern: [Searched.Element], by areEquivalent: @escaping (Searched.Element, Searched.Element ) -> Bool) { @@ -25,7 +25,7 @@ public struct ZSearcher { } extension ZSearcher: StatelessCollectionSearcher { - public func search( + func search( _ searched: Searched, in range: Range ) -> Range? { From 7e71f864feae0e28647155b89f07d980946878be Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Fri, 8 Apr 2022 13:56:01 -0500 Subject: [PATCH 3/3] Make MatchingOptions type internal as well --- Package.swift | 1 + Sources/_StringProcessing/MatchingOptions.swift | 2 +- .../_StringProcessing/_CharacterClassModel.swift | 16 ++++++++-------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Package.swift b/Package.swift index e95d98b67..47a73ca72 100644 --- a/Package.swift +++ b/Package.swift @@ -22,6 +22,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0"), + .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. diff --git a/Sources/_StringProcessing/MatchingOptions.swift b/Sources/_StringProcessing/MatchingOptions.swift index cafb07da0..0f244f1b8 100644 --- a/Sources/_StringProcessing/MatchingOptions.swift +++ b/Sources/_StringProcessing/MatchingOptions.swift @@ -13,7 +13,7 @@ import _RegexParser /// A type that represents the current state of regex matching options, with /// stack-based scoping. -public struct MatchingOptions { +struct MatchingOptions { fileprivate var stack: [Representation] fileprivate func _invariantCheck() { diff --git a/Sources/_StringProcessing/_CharacterClassModel.swift b/Sources/_StringProcessing/_CharacterClassModel.swift index 94a42b549..e184689f1 100644 --- a/Sources/_StringProcessing/_CharacterClassModel.swift +++ b/Sources/_StringProcessing/_CharacterClassModel.swift @@ -61,7 +61,7 @@ public struct _CharacterClassModel: Hashable { var op: SetOperator var rhs: CharacterSetComponent - public func matches(_ c: Character, with options: MatchingOptions) -> Bool { + func matches(_ c: Character, with options: MatchingOptions) -> Bool { switch op { case .intersection: return lhs.matches(c, with: options) && rhs.matches(c, with: options) @@ -90,7 +90,7 @@ public struct _CharacterClassModel: Hashable { .setOperation(.init(lhs: lhs, op: op, rhs: rhs)) } - public func matches(_ character: Character, with options: MatchingOptions) -> Bool { + func matches(_ character: Character, with options: MatchingOptions) -> Bool { switch self { case .character(let c): if options.isCaseInsensitive { @@ -116,20 +116,20 @@ public struct _CharacterClassModel: Hashable { } } - public enum MatchLevel { + enum MatchLevel { /// Match at the extended grapheme cluster level. case graphemeCluster /// Match at the Unicode scalar level. case unicodeScalar } - public var scalarSemantic: Self { + var scalarSemantic: Self { var result = self result.matchLevel = .unicodeScalar return result } - public var graphemeClusterSemantic: Self { + var graphemeClusterSemantic: Self { var result = self result.matchLevel = .graphemeCluster return result @@ -137,7 +137,7 @@ public struct _CharacterClassModel: Hashable { /// Returns an inverted character class if true is passed, otherwise the /// same character class is returned. - public func withInversion(_ invertion: Bool) -> Self { + func withInversion(_ invertion: Bool) -> Self { var copy = self if invertion { copy.isInverted.toggle() @@ -152,7 +152,7 @@ public struct _CharacterClassModel: Hashable { /// Returns the end of the match of this character class in `str`, if /// it matches. - public func matches(in str: String, at i: String.Index, with options: MatchingOptions) -> String.Index? { + func matches(in str: String, at i: String.Index, with options: MatchingOptions) -> String.Index? { switch matchLevel { case .graphemeCluster: let c = str[i] @@ -363,7 +363,7 @@ extension DSLTree.Node { } extension _CharacterClassModel { - public func withMatchLevel( + func withMatchLevel( _ level: _CharacterClassModel.MatchLevel ) -> _CharacterClassModel { var cc = self