From 0db0ce6171b969a5aae3f971ccbadc84bc811199 Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Thu, 17 Mar 2022 00:08:36 -0700 Subject: [PATCH] Add `Regex.Match.subscript(_: Reference)` to DSL pitch. Given that the `Regex` type pitch is going to include the `Regex.Match` type, we add the extension that contains `subscript(_: Reference)` to the DSL pitch. Also fix the `Reference` code example. --- Documentation/Evolution/RegexBuilderDSL.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Documentation/Evolution/RegexBuilderDSL.md b/Documentation/Evolution/RegexBuilderDSL.md index d82a0790d..f0a477644 100644 --- a/Documentation/Evolution/RegexBuilderDSL.md +++ b/Documentation/Evolution/RegexBuilderDSL.md @@ -1195,13 +1195,17 @@ public struct Reference: RegexComponent { public init(_ captureType: Capture.Type = Capture.self) public var regex: Regex } + +extension Regex.Match { + public subscript(_ reference: Reference) -> 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) @@ -1209,7 +1213,6 @@ let regex = Regex { Capture(b) } -// To be introduced in a different pitch: if let result = input.firstMatch(of: regex) { print(result[a]) // => "abc" print(result[b]) // => "def"