-
Notifications
You must be signed in to change notification settings - Fork 50
Replace DynamicCaptures
with AnyRegexOutput
.
#222
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
Sources/_StringProcessing/RegexDSL/AnyRegexOutput.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import _MatchingEngine | ||
|
||
extension Regex where Output == AnyRegexOutput { | ||
public init(_ pattern: String) throws { | ||
self.init(ast: try parse(pattern, .traditional)) | ||
} | ||
} | ||
|
||
extension Regex.Match where Output == AnyRegexOutput { | ||
// Ensures `.0` always refers to the whole match. | ||
public subscript( | ||
dynamicMember keyPath: KeyPath<(Substring, _doNotUse: ()), Substring> | ||
) -> Substring { | ||
input[range] | ||
} | ||
} | ||
|
||
public struct AnyRegexOutput { | ||
let input: String | ||
fileprivate let _elements: [ElementRepresentation] | ||
|
||
/// The underlying representation of the element of a type-erased regex | ||
/// output. | ||
fileprivate struct ElementRepresentation { | ||
/// The depth of `Optioals`s wrapping the underlying value. For example, | ||
/// `Substring` has optional depth `0`, and `Int??` has optional depth `2`. | ||
let optionalDepth: Int | ||
/// The bounds of the output element. | ||
let bounds: Range<String.Index>? | ||
} | ||
} | ||
|
||
extension AnyRegexOutput { | ||
/// Creates a type-erased regex output from an existing output. | ||
/// | ||
/// Use this initializer to fit a regex with strongly typed captures into the | ||
/// use site of a dynamic regex, i.e. one that was created from a string. | ||
public init<Output>(_ match: Regex<Output>.Match) { | ||
// Note: We use type equality instead of `match.output as? ...` to prevent | ||
// unexpected optional flattening. | ||
if Output.self == AnyRegexOutput.self { | ||
self = match.output as! AnyRegexOutput | ||
return | ||
} | ||
fatalError("FIXME: Not implemented") | ||
// self.init(input: match.input, _elements: <elements of output tuple>) | ||
} | ||
} | ||
|
||
extension AnyRegexOutput { | ||
internal init<C: Collection>( | ||
input: String, elements: C | ||
) where C.Element == StructuredCapture { | ||
self.init(input: input, _elements: elements.map(ElementRepresentation.init)) | ||
} | ||
} | ||
|
||
extension AnyRegexOutput.ElementRepresentation { | ||
init(_ element: StructuredCapture) { | ||
self.init( | ||
optionalDepth: element.optionalCount, | ||
bounds: element.storedCapture.flatMap(\.range)) | ||
} | ||
|
||
func value(forInput input: String) -> Any { | ||
// Ok for now because `existentialMatchComponent` | ||
// wont slice the input if there's no range to slice with | ||
// | ||
// FIXME: This is ugly :-/ | ||
let input = bounds.map { input[$0] } ?? "" | ||
|
||
return constructExistentialOutputComponent( | ||
from: input, | ||
in: bounds, | ||
value: nil, | ||
optionalCount: optionalDepth) | ||
} | ||
} | ||
|
||
extension AnyRegexOutput: RandomAccessCollection { | ||
public struct Element { | ||
fileprivate let representation: ElementRepresentation | ||
let input: String | ||
|
||
public var range: Range<String.Index>? { | ||
representation.bounds | ||
} | ||
|
||
public var substring: Substring? { | ||
range.map { input[$0] } | ||
} | ||
} | ||
|
||
public var startIndex: Int { | ||
_elements.startIndex | ||
} | ||
|
||
public var endIndex: Int { | ||
_elements.endIndex | ||
} | ||
|
||
public var count: Int { | ||
_elements.count | ||
} | ||
|
||
public func index(after i: Int) -> Int { | ||
_elements.index(after: i) | ||
} | ||
|
||
public func index(before i: Int) -> Int { | ||
_elements.index(before: i) | ||
} | ||
|
||
public subscript(position: Int) -> Element { | ||
.init(representation: _elements[position], input: input) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR, did we decide on an optional nesting story?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There’s no non-factorial solution to optional flattening this year, unfortunately. So we’ll have nested optionals.