Skip to content

Conversation

@rxwei
Copy link
Contributor

@rxwei rxwei commented Jan 20, 2022

tryCapture(_:) works like capture(_:) but accepts an optional or throwing capture transform instead. When the transform fails (returns nil or throws), it will cause matching to fail. This works well with failable conversion initializers such as Int.init?(_:), Unicode.Scalar.init?(_:), etc.


Example:

With tryCapture, the following example avoids unnecessary levels of optionals and double optionals.

line.match { OneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:)) Optionally { ".." OneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:)) } OneOrMore(.whitespace) ";" OneOrMore(.whitespace) OneOrMore(.word).tryCapture(Unicode.GraphemeBreakProperty.init) Repeat(.any) } // Regex<(Substring, Unicode.Scalar, Unicode.Scalar?, Unicode.GraphemeBreakProperty)>
@rxwei rxwei changed the title Introduce tryCapture(_:). Introduce tryCapture(_:). Jan 20, 2022
@rxwei rxwei requested a review from milseman January 20, 2022 08:00
@rxwei
Copy link
Contributor Author

rxwei commented Jan 20, 2022

Note: This PR depends on #120 and #119. Please review 2148ed5 for changes specific to tryCapture(_:).

@milseman
Copy link
Member

Should we have a throw abort? That's the pattern we're planning to follow elsewhere and Perl/PCRE's more general-form callouts have that capability.

Copy link
Member

@milseman milseman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM!

@milseman
Copy link
Member

If this passes your local testing, I'm ok merging this.

@shahmishal any idea why Linux CI can't get us a machine? We run in around 1min total time, so we're not going to clog up any pipelines.

@rxwei
Copy link
Contributor Author

rxwei commented Jan 20, 2022

I haven't triggered tests here, as #119 needed to be merged first.

`tryCapture(_:)` works like `capture(_:)` but accepts an optional or throwing capture transform instead. When the transform fails (returns `nil` or throws), it will cause matching to fail. This works well with failable conversion initializers such as `Int.init?(_:)`, `Unicode.Scalar.init?(_:)`, etc. ---- Example: With `tryCapture`, the following example avoids unnecessary levels of optionals and double optionals. ```swift line.match { OneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:)) Optionally { ".." OneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:)) } OneOrMore(.whitespace) ";" OneOrMore(.whitespace) OneOrMore(.word).tryCapture(Unicode.GraphemeBreakProperty.init) Repeat(.any) } // Regex<(Substring, Unicode.Scalar, Unicode.Scalar?, Unicode.GraphemeBreakProperty)> ```
@rxwei
Copy link
Contributor Author

rxwei commented Jan 20, 2022

@swift-ci please test Linux

@rxwei rxwei merged commit b94c9d5 into swiftlang:main Jan 20, 2022
@rxwei rxwei deleted the try-capture branch January 20, 2022 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants