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/// AST nodes for the Harn language.
32#[derive(Debug, Clone, PartialEq)]
33pub enum Node {
34    // Declarations
35    Pipeline {
36        name: String,
37        params: Vec<String>,
38        body: Vec<SNode>,
39        extends: Option<String>,
40        is_pub: bool,
41    },
42    LetBinding {
43        pattern: BindingPattern,
44        type_ann: Option<TypeExpr>,
45        value: Box<SNode>,
46    },
47    VarBinding {
48        pattern: BindingPattern,
49        type_ann: Option<TypeExpr>,
50        value: Box<SNode>,
51    },
52    OverrideDecl {
53        name: String,
54        params: Vec<String>,
55        body: Vec<SNode>,
56    },
57    ImportDecl {
58        path: String,
59    },
60    /// Selective import: import { foo, bar } from "module"
61    SelectiveImport {
62        names: Vec<String>,
63        path: String,
64    },
65    EnumDecl {
66        name: String,
67        type_params: Vec<TypeParam>,
68        variants: Vec<EnumVariant>,
69        is_pub: bool,
70    },
71    StructDecl {
72        name: String,
73        type_params: Vec<TypeParam>,
74        fields: Vec<StructField>,
75        is_pub: bool,
76    },
77    InterfaceDecl {
78        name: String,
79        type_params: Vec<TypeParam>,
80        associated_types: Vec<(String, Option<TypeExpr>)>,
81        methods: Vec<InterfaceMethod>,
82    },
83    /// Impl block: impl TypeName { fn method(self, ...) { ... } ... }
84    ImplBlock {
85        type_name: String,
86        methods: Vec<SNode>,
87    },
88
89    // Control flow
90    IfElse {
91        condition: Box<SNode>,
92        then_body: Vec<SNode>,
93        else_body: Option<Vec<SNode>>,
94    },
95    ForIn {
96        pattern: BindingPattern,
97        iterable: Box<SNode>,
98        body: Vec<SNode>,
99    },
100    MatchExpr {
101        value: Box<SNode>,
102        arms: Vec<MatchArm>,
103    },
104    WhileLoop {
105        condition: Box<SNode>,
106        body: Vec<SNode>,
107    },
108    Retry {
109        count: Box<SNode>,
110        body: Vec<SNode>,
111    },
112    ReturnStmt {
113        value: Option<Box<SNode>>,
114    },
115    TryCatch {
116        body: Vec<SNode>,
117        error_var: Option<String>,
118        error_type: Option<TypeExpr>,
119        catch_body: Vec<SNode>,
120        finally_body: Option<Vec<SNode>>,
121    },
122    /// Try expression: try { body } — returns Result.Ok(value) or Result.Err(error).
123    TryExpr {
124        body: Vec<SNode>,
125    },
126    FnDecl {
127        name: String,
128        type_params: Vec<TypeParam>,
129        params: Vec<TypedParam>,
130        return_type: Option<TypeExpr>,
131        where_clauses: Vec<WhereClause>,
132        body: Vec<SNode>,
133        is_pub: bool,
134    },
135    ToolDecl {
136        name: String,
137        description: Option<String>,
138        params: Vec<TypedParam>,
139        return_type: Option<TypeExpr>,
140        body: Vec<SNode>,
141        is_pub: bool,
142    },
143    TypeDecl {
144        name: String,
145        type_expr: TypeExpr,
146    },
147    SpawnExpr {
148        body: Vec<SNode>,
149    },
150    /// Duration literal: 500ms, 5s, 30m, 2h, 1d, 1w
151    DurationLiteral(u64),
152    /// Range expression: start upto end (exclusive) or start thru end (inclusive)
153    RangeExpr {
154        start: Box<SNode>,
155        end: Box<SNode>,
156        inclusive: bool,
157    },
158    /// Guard clause: guard condition else { body }
159    GuardStmt {
160        condition: Box<SNode>,
161        else_body: Vec<SNode>,
162    },
163    RequireStmt {
164        condition: Box<SNode>,
165        message: Option<Box<SNode>>,
166    },
167    /// Defer statement: defer { body } — runs body at scope exit.
168    DeferStmt {
169        body: Vec<SNode>,
170    },
171    /// Deadline block: deadline DURATION { body }
172    DeadlineBlock {
173        duration: Box<SNode>,
174        body: Vec<SNode>,
175    },
176    /// Yield expression: yields control to host, optionally with a value.
177    YieldExpr {
178        value: Option<Box<SNode>>,
179    },
180    /// Mutex block: mutual exclusion for concurrent access.
181    MutexBlock {
182        body: Vec<SNode>,
183    },
184    /// Break out of a loop.
185    BreakStmt,
186    /// Continue to next loop iteration.
187    ContinueStmt,
188
189    // Concurrency
190    Parallel {
191        mode: ParallelMode,
192        /// For Count mode: the count expression. For Each/Settle: the list expression.
193        expr: Box<SNode>,
194        variable: Option<String>,
195        body: Vec<SNode>,
196    },
197
198    SelectExpr {
199        cases: Vec<SelectCase>,
200        timeout: Option<(Box<SNode>, Vec<SNode>)>,
201        default_body: Option<Vec<SNode>>,
202    },
203
204    // Expressions
205    FunctionCall {
206        name: String,
207        args: Vec<SNode>,
208    },
209    MethodCall {
210        object: Box<SNode>,
211        method: String,
212        args: Vec<SNode>,
213    },
214    /// Optional method call: `obj?.method(args)` — returns nil if obj is nil.
215    OptionalMethodCall {
216        object: Box<SNode>,
217        method: String,
218        args: Vec<SNode>,
219    },
220    PropertyAccess {
221        object: Box<SNode>,
222        property: String,
223    },
224    /// Optional chaining: `obj?.property` — returns nil if obj is nil.
225    OptionalPropertyAccess {
226        object: Box<SNode>,
227        property: String,
228    },
229    SubscriptAccess {
230        object: Box<SNode>,
231        index: Box<SNode>,
232    },
233    SliceAccess {
234        object: Box<SNode>,
235        start: Option<Box<SNode>>,
236        end: Option<Box<SNode>>,
237    },
238    BinaryOp {
239        op: String,
240        left: Box<SNode>,
241        right: Box<SNode>,
242    },
243    UnaryOp {
244        op: String,
245        operand: Box<SNode>,
246    },
247    Ternary {
248        condition: Box<SNode>,
249        true_expr: Box<SNode>,
250        false_expr: Box<SNode>,
251    },
252    Assignment {
253        target: Box<SNode>,
254        value: Box<SNode>,
255        /// None = plain `=`, Some("+") = `+=`, etc.
256        op: Option<String>,
257    },
258    ThrowStmt {
259        value: Box<SNode>,
260    },
261
262    /// Enum variant construction: EnumName.Variant(args)
263    EnumConstruct {
264        enum_name: String,
265        variant: String,
266        args: Vec<SNode>,
267    },
268    /// Struct construction: StructName { field: value, ... }
269    StructConstruct {
270        struct_name: String,
271        fields: Vec<DictEntry>,
272    },
273
274    // Literals
275    InterpolatedString(Vec<StringSegment>),
276    StringLiteral(String),
277    /// Raw string literal `r"..."` — no escape processing.
278    RawStringLiteral(String),
279    IntLiteral(i64),
280    FloatLiteral(f64),
281    BoolLiteral(bool),
282    NilLiteral,
283    Identifier(String),
284    ListLiteral(Vec<SNode>),
285    DictLiteral(Vec<DictEntry>),
286    /// Spread expression `...expr` inside list/dict literals.
287    Spread(Box<SNode>),
288    /// Try operator: expr? — unwraps Result.Ok or propagates Result.Err.
289    TryOperator {
290        operand: Box<SNode>,
291    },
292
293    // Blocks
294    Block(Vec<SNode>),
295    Closure {
296        params: Vec<TypedParam>,
297        body: Vec<SNode>,
298        /// When true, this closure was written as `fn(params) { body }`.
299        /// The formatter preserves this distinction.
300        fn_syntax: bool,
301    },
302}
303
304/// Parallel execution mode.
305#[derive(Debug, Clone, Copy, PartialEq)]
306pub enum ParallelMode {
307    /// `parallel N { i -> ... }` — run N concurrent tasks.
308    Count,
309    /// `parallel each list { item -> ... }` — map over list concurrently.
310    Each,
311    /// `parallel settle list { item -> ... }` — map with error collection.
312    Settle,
313}
314
315#[derive(Debug, Clone, PartialEq)]
316pub struct MatchArm {
317    pub pattern: SNode,
318    /// Optional guard: `pattern if condition -> { body }`.
319    pub guard: Option<Box<SNode>>,
320    pub body: Vec<SNode>,
321}
322
323#[derive(Debug, Clone, PartialEq)]
324pub struct SelectCase {
325    pub variable: String,
326    pub channel: Box<SNode>,
327    pub body: Vec<SNode>,
328}
329
330#[derive(Debug, Clone, PartialEq)]
331pub struct DictEntry {
332    pub key: SNode,
333    pub value: SNode,
334}
335
336/// An enum variant declaration.
337#[derive(Debug, Clone, PartialEq)]
338pub struct EnumVariant {
339    pub name: String,
340    pub fields: Vec<TypedParam>,
341}
342
343/// A struct field declaration.
344#[derive(Debug, Clone, PartialEq)]
345pub struct StructField {
346    pub name: String,
347    pub type_expr: Option<TypeExpr>,
348    pub optional: bool,
349}
350
351/// An interface method signature.
352#[derive(Debug, Clone, PartialEq)]
353pub struct InterfaceMethod {
354    pub name: String,
355    pub type_params: Vec<TypeParam>,
356    pub params: Vec<TypedParam>,
357    pub return_type: Option<TypeExpr>,
358}
359
360/// A type annotation (optional, for runtime checking).
361#[derive(Debug, Clone, PartialEq)]
362pub enum TypeExpr {
363    /// A named type: int, string, float, bool, nil, list, dict, closure,
364    /// or a user-defined type name.
365    Named(String),
366    /// A union type: `string | nil`, `int | float`.
367    Union(Vec<TypeExpr>),
368    /// A dict shape type: `{name: string, age: int, active?: bool}`.
369    Shape(Vec<ShapeField>),
370    /// A list type: `list<int>`.
371    List(Box<TypeExpr>),
372    /// A dict type with key and value types: `dict<string, int>`.
373    DictType(Box<TypeExpr>, Box<TypeExpr>),
374    /// A generic type application: `Option<int>`, `Result<string, int>`.
375    Applied { name: String, args: Vec<TypeExpr> },
376    /// A function type: `fn(int, string) -> bool`.
377    FnType {
378        params: Vec<TypeExpr>,
379        return_type: Box<TypeExpr>,
380    },
381    /// The bottom type: the type of expressions that never produce a value
382    /// (return, throw, break, continue).
383    Never,
384}
385
386/// A field in a dict shape type.
387#[derive(Debug, Clone, PartialEq)]
388pub struct ShapeField {
389    pub name: String,
390    pub type_expr: TypeExpr,
391    pub optional: bool,
392}
393
394/// A binding pattern for destructuring in let/var/for-in.
395#[derive(Debug, Clone, PartialEq)]
396pub enum BindingPattern {
397    /// Simple identifier: `let x = ...`
398    Identifier(String),
399    /// Dict destructuring: `let {name, age} = ...`
400    Dict(Vec<DictPatternField>),
401    /// List destructuring: `let [a, b] = ...`
402    List(Vec<ListPatternElement>),
403}
404
405/// A field in a dict destructuring pattern.
406#[derive(Debug, Clone, PartialEq)]
407pub struct DictPatternField {
408    /// The dict key to extract.
409    pub key: String,
410    /// Renamed binding (if different from key), e.g. `{name: alias}`.
411    pub alias: Option<String>,
412    /// True for `...rest` (rest pattern).
413    pub is_rest: bool,
414    /// Default value if the key is missing (nil), e.g. `{name = "default"}`.
415    pub default_value: Option<Box<SNode>>,
416}
417
418/// An element in a list destructuring pattern.
419#[derive(Debug, Clone, PartialEq)]
420pub struct ListPatternElement {
421    /// The variable name to bind.
422    pub name: String,
423    /// True for `...rest` (rest pattern).
424    pub is_rest: bool,
425    /// Default value if the index is out of bounds (nil), e.g. `[a = 0]`.
426    pub default_value: Option<Box<SNode>>,
427}
428
429/// A generic type parameter on a function or pipeline declaration.
430#[derive(Debug, Clone, PartialEq)]
431pub struct TypeParam {
432    pub name: String,
433}
434
435/// A where-clause constraint on a generic type parameter.
436#[derive(Debug, Clone, PartialEq)]
437pub struct WhereClause {
438    pub type_name: String,
439    pub bound: String,
440}
441
442/// A parameter with an optional type annotation and optional default value.
443#[derive(Debug, Clone, PartialEq)]
444pub struct TypedParam {
445    pub name: String,
446    pub type_expr: Option<TypeExpr>,
447    pub default_value: Option<Box<SNode>>,
448    /// If true, this is a rest parameter (`...name`) that collects remaining arguments.
449    pub rest: bool,
450}
451
452impl TypedParam {
453    /// Create an untyped parameter.
454    pub fn untyped(name: impl Into<String>) -> Self {
455        Self {
456            name: name.into(),
457            type_expr: None,
458            default_value: None,
459            rest: false,
460        }
461    }
462
463    /// Create a typed parameter.
464    pub fn typed(name: impl Into<String>, type_expr: TypeExpr) -> Self {
465        Self {
466            name: name.into(),
467            type_expr: Some(type_expr),
468            default_value: None,
469            rest: false,
470        }
471    }
472
473    /// Extract just the names from a list of typed params.
474    pub fn names(params: &[TypedParam]) -> Vec<String> {
475        params.iter().map(|p| p.name.clone()).collect()
476    }
477
478    /// Return the index of the first parameter with a default value, or None.
479    pub fn default_start(params: &[TypedParam]) -> Option<usize> {
480        params.iter().position(|p| p.default_value.is_some())
481    }
482}