Skip to main content

harn_parser/
ast.rs

1use harn_lexer::{Span, StringSegment};
2
3/// A node wrapped with source location information.
4#[derive(Debug, Clone, PartialEq)]
5pub struct Spanned<T> {
6    pub node: T,
7    pub span: Span,
8}
9
10impl<T> Spanned<T> {
11    pub fn new(node: T, span: Span) -> Self {
12        Self { node, span }
13    }
14
15    pub fn dummy(node: T) -> Self {
16        Self {
17            node,
18            span: Span::dummy(),
19        }
20    }
21}
22
23/// A spanned AST node — the primary unit throughout the compiler.
24pub type SNode = Spanned<Node>;
25
26/// Helper to wrap a node with a span.
27pub fn spanned(node: Node, span: Span) -> SNode {
28    SNode::new(node, span)
29}
30
31/// If `node` is an `AttributedDecl`, returns `(attrs, inner)`; otherwise
32/// returns an empty attribute slice and the node itself. Use at the top
33/// of any consumer that processes top-level statements so attributes
34/// flow through transparently.
35pub fn peel_attributes(node: &SNode) -> (&[Attribute], &SNode) {
36    match &node.node {
37        Node::AttributedDecl { attributes, inner } => (attributes.as_slice(), inner.as_ref()),
38        _ => (&[], node),
39    }
40}
41
42/// A single argument to an attribute. Positional args have `name = None`;
43/// named args use `name: Some("key")`. Values are restricted to literal
44/// expressions by the parser (string/int/float/bool/nil/identifier).
45#[derive(Debug, Clone, PartialEq)]
46pub struct AttributeArg {
47    pub name: Option<String>,
48    pub value: SNode,
49    pub span: Span,
50}
51
52/// An attribute attached to a declaration: `@deprecated(since: "0.8")`.
53#[derive(Debug, Clone, PartialEq)]
54pub struct Attribute {
55    pub name: String,
56    pub args: Vec<AttributeArg>,
57    pub span: Span,
58}
59
60impl Attribute {
61    /// Find a named argument by key.
62    pub fn named_arg(&self, key: &str) -> Option<&SNode> {
63        self.args
64            .iter()
65            .find(|a| a.name.as_deref() == Some(key))
66            .map(|a| &a.value)
67    }
68
69    /// First positional argument, if any.
70    pub fn positional(&self, idx: usize) -> Option<&SNode> {
71        self.args
72            .iter()
73            .filter(|a| a.name.is_none())
74            .nth(idx)
75            .map(|a| &a.value)
76    }
77
78    /// Convenience: extract a string-literal arg by name.
79    pub fn string_arg(&self, key: &str) -> Option<String> {
80        match self.named_arg(key).map(|n| &n.node) {
81            Some(Node::StringLiteral(s)) => Some(s.clone()),
82            Some(Node::RawStringLiteral(s)) => Some(s.clone()),
83            _ => None,
84        }
85    }
86}
87
88/// AST nodes for the Harn language.
89#[derive(Debug, Clone, PartialEq)]
90pub enum Node {
91    /// A declaration carrying one or more attributes (`@attr`). The inner
92    /// node is always one of: FnDecl, ToolDecl, Pipeline, StructDecl,
93    /// EnumDecl, TypeDecl, InterfaceDecl, ImplBlock.
94    AttributedDecl {
95        attributes: Vec<Attribute>,
96        inner: Box<SNode>,
97    },
98    Pipeline {
99        name: String,
100        params: Vec<String>,
101        return_type: Option<TypeExpr>,
102        body: Vec<SNode>,
103        extends: Option<String>,
104        is_pub: bool,
105    },
106    LetBinding {
107        pattern: BindingPattern,
108        type_ann: Option<TypeExpr>,
109        value: Box<SNode>,
110    },
111    VarBinding {
112        pattern: BindingPattern,
113        type_ann: Option<TypeExpr>,
114        value: Box<SNode>,
115    },
116    OverrideDecl {
117        name: String,
118        params: Vec<String>,
119        body: Vec<SNode>,
120    },
121    ImportDecl {
122        path: String,
123        /// When true, the wildcard import is a re-export: every public symbol
124        /// from the target module becomes part of this module's public surface.
125        is_pub: bool,
126    },
127    /// Selective import: import { foo, bar } from "module"
128    SelectiveImport {
129        names: Vec<String>,
130        path: String,
131        /// When true, the listed names are re-exported as part of this
132        /// module's public surface.
133        is_pub: bool,
134    },
135    EnumDecl {
136        name: String,
137        type_params: Vec<TypeParam>,
138        variants: Vec<EnumVariant>,
139        is_pub: bool,
140    },
141    StructDecl {
142        name: String,
143        type_params: Vec<TypeParam>,
144        fields: Vec<StructField>,
145        is_pub: bool,
146    },
147    InterfaceDecl {
148        name: String,
149        type_params: Vec<TypeParam>,
150        associated_types: Vec<(String, Option<TypeExpr>)>,
151        methods: Vec<InterfaceMethod>,
152    },
153    /// Impl block: impl TypeName { fn method(self, ...) { ... } ... }
154    ImplBlock {
155        type_name: String,
156        methods: Vec<SNode>,
157    },
158
159    IfElse {
160        condition: Box<SNode>,
161        then_body: Vec<SNode>,
162        else_body: Option<Vec<SNode>>,
163    },
164    ForIn {
165        pattern: BindingPattern,
166        iterable: Box<SNode>,
167        body: Vec<SNode>,
168    },
169    MatchExpr {
170        value: Box<SNode>,
171        arms: Vec<MatchArm>,
172    },
173    WhileLoop {
174        condition: Box<SNode>,
175        body: Vec<SNode>,
176    },
177    Retry {
178        count: Box<SNode>,
179        body: Vec<SNode>,
180    },
181    /// Scoped cost-aware LLM routing block:
182    /// `cost_route { key: value ... body }`.
183    ///
184    /// Options are inherited by nested `llm_call` invocations unless a
185    /// call explicitly overrides the same option.
186    CostRoute {
187        options: Vec<(String, SNode)>,
188        body: Vec<SNode>,
189    },
190    ReturnStmt {
191        value: Option<Box<SNode>>,
192    },
193    TryCatch {
194        body: Vec<SNode>,
195        error_var: Option<String>,
196        error_type: Option<TypeExpr>,
197        catch_body: Vec<SNode>,
198        finally_body: Option<Vec<SNode>>,
199    },
200    /// Try expression: try { body } — returns Result.Ok(value), an existing Result,
201    /// or Result.Err(error).
202    TryExpr {
203        body: Vec<SNode>,
204    },
205    FnDecl {
206        name: String,
207        type_params: Vec<TypeParam>,
208        params: Vec<TypedParam>,
209        return_type: Option<TypeExpr>,
210        where_clauses: Vec<WhereClause>,
211        body: Vec<SNode>,
212        is_pub: bool,
213        is_stream: bool,
214    },
215    ToolDecl {
216        name: String,
217        description: Option<String>,
218        params: Vec<TypedParam>,
219        return_type: Option<TypeExpr>,
220        body: Vec<SNode>,
221        is_pub: bool,
222    },
223    /// Top-level `skill NAME { ... }` declaration.
224    ///
225    /// Skills bundle metadata, tool references, MCP server lists, and
226    /// optional lifecycle hooks into a typed unit. Each body entry is a
227    /// `<field_name> <expression>` pair; the compiler lowers the decl to
228    /// `skill_define(skill_registry(), NAME, { field: expr, ... })` and
229    /// binds the resulting registry dict to `NAME`.
230    SkillDecl {
231        name: String,
232        fields: Vec<(String, SNode)>,
233        is_pub: bool,
234    },
235    /// Top-level `eval_pack NAME_OR_STRING { ... }` declaration.
236    ///
237    /// The compiler lowers fields into `eval_pack_manifest({ ... })` and
238    /// binds the normalized manifest to `binding_name`. Optional executable
239    /// body statements are only run when the declaration itself is executed
240    /// in script/block position; top-level pipeline preloading registers the
241    /// manifest data without running the body.
242    EvalPackDecl {
243        binding_name: String,
244        pack_id: String,
245        fields: Vec<(String, SNode)>,
246        body: Vec<SNode>,
247        summarize: Option<Vec<SNode>>,
248        is_pub: bool,
249    },
250    TypeDecl {
251        name: String,
252        type_params: Vec<TypeParam>,
253        type_expr: TypeExpr,
254    },
255    SpawnExpr {
256        body: Vec<SNode>,
257    },
258    /// Duration literal: 500ms, 5s, 30m, 2h, 1d, 1w
259    DurationLiteral(u64),
260    /// Range expression: `start to end` (inclusive) or `start to end exclusive` (half-open)
261    RangeExpr {
262        start: Box<SNode>,
263        end: Box<SNode>,
264        inclusive: bool,
265    },
266    /// Guard clause: guard condition else { body }
267    GuardStmt {
268        condition: Box<SNode>,
269        else_body: Vec<SNode>,
270    },
271    RequireStmt {
272        condition: Box<SNode>,
273        message: Option<Box<SNode>>,
274    },
275    /// Defer statement: defer { body } — runs body at scope exit.
276    DeferStmt {
277        body: Vec<SNode>,
278    },
279    /// Deadline block: deadline DURATION { body }
280    DeadlineBlock {
281        duration: Box<SNode>,
282        body: Vec<SNode>,
283    },
284    /// Yield expression: yields control to host, optionally with a value.
285    YieldExpr {
286        value: Option<Box<SNode>>,
287    },
288    /// Emit expression: emits one value from a `gen fn` stream.
289    EmitExpr {
290        value: Box<SNode>,
291    },
292    /// Mutex block: mutual exclusion for concurrent access.
293    MutexBlock {
294        body: Vec<SNode>,
295    },
296    /// Break out of a loop.
297    BreakStmt,
298    /// Continue to next loop iteration.
299    ContinueStmt,
300
301    Parallel {
302        mode: ParallelMode,
303        /// For Count mode: the count expression. For Each/Settle: the list expression.
304        expr: Box<SNode>,
305        variable: Option<String>,
306        body: Vec<SNode>,
307        /// Optional trailing `with { max_concurrent: N, ... }` option block.
308        /// A vec (rather than a dict) preserves source order for error
309        /// reporting and keeps parsing cheap. Only `max_concurrent` is
310        /// currently honored; unknown keys are rejected by the parser.
311        options: Vec<(String, SNode)>,
312    },
313
314    SelectExpr {
315        cases: Vec<SelectCase>,
316        timeout: Option<(Box<SNode>, Vec<SNode>)>,
317        default_body: Option<Vec<SNode>>,
318    },
319
320    FunctionCall {
321        name: String,
322        type_args: Vec<TypeExpr>,
323        args: Vec<SNode>,
324    },
325    MethodCall {
326        object: Box<SNode>,
327        method: String,
328        args: Vec<SNode>,
329    },
330    /// Optional method call: `obj?.method(args)` — returns nil if obj is nil.
331    OptionalMethodCall {
332        object: Box<SNode>,
333        method: String,
334        args: Vec<SNode>,
335    },
336    PropertyAccess {
337        object: Box<SNode>,
338        property: String,
339    },
340    /// Optional chaining: `obj?.property` — returns nil if obj is nil.
341    OptionalPropertyAccess {
342        object: Box<SNode>,
343        property: String,
344    },
345    SubscriptAccess {
346        object: Box<SNode>,
347        index: Box<SNode>,
348    },
349    /// Optional subscript: `obj?[index]` — returns nil if obj is nil.
350    OptionalSubscriptAccess {
351        object: Box<SNode>,
352        index: Box<SNode>,
353    },
354    SliceAccess {
355        object: Box<SNode>,
356        start: Option<Box<SNode>>,
357        end: Option<Box<SNode>>,
358    },
359    BinaryOp {
360        op: String,
361        left: Box<SNode>,
362        right: Box<SNode>,
363    },
364    UnaryOp {
365        op: String,
366        operand: Box<SNode>,
367    },
368    Ternary {
369        condition: Box<SNode>,
370        true_expr: Box<SNode>,
371        false_expr: Box<SNode>,
372    },
373    Assignment {
374        target: Box<SNode>,
375        value: Box<SNode>,
376        /// None = plain `=`, Some("+") = `+=`, etc.
377        op: Option<String>,
378    },
379    ThrowStmt {
380        value: Box<SNode>,
381    },
382
383    /// Enum variant construction: EnumName.Variant(args)
384    EnumConstruct {
385        enum_name: String,
386        variant: String,
387        args: Vec<SNode>,
388    },
389    /// Struct construction: StructName { field: value, ... }
390    StructConstruct {
391        struct_name: String,
392        fields: Vec<DictEntry>,
393    },
394
395    InterpolatedString(Vec<StringSegment>),
396    StringLiteral(String),
397    /// Raw string literal `r"..."` — no escape processing.
398    RawStringLiteral(String),
399    IntLiteral(i64),
400    FloatLiteral(f64),
401    BoolLiteral(bool),
402    NilLiteral,
403    Identifier(String),
404    ListLiteral(Vec<SNode>),
405    DictLiteral(Vec<DictEntry>),
406    /// Spread expression `...expr` inside list/dict literals.
407    Spread(Box<SNode>),
408    /// Try operator: expr? — unwraps Result.Ok or propagates Result.Err.
409    TryOperator {
410        operand: Box<SNode>,
411    },
412    /// Try-star operator: `try* EXPR` — evaluates EXPR; on throw, runs
413    /// pending finally blocks up to the enclosing catch and rethrows
414    /// the original value. On success, evaluates to EXPR's value.
415    /// Lowered per spec/HARN_SPEC.md as:
416    ///   { let _r = try { EXPR }
417    ///     guard is_ok(_r) else { throw unwrap_err(_r) }
418    ///     unwrap(_r) }
419    TryStar {
420        operand: Box<SNode>,
421    },
422
423    /// Or-pattern in a `match` arm: `"ping" | "pong" -> body`. One or
424    /// more alternative patterns that share a single arm body. Only
425    /// legal inside a `MatchArm.pattern` slot.
426    OrPattern(Vec<SNode>),
427
428    Block(Vec<SNode>),
429    Closure {
430        params: Vec<TypedParam>,
431        body: Vec<SNode>,
432        /// When true, this closure was written as `fn(params) { body }`.
433        /// The formatter preserves this distinction.
434        fn_syntax: bool,
435    },
436}
437
438/// Parallel execution mode.
439#[derive(Debug, Clone, Copy, PartialEq)]
440pub enum ParallelMode {
441    /// `parallel N { i -> ... }` — run N concurrent tasks.
442    Count,
443    /// `parallel each list { item -> ... }` — map over list concurrently.
444    Each,
445    /// `parallel each list { item -> ... } as stream` — emit as each task completes.
446    EachStream,
447    /// `parallel settle list { item -> ... }` — map with error collection.
448    Settle,
449}
450
451#[derive(Debug, Clone, PartialEq)]
452pub struct MatchArm {
453    pub pattern: SNode,
454    /// Optional guard: `pattern if condition -> { body }`.
455    pub guard: Option<Box<SNode>>,
456    pub body: Vec<SNode>,
457}
458
459#[derive(Debug, Clone, PartialEq)]
460pub struct SelectCase {
461    pub variable: String,
462    pub channel: Box<SNode>,
463    pub body: Vec<SNode>,
464}
465
466#[derive(Debug, Clone, PartialEq)]
467pub struct DictEntry {
468    pub key: SNode,
469    pub value: SNode,
470}
471
472/// An enum variant declaration.
473#[derive(Debug, Clone, PartialEq)]
474pub struct EnumVariant {
475    pub name: String,
476    pub fields: Vec<TypedParam>,
477}
478
479/// A struct field declaration.
480#[derive(Debug, Clone, PartialEq)]
481pub struct StructField {
482    pub name: String,
483    pub type_expr: Option<TypeExpr>,
484    pub optional: bool,
485}
486
487/// An interface method signature.
488#[derive(Debug, Clone, PartialEq)]
489pub struct InterfaceMethod {
490    pub name: String,
491    pub type_params: Vec<TypeParam>,
492    pub params: Vec<TypedParam>,
493    pub return_type: Option<TypeExpr>,
494}
495
496/// A type annotation (optional, for runtime checking).
497#[derive(Debug, Clone, PartialEq)]
498pub enum TypeExpr {
499    /// A named type: int, string, float, bool, nil, list, dict, closure,
500    /// or a user-defined type name.
501    Named(String),
502    /// A union type: `string | nil`, `int | float`.
503    Union(Vec<TypeExpr>),
504    /// A dict shape type: `{name: string, age: int, active?: bool}`.
505    Shape(Vec<ShapeField>),
506    /// A list type: `list<int>`.
507    List(Box<TypeExpr>),
508    /// A dict type with key and value types: `dict<string, int>`.
509    DictType(Box<TypeExpr>, Box<TypeExpr>),
510    /// A lazy iterator type: `iter<int>`. Yields values of the inner type
511    /// via the combinator/sink protocol (`VmValue::Iter` at runtime).
512    Iter(Box<TypeExpr>),
513    /// A synchronous generator type: `Generator<int>`. Produced by a regular
514    /// `fn` body containing `yield`.
515    Generator(Box<TypeExpr>),
516    /// An asynchronous stream type: `Stream<int>`. Produced by `gen fn`.
517    Stream(Box<TypeExpr>),
518    /// A generic type application: `Option<int>`, `Result<string, int>`.
519    Applied { name: String, args: Vec<TypeExpr> },
520    /// A function type: `fn(int, string) -> bool`.
521    FnType {
522        params: Vec<TypeExpr>,
523        return_type: Box<TypeExpr>,
524    },
525    /// The bottom type: the type of expressions that never produce a value
526    /// (return, throw, break, continue).
527    Never,
528    /// A string-literal type: `"pass"`, `"fail"`. Assignable to `string`.
529    /// Used in unions to represent enum-like discriminated values.
530    LitString(String),
531    /// An int-literal type: `0`, `1`, `-1`. Assignable to `int`.
532    LitInt(i64),
533}
534
535/// A field in a dict shape type.
536#[derive(Debug, Clone, PartialEq)]
537pub struct ShapeField {
538    pub name: String,
539    pub type_expr: TypeExpr,
540    pub optional: bool,
541}
542
543/// A binding pattern for destructuring in let/var/for-in.
544#[derive(Debug, Clone, PartialEq)]
545pub enum BindingPattern {
546    /// Simple identifier: `let x = ...`
547    Identifier(String),
548    /// Dict destructuring: `let {name, age} = ...`
549    Dict(Vec<DictPatternField>),
550    /// List destructuring: `let [a, b] = ...`
551    List(Vec<ListPatternElement>),
552    /// Pair destructuring for `for (a, b) in iter { ... }`. The iter must
553    /// yield `VmValue::Pair` values. Not valid in let/var bindings.
554    Pair(String, String),
555}
556
557/// `_` is the discard binding name in `let`/`var`/destructuring positions.
558pub fn is_discard_name(name: &str) -> bool {
559    name == "_"
560}
561
562/// A field in a dict destructuring pattern.
563#[derive(Debug, Clone, PartialEq)]
564pub struct DictPatternField {
565    /// The dict key to extract.
566    pub key: String,
567    /// Renamed binding (if different from key), e.g. `{name: alias}`.
568    pub alias: Option<String>,
569    /// True for `...rest` (rest pattern).
570    pub is_rest: bool,
571    /// Default value if the key is missing (nil), e.g. `{name = "default"}`.
572    pub default_value: Option<Box<SNode>>,
573}
574
575/// An element in a list destructuring pattern.
576#[derive(Debug, Clone, PartialEq)]
577pub struct ListPatternElement {
578    /// The variable name to bind.
579    pub name: String,
580    /// True for `...rest` (rest pattern).
581    pub is_rest: bool,
582    /// Default value if the index is out of bounds (nil), e.g. `[a = 0]`.
583    pub default_value: Option<Box<SNode>>,
584}
585
586/// Declared variance of a generic type parameter.
587///
588/// - `Invariant` (default, no marker): the parameter appears in both
589///   input and output positions, or mutable state. `T<A>` and `T<B>`
590///   are unrelated unless `A == B`.
591/// - `Covariant` (`out T`): the parameter appears only in output
592///   positions (produced, not consumed). `T<Sub>` flows into
593///   `T<Super>`.
594/// - `Contravariant` (`in T`): the parameter appears only in input
595///   positions (consumed, not produced). `T<Super>` flows into
596///   `T<Sub>`.
597#[derive(Debug, Clone, Copy, PartialEq, Eq)]
598pub enum Variance {
599    Invariant,
600    Covariant,
601    Contravariant,
602}
603
604/// A generic type parameter on a function or pipeline declaration.
605#[derive(Debug, Clone, PartialEq)]
606pub struct TypeParam {
607    pub name: String,
608    pub variance: Variance,
609}
610
611impl TypeParam {
612    /// Construct an invariant type parameter (the default for
613    /// unannotated `<T>`).
614    pub fn invariant(name: impl Into<String>) -> Self {
615        Self {
616            name: name.into(),
617            variance: Variance::Invariant,
618        }
619    }
620}
621
622/// A where-clause constraint on a generic type parameter.
623#[derive(Debug, Clone, PartialEq)]
624pub struct WhereClause {
625    pub type_name: String,
626    pub bound: String,
627}
628
629/// A parameter with an optional type annotation and optional default value.
630#[derive(Debug, Clone, PartialEq)]
631pub struct TypedParam {
632    pub name: String,
633    pub type_expr: Option<TypeExpr>,
634    pub default_value: Option<Box<SNode>>,
635    /// If true, this is a rest parameter (`...name`) that collects remaining arguments.
636    pub rest: bool,
637}
638
639impl TypedParam {
640    /// Create an untyped parameter.
641    pub fn untyped(name: impl Into<String>) -> Self {
642        Self {
643            name: name.into(),
644            type_expr: None,
645            default_value: None,
646            rest: false,
647        }
648    }
649
650    /// Create a typed parameter.
651    pub fn typed(name: impl Into<String>, type_expr: TypeExpr) -> Self {
652        Self {
653            name: name.into(),
654            type_expr: Some(type_expr),
655            default_value: None,
656            rest: false,
657        }
658    }
659
660    /// Extract just the names from a list of typed params.
661    pub fn names(params: &[TypedParam]) -> Vec<String> {
662        params.iter().map(|p| p.name.clone()).collect()
663    }
664
665    /// Return the index of the first parameter with a default value, or None.
666    pub fn default_start(params: &[TypedParam]) -> Option<usize> {
667        params.iter().position(|p| p.default_value.is_some())
668    }
669}