teksilo_parse/ir.rs
1// SPDX-License-Identifier: MPL-2.0
2// SPDX-FileCopyrightText: 2026 FernTech
3
4//! Intermediate representation for the `teksu!` DSL.
5//!
6//! The parser builds an IR tree; the lowering walks the tree and emits
7//! the final builder-call token stream. Keeping parse and lower separate
8//! keeps span handling predictable: each IR node carries the span of the
9//! user token it originated from.
10
11use proc_macro2::Span;
12use syn::{Block, Expr, Ident, Local, Pat, Path};
13
14/// The root of a `teksu!` invocation.
15pub struct TeksiRoot {
16 /// If `Some(ident)`, the macro was called as `teksu!(ident => ...)`
17 /// and expansion should wrap the root in `ident.add(...)` to return
18 /// a `WidgetId`. If `None`, expansion returns the widget value
19 /// directly.
20 pub ctx: Option<Ident>,
21 pub root: TeksiElement,
22}
23
24/// An element: `Type[::ctor](args...) { body }`.
25pub struct TeksiElement {
26 /// The full callable path the user wrote. `Button("x")` stores
27 /// `Button`; `Button::new(lit!("x"))` stores the whole
28 /// `Button::new_literal` path. Lowering appends `::new` only when
29 /// `has_explicit_ctor` is false.
30 pub type_path: Path,
31 /// True when the user named a constructor explicitly (a lowercase
32 /// last path segment, per Rust naming convention). Lowering then
33 /// calls the path as-is without appending `::new`.
34 pub has_explicit_ctor: bool,
35 /// Positional arguments between the parens after the type path.
36 /// Empty when the user wrote `VStack` with no parens (equivalent to
37 /// `VStack()`).
38 pub args: Vec<Expr>,
39 /// Body items in source order.
40 pub body: Vec<BodyItem>,
41 /// Span of the type path's first segment — used for error reporting
42 /// on constructor typos.
43 pub head_span: Span,
44 /// Span of the closing `)` after the args, if the user wrote any
45 /// args parens (including empty `()`). `None` when no arg parens
46 /// were written. Consumers (e.g. the formatter) use this to find
47 /// the exact byte offset just past the args.
48 pub args_close: Option<Span>,
49 /// Span of the closing `}` of the body, if the user wrote any body
50 /// braces (including empty `{}`). `None` when no body braces were
51 /// written. Consumers use this to find the exact end of the body
52 /// for trivia attribution.
53 pub body_close: Option<Span>,
54}
55
56/// One item in an element's body block.
57#[allow(clippy::large_enum_variant)]
58pub enum BodyItem {
59 /// `name: arg1, arg2, ...` — builder method call with N args.
60 /// A bare lowercase ident with no body is modeled as `args == []`.
61 Property(TeksiProperty),
62 /// A bare element at body position — attaches via `.child(...)`.
63 Child(TeksiElement),
64 /// `name = Element` — a binding that hoists `let name = ctx.add(...)`
65 /// to the enclosing statement-forming block, then attaches via
66 /// `.add_child(name)` on the parent.
67 Binding { name: Ident, element: TeksiElement },
68 /// `#{ expr }` at body position — the expr is expected to evaluate
69 /// to a `WidgetId` and attaches via `.add_child(expr)`.
70 /// The semantics are simple: always WidgetId. The full
71 /// `IntoTeksiChild` routing (widget-or-id dispatch) is not yet implemented.
72 Escape { expr: Expr, span: Span },
73 /// `let pat = expr;` at body position. Introduces a
74 /// local whose value is used by subsequent body items. Triggers
75 /// statement-sequence lowering on the enclosing element.
76 Let(Local),
77 /// `rust { ... }` imperative escape. Two forms:
78 /// expression-producing (block tail has no semicolon, lowered as
79 /// `.child(block)`) and side-effect (block tail ends in `;`,
80 /// emitted inline as a side-effect statement).
81 Rust {
82 block: Block,
83 span: Span,
84 shape: RustShape,
85 },
86 /// `if cond { Element } [else if cond { Element }]* [else { Element }]?`
87 /// Lowers to `.child_opt(...)` (no-else) or
88 /// `.child(TeksiBranch{N}::...)` (with else branches).
89 If(TeksiIf),
90 /// `match expr { pat => Element, ... }`. Lowers to
91 /// `.child(match ... { ... TeksiBranch{N}::... })` with arms
92 /// dispatched by variant index.
93 Match(TeksiMatch),
94 /// `for pat in iter { Element }`. Lowers to
95 /// `.children(iter.map(|pat| Element))`.
96 For(TeksiFor),
97 /// `..expr` — inlines an iterator of `WidgetId`s as
98 /// children. Forces statement-sequence lowering on the parent.
99 Spread { expr: Expr, span: Span },
100}
101
102pub struct TeksiIf {
103 pub cond: Expr,
104 pub then: TeksiElement,
105 pub else_branch: Option<Box<TeksiElse>>,
106 pub span: Span,
107 /// Span of the `}` that closes this if's `then` block. Held by every
108 /// `TeksiIf` (including else-if recursion) so consumers can compute
109 /// the rightmost byte of an if-chain. Inner `TeksiElement`s carry no
110 /// braces of their own — the structural form owns them.
111 pub body_close: Span,
112}
113
114#[allow(clippy::large_enum_variant)]
115pub enum TeksiElse {
116 ElseIf(TeksiIf),
117 Element {
118 element: TeksiElement,
119 /// Span of the `}` that closes the trailing `else { ... }` block.
120 body_close: Span,
121 },
122}
123
124pub struct TeksiMatch {
125 pub scrutinee: Expr,
126 pub arms: Vec<TeksiMatchArm>,
127 pub span: Span,
128 /// Span of the `}` that closes the match block.
129 pub body_close: Span,
130}
131
132pub struct TeksiMatchArm {
133 pub pat: Pat,
134 pub guard: Option<(syn::Token![if], Expr)>,
135 pub element: TeksiElement,
136}
137
138pub struct TeksiFor {
139 pub pat: Pat,
140 pub iter: Expr,
141 pub lets: Vec<Local>,
142 pub element: TeksiElement,
143 pub span: Span,
144 /// Span of the `}` that closes the for block.
145 pub body_close: Span,
146}
147
148#[derive(Clone, Copy, PartialEq, Eq)]
149pub enum RustShape {
150 /// Block's last statement is an expression without trailing `;` —
151 /// the block's value becomes a child via `.child(block)`.
152 Expression,
153 /// Block has a unit tail (last statement ends in `;` or is a
154 /// `Stmt::Semi`) — emitted as a side-effect statement.
155 SideEffect,
156}
157
158pub struct TeksiProperty {
159 pub name: Ident,
160 pub args: Vec<PropArg>,
161}
162
163/// A property argument. `Expr` and `Element` both emit `.prop_name(...)`;
164/// `Escape` and `Binding` force the `_id` slot suffix per spec §A.3 and
165/// hoist the binding when present.
166pub enum PropArg {
167 /// A plain Rust expression (scalars, closures, method calls).
168 Expr(Expr),
169 /// An embedded teksu element — `tab: "name", Card { ... }`.
170 Element(TeksiElement),
171 /// `#{ expr }` — a WidgetId expression that routes to `.prop_id`.
172 Escape(Expr),
173 /// `name = Element` — hoists `let name = ctx.add(...)` and routes
174 /// to `.prop_id(name)`.
175 Binding { name: Ident, element: TeksiElement },
176}