1use harn_lexer::{Span, StringSegment};
2
3#[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
23pub type SNode = Spanned<Node>;
25
26pub fn spanned(node: Node, span: Span) -> SNode {
28 SNode::new(node, span)
29}
30
31#[derive(Debug, Clone, PartialEq)]
33pub enum Node {
34 Pipeline {
36 name: String,
37 params: Vec<String>,
38 body: Vec<SNode>,
39 extends: Option<String>,
40 },
41 LetBinding {
42 pattern: BindingPattern,
43 type_ann: Option<TypeExpr>,
44 value: Box<SNode>,
45 },
46 VarBinding {
47 pattern: BindingPattern,
48 type_ann: Option<TypeExpr>,
49 value: Box<SNode>,
50 },
51 OverrideDecl {
52 name: String,
53 params: Vec<String>,
54 body: Vec<SNode>,
55 },
56 ImportDecl {
57 path: String,
58 },
59 SelectiveImport {
61 names: Vec<String>,
62 path: String,
63 },
64 EnumDecl {
65 name: String,
66 variants: Vec<EnumVariant>,
67 },
68 StructDecl {
69 name: String,
70 fields: Vec<StructField>,
71 },
72 InterfaceDecl {
73 name: String,
74 methods: Vec<InterfaceMethod>,
75 },
76 ImplBlock {
78 type_name: String,
79 methods: Vec<SNode>,
80 },
81
82 IfElse {
84 condition: Box<SNode>,
85 then_body: Vec<SNode>,
86 else_body: Option<Vec<SNode>>,
87 },
88 ForIn {
89 pattern: BindingPattern,
90 iterable: Box<SNode>,
91 body: Vec<SNode>,
92 },
93 MatchExpr {
94 value: Box<SNode>,
95 arms: Vec<MatchArm>,
96 },
97 WhileLoop {
98 condition: Box<SNode>,
99 body: Vec<SNode>,
100 },
101 Retry {
102 count: Box<SNode>,
103 body: Vec<SNode>,
104 },
105 ReturnStmt {
106 value: Option<Box<SNode>>,
107 },
108 TryCatch {
109 body: Vec<SNode>,
110 error_var: Option<String>,
111 error_type: Option<TypeExpr>,
112 catch_body: Vec<SNode>,
113 finally_body: Option<Vec<SNode>>,
114 },
115 TryExpr {
117 body: Vec<SNode>,
118 },
119 FnDecl {
120 name: String,
121 type_params: Vec<TypeParam>,
122 params: Vec<TypedParam>,
123 return_type: Option<TypeExpr>,
124 where_clauses: Vec<WhereClause>,
125 body: Vec<SNode>,
126 is_pub: bool,
127 },
128 TypeDecl {
129 name: String,
130 type_expr: TypeExpr,
131 },
132 SpawnExpr {
133 body: Vec<SNode>,
134 },
135 DurationLiteral(u64),
137 RangeExpr {
139 start: Box<SNode>,
140 end: Box<SNode>,
141 inclusive: bool,
142 },
143 GuardStmt {
145 condition: Box<SNode>,
146 else_body: Vec<SNode>,
147 },
148 AskExpr {
150 fields: Vec<DictEntry>,
151 },
152 DeadlineBlock {
154 duration: Box<SNode>,
155 body: Vec<SNode>,
156 },
157 YieldExpr {
159 value: Option<Box<SNode>>,
160 },
161 MutexBlock {
163 body: Vec<SNode>,
164 },
165 BreakStmt,
167 ContinueStmt,
169
170 Parallel {
172 count: Box<SNode>,
173 variable: Option<String>,
174 body: Vec<SNode>,
175 },
176 ParallelMap {
177 list: Box<SNode>,
178 variable: String,
179 body: Vec<SNode>,
180 },
181
182 SelectExpr {
183 cases: Vec<SelectCase>,
184 timeout: Option<(Box<SNode>, Vec<SNode>)>,
185 default_body: Option<Vec<SNode>>,
186 },
187
188 FunctionCall {
190 name: String,
191 args: Vec<SNode>,
192 },
193 MethodCall {
194 object: Box<SNode>,
195 method: String,
196 args: Vec<SNode>,
197 },
198 OptionalMethodCall {
200 object: Box<SNode>,
201 method: String,
202 args: Vec<SNode>,
203 },
204 PropertyAccess {
205 object: Box<SNode>,
206 property: String,
207 },
208 OptionalPropertyAccess {
210 object: Box<SNode>,
211 property: String,
212 },
213 SubscriptAccess {
214 object: Box<SNode>,
215 index: Box<SNode>,
216 },
217 SliceAccess {
218 object: Box<SNode>,
219 start: Option<Box<SNode>>,
220 end: Option<Box<SNode>>,
221 },
222 BinaryOp {
223 op: String,
224 left: Box<SNode>,
225 right: Box<SNode>,
226 },
227 UnaryOp {
228 op: String,
229 operand: Box<SNode>,
230 },
231 Ternary {
232 condition: Box<SNode>,
233 true_expr: Box<SNode>,
234 false_expr: Box<SNode>,
235 },
236 Assignment {
237 target: Box<SNode>,
238 value: Box<SNode>,
239 op: Option<String>,
241 },
242 ThrowStmt {
243 value: Box<SNode>,
244 },
245
246 EnumConstruct {
248 enum_name: String,
249 variant: String,
250 args: Vec<SNode>,
251 },
252 StructConstruct {
254 struct_name: String,
255 fields: Vec<DictEntry>,
256 },
257
258 InterpolatedString(Vec<StringSegment>),
260 StringLiteral(String),
261 IntLiteral(i64),
262 FloatLiteral(f64),
263 BoolLiteral(bool),
264 NilLiteral,
265 Identifier(String),
266 ListLiteral(Vec<SNode>),
267 DictLiteral(Vec<DictEntry>),
268 Spread(Box<SNode>),
270 TryOperator {
272 operand: Box<SNode>,
273 },
274
275 Block(Vec<SNode>),
277 Closure {
278 params: Vec<TypedParam>,
279 body: Vec<SNode>,
280 },
281}
282
283#[derive(Debug, Clone, PartialEq)]
284pub struct MatchArm {
285 pub pattern: SNode,
286 pub body: Vec<SNode>,
287}
288
289#[derive(Debug, Clone, PartialEq)]
290pub struct SelectCase {
291 pub variable: String,
292 pub channel: Box<SNode>,
293 pub body: Vec<SNode>,
294}
295
296#[derive(Debug, Clone, PartialEq)]
297pub struct DictEntry {
298 pub key: SNode,
299 pub value: SNode,
300}
301
302#[derive(Debug, Clone, PartialEq)]
304pub struct EnumVariant {
305 pub name: String,
306 pub fields: Vec<TypedParam>,
307}
308
309#[derive(Debug, Clone, PartialEq)]
311pub struct StructField {
312 pub name: String,
313 pub type_expr: Option<TypeExpr>,
314 pub optional: bool,
315}
316
317#[derive(Debug, Clone, PartialEq)]
319pub struct InterfaceMethod {
320 pub name: String,
321 pub params: Vec<TypedParam>,
322 pub return_type: Option<TypeExpr>,
323}
324
325#[derive(Debug, Clone, PartialEq)]
327pub enum TypeExpr {
328 Named(String),
331 Union(Vec<TypeExpr>),
333 Shape(Vec<ShapeField>),
335 List(Box<TypeExpr>),
337 DictType(Box<TypeExpr>, Box<TypeExpr>),
339 FnType {
341 params: Vec<TypeExpr>,
342 return_type: Box<TypeExpr>,
343 },
344}
345
346#[derive(Debug, Clone, PartialEq)]
348pub struct ShapeField {
349 pub name: String,
350 pub type_expr: TypeExpr,
351 pub optional: bool,
352}
353
354#[derive(Debug, Clone, PartialEq)]
356pub enum BindingPattern {
357 Identifier(String),
359 Dict(Vec<DictPatternField>),
361 List(Vec<ListPatternElement>),
363}
364
365#[derive(Debug, Clone, PartialEq)]
367pub struct DictPatternField {
368 pub key: String,
370 pub alias: Option<String>,
372 pub is_rest: bool,
374}
375
376#[derive(Debug, Clone, PartialEq)]
378pub struct ListPatternElement {
379 pub name: String,
381 pub is_rest: bool,
383}
384
385#[derive(Debug, Clone, PartialEq)]
387pub struct TypeParam {
388 pub name: String,
389}
390
391#[derive(Debug, Clone, PartialEq)]
393pub struct WhereClause {
394 pub type_name: String,
395 pub bound: String,
396}
397
398#[derive(Debug, Clone, PartialEq)]
400pub struct TypedParam {
401 pub name: String,
402 pub type_expr: Option<TypeExpr>,
403 pub default_value: Option<Box<SNode>>,
404}
405
406impl TypedParam {
407 pub fn untyped(name: impl Into<String>) -> Self {
409 Self {
410 name: name.into(),
411 type_expr: None,
412 default_value: None,
413 }
414 }
415
416 pub fn typed(name: impl Into<String>, type_expr: TypeExpr) -> Self {
418 Self {
419 name: name.into(),
420 type_expr: Some(type_expr),
421 default_value: None,
422 }
423 }
424
425 pub fn names(params: &[TypedParam]) -> Vec<String> {
427 params.iter().map(|p| p.name.clone()).collect()
428 }
429
430 pub fn default_start(params: &[TypedParam]) -> Option<usize> {
432 params.iter().position(|p| p.default_value.is_some())
433 }
434}