brink-syntax-native 0.0.16

Lexer and error-resilient CST for the .brink native surface (B0.5 grammar skeleton)
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! Choice anatomy — choice points, bracket display, guards, interpolation.
//! Family for #1195.
//!
//! Parity target: `brink-syntax/src/parser/tests/choice/mod.rs`. That
//! grammar's `double_plus_choice`/`triple_plus_choice`/
//! `double_plus_choice_in_knot` tests exercise ink's stacked-bullet nesting
//! depth (`**`/`+++`), which reads as ink's implicit gather-based nesting
//! (charter §5). The native grammar has no such concept: `choice()` bumps
//! exactly one `STAR`/`PLUS` bullet token per `CHOICE`, and a second
//! `*`/`+` immediately following just becomes ordinary `CHOICE_START_CONTENT`
//! prose (see `stacked_bullets_do_not_open_a_second_choice`
//! below) — nesting is instead expressed structurally, a `CHOICE_BODY`
//! containing another `{? … }` (see
//! `choice_body_may_nest_another_choice_point`). So those three parity
//! tests have no native analogue; every other shape in the parity file is
//! covered here.

use super::*;

#[test]
fn choice_point_parses() {
    let src = "flow garden() {\n  {?\n    * [Look] You look around.\n    + (again) [Look again] Still a garden.\n    else { Nothing left to do. }\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
}

// ── Significant inter-token whitespace in content position ───────────────

#[test]
fn space_after_choice_bracket_close_survives_inside_the_text_node() {
    // `* text[] more` — the display-split anatomy. The space right after the
    // `]` bracket close is the leading char of `CHOICE_INNER_CONTENT` and
    // must be preserved AS PROSE (folded into the inner `TEXT` node), not
    // eaten as bare trivia. Regression for the `'A wager!'I returned.`
    // (native) vs `'A wager!' I returned.` (oracle) divergence.
    let src = "flow f() {\n  {?\n    * 'A wager!'[] I returned.\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());

    let inner = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_INNER_CONTENT)
        .expect("CHOICE_INNER_CONTENT");
    let inner_text = text_run_concat(&inner);
    assert_eq!(
        inner_text, " I returned.",
        "space after `]` must be folded into the inner TEXT node"
    );

    // The reconstructed choice display (start-content + inner-content, the
    // bracket content being choice-only) keeps the separating space.
    let choice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE)
        .expect("CHOICE");
    let start = choice
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_START_CONTENT)
        .expect("CHOICE_START_CONTENT");
    let display = format!("{}{}", text_run_concat(&start), inner_text);
    assert_eq!(display, "'A wager!' I returned.");
}

#[test]
fn charter_wager_shape_preserves_space_after_bracket_close() {
    // The charter's literal spelling `* 'A wager!'[] I returned. { … }`
    // (complex-flow-v1) — the bracket-close space with a trailing
    // nested-content CHOICE_BODY brace also present, exercising the G-2
    // body-open disambiguation alongside the whitespace fix.
    let src = concat!(
        "flow f() {\n",
        "  {?\n",
        "    * 'A wager!'[] I returned. {\n",
        "      -> f\n",
        "    }\n",
        "  }\n",
        "}\n",
    );
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());

    let inner = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_INNER_CONTENT)
        .expect("CHOICE_INNER_CONTENT");
    assert_eq!(text_run_concat(&inner), " I returned. ");
    // The trailing brace still opened a real CHOICE_BODY (not interpolation).
    assert!(has_node_kind(&p.syntax(), SyntaxKind::CHOICE_BODY));
}

#[test]
fn whitespace_only_inner_content_makes_no_spurious_text_node() {
    // The complement: whitespace that is NOT followed by prose (a bracket
    // close then only trailing spaces before the newline) must still be bare
    // trivia — the fix must not manufacture an empty/whitespace-only `TEXT`
    // node. `starts_text_run` returns `false` when the next non-trivia token
    // is a stop token (here `NEWLINE`), so the loop `skip_ws`es and breaks.
    let src = "flow f() {\n  {?\n    * choose[] \n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let inner = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_INNER_CONTENT)
        .expect("CHOICE_INNER_CONTENT");
    assert!(
        !has_node_kind(&inner, SyntaxKind::TEXT),
        "whitespace-only inner content must not create a TEXT node, tree: {inner:#?}"
    );
}

// ── G-2: choice-line `{expr}` interpolation ──────────────────────────

#[test]
fn choice_line_interpolation_before_bracket_parses_as_interpolation() {
    // `* Gold: {gold}` — from the README's G-2 finding: the `{` used to be
    // swallowed as a premature CHOICE_BODY open.
    let src = "flow f() {\n  {?\n    * Gold: {gold}\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let choice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE)
        .expect("CHOICE");
    assert!(has_node_kind(&choice, SyntaxKind::INTERPOLATION));
    // And no CHOICE_BODY was spuriously opened — this choice has no
    // nested-content braces at all.
    assert!(!has_node_kind(&choice, SyntaxKind::CHOICE_BODY));
}

#[test]
fn choice_line_interpolation_inside_bracket_inner_content_parses() {
    let src = "flow f() {\n  {?\n    * [Buy] You have {gold} left.\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let inner = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_INNER_CONTENT)
        .expect("CHOICE_INNER_CONTENT");
    assert!(has_node_kind(&inner, SyntaxKind::INTERPOLATION));
}

#[test]
fn choice_body_still_opens_as_a_body_not_interpolation() {
    // The flip side: a genuine multiline CHOICE_BODY brace must still be
    // recognized as CHOICE_BODY, not mis-swallowed as a (garbage)
    // interpolation expression now that plain `{` no longer stops early.
    let src = "flow f() {\n  {?\n    * [Eat] {\n      You eat. -> f\n    }\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    assert!(has_node_kind(&p.syntax(), SyntaxKind::CHOICE_BODY));
}

#[test]
fn choice_line_conditional_guard_and_trailing_interpolation_coexist() {
    // Guard braces (handled before `choice_text` even runs) plus a trailing
    // interpolation in the same choice line.
    let src = "flow f() {\n  {?\n    * {if hp > 0} Gold: {gold}\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let choice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE)
        .expect("CHOICE");
    assert!(has_node_kind(&choice, SyntaxKind::CHOICE_GUARD));
    assert!(has_node_kind(&choice, SyntaxKind::INTERPOLATION));
}

// ── CHOICE_BULLET: each kind standalone, and mixed within one point ──────

#[test]
fn star_bullet_choice_standalone() {
    let src = "flow f() {\n  {?\n    * Choice text\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let bullet = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_BULLET)
        .expect("CHOICE_BULLET");
    assert_eq!(bullet.text().to_string(), "*");
}

#[test]
fn plus_bullet_choice_standalone() {
    let src = "flow f() {\n  {?\n    + Choice text\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let bullet = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_BULLET)
        .expect("CHOICE_BULLET");
    assert_eq!(bullet.text().to_string(), "+");
}

#[test]
fn star_and_plus_bullets_mixed_within_one_choice_point() {
    let src = "flow f() {\n  {?\n    * a\n    + b\n    * c\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    assert_eq!(count_node_kind(&p.syntax(), SyntaxKind::CHOICE), 3);
    let bullets: Vec<String> = p
        .syntax()
        .descendants()
        .filter(|n| n.kind() == SyntaxKind::CHOICE_BULLET)
        .map(|n| n.text().to_string())
        .collect();
    assert_eq!(bullets, vec!["*", "+", "*"]);
}

#[test]
fn stacked_bullets_do_not_open_a_second_choice() {
    // The native-grammar complement to brink-syntax's `double_plus_choice`:
    // a second `+` immediately stacked on the first (`++`, no star
    // involved — the actual mixing of `*` and `+` within one point is
    // covered separately by `star_and_plus_bullets_mixed_within_one_choice_point`
    // above) is NOT a deeper nesting level here (there is no gather-based
    // nesting concept, charter §5 / this file's module doc) — it is
    // ordinary `CHOICE_START_CONTENT` prose, folded into the one
    // `CHOICE`'s `TEXT`.
    let src = "flow f() {\n  {?\n    ++[text] inner\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    assert_eq!(
        count_node_kind(&p.syntax(), SyntaxKind::CHOICE),
        1,
        "the second `+` must not open a second CHOICE node"
    );
    let start = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_START_CONTENT)
        .expect("CHOICE_START_CONTENT");
    assert_eq!(text_run_concat(&start), "+");
}

// ── LABEL + CHOICE_GUARD ──────────────────────────────────────────────

#[test]
fn choice_label_alone_produces_a_label_node() {
    let src = "flow f() {\n  {?\n    * (myLabel) Choice text\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let label = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::LABEL)
        .expect("LABEL");
    assert_eq!(label.text().to_string(), "(myLabel)");
}

#[test]
fn choice_guard_alone_produces_a_guard_node() {
    // Unlike brink-syntax's bare `{x > 5}` condition shape, the native
    // grammar's `CHOICE_GUARD` always requires the literal `if` keyword
    // (`choice_guard`'s `p.expect(KW_IF)`) — `{x > 5}` alone (no `if`)
    // would not even be recognized as a guard at all (the dispatch check
    // is `p.at(L_BRACE) && p.nth(1) == KW_IF`).
    let src = "flow f() {\n  {?\n    * {if x > 5} Choice text\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    assert!(has_node_kind(&p.syntax(), SyntaxKind::CHOICE_GUARD));
}

#[test]
fn choice_guard_and_label_combine_in_the_documented_order() {
    // `choice.rs`'s own doc comment on `choice()`: "bullet, optional
    // `{if cond}` guard, optional `(label)`" — guard THEN label, both
    // present together.
    let src = "flow f() {\n  {?\n    * {if visited} (again) Been here.\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let choice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE)
        .expect("CHOICE");
    let guard = choice
        .children()
        .find(|n| n.kind() == SyntaxKind::CHOICE_GUARD)
        .expect("CHOICE_GUARD");
    let label = choice
        .children()
        .find(|n| n.kind() == SyntaxKind::LABEL)
        .expect("LABEL");
    assert!(
        guard.text_range().start() < label.text_range().start(),
        "guard must precede label in child order"
    );
}

#[test]
fn label_before_guard_is_currently_not_recognized_as_a_choice_guard() {
    // GAP, not a ruling: `choice()` only checks for a guard BEFORE
    // consuming a label, never after (`choice.rs`'s own doc comment
    // reflects this implementation order, but the charter itself
    // (native-surface-charter.md §6, §11) gives `{if cond}` and `(name)`
    // as separate exhibits and is silent on their combined order).
    // brink-syntax's reference grammar — the ink-parity source of truth —
    // takes label FIRST, then condition(s)
    // (`brink-syntax/src/parser/choice.rs`'s `choice()`: `label?` before
    // `choice_condition*`), i.e. `* (name) {cond} text` is the canonical
    // ink spelling. The native parser currently rejects that spelling: a
    // `{if cond}` following a label is reparsed from scratch by the
    // generic content scanner, which recognizes it as a bare inline
    // `CONDITIONAL_BLOCK` (the annotated-brace family, charter §6)
    // instead of a `CHOICE_GUARD` — and since that shorthand form has no
    // `:`/`{` body opener here, it errors. Asserting the CURRENT (buggy)
    // behavior below, characterized as a gap, not a documented ruling —
    // see #1253 for the tracking issue on this ordering divergence.
    let src = "flow f() {\n  {?\n    * (again) {if visited} Been here.\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(
        !p.errors().is_empty(),
        "the reversed order is expected to produce an error: {:?}",
        p.errors()
    );
    let choice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE)
        .expect("CHOICE");
    assert!(!has_node_kind(&choice, SyntaxKind::CHOICE_GUARD));
    assert!(has_node_kind(&choice, SyntaxKind::CONDITIONAL_BLOCK));
}

// ── Bracket-split anatomy: isolated, and combined with tags/diverts ──────

#[test]
fn three_part_bracket_split_anatomy_in_isolation() {
    // `text[bracket]inner` — the exact three-node split, no other anatomy.
    let src = "flow f() {\n  {?\n    * Start[middle]end\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let choice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE)
        .expect("CHOICE");
    let start = choice
        .children()
        .find(|n| n.kind() == SyntaxKind::CHOICE_START_CONTENT)
        .expect("CHOICE_START_CONTENT");
    let bracket = choice
        .children()
        .find(|n| n.kind() == SyntaxKind::CHOICE_BRACKET_CONTENT)
        .expect("CHOICE_BRACKET_CONTENT");
    let inner = choice
        .children()
        .find(|n| n.kind() == SyntaxKind::CHOICE_INNER_CONTENT)
        .expect("CHOICE_INNER_CONTENT");
    assert_eq!(text_run_concat(&start), "Start");
    assert_eq!(text_run_concat(&bracket), "middle");
    assert_eq!(text_run_concat(&inner), "end");
}

#[test]
fn bracket_only_choice_with_no_start_or_inner_text() {
    // `[hidden] shown` — an empty `CHOICE_START_CONTENT` before the
    // bracket (parity: brink-syntax's `choice_with_bracket`).
    let src = "flow f() {\n  {?\n    * [hidden] shown\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let choice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE)
        .expect("CHOICE");
    let start = choice
        .children()
        .find(|n| n.kind() == SyntaxKind::CHOICE_START_CONTENT)
        .expect("CHOICE_START_CONTENT");
    assert_eq!(text_run_concat(&start), "");
    let bracket = choice
        .children()
        .find(|n| n.kind() == SyntaxKind::CHOICE_BRACKET_CONTENT)
        .expect("CHOICE_BRACKET_CONTENT");
    assert_eq!(text_run_concat(&bracket), "hidden");
}

#[test]
fn choice_text_followed_by_divert_without_a_bracket() {
    // Parity: brink-syntax's `choice_with_divert` (`* Choice -> knot`) —
    // no bracket-split anatomy at all, just prose then a bare divert.
    let src = "flow f() {\n  {?\n    * Choice -> knot\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let choice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE)
        .expect("CHOICE");
    assert!(has_node_kind(&choice, SyntaxKind::DIVERT_STMT));
    assert!(
        !has_node_kind(&choice, SyntaxKind::CHOICE_BRACKET_CONTENT),
        "no `[` was ever written, so no bracket anatomy should appear"
    );
}

#[test]
fn bracket_split_anatomy_combined_with_a_divert_in_the_bracket_region() {
    // The bracket region itself (not just the inner region, already
    // covered by `divert.rs`) can carry a divert.
    let src = "flow f() {\n  {?\n    * Go[-> elsewhere]stays\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let bracket = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_BRACKET_CONTENT)
        .expect("CHOICE_BRACKET_CONTENT");
    assert!(has_node_kind(&bracket, SyntaxKind::DIVERT_STMT));
}

// ── Trailing `#tag` on a choice line ───────────────────────────────────────
//
// #1264 (fixes #1252/#1195): unlike `content_line` (`content.rs`), which
// calls `tag_line_tail` after its `content_items_until` scan to fold
// trailing `#tag`s into the `CONTENT_LINE`, `choice_text` (`choice.rs`)
// never did — `content_items_until`'s loop unconditionally breaks on `HASH`
// (see `content.rs`'s `content_items_until` doc comment), but `choice()`
// had no follow-up call to consume it, so the `#tag` fell to the enclosing
// `choice_point` loop's `error_recover` arm and got wrapped in `ERROR`
// nodes token-by-token. `choice()` now mirrors `content_line`'s tail call,
// matching brink-syntax's `choice_with_tags` parity test.
#[test]
fn choice_line_trailing_tag_is_recognized_as_a_tag_node() {
    let src = "flow f() {\n  {?\n    * Choice #tag1\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    assert!(
        has_node_kind(&p.syntax(), SyntaxKind::TAG),
        "the `#tag1` should be a real TAG node"
    );
    assert!(
        !has_node_kind(&p.syntax(), SyntaxKind::ERROR),
        "the `#tag1` must no longer be wrapped in ERROR nodes"
    );
    let choice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE)
        .expect("CHOICE");
    assert!(
        has_node_kind(&choice, SyntaxKind::TAG),
        "the TAG node should attach to the CHOICE the tag trails"
    );
}

// ── CHOICE_BODY: braced nested-content form, including nested choices ────

#[test]
fn choice_body_multiline_multi_region_content() {
    let src = concat!(
        "flow f() {\n",
        "  {?\n",
        "    * [Eat] {\n",
        "      You eat a donut.\n",
        "      Delicious.\n",
        "    }\n",
        "  }\n",
        "}\n",
    );
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let body = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_BODY)
        .expect("CHOICE_BODY");
    assert_eq!(count_node_kind(&body, SyntaxKind::CONTENT_LINE), 2);
}

#[test]
fn choice_body_may_nest_another_choice_point() {
    // The native equivalent of brink-syntax's `nested_choice`: since there
    // is no gather-based stacking (this file's module doc), nesting is
    // spelled structurally — a `CHOICE_BODY` containing its own `{? … }`.
    let src = concat!(
        "flow f() {\n",
        "  {?\n",
        "    * outer {\n",
        "      {?\n",
        "        * inner\n",
        "      }\n",
        "    }\n",
        "  }\n",
        "}\n",
    );
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    assert_eq!(count_node_kind(&p.syntax(), SyntaxKind::CHOICE_POINT), 2);
    let outer_body = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_BODY)
        .expect("CHOICE_BODY");
    assert!(has_node_kind(&outer_body, SyntaxKind::CHOICE_POINT));
}

// ── ELSE_BRANCH: present and absent ───────────────────────────────────

#[test]
fn else_branch_present_provides_a_fallback() {
    let src = "flow f() {\n  {?\n    * a\n    else { Nothing left. }\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let point = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_POINT)
        .expect("CHOICE_POINT");
    let else_branch = point
        .children()
        .find(|n| n.kind() == SyntaxKind::ELSE_BRANCH)
        .expect("ELSE_BRANCH");
    assert!(has_node_kind(&else_branch, SyntaxKind::CHOICE_BODY));
}

#[test]
fn else_branch_absent_is_not_required() {
    let src = "flow f() {\n  {?\n    * a\n    * b\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    assert!(!has_node_kind(&p.syntax(), SyntaxKind::ELSE_BRANCH));
}

#[test]
fn else_not_immediately_followed_by_a_brace_is_not_treated_as_an_else_branch_attempt() {
    // `else` always requires a braced body (choice.rs's doc comment: "no
    // colon form, unlike IF_ARM/ELSE_BRANCH in the conditional family").
    // The `choice_point` loop's own dispatch guard is
    // `KW_ELSE if p.nth(1) == L_BRACE` — `else` NOT immediately followed
    // by `{` (trivia aside) isn't even recognized as an else-branch
    // attempt; it falls through to the loop's generic recovery arm
    // instead (one `ERROR`-wrapped token at a time), same as any other
    // unexpected token. `else_branch`'s own "expected `{` after a choice
    // point's `else`" error path is therefore unreachable through this
    // call site by construction — dispatch already guarantees `{` follows
    // whenever `else_branch` runs.
    let src = "flow f() {\n  {?\n    * a\n    else nope\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(!p.errors().is_empty());
    assert!(
        !has_node_kind(&p.syntax(), SyntaxKind::ELSE_BRANCH),
        "no ELSE_BRANCH should be attempted when `else` isn't followed by `{{`"
    );
    assert!(has_node_kind(&p.syntax(), SyntaxKind::ERROR));
}

// ── SPLICE: `<- flow(args)` in choice context ─────────────────────────

#[test]
fn splice_with_arguments_parses() {
    let src = "flow f() {\n  {?\n    <- options(1, 2)\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let splice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::SPLICE)
        .expect("SPLICE");
    assert!(has_node_kind(&splice, SyntaxKind::ARG_LIST));
}

#[test]
fn splice_without_arguments_parses() {
    let src = "flow f() {\n  {?\n    <- options\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let splice = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::SPLICE)
        .expect("SPLICE");
    assert!(!has_node_kind(&splice, SyntaxKind::ARG_LIST));
}

#[test]
fn multiple_splices_mixed_with_choices_in_one_point() {
    let src = "flow f() {\n  {?\n    * a\n    <- shared_options()\n    + b\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    assert_eq!(count_node_kind(&p.syntax(), SyntaxKind::SPLICE), 1);
    assert_eq!(count_node_kind(&p.syntax(), SyntaxKind::CHOICE), 2);
}

#[test]
fn splice_missing_a_target_path_does_not_panic() {
    let src = "flow f() {\n  {?\n    <-\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(!p.errors().is_empty());
    assert!(has_node_kind(&p.syntax(), SyntaxKind::SPLICE));
}

// ── Error recovery ─────────────────────────────────────────────────────

#[test]
fn choice_point_with_no_choices_parses_with_no_errors() {
    // An empty `{? … }` is not itself malformed — no choice lines is a
    // legal (if useless) choice point, not an error-recovery case.
    let src = "flow f() {\n  {?\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    assert!(has_node_kind(&p.syntax(), SyntaxKind::CHOICE_POINT));
    assert!(!has_node_kind(&p.syntax(), SyntaxKind::CHOICE));
}

#[test]
fn unclosed_choice_point_brace_recovers_without_panicking() {
    let src = "flow f() {\n  {?\n    * a\n";
    let p = assert_lossless(src);
    assert!(!p.errors().is_empty());
    assert!(has_node_kind(&p.syntax(), SyntaxKind::CHOICE_POINT));
}

#[test]
fn bullet_with_no_content_parses_with_an_empty_start_content() {
    // `*` alone on a line: no error — an empty `CHOICE_START_CONTENT` is
    // valid, not a recovery case.
    let src = "flow f() {\n  {?\n    *\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(p.errors().is_empty(), "errors: {:?}", p.errors());
    let start = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::CHOICE_START_CONTENT)
        .expect("CHOICE_START_CONTENT");
    assert_eq!(text_run_concat(&start), "");
}

#[test]
fn unclosed_bracket_recovers_without_panicking() {
    let src = "flow f() {\n  {?\n    * [text\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(!p.errors().is_empty());
    assert!(has_node_kind(
        &p.syntax(),
        SyntaxKind::CHOICE_BRACKET_CONTENT
    ));
}

#[test]
fn malformed_guard_missing_condition_does_not_panic() {
    let src = "flow f() {\n  {?\n    * {if} text\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(!p.errors().is_empty());
    assert!(has_node_kind(&p.syntax(), SyntaxKind::CHOICE_GUARD));
}

#[test]
fn malformed_label_non_ident_falls_back_to_prose_without_panicking() {
    // `label()` requires `L_PAREN IDENT R_PAREN`; a non-`IDENT` (a bare
    // integer here) doesn't satisfy `expect(IDENT)`, so the `LABEL` node
    // ends up containing only the `L_PAREN`, and the rest becomes ordinary
    // choice text — no panic, no infinite loop (the `(` was still real
    // forward progress).
    let src = "flow f() {\n  {?\n    * (1) text\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(!p.errors().is_empty());
    let label = p
        .syntax()
        .descendants()
        .find(|n| n.kind() == SyntaxKind::LABEL)
        .expect("LABEL");
    assert!(!has_node_kind(&label, SyntaxKind::IDENT));
}

#[test]
fn garbage_inside_choice_point_recovers_token_by_token() {
    // A token that is none of `STAR`/`PLUS`/`THREAD`/`KW_ELSE`/`R_BRACE`
    // (here a bare `=`) hits the `choice_point` loop's `_` arm, gets
    // wrapped in one `ERROR` node per token, and the loop keeps making
    // forward progress rather than spinning or panicking.
    let src = "flow f() {\n  {?\n    = = =\n    * a\n  }\n}\n";
    let p = assert_lossless(src);
    assert!(!p.errors().is_empty());
    assert!(has_node_kind(&p.syntax(), SyntaxKind::ERROR));
    // Recovery didn't eat the whole rest of the point: the trailing real
    // choice line still parses as a CHOICE.
    assert!(has_node_kind(&p.syntax(), SyntaxKind::CHOICE));
}

// ── Adversarial ─────────────────────────────────────────────────────────

#[test]
fn extremely_deep_nested_choice_points_do_not_overflow_the_stack() {
    // Pathological nesting: a `CHOICE_POINT` inside a `CHOICE_BODY` inside
    // the previous `CHOICE`, repeated well past `MAX_DEPTH` (256,
    // `parser/mod.rs`). Each level increments the shared depth counter
    // twice (`choice_point`'s own `enter_depth`, plus
    // `braced_item_list`'s for the `CHOICE_BODY`), so this must trip the
    // guard — CLAUDE.md's "guard against unbounded growth" — well before
    // Rust's own recursion limit, and recover without panicking or hanging
    // (the VM/parser step-limit rule applies here too).
    const LEVELS: usize = 150;
    let mut src = String::new();
    for _ in 0..LEVELS {
        src.push_str("{?\n* a {\n");
    }
    src.push_str("* leaf\n");
    for _ in 0..LEVELS {
        src.push_str("}\n}\n");
    }
    let wrapped = format!("flow f() {{\n{src}}}\n");
    // Lossless round-trip must hold even under the depth guard's
    // best-effort recovery path.
    let p = assert_lossless(&wrapped);
    assert!(
        p.errors()
            .iter()
            .any(|e| e.message.contains("maximum nesting depth exceeded")),
        "expected the depth guard to trip: {:?}",
        p.errors()
    );
}

#[test]
fn adversarial_structural_soup_inside_a_choice_point_never_panics() {
    // Every choice-family structural character in one line, malformed:
    // unmatched brackets/braces/parens, a bare `<-`, a bare `?`.
    let src = "flow f() {\n  {?\n    *(){}[]<-?{if}else\n  }\n}\n";
    // No specific assertion on error count/shape — the invariant under
    // test is solely "does not panic, does not hang, stays lossless"
    // (`assert_lossless` already checks the round-trip).
    assert_lossless(src);
}

// ── Local proptest generators (per this issue's own instruction: a
// family-owned generator, not a change to the shared `tests/proptest_native.rs`,
// which #1199 owns this wave) ──────────────────────────────────────────

mod choice_proptests {
    use proptest::prelude::*;

    use super::{SyntaxKind, has_node_kind, parse};

    /// Mirrors `tests/proptest_native.rs`'s own `arb_ident` keyword filter
    /// (studied for this local generator's style, per this issue's "put it
    /// in your own family file" instruction): a bare keyword tokenizes as
    /// its `KW_*` kind, not `IDENT`, which would make `label()`'s
    /// `expect(IDENT)` or `expression()`'s atom parsing fail — breaking the
    /// "well-formed input round-trips with zero errors" property below for
    /// reasons unrelated to what this generator is testing.
    const KEYWORDS: &[&str] = &[
        "pub", "flow", "fn", "var", "const", "let", "flags", "struct", "extern", "import", "use",
        "module", "return", "ref", "if", "match", "else", "while", "for", "in", "until", "break",
        "continue", "as", "or", "true", "false", "END", "DONE",
    ];

    fn arb_ident() -> impl Strategy<Value = String> {
        "[a-z][a-z0-9_]{0,6}"
            .prop_filter("must not be a keyword", |s| !KEYWORDS.contains(&s.as_str()))
    }

    fn arb_bullet() -> impl Strategy<Value = &'static str> {
        prop_oneof![Just("*"), Just("+")]
    }

    /// A choice line combining every optional piece this family owns:
    /// bullet, guard, label, bracket-split anatomy — in the grammar's
    /// documented order (guard, then label).
    fn arb_full_choice_line() -> impl Strategy<Value = String> {
        (
            arb_bullet(),
            proptest::option::of(arb_ident()),
            proptest::option::of(arb_ident()),
            arb_ident(),
            proptest::option::of(arb_ident()),
        )
            .prop_map(|(bullet, guard_cond, label, start, bracket)| {
                use std::fmt::Write as _;

                let mut line = bullet.to_string();
                if let Some(cond) = guard_cond {
                    let _ = write!(line, " {{if {cond}}}");
                }
                if let Some(name) = label {
                    let _ = write!(line, " ({name})");
                }
                line.push(' ');
                line.push_str(&start);
                if let Some(inner) = bracket {
                    let _ = write!(line, "[{inner}]");
                }
                line.push('\n');
                line
            })
    }

    fn arb_full_choice_point() -> impl Strategy<Value = String> {
        prop::collection::vec(arb_full_choice_line(), 1..=4)
            .prop_map(|lines| format!("{{?\n{}}}\n", lines.join("")))
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(128))]

        #[test]
        fn full_choice_point_roundtrips(point in arb_full_choice_point()) {
            let src = format!("flow f() {{\n{point}}}\n");
            let parsed = parse(&src);
            prop_assert_eq!(parsed.syntax().text().to_string(), src);
        }

        #[test]
        fn full_choice_point_never_panics_and_produces_a_choice_point_node(point in arb_full_choice_point()) {
            let src = format!("flow f() {{\n{point}}}\n");
            let parsed = parse(&src);
            prop_assert!(has_node_kind(&parsed.syntax(), SyntaxKind::CHOICE_POINT));
        }

        #[test]
        fn full_choice_point_produces_no_errors(point in arb_full_choice_point()) {
            // Every piece is generated in the documented, well-formed
            // shape (guard before label, valid idents), so this must be
            // clean end to end.
            let src = format!("flow f() {{\n{point}}}\n");
            let parsed = parse(&src);
            prop_assert!(parsed.errors().is_empty(), "errors: {:?}", parsed.errors());
        }
    }
}