Skip to content

Add Regex.Match.subscript(_: Reference) to DSL pitch. #212

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 17, 2022
Merged
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
11 changes: 7 additions & 4 deletions Documentation/Evolution/RegexBuilderDSL.md
Original file line number Diff line number Diff line change
Expand Up @@ -1195,21 +1195,24 @@ public struct Reference<Capture>: RegexComponent {
public init(_ captureType: Capture.Type = Capture.self)
public var regex: Regex<Capture>
}

extension Regex.Match {
public subscript<Capture>(_ reference: Reference<Capture>) -> Capture { get }
}
```

When capturing some regex with a reference specified, the reference will refer to the most recently captured content. The reference itself can be used as a regex to match the most recently captured content. This API is also designed to work with match result lookup, which will be introduced in a different pitch.
When capturing some regex with a reference specified, the reference will refer to the most recently captured content. The reference itself can be used as a regex to match the most recently captured content, or as a name to look up the result of matching.

```swift
let a = Reference()
let b = Reference()
let a = Reference(Substring.self)
let b = Reference(Substring.self)
let regex = Regex {
Capture("abc", as: a)
Capture("def", as: b)
a
Capture(b)
}

// To be introduced in a different pitch:
if let result = input.firstMatch(of: regex) {
print(result[a]) // => "abc"
print(result[b]) // => "def"
Expand Down