Skip to content

Separate captures in DSLTree #206

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 20, 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
138 changes: 67 additions & 71 deletions Sources/VariadicsGenerator/VariadicsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ struct VariadicsGenerator: ParsableCommand {
output("""
extension \(altBuilderName) {
public static func buildPartialBlock<\(genericParams)>(first regex: R) -> ChoiceOf<(W, \(resultCaptures))> \(whereClause) {
.init(node: .alternation([regex.regex.root]))
.init(node: .orderedChoice([regex.regex.root]))
}
}

Expand Down Expand Up @@ -554,27 +554,26 @@ struct VariadicsGenerator: ParsableCommand {
public init<\(genericParams)>(
_ component: R
) \(whereClauseRaw) {
self.init(node: .group(.capture, component.regex.root))
self.init(node: .capture(component.regex.root))
}

\(disfavored)\
public init<\(genericParams)>(
_ component: R, as reference: Reference<W>
) \(whereClauseRaw) {
self.init(node: .group(.capture, component.regex.root, reference.id))
self.init(node: .capture(reference: reference.id, component.regex.root))
}

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

\(disfavored)\
Expand All @@ -583,13 +582,13 @@ struct VariadicsGenerator: ParsableCommand {
as reference: Reference<NewCapture>,
transform: @escaping (Substring) -> NewCapture
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component.regex.root,
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any
},
reference.id))
self.init(node: .capture(
reference: reference.id,
.transform(
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any
},
component.regex.root)))
}
}

Expand All @@ -599,12 +598,11 @@ struct VariadicsGenerator: ParsableCommand {
_ component: R,
transform: @escaping (Substring) throws -> NewCapture
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component.regex.root,
self.init(node: .capture(.transform(
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
}))
},
component.regex.root)))
}

\(disfavored)\
Expand All @@ -613,26 +611,25 @@ struct VariadicsGenerator: ParsableCommand {
as reference: Reference<NewCapture>,
transform: @escaping (Substring) throws -> NewCapture
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component.regex.root,
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
},
reference.id))
self.init(node: .capture(
reference: reference.id,
.transform(
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
},
component.regex.root)))
}

\(disfavored)\
public init<\(genericParams), NewCapture>(
_ component: R,
transform: @escaping (Substring) -> NewCapture?
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component.regex.root,
self.init(node: .capture(.transform(
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any?
}))
},
component.regex.root)))
}

\(disfavored)\
Expand All @@ -641,13 +638,13 @@ struct VariadicsGenerator: ParsableCommand {
as reference: Reference<NewCapture>,
transform: @escaping (Substring) -> NewCapture?
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component.regex.root,
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any?
},
reference.id))
self.init(node: .capture(
reference: reference.id,
.transform(
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any?
},
component.regex.root)))
}
}

Expand All @@ -658,28 +655,29 @@ struct VariadicsGenerator: ParsableCommand {
public init<\(genericParams)>(
@\(concatBuilderName) _ component: () -> R
) \(whereClauseRaw) {
self.init(node: .group(.capture, component().regex.root))
self.init(node: .capture(component().regex.root))
}

\(disfavored)\
public init<\(genericParams)>(
as reference: Reference<W>,
@\(concatBuilderName) _ component: () -> R
) \(whereClauseRaw) {
self.init(node: .group(.capture, component().regex.root, reference.id))
self.init(node: .capture(
reference: reference.id,
component().regex.root))
}

\(disfavored)\
public init<\(genericParams), NewCapture>(
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) -> NewCapture
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component().regex.root,
self.init(node: .capture(.transform(
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any
}))
},
component().regex.root)))
}

\(disfavored)\
Expand All @@ -688,13 +686,13 @@ struct VariadicsGenerator: ParsableCommand {
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) -> NewCapture
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component().regex.root,
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any
},
reference.id))
self.init(node: .capture(
reference: reference.id,
.transform(
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any
},
component().regex.root)))
}
}

Expand All @@ -704,12 +702,11 @@ struct VariadicsGenerator: ParsableCommand {
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) throws -> NewCapture
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component().regex.root,
self.init(node: .capture(.transform(
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
}))
},
component().regex.root)))
}

\(disfavored)\
Expand All @@ -718,26 +715,25 @@ struct VariadicsGenerator: ParsableCommand {
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) throws -> NewCapture
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component().regex.root,
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
},
reference.id))
self.init(node: .capture(
reference: reference.id,
.transform(
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
},
component().regex.root)))
}

\(disfavored)\
public init<\(genericParams), NewCapture>(
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) -> NewCapture?
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component().regex.root,
self.init(node: .capture(.transform(
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any?
}))
},
component().regex.root)))
}

\(disfavored)\
Expand All @@ -746,13 +742,13 @@ struct VariadicsGenerator: ParsableCommand {
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) -> NewCapture?
) \(whereClauseTransformed) {
self.init(node: .groupTransform(
.capture,
component().regex.root,
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any?
},
reference.id))
self.init(node: .capture(
reference: reference.id,
.transform(
CaptureTransform(resultType: NewCapture.self) {
transform($0) as Any?
},
component().regex.root)))
}
}

Expand Down
47 changes: 11 additions & 36 deletions Sources/_MatchingEngine/Regex/Parse/CaptureStructure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,54 +59,29 @@ extension CaptureStructure.Constructor {
}

public mutating func grouping<T: _TreeNode>(
_ child: T, as kind: AST.Group.Kind
_ child: T,
as kind: AST.Group.Kind
) -> CaptureStructure {
let innerCaptures = child._captureStructure(&self)
switch kind {
case .capture:
return .atom() + innerCaptures
return capturing(child)
case .namedCapture(let name):
return .atom(name: name.value) + innerCaptures
return capturing(name: name.value, child)
case .balancedCapture(let b):
return .atom(name: b.name?.value) + innerCaptures
return capturing(name: b.name?.value, child)
default:
precondition(!kind.isCapturing)
return innerCaptures
}
}

public mutating func grouping<T: _TreeNode>(
_ child: T,
as kind: AST.Group.Kind,
withTransform transform: CaptureTransform
) -> CaptureStructure {
let innerCaptures = child._captureStructure(&self)
switch kind {
case .capture:
return .atom(type: AnyType(transform.resultType)) + innerCaptures
case .namedCapture(let name):
return .atom(name: name.value, type: AnyType(transform.resultType))
+ innerCaptures
default:
return innerCaptures
return child._captureStructure(&self)
}
}

public mutating func grouping<T: _TreeNode>(
public mutating func capturing<T: _TreeNode>(
name: String? = nil,
_ child: T,
as kind: AST.Group.Kind,
withType type: AnyType
withType type: AnyType? = nil
) -> CaptureStructure {
let innerCaptures = child._captureStructure(&self)
switch kind {
case .capture:
return .atom(type: type) + innerCaptures
case .namedCapture(let name):
return .atom(name: name.value, type: type)
+ innerCaptures
default:
return innerCaptures
}
.atom(name: name, type: type)
+ child._captureStructure(&self)
}

// TODO: We'll likely want/need a generalization of
Expand Down
Loading