Skip to content

Make variadic builder overloads AEIC (take 3) #520

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 2 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions Sources/RegexBuilder/Anchor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension Anchor: RegexComponent {
}

public var regex: Regex<Substring> {
Regex(node: .atom(.assertion(baseAssertion)))
_RegexFactory.assertion(baseAssertion)
}
}

Expand Down Expand Up @@ -159,14 +159,14 @@ public struct Lookahead<Output>: _BuiltinRegexComponent {
public init<R: RegexComponent>(
_ component: R
) where R.RegexOutput == Output {
self.init(node: .nonCapturingGroup(.lookahead, component.regex.root))
self.init(_RegexFactory.lookaheadNonCapturing(component))
}

/// Creates a lookahead from the regex generated by the given builder closure.
public init<R: RegexComponent>(
@RegexComponentBuilder _ component: () -> R
) where R.RegexOutput == Output {
self.init(node: .nonCapturingGroup(.lookahead, component().regex.root))
self.init(_RegexFactory.lookaheadNonCapturing(component()))
}
}

Expand All @@ -189,14 +189,14 @@ public struct NegativeLookahead<Output>: _BuiltinRegexComponent {
public init<R: RegexComponent>(
_ component: R
) where R.RegexOutput == Output {
self.init(node: .nonCapturingGroup(.negativeLookahead, component.regex.root))
self.init(_RegexFactory.negativeLookaheadNonCapturing(component))
}

/// Creates a negative lookahead from the regex generated by the given builder
/// closure.
public init<R: RegexComponent>(
@RegexComponentBuilder _ component: () -> R
) where R.RegexOutput == Output {
self.init(node: .nonCapturingGroup(.negativeLookahead, component().regex.root))
self.init(_RegexFactory.negativeLookaheadNonCapturing(component()))
}
}
2 changes: 1 addition & 1 deletion Sources/RegexBuilder/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@resultBuilder
public enum RegexComponentBuilder {
public static func buildBlock() -> Regex<Substring> {
.init(node: .empty)
_RegexFactory.empty()
}

public static func buildPartialBlock<R: RegexComponent>(
Expand Down
2 changes: 1 addition & 1 deletion Sources/RegexBuilder/CharacterClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct CharacterClass {
@available(SwiftStdlib 5.7, *)
extension CharacterClass: RegexComponent {
public var regex: Regex<Substring> {
return Regex(node: DSLTree.Node.customCharacterClass(ccc))
_RegexFactory.customCharacterClass(ccc)
}
}

Expand Down
59 changes: 16 additions & 43 deletions Sources/RegexBuilder/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ internal protocol _BuiltinRegexComponent: RegexComponent {
init(_ regex: Regex<RegexOutput>)
}

@available(SwiftStdlib 5.7, *)
extension _BuiltinRegexComponent {
init(node: DSLTree.Node) {
self.init(Regex(node: node))
}
}

// MARK: - Primitive regex components

@available(SwiftStdlib 5.7, *)
Expand All @@ -56,7 +49,7 @@ extension Character: RegexComponent {
public typealias Output = Substring

public var regex: Regex<Output> {
.init(node: .atom(.char(self)))
_RegexFactory.char(self)
}
}

Expand All @@ -65,7 +58,7 @@ extension UnicodeScalar: RegexComponent {
public typealias Output = Substring

public var regex: Regex<Output> {
.init(node: .atom(.scalar(self)))
_RegexFactory.scalar(self)
}
}

Expand All @@ -90,39 +83,6 @@ extension UnicodeScalar: RegexComponent {

// Note: Quantifiers are currently gyb'd.

extension DSLTree.Node {
// Individual public API functions are in the generated Variadics.swift file.
/// Generates a DSL tree node for a repeated range of the given node.
@available(SwiftStdlib 5.7, *)
static func repeating(
_ range: Range<Int>,
_ behavior: RegexRepetitionBehavior?,
_ 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")

let kind: DSLTree.QuantificationKind = behavior.map { .explicit($0.dslTreeKind) } ?? .default

switch (range.lowerBound, range.upperBound) {
case (0, Int.max): // 0...
return .quantification(.zeroOrMore, kind, node)
case (1, Int.max): // 1...
return .quantification(.oneOrMore, kind, 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(range.lowerBound), .default, node)
case (0, _): // 0..<n or 0...n or ..<n or ...n
return .quantification(.upToN(range.upperBound), kind, node)
case (_, Int.max): // n...
return .quantification(.nOrMore(range.lowerBound), kind, node)
default: // any other range
return .quantification(.range(range.lowerBound, range.upperBound), kind, node)
}
}
}

/// A regex component that matches exactly one occurrence of its underlying
/// component.
@available(SwiftStdlib 5.7, *)
Expand All @@ -140,6 +100,7 @@ public struct One<Output>: RegexComponent {
public struct OneOrMore<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

@usableFromInline
internal init(_ regex: Regex<Output>) {
self.regex = regex
}
Expand All @@ -152,6 +113,7 @@ public struct OneOrMore<Output>: _BuiltinRegexComponent {
public struct ZeroOrMore<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

@usableFromInline
internal init(_ regex: Regex<Output>) {
self.regex = regex
}
Expand All @@ -164,6 +126,7 @@ public struct ZeroOrMore<Output>: _BuiltinRegexComponent {
public struct Optionally<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

@usableFromInline
internal init(_ regex: Regex<Output>) {
self.regex = regex
}
Expand All @@ -176,6 +139,7 @@ public struct Optionally<Output>: _BuiltinRegexComponent {
public struct Repeat<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

@usableFromInline
internal init(_ regex: Regex<Output>) {
self.regex = regex
}
Expand Down Expand Up @@ -217,6 +181,7 @@ public struct AlternationBuilder {
public struct ChoiceOf<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

@usableFromInline
internal init(_ regex: Regex<Output>) {
self.regex = regex
}
Expand All @@ -232,6 +197,7 @@ public struct ChoiceOf<Output>: _BuiltinRegexComponent {
public struct Capture<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

@usableFromInline
internal init(_ regex: Regex<Output>) {
self.regex = regex
}
Expand All @@ -243,6 +209,7 @@ public struct Capture<Output>: _BuiltinRegexComponent {
public struct TryCapture<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

@usableFromInline
internal init(_ regex: Regex<Output>) {
self.regex = regex
}
Expand All @@ -260,6 +227,7 @@ public struct TryCapture<Output>: _BuiltinRegexComponent {
public struct Local<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

@usableFromInline
internal init(_ regex: Regex<Output>) {
self.regex = regex
}
Expand All @@ -274,8 +242,13 @@ public struct Reference<Capture>: RegexComponent {

public init(_ captureType: Capture.Type = Capture.self) {}

@usableFromInline
var _raw: Int {
id._raw
}

public var regex: Regex<Capture> {
.init(node: .atom(.symbolicReference(id)))
_RegexFactory.symbolicReference(id)
}
}

Expand Down
Loading