macroforge_ts_quote 0.1.81

Quote macro for generating TypeScript code at compile time
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
//! Parser for the template language.
//!
//! This parser produces an AST directly from tokens, with inline placeholder
//! classification based on syntactic context.

#[cfg(test)]
mod tests;

// Submodules containing impl Parser blocks
mod comments;
mod context;
mod control_blocks;
mod decl;
mod directives;
pub mod expr;
mod helpers;
mod ident;
mod interpolation;
mod stmt;

// Re-export error types for use throughout the parser
pub use expr::errors::{
    ParseError, ParseErrorKind, ParseNodeResult, ParseOutcome, ParseResult, SourceLocation,
};

use super::ir::{
    Accessibility, Ir, IrNode, IrSpan, MatchArm, PlaceholderKind, VarDeclarator, VarKind,
};
use super::lexer::{Lexer, Token};
use super::syntax::SyntaxKind;
use proc_macro2::TokenStream;
use smallvec::SmallVec;
use std::str::FromStr;

/// Control flow markers that ALWAYS terminate expression parsing.
/// These are template-language constructs, not JS/TS syntax.
pub(super) const CONTROL_FLOW_TERMINATORS: &[SyntaxKind] = &[
    // Opening constructs: {#if}, {#for}, {#while}, {#match}
    SyntaxKind::BraceHashIf,
    SyntaxKind::BraceHashFor,
    SyntaxKind::BraceHashWhile,
    SyntaxKind::BraceHashMatch,
    // Closing constructs: {/if}, {/for}, {/while}, {/match}
    SyntaxKind::BraceSlashIfBrace,
    SyntaxKind::BraceSlashForBrace,
    SyntaxKind::BraceSlashWhileBrace,
    SyntaxKind::BraceSlashMatchBrace,
    // Continuation constructs: {:else}, {:else if}, {:case}
    SyntaxKind::BraceColonElseBrace,
    SyntaxKind::BraceColonElseIf,
    SyntaxKind::BraceColonCase,
];

/// Analysis context for placeholder classification.
/// Combines the context kind (for placeholder classification) with
/// terminators (for expression parsing termination).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Context {
    kind: ContextKind,
    terminators: SmallVec<[SyntaxKind; 4]>,
}

impl Context {
    /// Creates an expression context with the given kind and terminators (array form).
    fn expression<const N: usize>(kind: ExpressionKind, terminators: [SyntaxKind; N]) -> Self {
        Self {
            kind: ContextKind::Expression(kind),
            terminators: SmallVec::from_iter(terminators),
        }
    }

    /// Creates an expression context with the given kind and terminators (slice form).
    fn expression_from_slice(kind: ExpressionKind, terminators: &[SyntaxKind]) -> Self {
        Self {
            kind: ContextKind::Expression(kind),
            terminators: SmallVec::from_iter(terminators.iter().copied()),
        }
    }

    /// Creates a type annotation context with the given terminators.
    fn type_annotation<const N: usize>(terminators: [SyntaxKind; N]) -> Self {
        Self {
            kind: ContextKind::TypeAnnotation,
            terminators: SmallVec::from_iter(terminators),
        }
    }

    /// Creates a type assertion context with the given terminators.
    fn type_assertion<const N: usize>(terminators: [SyntaxKind; N]) -> Self {
        Self {
            kind: ContextKind::TypeAssertion,
            terminators: SmallVec::from_iter(terminators),
        }
    }

    /// Creates an identifier context with the given terminators.
    fn identifier<const N: usize>(terminators: [SyntaxKind; N]) -> Self {
        Self {
            kind: ContextKind::Identifier,
            terminators: SmallVec::from_iter(terminators),
        }
    }

    /// Creates a generic params context with the given terminators.
    fn generic_params<const N: usize>(terminators: [SyntaxKind; N]) -> Self {
        Self {
            kind: ContextKind::GenericParams,
            terminators: SmallVec::from_iter(terminators),
        }
    }

    /// Creates a statement context (no terminators needed).
    fn statement() -> Self {
        Self {
            kind: ContextKind::Statement,
            terminators: SmallVec::new(),
        }
    }

    /// Creates a parameters context with the given terminators.
    fn parameters<const N: usize>(terminators: [SyntaxKind; N]) -> Self {
        Self {
            kind: ContextKind::Parameters,
            terminators: SmallVec::from_iter(terminators),
        }
    }

    /// Creates a template control block context (for `{#if}`, `{#for}`, etc.)
    fn template_control_block<const N: usize>(terminators: [SyntaxKind; N]) -> Self {
        Self {
            kind: ContextKind::Expression(ExpressionKind::TemplateControlBlock),
            terminators: SmallVec::from_iter(terminators),
        }
    }

    /// Creates an interface member context (for property/method signatures).
    fn interface_member<const N: usize>(terminators: [SyntaxKind; N]) -> Self {
        Self {
            kind: ContextKind::InterfaceMember,
            terminators: SmallVec::from_iter(terminators),
        }
    }
}

/// The kind of context (for placeholder classification).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ContextKind {
    /// Top-level or statement context.
    Statement,
    /// Expression context with optional sub-kind.
    Expression(ExpressionKind),
    /// Type annotation context (after `:`)
    TypeAnnotation,
    /// Type assertion context (after `as`, `satisfies`, etc.)
    TypeAssertion,
    /// Generic type parameters (inside `<...>`)
    GenericParams,
    /// Function parameter list.
    Parameters,
    /// Identifier context (for function/class/variable names).
    Identifier,
    /// Interface member context (property/method signatures).
    InterfaceMember,
}

/// Sub-kinds of expression context.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
enum ExpressionKind {
    /// Normal expression context.
    #[default]
    Normal,
    /// Inside ternary conditional (after `?`, waiting for `:`)
    Ternary,
    /// Object literal expression (`:` is property separator, not type annotation)
    ObjectLiteral,
    /// Inside a template control block (`{#if}`, `{#for}`, `{#while}`, `{#match}`).
    /// This preserves the parent context so `is_object_literal()` can search the stack.
    TemplateControlBlock,
}

/// The parser for template input.
pub struct Parser {
    /// The tokens to parse.
    tokens: Vec<Token>,
    /// Current position in the token stream.
    pos: usize,
    /// Context stack for placeholder classification.
    context_stack: Vec<Context>,
    /// Full source text for debugging.
    #[allow(dead_code)]
    source: String,
    /// Pending doc comment to attach to next node.
    pending_doc: Option<String>,
    /// Pending decorators to attach to next class/function.
    pending_decorators: Vec<IrNode>,
}

/// Checkpoint for parser state, used for backtracking.
#[derive(Clone)]
pub struct Checkpoint {
    pos: usize,
    context_stack_len: usize,
    pending_doc: Option<String>,
}

impl Parser {
    /// Create a checkpoint of current parser state for backtracking.
    pub(super) fn checkpoint(&self) -> Checkpoint {
        Checkpoint {
            pos: self.pos,
            context_stack_len: self.context_stack.len(),
            pending_doc: self.pending_doc.clone(),
        }
    }

    /// Restore parser state to a previous checkpoint.
    pub(super) fn restore(&mut self, checkpoint: Checkpoint) {
        self.pos = checkpoint.pos;
        self.context_stack.truncate(checkpoint.context_stack_len);
        self.pending_doc = checkpoint.pending_doc;
        // Note: pending_decorators are not restored - they're typically consumed before checkpoints
    }

    /// Creates a new parser from input text.
    /// Panics if the input cannot be tokenized (lexer error).
    pub fn new(input: &str) -> Self {
        let tokens = Lexer::new(input).tokenize().unwrap_or_else(|e| {
            panic!("Lexer error: {}", e);
        });
        Self {
            tokens,
            pos: 0,
            context_stack: vec![Context::expression(ExpressionKind::Normal, [])],
            source: input.to_string(),
            pending_doc: None,
            pending_decorators: Vec::new(),
        }
    }

    /// Creates a new parser from input text, returning an error if lexing fails.
    pub fn try_new(input: &str) -> Result<Self, crate::compiler::lexer::LexError> {
        let tokens = Lexer::new(input).tokenize()?;
        Ok(Self {
            tokens,
            pos: 0,
            context_stack: vec![Context::expression(ExpressionKind::Normal, [])],
            source: input.to_string(),
            pending_doc: None,
            pending_decorators: Vec::new(),
        })
    }

    /// Parses the input and returns an AST.
    ///
    /// Returns an error if parsing fails with unrecoverable issues.
    pub fn parse(mut self) -> ParseResult<Ir> {
        let nodes = self.parse_nodes()?;
        Ok(Ir::with_nodes(Self::merge_adjacent_text(nodes)))
    }

    /// Execute a function with a temporary context pushed.
    /// Context is popped when function returns.
    pub(super) fn with_context<T>(&mut self, ctx: Context, f: impl FnOnce(&mut Self) -> T) -> T {
        self.context_stack.push(ctx);
        let result = f(self);
        // Use guarded pop to ensure we never empty the stack
        // (something inside f() may have already popped our context)
        if self.context_stack.len() > 1 {
            self.context_stack.pop();
        }
        result
    }

    // =========================================================================
    // Token navigation
    // =========================================================================

    fn current(&self) -> Option<&Token> {
        self.tokens.get(self.pos)
    }

    fn current_kind(&self) -> Option<SyntaxKind> {
        self.current().map(|t| t.kind)
    }

    fn at(&self, kind: SyntaxKind) -> bool {
        self.current_kind() == Some(kind)
    }

    /// Returns true if the current token is any `{#...` opening token (BraceHashIf, BraceHashFor, etc.)
    fn at_brace_hash_open(&self) -> bool {
        self.current_kind()
            .map_or(false, |k| k.is_brace_hash_open())
    }

    /// Returns true if the current token is any `{/...}` closing token (BraceSlashIfBrace, BraceSlashForBrace, etc.)
    fn at_brace_slash_close(&self) -> bool {
        self.current_kind()
            .map_or(false, |k| k.is_brace_slash_close())
    }

    /// Returns true if the current token is any `{:...` continuation token (BraceColonElseBrace, BraceColonElseIf, etc.)
    fn at_brace_colon(&self) -> bool {
        self.current_kind().map_or(false, |k| k.is_brace_colon())
    }

    fn at_eof(&self) -> bool {
        self.pos >= self.tokens.len()
    }

    fn advance(&mut self) {
        if !self.at_eof() {
            // Clone token data to avoid borrow issues
            // SAFETY: We just verified !at_eof(), so current() must return Some
            let token = self
                .current()
                .expect("advance() called when not at EOF but current() returned None");
            let (kind, text) = (token.kind, token.text.clone());

            // Update context based on the token we're consuming
            self.update_context(kind, &text);
            self.pos += 1;
        }
    }

    fn consume(&mut self) -> Option<Token> {
        if self.at_eof() {
            return None;
        }
        let token = self.tokens[self.pos].clone();
        self.advance();
        Some(token)
    }

    fn expect(&mut self, kind: SyntaxKind) -> Option<Token> {
        if self.at(kind) { self.consume() } else { None }
    }

    fn skip_whitespace(&mut self) {
        while let Some(kind) = self.current_kind() {
            if kind.is_trivia() {
                self.advance();
            } else if kind == SyntaxKind::JsDocOpen || kind == SyntaxKind::DocCommentPrefix {
                // Parse JSDoc and store in pending_doc so it's preserved in output
                if let Some(IrNode::DocComment { text, .. }) = self.parse_doc_comment() {
                    self.pending_doc = Some(text);
                }
            } else {
                break;
            }
        }
    }

    /// Returns the byte offset of the current token in the source string.
    /// If at EOF, returns the length of the source.
    pub(super) fn current_byte_offset(&self) -> usize {
        self.tokens
            .get(self.pos)
            .map(|t| t.start)
            .unwrap_or(self.source.len())
    }

    // =========================================================================
    // Parsing
    // =========================================================================

    fn parse_nodes(&mut self) -> ParseResult<Vec<IrNode>> {
        let mut nodes = Vec::new();
        let mut iterations = 0usize;
        const MAX_ITERATIONS: usize = 100_000;

        while !self.at_eof() {
            iterations += 1;
            let pos_before = self.pos;

            #[cfg(debug_assertions)]
            if std::env::var("MF_DEBUG_PARSER").is_ok() && iterations % 1000 == 0 {
                eprintln!(
                    "[MF_DEBUG_PARSER] parse_nodes iteration {}, pos={}/{}, current={:?}",
                    iterations,
                    self.pos,
                    self.tokens.len(),
                    self.current().map(|t| (&t.kind, &t.text))
                );
            }

            if iterations > MAX_ITERATIONS {
                #[cfg(debug_assertions)]
                {
                    eprintln!(
                        "[MF_DEBUG_PARSER] ERROR: parse_nodes exceeded {} iterations!",
                        MAX_ITERATIONS
                    );
                    eprintln!(
                        "[MF_DEBUG_PARSER] pos={}/{}, current={:?}",
                        self.pos,
                        self.tokens.len(),
                        self.current()
                    );
                    eprintln!(
                        "[MF_DEBUG_PARSER] source snippet: {:?}",
                        &self.source[..self.source.len().min(500)]
                    );
                }
                return Err(ParseError::new(
                    ParseErrorKind::UnterminatedExpression,
                    self.current_byte_offset(),
                )
                .with_context("parser exceeded maximum iterations (possible infinite loop)"));
            }

            match self.parse_node()? {
                ParseOutcome::Node(node) => nodes.push(node),
                ParseOutcome::Skip => {}
            }

            // Safety check: ensure we made progress
            if self.pos == pos_before && !self.at_eof() {
                #[cfg(debug_assertions)]
                {
                    eprintln!(
                        "[MF_DEBUG_PARSER] ERROR: No progress at pos={}, token={:?}",
                        self.pos,
                        self.current()
                    );
                }
                // Force progress to avoid infinite loop
                self.advance();
            }
        }

        Ok(nodes)
    }

    fn parse_node(&mut self) -> ParseNodeResult<IrNode> {
        self.parse_node_inner(0)
    }

    fn parse_node_inner(&mut self, depth: usize) -> ParseNodeResult<IrNode> {
        const MAX_DEPTH: usize = 100;
        if depth > MAX_DEPTH {
            #[cfg(debug_assertions)]
            eprintln!(
                "[MF_DEBUG_PARSER] ERROR: parse_node recursion depth {} exceeded at pos={}, token={:?}",
                depth,
                self.pos,
                self.current()
            );
            return Err(ParseError::new(
                ParseErrorKind::UnterminatedExpression,
                self.current_byte_offset(),
            )
            .with_context("parser recursion depth exceeded"));
        }

        // Check for doc comments first - store them for the next node
        let Some(current_kind) = self.current_kind() else {
            return Ok(ParseOutcome::Skip);
        };

        match current_kind {
            SyntaxKind::RustDocAttr => {
                let pos_before = self.pos;
                if let Some(IrNode::DocComment { text, .. }) = self.parse_rust_doc_attr() {
                    self.pending_doc = Some(text);
                }
                // Ensure we made progress before recursing
                if self.pos == pos_before {
                    #[cfg(debug_assertions)]
                    eprintln!(
                        "[MF_DEBUG_PARSER] WARNING: parse_rust_doc_attr didn't advance at pos={}",
                        self.pos
                    );
                    self.advance(); // Force progress
                }
                return self.parse_node_inner(depth + 1);
            }
            SyntaxKind::DocCommentPrefix | SyntaxKind::JsDocOpen => {
                let pos_before = self.pos;
                if let Some(IrNode::DocComment { text, .. }) = self.parse_doc_comment() {
                    self.pending_doc = Some(text);
                }
                // Ensure we made progress before recursing
                if self.pos == pos_before {
                    #[cfg(debug_assertions)]
                    eprintln!(
                        "[MF_DEBUG_PARSER] WARNING: parse_doc_comment didn't advance at pos={}",
                        self.pos
                    );
                    self.advance(); // Force progress
                }
                return self.parse_node_inner(depth + 1);
            }
            _ => {}
        }

        let Some(kind) = self.current_kind() else {
            return Ok(ParseOutcome::Skip);
        };

        let node: Option<IrNode> = match kind {
            SyntaxKind::At => {
                let start_byte = self.current_byte_offset();
                match self.parse_interpolation() {
                    Ok(placeholder) => {
                        // Check if there's an identifier suffix immediately after (e.g., @{name}Obj)
                        if let Some(token) = self.current() {
                            if token.kind == SyntaxKind::Ident {
                                let suffix_token = token.clone();
                                self.consume();
                                // Force placeholder to be Ident kind for identifier concatenation
                                let ident_placeholder = match placeholder {
                                    IrNode::Placeholder { span, expr, .. } => IrNode::Placeholder {
                                        span,
                                        kind: PlaceholderKind::Ident,
                                        expr,
                                    },
                                    other => other,
                                };
                                // Create an IdentBlock combining placeholder + suffix
                                return Ok(ParseOutcome::Node(IrNode::IdentBlock {
                                    span: IrSpan::new(start_byte, self.current_byte_offset()),
                                    parts: vec![ident_placeholder, IrNode::ident(&suffix_token)],
                                }));
                            }
                        }
                        Some(placeholder)
                    }
                    Err(e) => return Err(e),
                }
            }
            // Template control flow opening constructs
            SyntaxKind::BraceHashIf => Some(self.parse_if_block_from_token()?),
            SyntaxKind::BraceHashFor => Some(self.parse_for_block_from_token()?),
            SyntaxKind::BraceHashWhile => Some(self.parse_while_block_from_token()?),
            SyntaxKind::BraceHashMatch => Some(self.parse_match_block_from_token()?),
            // Template control flow closing constructs - consume and skip
            SyntaxKind::BraceSlashIfBrace
            | SyntaxKind::BraceSlashForBrace
            | SyntaxKind::BraceSlashWhileBrace
            | SyntaxKind::BraceSlashMatchBrace => {
                // End of control block - consume the closing token
                self.consume();
                return Ok(ParseOutcome::Skip);
            }
            // Template control flow continuation constructs - error at top level
            SyntaxKind::BraceColonElseBrace
            | SyntaxKind::BraceColonElseIf
            | SyntaxKind::BraceColonCase => {
                // Else/case clause at top level - error, consume
                self.consume();
                return Ok(ParseOutcome::Skip);
            }
            SyntaxKind::DollarOpen => self.parse_directive(),
            SyntaxKind::PipeOpen => Some(self.parse_ident_block()?),
            SyntaxKind::CommentLineOpen => self.parse_line_comment(),
            SyntaxKind::CommentBlockOpen => self.parse_block_comment(),
            SyntaxKind::DoubleQuote => Some(self.parse_string_literal()?),
            SyntaxKind::Backtick => Some(self.parse_template_literal()?),
            // TypeScript decorator - collect and attach to next class/function
            SyntaxKind::DecoratorAt => {
                if let Some(decorator) = self.parse_decorator_raw()? {
                    self.pending_decorators.push(decorator);
                }
                // Continue to parse the next node (the decorated class/function)
                return self.parse_node_inner(depth + 1);
            }
            // TypeScript declarations
            SyntaxKind::ExportKw => Some(self.parse_export_decl()?),
            SyntaxKind::ImportKw => self.parse_import_decl(),
            SyntaxKind::EnumKw => Some(self.parse_enum_decl(false, false)?),
            SyntaxKind::ClassKw => Some(self.parse_class_decl(false)?),
            SyntaxKind::FunctionKw => Some(self.parse_function_decl(false, false)?),
            SyntaxKind::InterfaceKw => Some(self.parse_interface_decl(false)?),
            SyntaxKind::ConstKw => {
                // Check for `const enum`
                if self.peek_is_enum() {
                    Some(self.parse_enum_decl(false, true)?)
                } else {
                    Some(self.parse_var_decl(false)?)
                }
            }
            SyntaxKind::LetKw | SyntaxKind::VarKw => Some(self.parse_var_decl(false)?),
            SyntaxKind::AsyncKw => Some(self.parse_async_decl(false)?),
            // Interface/type members (can appear in for-loop bodies inside interfaces)
            SyntaxKind::ReadonlyKw => self.parse_maybe_interface_member()?,
            // Block statement at module level - use lookahead to distinguish from object literal
            SyntaxKind::LBrace if self.looks_like_block_stmt() => Some(self.parse_block_stmt()?),
            // TypeScript statements that can appear at module level
            SyntaxKind::IfKw => Some(self.parse_ts_if_stmt()?),
            SyntaxKind::ForKw | SyntaxKind::WhileKw => Some(self.parse_ts_loop_stmt()?),
            SyntaxKind::TryKw => Some(self.parse_ts_try_stmt()?),
            SyntaxKind::ReturnKw => Some(self.parse_return_stmt()?),
            SyntaxKind::ThrowKw => Some(self.parse_throw_stmt()?),
            _ => self.parse_text_token(),
        };

        // Wrap with pending doc comment if present
        match node {
            Some(n) => {
                let wrapped = if let Some(doc) = self.pending_doc.take() {
                    let span = n.span();
                    IrNode::Documented {
                        span,
                        doc,
                        inner: Box::new(n),
                    }
                } else {
                    n
                };
                Ok(ParseOutcome::Node(wrapped))
            }
            None => Ok(ParseOutcome::Skip),
        }
    }

    /// Parse a TypeScript decorator and pass through as text.
    /// Handles: `@decorator`, `@decorator(args)`, `@decorator.member`, etc.
    fn parse_decorator_raw(&mut self) -> ParseResult<Option<IrNode>> {
        let start_byte = self.current_byte_offset();
        // The DecoratorAt token is just "@", we need to collect the full decorator
        self.consume(); // consume @
        let mut parts = vec![IrNode::StrLit {
            span: IrSpan::new(start_byte, start_byte + 1),
            value: "@".to_string(),
        }];

        // Consume the decorator identifier/expression
        // This could be: identifier, member access (a.b.c), or call (fn(args))
        let mut paren_depth = 0;

        loop {
            match self.current_kind() {
                Some(SyntaxKind::Ident) => {
                    if let Some(t) = self.consume() {
                        parts.push(IrNode::ident(&t));
                    }
                }
                Some(SyntaxKind::At) => {
                    // Interpolation inside decorator
                    let placeholder = self.parse_interpolation()?;
                    parts.push(placeholder);
                }
                Some(SyntaxKind::LParen) => {
                    paren_depth += 1;
                    if let Some(t) = self.consume() {
                        parts.push(IrNode::ident(&t));
                    }
                }
                Some(SyntaxKind::RParen) => {
                    paren_depth -= 1;
                    if let Some(t) = self.consume() {
                        parts.push(IrNode::ident(&t));
                    }
                    if paren_depth <= 0 {
                        break;
                    }
                }
                Some(SyntaxKind::Dot)
                | Some(SyntaxKind::Colon)
                | Some(SyntaxKind::Comma)
                | Some(SyntaxKind::LBrace)
                | Some(SyntaxKind::RBrace)
                | Some(SyntaxKind::LBracket)
                | Some(SyntaxKind::RBracket)
                | Some(SyntaxKind::Lt)
                | Some(SyntaxKind::Gt)
                | Some(SyntaxKind::DoubleQuote)
                | Some(SyntaxKind::SingleQuote)
                | Some(SyntaxKind::Text)
                | Some(SyntaxKind::Eq) => {
                    if paren_depth > 0 {
                        // Inside parens, consume everything
                        if self.at(SyntaxKind::DoubleQuote) {
                            let s = self.parse_string_literal()?;
                            parts.push(s);
                        } else if let Some(t) = self.consume() {
                            parts.push(IrNode::ident(&t));
                        }
                    } else {
                        // Outside parens, we're done with the decorator
                        break;
                    }
                }
                Some(SyntaxKind::Whitespace) => {
                    if paren_depth > 0 {
                        // Consume whitespace inside parens
                        if let Some(t) = self.consume() {
                            parts.push(IrNode::ident(&t));
                        }
                    } else {
                        // Whitespace after decorator, we're done
                        break;
                    }
                }
                _ => {
                    if paren_depth > 0 {
                        // Inside parens, consume anything
                        if let Some(t) = self.consume() {
                            parts.push(IrNode::ident(&t));
                        }
                    } else {
                        break;
                    }
                }
            }
        }

        // Merge and return
        let merged = Self::merge_adjacent_text(parts);
        if merged.len() == 1 {
            Ok(Some(merged.into_iter().next().unwrap()))
        } else {
            Ok(Some(IrNode::IdentBlock {
                span: IrSpan::new(start_byte, self.current_byte_offset()),
                parts: merged,
            }))
        }
    }

    // =========================================================================
    // Control block handlers (called from parse_node for new token types)
    // =========================================================================

    fn parse_if_block_from_token(&mut self) -> ParseResult<IrNode> {
        self.parse_control_block(SyntaxKind::BraceHashIf)
    }

    fn parse_for_block_from_token(&mut self) -> ParseResult<IrNode> {
        self.parse_control_block(SyntaxKind::BraceHashFor)
    }

    fn parse_while_block_from_token(&mut self) -> ParseResult<IrNode> {
        self.parse_control_block(SyntaxKind::BraceHashWhile)
    }

    fn parse_match_block_from_token(&mut self) -> ParseResult<IrNode> {
        self.parse_control_block(SyntaxKind::BraceHashMatch)
    }

    fn parse_text_token(&mut self) -> Option<IrNode> {
        let token = self.consume()?;
        Some(IrNode::ident(&token))
    }

    fn consume_until_rbrace(&mut self) {
        while !self.at_eof() && !self.at(SyntaxKind::RBrace) {
            self.advance();
        }
        self.expect(SyntaxKind::RBrace);
    }
}