Skip to content

Implement .as for Regex and Unify Match and AnyRegexOutput #376

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 5 commits into from
May 18, 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
21 changes: 0 additions & 21 deletions Sources/_StringProcessing/Capture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@

@_implementationOnly import _RegexParser

/// A structured capture
struct StructuredCapture {
/// The `.optional` height of the result
var optionalCount = 0

var storedCapture: StoredCapture?

var someCount: Int {
storedCapture == nil ? optionalCount - 1 : optionalCount
}
}

/// A storage form for a successful capture
struct StoredCapture {
// TODO: drop optional when engine tracks all ranges
var range: Range<String.Index>?

// If strongly typed, value is set
var value: Any? = nil
}

// TODO: Where should this live? Inside TypeConstruction?
func constructExistentialOutputComponent(
from input: Substring,
Expand Down
27 changes: 16 additions & 11 deletions Sources/_StringProcessing/Engine/Structuralize.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
@_implementationOnly import _RegexParser

extension CaptureList {
func structuralize(
@available(SwiftStdlib 5.7, *)
func createElements(
_ list: MECaptureList,
_ input: String
) -> [StructuredCapture] {
) -> [AnyRegexOutput.ElementRepresentation] {
assert(list.values.count == captures.count)

var result = [StructuredCapture]()
for (cap, meStored) in zip(self.captures, list.values) {
let stored = StoredCapture(
range: meStored.latest, value: meStored.latestValue)

result.append(.init(
optionalCount: cap.optionalDepth, storedCapture: stored))

var result = [AnyRegexOutput.ElementRepresentation]()

for (cap, meStored) in zip(captures, list.values) {
let element = AnyRegexOutput.ElementRepresentation(
optionalDepth: cap.optionalDepth,
bounds: meStored.latest,
name: cap.name,
value: meStored.latestValue
)

result.append(element)
}

return result
}
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/_StringProcessing/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Executor {
namedCaptureOffsets: engine.program.namedCaptureOffsets)

let range = inputRange.lowerBound..<endIdx
let caps = engine.program.captureList.structuralize(capList, input)
let caps = engine.program.captureList.createElements(capList, input)

// FIXME: This is a workaround for not tracking (or
// specially compiling) whole-match values.
Expand All @@ -58,7 +58,6 @@ struct Executor {

let anyRegexOutput = AnyRegexOutput(
input: input,
namedCaptureOffsets: capList.namedCaptureOffsets,
elements: caps
)

Expand Down
37 changes: 19 additions & 18 deletions Sources/_StringProcessing/Regex/AnyRegexOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ extension Regex.Match where Output == AnyRegexOutput {
}

public subscript(name: String) -> AnyRegexOutput.Element? {
anyRegexOutput.namedCaptureOffsets[name].map { self[$0 + 1] }
anyRegexOutput.first {
$0.name == name
}
}
}

/// A type-erased regex output.
@available(SwiftStdlib 5.7, *)
public struct AnyRegexOutput {
let input: String
let namedCaptureOffsets: [String: Int]
let _elements: [ElementRepresentation]

/// The underlying representation of the element of a type-erased regex
Expand All @@ -65,6 +66,10 @@ public struct AnyRegexOutput {

/// The bounds of the output element.
let bounds: Range<String.Index>?

/// The name of the capture.
var name: String? = nil

/// If the output vaule is strongly typed, then this will be set.
var value: Any? = nil
}
Expand Down Expand Up @@ -96,27 +101,16 @@ extension AnyRegexOutput {

@available(SwiftStdlib 5.7, *)
extension AnyRegexOutput {
internal init<C: Collection>(
input: String, namedCaptureOffsets: [String: Int], elements: C
) where C.Element == StructuredCapture {
internal init(input: String, elements: [ElementRepresentation]) {
self.init(
input: input,
namedCaptureOffsets: namedCaptureOffsets,
_elements: elements.map(ElementRepresentation.init)
_elements: elements
)
}
}

@available(SwiftStdlib 5.7, *)
extension AnyRegexOutput.ElementRepresentation {
init(_ element: StructuredCapture) {
self.init(
optionalDepth: element.optionalCount,
bounds: element.storedCapture.flatMap(\.range),
value: element.storedCapture.flatMap(\.value)
)
}

func value(forInput input: String) -> Any {
// Ok for now because `existentialMatchComponent`
// wont slice the input if there's no range to slice with
Expand All @@ -128,7 +122,8 @@ extension AnyRegexOutput.ElementRepresentation {
from: input,
in: bounds,
value: nil,
optionalCount: optionalDepth)
optionalCount: optionalDepth
)
}
}

Expand All @@ -141,7 +136,11 @@ extension AnyRegexOutput: RandomAccessCollection {
var optionalDepth: Int {
representation.optionalDepth
}


var name: String? {
representation.name
}

/// The range over which a value was captured. `nil` for no-capture.
public var range: Range<String.Index>? {
representation.bounds
Expand Down Expand Up @@ -186,7 +185,9 @@ extension AnyRegexOutput: RandomAccessCollection {
@available(SwiftStdlib 5.7, *)
extension AnyRegexOutput {
public subscript(name: String) -> Element? {
namedCaptureOffsets[name].map { self[$0 + 1] }
first {
$0.name == name
}
}
}

Expand Down
1 change: 0 additions & 1 deletion Sources/_StringProcessing/Regex/Match.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ extension Regex.Match {

let output = AnyRegexOutput(
input: anyRegexOutput.input,
namedCaptureOffsets: anyRegexOutput.namedCaptureOffsets,
_elements: [wholeMatchCapture] + anyRegexOutput._elements
)

Expand Down
5 changes: 4 additions & 1 deletion Sources/_StringProcessing/Utility/TypeVerification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ extension Regex {

let createdType = TypeConstruction.tupleType(
of: tupleElements,
labels: labels

// If all of our labels are spaces, that means no actual label was added
// to the tuple. In that case, don't pass a label string.
labels: labels.all { $0 == " " } ? nil : labels
)

return Output.self == createdType
Expand Down
6 changes: 3 additions & 3 deletions Tests/RegexTests/CaptureTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,17 @@ extension RegexTests {

func testTypeVerification() throws {
let opaque1 = try Regex("abc")
let concrete1 = try XCTUnwrap(opaque1.as(Substring.self))
_ = try XCTUnwrap(opaque1.as(Substring.self))
XCTAssertNil(opaque1.as((Substring, Substring).self))
XCTAssertNil(opaque1.as(Int.self))

let opaque2 = try Regex("(abc)")
let concrete2 = try XCTUnwrap(opaque2.as((Substring, Substring).self))
_ = try XCTUnwrap(opaque2.as((Substring, Substring).self))
XCTAssertNil(opaque2.as(Substring.self))
XCTAssertNil(opaque2.as((Substring, Int).self))

let opaque3 = try Regex("(?<someLabel>abc)")
let concrete3 = try XCTUnwrap(opaque3.as((Substring, someLabel: Substring).self))
_ = try XCTUnwrap(opaque3.as((Substring, someLabel: Substring).self))
XCTAssertNil(opaque3.as((Substring, Substring).self))
XCTAssertNil(opaque3.as(Substring.self))
}
Expand Down