Skip to content

[5.7-04182022] Introduce One #407

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
May 12, 2022

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented May 12, 2022

Cherry-pick of #403


One is a lightweight component that allows the use of the leading dot syntax to reference RegexComponent static members such as character classes as a non-first expression in a regex builder block.


Before:

Regex {
    .digit // works today but brittle; inserting anything above this line will break this

    OneOrMore(.whitespace)

    .word // ❌ error: 'OneOrMore' has no member named 'word' (because this is parsed as a member reference on the preceeding expression)
}

After:

Regex {
    One(.digit)              // recommended even though `.digit` works today
    OneOrMore(.whitespace)
    One(.word)
} // ✅

In a follow-up patch, we will propose adding an additional protocol inheriting from RegexComponent that will ban the use of the leading dot syntax even on the first line of Regex { ... }, as this will enforce the recommended style (use of One), and prevent surprises when the user inserts a pattern above the leading dot line.

One is a lightweight component that allows the use of the leading dot syntax to reference `RegexComponent` static members such as character classes as a non-first expression in a regex builder block.

---

Before:

```swift
Regex {
    .digit // works today but brittle; inserting anything above this line will break this

    OneOrMore(.whitespace)

    .word // ❌ error: 'OneOrMore' has no member named 'word' (because this is parsed as a member reference on the preceeding expression)
}
```

After:

```swift
Regex {
    One(.digit)              // recommended even though `.digit` works today
    OneOrMore(.whitespace)
    One(.word)
} // ✅
```

In a follow-up patch, we will propose adding an additional protocol inheriting from `RegexComponent` that will ban the use of the leading dot syntax even on the first line of `Regex { ... }`, as this will enforce the recommended style (use of `One`), and prevent surprises when the user inserts a pattern above the leading dot line.
@stephentyrone stephentyrone self-requested a review May 12, 2022 18:44
@rxwei rxwei merged commit 753931e into swiftlang:swift/release/5.7-04182022 May 12, 2022
natecook1000 added a commit to natecook1000/swift-experimental-string-processing that referenced this pull request May 18, 2022
Character class ranges don't work well with multi-scalar inputs,
in either the range or the matched character. This change limits
range endpoints to single-scalar characters and matches only
characters that are themselves a single scalar.

Fixes issue swiftlang#407, which now displays this behavior:

```
try /[1-2]/.wholeMatch(in: "1️⃣")     // nil
try /[12]/.wholeMatch(in: "1️⃣")      // nil
try /(?U)[\d]/.wholeMatch(in: "1️⃣")  // nil
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants