Skip to content

Drop arrays and unzip tuples for quantified captures. #162

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 15, 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
18 changes: 9 additions & 9 deletions Sources/VariadicsGenerator/VariadicsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ struct VariadicsGenerator: ParsableCommand {
var operatorName: String {
switch self {
case .zeroOrOne: return ".?"
case .zeroOrMore: return ".+"
case .oneOrMore: return ".*"
case .zeroOrMore: return ".*"
case .oneOrMore: return ".+"
}
}

Expand All @@ -320,16 +320,16 @@ struct VariadicsGenerator: ParsableCommand {
result += "Component: \(regexProtocolName)"
return result
}()
let captures = (0..<arity).map { "C\($0)" }.joined(separator: ", ")
let capturesTupled = arity == 1 ? captures : "(\(captures))"
let captures = (0..<arity).map { "C\($0)" }
let capturesJoined = captures.joined(separator: ", ")
let whereClause: String = arity == 0 ? "" :
"where Component.Match == (W, \(captures))"
"where Component.Match == (W, \(capturesJoined))"
let quantifiedCaptures: String = {
switch kind {
case .zeroOrOne:
return "\(capturesTupled)?"
case .zeroOrMore, .oneOrMore:
return "[\(capturesTupled)]"
case .zeroOrOne, .zeroOrMore:
return captures.map { "\($0)?" }.joined(separator: ", ")
case .oneOrMore:
return capturesJoined
}
}()
let matchType = arity == 0 ? baseMatchTypeName : "(\(baseMatchTypeName), \(quantifiedCaptures))"
Expand Down
11 changes: 4 additions & 7 deletions Sources/_MatchingEngine/Regex/Parse/CaptureStructure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ extension CaptureStructure.Constructor {
public mutating func alternating<C: Collection>(
_ children: C
) -> CaptureStructure where C.Element: _TreeNode {
// assert(children.count > 1)
return children.map {
$0._captureStructure(&self)
}.reduce(.empty, +)
Expand Down Expand Up @@ -113,17 +112,15 @@ extension CaptureStructure.Constructor {
}
let branchCaptures = trueBranch._captureStructure(&self) +
falseBranch._captureStructure(&self)
return captures + branchCaptures.map(
CaptureStructure.optional)
return captures + branchCaptures.map(CaptureStructure.optional)
}

public mutating func quantifying<T: _TreeNode>(
_ child: T, amount: AST.Quantification.Amount
) -> CaptureStructure {
return child._captureStructure(&self).map(
amount == .zeroOrOne
? CaptureStructure.optional
: CaptureStructure.array)
let result = child._captureStructure(&self)
return amount.bounds.atLeast == 0
? result.map(CaptureStructure.optional) : result
}

// TODO: Will need to adjust for DSLTree support, and
Expand Down
Loading