lumen-compiler 0.1.1

The AI-Native Programming Language
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
//! Lumen Compiler
//!
//! Transforms Lumen source files (`.lm`, `.lumen`, `.lm.md`, `.lumen.md`) into LIR modules.

pub mod compiler;
pub mod diagnostics;
pub mod markdown;

use compiler::ast::{Directive, ImportDecl, ImportList, Item};
use compiler::lir::LirModule;
use compiler::resolve::SymbolTable;
use std::collections::HashSet;

use thiserror::Error;

#[derive(Debug, Error)]
pub enum CompileError {
    #[error("lex error: {0}")]
    Lex(#[from] compiler::lexer::LexError),
    #[error("parse errors: {0:?}")]
    Parse(Vec<compiler::parser::ParseError>),
    #[error("resolve errors: {0:?}")]
    Resolve(Vec<compiler::resolve::ResolveError>),
    #[error("type errors: {0:?}")]
    Type(Vec<compiler::typecheck::TypeError>),
    #[error("constraint errors: {0:?}")]
    Constraint(Vec<compiler::constraints::ConstraintError>),
}

impl From<compiler::parser::ParseError> for CompileError {
    fn from(err: compiler::parser::ParseError) -> Self {
        CompileError::Parse(vec![err])
    }
}

/// Compile with access to external modules for import resolution.
///
/// The `resolve_import` callback takes a module path (e.g., "mathlib") and returns
/// the source content of that module if it exists, or None if not found.
pub fn compile_with_imports(
    source: &str,
    resolve_import: &dyn Fn(&str) -> Option<String>,
) -> Result<LirModule, CompileError> {
    let mut compilation_stack = HashSet::new();
    compile_with_imports_internal(source, resolve_import, &mut compilation_stack, None)
}

/// Internal implementation that tracks the compilation stack for circular import detection
fn compile_with_imports_internal(
    source: &str,
    resolve_import: &dyn Fn(&str) -> Option<String>,
    compilation_stack: &mut HashSet<String>,
    _current_module: Option<&str>,
) -> Result<LirModule, CompileError> {
    // 1. Extract Markdown blocks
    let extracted = markdown::extract::extract_blocks(source);

    // 2. Build directives
    let directives: Vec<Directive> = extracted
        .directives
        .iter()
        .map(|d| Directive {
            name: d.name.clone(),
            value: d.value.clone(),
            span: d.span,
        })
        .collect();

    // 3. Concatenate all code blocks
    // 3. Concatenate all code blocks preserving line numbers
    let mut full_code = String::new();
    let mut current_line = 1;

    for block in extracted.code_blocks.iter() {
        // Pad with newlines to reach the block's start line
        while current_line < block.code_start_line {
            full_code.push('\n');
            current_line += 1;
        }

        full_code.push_str(&block.code);

        let lines_in_block = block.code.chars().filter(|&c| c == '\n').count();
        current_line += lines_in_block;
    }

    if full_code.trim().is_empty() {
        return Ok(LirModule::new("sha256:empty".to_string()));
    }

    // 4. Lex
    // We start at line 1 because we padded the code to match the file structure
    let mut lexer = compiler::lexer::Lexer::new(&full_code, 1, 0);
    let tokens = lexer.tokenize()?;

    // 5. Parse
    let mut parser = compiler::parser::Parser::new(tokens);
    let (program, parse_errors) = parser.parse_program_with_recovery(directives);
    if !parse_errors.is_empty() {
        return Err(CompileError::Parse(parse_errors));
    }

    // 6. Process imports before resolution
    let mut base_symbols = SymbolTable::new();
    let mut import_errors = Vec::new();
    let mut imported_modules: Vec<LirModule> = Vec::new();

    // Collect all imports
    let imports: Vec<&ImportDecl> = program
        .items
        .iter()
        .filter_map(|item| {
            if let Item::Import(imp) = item {
                Some(imp)
            } else {
                None
            }
        })
        .collect();

    // Process each import
    for import in imports {
        let module_path = import.path.join(".");

        // Check for circular imports
        if compilation_stack.contains(&module_path) {
            let chain: Vec<String> = compilation_stack.iter().cloned().collect();
            let chain_str = format!("{} -> {}", chain.join(" -> "), module_path);
            import_errors.push(compiler::resolve::ResolveError::CircularImport {
                module: module_path.clone(),
                chain: chain_str,
            });
            continue;
        }

        // Resolve the module source
        let imported_source = match resolve_import(&module_path) {
            Some(src) => src,
            None => {
                import_errors.push(compiler::resolve::ResolveError::ModuleNotFound {
                    module: module_path.clone(),
                    line: import.span.line,
                });
                continue;
            }
        };

        // Track this module in the compilation stack
        compilation_stack.insert(module_path.clone());

        // Recursively compile the imported module. The markdown pipeline now
        // supports fenced and unfenced source forms.
        let imported_module = compile_with_imports_internal(
            &imported_source,
            resolve_import,
            compilation_stack,
            Some(&module_path),
        )?;

        // Remove from stack after compilation
        compilation_stack.remove(&module_path);

        // Keep the compiled module for later merging
        imported_modules.push(imported_module);

        // Extract symbols from the imported module by parsing it as markdown if it has
        // fenced lumen blocks, otherwise as raw source.
        let imported_extracted = markdown::extract::extract_blocks(&imported_source);
        let (imported_code, imported_directives, imported_line, imported_offset) =
            if imported_extracted.code_blocks.is_empty() {
                (imported_source.clone(), vec![], 1, 0)
            } else {
                let mut code = String::new();
                let mut first_line = 1;
                let mut first_offset = 0;
                for (i, block) in imported_extracted.code_blocks.iter().enumerate() {
                    if i == 0 {
                        first_line = block.code_start_line;
                        first_offset = block.code_offset;
                    }
                    if !code.is_empty() {
                        code.push('\n');
                    }
                    code.push_str(&block.code);
                }
                let directives: Vec<Directive> = imported_extracted
                    .directives
                    .iter()
                    .map(|d| Directive {
                        name: d.name.clone(),
                        value: d.value.clone(),
                        span: d.span,
                    })
                    .collect();
                (code, directives, first_line, first_offset)
            };

        let mut imported_lexer =
            compiler::lexer::Lexer::new(&imported_code, imported_line, imported_offset);
        if let Ok(imported_tokens) = imported_lexer.tokenize() {
            let mut imported_parser = compiler::parser::Parser::new(imported_tokens);
            if let Ok(imported_program) = imported_parser.parse_program(imported_directives) {
                if let Ok(imported_symbols) = compiler::resolve::resolve(&imported_program) {
                    // Import the requested symbols
                    match &import.names {
                        ImportList::Wildcard => {
                            // Import all top-level definitions
                            for (name, info) in imported_symbols.cells {
                                base_symbols.import_cell(name, info);
                            }
                            for (name, info) in imported_symbols.types {
                                base_symbols.import_type(name, info);
                            }
                            for (name, type_expr) in imported_symbols.type_aliases {
                                base_symbols.import_type_alias(name, type_expr);
                            }
                        }
                        ImportList::Names(names) => {
                            for import_name in names {
                                let symbol_name = &import_name.name;
                                let local_name = import_name.alias.as_ref().unwrap_or(symbol_name);

                                // Try to find the symbol in cells, types, or type aliases
                                let mut found = false;

                                if let Some(cell_info) = imported_symbols.cells.get(symbol_name) {
                                    base_symbols.import_cell(local_name.clone(), cell_info.clone());
                                    found = true;
                                }

                                if let Some(type_info) = imported_symbols.types.get(symbol_name) {
                                    base_symbols.import_type(local_name.clone(), type_info.clone());
                                    found = true;
                                }

                                if let Some(type_expr) =
                                    imported_symbols.type_aliases.get(symbol_name)
                                {
                                    base_symbols
                                        .import_type_alias(local_name.clone(), type_expr.clone());
                                    found = true;
                                }

                                if !found {
                                    import_errors.push(
                                        compiler::resolve::ResolveError::ImportedSymbolNotFound {
                                            symbol: symbol_name.clone(),
                                            module: module_path.clone(),
                                            line: import_name.span.line,
                                        },
                                    );
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if !import_errors.is_empty() {
        return Err(CompileError::Resolve(import_errors));
    }

    // 7. Resolve with imported symbols pre-populated
    // Use resolve_with_base so imported symbols are available during resolution
    let symbols = compiler::resolve::resolve_with_base(&program, base_symbols)
        .map_err(CompileError::Resolve)?;

    // 8. Typecheck
    compiler::typecheck::typecheck(&program, &symbols).map_err(CompileError::Type)?;

    // 9. Validate constraints
    compiler::constraints::validate_constraints(&program).map_err(CompileError::Constraint)?;

    // 10. Lower to LIR
    let mut module = compiler::lower::lower(&program, &symbols, source);

    // 11. Merge imported modules
    for imported_module in imported_modules {
        module.merge(&imported_module);
    }

    Ok(module)
}

/// Compile raw .lm source with access to external modules for import resolution.
///
/// The `resolve_import` callback takes a module path (e.g., "mathlib") and returns
/// the source content of that module if it exists, or None if not found.
pub fn compile_raw_with_imports(
    source: &str,
    resolve_import: &dyn Fn(&str) -> Option<String>,
) -> Result<LirModule, CompileError> {
    let mut compilation_stack = HashSet::new();
    compile_raw_with_imports_internal(source, resolve_import, &mut compilation_stack, None)
}

/// Internal implementation for raw source compilation with imports
fn compile_raw_with_imports_internal(
    source: &str,
    resolve_import: &dyn Fn(&str) -> Option<String>,
    compilation_stack: &mut HashSet<String>,
    _current_module: Option<&str>,
) -> Result<LirModule, CompileError> {
    if source.trim().is_empty() {
        return Ok(LirModule::new("sha256:empty".to_string()));
    }

    // 1. Lex (start at line 1, offset 0)
    let mut lexer = compiler::lexer::Lexer::new(source, 1, 0);
    let tokens = lexer.tokenize()?;

    // 2. Parse (no directives for raw source)
    let mut parser = compiler::parser::Parser::new(tokens);
    let (program, parse_errors) = parser.parse_program_with_recovery(vec![]);
    if !parse_errors.is_empty() {
        return Err(CompileError::Parse(parse_errors));
    }

    // 3. Process imports before resolution
    let mut base_symbols = SymbolTable::new();
    let mut import_errors = Vec::new();
    let mut imported_modules: Vec<LirModule> = Vec::new();

    // Collect all imports
    let imports: Vec<&ImportDecl> = program
        .items
        .iter()
        .filter_map(|item| {
            if let Item::Import(imp) = item {
                Some(imp)
            } else {
                None
            }
        })
        .collect();

    // Process each import
    for import in imports {
        let module_path = import.path.join(".");

        // Check for circular imports
        if compilation_stack.contains(&module_path) {
            let chain: Vec<String> = compilation_stack.iter().cloned().collect();
            let chain_str = format!("{} -> {}", chain.join(" -> "), module_path);
            import_errors.push(compiler::resolve::ResolveError::CircularImport {
                module: module_path.clone(),
                chain: chain_str,
            });
            continue;
        }

        // Resolve the module source
        let imported_source = match resolve_import(&module_path) {
            Some(src) => src,
            None => {
                import_errors.push(compiler::resolve::ResolveError::ModuleNotFound {
                    module: module_path.clone(),
                    line: import.span.line,
                });
                continue;
            }
        };

        // Track this module in the compilation stack
        compilation_stack.insert(module_path.clone());

        // Recursively compile the imported module through the markdown pipeline,
        // which also handles unfenced source.
        let imported_module = compile_with_imports_internal(
            &imported_source,
            resolve_import,
            compilation_stack,
            Some(&module_path),
        )?;

        // Remove from stack after compilation
        compilation_stack.remove(&module_path);

        // Keep the compiled module for later merging
        imported_modules.push(imported_module);

        // Extract symbols from the imported module by parsing it as markdown if it has
        // fenced lumen blocks, otherwise as raw source.
        let imported_extracted = markdown::extract::extract_blocks(&imported_source);
        let (imported_code, imported_directives, imported_line, imported_offset) =
            if imported_extracted.code_blocks.is_empty() {
                (imported_source.clone(), vec![], 1, 0)
            } else {
                let mut code = String::new();
                let mut first_line = 1;
                let mut first_offset = 0;
                for (i, block) in imported_extracted.code_blocks.iter().enumerate() {
                    if i == 0 {
                        first_line = block.code_start_line;
                        first_offset = block.code_offset;
                    }
                    if !code.is_empty() {
                        code.push('\n');
                    }
                    code.push_str(&block.code);
                }
                let directives: Vec<Directive> = imported_extracted
                    .directives
                    .iter()
                    .map(|d| Directive {
                        name: d.name.clone(),
                        value: d.value.clone(),
                        span: d.span,
                    })
                    .collect();
                (code, directives, first_line, first_offset)
            };

        let mut imported_lexer =
            compiler::lexer::Lexer::new(&imported_code, imported_line, imported_offset);
        if let Ok(imported_tokens) = imported_lexer.tokenize() {
            let mut imported_parser = compiler::parser::Parser::new(imported_tokens);
            if let Ok(imported_program) = imported_parser.parse_program(imported_directives) {
                if let Ok(imported_symbols) = compiler::resolve::resolve(&imported_program) {
                    // Import the requested symbols
                    match &import.names {
                        ImportList::Wildcard => {
                            // Import all top-level definitions
                            for (name, info) in imported_symbols.cells {
                                base_symbols.import_cell(name, info);
                            }
                            for (name, info) in imported_symbols.types {
                                base_symbols.import_type(name, info);
                            }
                            for (name, type_expr) in imported_symbols.type_aliases {
                                base_symbols.import_type_alias(name, type_expr);
                            }
                        }
                        ImportList::Names(names) => {
                            for import_name in names {
                                let symbol_name = &import_name.name;
                                let local_name = import_name.alias.as_ref().unwrap_or(symbol_name);

                                // Try to find the symbol in cells, types, or type aliases
                                let mut found = false;

                                if let Some(cell_info) = imported_symbols.cells.get(symbol_name) {
                                    base_symbols.import_cell(local_name.clone(), cell_info.clone());
                                    found = true;
                                }

                                if let Some(type_info) = imported_symbols.types.get(symbol_name) {
                                    base_symbols.import_type(local_name.clone(), type_info.clone());
                                    found = true;
                                }

                                if let Some(type_expr) =
                                    imported_symbols.type_aliases.get(symbol_name)
                                {
                                    base_symbols
                                        .import_type_alias(local_name.clone(), type_expr.clone());
                                    found = true;
                                }

                                if !found {
                                    import_errors.push(
                                        compiler::resolve::ResolveError::ImportedSymbolNotFound {
                                            symbol: symbol_name.clone(),
                                            module: module_path.clone(),
                                            line: import_name.span.line,
                                        },
                                    );
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    if !import_errors.is_empty() {
        return Err(CompileError::Resolve(import_errors));
    }

    // 4. Resolve with imported symbols pre-populated
    let symbols = compiler::resolve::resolve_with_base(&program, base_symbols)
        .map_err(CompileError::Resolve)?;

    // 5. Typecheck
    compiler::typecheck::typecheck(&program, &symbols).map_err(CompileError::Type)?;

    // 6. Validate constraints
    compiler::constraints::validate_constraints(&program).map_err(CompileError::Constraint)?;

    // 7. Lower to LIR
    let mut module = compiler::lower::lower(&program, &symbols, source);

    // 8. Merge imported modules
    for imported_module in imported_modules {
        module.merge(&imported_module);
    }

    Ok(module)
}

/// Compile a `.lm` raw Lumen source file to a LIR module.
/// This skips markdown extraction and processes the source directly.
pub fn compile_raw(source: &str) -> Result<LirModule, CompileError> {
    if source.trim().is_empty() {
        return Ok(LirModule::new("sha256:empty".to_string()));
    }

    // 1. Lex (start at line 1, offset 0)
    let mut lexer = compiler::lexer::Lexer::new(source, 1, 0);
    let tokens = lexer.tokenize()?;

    // 2. Parse (no directives for raw source)
    let mut parser = compiler::parser::Parser::new(tokens);
    let (program, parse_errors) = parser.parse_program_with_recovery(vec![]);
    if !parse_errors.is_empty() {
        return Err(CompileError::Parse(parse_errors));
    }

    // 3. Resolve
    let symbols = compiler::resolve::resolve(&program).map_err(CompileError::Resolve)?;

    // 4. Typecheck
    compiler::typecheck::typecheck(&program, &symbols).map_err(CompileError::Type)?;

    // 5. Validate constraints
    compiler::constraints::validate_constraints(&program).map_err(CompileError::Constraint)?;

    // 6. Lower to LIR
    let module = compiler::lower::lower(&program, &symbols, source);

    Ok(module)
}

pub fn compile(source: &str) -> Result<LirModule, CompileError> {
    // 1. Extract Markdown blocks
    let extracted = markdown::extract::extract_blocks(source);

    // 2. Build directives
    let directives: Vec<Directive> = extracted
        .directives
        .iter()
        .map(|d| Directive {
            name: d.name.clone(),
            value: d.value.clone(),
            span: d.span,
        })
        .collect();

    // 3. Concatenate all code blocks preserving line numbers
    let mut full_code = String::new();
    let mut current_line = 1;

    for block in extracted.code_blocks.iter() {
        while current_line < block.code_start_line {
            full_code.push('\n');
            current_line += 1;
        }
        full_code.push_str(&block.code);
        let lines_in_block = block.code.chars().filter(|&c| c == '\n').count();
        current_line += lines_in_block;
    }

    if full_code.trim().is_empty() {
        return Ok(LirModule::new("sha256:empty".to_string()));
    }

    // 4. Lex
    let mut lexer = compiler::lexer::Lexer::new(&full_code, 1, 0);
    let tokens = lexer.tokenize()?;

    // 5. Parse
    let mut parser = compiler::parser::Parser::new(tokens);
    let (program, parse_errors) = parser.parse_program_with_recovery(directives);
    if !parse_errors.is_empty() {
        return Err(CompileError::Parse(parse_errors));
    }

    // 6. Resolve
    let symbols = compiler::resolve::resolve(&program).map_err(CompileError::Resolve)?;

    // 7. Typecheck
    compiler::typecheck::typecheck(&program, &symbols).map_err(CompileError::Type)?;

    // 8. Validate constraints
    compiler::constraints::validate_constraints(&program).map_err(CompileError::Constraint)?;

    // 9. Lower to LIR
    let module = compiler::lower::lower(&program, &symbols, source);

    Ok(module)
}

/// Format a compile error with rich diagnostics (colors, source snippets, suggestions).
///
/// This is a convenience function that wraps `diagnostics::format_compile_error`
/// and renders all diagnostics with ANSI colors for terminal display.
pub fn format_error(error: &CompileError, source: &str, filename: &str) -> String {
    diagnostics::format_compile_error(error, source, filename)
        .iter()
        .map(|d| d.render_ansi())
        .collect::<Vec<_>>()
        .join("\n")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_compile_simple() {
        let src = r#"# Test

```lumen
cell main() -> Int
  return 42
end
```
"#;
        let module = compile(src).unwrap();
        assert_eq!(module.cells.len(), 1);
        assert_eq!(module.cells[0].name, "main");
    }

    #[test]
    fn test_compile_with_record() {
        let src = r#"# Test

```lumen
record Point
  x: Int
  y: Int
end
```

```lumen
cell origin() -> Point
  return Point(x: 0, y: 0)
end
```
"#;
        let module = compile(src).unwrap();
        assert_eq!(module.types.len(), 1);
        assert_eq!(module.cells.len(), 1);
    }

    #[test]
    fn test_compile_full_example() {
        let src = r#"@lumen 1
@package "test"

# Hello World

```lumen
record Greeting
  message: String
end
```

```lumen
cell greet(name: String) -> Greeting
  let msg = "Hello, " + name
  return Greeting(message: msg)
end
```
"#;
        let module = compile(src).unwrap();
        assert_eq!(module.types.len(), 1);
        assert_eq!(module.cells.len(), 1);
        assert_eq!(module.version, "1.0.0");
    }

    #[test]
    fn test_compile_raw_collects_multiple_parse_errors() {
        let src = r#"
cell bad1() -> Int
  let x =
  return 1
end

cell bad2(param Int) -> Int
  return param
end

record Broken
  x:
end

cell bad3() -> Int
  return
end
"#;

        let err = compile_raw(src).expect_err("expected parse errors");
        match err {
            CompileError::Parse(errors) => {
                assert!(
                    errors.len() >= 3,
                    "expected at least 3 parse errors, got {}",
                    errors.len()
                );
            }
            other => panic!("expected parse errors, got {:?}", other),
        }
    }

    #[test]
    fn test_compile_markdown_collects_multiple_parse_errors() {
        let src = r#"# Broken

```lumen
cell bad1() -> Int
  let x =
  return 1
end

cell bad2(param Int) -> Int
  return param
end

record Broken
  x:
end

cell bad3() -> Int
  return
end
```
"#;

        let err = compile(src).expect_err("expected parse errors");
        match err {
            CompileError::Parse(errors) => {
                assert!(
                    errors.len() >= 3,
                    "expected at least 3 parse errors, got {}",
                    errors.len()
                );
            }
            other => panic!("expected parse errors, got {:?}", other),
        }
    }
}