Skip to content

Add support for quantification kinds to the DSL #150

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 8 commits 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
48 changes: 21 additions & 27 deletions Sources/VariadicsGenerator/VariadicsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,14 @@ struct VariadicsGenerator: ParsableCommand {
}

func emitConcatenation(leftArity: Int, rightArity: Int) {
func genericParameters(withConstraints: Bool) -> String {
let genericParams: String = {
var result = "W0, W1"
result += (0..<leftArity+rightArity).map {
", C\($0)"
}.joined()
result += ", "
if withConstraints {
result += "R0: \(regexProtocolName), R1: \(regexProtocolName)"
} else {
result += "R0, R1"
}
result += ", R0: \(regexProtocolName), R1: \(regexProtocolName)"
return result
}
}()

// Emit concatenation type declaration.

Expand Down Expand Up @@ -260,13 +255,13 @@ struct VariadicsGenerator: ParsableCommand {
// Emit concatenation builder.
output("extension \(patternBuilderTypeName) {\n")
output("""
@_disfavoredOverload
public static func buildBlock<\(genericParameters(withConstraints: true))>(
combining next: R1, into combined: R0
) -> \(regexTypeName)<\(matchType)> \(whereClause) {
.init(node: combined.regex.root.appending(next.regex.root))
}
@_disfavoredOverload
public static func buildBlock<\(genericParams)>(
combining next: R1, into combined: R0
) -> \(regexTypeName)<\(matchType)> \(whereClause) {
.init(node: combined.regex.root.appending(next.regex.root))
}
}

""")
}
Expand Down Expand Up @@ -338,19 +333,16 @@ struct VariadicsGenerator: ParsableCommand {

func emitQuantifier(kind: QuantifierKind, arity: Int) {
assert(arity >= 0)
func genericParameters(withConstraints: Bool) -> String {
let genericParams: String = {
var result = ""
if arity > 0 {
result += "W"
result += (0..<arity).map { ", C\($0)" }.joined()
result += ", "
}
result += "Component"
if withConstraints {
result += ": \(regexProtocolName)"
}
result += "Component: \(regexProtocolName)"
return result
}
}()
let captures = (0..<arity).map { "C\($0)" }.joined(separator: ", ")
let capturesTupled = arity == 1 ? captures : "(\(captures))"
let whereClause: String = arity == 0 ? "" :
Expand All @@ -366,21 +358,23 @@ struct VariadicsGenerator: ParsableCommand {
let matchType = arity == 0 ? baseMatchTypeName : "(\(baseMatchTypeName), \(quantifiedCaptures))"
output("""
\(arity == 0 ? "@_disfavoredOverload" : "")
public func \(kind.rawValue)<\(genericParameters(withConstraints: true))>(
_ component: Component
public func \(kind.rawValue)<\(genericParams)>(
_ component: Component,
_ behavior: QuantificationBehavior = .eagerly
) -> \(regexTypeName)<\(matchType)> \(whereClause) {
.init(node: .quantification(.\(kind.astQuantifierAmount), .eager, component.regex.root))
.init(node: .quantification(.\(kind.astQuantifierAmount), behavior.astKind, component.regex.root))
}

\(arity == 0 ? "@_disfavoredOverload" : "")
public func \(kind.rawValue)<\(genericParameters(withConstraints: true))>(
public func \(kind.rawValue)<\(genericParams)>(
_ behavior: QuantificationBehavior = .eagerly,
@RegexBuilder _ component: () -> Component
) -> \(regexTypeName)<\(matchType)> \(whereClause) {
.init(node: .quantification(.\(kind.astQuantifierAmount), .eager, component().regex.root))
.init(node: .quantification(.\(kind.astQuantifierAmount), behavior.astKind, component().regex.root))
}

\(arity == 0 ? "@_disfavoredOverload" : "")
public postfix func \(kind.operatorName)<\(genericParameters(withConstraints: true))>(
public postfix func \(kind.operatorName)<\(genericParams)>(
_ component: Component
) -> \(regexTypeName)<\(matchType)> \(whereClause) {
.init(node: .quantification(.\(kind.astQuantifierAmount), .eager, component.regex.root))
Expand All @@ -389,7 +383,7 @@ struct VariadicsGenerator: ParsableCommand {
\(kind == .zeroOrOne ?
"""
extension RegexBuilder {
public static func buildLimitedAvailability<\(genericParameters(withConstraints: true))>(
public static func buildLimitedAvailability<\(genericParams)>(
_ component: Component
) -> \(regexTypeName)<\(matchType)> \(whereClause) {
.init(node: .quantification(.\(kind.astQuantifierAmount), .eager, component.regex.root))
Expand Down
59 changes: 53 additions & 6 deletions Sources/_StringProcessing/RegexDSL/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,44 @@ extension CharacterClass: RegexProtocol {

// Note: Quantifiers are currently gyb'd.

/// Specifies how much to attempt to match when using a quantifier.
public struct QuantificationBehavior {
Copy link
Member

Choose a reason for hiding this comment

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

Thoughts on this being a "thing" in API vs a set of operations on quantifiable patterns? E.g. there could be a sub-protocol for quantifiables with a var eagerly: Self requirement.

(Not arguing one way or another)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it makes the most sense as a parameter to the quantification methods — making it an operation on a oneOrMore that uses a builder puts the adverb too far from what it describes. The .eagerly in this example doesn't read clearly as modifying oneOrMore to me:

let regex = Regex {
    oneOrMore {
        "a"
        optionally(.digit)
        .word
    }.eagerly
}

internal enum Kind {
case eagerly
case reluctantly
case possessively
}

var kind: Kind

internal var astKind: AST.Quantification.Kind {
switch kind {
case .eagerly: return .eager
case .reluctantly: return .reluctant
case .possessively: return .possessive
}
}
}

extension QuantificationBehavior {
/// Match as much of the input string as possible, backtracking when
/// necessary.
public static var eagerly: QuantificationBehavior {
.init(kind: .eagerly)
}

/// Match as little of the input string as possible, expanding the matched
/// region as necessary to complete a match.
public static var reluctantly: QuantificationBehavior {
.init(kind: .reluctantly)
}

/// Match as much of the input string as possible, performing no backtracking.
public static var possessively: QuantificationBehavior {
.init(kind: .possessively)
}
}

// TODO: Variadic generics
// struct _OneOrMore<W, C..., Component: RegexProtocol>
// where R.Match == (W, C...)
Expand Down Expand Up @@ -99,16 +137,25 @@ postfix operator .*
postfix operator .+

// Overloads for quantifying over a character class.
public func zeroOrOne(_ cc: CharacterClass) -> Regex<Substring> {
.init(node: .quantification(.zeroOrOne, .eager, cc.regex.root))
public func zeroOrOne(
_ cc: CharacterClass,
_ behavior: QuantificationBehavior = .eagerly
) -> Regex<Substring> {
.init(node: .quantification(.zeroOrOne, behavior.astKind, cc.regex.root))
}

public func many(_ cc: CharacterClass) -> Regex<Substring> {
.init(node: .quantification(.zeroOrMore, .eager, cc.regex.root))
public func many(
_ cc: CharacterClass,
_ behavior: QuantificationBehavior = .eagerly
) -> Regex<Substring> {
.init(node: .quantification(.zeroOrMore, behavior.astKind, cc.regex.root))
}

public func oneOrMore(_ cc: CharacterClass) -> Regex<Substring> {
.init(node: .quantification(.oneOrMore, .eager, cc.regex.root))
public func oneOrMore(
_ cc: CharacterClass,
_ behavior: QuantificationBehavior = .eagerly
) -> Regex<Substring> {
.init(node: .quantification(.oneOrMore, behavior.astKind, cc.regex.root))
}

// MARK: Alternation
Expand Down
Loading