Skip to content

DSL free functions -> types #203

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 11, 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
14 changes: 7 additions & 7 deletions Sources/Exercises/Participants/RegexParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ private func graphemeBreakPropertyData(
forLine line: String
) -> GraphemeBreakEntry? {
line.match {
tryCapture(oneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) }
optionally {
TryCapture(OneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) }
Optionally {
".."
tryCapture(oneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) }
TryCapture(OneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) }
}
oneOrMore(.whitespace)
OneOrMore(.whitespace)
";"
oneOrMore(.whitespace)
tryCapture(oneOrMore(.word)) { Unicode.GraphemeBreakProperty($0) }
zeroOrMore(.any)
OneOrMore(.whitespace)
TryCapture(OneOrMore(.word)) { Unicode.GraphemeBreakProperty($0) }
ZeroOrMore(.any)
}.map {
let (_, lower, upper, property) = $0.match
return GraphemeBreakEntry(lower...(upper ?? lower), property)
Expand Down
459 changes: 260 additions & 199 deletions Sources/VariadicsGenerator/VariadicsGenerator.swift

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion Sources/_StringProcessing/RegexDSL/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

@resultBuilder
public enum RegexComponentBuilder {
@_disfavoredOverload
public static func buildBlock() -> Regex<Substring> {
.init(node: .empty)
}

// TODO: Rename to `buildPartialBlock(first:)` when the feature lands.
public static func buildBlock<R0: RegexComponent>(_ r0: R0) -> R0 {
r0
}
Expand Down
126 changes: 89 additions & 37 deletions Sources/_StringProcessing/RegexDSL/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@

import _MatchingEngine

// A convenience protocol for builtin regex components that are initialized with
// a `DSLTree` node.
internal protocol _BuiltinRegexComponent: RegexComponent {
init(_ regex: Regex<Match>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this defaulted with self.regex = regex? Or even just have this in an extension?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protocol initializers can only do delegate initialization, so I don’t think we can. There’s no way to express a formal guarantee that a protocol property will be a stored property.

}

extension _BuiltinRegexComponent {
init(node: DSLTree.Node) {
self.init(Regex(node: node))
}
}

// MARK: - Primitives

extension String: RegexComponent {
Expand Down Expand Up @@ -115,37 +127,49 @@ extension QuantificationBehavior {
}
}

// TODO: Variadic generics
// struct _OneOrMore<W, C..., Component: RegexComponent>
// where R.Match == (W, C...)
// {
// typealias Match = (Substring, [(C...)])
// let regex: Regex<Match>
// init(_ component: Component) {
// regex = .init(oneOrMore(r0))
// }
// }
//
// struct _OneOrMoreNonCapturing<Component: RegexComponent> {
// typealias Match = Substring
// let regex: Regex<Match>
// init(_ component: Component) {
// regex = .init(oneOrMore(r0))
// }
// }
//
// func oneOrMore<W, C..., Component: RegexComponent>(
// _ component: Component
// ) -> <R: RegexComponent where R.Match == (Substring, [(C...)])> R {
// _OneOrMore(component)
// }
//
// @_disfavoredOverload
// func oneOrMore<Component: RegexComponent>(
// _ component: Component
// ) -> <R: RegexComponent where R.Match == Substring> R {
// _OneOrMoreNonCapturing(component)
// }
public struct OneOrMore<Match>: _BuiltinRegexComponent {
public var regex: Regex<Match>

internal init(_ regex: Regex<Match>) {
self.regex = regex
}

// Note: Public initializers and operators are currently gyb'd. See
// Variadics.swift.
}

public struct ZeroOrMore<Match>: _BuiltinRegexComponent {
public var regex: Regex<Match>

internal init(_ regex: Regex<Match>) {
self.regex = regex
}

// Note: Public initializers and operators are currently gyb'd. See
// Variadics.swift.
}

public struct Optionally<Match>: _BuiltinRegexComponent {
public var regex: Regex<Match>

internal init(_ regex: Regex<Match>) {
self.regex = regex
}

// Note: Public initializers and operators are currently gyb'd. See
// Variadics.swift.
}

public struct Repeat<Match>: _BuiltinRegexComponent {
public var regex: Regex<Match>

internal init(_ regex: Regex<Match>) {
self.regex = regex
}

// Note: Public initializers and operators are currently gyb'd. See
// Variadics.swift.
}

postfix operator .?
postfix operator .*
Expand All @@ -168,8 +192,8 @@ postfix operator .+
@resultBuilder
public struct AlternationBuilder {
@_disfavoredOverload
public static func buildBlock<R: RegexComponent>(_ regex: R) -> R {
regex
public static func buildBlock<R: RegexComponent>(_ component: R) -> ChoiceOf<R.Match> {
.init(component.regex)
}

public static func buildExpression<R: RegexComponent>(_ regex: R) -> R {
Expand All @@ -185,10 +209,38 @@ public struct AlternationBuilder {
}
}

public func choiceOf<R: RegexComponent>(
@AlternationBuilder builder: () -> R
) -> R {
builder()
public struct ChoiceOf<Match>: _BuiltinRegexComponent {
public var regex: Regex<Match>

internal init(_ regex: Regex<Match>) {
self.regex = regex
}

public init(@AlternationBuilder _ builder: () -> Self) {
self = builder()
}
}

// MARK: - Capture

public struct Capture<Match>: _BuiltinRegexComponent {
public var regex: Regex<Match>

internal init(_ regex: Regex<Match>) {
self.regex = regex
}

// Note: Public initializers are currently gyb'd. See Variadics.swift.
}

public struct TryCapture<Match>: _BuiltinRegexComponent {
public var regex: Regex<Match>

internal init(_ regex: Regex<Match>) {
self.regex = regex
}

// Note: Public initializers are currently gyb'd. See Variadics.swift.
}

// MARK: - Backreference
Expand Down
Loading