Skip to content

Prefix capture and tryCapture. #151

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
Feb 10, 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
6 changes: 3 additions & 3 deletions Sources/Exercises/Participants/RegexParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ private func graphemeBreakPropertyData(
forLine line: String
) -> GraphemeBreakEntry? {
line.match {
oneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:))
tryCapture(oneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) }
optionally {
".."
oneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:))
tryCapture(oneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) }
}
oneOrMore(.whitespace)
";"
oneOrMore(.whitespace)
oneOrMore(.word).tryCapture(Unicode.GraphemeBreakProperty.init)
tryCapture(oneOrMore(.word)) { Unicode.GraphemeBreakProperty($0) }
many(.any)
}.map {
let (_, lower, upper, property) = $0.match
Expand Down
107 changes: 107 additions & 0 deletions Sources/VariadicsGenerator/VariadicsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ struct VariadicsGenerator: ParsableCommand {
emitUnaryAlternationBuildBlock(arity: arity)
}

print("Generating 'capture' and 'tryCapture' overloads...", to: &standardError)
for arity in 0..<maxArity {
print(" Capture arity: \(arity)", to: &standardError)
emitCapture(arity: arity)
}

output("\n\n")

output("// END AUTO-GENERATED CONTENT\n")
Expand Down Expand Up @@ -471,4 +477,105 @@ struct VariadicsGenerator: ParsableCommand {

""")
}

func emitCapture(arity: Int) {
let genericParams = arity == 0
? "R: \(regexProtocolName), W"
: "R: \(regexProtocolName), W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ")
let matchType = arity == 0
? "W"
: "(W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ") + ")"
func newMatchType(transformed: Bool) -> String {
let newCaptureType = transformed ? "NewCapture" : baseMatchTypeName
return arity == 0
? "(W, \(newCaptureType))"
: "(W, \(newCaptureType), " + (0..<arity).map { "C\($0)" }.joined(separator: ", ") + ")"
}
let whereClause = "where R.\(matchAssociatedTypeName) == \(matchType)"
output("""
// MARK: - Non-builder capture arity \(arity)

public func capture<\(genericParams)>(_ component: R) -> \(regexTypeName)<\(newMatchType(transformed: false))> \(whereClause) {
.init(node: .group(.capture, component.regex.root))
}

public func capture<\(genericParams), NewCapture>(
_ component: R, transform: @escaping (Substring) -> NewCapture
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
.init(node: .groupTransform(
.capture,
component.regex.root,
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any
}))
}

public func tryCapture<\(genericParams), NewCapture>(
_ component: R, transform: @escaping (Substring) throws -> NewCapture
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
.init(node: .groupTransform(
.capture,
component.regex.root,
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
}))
}

public func tryCapture<\(genericParams), NewCapture>(
_ component: R, transform: @escaping (Substring) -> NewCapture?
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
.init(node: .groupTransform(
.capture,
component.regex.root,
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any?
}))
}

// MARK: - Builder capture arity \(arity)

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

public func capture<\(genericParams), NewCapture>(
@RegexBuilder _ component: () -> R,
transform: @escaping (Substring) -> NewCapture
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
.init(node: .groupTransform(
.capture,
component().regex.root,
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any
}))
}

public func tryCapture<\(genericParams), NewCapture>(
@RegexBuilder _ component: () -> R,
transform: @escaping (Substring) throws -> NewCapture
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
.init(node: .groupTransform(
.capture,
component().regex.root,
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
}))
}

public func tryCapture<\(genericParams), NewCapture>(
@RegexBuilder _ component: () -> R,
transform: @escaping (Substring) -> NewCapture?
) -> \(regexTypeName)<\(newMatchType(transformed: true))> \(whereClause) {
.init(node: .groupTransform(
.capture,
component().regex.root,
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any?
}))
}

""")
}
}
56 changes: 0 additions & 56 deletions Sources/_StringProcessing/RegexDSL/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,59 +150,3 @@ public func oneOf<R: RegexProtocol>(
) -> R {
builder()
}

// MARK: - Capture

public struct CapturingGroup<Match>: RegexProtocol {
public let regex: Regex<Match>

init<Component: RegexProtocol>(
_ component: Component
) {
self.regex = .init(node: .group(
.capture, component.regex.root))
}

init<Component: RegexProtocol>(
_ component: Component,
transform: CaptureTransform
) {
self.regex = .init(node: .groupTransform(
.capture,
component.regex.root,
transform))
}

init<NewCapture, Component: RegexProtocol>(
_ component: Component,
transform: @escaping (Substring) -> NewCapture
) {
self.init(
component,
transform: CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any
})
}

init<NewCapture, Component: RegexProtocol>(
_ component: Component,
transform: @escaping (Substring) throws -> NewCapture
) {
self.init(
component,
transform: CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
})
}

init<NewCapture, Component: RegexProtocol>(
_ component: Component,
transform: @escaping (Substring) -> NewCapture?
) {
self.init(
component,
transform: CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any?
})
}
}
120 changes: 0 additions & 120 deletions Sources/_StringProcessing/RegexDSL/DSLCapture.swift

This file was deleted.

Loading