Repository Public License
1// FlowEngine — the typed syntax tree of a workflow, the type system and the run-time values.2import Foundation34// MARK: - Types56/// The types of the Flow language. There are no user-defined types and no function types.7public indirect enum FlowType: Hashable, Sendable, CustomStringConvertible {8 case text, number, bool, url, duration9 /// An open tab the host handed back.10 case page11 case article, report, file12 case list(FlowType)13 /// The result of an action that produces nothing.14 case void15 /// One word out of a fixed set (`markdown`, `downloads`…); only exists as an action argument.16 case option17 /// Used after an error so one mistake is reported once.18 case unknown1920 public var description: String {21 switch self {22 case .text: return "text"23 case .number: return "number"24 case .bool: return "bool"25 case .url: return "url"26 case .duration: return "duration"27 case .page: return "page"28 case .article: return "article"29 case .report: return "report"30 case .file: return "file"31 case .list(let element): return "list<\(element)>"32 case .void: return "nothing"33 case .option: return "option"34 case .unknown: return "unknown"35 }36 }3738 /// Fields readable with `value.field`.39 public var members: [String: FlowType] {40 switch self {41 case .page: return ["url": .url, "title": .text]42 case .article:43 return ["title": .text, "text": .text, "url": .url, "byline": .text, "language": .text, "links": .list(.url)]44 case .report: return ["title": .text, "text": .text, "citations": .list(.url)]45 case .file: return ["name": .text, "path": .text]46 default: return [:]47 }48 }4950 var containsUnknown: Bool {51 switch self {52 case .unknown: return true53 case .list(let element): return element.containsUnknown54 default: return false55 }56 }5758 /// Whether a value of this type can be used where `expected` is required (`unknown` matches anything so a59 /// first error does not cascade).60 func isAssignable(to expected: FlowType) -> Bool {61 if self == expected || containsUnknown || expected.containsUnknown { return true }62 if case .list(let mine) = self, case .list(let theirs) = expected { return mine.isAssignable(to: theirs) }63 return false64 }65}6667// MARK: - Values6869public struct FlowPage: Hashable, Sendable, Codable {70 /// Opaque handle chosen by the host (a tab id). After a resume the handle may be stale: the host then71 /// reopens `url`, which the engine has re-checked against the allow-list.72 public var handle: String73 public var url: URL74 public var title: String75 public init(handle: String, url: URL, title: String = "") { self.handle = handle; self.url = url; self.title = title }76}7778public struct FlowArticle: Hashable, Sendable, Codable {79 public var url: URL80 public var title: String81 public var text: String82 public var byline: String83 public var language: String84 public var links: [URL]85 public init(url: URL, title: String, text: String, byline: String = "", language: String = "", links: [URL] = []) {86 self.url = url; self.title = title; self.text = text; self.byline = byline; self.language = language; self.links = links87 }88}8990public struct FlowReport: Hashable, Sendable, Codable {91 public var title: String92 public var text: String93 public var citations: [URL]94 public init(title: String, text: String, citations: [URL] = []) { self.title = title; self.text = text; self.citations = citations }95}9697public struct FlowFile: Hashable, Sendable, Codable {98 public var name: String99 public var path: String100 public init(name: String, path: String) { self.name = name; self.path = path }101}102103/// A run-time value. Values are plain data: nothing here can hold code.104public indirect enum FlowValue: Hashable, Sendable, Codable {105 case text(String)106 case number(Double)107 case bool(Bool)108 case url(URL)109 case duration(Double)110 case page(FlowPage)111 case article(FlowArticle)112 case report(FlowReport)113 case file(FlowFile)114 case list([FlowValue])115 case option(String)116 case nothing117118 /// The static type of the value. An empty list is `list<unknown>` (assignable to any list).119 public var type: FlowType {120 switch self {121 case .text: return .text122 case .number: return .number123 case .bool: return .bool124 case .url: return .url125 case .duration: return .duration126 case .page: return .page127 case .article: return .article128 case .report: return .report129 case .file: return .file130 case .list(let items): return .list(items.first?.type ?? .unknown)131 case .option: return .option132 case .nothing: return .void133 }134 }135136 /// Every URL the value carries that a host could navigate to or fetch: a url, a page's url, list elements.137 var navigableURLs: [URL] {138 switch self {139 case .url(let url): return [url]140 case .page(let page): return [page.url]141 case .list(let items): return items.flatMap(\.navigableURLs)142 default: return []143 }144 }145146 /// Text used by interpolation, `text()` and logs.147 public var displayText: String {148 switch self {149 case .text(let text): return text150 case .number(let number):151 return number == number.rounded() && abs(number) < 1e15 ? String(Int64(number)) : String(number)152 case .bool(let flag): return flag ? "true" : "false"153 case .url(let url): return url.absoluteString154 case .duration(let seconds): return "\(FlowValue.number(seconds).displayText)s"155 case .page(let page): return page.url.absoluteString156 case .article(let article): return article.title157 case .report(let report): return report.title158 case .file(let file): return file.name159 case .list(let items): return items.map(\.displayText).joined(separator: ", ")160 case .option(let word): return word161 case .nothing: return ""162 }163 }164}165166// MARK: - Syntax tree167168public struct FlowStringLiteral: Hashable, Sendable {169 public var parts: [FlowStringPart]170 public var range: FlowRange171172 /// The literal text when there is no interpolation.173 public var plainText: String? {174 var result = ""175 for part in parts {176 guard case .text(let text) = part else { return nil }177 result += text178 }179 return result180 }181}182183public enum FlowBinaryOperator: String, Sendable, Hashable {184 case equal = "==", notEqual = "!=", less = "<", lessOrEqual = "<=", greater = ">", greaterOrEqual = ">="185 case and, or186}187188public indirect enum FlowExpression: Hashable, Sendable {189 case string(FlowStringLiteral)190 case number(Double, FlowRange)191 case bool(Bool, FlowRange)192 case duration(Double, FlowRange)193 case identifier(String, FlowRange)194 case member(FlowExpression, String, FlowRange)195 case call(String, [FlowExpression], FlowRange)196 case list([FlowExpression], FlowRange)197 case not(FlowExpression, FlowRange)198 case binary(FlowBinaryOperator, FlowExpression, FlowExpression, FlowRange)199 /// What the parser puts where it could not read an expression.200 case invalid(FlowRange)201202 public var range: FlowRange {203 switch self {204 case .string(let literal): return literal.range205 case .number(_, let range), .bool(_, let range), .duration(_, let range), .identifier(_, let range),206 .member(_, _, let range), .call(_, _, let range), .list(_, let range), .not(_, let range),207 .binary(_, _, _, let range), .invalid(let range):208 return range209 }210 }211}212213public struct FlowParameterDeclaration: Hashable, Sendable {214 public var name: String215 public var nameRange: FlowRange216 public var typeName: String217 public var typeRange: FlowRange218 public var defaultValue: FlowExpression?219 public var comments: [String]220 public var range: FlowRange221}222223public struct FlowDomainLiteral: Hashable, Sendable {224 public var domain: String225 public var range: FlowRange226}227228public struct FlowLimitDeclaration: Hashable, Sendable {229 public enum Kind: String, Sendable, CaseIterable { case steps, duration, timeout }230 public var kind: Kind231 /// A count for `steps`, seconds for `duration` and `timeout`.232 public var value: Double233 public var range: FlowRange234}235236public struct FlowArgument: Hashable, Sendable {237 /// `nil` for the positional argument.238 public var label: String?239 public var labelRange: FlowRange?240 public var value: FlowExpression241}242243public struct FlowActionStatement: Hashable, Sendable {244 public var name: String245 public var nameRange: FlowRange246 public var arguments: [FlowArgument]247 public var binding: String?248 public var bindingRange: FlowRange?249 public var range: FlowRange250}251252public struct FlowLetStatement: Hashable, Sendable {253 public var name: String254 public var nameRange: FlowRange255 public var value: FlowExpression256 public var range: FlowRange257}258259public struct FlowForStatement: Hashable, Sendable {260 public var variable: String261 public var variableRange: FlowRange262 public var sequence: FlowExpression263 /// Static bound on the number of iterations; `nil` only in a program that failed to parse.264 public var limit: Int?265 public var limitRange: FlowRange?266 /// Iterations run concurrently, at most this many at a time.267 public var parallelWidth: Int?268 public var body: FlowBlock269 public var range: FlowRange270}271272public struct FlowParallelStatement: Hashable, Sendable {273 public var width: Int?274 public var widthRange: FlowRange?275 /// Each statement of the block is one branch.276 public var body: FlowBlock277 public var range: FlowRange278}279280public struct FlowIfStatement: Hashable, Sendable {281 public var condition: FlowExpression282 public var thenBlock: FlowBlock283 public var elseBlock: FlowBlock?284 public var range: FlowRange285}286287public struct FlowCollectStatement: Hashable, Sendable {288 public var value: FlowExpression289 public var target: String290 public var targetRange: FlowRange291 public var range: FlowRange292}293294public indirect enum FlowStatementKind: Hashable, Sendable {295 case action(FlowActionStatement)296 case let_(FlowLetStatement)297 case forEach(FlowForStatement)298 case parallel(FlowParallelStatement)299 case conditional(FlowIfStatement)300 case collect(FlowCollectStatement)301}302303public struct FlowStatement: Hashable, Sendable {304 public var kind: FlowStatementKind305 /// Comment lines written above the statement (kept by the formatter).306 public var comments: [String]307308 public var range: FlowRange {309 switch kind {310 case .action(let statement): return statement.range311 case .let_(let statement): return statement.range312 case .forEach(let statement): return statement.range313 case .parallel(let statement): return statement.range314 case .conditional(let statement): return statement.range315 case .collect(let statement): return statement.range316 }317 }318}319320public struct FlowBlock: Hashable, Sendable {321 public var statements: [FlowStatement]322 /// Comments after the last statement of the block.323 public var trailingComments: [String]324 public var range: FlowRange325}326327public struct FlowProgram: Hashable, Sendable {328 public var name: String329 public var nameRange: FlowRange330 public var version: Int331 public var headerComments: [String]332 public var parameters: [FlowParameterDeclaration]333 public var domains: [FlowDomainLiteral]334 public var domainComments: [String]335 public var limits: [FlowLimitDeclaration]336 public var limitComments: [String]337 /// Name of the space the workflow opens its tabs in (`space "Research" { … }`), if any.338 public var spaceName: String?339 public var bodyComments: [String]340 public var body: FlowBlock341}342