1use tl_errors::Span;
9
10#[derive(Debug, Clone)]
12pub struct Program {
13 pub statements: Vec<Stmt>,
14 pub module_doc: Option<String>,
16}
17
18#[derive(Debug, Clone)]
20pub struct Stmt {
21 pub kind: StmtKind,
22 pub span: Span,
23 pub doc_comment: Option<String>,
25}
26
27#[derive(Debug, Clone)]
29pub enum UseItem {
30 Single(Vec<String>),
32 Group(Vec<String>, Vec<String>),
34 Wildcard(Vec<String>),
36 Aliased(Vec<String>, String),
38}
39
40#[derive(Debug, Clone)]
42pub struct TraitBound {
43 pub type_param: String,
44 pub traits: Vec<String>,
45}
46
47#[derive(Debug, Clone)]
49pub struct TraitMethod {
50 pub name: String,
51 pub params: Vec<Param>,
52 pub return_type: Option<TypeExpr>,
53}
54
55#[derive(Debug, Clone)]
57pub enum StmtKind {
58 Let {
60 name: String,
61 mutable: bool,
62 type_ann: Option<TypeExpr>,
63 value: Expr,
64 is_public: bool,
65 },
66
67 FnDecl {
69 name: String,
70 type_params: Vec<String>,
71 params: Vec<Param>,
72 return_type: Option<TypeExpr>,
73 bounds: Vec<TraitBound>,
74 body: Vec<Stmt>,
75 is_generator: bool,
76 is_public: bool,
77 is_async: bool,
78 },
79
80 Expr(Expr),
82
83 Return(Option<Expr>),
85
86 If {
88 condition: Expr,
89 then_body: Vec<Stmt>,
90 else_ifs: Vec<(Expr, Vec<Stmt>)>,
91 else_body: Option<Vec<Stmt>>,
92 },
93
94 While { condition: Expr, body: Vec<Stmt> },
96
97 For {
99 name: String,
100 iter: Expr,
101 body: Vec<Stmt>,
102 },
103
104 ParallelFor {
106 name: String,
107 iter: Expr,
108 body: Vec<Stmt>,
109 },
110
111 Schema {
113 name: String,
114 fields: Vec<SchemaField>,
115 is_public: bool,
116 version: Option<i64>,
118 parent_version: Option<i64>,
120 },
121
122 Migrate {
124 schema_name: String,
125 from_version: i64,
126 to_version: i64,
127 operations: Vec<MigrateOp>,
128 },
129
130 Train {
132 name: String,
133 algorithm: String,
134 config: Vec<(String, Expr)>,
135 },
136
137 Pipeline {
139 name: String,
140 extract: Vec<Stmt>,
141 transform: Vec<Stmt>,
142 load: Vec<Stmt>,
143 schedule: Option<String>,
144 timeout: Option<String>,
145 retries: Option<i64>,
146 on_failure: Option<Vec<Stmt>>,
147 on_success: Option<Vec<Stmt>>,
148 },
149
150 StreamDecl {
152 name: String,
153 source: Expr,
154 transform: Vec<Stmt>,
155 sink: Option<Expr>,
156 window: Option<WindowSpec>,
157 watermark: Option<String>,
158 },
159
160 SourceDecl {
162 name: String,
163 connector_type: String,
164 config: Vec<(String, Expr)>,
165 },
166
167 SinkDecl {
169 name: String,
170 connector_type: String,
171 config: Vec<(String, Expr)>,
172 },
173
174 StructDecl {
176 name: String,
177 type_params: Vec<String>,
178 fields: Vec<SchemaField>,
179 is_public: bool,
180 },
181
182 EnumDecl {
184 name: String,
185 type_params: Vec<String>,
186 variants: Vec<EnumVariant>,
187 is_public: bool,
188 },
189
190 ImplBlock {
192 type_name: String,
193 type_params: Vec<String>,
194 methods: Vec<Stmt>,
195 },
196
197 TryCatch {
199 try_body: Vec<Stmt>,
200 catch_var: String,
201 catch_body: Vec<Stmt>,
202 finally_body: Option<Vec<Stmt>>,
203 },
204
205 Throw(Expr),
207
208 Import { path: String, alias: Option<String> },
210
211 Test { name: String, body: Vec<Stmt> },
213
214 Use { item: UseItem, is_public: bool },
216
217 ModDecl { name: String, is_public: bool },
219
220 TraitDef {
222 name: String,
223 type_params: Vec<String>,
224 methods: Vec<TraitMethod>,
225 is_public: bool,
226 },
227
228 TraitImpl {
230 trait_name: String,
231 type_name: String,
232 type_params: Vec<String>,
233 methods: Vec<Stmt>,
234 },
235
236 LetDestructure {
238 pattern: Pattern,
239 mutable: bool,
240 value: Expr,
241 is_public: bool,
242 },
243
244 TypeAlias {
246 name: String,
247 type_params: Vec<String>,
248 value: TypeExpr,
249 is_public: bool,
250 },
251
252 Agent {
254 name: String,
255 model: String,
256 system_prompt: Option<String>,
257 tools: Vec<(String, Expr)>,
258 max_turns: Option<i64>,
259 temperature: Option<f64>,
260 max_tokens: Option<i64>,
261 base_url: Option<String>,
262 api_key: Option<String>,
263 output_format: Option<String>,
264 on_tool_call: Option<Vec<Stmt>>,
265 on_complete: Option<Vec<Stmt>>,
266 },
267
268 Break,
270
271 Continue,
273}
274
275#[derive(Debug, Clone)]
277pub struct EnumVariant {
278 pub name: String,
279 pub fields: Vec<TypeExpr>,
280}
281
282#[derive(Debug, Clone)]
284pub enum WindowSpec {
285 Tumbling(String),
287 Sliding(String, String),
289 Session(String),
291}
292
293#[derive(Debug, Clone)]
297pub enum Pattern {
298 Wildcard,
300 Literal(Expr),
302 Binding(String),
304 Enum {
306 type_name: String,
307 variant: String,
308 args: Vec<Pattern>,
309 },
310 Struct {
312 name: Option<String>,
313 fields: Vec<StructPatternField>,
314 },
315 List {
317 elements: Vec<Pattern>,
318 rest: Option<String>,
319 },
320 Or(Vec<Pattern>),
322}
323
324#[derive(Debug, Clone)]
326pub struct StructPatternField {
327 pub name: String,
328 pub pattern: Option<Pattern>,
330}
331
332#[derive(Debug, Clone)]
334pub struct MatchArm {
335 pub pattern: Pattern,
336 pub guard: Option<Expr>,
337 pub body: Expr,
338}
339
340#[derive(Debug, Clone)]
342pub enum MigrateOp {
343 AddColumn {
345 name: String,
346 type_ann: TypeExpr,
347 default: Option<Expr>,
348 },
349 DropColumn { name: String },
351 RenameColumn { from: String, to: String },
353 AlterType { column: String, new_type: TypeExpr },
355 AddConstraint { column: String, constraint: String },
357 DropConstraint { column: String, constraint: String },
359}
360
361#[derive(Debug, Clone)]
363pub enum ClosureBody {
364 Expr(Box<Expr>),
366 Block {
368 stmts: Vec<Stmt>,
369 expr: Option<Box<Expr>>,
370 },
371}
372
373#[derive(Debug, Clone)]
375pub enum Expr {
376 Int(i64),
378 Float(f64),
379 String(String),
380 Bool(bool),
381 None,
382 Decimal(String),
384
385 Ident(String),
387
388 BinOp {
390 left: Box<Expr>,
391 op: BinOp,
392 right: Box<Expr>,
393 },
394
395 UnaryOp {
397 op: UnaryOp,
398 expr: Box<Expr>,
399 },
400
401 Call {
403 function: Box<Expr>,
404 args: Vec<Expr>,
405 },
406
407 NamedArg {
409 name: String,
410 value: Box<Expr>,
411 },
412
413 Pipe {
415 left: Box<Expr>,
416 right: Box<Expr>,
417 },
418
419 Member {
421 object: Box<Expr>,
422 field: String,
423 },
424
425 Index {
427 object: Box<Expr>,
428 index: Box<Expr>,
429 },
430
431 List(Vec<Expr>),
433
434 Map(Vec<(Expr, Expr)>),
436
437 Block {
439 stmts: Vec<Stmt>,
440 expr: Option<Box<Expr>>,
441 },
442
443 Case {
445 arms: Vec<MatchArm>,
446 },
447
448 Match {
450 subject: Box<Expr>,
451 arms: Vec<MatchArm>,
452 },
453
454 Closure {
456 params: Vec<Param>,
457 return_type: Option<TypeExpr>,
458 body: ClosureBody,
459 },
460
461 Range {
463 start: Box<Expr>,
464 end: Box<Expr>,
465 },
466
467 NullCoalesce {
469 expr: Box<Expr>,
470 default: Box<Expr>,
471 },
472
473 Assign {
475 target: Box<Expr>,
476 value: Box<Expr>,
477 },
478
479 StructInit {
481 name: String,
482 fields: Vec<(String, Expr)>,
483 },
484
485 EnumVariant {
487 enum_name: String,
488 variant: String,
489 args: Vec<Expr>,
490 },
491
492 Await(Box<Expr>),
494
495 Yield(Option<Box<Expr>>),
497
498 Try(Box<Expr>),
500}
501
502#[derive(Debug, Clone, PartialEq)]
504pub enum BinOp {
505 Add,
507 Sub,
508 Mul,
509 Div,
510 Mod,
511 Pow,
512 Eq,
514 Neq,
515 Lt,
516 Gt,
517 Lte,
518 Gte,
519 And,
521 Or,
522}
523
524impl std::fmt::Display for BinOp {
525 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
526 match self {
527 BinOp::Add => write!(f, "+"),
528 BinOp::Sub => write!(f, "-"),
529 BinOp::Mul => write!(f, "*"),
530 BinOp::Div => write!(f, "/"),
531 BinOp::Mod => write!(f, "%"),
532 BinOp::Pow => write!(f, "**"),
533 BinOp::Eq => write!(f, "=="),
534 BinOp::Neq => write!(f, "!="),
535 BinOp::Lt => write!(f, "<"),
536 BinOp::Gt => write!(f, ">"),
537 BinOp::Lte => write!(f, "<="),
538 BinOp::Gte => write!(f, ">="),
539 BinOp::And => write!(f, "and"),
540 BinOp::Or => write!(f, "or"),
541 }
542 }
543}
544
545#[derive(Debug, Clone, PartialEq)]
547pub enum UnaryOp {
548 Neg,
549 Not,
550 Ref,
552}
553
554#[derive(Debug, Clone)]
556pub struct Param {
557 pub name: String,
558 pub type_ann: Option<TypeExpr>,
559}
560
561#[derive(Debug, Clone, PartialEq)]
563pub enum Annotation {
564 Sensitive,
565 Redact,
566 Pii,
567 Custom(String),
568}
569
570#[derive(Debug, Clone)]
572pub struct SchemaField {
573 pub name: String,
574 pub type_ann: TypeExpr,
575 pub doc_comment: Option<String>,
577 pub default_value: Option<Expr>,
579 pub annotations: Vec<Annotation>,
581}
582
583#[derive(Debug, Clone)]
585pub enum TypeExpr {
586 Named(String),
588 Generic { name: String, args: Vec<TypeExpr> },
590 Optional(Box<TypeExpr>),
592 Function {
594 params: Vec<TypeExpr>,
595 return_type: Box<TypeExpr>,
596 },
597}