mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
* Remove the disable of availability and mark methods as available to clock/instant/duration * Rework availability to be more permissive for tests * Use hand coded values instead of defines (only available via Darwin versions of Dispatch) * Adjust additional test availability * Correct debounce test availability guard * Update build instructions to indicate Xcode requirements * Ensure the clock property of the validation diagram is only available for targets that can support it
165 lines
5.0 KiB
Swift
165 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
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 {
|
|
if grouping == 0 {
|
|
when = when.advanced(by: .steps(1))
|
|
} else {
|
|
throw ParseFailure.stepInGroup(dsl, index, location)
|
|
}
|
|
} 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:
|
|
if grouping == 0 {
|
|
when = when.advanced(by: .steps(1))
|
|
} else {
|
|
throw ParseFailure.nestedGroup(dsl, index, location)
|
|
}
|
|
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
|
|
}
|
|
}
|
|
}
|