bynk_syntax/ast.rs
1//! Abstract syntax tree types for Bynk v0 (spec §9.2).
2
3use crate::span::Span;
4
5/// An identifier with its source span.
6#[derive(Debug, Clone)]
7pub struct Ident {
8 pub name: String,
9 pub span: Span,
10}
11
12/// Comment trivia attached to a declaration or statement (v1.1 LSP spec
13/// §3.5). The parser collects line comments from the token stream and
14/// attaches them to nearby AST nodes so the formatter can re-emit them.
15///
16/// - `leading` holds comments that appear immediately above the node,
17/// ordered top-to-bottom. Each entry is the body of one `--` line
18/// (the text after the marker, with its original inline whitespace
19/// preserved).
20/// - `trailing` holds a single comment that appears on the same source
21/// line as the node's final token (e.g. `expr -- note`).
22#[derive(Debug, Clone, Default)]
23pub struct Trivia {
24 pub leading: Vec<String>,
25 pub trailing: Option<String>,
26}
27
28impl Trivia {
29 pub fn is_empty(&self) -> bool {
30 self.leading.is_empty() && self.trailing.is_none()
31 }
32}
33
34/// A whole parsed commons source file.
35///
36/// In v0.3 a commons may be split across multiple files in a directory; the
37/// resolver merges them into one logical commons. Each parsed AST instance
38/// represents the contribution from a single source file.
39#[derive(Debug, Clone)]
40pub struct Commons {
41 pub name: QualifiedName,
42 pub items: Vec<CommonsItem>,
43 /// `uses` clauses declared in this file.
44 pub uses: Vec<UsesDecl>,
45 /// Optional documentation block attached to the commons declaration.
46 pub documentation: Option<String>,
47 /// Surface form of the file: brace-delimited body or headerless fragment.
48 pub form: CommonsForm,
49 pub span: Span,
50 /// Trivia attached to the commons declaration itself — leading comments
51 /// before the `commons` keyword and a trailing comment after the header
52 /// or closing brace.
53 pub trivia: Trivia,
54 /// Comments appearing after the last item but before the file ends
55 /// (or the closing brace, for brace form). One entry per `--` line.
56 pub trailing_comments: Vec<String>,
57}
58
59/// The two surface forms in which a commons body may be parsed (v0.3 §3.1).
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum CommonsForm {
62 /// `commons name { ... }`
63 Brace,
64 /// `commons name` followed by top-level declarations to EOF.
65 Fragment,
66}
67
68/// A `uses other.commons` declaration (v0.3 §3.3).
69#[derive(Debug, Clone)]
70pub struct UsesDecl {
71 pub target: QualifiedName,
72 pub span: Span,
73 pub trivia: Trivia,
74}
75
76/// A whole parsed context source file (v0.4 §3.1).
77///
78/// Contexts are the architectural-layer declaration kind. Like commons, a
79/// context may be split across multiple files in a directory.
80#[derive(Debug, Clone)]
81pub struct Context {
82 pub name: QualifiedName,
83 pub items: Vec<CommonsItem>,
84 /// `uses` clauses declared in this file.
85 pub uses: Vec<UsesDecl>,
86 /// `consumes` clauses declared in this file.
87 pub consumes: Vec<ConsumesDecl>,
88 /// `exports` clauses declared in this file.
89 pub exports: Vec<ExportsDecl>,
90 /// Optional documentation block attached to the context declaration.
91 pub documentation: Option<String>,
92 /// Surface form of the file: brace-delimited body or headerless fragment.
93 pub form: CommonsForm,
94 pub span: Span,
95 /// Trivia attached to the context declaration itself — leading comments
96 /// before the `context` keyword.
97 pub trivia: Trivia,
98 /// Comments appearing after the last item but before the file ends
99 /// (or the closing brace, for brace form). One entry per `--` line.
100 pub trailing_comments: Vec<String>,
101}
102
103/// A `consumes other.context` declaration (v0.4 §3.2). May optionally carry
104/// an alias introduced by `consumes other.context as Alias` (v0.6 §3.1).
105#[derive(Debug, Clone)]
106pub struct ConsumesDecl {
107 pub target: QualifiedName,
108 pub alias: Option<Ident>,
109 /// v0.17: `consumes U { Cap, … }` — selected capabilities flattened into
110 /// the consumer's local capability namespace under their bare names (§3.3).
111 /// `None` for the whole-unit forms; `Some` (possibly empty) for the braced
112 /// form. Mutually exclusive with `alias`.
113 pub selected: Option<Vec<Ident>>,
114 pub span: Span,
115 pub trivia: Trivia,
116}
117
118/// An `exports visibility { names }` clause (v0.4 §3.3) or, v0.15, an
119/// `exports capability { names }` clause.
120#[derive(Debug, Clone)]
121pub struct ExportsDecl {
122 pub kind: ExportKind,
123 pub names: Vec<Ident>,
124 pub span: Span,
125 pub trivia: Trivia,
126}
127
128/// What an `exports` clause exposes: types (with a visibility) or, v0.15,
129/// capabilities offered for cross-context consumption.
130#[derive(Debug, Clone, Copy, PartialEq, Eq)]
131pub enum ExportKind {
132 /// `exports opaque { ... }` / `exports transparent { ... }` — type exports.
133 Type(Visibility),
134 /// `exports capability { ... }` — capabilities offered to consumers (v0.15).
135 Capability,
136}
137
138/// Visibility level for an exports clause (v0.4 §3.3).
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140pub enum Visibility {
141 /// Token-only outside the context: hold, pass, compare; no inspect, no construct.
142 Opaque,
143 /// Readable shape outside the context: inspect fields, match variants; no construct.
144 Transparent,
145}
146
147/// An `adapter qualified.name { … }` declaration (v0.17 §3.1). An adapter
148/// co-locates a capability contract with a non-Bynk binding: it may declare
149/// capabilities, the boundary types they reference, inline pure helper
150/// `type`/`fn` (and `uses`), external (bodiless) providers, `exports
151/// capability`, and exactly one `binding` clause. It may *not* declare
152/// services, agents, or bodied providers. Like commons/contexts it may be
153/// split across files in a directory.
154#[derive(Debug, Clone)]
155pub struct AdapterDecl {
156 pub name: QualifiedName,
157 pub items: Vec<CommonsItem>,
158 /// `uses` clauses declared in this file (pure-vocabulary mixin; allowed
159 /// because helpers cannot pierce containment — spec [DECISION B]).
160 pub uses: Vec<UsesDecl>,
161 /// `exports capability { … }` clauses (adapters export capabilities and
162 /// boundary types, never services).
163 pub exports: Vec<ExportsDecl>,
164 /// v0.18: `consumes U { Cap, … }` clauses — adapter-to-adapter capability
165 /// dependencies (spec §4.5, \[N\]). Braced form only; adapter targets only
166 /// (both enforced semantically, not in the parser).
167 pub consumes: Vec<ConsumesDecl>,
168 /// The `binding "<module>" requires { … }` clause, if present. Required
169 /// when the adapter declares any external provider (`bynk.adapter.no_binding`).
170 pub binding: Option<BindingDecl>,
171 pub documentation: Option<String>,
172 pub form: CommonsForm,
173 pub span: Span,
174 pub trivia: Trivia,
175 pub trailing_comments: Vec<String>,
176}
177
178/// A `binding "<module>" requires { "pkg": "range", … }` clause inside an
179/// adapter (v0.17 §3.5). `module` is the TypeScript module supplying the
180/// adapter's external provider symbols, resolved relative to the adapter's
181/// source file. `requires` declares npm dependencies folded into the
182/// generated `package.json`.
183#[derive(Debug, Clone)]
184pub struct BindingDecl {
185 /// The module path as written (the string-literal contents, no quotes).
186 pub module: String,
187 pub module_span: Span,
188 pub requires: Vec<RequiresDep>,
189 pub span: Span,
190 pub trivia: Trivia,
191}
192
193/// One `"pkg": "range"` entry in a binding's `requires { … }` map.
194#[derive(Debug, Clone)]
195pub struct RequiresDep {
196 pub package: String,
197 pub range: String,
198 pub span: Span,
199}
200
201/// Either a commons or a context — the two declaration kinds at the file
202/// level (v0.4 §3.1). v0.7 adds the test declaration kind; v0.17 the adapter.
203#[derive(Debug, Clone)]
204pub enum SourceUnit {
205 Commons(Commons),
206 Context(Context),
207 Suite(SuiteDecl),
208 /// v0.17: an `adapter` unit — the host boundary (capability contract +
209 /// external binding).
210 Adapter(AdapterDecl),
211}
212
213impl SourceUnit {
214 pub fn name(&self) -> &QualifiedName {
215 match self {
216 SourceUnit::Commons(c) => &c.name,
217 SourceUnit::Context(c) => &c.name,
218 SourceUnit::Suite(t) => &t.target,
219 SourceUnit::Adapter(a) => &a.name,
220 }
221 }
222
223 pub fn span(&self) -> Span {
224 match self {
225 SourceUnit::Commons(c) => c.span,
226 SourceUnit::Context(c) => c.span,
227 SourceUnit::Suite(t) => t.span,
228 SourceUnit::Adapter(a) => a.span,
229 }
230 }
231
232 pub fn kind_name(&self) -> &'static str {
233 match self {
234 SourceUnit::Commons(_) => "commons",
235 SourceUnit::Context(_) => "context",
236 SourceUnit::Suite(_) => "suite",
237 SourceUnit::Adapter(_) => "adapter",
238 }
239 }
240}
241
242/// A `test <qualified-name> { ... }` declaration (v0.7 §3.1).
243///
244/// A test targets a commons or context by qualified name and bundles a set of
245/// test cases plus optional mock declarations. As with commons and contexts, a
246/// test may be split across multiple files (fragment form).
247#[derive(Debug, Clone)]
248pub struct SuiteDecl {
249 /// The targeted commons or context.
250 pub target: QualifiedName,
251 /// `uses` clauses brought in by this test fragment.
252 pub uses: Vec<UsesDecl>,
253 /// v0.118: suite-scoped `stub` clauses — per-seam provider overrides
254 /// applied to every case (a case-scoped `stub` takes precedence). Formerly
255 /// the punned `provides` stub; renamed to `stub` in the keyword-hygiene
256 /// batch (#548).
257 pub stubs: Vec<StubClause>,
258 /// The individual test cases.
259 pub cases: Vec<Case>,
260 /// v0.114: generative `property` blocks (testing track slice 2).
261 pub properties: Vec<PropertyDecl>,
262 /// v0.118: the suite-level tier default (`suite … as integration`). `None`
263 /// means the `unit` default; a `case`'s own tier overrides it. A `property`
264 /// ignores a suite tier (tiers are a `case`-only affordance).
265 pub tier: Option<TestTier>,
266 /// Surface form: brace-delimited body or headerless fragment.
267 pub form: CommonsForm,
268 /// Optional documentation block attached to the test declaration.
269 pub documentation: Option<String>,
270 pub span: Span,
271 pub trivia: Trivia,
272 pub trailing_comments: Vec<String>,
273}
274
275/// v0.118: the tier a `case` runs at (testing track slice 6, ADR 0153). One
276/// body promoted across the testing pyramid; `unit` is the default and elided.
277#[derive(Debug, Clone, Copy, PartialEq, Eq)]
278pub enum TestTier {
279 /// Collaborators stubbed (the default).
280 Unit,
281 /// Real collaborators within one context, no serialisation wire.
282 Integration,
283 /// Contexts wired across the real serialise → JSON → deserialise boundary.
284 System,
285}
286
287impl TestTier {
288 pub fn as_str(self) -> &'static str {
289 match self {
290 TestTier::Unit => "unit",
291 TestTier::Integration => "integration",
292 TestTier::System => "system",
293 }
294 }
295}
296
297/// v0.118: a per-seam provider override `stub Cap.method(<args>) returns <v>
298/// | fails` (testing track slice 6, ADR 0154; keyword `stub` since #548).
299/// Substitutes one capability method's provision under test; the right-hand
300/// side is a value or a fault, never a computed body.
301#[derive(Debug, Clone)]
302pub struct StubClause {
303 /// The capability being overridden (a consumed seam of the unit).
304 pub capability: Ident,
305 /// The overridden method.
306 pub method: Ident,
307 /// One argument pattern per parameter (`_` or a value the arg must equal).
308 pub args: Vec<ArgPattern>,
309 /// The provision: a value, a fault, or a per-call sequence.
310 pub rhs: StubRhs,
311 pub documentation: Option<String>,
312 pub span: Span,
313 pub trivia: Trivia,
314}
315
316/// v0.118: one argument pattern in a `stub` call pattern. Patterns for the
317/// same method are tried top-to-bottom, first match wins.
318#[derive(Debug, Clone)]
319pub enum ArgPattern {
320 /// `_` — matches any argument.
321 Any(Span),
322 /// A value the recorded argument must equal (a literal or pure value expr).
323 Value(Expr),
324}
325
326/// v0.118: the right-hand side of a `stub` clause.
327#[derive(Debug, Clone)]
328pub enum StubRhs {
329 /// `returns <value>` — a single success value, repeated for every call.
330 Returns(Expr),
331 /// `fails` — inject a capability fault (Principle 3).
332 Fails(Span),
333 /// `returns each [<outcome>, …]` — one outcome per call, in order; the last
334 /// outcome repeats once the sequence is exhausted (DECISION V).
335 ReturnsEach(Vec<SeqOutcome>, Span),
336}
337
338impl StubRhs {
339 pub fn span(&self) -> Span {
340 match self {
341 StubRhs::Returns(e) => e.span,
342 StubRhs::Fails(s) => *s,
343 StubRhs::ReturnsEach(_, s) => *s,
344 }
345 }
346}
347
348/// v0.118: one outcome in a sequenced (`returns each`) `stub`.
349#[derive(Debug, Clone)]
350pub enum SeqOutcome {
351 /// A success value.
352 Value(Expr),
353 /// A fault.
354 Fails(Span),
355}
356
357/// A `case "name" [as <tier>] { [stub …] body }` block inside a suite
358/// (v0.7 §3.3; v0.118 adds the tier clause and case-scoped stubs).
359#[derive(Debug, Clone)]
360pub struct Case {
361 /// The test name, taken from the string literal.
362 pub name: String,
363 /// The span of the string literal — used for diagnostics and runtime
364 /// failure reports.
365 pub name_span: Span,
366 /// v0.118: the case's own tier, if written (`as integration` / `as system`).
367 /// `None` means inherit the suite default (itself `unit` when unset).
368 pub tier: Option<TestTier>,
369 /// v0.118: case-scoped `stub` clauses (override the suite's, and the
370 /// tier default).
371 pub stubs: Vec<StubClause>,
372 pub body: Block,
373 pub documentation: Option<String>,
374 pub span: Span,
375 pub trivia: Trivia,
376}
377
378/// A `property "name" { for all <bindings> [where <pred>] { body } }` block
379/// inside a suite (v0.114, testing track slice 2, ADR 0149). The generative
380/// sibling of [`Case`]: the runner draws inhabitants of each binding's type from
381/// its refinement domain and evaluates the body's `expect`s over them.
382#[derive(Debug, Clone)]
383pub struct PropertyDecl {
384 /// The property name, taken from the string literal.
385 pub name: String,
386 /// The span of the string literal — used for diagnostics and reports.
387 pub name_span: Span,
388 /// The `for all` binder: the generated bindings, an optional `where` filter,
389 /// and the predicate body.
390 pub forall: ForAll,
391 pub documentation: Option<String>,
392 pub span: Span,
393 pub trivia: Trivia,
394}
395
396/// The `for all x: T, … [where <pred>] { … }` binder inside a [`PropertyDecl`].
397#[derive(Debug, Clone)]
398pub struct ForAll {
399 /// The generated bindings, `x: T` (one or more).
400 pub bindings: Vec<ForAllBinding>,
401 /// An optional `where <pred>` filter (a pure `Bool`) applied to generated
402 /// tuples before the body runs.
403 pub where_pred: Option<Expr>,
404 /// The body — one or more statements, typically `expect`s.
405 pub body: Block,
406 pub span: Span,
407}
408
409/// One `for all` binding: `name: T`, where the runner generates inhabitants of
410/// `T` from its refinements.
411#[derive(Debug, Clone)]
412pub struct ForAllBinding {
413 pub name: Ident,
414 pub type_ref: TypeRef,
415}
416
417/// A capability reference in a `given` clause (v0.15 §3.2). A bare name is a
418/// local capability (`given Cap`); a dotted name refers to a capability a
419/// consumed context provides (`given B.Cap` / `given Alias.Cap`).
420#[derive(Debug, Clone)]
421pub struct CapRef {
422 /// `None` for a local capability; `Some(prefix)` for a cross-context
423 /// reference where `prefix` is a consumed-context qualified name or alias.
424 pub context: Option<QualifiedName>,
425 /// The capability's simple name (also the local deps key).
426 pub name: Ident,
427 pub span: Span,
428}
429
430impl CapRef {
431 /// The local deps key / capability simple name (e.g. `Clock`).
432 pub fn key(&self) -> &str {
433 &self.name.name
434 }
435
436 /// True when this references a capability provided by a consumed context.
437 pub fn is_cross_context(&self) -> bool {
438 self.context.is_some()
439 }
440
441 /// The cross-context prefix (consumed-context qualified name or alias) as
442 /// a dotted string, if any.
443 pub fn prefix(&self) -> Option<String> {
444 self.context.as_ref().map(|q| q.joined())
445 }
446}
447
448/// A dotted name like `fitness.units`.
449#[derive(Debug, Clone)]
450pub struct QualifiedName {
451 pub parts: Vec<Ident>,
452 pub span: Span,
453}
454
455impl QualifiedName {
456 pub fn joined(&self) -> String {
457 self.parts
458 .iter()
459 .map(|p| p.name.as_str())
460 .collect::<Vec<_>>()
461 .join(".")
462 }
463}
464
465#[derive(Debug, Clone)]
466pub enum CommonsItem {
467 Type(TypeDecl),
468 Fn(FnDecl),
469 /// `capability Name { fn op(...) -> T ... }` (v0.5; contexts only).
470 Capability(CapabilityDecl),
471 /// `provides Cap = ProviderName { fn op(...) -> T { ... } ... }` (v0.5).
472 Provider(ProviderDecl),
473 /// `service Name { on call(...) -> T { ... } ... }` (v0.5).
474 Service(ServiceDecl),
475 /// `agent Name { key id: T; state { ... }; on call ... }` (v0.5).
476 Agent(AgentDecl),
477 /// `actor Name { auth = Scheme, identity = T }` (v0.45). A nominal boundary
478 /// contract consumed by a handler's `by` clause; not a runnable entity.
479 Actor(ActorDecl),
480}
481
482impl CommonsItem {
483 pub fn name(&self) -> &Ident {
484 match self {
485 CommonsItem::Type(t) => &t.name,
486 CommonsItem::Fn(f) => f.name.ident(),
487 CommonsItem::Capability(c) => &c.name,
488 CommonsItem::Provider(p) => &p.provider_name,
489 CommonsItem::Service(s) => &s.name,
490 CommonsItem::Agent(a) => &a.name,
491 CommonsItem::Actor(a) => &a.name,
492 }
493 }
494}
495
496/// A capability declaration (v0.5 §3.3). Capabilities are interface-like
497/// contracts for external dependencies, used inside contexts. They may only
498/// appear inside a `context` declaration.
499#[derive(Debug, Clone)]
500pub struct CapabilityDecl {
501 pub name: Ident,
502 pub ops: Vec<CapabilityOp>,
503 pub documentation: Option<String>,
504 pub span: Span,
505 pub trivia: Trivia,
506}
507
508/// One operation in a capability (signature only; no body).
509#[derive(Debug, Clone)]
510pub struct CapabilityOp {
511 pub name: Ident,
512 pub params: Vec<Param>,
513 pub return_type: TypeRef,
514 pub documentation: Option<String>,
515 pub span: Span,
516 pub trivia: Trivia,
517}
518
519/// A provider declaration (v0.5 §3.4). Supplies an implementation for a
520/// capability.
521#[derive(Debug, Clone)]
522pub struct ProviderDecl {
523 /// The capability being implemented.
524 pub capability: Ident,
525 /// The provider's identifier (used in tests/config to select impls).
526 pub provider_name: Ident,
527 /// v0.12: capabilities this provider depends on (`provides X = Impl given
528 /// Y, Z { … }`). The provider's operation bodies may use these. v0.15:
529 /// a dependency may be a cross-context capability (`given B.Cap`).
530 pub given: Vec<CapRef>,
531 pub ops: Vec<ProviderOp>,
532 /// v0.17: an *external* provider — `provides Cap = Name` with **no** brace
533 /// block — inside an adapter, supplied by the adapter's binding rather than
534 /// a Bynk body. When `true`, `ops` is empty and the emitter produces no
535 /// class. The absence of the brace block (not an empty one) is the signal.
536 pub external: bool,
537 pub documentation: Option<String>,
538 pub span: Span,
539 pub trivia: Trivia,
540}
541
542/// One operation in a provider (signature plus body).
543#[derive(Debug, Clone)]
544pub struct ProviderOp {
545 pub name: Ident,
546 pub params: Vec<Param>,
547 pub return_type: TypeRef,
548 pub body: Block,
549 pub span: Span,
550 pub trivia: Trivia,
551}
552
553/// A service declaration (v0.5 §3.5). Services are the boundary interface
554/// of a context.
555#[derive(Debug, Clone)]
556pub struct ServiceDecl {
557 pub name: Ident,
558 /// The protocol the service conforms to, from the `from <protocol>` header
559 /// clause (v0.44). `Call` when there is no clause.
560 pub protocol: ServiceProtocol,
561 /// The optional service-level `by` default (v0.155) — a `by <Actor>` clause on
562 /// the service header, `service Api from http by v: Visitor { … }`. Every
563 /// handler that omits its own `by` inherits this one (injected by the
564 /// normalization pass). `None` when absent — handlers then fall back to the
565 /// per-protocol default actor (HTTP/WebSocket have none, so `by` stays
566 /// mandatory there). The "public / bearer-authed" fact is usually a service
567 /// fact, so this removes the per-handler repetition.
568 pub default_by: Option<ByClause>,
569 /// The optional service-level `given` default (v0.155) — a `given C1, C2`
570 /// clause on the service header, following the `by` default. Every handler
571 /// that declares no `given` of its own inherits this list. Empty when absent.
572 pub default_given: Vec<CapRef>,
573 /// The optional cross-origin (CORS) policy (v0.131, ADR 0159) — a `cors { }`
574 /// section in the service body, only meaningful on a `from http` service.
575 /// `None` when absent (same-origin default, byte-for-byte unchanged output).
576 pub cors: Option<CorsPolicy>,
577 /// The optional security-headers policy (v0.141, ADR 0164) — a `security { }`
578 /// section in the service body, only meaningful on a `from http` service.
579 /// `None` when absent, but unlike `cors` the *absence* still stamps the safe
580 /// defaults (`nosniff` on) — the emitter synthesises a default policy for every
581 /// `from http` service, so `None` here means "defaults", not "no headers".
582 pub security: Option<SecurityPolicy>,
583 /// The optional request-body-size policy (v0.142, ADR 0165) — a `limits { }`
584 /// section in the service body, only meaningful on a `from http` service. It
585 /// declares a per-service `maxBody` ceiling (in bytes) for the service's
586 /// body-taking routes; a route may override it with `@limit(maxBody: …)`.
587 /// `None` when absent (no cap — byte-for-byte unchanged output, the opt-in
588 /// CORS posture, not the `security` default-on posture).
589 pub limits: Option<LimitsPolicy>,
590 pub handlers: Vec<Handler>,
591 pub documentation: Option<String>,
592 pub span: Span,
593 pub trivia: Trivia,
594}
595
596/// A cross-origin resource-sharing policy on a `from http` service (v0.131,
597/// ADR 0159): the `cors { }` section in the service body. Parsed leniently as a
598/// list of `name: value` fields (the grammar accepts any field name — an unknown
599/// one is a checker diagnostic, per the `@`-annotation precedent, ADR 0111), and
600/// interpreted through the typed accessors below.
601///
602/// `Access-Control-Allow-Methods` is deliberately **not** a field — it is derived
603/// from the service's routes at emit time (the routes already enumerate the
604/// methods; a restated list would drift). Likewise `Allow-Headers` defaults to
605/// `content-type` (+ `Authorization` when a Bearer route exists) and is only
606/// stored here when the author overrides it.
607#[derive(Debug, Clone)]
608pub struct CorsPolicy {
609 /// The `cors { }` fields as written, in source order. Field names are
610 /// validated against the closed set (`origins`/`headers`/`credentials`/
611 /// `maxAge`) by the checker, not the parser.
612 pub fields: Vec<CorsField>,
613 pub span: Span,
614 pub trivia: Trivia,
615}
616
617/// One `name: value` field inside a `cors { }` policy (v0.131).
618#[derive(Debug, Clone)]
619pub struct CorsField {
620 pub name: Ident,
621 pub value: Expr,
622 pub span: Span,
623}
624
625impl CorsPolicy {
626 /// The raw value expression for a field, by name (the last one wins if a
627 /// field is repeated — the checker flags the duplicate separately).
628 pub fn field(&self, name: &str) -> Option<&Expr> {
629 self.fields
630 .iter()
631 .rev()
632 .find(|f| f.name.name == name)
633 .map(|f| &f.value)
634 }
635
636 /// The allowed origins — the string literals of the `origins:` list. An
637 /// absent or malformed field yields an empty list (the checker has already
638 /// reported the shape error; the emitter fails closed on an empty list).
639 pub fn origins(&self) -> Vec<String> {
640 Self::str_list(self.field("origins")).unwrap_or_default()
641 }
642
643 /// `true` iff `origins` is exactly the wildcard `["*"]`.
644 pub fn is_wildcard(&self) -> bool {
645 let os = self.origins();
646 os.len() == 1 && os[0] == "*"
647 }
648
649 /// Whether credentialed requests are allowed (`credentials: true`); defaults
650 /// to `false` when the field is absent.
651 pub fn credentials(&self) -> bool {
652 matches!(
653 self.field("credentials").map(|e| &e.kind),
654 Some(ExprKind::BoolLit(true))
655 )
656 }
657
658 /// The explicit `Access-Control-Allow-Headers` override, if the author gave
659 /// a `headers:` list; `None` leaves the emitter to apply its smart default.
660 pub fn allow_headers(&self) -> Option<Vec<String>> {
661 self.field("headers").and_then(Self::str_list_of)
662 }
663
664 /// The `Access-Control-Max-Age` in whole seconds, if a `maxAge:` duration was
665 /// given; `None` leaves the header off (the browser default).
666 pub fn max_age_secs(&self) -> Option<i64> {
667 match self.field("maxAge").map(|e| &e.kind) {
668 Some(ExprKind::DurationLit { millis, .. }) => Some(millis / 1_000),
669 _ => None,
670 }
671 }
672
673 /// Interpret an expression as a list of string literals, if it is one.
674 fn str_list(expr: Option<&Expr>) -> Option<Vec<String>> {
675 expr.and_then(Self::str_list_of)
676 }
677
678 fn str_list_of(expr: &Expr) -> Option<Vec<String>> {
679 match &expr.kind {
680 ExprKind::ListLit(items) => items
681 .iter()
682 .map(|e| match &e.kind {
683 ExprKind::StrLit(s) => Some(s.clone()),
684 _ => None,
685 })
686 .collect(),
687 _ => None,
688 }
689 }
690}
691
692/// A security-headers policy on a `from http` service (v0.141, ADR 0164): the
693/// `security { }` section in the service body. Parsed leniently as a list of
694/// `name: value` fields (an unknown one is a checker diagnostic, per the CORS /
695/// `@`-annotation precedent) and interpreted through the typed accessors below.
696///
697/// The closed set is `nosniff` (a `Bool`, default `true` — stamps
698/// `X-Content-Type-Options: nosniff`) and `hsts` (a positive `Duration`, opt-in —
699/// stamps `Strict-Transport-Security: max-age=…`). Unlike `cors`, the *safe*
700/// header is on by default: a `from http` service with no `security { }` still
701/// stamps `nosniff`, because a security header you have to remember to switch on
702/// is the one you forget (ADR 0164 DECISION A).
703#[derive(Debug, Clone)]
704pub struct SecurityPolicy {
705 /// The `security { }` fields as written, in source order. Field names are
706 /// validated against the closed set (`hsts`/`nosniff`) by the checker, not
707 /// the parser.
708 pub fields: Vec<SecurityField>,
709 pub span: Span,
710 pub trivia: Trivia,
711}
712
713/// One `name: value` field inside a `security { }` policy (v0.141).
714#[derive(Debug, Clone)]
715pub struct SecurityField {
716 pub name: Ident,
717 pub value: Expr,
718 pub span: Span,
719}
720
721impl SecurityPolicy {
722 /// The raw value expression for a field, by name (the last one wins if a
723 /// field is repeated — the checker flags the duplicate separately).
724 pub fn field(&self, name: &str) -> Option<&Expr> {
725 self.fields
726 .iter()
727 .rev()
728 .find(|f| f.name.name == name)
729 .map(|f| &f.value)
730 }
731
732 /// Whether `X-Content-Type-Options: nosniff` is stamped. Defaults to `true`
733 /// (the safe default, ADR 0164 DECISION A); only an explicit `nosniff: false`
734 /// opts out. A malformed value has already been reported by the checker; it
735 /// falls back to the safe default here.
736 pub fn nosniff(&self) -> bool {
737 !matches!(
738 self.field("nosniff").map(|e| &e.kind),
739 Some(ExprKind::BoolLit(false))
740 )
741 }
742
743 /// The `Strict-Transport-Security` `max-age` in whole seconds, if the author
744 /// opted in with an `hsts:` duration; `None` leaves HSTS off (the default —
745 /// HSTS pins the browser to HTTPS and is a deliberate opt-in, DECISION A).
746 pub fn hsts_max_age_secs(&self) -> Option<i64> {
747 match self.field("hsts").map(|e| &e.kind) {
748 Some(ExprKind::DurationLit { millis, .. }) => Some(millis / 1_000),
749 _ => None,
750 }
751 }
752}
753
754/// A request-body-size policy on a `from http` service (v0.142, ADR 0165): the
755/// `limits { }` section in the service body. Parsed leniently as a list of
756/// `name: value` fields (an unknown one is a checker diagnostic, per the CORS /
757/// `security` / `@`-annotation precedent) and interpreted through the typed
758/// accessor below.
759///
760/// The closed set is `maxBody` — a positive `Int` byte count (there is no byte
761/// `Size` literal yet; a `1.mb`-style literal is a named follow-on, the
762/// `Duration` playbook). Unlike `security`, this is opt-in: a service with no
763/// `limits { }` (and no route `@limit`) has no cap and emits byte-for-byte
764/// unchanged output (ADR 0165 DECISION E — the CORS posture).
765#[derive(Debug, Clone)]
766pub struct LimitsPolicy {
767 /// The `limits { }` fields as written, in source order. Field names are
768 /// validated against the closed set (`maxBody`) by the checker, not the
769 /// parser.
770 pub fields: Vec<LimitsField>,
771 pub span: Span,
772 pub trivia: Trivia,
773}
774
775/// One `name: value` field inside a `limits { }` policy (v0.142).
776#[derive(Debug, Clone)]
777pub struct LimitsField {
778 pub name: Ident,
779 pub value: Expr,
780 pub span: Span,
781}
782
783impl LimitsPolicy {
784 /// The raw value expression for a field, by name (the last one wins if a
785 /// field is repeated — the checker flags the duplicate separately).
786 pub fn field(&self, name: &str) -> Option<&Expr> {
787 self.fields
788 .iter()
789 .rev()
790 .find(|f| f.name.name == name)
791 .map(|f| &f.value)
792 }
793
794 /// The service-wide maximum request-body size in bytes, if the author gave a
795 /// positive `maxBody:` `Int` literal; `None` leaves the service without a
796 /// default cap. A malformed or non-positive value has already been reported
797 /// by the checker; it falls back to `None` here (no cap).
798 pub fn max_body(&self) -> Option<i64> {
799 match self.field("maxBody").map(|e| &e.kind) {
800 Some(ExprKind::IntLit { value, .. }) if *value > 0 => Some(*value),
801 _ => None,
802 }
803 }
804}
805
806/// The protocol a service conforms to — declared on the header via
807/// `from <protocol>` (v0.44). `Call` is the default (no `from` clause): a
808/// contract-mediated internal-RPC surface, not a wire protocol. Multi-endpoint
809/// protocols (`Http`, `Cron`) carry no binding — the endpoint lives on each
810/// handler; single-binding `Queue` carries its queue name.
811#[derive(Debug, Clone)]
812pub enum ServiceProtocol {
813 /// No `from` clause: the service holds `on call` handlers only.
814 Call,
815 /// `from http` — many routes; each handler is `on <Method>("route")`.
816 Http,
817 /// `from cron` — many schedules; each handler is `on schedule("expr")`.
818 Cron,
819 /// `from queue("name")` — one bound queue; handlers are `on message(...)`.
820 Queue { name: String },
821 /// `from websocket(in: ClientFrame, out: ServerFrame)` — a held WebSocket
822 /// connection (v0.103, real-time track slice 3). `in_type` is the inbound
823 /// frame type (client→server, decoded and routed as typed agent messages);
824 /// `out_type` is the server→client frame type the held `Connection[out_type]`
825 /// carries. The service holds exactly one `on open` handler (edge auth via
826 /// `by`, then transfer of the connection to an agent).
827 WebSocket { in_type: TypeRef, out_type: TypeRef },
828}
829
830/// An agent declaration (v0.5 §3.6). Agents are state-bearing entities
831/// with their own handlers.
832#[derive(Debug, Clone)]
833pub struct AgentDecl {
834 pub name: Ident,
835 /// `key id: Type` — the identifier-typed value identifying instances.
836 pub key_name: Ident,
837 pub key_type: TypeRef,
838 /// `store` fields (v0.81, storage track) — each an access-pattern slot of a
839 /// declared storage kind (`Cell`/`Map`/…). The successor to the removed
840 /// `state { }` record (ADR 0108); every agent declares its state this way.
841 pub store_fields: Vec<StoreField>,
842 /// Invariants (v0.80 §14) — universally-quantified predicates over the
843 /// agent's `store` fields. The phase sits between the fields and the
844 /// handlers; each is checked against the state staged by a handler's writes
845 /// before it commits.
846 pub invariants: Vec<Invariant>,
847 /// Step invariants (v0.116 §, testing track slice 4) — named predicates over
848 /// the pre-/post-commit state *pair* (`old`/`new`), checked at the commit
849 /// boundary beside [`invariants`], from the second commit onward. Widen the
850 /// invariant subject from a snapshot to a step (ADR 0144 — one predicate
851 /// surface).
852 ///
853 /// [`invariants`]: AgentDecl::invariants
854 pub transitions: Vec<Transition>,
855 pub handlers: Vec<Handler>,
856 pub documentation: Option<String>,
857 pub span: Span,
858 pub trivia: Trivia,
859}
860
861/// A `store` field (v0.81, storage track). Each is an access-pattern slot of a
862/// declared storage kind: `store <name>: <Kind>[…] [@annotations] [= <init>]`.
863/// The kind and its element type are carried as an ordinary [`TypeRef`]
864/// (`Cell[Int]`, `Map[K, V]`); the checker restricts which heads are storage
865/// kinds. Access-pattern annotations (`@indexed`, …) parse into [`annotations`]
866/// (v0.85, ADR 0111); the checker validates them against the closed registry.
867///
868/// [`annotations`]: StoreField::annotations
869#[derive(Debug, Clone)]
870pub struct StoreField {
871 pub name: Ident,
872 /// The storage kind and its element type(s): `Cell[Int]`, `Map[K, V]`. A
873 /// dedicated [`StoreKind`] rather than a [`TypeRef`] — storage kinds are not
874 /// value types, and the checker dispatches kind-aware operations on the head.
875 pub kind: StoreKind,
876 /// Storage annotations on the field (v0.85, ADR 0111): `@ttl(5.minutes)`,
877 /// `@indexed(by: orderId)`. Parsed in declaration order (after the kind,
878 /// before the initialiser); the checker validates names against the closed
879 /// registry and gates each to the slice that implements it.
880 pub annotations: Vec<Annotation>,
881 /// The fresh-key initial value (`= expr`), if given — same disposition as a
882 /// `state` field's initialiser (ADRs 0003/0004 carry forward).
883 pub init: Option<Expr>,
884 pub documentation: Option<String>,
885 pub span: Span,
886 pub trivia: Trivia,
887}
888
889/// A storage annotation on a `store` field (v0.85, storage track; ADR 0111):
890/// `@<name>(<args>)`. The `name` is matched against the closed registry
891/// (`@indexed`/`@ttl`/`@retain`/`@bounded`) by the checker; the grammar accepts
892/// any identifier so an unknown name is a checker diagnostic, not a parse error.
893/// Arguments are compile-time metadata, restricted to literals (and the `by:`
894/// field-name labels of `@indexed`) by the checker per ADR 0111 D4.
895#[derive(Debug, Clone)]
896pub struct Annotation {
897 pub name: Ident,
898 pub args: Vec<AnnotationArg>,
899 pub span: Span,
900}
901
902/// A single annotation argument (v0.85; ADR 0111): an optional `label:` followed
903/// by a value expression — `by: orderId` (labelled) or `5.minutes` (positional).
904/// The value is parsed as an ordinary [`Expr`] so the duration-literal form
905/// (`5.minutes`, landing with the `Duration` slice) needs no special grammar;
906/// the checker restricts it to a literal where the annotation is functional.
907#[derive(Debug, Clone)]
908pub struct AnnotationArg {
909 pub label: Option<Ident>,
910 pub value: Expr,
911 pub span: Span,
912}
913
914/// A storage kind applied to its element type(s) (v0.81): `Cell[Int]`,
915/// `Map[ReservationId, Reservation]`. The `head` is the kind name (`Cell`,
916/// `Map`, `Set`, `Log`, `Queue`, `Cache`); the checker validates it against the
917/// closed catalogue. Element types are ordinary [`TypeRef`]s. Refined element
918/// types (`Cell[Int where NonNegative]`) ride a later slice (parse_type_ref does
919/// not yet accept an inline refinement in type-argument position).
920#[derive(Debug, Clone)]
921pub struct StoreKind {
922 pub head: Ident,
923 pub args: Vec<TypeRef>,
924 pub span: Span,
925}
926
927/// An agent invariant (v0.80 §14). A named predicate over the agent's state
928/// fields that must hold of every committed state; a commit that would violate
929/// it faults (`InvariantViolation`) before the state is persisted. The
930/// predicate references state fields by bare name, mirroring the design-notes
931/// worked examples (`status == Paid implies paymentRef.isSome()`).
932#[derive(Debug, Clone)]
933pub struct Invariant {
934 pub name: Ident,
935 /// The predicate expression — an ordinary `Bool`-typed expression over the
936 /// state fields, plus `implies` and `is`. The parsed-predicate-on-a-
937 /// declaration shape mirrors [`ActorRefinement::predicate`].
938 pub predicate: Expr,
939 pub documentation: Option<String>,
940 pub span: Span,
941 pub trivia: Trivia,
942}
943
944/// An agent step invariant (v0.116 §, testing track slice 4). A named predicate
945/// over the *pair* of committed states — the pre-commit `old` and the proposed
946/// `new`, each the agent's state record — that must hold of every state move; a
947/// commit that would violate it faults (`InvariantViolation`) before the state is
948/// persisted, exactly as a snapshot [`Invariant`] does. Widens the invariant
949/// subject from a snapshot to a step (ADR 0144 — one predicate surface); the
950/// predicate reuses the invariant surface (`implies`/`is`/pure methods) with
951/// `old`/`new` bound contextually (`old.status is Paid implies new.status is
952/// Paid`).
953#[derive(Debug, Clone)]
954pub struct Transition {
955 pub name: Ident,
956 /// The predicate expression — an ordinary `Bool`-typed expression over the
957 /// `old` and `new` state records, with `implies`/`is` and pure methods,
958 /// mirroring [`Invariant`].
959 pub predicate: Expr,
960 pub documentation: Option<String>,
961 pub span: Span,
962 pub trivia: Trivia,
963}
964
965/// A function contract clause (v0.115 §, testing track slice 3). A named
966/// predicate on a `fn` signature — a `requires` (precondition) or `ensures`
967/// (postcondition). A contract is the invariant predicate attached to a
968/// function (ADR 0144 — one predicate surface): the predicate is a pure `Bool`
969/// expression over the parameters (`requires`) or the parameters plus `result`
970/// (`ensures`), with `implies`/`is` and pure methods, mirroring [`Invariant`].
971/// The name rides the failure report and the redundant-test dedup.
972#[derive(Debug, Clone)]
973pub struct Contract {
974 pub name: Ident,
975 /// The predicate expression — an ordinary `Bool`-typed expression over the
976 /// parameters (and, for an `ensures`, the contextual `result` binding).
977 pub predicate: Expr,
978 pub span: Span,
979}
980
981/// An actor declaration (v0.45 §3.7). An actor is a nominal *contract type*
982/// describing an external party at a boundary — not a runnable entity. A
983/// handler consumes an actor on its `by` clause; the boundary verifies the
984/// declared `auth` scheme and mints a sealed identity (`name.identity`).
985#[derive(Debug, Clone)]
986pub struct ActorDecl {
987 pub name: Ident,
988 /// The authentication scheme from `auth = <Scheme>`, stored as the raw
989 /// identifier. The checker classifies it: `None`/`Internal`/`Bearer` are
990 /// admitted; `Signature` is reserved-and-rejected
991 /// (`bynk.actor.scheme_unsupported`); anything else is
992 /// `bynk.actor.unknown_scheme`. `None` for the refinement form.
993 pub auth: Option<Ident>,
994 /// The scheme's keyed config from `auth = Scheme(key = value, …)` (v0.47
995 /// `Bearer(secret = "…")`; v0.51 generalised for `Signature(secret, header,
996 /// timestamp?, tolerance?)`). Empty for schemes/forms with no config. The
997 /// checker validates which keys each scheme requires/allows.
998 pub auth_config: Vec<SchemeArg>,
999 /// The optional identity type from `, identity = <T>`. Absent ⇒ the
1000 /// scheme default (`()` for `None`; a sealed `CallerId` for the `Internal`
1001 /// `on call` channel, `()` for other `Internal` channels).
1002 pub identity: Option<TypeRef>,
1003 /// The refinement form `actor Admin = Base where <predicate>` — narrows a
1004 /// base actor by an authorisation claim (ADR 0091). The predicate is parsed
1005 /// as a full expression; a static-semantics rule restricts it to the closed
1006 /// actor-claim catalogue (`hasClaim`/`claimEquals` over a `Bearer` base;
1007 /// `bynk.actor.refinement_predicate_unsupported` / `…_base_unsupported`).
1008 pub refinement: Option<ActorRefinement>,
1009 pub documentation: Option<String>,
1010 pub span: Span,
1011 pub trivia: Trivia,
1012}
1013
1014impl ActorDecl {
1015 /// The value of a scheme config arg by key, if present (e.g. `secret`,
1016 /// `header`).
1017 pub fn scheme_arg(&self, key: &str) -> Option<&SchemeArg> {
1018 self.auth_config.iter().find(|a| a.key.name == key)
1019 }
1020}
1021
1022/// One `key = value` argument in a scheme config (`Scheme(key = value, …)`).
1023#[derive(Debug, Clone)]
1024pub struct SchemeArg {
1025 pub key: Ident,
1026 pub value: SchemeArgValue,
1027 /// Span of the value, for diagnostics.
1028 pub span: Span,
1029}
1030
1031/// A scheme config arg value — a string literal or an integer.
1032#[derive(Debug, Clone)]
1033pub enum SchemeArgValue {
1034 Str(String),
1035 Int(i64),
1036}
1037
1038impl SchemeArgValue {
1039 pub fn as_str(&self) -> Option<&str> {
1040 match self {
1041 SchemeArgValue::Str(s) => Some(s),
1042 SchemeArgValue::Int(_) => None,
1043 }
1044 }
1045 pub fn as_int(&self) -> Option<i64> {
1046 match self {
1047 SchemeArgValue::Int(n) => Some(*n),
1048 SchemeArgValue::Str(_) => None,
1049 }
1050 }
1051}
1052
1053/// The reserved refinement form `actor Admin = User where <predicate>` (Q3).
1054/// Parsed in Foundations so the grammar is fixed; admission is a later slice.
1055#[derive(Debug, Clone)]
1056pub struct ActorRefinement {
1057 /// The base actor being refined.
1058 pub base: Ident,
1059 /// The `where` predicate. Parsed but not yet checked.
1060 pub predicate: Expr,
1061 pub span: Span,
1062}
1063
1064/// The `by (<binder>:)? <Actor>` clause on a handler (v0.45; binder optional in
1065/// v0.50). Names the actor contract the handler consumes; when a `binder` is
1066/// given, the verified identity binds to it and is read as `binder.identity`.
1067/// Omitting the binder (`by <Actor>`) declares-and-verifies the contract without
1068/// capturing the identity — for anonymous or verify-and-discard handlers. Sits
1069/// after the protocol config and before the parameters.
1070#[derive(Debug, Clone)]
1071pub struct ByClause {
1072 /// The identity binder, if the handler consumes the identity. `None` for the
1073 /// binder-less `by <Actor>` form. Required when `actors` names more than one
1074 /// (a sum is resolved by matching on the bound actor).
1075 pub binder: Option<Ident>,
1076 /// The actor contract(s) referenced — each a local actor decl or a prelude
1077 /// actor. A single name is the ordinary single-actor handler; more than one
1078 /// (`by who: A | B`, v0.52) is an **ordered sum of peer actors** resolved
1079 /// first-wins, the body matching on the resolved actor. Always non-empty.
1080 pub actors: Vec<Ident>,
1081 pub span: Span,
1082}
1083
1084impl ByClause {
1085 /// The first (and, for a single-actor handler, only) actor contract named.
1086 pub fn primary(&self) -> &Ident {
1087 &self.actors[0]
1088 }
1089 /// Whether this `by` clause names an ordered sum of peer actors (`A | B`).
1090 pub fn is_sum(&self) -> bool {
1091 self.actors.len() > 1
1092 }
1093}
1094
1095/// v0.182 (testing-the-boundary Slice A, #664): a call-site actor clause on a
1096/// test-body `let x <- <service address> by <Actor>(<identity>)`. Distinct from
1097/// [`ByClause`] (the handler/header form): the *declaration* names which actor
1098/// may call and binds the verified identity, whereas the *call site* names the
1099/// actor the case is acting as and supplies the identity value. A unit-identity
1100/// actor (`Visitor`, and cron/queue's internal actors) carries no `identity`.
1101#[derive(Debug, Clone)]
1102pub struct CallSiteActor {
1103 /// The actor the case acts as — a local actor decl or a prelude actor.
1104 pub actor: Ident,
1105 /// The supplied identity value (`"bob"` in `by User("bob")`), or `None` for a
1106 /// unit-identity actor written `by Visitor` with no argument.
1107 pub identity: Option<Box<Expr>>,
1108 pub span: Span,
1109}
1110
1111/// A handler block — `on call(args) -> T given C1, C2 { body }`.
1112/// Used by both services and agents.
1113#[derive(Debug, Clone)]
1114pub struct Handler {
1115 pub kind: HandlerKind,
1116 /// Handler-position annotations (v0.140, ADR 0163): `@cache(maxAge: 5.minutes)`
1117 /// written immediately before `on <METHOD>(…)`. Reuses the [`Annotation`] AST
1118 /// shared with `store` fields (ADR 0111); the grammar accepts any `@name(args)`
1119 /// so an unknown name is a project-validation diagnostic, not a parse error. The
1120 /// first handler-position annotation surface — empty for every handler that
1121 /// carries none.
1122 pub annotations: Vec<Annotation>,
1123 /// For agent handlers, the method-style handler name (e.g.
1124 /// `on call addItem(...)`). For service handlers, this is None (just
1125 /// `on call(...)`).
1126 pub method_name: Option<Ident>,
1127 /// The `by <binder>: <Actor>` clause (v0.45), if present. Service handlers
1128 /// only; an absent clause inherits the protocol's default actor.
1129 pub by_clause: Option<ByClause>,
1130 pub params: Vec<Param>,
1131 pub return_type: TypeRef,
1132 pub given: Vec<CapRef>,
1133 pub body: Block,
1134 pub documentation: Option<String>,
1135 pub span: Span,
1136 pub trivia: Trivia,
1137}
1138
1139#[derive(Debug, Clone, PartialEq, Eq)]
1140pub enum HandlerKind {
1141 /// `on call(...)` — typed RPC (the only kind in v0.5).
1142 Call,
1143 /// `on http METHOD "path"` — external-facing HTTP route (v0.9).
1144 Http { method: HttpMethod, path: String },
1145 /// `on cron "expr"` — scheduled task; `expr` is a 5-field cron
1146 /// expression (v0.10a).
1147 Cron { expr: String },
1148 /// `on message(m: T)` — a message off the service's bound queue. The queue
1149 /// binding lives on the service's `ServiceProtocol::Queue` (v0.44).
1150 Message,
1151 /// `on open ...` — the WebSocket upgrade handler (v0.103, real-time track
1152 /// slice 3). Exactly one per `from websocket` service; carries a mandatory
1153 /// `by` clause (edge auth) and receives a fresh owned `Connection[out]`.
1154 Open,
1155 /// `on close ...` — the WebSocket close handler (v0.106, real-time track slice
1156 /// 3b-iii). Optional, ≤1 per `from websocket` service; runs when the socket
1157 /// closes. Like `on open`, edge-authenticated (`by`), with the identity/params
1158 /// recovered from the socket attachment (set at `on open`). (A `from websocket`
1159 /// `on message` reuses [`HandlerKind::Message`], disambiguated by the protocol.)
1160 Close,
1161}
1162
1163/// HTTP methods supported by `on http` handlers (v0.9).
1164#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1165pub enum HttpMethod {
1166 Get,
1167 Post,
1168 Put,
1169 Patch,
1170 Delete,
1171}
1172
1173impl HttpMethod {
1174 pub fn as_str(self) -> &'static str {
1175 match self {
1176 HttpMethod::Get => "GET",
1177 HttpMethod::Post => "POST",
1178 HttpMethod::Put => "PUT",
1179 HttpMethod::Patch => "PATCH",
1180 HttpMethod::Delete => "DELETE",
1181 }
1182 }
1183
1184 pub fn from_ident(s: &str) -> Option<HttpMethod> {
1185 match s {
1186 "GET" => Some(HttpMethod::Get),
1187 "POST" => Some(HttpMethod::Post),
1188 "PUT" => Some(HttpMethod::Put),
1189 "PATCH" => Some(HttpMethod::Patch),
1190 "DELETE" => Some(HttpMethod::Delete),
1191 _ => None,
1192 }
1193 }
1194
1195 /// True if this method conventionally has no request body.
1196 pub fn forbids_body(self) -> bool {
1197 matches!(self, HttpMethod::Get | HttpMethod::Delete)
1198 }
1199}
1200
1201/// Payload shape of an `HttpResult[T]` variant (v0.9 §3.3).
1202#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1203pub enum HttpVariantPayload {
1204 /// No payload (e.g. `NoContent`, `Unauthorized`).
1205 None,
1206 /// Carries a value of the `HttpResult` type parameter `T`.
1207 Value,
1208 /// Carries a `String` message (e.g. `BadRequest`, `Conflict`).
1209 Message,
1210 /// Carries a `String` target URL, emitted as a `Location` header — the
1211 /// redirect variants (`Found`, `SeeOther`, `PermanentRedirect`, …).
1212 Location,
1213 /// Carries a `Stream[String]`, emitted as an SSE (`text/event-stream`)
1214 /// streaming body — the `Streaming` (200) variant (v0.101, real-time track
1215 /// slice 1).
1216 Streamed,
1217 /// Carries `(body: Bytes, contentType: String)` — the author-owned raw body
1218 /// written straight into the response with the declared `content-type` and
1219 /// **no codec** (the typed-wire guarantee is deliberately off). The `Raw`
1220 /// (200) variant (v0.111); the first two-argument payload shape.
1221 Raw,
1222}
1223
1224/// One variant of the built-in `HttpResult[T]` sum (v0.9 §3.3).
1225#[derive(Debug, Clone, Copy)]
1226pub struct HttpVariant {
1227 pub name: &'static str,
1228 pub payload: HttpVariantPayload,
1229 pub status: u16,
1230}
1231
1232/// All `HttpResult[T]` variants, in declaration order (ascending status). The
1233/// vocabulary tracks the common, modern HTTP status codes (RFC 9110): success
1234/// and created/accepted (`Value`), redirects carrying a `Location` URL, and
1235/// the client/server failures that handlers routinely return (`Message` when
1236/// an explanation helps the caller, `None` for self-describing statuses).
1237pub const HTTP_VARIANTS: &[HttpVariant] = &[
1238 // ── 2xx success ──────────────────────────────────────────────────────
1239 HttpVariant {
1240 name: "Ok",
1241 payload: HttpVariantPayload::Value,
1242 status: 200,
1243 },
1244 // v0.101 (real-time track slice 1): a 200 whose body is a streamed
1245 // `Stream[String]`, SSE-framed. Status precedes the body, so streaming is
1246 // 200-only — pre-stream failures are ordinary variants returned instead.
1247 HttpVariant {
1248 name: "Streaming",
1249 payload: HttpVariantPayload::Streamed,
1250 status: 200,
1251 },
1252 // v0.111: a 200 whose body is an author-owned `Bytes` written straight into
1253 // the response with the declared `content-type` — no codec runs. 200-only,
1254 // like `Streaming`: it serves service-tier raw bodies (`robots.txt`,
1255 // `sitemap.xml`, feeds, a QR PNG), not custom-status error pages.
1256 HttpVariant {
1257 name: "Raw",
1258 payload: HttpVariantPayload::Raw,
1259 status: 200,
1260 },
1261 HttpVariant {
1262 name: "Created",
1263 payload: HttpVariantPayload::Value,
1264 status: 201,
1265 },
1266 HttpVariant {
1267 name: "Accepted",
1268 payload: HttpVariantPayload::Value,
1269 status: 202,
1270 },
1271 HttpVariant {
1272 name: "NoContent",
1273 payload: HttpVariantPayload::None,
1274 status: 204,
1275 },
1276 // ── 3xx redirection (carry a `Location` URL) ─────────────────────────
1277 HttpVariant {
1278 name: "MovedPermanently",
1279 payload: HttpVariantPayload::Location,
1280 status: 301,
1281 },
1282 HttpVariant {
1283 name: "Found",
1284 payload: HttpVariantPayload::Location,
1285 status: 302,
1286 },
1287 HttpVariant {
1288 name: "SeeOther",
1289 payload: HttpVariantPayload::Location,
1290 status: 303,
1291 },
1292 HttpVariant {
1293 name: "TemporaryRedirect",
1294 payload: HttpVariantPayload::Location,
1295 status: 307,
1296 },
1297 HttpVariant {
1298 name: "PermanentRedirect",
1299 payload: HttpVariantPayload::Location,
1300 status: 308,
1301 },
1302 // ── 4xx client error ─────────────────────────────────────────────────
1303 HttpVariant {
1304 name: "BadRequest",
1305 payload: HttpVariantPayload::Message,
1306 status: 400,
1307 },
1308 HttpVariant {
1309 name: "Unauthorized",
1310 payload: HttpVariantPayload::None,
1311 status: 401,
1312 },
1313 HttpVariant {
1314 name: "Forbidden",
1315 payload: HttpVariantPayload::None,
1316 status: 403,
1317 },
1318 HttpVariant {
1319 name: "NotFound",
1320 payload: HttpVariantPayload::None,
1321 status: 404,
1322 },
1323 HttpVariant {
1324 name: "MethodNotAllowed",
1325 payload: HttpVariantPayload::None,
1326 status: 405,
1327 },
1328 HttpVariant {
1329 name: "NotAcceptable",
1330 payload: HttpVariantPayload::None,
1331 status: 406,
1332 },
1333 HttpVariant {
1334 name: "RequestTimeout",
1335 payload: HttpVariantPayload::None,
1336 status: 408,
1337 },
1338 HttpVariant {
1339 name: "Conflict",
1340 payload: HttpVariantPayload::Message,
1341 status: 409,
1342 },
1343 HttpVariant {
1344 name: "Gone",
1345 payload: HttpVariantPayload::None,
1346 status: 410,
1347 },
1348 HttpVariant {
1349 name: "LengthRequired",
1350 payload: HttpVariantPayload::None,
1351 status: 411,
1352 },
1353 HttpVariant {
1354 name: "PayloadTooLarge",
1355 payload: HttpVariantPayload::Message,
1356 status: 413,
1357 },
1358 HttpVariant {
1359 name: "UnsupportedMediaType",
1360 payload: HttpVariantPayload::Message,
1361 status: 415,
1362 },
1363 HttpVariant {
1364 name: "UnprocessableEntity",
1365 payload: HttpVariantPayload::Message,
1366 status: 422,
1367 },
1368 HttpVariant {
1369 name: "TooManyRequests",
1370 payload: HttpVariantPayload::Message,
1371 status: 429,
1372 },
1373 HttpVariant {
1374 name: "UnavailableForLegalReasons",
1375 payload: HttpVariantPayload::Message,
1376 status: 451,
1377 },
1378 // ── 5xx server error ─────────────────────────────────────────────────
1379 HttpVariant {
1380 name: "ServerError",
1381 payload: HttpVariantPayload::Message,
1382 status: 500,
1383 },
1384 HttpVariant {
1385 name: "NotImplemented",
1386 payload: HttpVariantPayload::Message,
1387 status: 501,
1388 },
1389 HttpVariant {
1390 name: "BadGateway",
1391 payload: HttpVariantPayload::Message,
1392 status: 502,
1393 },
1394 HttpVariant {
1395 name: "ServiceUnavailable",
1396 payload: HttpVariantPayload::Message,
1397 status: 503,
1398 },
1399 HttpVariant {
1400 name: "GatewayTimeout",
1401 payload: HttpVariantPayload::Message,
1402 status: 504,
1403 },
1404];
1405
1406/// Find an `HttpResult[T]` variant by name. Returns the variant info or
1407/// `None` if the name doesn't match.
1408pub fn http_variant(name: &str) -> Option<HttpVariant> {
1409 HTTP_VARIANTS.iter().copied().find(|v| v.name == name)
1410}
1411
1412/// Payload shape of a `QueueResult` variant (v0.44). Non-generic — a verdict
1413/// carries no value; `Retry` carries a `String` reason for the log path.
1414#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1415pub enum QueueVariantPayload {
1416 /// No payload (`Ack`).
1417 None,
1418 /// Carries a `String` reason (`Retry`).
1419 Message,
1420}
1421
1422/// One variant of the built-in `QueueResult` sum (v0.44).
1423#[derive(Debug, Clone, Copy)]
1424pub struct QueueVariant {
1425 pub name: &'static str,
1426 pub payload: QueueVariantPayload,
1427}
1428
1429/// All `QueueResult` variants, in declaration order. `Ack` confirms the
1430/// message; `Retry` redelivers it, carrying a reason for observability.
1431pub const QUEUE_VARIANTS: &[QueueVariant] = &[
1432 QueueVariant {
1433 name: "Ack",
1434 payload: QueueVariantPayload::None,
1435 },
1436 QueueVariant {
1437 name: "Retry",
1438 payload: QueueVariantPayload::Message,
1439 },
1440];
1441
1442/// Find a `QueueResult` variant by name.
1443pub fn queue_variant(name: &str) -> Option<QueueVariant> {
1444 QUEUE_VARIANTS.iter().copied().find(|v| v.name == name)
1445}
1446
1447#[derive(Debug, Clone)]
1448pub struct TypeDecl {
1449 pub name: Ident,
1450 /// `[T, U]` type parameters (v0.157, ADR 0183): empty for a non-generic
1451 /// type. A generic *record* type (`type Paginated[T] = { … }`) is the only
1452 /// generic body accepted; the checker rejects type parameters on refined /
1453 /// opaque / sum bodies. Mirrors [`FnDecl::type_params`].
1454 pub type_params: Vec<TypeParam>,
1455 pub body: TypeBody,
1456 /// Documentation block attached to this declaration (v0.3).
1457 pub documentation: Option<String>,
1458 pub span: Span,
1459 pub trivia: Trivia,
1460}
1461
1462/// The right-hand side of a `type` declaration. In v0/v0.1 only the
1463/// `Refined` variant existed; v0.2 adds records and sums; v0.3 adds opaque.
1464#[derive(Debug, Clone)]
1465pub enum TypeBody {
1466 /// Refined base type: `BaseType where refinement`.
1467 Refined {
1468 base: BaseType,
1469 base_span: Span,
1470 refinement: Option<Refinement>,
1471 },
1472 /// Record type: `{ field: T where ..., ... }`.
1473 Record(RecordBody),
1474 /// Sum type: pipe-form variants or `enum { ... }` shorthand.
1475 Sum(SumBody),
1476 /// Opaque base type: `opaque BaseType (where refinement)?` (v0.3 §3.4).
1477 /// Identity is nominal; the base type is hidden outside the defining commons.
1478 Opaque {
1479 base: BaseType,
1480 base_span: Span,
1481 refinement: Option<Refinement>,
1482 },
1483}
1484
1485/// Body of a record-type declaration (v0.2 §3.1).
1486#[derive(Debug, Clone)]
1487pub struct RecordBody {
1488 pub fields: Vec<RecordField>,
1489 pub span: Span,
1490}
1491
1492/// One field of a record type declaration. Each field may carry inline
1493/// refinement, which is enforced at construction time on the field's value.
1494#[derive(Debug, Clone)]
1495pub struct RecordField {
1496 pub name: Ident,
1497 pub type_ref: TypeRef,
1498 pub refinement: Option<Refinement>,
1499 /// v0.11: an optional initial-value expression. Only meaningful on agent
1500 /// `state` fields (the field's fresh-key value); ignored / rejected on
1501 /// record-type fields by the checker.
1502 pub init: Option<Expr>,
1503 pub span: Span,
1504}
1505
1506/// Body of a sum-type declaration (v0.2 §3.2).
1507#[derive(Debug, Clone)]
1508pub struct SumBody {
1509 pub variants: Vec<Variant>,
1510 /// v0.154 (ADR 0178): declared error embeddings — `embeds E as V, …` after
1511 /// the variants. Each says "an `E` value auto-wraps into variant `V`", which
1512 /// the `?` operator uses to convert a cross-context error without a manual
1513 /// `.mapErr`. Empty for a sum with no embeddings.
1514 pub embeds: Vec<EmbedsClause>,
1515 pub span: Span,
1516}
1517
1518/// One `embeds <source_type> as <variant>` mapping in a sum body (v0.154, ADR
1519/// 0178). Declares that a value of `source_type` can be auto-wrapped into the
1520/// named single-payload `variant` of the enclosing sum.
1521#[derive(Debug, Clone)]
1522pub struct EmbedsClause {
1523 pub source_type: TypeRef,
1524 pub variant: Ident,
1525 pub span: Span,
1526}
1527
1528/// One variant of a sum type. Variants may have payload fields; a
1529/// payload-less variant is a simple tag.
1530#[derive(Debug, Clone)]
1531pub struct Variant {
1532 pub name: Ident,
1533 pub payload: Vec<VariantField>,
1534 pub span: Span,
1535}
1536
1537/// One payload field of a sum variant. Variant payload fields use named
1538/// declarations like record fields, but do not carry refinement in v0.2.
1539#[derive(Debug, Clone)]
1540pub struct VariantField {
1541 pub name: Ident,
1542 pub type_ref: TypeRef,
1543 pub span: Span,
1544}
1545
1546#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1547pub enum BaseType {
1548 Int,
1549 String,
1550 Bool,
1551 Float,
1552 /// `Duration` (v0.86, ADR 0112) — a span of time, a distinct base type
1553 /// erased to TS `number` carrying milliseconds (the `Clock` unit). Modelled
1554 /// on `Float`: Bynk-side-only, no implicit `Int` coercion (save the one
1555 /// sanctioned clock-math mix).
1556 Duration,
1557 /// `Instant` (v0.90, ADR 0114) — an absolute point in time, a distinct base
1558 /// type erased to TS `number` carrying Unix epoch milliseconds (the
1559 /// `Clock` unit). No literal (minted by `Clock.now()`); arithmetic composes
1560 /// with `Duration` (`Instant ± Duration -> Instant`, `Instant − Instant ->
1561 /// Duration`). Supersedes ADR 0112 D4's `Int`↔`Duration` clock-math mix.
1562 Instant,
1563 /// `Bytes` (v0.110, ADR 0142) — an immutable finite octet sequence, the
1564 /// seventh base type. Unlike its neighbours it does **not** erase to TS
1565 /// `number`: a `Bytes` lowers to a `Uint8Array`. No source literal
1566 /// (constructed via `Bytes.fromUtf8`/`fromBase64`/`empty`); `==` compares
1567 /// by content (real emitter codegen, not host `===`); wires as a base64
1568 /// JSON string; not `Map`-keyable and not orderable.
1569 Bytes,
1570}
1571
1572impl BaseType {
1573 pub fn name(self) -> &'static str {
1574 match self {
1575 BaseType::Int => "Int",
1576 BaseType::String => "String",
1577 BaseType::Bool => "Bool",
1578 BaseType::Float => "Float",
1579 BaseType::Duration => "Duration",
1580 BaseType::Instant => "Instant",
1581 BaseType::Bytes => "Bytes",
1582 }
1583 }
1584}
1585
1586/// A `Duration` literal unit (v0.86, ADR 0112) — the closed set of suffixes in a
1587/// `<int>.<unit>` literal. Each maps to a fixed millisecond factor (`Duration`
1588/// erases to `Int` milliseconds).
1589#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1590pub enum DurationUnit {
1591 Milliseconds,
1592 Seconds,
1593 Minutes,
1594 Hours,
1595 Days,
1596}
1597
1598impl DurationUnit {
1599 /// Resolve a unit name (`minutes`) to its variant, or `None` if it is not one
1600 /// of the closed set. Used by the parser to recognise an `<int>.<unit>`
1601 /// literal; an unrecognised name leaves the expression a field access.
1602 pub fn from_name(name: &str) -> Option<Self> {
1603 Some(match name {
1604 "milliseconds" => DurationUnit::Milliseconds,
1605 "seconds" => DurationUnit::Seconds,
1606 "minutes" => DurationUnit::Minutes,
1607 "hours" => DurationUnit::Hours,
1608 "days" => DurationUnit::Days,
1609 _ => return None,
1610 })
1611 }
1612
1613 /// The unit name as written.
1614 pub fn name(self) -> &'static str {
1615 match self {
1616 DurationUnit::Milliseconds => "milliseconds",
1617 DurationUnit::Seconds => "seconds",
1618 DurationUnit::Minutes => "minutes",
1619 DurationUnit::Hours => "hours",
1620 DurationUnit::Days => "days",
1621 }
1622 }
1623
1624 /// The unit's value in milliseconds.
1625 pub fn millis(self) -> i64 {
1626 match self {
1627 DurationUnit::Milliseconds => 1,
1628 DurationUnit::Seconds => 1_000,
1629 DurationUnit::Minutes => 60_000,
1630 DurationUnit::Hours => 3_600_000,
1631 DurationUnit::Days => 86_400_000,
1632 }
1633 }
1634}
1635
1636/// An integer refinement bound (v0.40, ADR 0073): the parsed value plus the
1637/// bound's source span (covering a leading `-`). Value-only beyond the span —
1638/// ints have one canonical printed form, so the formatter stays idempotent
1639/// without a stored lexeme. The span backs the `InRange`-swap quick-fix.
1640#[derive(Debug, Clone)]
1641pub struct IntBound {
1642 pub value: i64,
1643 pub span: Span,
1644}
1645
1646/// A float refinement bound (v0.21): the parsed value plus the signed source
1647/// lexeme (for byte-stable emission). v0.40 (ADR 0073): also the source span,
1648/// for the `InRange`-swap quick-fix.
1649#[derive(Debug, Clone)]
1650pub struct FloatBound {
1651 pub value: f64,
1652 pub lexeme: String,
1653 pub span: Span,
1654}
1655
1656#[derive(Debug, Clone)]
1657pub struct Refinement {
1658 pub predicates: Vec<RefinementPred>,
1659 pub span: Span,
1660}
1661
1662#[derive(Debug, Clone)]
1663pub struct RefinementPred {
1664 pub kind: PredKind,
1665 pub span: Span,
1666}
1667
1668#[derive(Debug, Clone)]
1669pub enum PredKind {
1670 Matches(String),
1671 InRange(IntBound, IntBound),
1672 /// `InRange` with float bounds (v0.21) — a separate variant so every
1673 /// `Int` refinement path stays untouched. Bounds keep their source
1674 /// lexemes (including any sign) so emitted runtime checks are
1675 /// byte-stable.
1676 InRangeF(FloatBound, FloatBound),
1677 MinLength(i64),
1678 MaxLength(i64),
1679 Length(i64),
1680 NonNegative,
1681 Positive,
1682 NonEmpty,
1683}
1684
1685impl PredKind {
1686 pub fn name(&self) -> &'static str {
1687 match self {
1688 PredKind::Matches(_) => "Matches",
1689 PredKind::InRange(..) | PredKind::InRangeF(..) => "InRange",
1690 PredKind::MinLength(_) => "MinLength",
1691 PredKind::MaxLength(_) => "MaxLength",
1692 PredKind::Length(_) => "Length",
1693 PredKind::NonNegative => "NonNegative",
1694 PredKind::Positive => "Positive",
1695 PredKind::NonEmpty => "NonEmpty",
1696 }
1697 }
1698}
1699
1700/// A function type parameter (v0.20a, `fn name[A, B](…)`). A struct rather
1701/// than a bare Ident so the ADR-0028 "bound-capable" promise is a later field
1702/// addition, not a representation change.
1703#[derive(Debug, Clone)]
1704pub struct TypeParam {
1705 pub name: Ident,
1706 pub span: Span,
1707}
1708
1709/// A lambda expression (v0.20a): `(params) => expr` or `(params) => { … }`.
1710/// `=>` is the value arrow (shared with `match`); param annotations are
1711/// optional where an expected function type supplies them.
1712#[derive(Debug, Clone)]
1713pub struct LambdaExpr {
1714 pub params: Vec<LambdaParam>,
1715 pub body: Box<Expr>,
1716 pub span: Span,
1717}
1718
1719/// A lambda parameter. A separate type from [`Param`] because its annotation
1720/// is optional — `Param.type_ref` stays mandatory at every signature site.
1721#[derive(Debug, Clone)]
1722pub struct LambdaParam {
1723 pub name: Ident,
1724 pub type_ref: Option<TypeRef>,
1725 pub span: Span,
1726}
1727
1728#[derive(Debug, Clone)]
1729pub struct FnDecl {
1730 /// v0.20a: `[A, B]` type parameters; empty for non-generic functions.
1731 pub type_params: Vec<TypeParam>,
1732 /// Free function or method (`TypeName.methodName`). See [`FnName`].
1733 pub name: FnName,
1734 pub params: Vec<Param>,
1735 pub return_type: TypeRef,
1736 /// v0.115: preconditions (`requires <name>: <pred>`), parsed between the
1737 /// return type and the body. A contract clause is the invariant predicate
1738 /// attached to a function (ADR 0144 — one predicate surface); `requires`
1739 /// scopes over the parameters only.
1740 pub requires: Vec<Contract>,
1741 /// v0.115: postconditions (`ensures <name>: <pred>`). Scopes over the
1742 /// parameters *and* `result`, the contextual binding for the return value.
1743 pub ensures: Vec<Contract>,
1744 pub body: Block,
1745 /// True when the first parameter is the special `self` parameter. Only
1746 /// valid for method declarations.
1747 pub has_self: bool,
1748 /// Documentation block attached to this declaration (v0.3).
1749 pub documentation: Option<String>,
1750 pub span: Span,
1751 pub trivia: Trivia,
1752}
1753
1754/// A function-declaration name: either a free function `f` or a method
1755/// `T.method` (v0.2 §3.6).
1756#[derive(Debug, Clone)]
1757pub enum FnName {
1758 /// `fn name(...)` — a free function.
1759 Free(Ident),
1760 /// `fn TypeName.methodName(...)` — a method attached to a type.
1761 Method {
1762 type_name: Ident,
1763 method_name: Ident,
1764 },
1765}
1766
1767impl FnName {
1768 /// The function's short name for diagnostics. For methods returns the
1769 /// method portion only; the type prefix is recovered via `type_name`.
1770 pub fn ident(&self) -> &Ident {
1771 match self {
1772 FnName::Free(id) => id,
1773 FnName::Method { method_name, .. } => method_name,
1774 }
1775 }
1776
1777 /// For methods, the attached type's identifier; `None` for free fns.
1778 pub fn type_name(&self) -> Option<&Ident> {
1779 match self {
1780 FnName::Free(_) => None,
1781 FnName::Method { type_name, .. } => Some(type_name),
1782 }
1783 }
1784
1785 /// The displayed full name (e.g., `Money.add` or `parseSku`).
1786 pub fn display(&self) -> String {
1787 match self {
1788 FnName::Free(id) => id.name.clone(),
1789 FnName::Method {
1790 type_name,
1791 method_name,
1792 } => format!("{}.{}", type_name.name, method_name.name),
1793 }
1794 }
1795}
1796
1797/// A brace-delimited block of statements ending in a tail expression
1798/// whose value is the block's value (spec v0.1 §3.1).
1799#[derive(Debug, Clone)]
1800pub struct Block {
1801 pub statements: Vec<Statement>,
1802 pub tail: Box<Expr>,
1803 pub span: Span,
1804 /// Line comments that appear between the last statement (or the
1805 /// opening brace) and the tail expression. Preserved here because
1806 /// expressions do not carry trivia in v1.1.
1807 pub tail_leading_comments: Vec<String>,
1808 /// `true` when the block was written with no explicit tail expression and
1809 /// the parser synthesised a `()` (unit) tail (v0.146, ADR 0170). The tail
1810 /// is a real `ExprKind::UnitLit` either way; this flag records that it was
1811 /// *implicit* so the formatter can omit it (Bynk has no statement
1812 /// terminator, so a printed `()` would re-attach to the last statement on
1813 /// re-parse — `x` `()` → `x()`). The parser re-derives the implicit unit
1814 /// tail, so omitting it is loss-free.
1815 pub implicit_tail: bool,
1816}
1817
1818impl Block {
1819 /// Whether this block is a synthesised empty unit block — no statements and
1820 /// an *implicit* `()` tail (v0.146, ADR 0170). This is exactly the shape the
1821 /// parser inserts for an `if` with no `else` branch, so both the checker
1822 /// (gating the else-less form to unit) and the formatter (omitting the
1823 /// synthetic `else { () }`) recognise it here.
1824 pub fn is_synth_unit(&self) -> bool {
1825 self.statements.is_empty()
1826 && self.implicit_tail
1827 && matches!(self.tail.kind, ExprKind::UnitLit)
1828 }
1829}
1830
1831/// Block-level statement.
1832#[derive(Debug, Clone)]
1833pub enum Statement {
1834 /// `let name (: T)? = expr` — pure binding (v0.1).
1835 Let(LetStmt),
1836 /// `let name (: T)? <- expr` — effectful binding (v0.5).
1837 EffectLet(LetStmt),
1838 /// `expect expr` — verify a Bool predicate at test runtime (v0.7; renamed
1839 /// from `assert` in v0.112). Only valid inside test case bodies.
1840 Expect(ExpectStmt),
1841 /// `~> expr` — an asynchronous fire-and-forget send (v0.79). The caller does
1842 /// not await the reply; legal only when the reply is `Effect[()]`. No binder.
1843 Send(SendStmt),
1844 /// `do expr` — an effect-performing expression statement (v0.146, ADR 0170).
1845 /// Runs an `Effect[()]` and discards its (unit) result — the binder-free
1846 /// sugar for `let _ <- expr` when the awaited value is unit. Legal only in
1847 /// an effectful body; the operand MUST be `Effect[()]` (a valued reply keeps
1848 /// the explicit `let _ <- e`, so throwing away a real value stays visible).
1849 Do(DoStmt),
1850 /// `name := expr` — a `Cell` store write (v0.81, storage track). The
1851 /// unconditional write form; `.update(fn)` (a method call) is the
1852 /// read-modify-write form. ADR 0108.
1853 Assign(AssignStmt),
1854}
1855
1856impl Statement {
1857 pub fn span(&self) -> Span {
1858 match self {
1859 Statement::Let(l) | Statement::EffectLet(l) => l.span,
1860 Statement::Expect(a) => a.span,
1861 Statement::Send(s) => s.span,
1862 Statement::Do(d) => d.span,
1863 Statement::Assign(a) => a.span,
1864 }
1865 }
1866}
1867
1868#[derive(Debug, Clone)]
1869pub struct ExpectStmt {
1870 pub value: Expr,
1871 pub span: Span,
1872 pub trivia: Trivia,
1873}
1874
1875/// `name := expr` — a `Cell` store write (v0.81, storage track). `target` is the
1876/// `Cell` field being written (a bare name for now; the checker resolves it to a
1877/// `store` field). `value` is the new value.
1878#[derive(Debug, Clone)]
1879pub struct AssignStmt {
1880 pub target: Ident,
1881 pub value: Expr,
1882 pub span: Span,
1883 pub trivia: Trivia,
1884}
1885
1886#[derive(Debug, Clone)]
1887pub struct LetStmt {
1888 pub name: Ident,
1889 pub type_annot: Option<TypeRef>,
1890 pub value: Expr,
1891 /// v0.182 (#664): the call-site `by <Actor>(<identity>)` clause on an
1892 /// `EffectLet` whose value addresses a test service handler. `None` on a pure
1893 /// `Let` (the `by` is parsed only in the `<-` arm) and on an effect-let with
1894 /// no principal.
1895 pub principal: Option<CallSiteActor>,
1896 pub span: Span,
1897 pub trivia: Trivia,
1898}
1899
1900#[derive(Debug, Clone)]
1901pub struct SendStmt {
1902 /// The send target — a recipient call, e.g. `Logger.info(msg)`.
1903 pub value: Expr,
1904 pub span: Span,
1905 pub trivia: Trivia,
1906}
1907
1908/// `do expr` — an effect-performing expression statement (v0.146, ADR 0170).
1909/// `value` is the awaited effect, which MUST be `Effect[()]`.
1910#[derive(Debug, Clone)]
1911pub struct DoStmt {
1912 pub value: Expr,
1913 pub span: Span,
1914 pub trivia: Trivia,
1915}
1916
1917#[derive(Debug, Clone)]
1918pub struct Param {
1919 pub name: Ident,
1920 pub type_ref: TypeRef,
1921 pub span: Span,
1922}
1923
1924#[derive(Debug, Clone)]
1925pub enum TypeRef {
1926 Base(BaseType, Span),
1927 Named(Ident),
1928 /// `Result[T, E]` — the built-in generic Result type (v0.1).
1929 Result(Box<TypeRef>, Box<TypeRef>, Span),
1930 /// `Option[T]` — the built-in generic Option type (v0.2).
1931 Option(Box<TypeRef>, Span),
1932 /// `Effect[T]` — the built-in generic Effect type (v0.5).
1933 Effect(Box<TypeRef>, Span),
1934 /// `HttpResult[T]` — the built-in HTTP-result sum (v0.9).
1935 HttpResult(Box<TypeRef>, Span),
1936 /// `QueueResult` — the built-in queue verdict sum (`Ack | Retry`),
1937 /// non-generic; the required return of a queue handler (v0.44).
1938 QueueResult(Span),
1939 /// `List[T]` — the built-in generic immutable list type (v0.20b).
1940 List(Box<TypeRef>, Span),
1941 /// `Map[K, V]` — the built-in generic immutable map type (v0.20b).
1942 /// Keys are confined to value-keyable types
1943 /// (`bynk.types.unkeyable_map_key`).
1944 Map(Box<TypeRef>, Box<TypeRef>, Span),
1945 /// `Query[T]` — the built-in lazy storage-read description (v0.91, ADR 0115).
1946 /// Nameable in a pure helper's return type; non-storable and non-boundary
1947 /// (like `Effect`/`Fn`).
1948 Query(Box<TypeRef>, Span),
1949 /// `Stream[T]` — the value-over-time primitive (v0.100, real-time track
1950 /// slice 0). A lazy, pull-shaped sequence produced over time; non-storable
1951 /// and non-boundary (like `Query`/`Effect`/`Fn`).
1952 Stream(Box<TypeRef>, Span),
1953 /// `Connection[F]` — a held WebSocket connection (v0.102, real-time track
1954 /// slice 2). `F` is the server→client frame type. A `Held` resource:
1955 /// non-serialisable, non-boundary, and governed by the linearity discipline
1956 /// (§2.9); storable only in `Cell[Option[Connection]]` / `Map[K, Connection]`.
1957 Connection(Box<TypeRef>, Span),
1958 /// `History[Agent]` — a generated, driven call-history of an agent (v0.119,
1959 /// testing track slice 7, ADR 0155). A test-only generator, legal only in
1960 /// `for all` binding position inside a `property`; it is not a value type,
1961 /// so it never resolves in a field/param/return position. The bound subject
1962 /// behaves as an ordinary `List[Step]`.
1963 History(Box<TypeRef>, Span),
1964 /// `ValidationError` — the built-in error type used by refined-type
1965 /// constructors (v0.1).
1966 ValidationError(Span),
1967 /// `JsonError` — the built-in JSON-decode error type (v0.22b). A
1968 /// uniform record (`kind`/`path`/`message`, all `String`) the codec
1969 /// maps `BoundaryError` variants and parse failures into.
1970 JsonError(Span),
1971 /// `()` — the unit type (v0.5).
1972 Unit(Span),
1973 /// `A -> B` / `(A, B) -> C` / `() -> B` — a function type (v0.20a).
1974 /// Right-associative; effectful iff the return type is `Effect[_]`
1975 /// (the structural rule). Confined to non-boundary positions
1976 /// (`bynk.types.function_at_boundary`).
1977 Fn(Vec<TypeRef>, Box<TypeRef>, Span),
1978 /// `Name[Arg, …]` — an application of a user-declared generic type
1979 /// (v0.157, ADR 0183). `name` is a user type name (never a built-in
1980 /// generic, which each have a dedicated variant above). Arity and the
1981 /// existence of the referenced type are checked in the resolver.
1982 App {
1983 name: Ident,
1984 args: Vec<TypeRef>,
1985 span: Span,
1986 },
1987}
1988
1989impl TypeRef {
1990 pub fn span(&self) -> Span {
1991 match self {
1992 TypeRef::Base(_, s) => *s,
1993 TypeRef::Named(id) => id.span,
1994 TypeRef::Result(_, _, s) => *s,
1995 TypeRef::Option(_, s) => *s,
1996 TypeRef::Effect(_, s) => *s,
1997 TypeRef::HttpResult(_, s) => *s,
1998 TypeRef::QueueResult(s) => *s,
1999 TypeRef::List(_, s) => *s,
2000 TypeRef::Map(_, _, s) => *s,
2001 TypeRef::Query(_, s) => *s,
2002 TypeRef::Stream(_, s) => *s,
2003 TypeRef::Connection(_, s) => *s,
2004 TypeRef::History(_, s) => *s,
2005 TypeRef::ValidationError(s) => *s,
2006 TypeRef::JsonError(s) => *s,
2007 TypeRef::Unit(s) => *s,
2008 TypeRef::Fn(_, _, s) => *s,
2009 TypeRef::App { span, .. } => *span,
2010 }
2011 }
2012}
2013
2014/// v0.174 (#592): does the generic record type `name` transitively contain a
2015/// reference to itself — through any field-type path, including collection and
2016/// `Option` wrappers, sum-variant payloads, and generic type arguments? Such a
2017/// type has no finite set of monomorphised boundary codecs: uniform recursion
2018/// (`Node[T] = { next: Option[Node[T]] }`) would need a self-referential codec
2019/// chain the per-instantiation model does not yet generate, and polymorphic
2020/// recursion (`Weird[T] = { next: Option[Weird[List[T]]] }`) an unbounded set of
2021/// instantiations. Both are rejected at a boundary
2022/// (`bynk.generics.recursive_generic_at_boundary`).
2023///
2024/// Detection is reachability over the type-containment graph: `name` is
2025/// recursive iff it is reachable from its own body, following every named /
2026/// applied head and descending into every wrapper, map/result pair, function
2027/// position, and generic argument. Terminates via the `visited` set.
2028pub fn generic_record_is_recursive(
2029 name: &str,
2030 types: &std::collections::HashMap<String, TypeDecl>,
2031) -> bool {
2032 fn heads(t: &TypeRef, out: &mut Vec<String>) {
2033 match t {
2034 TypeRef::Named(id) => out.push(id.name.clone()),
2035 TypeRef::App {
2036 name: app_name,
2037 args,
2038 ..
2039 } => {
2040 out.push(app_name.name.clone());
2041 for a in args {
2042 heads(a, out);
2043 }
2044 }
2045 TypeRef::Option(a, _)
2046 | TypeRef::List(a, _)
2047 | TypeRef::Effect(a, _)
2048 | TypeRef::HttpResult(a, _)
2049 | TypeRef::Query(a, _)
2050 | TypeRef::Stream(a, _)
2051 | TypeRef::Connection(a, _)
2052 | TypeRef::History(a, _) => heads(a, out),
2053 TypeRef::Result(a, b, _) | TypeRef::Map(a, b, _) => {
2054 heads(a, out);
2055 heads(b, out);
2056 }
2057 TypeRef::Fn(ps, r, _) => {
2058 for p in ps {
2059 heads(p, out);
2060 }
2061 heads(r, out);
2062 }
2063 TypeRef::Base(..)
2064 | TypeRef::QueueResult(_)
2065 | TypeRef::ValidationError(_)
2066 | TypeRef::JsonError(_)
2067 | TypeRef::Unit(_) => {}
2068 }
2069 }
2070 fn body_heads(decl: &TypeDecl, out: &mut Vec<String>) {
2071 match &decl.body {
2072 TypeBody::Record(r) => {
2073 for f in &r.fields {
2074 heads(&f.type_ref, out);
2075 }
2076 }
2077 TypeBody::Sum(s) => {
2078 for v in &s.variants {
2079 for p in &v.payload {
2080 heads(&p.type_ref, out);
2081 }
2082 }
2083 }
2084 TypeBody::Refined { .. } | TypeBody::Opaque { .. } => {}
2085 }
2086 }
2087 let Some(root) = types.get(name) else {
2088 return false;
2089 };
2090 let mut visited: std::collections::HashSet<String> = std::collections::HashSet::new();
2091 let mut stack: Vec<String> = Vec::new();
2092 body_heads(root, &mut stack);
2093 while let Some(n) = stack.pop() {
2094 if n == name {
2095 return true;
2096 }
2097 if !visited.insert(n.clone()) {
2098 continue;
2099 }
2100 if let Some(decl) = types.get(&n) {
2101 body_heads(decl, &mut stack);
2102 }
2103 }
2104 false
2105}
2106
2107#[derive(Debug, Clone)]
2108pub struct Expr {
2109 pub kind: ExprKind,
2110 pub span: Span,
2111}
2112
2113impl ExprKind {
2114 /// Construct an `IntLit` for a *synthesized* integer — one the compiler
2115 /// invents rather than reading from source (a default `1`, a computed bound).
2116 /// The lexeme is the canonical decimal form (no separators). Source-parsed
2117 /// literals keep their as-written lexeme instead (v0.142, ADR 0166).
2118 pub fn int_lit(value: i64) -> ExprKind {
2119 ExprKind::IntLit {
2120 value,
2121 lexeme: value.to_string(),
2122 }
2123 }
2124}
2125
2126#[derive(Debug, Clone)]
2127pub enum ExprKind {
2128 /// An integer literal (typed `Int`). The lexeme is kept alongside the parsed
2129 /// value (v0.142, ADR 0166) so formatting is byte-stable: an author's `_`
2130 /// digit separators (`1_048_576`) survive a round-trip, mirroring the
2131 /// `FloatLit` treatment. The value is separator-free; emission lowers the
2132 /// value, so emitted output is unaffected.
2133 IntLit {
2134 value: i64,
2135 lexeme: String,
2136 },
2137 /// A float literal (v0.21). The lexeme is kept alongside the parsed
2138 /// value so emission and formatting are byte-stable (`1e10` must not
2139 /// normalise to `10000000000`).
2140 FloatLit {
2141 value: f64,
2142 lexeme: String,
2143 },
2144 /// A duration literal `<int>.<unit>` (v0.86, ADR 0112): `5.minutes`,
2145 /// `30.days`. The parser recognises the `IntLit . <unit>` shape and records
2146 /// the magnitude, the unit, and the resolved milliseconds (the value the
2147 /// emitter lowers to). Typed `Duration`.
2148 DurationLit {
2149 /// The integer magnitude as written (`5` in `5.minutes`).
2150 value: i64,
2151 /// The unit name (`minutes`), one of the closed set.
2152 unit: DurationUnit,
2153 /// The value in milliseconds — `value * unit factor`.
2154 millis: i64,
2155 },
2156 StrLit(String),
2157 /// An interpolated string `"… \(expr) …"` (v0.43, ADR 0075). Chunks and
2158 /// holes alternate. A plain `"…"` with no holes stays [`ExprKind::StrLit`],
2159 /// so existing code and the emitter/formatter fast-path are untouched.
2160 InterpStr(Vec<InterpPart>),
2161 BoolLit(bool),
2162 Ident(Ident),
2163 Call {
2164 name: Ident,
2165 /// v0.20a: explicit type arguments (`name[T](…)`); empty when absent.
2166 type_args: Vec<TypeRef>,
2167 args: Vec<Expr>,
2168 },
2169 /// A lambda (v0.20a). See [`LambdaExpr`].
2170 Lambda(LambdaExpr),
2171 BinOp(BinOp, Box<Expr>, Box<Expr>),
2172 UnaryOp(UnaryOp, Box<Expr>),
2173 Paren(Box<Expr>),
2174 /// `{ stmts; expr }` — block expression (v0.1).
2175 Block(Block),
2176 /// `if cond { then } else { else }` (v0.1).
2177 If {
2178 cond: Box<Expr>,
2179 then_block: Box<Block>,
2180 else_block: Box<Block>,
2181 },
2182 /// `Ok(value)` — Result success constructor (v0.1).
2183 Ok(Box<Expr>),
2184 /// `Err(error)` — Result failure constructor (v0.1).
2185 Err(Box<Expr>),
2186 /// `expr?` — propagation operator (v0.1).
2187 Question(Box<Expr>),
2188 /// `TypeName.method(args)` — qualified static call on a type
2189 /// (v0.1: only refined-type `of`; v0.2: any static method or variant
2190 /// constructor for sum types). The resolver decides which.
2191 ConstructorCall {
2192 type_name: Ident,
2193 method: Ident,
2194 args: Vec<Expr>,
2195 },
2196 /// `TypeName { field: value, ... }` — record construction (v0.2).
2197 RecordConstruction {
2198 type_name: Ident,
2199 fields: Vec<FieldInit>,
2200 },
2201 /// `receiver.field` — field access on a record value (v0.2). v0.3 adds
2202 /// `.raw` on opaque types within the defining commons.
2203 FieldAccess {
2204 receiver: Box<Expr>,
2205 field: Ident,
2206 },
2207 /// `receiver.method(args)` — instance method call (v0.2). The
2208 /// resolver determines the receiver's type and looks up the method.
2209 MethodCall {
2210 receiver: Box<Expr>,
2211 method: Ident,
2212 /// v0.22b: explicit type arguments on a qualified static
2213 /// (`Json.decode[T](…)`); empty when absent. The same-line-`[`
2214 /// rule applies as for `Call` type application (0039).
2215 type_args: Vec<TypeRef>,
2216 args: Vec<Expr>,
2217 },
2218 /// `match disc { arm+ }` — pattern matching (v0.2).
2219 Match {
2220 discriminant: Box<Expr>,
2221 arms: Vec<MatchArm>,
2222 },
2223 /// `expr is pattern` — pattern test, returns Bool (v0.2).
2224 Is {
2225 value: Box<Expr>,
2226 pattern: Pattern,
2227 },
2228 /// `Some(value)` — Option Some constructor (v0.2).
2229 Some(Box<Expr>),
2230 /// `None` — Option None constructor (v0.2).
2231 None,
2232 /// `()` — unit literal (v0.5).
2233 UnitLit,
2234 /// `TypeName { ...base, field: value, ... }` or `{ ...base, ... }` —
2235 /// record spread expression (v0.5).
2236 RecordSpread {
2237 /// Optional type prefix (`TypeName { ...base }`). Absent for the
2238 /// bare form used inside `commit`.
2239 type_name: Option<Ident>,
2240 /// The base record being spread.
2241 base: Box<Expr>,
2242 /// Field overrides (always full `name: value` form — never shorthand).
2243 overrides: Vec<FieldInit>,
2244 },
2245 /// `Effect.pure(value)` — wrap a synchronous value into `Effect[T]`
2246 /// (v0.5). Recognised in the parser as a special-form.
2247 EffectPure(Box<Expr>),
2248 /// `expect expr` — expectation as an expression of type `()` (v0.9.1;
2249 /// renamed from `assert` in v0.112). Valid only inside test bodies. Evaluates
2250 /// `expr` (must be Bool); if false, the surrounding test case fails.
2251 Expect(Box<Expr>),
2252 /// `Val[T]`, `Val[T](args)` — test-context value construction (v0.9.4).
2253 /// `args` is empty for the bare form and holds the pin arguments for
2254 /// `Val[T](...)`. The record-override form `Val[T] { ... }` is not yet
2255 /// parsed. Valid only inside test bodies; has type `T`.
2256 Val {
2257 type_ref: TypeRef,
2258 args: Vec<Expr>,
2259 },
2260 /// `[a, b, c]` — list literal (v0.20b). An empty `[]` requires an
2261 /// expected type (`bynk.types.uninferable_element_type`).
2262 ListLit(Vec<Expr>),
2263 /// An observation over a consumed capability's recorded calls (v0.117,
2264 /// testing track slice 5). The direct subject of an `expect` in a `case`
2265 /// body — `expect Cap.op called once with <pred>`, `expect Cap.op never
2266 /// called`, `expect A.op before B.op`. Types as `Bool` (the claim about the
2267 /// recorded trace), lowered to a boolean over the recorded log.
2268 Observation(ObservationExpr),
2269 /// `trace(Cap.op)` — the bound-trace escape hatch (v0.117, testing track
2270 /// slice 5). Yields the recorded calls of `Cap.op` as a `List[<CallRecord>]`
2271 /// (a synthetic record of the operation's parameters), asserted over with the
2272 /// ordinary value surface. Test-body-only, like [`ExprKind::Val`].
2273 Trace {
2274 cap: Ident,
2275 op: Ident,
2276 },
2277}
2278
2279/// Every directly-nested sub-expression of `e` — the **total** child
2280/// iterator. The match is exhaustive (no `_` arm), so adding an [`ExprKind`]
2281/// variant is a compile error here rather than a silently incomplete walk —
2282/// the trap the checker's three hand-rolled partial walkers each fell into
2283/// (block statements and match-arm bodies were skipped, so e.g. the `:=`
2284/// self-reference rule was bypassable through a match arm).
2285///
2286/// Descends one level: block *statements* and the tail, match-arm bodies,
2287/// lambda bodies, interpolation holes, record-field values, and observation
2288/// predicates are all children. Callers recurse for a deep walk.
2289pub fn expr_children(e: &Expr) -> Vec<&Expr> {
2290 fn block_children<'a>(b: &'a Block, out: &mut Vec<&'a Expr>) {
2291 for s in &b.statements {
2292 statement_exprs(s, out);
2293 }
2294 out.push(&b.tail);
2295 }
2296 let mut out = Vec::new();
2297 match &e.kind {
2298 ExprKind::IntLit { .. }
2299 | ExprKind::FloatLit { .. }
2300 | ExprKind::DurationLit { .. }
2301 | ExprKind::StrLit(_)
2302 | ExprKind::BoolLit(_)
2303 | ExprKind::Ident(_)
2304 | ExprKind::None
2305 | ExprKind::UnitLit
2306 | ExprKind::Trace { .. } => {}
2307 ExprKind::InterpStr(parts) => {
2308 for p in parts {
2309 if let InterpPart::Hole(h) = p {
2310 out.push(h.as_ref());
2311 }
2312 }
2313 }
2314 ExprKind::Call { args, .. }
2315 | ExprKind::ConstructorCall { args, .. }
2316 | ExprKind::Val { args, .. }
2317 | ExprKind::ListLit(args) => out.extend(args.iter()),
2318 ExprKind::Lambda(l) => out.push(l.body.as_ref()),
2319 ExprKind::BinOp(_, l, r) => {
2320 out.push(l.as_ref());
2321 out.push(r.as_ref());
2322 }
2323 ExprKind::UnaryOp(_, inner)
2324 | ExprKind::Paren(inner)
2325 | ExprKind::Ok(inner)
2326 | ExprKind::Err(inner)
2327 | ExprKind::Question(inner)
2328 | ExprKind::Some(inner)
2329 | ExprKind::EffectPure(inner)
2330 | ExprKind::Expect(inner) => out.push(inner.as_ref()),
2331 ExprKind::Block(b) => block_children(b, &mut out),
2332 ExprKind::If {
2333 cond,
2334 then_block,
2335 else_block,
2336 } => {
2337 out.push(cond.as_ref());
2338 block_children(then_block, &mut out);
2339 block_children(else_block, &mut out);
2340 }
2341 ExprKind::RecordConstruction { fields, .. } => {
2342 out.extend(fields.iter().filter_map(|f| f.value.as_ref()));
2343 }
2344 ExprKind::FieldAccess { receiver, .. } => out.push(receiver.as_ref()),
2345 ExprKind::MethodCall { receiver, args, .. } => {
2346 out.push(receiver.as_ref());
2347 out.extend(args.iter());
2348 }
2349 ExprKind::Match { discriminant, arms } => {
2350 out.push(discriminant.as_ref());
2351 for arm in arms {
2352 match &arm.body {
2353 MatchBody::Expr(e) => out.push(e),
2354 MatchBody::Block(b) => block_children(b, &mut out),
2355 }
2356 }
2357 }
2358 ExprKind::Is { value, .. } => out.push(value.as_ref()),
2359 ExprKind::RecordSpread {
2360 base, overrides, ..
2361 } => {
2362 out.push(base.as_ref());
2363 out.extend(overrides.iter().filter_map(|f| f.value.as_ref()));
2364 }
2365 ExprKind::Observation(obs) => match &obs.matcher {
2366 ObservationMatcher::Called { count, with_pred } => {
2367 if let Some(c) = count {
2368 out.push(c.as_ref());
2369 }
2370 if let Some(p) = with_pred {
2371 out.push(p.as_ref());
2372 }
2373 }
2374 ObservationMatcher::NeverCalled | ObservationMatcher::Before { .. } => {}
2375 },
2376 }
2377 out
2378}
2379
2380/// The expressions directly contained in a statement — the statement half of
2381/// [`expr_children`]'s total walk. Exhaustive over [`Statement`] for the same
2382/// reason.
2383pub fn statement_exprs<'a>(s: &'a Statement, out: &mut Vec<&'a Expr>) {
2384 match s {
2385 Statement::Let(l) | Statement::EffectLet(l) => out.push(&l.value),
2386 Statement::Expect(a) => out.push(&a.value),
2387 Statement::Send(snd) => out.push(&snd.value),
2388 Statement::Do(d) => out.push(&d.value),
2389 Statement::Assign(a) => out.push(&a.value),
2390 }
2391}
2392
2393/// An observation of a capability operation's recorded calls (v0.117, testing
2394/// track slice 5). `cap`/`op` name the seam (`Logger.log`); `matcher` is the
2395/// claim about the recorded calls.
2396#[derive(Debug, Clone)]
2397pub struct ObservationExpr {
2398 pub cap: Ident,
2399 pub op: Ident,
2400 pub matcher: ObservationMatcher,
2401}
2402
2403/// The claim an [`ObservationExpr`] makes about a seam's recorded calls (v0.117).
2404#[derive(Debug, Clone)]
2405pub enum ObservationMatcher {
2406 /// `called` [`once` | `<n> times`]? [`with` `<pred>`]?. `count` is `None`
2407 /// for a bare `called` (at least one); `Some(expr)` is the exact-count claim
2408 /// (a literal; `once` desugars to `1`). `with_pred` matches a call whose
2409 /// arguments (in scope by the operation's parameter names) satisfy it.
2410 Called {
2411 count: Option<Box<Expr>>,
2412 with_pred: Option<Box<Expr>>,
2413 },
2414 /// `never called` — zero calls.
2415 NeverCalled,
2416 /// `before Cap.op` — the first call of the subject precedes the first call
2417 /// of the named operation (both must have occurred).
2418 Before { cap: Ident, op: Ident },
2419}
2420
2421/// One part of an interpolated string (v0.43, ADR 0075). An
2422/// [`ExprKind::InterpStr`] holds an alternating run of these.
2423#[derive(Debug, Clone)]
2424pub enum InterpPart {
2425 /// Literal text between holes, with escapes already resolved.
2426 Chunk(String),
2427 /// An interpolated expression `\(expr)`. Type-checked by the hole rule
2428 /// (base scalars only; see the checker) and lowered into a template-
2429 /// literal `${…}` slot.
2430 Hole(Box<Expr>),
2431}
2432
2433/// One field-initialiser inside a record construction expression:
2434/// either `name: expr` or the shorthand `name` (which requires a binding
2435/// of the same name in scope and uses its value).
2436#[derive(Debug, Clone)]
2437pub struct FieldInit {
2438 pub name: Ident,
2439 /// `None` means shorthand — the field's value is the same-named binding.
2440 pub value: Option<Expr>,
2441 pub span: Span,
2442}
2443
2444/// One arm of a `match` expression: `pattern => body` or, with a guard,
2445/// `pattern if guard => body` (guard added in the nested-patterns increment,
2446/// ADR 0169). A guarded arm matches only when the pattern matches **and** the
2447/// `Bool` guard evaluates true; it never contributes to exhaustiveness.
2448#[derive(Debug, Clone)]
2449pub struct MatchArm {
2450 pub pattern: Pattern,
2451 /// Optional `if <Bool-expr>` guard between the pattern and `=>`.
2452 pub guard: Option<Expr>,
2453 pub body: MatchBody,
2454 pub span: Span,
2455}
2456
2457/// The right-hand side of a match arm — either a single expression or
2458/// a block.
2459#[derive(Debug, Clone)]
2460pub enum MatchBody {
2461 Expr(Expr),
2462 Block(Block),
2463}
2464
2465impl MatchBody {
2466 pub fn span(&self) -> Span {
2467 match self {
2468 MatchBody::Expr(e) => e.span,
2469 MatchBody::Block(b) => b.span,
2470 }
2471 }
2472}
2473
2474/// A pattern (v0.2 §3.8). Patterns appear in `match` arms and as the
2475/// right-hand side of the `is` operator.
2476#[derive(Debug, Clone)]
2477pub enum Pattern {
2478 /// `_` — matches any value, no bindings.
2479 Wildcard(Span),
2480 /// A lowercase identifier — binds the whole value to `name` and matches
2481 /// anything (ADR 0169). At the top of a `match` arm it binds the scrutinee
2482 /// (`n if n > 0 => …`); inside a payload position it binds the field
2483 /// (`Some(user)`). The uppercase-led counterpart is a nullary [`Pattern::Variant`].
2484 Binding(Ident),
2485 /// A literal pattern — `31`, `"english"`, `true` (v0.130 §2.3.4). Matches a
2486 /// primitive scrutinee (`Int`/`String`/`Bool`) by value equality. The
2487 /// admitted set mirrors ADR 0001's closed literal set (integers — including
2488 /// a leading unary minus — strings, and booleans); `Float`/`()` are not
2489 /// admitted as patterns.
2490 Literal { value: LiteralValue, span: Span },
2491 /// `Variant` or `Variant(bindings)` or `TypeName.Variant(bindings)`. Each
2492 /// payload binding is itself a [`Pattern`] (ADR 0169), so payloads nest:
2493 /// `Some(Ok(x))`, `Err(PollClosed)`.
2494 Variant {
2495 /// Optional qualifier: `TypeName.Variant`.
2496 type_name: Option<Ident>,
2497 /// The variant name.
2498 variant: Ident,
2499 /// Payload bindings (empty for nullary variants).
2500 bindings: Vec<PatternBinding>,
2501 span: Span,
2502 },
2503}
2504
2505/// The value carried by a [`Pattern::Literal`]. A closed set (ADR 0001):
2506/// integer, string, and boolean. Kept distinct from [`ExprKind`] so patterns
2507/// carry only what they can actually match, and so it is `Eq`/`Hash` for the
2508/// duplicate-arm check.
2509#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2510pub enum LiteralValue {
2511 Int(i64),
2512 Str(String),
2513 Bool(bool),
2514}
2515
2516impl LiteralValue {
2517 /// A human-readable rendering for diagnostics (`31`, `"english"`, `true`).
2518 pub fn describe(&self) -> String {
2519 match self {
2520 LiteralValue::Int(n) => n.to_string(),
2521 LiteralValue::Str(s) => format!("{s:?}"),
2522 LiteralValue::Bool(b) => b.to_string(),
2523 }
2524 }
2525}
2526
2527impl Pattern {
2528 pub fn span(&self) -> Span {
2529 match self {
2530 Pattern::Wildcard(s) => *s,
2531 Pattern::Binding(id) => id.span,
2532 Pattern::Literal { span, .. } => *span,
2533 Pattern::Variant { span, .. } => *span,
2534 }
2535 }
2536
2537 /// Every identifier this pattern binds into scope, recursively (`_` and
2538 /// nullary variants bind nothing). Used by the resolver and the checker to
2539 /// populate an arm's scope, and by the guard to see the arm's bindings.
2540 pub fn bound_names(&self) -> Vec<&Ident> {
2541 match self {
2542 Pattern::Wildcard(_) | Pattern::Literal { .. } => Vec::new(),
2543 Pattern::Binding(id) => vec![id],
2544 Pattern::Variant { bindings, .. } => bindings
2545 .iter()
2546 .flat_map(|b| b.pattern().bound_names())
2547 .collect(),
2548 }
2549 }
2550
2551 /// True when this pattern matches every value and binds nothing — a bare
2552 /// `_`. A [`Pattern::Binding`] also matches everything but *does* bind, so it
2553 /// is not a pure wildcard.
2554 pub fn is_wildcard(&self) -> bool {
2555 matches!(self, Pattern::Wildcard(_))
2556 }
2557
2558 /// True when this pattern matches every value (a `_` or a name binding),
2559 /// i.e. it is irrefutable and covers the position for exhaustiveness.
2560 pub fn is_irrefutable(&self) -> bool {
2561 matches!(self, Pattern::Wildcard(_) | Pattern::Binding(_))
2562 }
2563}
2564
2565/// A single binding inside a variant pattern. Two surface forms:
2566/// `pattern` (positional — match the i-th payload field) and
2567/// `fieldName: pattern` (named — match the named payload field). The matched
2568/// sub-`pattern` is a full [`Pattern`] (ADR 0169), so a plain `name` is a
2569/// [`Pattern::Binding`], `_` a [`Pattern::Wildcard`], and `Ok(x)` a nested
2570/// [`Pattern::Variant`].
2571#[derive(Debug, Clone)]
2572pub struct PatternBinding {
2573 /// Source form: positional or named.
2574 pub kind: PatternBindingKind,
2575 pub span: Span,
2576}
2577
2578#[derive(Debug, Clone)]
2579pub enum PatternBindingKind {
2580 /// `pattern` (e.g. `x`, `_`, `Ok(v)`): match the payload field at this position.
2581 Positional { pattern: Pattern },
2582 /// `field: pattern`: match the named payload field against `pattern`.
2583 Named { field: Ident, pattern: Pattern },
2584}
2585
2586impl PatternBinding {
2587 /// The sub-pattern this binding matches its payload field against.
2588 pub fn pattern(&self) -> &Pattern {
2589 match &self.kind {
2590 PatternBindingKind::Positional { pattern } => pattern,
2591 PatternBindingKind::Named { pattern, .. } => pattern,
2592 }
2593 }
2594
2595 /// True when this binding discards its field (`_` or `field: _`) — a pure
2596 /// wildcard sub-pattern that binds nothing.
2597 pub fn is_wildcard(&self) -> bool {
2598 self.pattern().is_wildcard()
2599 }
2600}
2601
2602#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2603pub enum BinOp {
2604 /// `P implies Q` — logical implication (v0.80). Desugars to `!P || Q`; sits
2605 /// at the lowest precedence (below `||`). Reads directionally (P → Q).
2606 Implies,
2607 Or,
2608 And,
2609 Eq,
2610 NotEq,
2611 Lt,
2612 LtEq,
2613 Gt,
2614 GtEq,
2615 Add,
2616 Sub,
2617 Mul,
2618 Div,
2619}
2620
2621impl BinOp {
2622 pub fn name(self) -> &'static str {
2623 match self {
2624 BinOp::Implies => "implies",
2625 BinOp::Or => "||",
2626 BinOp::And => "&&",
2627 BinOp::Eq => "==",
2628 BinOp::NotEq => "!=",
2629 BinOp::Lt => "<",
2630 BinOp::LtEq => "<=",
2631 BinOp::Gt => ">",
2632 BinOp::GtEq => ">=",
2633 BinOp::Add => "+",
2634 BinOp::Sub => "-",
2635 BinOp::Mul => "*",
2636 BinOp::Div => "/",
2637 }
2638 }
2639}
2640
2641#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2642pub enum UnaryOp {
2643 Neg,
2644 Not,
2645}
2646
2647impl UnaryOp {
2648 pub fn name(self) -> &'static str {
2649 match self {
2650 UnaryOp::Neg => "-",
2651 UnaryOp::Not => "!",
2652 }
2653 }
2654}