Skip to content

DSL free functions -> types #203

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 11, 2022
Merged

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Mar 10, 2022

Replace free functions such as oneOrMore with types such as OneOrMore. This hides overloads of free functions as initializers within those types. It also makes the DSL consistent with SwiftUI.

  • oneOrMore -> OneOrMore
  • zeroOrMore -> ZeroOrMore
  • optionally -> Optionally
  • repeating -> Repeat
  • choiceOf -> ChoiceOf
  • capture -> Capture
  • tryCapture -> TryCapture

Note: The reason we didn't realize this was possible (e.g. in #126) was because we were narrowly focused on including the subpattern type in the quantifier/combinator's generic parameter, i.e. OneOrMore<Component: RegexComponent>, which made it impossible to deduce each type's typealias Match from Component. Now we have an unconstrained generic parameter (e.g. OneOrMore<Match>) which gives us the full flexibility to hide Match deduction rules in initializers' type signatures.

Result: All non-operator top-level functions have been eliminated.

@rxwei
Copy link
Contributor Author

rxwei commented Mar 10, 2022

@swift-ci please test

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.

Just when I was starting to appreciate the functions...

LGTM!

// A convenience protocol for builtin regex components that are initialized with
// a `DSLTree` node.
internal protocol _BuiltinRegexComponent: RegexComponent {
var regex: Regex<Match> { get }
Copy link
Member

Choose a reason for hiding this comment

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

BTW, do we have anything in-tree that uses this as an instance instead of a static var?

Copy link
Member

Choose a reason for hiding this comment

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

Also, I don't think we need to redeclare this, do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think custom regex types can use instance members to allow config options. We don’t have any examples in tree yet

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, will remove redeclaration.

// a `DSLTree` node.
internal protocol _BuiltinRegexComponent: RegexComponent {
var regex: Regex<Match> { get }
init(_ regex: Regex<Match>)
Copy link
Member

Choose a reason for hiding this comment

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

Do we want this defaulted with self.regex = regex? Or even just have this in an extension?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Protocol initializers can only do delegate initialization, so I don’t think we can. There’s no way to express a formal guarantee that a protocol property will be a stored property.

@rxwei
Copy link
Contributor Author

rxwei commented Mar 11, 2022

Just when I was starting to appreciate the functions...

I had the same inertia myself…

@rxwei rxwei force-pushed the dsl-functions-to-types branch from ee31d51 to a109f42 Compare March 11, 2022 07:57
Replace free functions such as `oneOrMore` with types such as `OneOrMore`. This hides overloads of free functions as initializers within those types. It also makes the DSL consistent with SwiftUI.

- `oneOrMore` -> `OneOrMore`
- `zeroOrMore` -> `ZeroOrMore`
- `optionally` -> `Optionally`
- `repeating` -> `Repeat`
- `choiceOf` -> `ChoiceOf`
- `capture` -> `Capture`
- `tryCapture` -> `TryCapture`

Note: The reason we didn't realize this was possible (e.g. in swiftlang#126) was because we were narrowly focused on including the subpattern type in the quantifier/combinator's generic parameter, i.e. `OneOrMore<Component: RegexComponent>`, which made it impossible to deduce each type's `typealias Match` from `Component`. Now we have an unconstrained generic parameter (e.g. `OneOrMore<Match>`) which gives us the full flexibility to hide `Match` deduction rules in initializers' type signatures.
@rxwei rxwei force-pushed the dsl-functions-to-types branch from a109f42 to f2ed846 Compare March 11, 2022 08:21
@rxwei
Copy link
Contributor Author

rxwei commented Mar 11, 2022

@swift-ci please test

@rxwei rxwei merged commit 85c7d90 into swiftlang:main Mar 11, 2022
@rxwei rxwei deleted the dsl-functions-to-types branch March 11, 2022 08:35
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