Skip to content

Rename RegexProtocol to RegexComponent and RegexBuilder to RegexComponentBuilder. #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Exercises/Participants/RegexParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private func extractFromCaptures(
}

@inline(__always) // get rid of generic please
private func graphemeBreakPropertyData<RP: RegexProtocol>(
private func graphemeBreakPropertyData<RP: RegexComponent>(
forLine line: String,
using regex: RP
) -> GraphemeBreakEntry? where RP.Match == (Substring, Substring, Substring?, Substring) {
Expand Down
22 changes: 11 additions & 11 deletions Sources/Prototypes/TourOfTypes/Literal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ enum SemanticsLevel {
}

/// Conformers can be ran as a regex / pattern
protocol RegexProtocol {
protocol RegexComponent {
var level: SemanticsLevel? { get }
}

/// Provide the option to encode semantic level statically
protocol RegexLiteralProtocol: ExpressibleByRegexLiteral {
associatedtype ScalarSemanticRegex: RegexProtocol
associatedtype GraphemeSemanticRegex: RegexProtocol
associatedtype POSIXSemanticRegex: RegexProtocol
associatedtype UnspecifiedSemanticRegex: RegexProtocol = RegexLiteral
associatedtype ScalarSemanticRegex: RegexComponent
associatedtype GraphemeSemanticRegex: RegexComponent
associatedtype POSIXSemanticRegex: RegexComponent
associatedtype UnspecifiedSemanticRegex: RegexComponent = RegexLiteral

var scalarSemantic: ScalarSemanticRegex { get }
var graphemeSemantic: GraphemeSemanticRegex { get }
Expand All @@ -84,16 +84,16 @@ struct StaticSemanticRegexLiteral: RegexLiteralProtocol {
*/

/// A regex that has statically bound its semantic level
struct ScalarSemanticRegex: RegexProtocol {
struct ScalarSemanticRegex: RegexComponent {
var level: SemanticsLevel? { .scalar }
}
struct GraphemeSemanticRegex: RegexProtocol {
struct GraphemeSemanticRegex: RegexComponent {
var level: SemanticsLevel? { .graphemeCluster }
}
struct POSIXSemanticRegex: RegexProtocol {
struct POSIXSemanticRegex: RegexComponent {
var level: SemanticsLevel? { .posix }
}
struct UnspecifiedSemanticRegex: RegexProtocol {
struct UnspecifiedSemanticRegex: RegexComponent {
var level: SemanticsLevel? { nil }
}

Expand Down Expand Up @@ -132,9 +132,9 @@ struct RegexLiteral: ExpressibleByRegexLiteral {
}
}

extension RegexLiteral: RegexProtocol, RegexLiteralProtocol {
extension RegexLiteral: RegexComponent, RegexLiteralProtocol {
/// A regex that has finally bound its semantic level (dynamically)
struct BoundSemantic: RegexProtocol {
struct BoundSemantic: RegexComponent {
var _level: SemanticsLevel // Bound semantic level
var level: SemanticsLevel? { _level }
}
Expand Down
47 changes: 24 additions & 23 deletions Sources/VariadicsGenerator/VariadicsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ struct StandardErrorStream: TextOutputStream {
var standardError = StandardErrorStream()

typealias Counter = Int64
let regexProtocolName = "RegexProtocol"
let regexComponentProtocolName = "RegexComponent"
let matchAssociatedTypeName = "Match"
let patternBuilderTypeName = "RegexBuilder"
let patternProtocolRequirementName = "regex"
let regexTypeName = "Regex"
let baseMatchTypeName = "Substring"
let concatBuilderName = "RegexComponentBuilder"
let altBuilderName = "AlternationBuilder"

@main
struct VariadicsGenerator: ParsableCommand {
Expand Down Expand Up @@ -194,7 +195,7 @@ struct VariadicsGenerator: ParsableCommand {
result += (0..<leftArity+rightArity).map {
", C\($0)"
}.joined()
result += ", R0: \(regexProtocolName), R1: \(regexProtocolName)"
result += ", R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)"
return result
}()

Expand Down Expand Up @@ -231,7 +232,7 @@ struct VariadicsGenerator: ParsableCommand {
}()

// Emit concatenation builder.
output("extension \(patternBuilderTypeName) {\n")
output("extension \(concatBuilderName) {\n")
output("""
public static func buildBlock<\(genericParams)>(
combining next: R1, into combined: R0
Expand All @@ -246,14 +247,14 @@ struct VariadicsGenerator: ParsableCommand {
func emitConcatenationWithEmpty(leftArity: Int) {
// T + () = T
output("""
extension RegexBuilder {
extension \(concatBuilderName) {
public static func buildBlock<W0
""")
outputForEach(0..<leftArity) {
", C\($0)"
}
output("""
, R0: \(regexProtocolName), R1: \(regexProtocolName)>(
, R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)>(
combining next: R1, into combined: R0
) -> \(regexTypeName)<
""")
Expand Down Expand Up @@ -329,7 +330,7 @@ struct VariadicsGenerator: ParsableCommand {
result += (0..<arity).map { ", C\($0)" }.joined()
result += ", "
}
result += "Component: \(regexProtocolName)"
result += "Component: \(regexComponentProtocolName)"
return result
}()

Expand Down Expand Up @@ -366,7 +367,7 @@ struct VariadicsGenerator: ParsableCommand {
\(params.disfavored)\
public func \(kind.rawValue)<\(params.genericParams)>(
_ behavior: QuantificationBehavior = .eagerly,
@RegexBuilder _ component: () -> Component
@\(concatBuilderName) _ component: () -> Component
) -> \(regexTypeName)<\(params.matchType)> \(params.whereClause) {
.init(node: .quantification(.\(kind.astQuantifierAmount), behavior.astKind, component().regex.root))
}
Expand All @@ -380,7 +381,7 @@ struct VariadicsGenerator: ParsableCommand {

\(kind == .zeroOrOne ?
"""
extension RegexBuilder {
extension \(concatBuilderName) {
public static func buildLimitedAvailability<\(params.genericParams)>(
_ component: Component
) -> \(regexTypeName)<\(params.matchType)> \(params.whereClause) {
Expand Down Expand Up @@ -413,7 +414,7 @@ struct VariadicsGenerator: ParsableCommand {
\(params.disfavored)\
public func repeating<\(params.genericParams)>(
count: Int,
@RegexBuilder _ component: () -> Component
@\(concatBuilderName) _ component: () -> Component
) -> \(regexTypeName)<\(params.matchType)> \(params.whereClause) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
Expand All @@ -433,7 +434,7 @@ struct VariadicsGenerator: ParsableCommand {
public func repeating<\(params.genericParams), R: RangeExpression>(
_ expression: R,
_ behavior: QuantificationBehavior = .eagerly,
@RegexBuilder _ component: () -> Component
@\(concatBuilderName) _ component: () -> Component
) -> \(regexTypeName)<\(params.matchType)> \(params.repeatingWhereClause) {
.init(node: .repeating(expression.relative(to: 0..<Int.max), behavior, component().regex.root))
}
Expand All @@ -456,7 +457,7 @@ struct VariadicsGenerator: ParsableCommand {
}()
let genericParams = leftGenParams + ", " + rightGenParams
let whereClause: String = {
var result = "where R0: \(regexProtocolName), R1: \(regexProtocolName)"
var result = "where R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)"
if leftArity > 0 {
result += ", R0.\(matchAssociatedTypeName) == (W0, \((0..<leftArity).map { "C\($0)" }.joined(separator: ", ")))"
}
Expand All @@ -480,7 +481,7 @@ struct VariadicsGenerator: ParsableCommand {
return "(\(baseMatchTypeName), \(resultCaptures))"
}()
output("""
extension AlternationBuilder {
extension \(altBuilderName) {
public static func buildBlock<\(genericParams)>(
combining next: R1, into combined: R0
) -> \(regexTypeName)<\(matchType)> \(whereClause) {
Expand All @@ -505,12 +506,12 @@ struct VariadicsGenerator: ParsableCommand {
return "R, W, " + captures
}()
let whereClause: String = """
where R: \(regexProtocolName), \
where R: \(regexComponentProtocolName), \
R.\(matchAssociatedTypeName) == (W, \(captures))
"""
let resultCaptures = (0..<arity).map { "C\($0)?" }.joined(separator: ", ")
output("""
extension AlternationBuilder {
extension \(altBuilderName) {
public static func buildBlock<\(genericParams)>(_ regex: R) -> \(regexTypeName)<(W, \(resultCaptures))> \(whereClause) {
.init(node: .alternation([regex.regex.root]))
}
Expand All @@ -521,8 +522,8 @@ struct VariadicsGenerator: ParsableCommand {

func emitCapture(arity: Int) {
let genericParams = arity == 0
? "R: \(regexProtocolName), W"
: "R: \(regexProtocolName), W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ")
? "R: \(regexComponentProtocolName), W"
: "R: \(regexComponentProtocolName), W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ")
let matchType = arity == 0
? "W"
: "(W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ") + ")"
Expand Down Expand Up @@ -630,20 +631,20 @@ struct VariadicsGenerator: ParsableCommand {
// MARK: - Builder capture arity \(arity)

public func capture<\(genericParams)>(
@RegexBuilder _ component: () -> R
@\(concatBuilderName) _ component: () -> R
) -> \(regexTypeName)<\(rawNewMatchType)> \(whereClause) {
.init(node: .group(.capture, component().regex.root))
}

public func capture<\(genericParams)>(
as reference: Reference<W>,
@RegexBuilder _ component: () -> R
@\(concatBuilderName) _ component: () -> R
) -> \(regexTypeName)<\(rawNewMatchType)> \(whereClause) {
.init(node: .group(.capture, component().regex.root, reference.id))
}

public func capture<\(genericParams), NewCapture>(
@RegexBuilder _ component: () -> R,
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) -> NewCapture
) -> \(regexTypeName)<\(transformedNewMatchType)> \(whereClause) {
.init(node: .groupTransform(
Expand All @@ -656,7 +657,7 @@ struct VariadicsGenerator: ParsableCommand {

public func tryCapture<\(genericParams), NewCapture>(
as reference: Reference<NewCapture>,
@RegexBuilder _ component: () -> R,
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) throws -> NewCapture
) -> \(regexTypeName)<\(transformedNewMatchType)> \(whereClause) {
.init(node: .groupTransform(
Expand All @@ -669,7 +670,7 @@ struct VariadicsGenerator: ParsableCommand {
}

public func tryCapture<\(genericParams), NewCapture>(
@RegexBuilder _ component: () -> R,
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) -> NewCapture?
) -> \(regexTypeName)<\(transformedNewMatchType)> \(whereClause) {
.init(node: .groupTransform(
Expand All @@ -682,7 +683,7 @@ struct VariadicsGenerator: ParsableCommand {

public func tryCapture<\(genericParams), NewCapture>(
as reference: Reference<NewCapture>,
@RegexBuilder _ component: () -> R,
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) -> NewCapture?
) -> \(regexTypeName)<\(transformedNewMatchType)> \(whereClause) {
.init(node: .groupTransform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ extension BidirectionalCollection where Element: Comparable {
// MARK: Regex algorithms

extension BidirectionalCollection where SubSequence == Substring {
public func contains<R: RegexProtocol>(_ regex: R) -> Bool {
public func contains<R: RegexComponent>(_ regex: R) -> Bool {
contains(RegexConsumer(regex))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ extension BidirectionalCollection where Element: Comparable {
// MARK: Regex algorithms

extension BidirectionalCollection where SubSequence == Substring {
public func firstRange<R: RegexProtocol>(of regex: R) -> Range<Index>? {
public func firstRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
firstRange(of: RegexConsumer(regex))
}

public func lastRange<R: RegexProtocol>(of regex: R) -> Range<Index>? {
public func lastRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
lastRange(of: RegexConsumer(regex))
}
}
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ extension BidirectionalCollection where Element: Comparable {
// MARK: Regex algorithms

extension BidirectionalCollection where SubSequence == Substring {
public func ranges<R: RegexProtocol>(
public func ranges<R: RegexComponent>(
of regex: R
) -> RangesCollection<RegexConsumer<R, Self>> {
ranges(of: RegexConsumer(regex))
}

public func rangesFromBack<R: RegexProtocol>(
public func rangesFromBack<R: RegexComponent>(
of regex: R
) -> ReversedRangesCollection<RegexConsumer<R, Self>> {
rangesFromBack(of: RegexConsumer(regex))
Expand Down
6 changes: 3 additions & 3 deletions Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ extension RangeReplaceableCollection
// MARK: Regex algorithms

extension RangeReplaceableCollection where SubSequence == Substring {
public func replacing<R: RegexProtocol, Replacement: Collection>(
public func replacing<R: RegexComponent, Replacement: Collection>(
_ regex: R,
with replacement: Replacement,
subrange: Range<Index>,
Expand All @@ -162,7 +162,7 @@ extension RangeReplaceableCollection where SubSequence == Substring {
maxReplacements: maxReplacements)
}

public func replacing<R: RegexProtocol, Replacement: Collection>(
public func replacing<R: RegexComponent, Replacement: Collection>(
_ regex: R,
with replacement: Replacement,
maxReplacements: Int = .max
Expand All @@ -174,7 +174,7 @@ extension RangeReplaceableCollection where SubSequence == Substring {
maxReplacements: maxReplacements)
}

public mutating func replace<R: RegexProtocol, Replacement: Collection>(
public mutating func replace<R: RegexComponent, Replacement: Collection>(
_ regex: R,
with replacement: Replacement,
maxReplacements: Int = .max
Expand Down
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/Algorithms/Algorithms/Split.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ extension BidirectionalCollection where Element: Comparable {
// MARK: Regex algorithms

extension BidirectionalCollection where SubSequence == Substring {
public func split<R: RegexProtocol>(
public func split<R: RegexComponent>(
by separator: R
) -> SplitCollection<RegexConsumer<R, Self>> {
split(by: RegexConsumer(separator))
}

public func splitFromBack<R: RegexProtocol>(
public func splitFromBack<R: RegexComponent>(
by separator: R
) -> ReversedSplitCollection<RegexConsumer<R, Self>> {
splitFromBack(by: RegexConsumer(separator))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ extension BidirectionalCollection where Element: Equatable {
// MARK: Regex algorithms

extension BidirectionalCollection where SubSequence == Substring {
public func starts<R: RegexProtocol>(with regex: R) -> Bool {
public func starts<R: RegexComponent>(with regex: R) -> Bool {
starts(with: RegexConsumer(regex))
}

public func ends<R: RegexProtocol>(with regex: R) -> Bool {
public func ends<R: RegexComponent>(with regex: R) -> Bool {
ends(with: RegexConsumer(regex))
}
}
Loading