File: Event.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (164 lines) | stat: -rw-r--r-- 5,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//===----------------------------------------------------------------------===//
//
// 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
    }
  }
}