Skip to content

'Regex<Captures>' -> 'Regex<Match>' #68

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
Dec 31, 2021
Merged

'Regex<Captures>' -> 'Regex<Match>' #68

merged 1 commit into from
Dec 31, 2021

Conversation

rxwei
Copy link
Contributor

@rxwei rxwei commented Dec 13, 2021

Changes Regex and result builder prototype to use Match as the generic parameter to make it consistent with the Strongly Typed Regex Captures pitch.

Introduces Tuple<n> structs in order to be able to express constraints on capture types (i.e. Match dropped first) while being able to filter out empty captures in concatenation. Tuple<n> is also needed to implement a prototype of the proposed matching semantics.

@frozen @dynamicMemberLookup
public struct Tuple{n}<_0, _1, ...> {
  public typealias Tuple = (_0, _1, ...)
  public var tuple: Tuple
  public init(_ tuple: Tuple)
  public init(_ _0: _0, _ _1: _1, ...)
  public subscript<T>(dynamicMember keyPath: WritableKeyPath<Tuple, T>) -> T { get _modify }
}

As coercion into Tuple<n> can no longer use runtime magic like native tuples do, we incorporate child capture type information into RECode's captureNil and captureArray instructions so that we will always get a concrete type when forming a nil or an empty array capture. The resulting existential tuple capture can then be opened and bitcast to a Tuple<n>.

@rxwei
Copy link
Contributor Author

rxwei commented Dec 13, 2021

@kylemacomber This paves the way for Submatch (or Unbound, whatever it's called) :)

@rxwei
Copy link
Contributor Author

rxwei commented Dec 14, 2021

@swift-ci please test linux

rxwei added a commit to rxwei/swift that referenced this pull request Dec 17, 2021
…d regex.

- Checkout apple/swift-experimental-string-processing using a tag.
- Build `_MatchingEngine` as part of libswift (`ExperimentalRegex`) using sources from the package.
- Parse regex literals using the parser from `_MatchingEngine`.
- Build both `_MatchingEngine` and `_StringProcessing` as part of core libs using sources from the package.
- Use `Regex<DynamicCaptures>` as the default regex type until we finalize swiftlang/swift-experimental-string-processing#68.
hamishknight pushed a commit to hamishknight/swift that referenced this pull request Dec 17, 2021
…d regex.

- Checkout apple/swift-experimental-string-processing using a tag.
- Build `_MatchingEngine` as part of libswift (`ExperimentalRegex`) using sources from the package.
- Parse regex literals using the parser from `_MatchingEngine`.
- Build both `_MatchingEngine` and `_StringProcessing` as part of core libs using sources from the package.
- Use `Regex<DynamicCaptures>` as the default regex type until we finalize swiftlang/swift-experimental-string-processing#68.
@rxwei rxwei force-pushed the match branch 2 times, most recently from 526b8ca to 980fbb0 Compare December 31, 2021 02:08
@rxwei rxwei changed the title [WIP] Regex<Captures> -> Regex<Match> Regex<Captures> -> Regex<Match> Dec 31, 2021
@rxwei rxwei changed the title Regex<Captures> -> Regex<Match> 'Regex<Captures>' -> 'Regex<Match>' Dec 31, 2021
@rxwei rxwei marked this pull request as ready for review December 31, 2021 02:09
@rxwei
Copy link
Contributor Author

rxwei commented Dec 31, 2021

@swift-ci please test linux

@rxwei rxwei force-pushed the match branch 3 times, most recently from 6faddbf to a5109b8 Compare December 31, 2021 02:37
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.

In general, any switch to a nominal type makes sense to me. Out of curiosity, what necessitated this change? Also, do we need those generic parameters?

}
extension Substring: MatchProtocol {
public typealias Capture = EmptyCapture
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't recall whether modern computing tolerates no newline at the end of a file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. It's partly because I've been trying to use Windows to develop these days, trying Swift for Windows myself.

@rxwei
Copy link
Contributor Author

rxwei commented Dec 31, 2021

buildBlocks need to determine whether a component has no captures. A component has no captures when every capture conforms to EmptyCaptureProtocol. For example, a OneOrMore quantifier's Match is a tuple of its whole match and an array of its capture. [Component.Match.Capture] conforms to EmptyCaptureProtocol when Component.Match.Capture (a Tuple{n}) conforms.

public struct OneOrMore<Component: RegexProtocol>: RegexProtocol {
  public typealias Match = Tuple2<Substring, [Component.Match.Capture]>
extension Tuple5: EmptyCaptureProtocol where _1: EmptyCaptureProtocol, _2: EmptyCaptureProtocol, _3: EmptyCaptureProtocol, _4: EmptyCaptureProtocol {}

@rxwei
Copy link
Contributor Author

rxwei commented Dec 31, 2021

@swift-ci please test Linux

@rxwei
Copy link
Contributor Author

rxwei commented Dec 31, 2021

This apparently affects regex literal type inference in the compiler, so I'll put up a PR on the compiler side to test against this.

@rxwei
Copy link
Contributor Author

rxwei commented Dec 31, 2021

@swift-ci please test linux

Changes `Regex` and result builder prototype to use `Match` as the generic parameter to make it consistent with the [Strongly Typed Regex Captures](https://forums.swift.org/t/pitch-strongly-typed-regex-captures/53391) pitch.

Introduces `Tuple<n>` structs in order to be able to express constraints on capture types (i.e. `Match` dropped first) while being able to filter out empty captures in concatenation. `Tuple<n>` is also needed to implement a prototype of the [proposed matching semantics](swiftlang#64).

As coercion into `Tuple<n>` can no longer use runtime magic like native tuples do, we incorporate child capture type information into RECode's `captureNil` and `captureArray` instructions so that we will always get a concrete type when forming a nil or an empty array capture. The resulting existential tuple capture can then be opened and bitcast to a `Tuple<n>`.
@rxwei
Copy link
Contributor Author

rxwei commented Dec 31, 2021

@swift-ci please test Linux

@rxwei rxwei merged commit cc7a433 into swiftlang:main Dec 31, 2021
@rxwei rxwei deleted the match branch January 2, 2022 04:56
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