mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
* Move platform requirements to availability annotations Adding or raising the deployment platforms in the package manifest is a SemVer major breaking change as consumers must also add or raise their deployment platforms. This is a known limitation of SwiftPM. Unforunately this means that it's very difficult for non-leaf packages to adopt packages which declare their platforms in the manifest. Doing so puts the brakes on adoption and ecosystem growth. For 'core' packages like this one availability constraints should be expressed on declarations rather than in the manifest. This patch adds equivalent availability annotations to declarations across the package and removes platforms from the package manifest. * Address naming feedback
168 lines
5.0 KiB
Swift
168 lines
5.0 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Async Algorithms open source project
|
|
//
|
|
// Copyright (c) 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
@available(AsyncAlgorithms 1.0, *)
|
|
extension AsyncSequenceValidationDiagram {
|
|
struct Failure: Error, Equatable {}
|
|
|
|
enum ParseFailure: Error, CustomStringConvertible, SourceFailure {
|
|
case stepInGroup(String, String.Index, SourceLocation)
|
|
case nestedGroup(String, String.Index, SourceLocation)
|
|
case unbalancedNesting(String, String.Index, SourceLocation)
|
|
|
|
var location: SourceLocation {
|
|
switch self {
|
|
case .stepInGroup(_, _, let location): return location
|
|
case .nestedGroup(_, _, let location): return location
|
|
case .unbalancedNesting(_, _, let location): return location
|
|
}
|
|
}
|
|
|
|
var description: String {
|
|
switch self {
|
|
case .stepInGroup:
|
|
return "validation diagram step symbol in group"
|
|
case .nestedGroup:
|
|
return "validation diagram nested grouping"
|
|
case .unbalancedNesting:
|
|
return "validation diagram unbalanced grouping"
|
|
}
|
|
}
|
|
}
|
|
|
|
enum Event {
|
|
case value(String, String.Index)
|
|
case failure(Error, String.Index)
|
|
case finish(String.Index)
|
|
case delayNext(String.Index)
|
|
case cancel(String.Index)
|
|
|
|
var results: [Result<String?, Error>] {
|
|
switch self {
|
|
case .value(let value, _): return [.success(value)]
|
|
case .failure(let failure, _): return [.failure(failure)]
|
|
case .finish: return [.success(nil)]
|
|
case .delayNext: return []
|
|
case .cancel: return []
|
|
}
|
|
}
|
|
|
|
var index: String.Index {
|
|
switch self {
|
|
case .value(_, let index): return index
|
|
case .failure(_, let index): return index
|
|
case .finish(let index): return index
|
|
case .delayNext(let index): return index
|
|
case .cancel(let index): return index
|
|
}
|
|
}
|
|
|
|
static func parse<Theme: AsyncSequenceValidationTheme>(
|
|
_ dsl: String,
|
|
theme: Theme,
|
|
location: SourceLocation
|
|
) throws -> [(Clock.Instant, Event)] {
|
|
var emissions = [(Clock.Instant, Event)]()
|
|
var when = Clock.Instant(when: .steps(0))
|
|
var string: String?
|
|
var grouping = 0
|
|
|
|
for index in dsl.indices {
|
|
let ch = dsl[index]
|
|
switch theme.token(dsl[index], inValue: string != nil) {
|
|
case .step:
|
|
if string == nil {
|
|
guard grouping == 0 else {
|
|
throw ParseFailure.stepInGroup(dsl, index, location)
|
|
}
|
|
when = when.advanced(by: .steps(1))
|
|
} else {
|
|
string?.append(ch)
|
|
}
|
|
case .error:
|
|
if string == nil {
|
|
if grouping == 0 {
|
|
when = when.advanced(by: .steps(1))
|
|
}
|
|
emissions.append((when, .failure(Failure(), index)))
|
|
} else {
|
|
string?.append(ch)
|
|
}
|
|
case .finish:
|
|
if string == nil {
|
|
if grouping == 0 {
|
|
when = when.advanced(by: .steps(1))
|
|
}
|
|
emissions.append((when, .finish(index)))
|
|
} else {
|
|
string?.append(ch)
|
|
}
|
|
case .cancel:
|
|
if string == nil {
|
|
if grouping == 0 {
|
|
when = when.advanced(by: .steps(1))
|
|
}
|
|
emissions.append((when, .cancel(index)))
|
|
} else {
|
|
string?.append(ch)
|
|
}
|
|
case .delayNext:
|
|
if string == nil {
|
|
if grouping == 0 {
|
|
when = when.advanced(by: .steps(1))
|
|
}
|
|
emissions.append((when, .delayNext(index)))
|
|
} else {
|
|
string?.append(ch)
|
|
}
|
|
case .beginValue:
|
|
string = ""
|
|
case .endValue:
|
|
if let value = string {
|
|
string = nil
|
|
if grouping == 0 {
|
|
when = when.advanced(by: .steps(1))
|
|
}
|
|
emissions.append((when, .value(value, index)))
|
|
}
|
|
case .beginGroup:
|
|
guard grouping == 0 else {
|
|
throw ParseFailure.nestedGroup(dsl, index, location)
|
|
}
|
|
when = when.advanced(by: .steps(1))
|
|
grouping += 1
|
|
case .endGroup:
|
|
grouping -= 1
|
|
if grouping < 0 {
|
|
throw ParseFailure.unbalancedNesting(dsl, index, location)
|
|
}
|
|
case .skip:
|
|
string?.append(ch)
|
|
continue
|
|
case .value(let str):
|
|
if string == nil {
|
|
if grouping == 0 {
|
|
when = when.advanced(by: .steps(1))
|
|
}
|
|
emissions.append((when, .value(String(ch), index)))
|
|
} else {
|
|
string?.append(str)
|
|
}
|
|
}
|
|
}
|
|
if grouping != 0 {
|
|
throw ParseFailure.unbalancedNesting(dsl, dsl.endIndex, location)
|
|
}
|
|
return emissions
|
|
}
|
|
}
|
|
}
|