Skip to content

Fully generalize "whole match" in the engine and enable transforming custom types #470

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
Jun 9, 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
836 changes: 308 additions & 528 deletions Sources/RegexBuilder/Variadics.swift

Large diffs are not rendered by default.

76 changes: 28 additions & 48 deletions Sources/VariadicsGenerator/VariadicsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -646,28 +646,23 @@ struct VariadicsGenerator: ParsableCommand {
\(disfavored)\
public init<\(genericParams), NewCapture>(
_ component: R,
transform: @escaping (Substring) throws -> NewCapture
transform: @escaping (W) throws -> NewCapture
) \(whereClauseTransformed) {
self.init(node: .capture(.transform(
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
},
component.regex.root)))
self.init(node: .capture(
component.regex.root,
CaptureTransform(transform)))
}

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

Expand All @@ -676,28 +671,23 @@ struct VariadicsGenerator: ParsableCommand {
\(disfavored)\
public init<\(genericParams), NewCapture>(
_ component: R,
transform: @escaping (Substring) throws -> NewCapture?
transform: @escaping (W) throws -> NewCapture?
) \(whereClauseTransformed) {
self.init(node: .capture(.transform(
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any?
},
component.regex.root)))
self.init(node: .capture(
component.regex.root,
CaptureTransform(transform)))
}

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

Expand Down Expand Up @@ -725,28 +715,23 @@ struct VariadicsGenerator: ParsableCommand {
\(disfavored)\
public init<\(genericParams), NewCapture>(
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) throws -> NewCapture
transform: @escaping (W) throws -> NewCapture
) \(whereClauseTransformed) {
self.init(node: .capture(.transform(
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any
},
component().regex.root)))
self.init(node: .capture(
component().regex.root,
CaptureTransform(transform)))
}

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

Expand All @@ -755,28 +740,23 @@ struct VariadicsGenerator: ParsableCommand {
\(disfavored)\
public init<\(genericParams), NewCapture>(
@\(concatBuilderName) _ component: () -> R,
transform: @escaping (Substring) throws -> NewCapture?
transform: @escaping (W) throws -> NewCapture?
) \(whereClauseTransformed) {
self.init(node: .capture(.transform(
CaptureTransform(resultType: NewCapture.self) {
try transform($0) as Any?
},
component().regex.root)))
self.init(node: .capture(
component().regex.root,
CaptureTransform(transform)))
}

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

Expand Down
24 changes: 8 additions & 16 deletions Sources/_RegexParser/Regex/Parse/CaptureList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public struct CaptureList {
extension CaptureList {
public struct Capture {
public var name: String?
public var type: Any.Type?
public var type: Any.Type
public var optionalDepth: Int
public var location: SourceLocation

public init(
name: String? = nil,
type: Any.Type? = nil,
type: Any.Type = Substring.self,
optionalDepth: Int,
_ location: SourceLocation
) {
Expand Down Expand Up @@ -122,18 +122,15 @@ extension AST.Node {
break
}
}

public var _captureList: CaptureList {
var caps = CaptureList()
self._addCaptures(to: &caps, optionalNesting: 0)
return caps
}
}

extension AST {
/// Get the capture list for this AST
/// The capture list (including the whole match) of this AST.
public var captureList: CaptureList {
root._captureList
var caps = CaptureList()
caps.append(.init(optionalDepth: 0, .fake))
root._addCaptures(to: &caps, optionalNesting: 0)
return caps
}
}

Expand All @@ -151,12 +148,7 @@ extension CaptureList: Equatable {}

extension CaptureList.Capture: CustomStringConvertible {
public var description: String {
let typeStr: String
if let ty = type {
typeStr = "\(ty)"
} else {
typeStr = "Substring"
}
let typeStr = String(describing: type)
let suffix = String(repeating: "?", count: optionalDepth)
return typeStr + suffix
}
Expand Down
7 changes: 2 additions & 5 deletions Sources/_RegexParser/Regex/Parse/CaptureStructure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ extension CaptureStructure: CustomStringConvertible {
extension AST {
/// The capture structure of this AST for compiler communication.
var captureStructure: CaptureStructure {
root._captureList._captureStructure(nestOptionals: true)
captureList._captureStructure(nestOptionals: true)
}
}

Expand All @@ -246,10 +246,7 @@ extension CaptureList {
extension CaptureList.Capture {
func _captureStructure(nestOptionals: Bool) -> CaptureStructure {
if optionalDepth == 0 {
if let ty = type {
return .atom(name: name, type: .init(ty))
}
return .atom(name: name)
return .atom(name: name, type: type == Substring.self ? nil : .init(type))
}
var copy = self
copy.optionalDepth = 0
Expand Down
2 changes: 1 addition & 1 deletion Sources/_RegexParser/Regex/Parse/Sema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extension RegexValidator {
}
switch ref.kind {
case .absolute(let i):
guard i <= captures.captures.count else {
guard i < captures.captures.count else {
throw error(.invalidReference(i), at: ref.innerLoc)
}
case .named(let name):
Expand Down
Loading