Skip to content

Option APIs for RegexProtocol #171

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
Feb 21, 2022
Merged

Conversation

natecook1000
Copy link
Member

@natecook1000 natecook1000 commented Feb 18, 2022

This adds an API to any conforming type that lets the user control whether a match uses case sensitive comparison from within a DSL declaration. Applying modifiers like this has the opposite precedence as a regex literal, where the nearest option setter has precedence.

For example, these two regex declarations are equivalent:

let regexDSL = Regex {
    oneOrMore {
        "a"
    }
    .caseSensitive(true)
    .caseSensitive(false)
}

let regexLiteral = #/(?-i)(?i)a+/#

Note: This requires #168.
Note 2: More options TK once we're okay with this approach.

@kylemacomber
Copy link

kylemacomber commented Feb 18, 2022

In SwiftUI, the tighter binding modifier wins. I think it'd be good to follow the same convention so when people more between result builder DSLs they don't have to turn their brain inside-out.

Tighter binding winning is also consistent with regex literals and variable binding in general.

} else {
return input[low].lowercased() == c.lowercased()
? input.index(after: low)
: nil
Copy link
Member

Choose a reason for hiding this comment

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

This code/logic is repeated a lot. Any chance to refactor it a little?

@@ -198,7 +202,22 @@ extension DSLTree.Atom {

extension DSLTree {
struct Options {
// TBD
enum Kind {
Copy link
Member

Choose a reason for hiding this comment

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

What's the difference between this an MatchingOptions?

Copy link
Member Author

Choose a reason for hiding this comment

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

I flattened this out — with the SwiftUI-style options, we don't need to coalesce, so the existing MatchingOptionsSequence works fine.

("cafe", true),
("CaFe", true),
("EfAc", true))
}
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the tests! What about multi-scalar expansions, like the German SS?

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe we need case folding to do all this properly, don't we? I'll follow up with some tests showing the holes.

@natecook1000
Copy link
Member Author

@swift-ci Please test Linux platform

This adds an API to any conforming type that lets the user control
whether a match uses case sensitive comparison from within a DSL
declaration. Adding successive modifiers is equivalent to nesting
an expression in a new Regex declaration.

For example, these two regex declarations are equivalent:

```
let regexDSL = Regex {
    oneOrMore {
        "a"
    }
    .caseSensitive(false)
    .caseSensitive(true)
}

let regexNested = Regex {
    Regex {
        oneOrMore {
            "a"
        }.caseSensitive(false)
    }.caseSensitive(true)
}
```
@natecook1000
Copy link
Member Author

@swift-ci Please test Linux platform

@@ -110,6 +110,16 @@ extension AST {
}
}

extension AST.MatchingOptionSequence {
Copy link
Member

Choose a reason for hiding this comment

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

FWIW, when we pay off some technical debt, DSLTree will have fewer and fewer AST constructs in it.

In general, we have 3 things, the AST's representation, the API's representation, and the model type.

Literal/AST lowers-to model
API lowers-to model

It's possible in some situations for all 3 to be the same type, or for the model to also be made public in API.

@natecook1000 natecook1000 merged commit 5e2b77c into swiftlang:main Feb 21, 2022
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.

4 participants