badness 0.12.0

A language server, formatter, and linter for LaTeX
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
//! Phase 1 parser tests: tree-shape snapshots over representative inputs, plus
//! targeted assertions on error-recovery behaviour. Every case also re-checks
//! the losslessness invariant. Regenerate snapshots with `task snapshots`.

use badness::parser::parse;
use badness::syntax::{SyntaxKind, SyntaxNode};
use rowan::NodeOrToken;

/// Render a CST as an indented `KIND@range` tree, with token text, followed by
/// any syntax errors. Stable and snapshot-friendly.
fn tree(input: &str) -> String {
    let parsed = parse(input);
    // Losslessness must hold for every input the parser sees.
    assert_eq!(
        parsed.syntax().to_string(),
        input,
        "losslessness violated for {input:?}"
    );

    let mut out = String::new();
    render(&parsed.syntax(), 0, &mut out);
    for err in &parsed.errors {
        out.push_str(&format!(
            "error @{}..{}: {}\n",
            err.start, err.end, err.message
        ));
    }
    out
}

fn render(node: &SyntaxNode, depth: usize, out: &mut String) {
    out.push_str(&format!(
        "{:indent$}{:?}@{:?}\n",
        "",
        node.kind(),
        node.text_range(),
        indent = depth * 2
    ));
    for child in node.children_with_tokens() {
        match child {
            NodeOrToken::Node(n) => render(&n, depth + 1, out),
            NodeOrToken::Token(t) => out.push_str(&format!(
                "{:indent$}{:?}@{:?} {:?}\n",
                "",
                t.kind(),
                t.text_range(),
                t.text(),
                indent = (depth + 1) * 2
            )),
        }
    }
}

#[test]
fn command_with_required_and_optional_args() {
    insta::assert_snapshot!(tree(r"\cmd[opt]{req}"));
}

#[test]
fn nested_groups() {
    insta::assert_snapshot!(tree(r"{a {b} c}"));
}

#[test]
fn environment_with_body() {
    insta::assert_snapshot!(tree("\\begin{itemize}\n\\item x\n\\end{itemize}"));
}

#[test]
fn inline_and_display_math() {
    insta::assert_snapshot!(tree(r"$x^2$ and \[ y_i \]"));
}

#[test]
fn display_math_dollars() {
    insta::assert_snapshot!(tree(r"$$a + b$$"));
}

#[test]
fn math_scripts_bind_to_base() {
    // Sub/superscripts in either order, a bare-group base, a command script
    // argument, and a nested script inside a `{…}` group. Atoms are separated by
    // `\,` (a control symbol that takes no arguments, so it does not greedily
    // swallow the following group the way a control word would).
    insta::assert_snapshot!(tree(
        r"$x^{n+1} \, a_i^2 \, x^2_i \, {a+b}^2 \, x^\alpha \, x^{a_b}$"
    ));
}

#[test]
fn left_right_pair() {
    // `\left( … \right)`: the `\left`/`\right` and their delimiter tokens are
    // direct children, the enclosed atoms wrapped in a `MATH` body.
    insta::assert_snapshot!(tree(r"$\left( x + y \right)$"));
}

#[test]
fn left_right_nested_and_scripted() {
    // Nested pairs recurse, and a script after `\right)` binds to the whole pair
    // (the `SCRIPTED` wraps the `LEFT_RIGHT`). The inner `\left[`/`\right]` is a
    // separate pair.
    insta::assert_snapshot!(tree(r"$\left[ \left( a \right) \right]^2$"));
}

#[test]
fn left_right_control_word_delimiters() {
    // A control-word delimiter (`\langle`/`\rangle`) is the delimiter token; a
    // control-symbol one (`\|`) likewise.
    insta::assert_snapshot!(tree(r"$\left\langle x \right\rangle$"));
}

#[test]
fn unclosed_left_demotes_without_diagnostic() {
    // `\left(` with no reachable `\right` before the closing `$`: shape-gated like
    // `\[` (issue #77), the `\left` stays an ordinary command and the `(` an
    // ordinary atom — no `LEFT_RIGHT` node, **no diagnostic** (a likely typo is
    // linter territory), and nothing corrupted.
    let parsed = parse(r"$\left( x $");
    assert_eq!(parsed.syntax().to_string(), r"$\left( x $");
    assert!(
        !tree(r"$\left( x $").contains("LEFT_RIGHT"),
        "unclosed `\\left` must not open a LEFT_RIGHT node"
    );
    let messages: Vec<&str> = parsed.errors.iter().map(|e| e.message.as_str()).collect();
    assert!(messages.is_empty(), "unexpected diagnostics: {messages:?}");
}

#[test]
fn left_right_pairs_through_nested_environments() {
    // The shape gate must reach the `\right` across a nested `\begin`/`\end`
    // (`matrix` holding `pmatrix` cells), so a well-formed pair still opens a
    // `LEFT_RIGHT` and its `\right` is never orphaned (issue #77 regression: a
    // gate that miscounted nested environments demoted this valid pair).
    let src = r"$\left\{\begin{matrix}\begin{pmatrix}1\end{pmatrix}\end{matrix}\right\}$";
    let parsed = parse(src);
    assert_eq!(parsed.syntax().to_string(), src);
    assert!(
        tree(src).contains("LEFT_RIGHT"),
        "a balanced `\\left…\\right` around nested environments must pair"
    );
    assert!(
        parsed.errors.is_empty(),
        "unexpected diagnostics: {:?}",
        parsed
            .errors
            .iter()
            .map(|e| e.message.as_str())
            .collect::<Vec<_>>()
    );
}

#[test]
fn stray_right_reports() {
    // A `\right)` with no open `\left`: reported, consumed with its delimiter,
    // still lossless.
    let parsed = parse(r"$x \right) y$");
    assert_eq!(parsed.syntax().to_string(), r"$x \right) y$");
    let messages: Vec<&str> = parsed.errors.iter().map(|e| e.message.as_str()).collect();
    assert_eq!(messages, ["`\\right` without matching `\\left`"]);
}

#[test]
fn left_missing_delimiter_recovers() {
    // `\left` immediately followed by `\right` (no delimiters): one error per
    // missing delimiter, lossless.
    let parsed = parse(r"$\left \right$");
    assert_eq!(parsed.syntax().to_string(), r"$\left \right$");
    let messages: Vec<&str> = parsed.errors.iter().map(|e| e.message.as_str()).collect();
    assert_eq!(
        messages,
        [
            "missing delimiter after `\\left`",
            "missing delimiter after `\\right`"
        ]
    );
}

#[test]
fn math_script_with_no_base() {
    // A leading `^` has no base atom: the `^` is consumed as a bare token and `2`
    // as the next atom — no SCRIPTED wrapper (the `^` has nothing to bind to).
    insta::assert_snapshot!(tree(r"$^2$"));
}

#[test]
fn math_script_missing_argument_recovers() {
    // `^` with no argument before the closing `$`: one recovery error, lossless.
    let parsed = parse(r"$x^$");
    assert_eq!(parsed.syntax().to_string(), r"$x^$");
    let messages: Vec<&str> = parsed.errors.iter().map(|e| e.message.as_str()).collect();
    assert_eq!(messages, ["missing argument after `^`/`_`"]);
}

#[test]
fn math_script_missing_argument_at_math_end_recovers() {
    // `^` right before the closing `$`: a missing-argument error, and nothing
    // is corrupted. (A bare `$x^` at EOF no longer opens math at all — the
    // `$` shape gate keeps a closer-less dollar plain — so the closing `$` is
    // what routes this through math recovery.)
    let parsed = parse(r"$x^$");
    assert_eq!(parsed.syntax().to_string(), r"$x^$");
    assert!(
        parsed
            .errors
            .iter()
            .any(|e| e.message == "missing argument after `^`/`_`"),
        "missing-argument is reported"
    );
}

#[test]
fn math_open_interval_bracket_stays_plain() {
    // French open-interval notation (#23): the `[` after `\num{0.5}` has no `]`
    // before the math ends, so it is an ordinary math atom, not the start of an
    // optional argument — no errors, and the closing `$` is not swallowed.
    insta::assert_snapshot!(tree(r"$]0;\num{0.5}[$"));
}

#[test]
fn math_optional_attaches_when_closed() {
    // A `[` with its `]` inside the math still attaches as an optional argument
    // (the interval gate must not over-fire).
    insta::assert_snapshot!(tree(r"$\sqrt[3]{x}$"));
}

#[test]
fn math_spaced_bracket_stays_content() {
    // #43: inside math a `[` separated from its command by whitespace is a
    // delimiter or interval, not an optional argument — `\bE [ x + y ]` keeps
    // the brackets as plain math atoms. (Real math optionals are written
    // tight: `\sqrt[3]{x}`.)
    insta::assert_snapshot!(tree(r"$\bE [ x + y ] .$"));
}

#[test]
fn big_delimiter_bracket_is_never_an_optional() {
    // #43: the delimiter-size commands size the delimiter that follows, so
    // even a tight `\Big[ … \Big]` never attaches the bracket as an optional
    // argument.
    insta::assert_snapshot!(tree(r"$\Big[ x + y \Big]$"));
}

#[test]
fn spaced_bracket_in_text_body_nested_in_math_stays_content() {
    // #43: an unknown environment's body parses in text mode, but lexically it
    // still sits inside `\[ … \]` — the math gate (spaced `[` is content)
    // must keep applying there, or `\Big [ … ]` inside a user alignment
    // environment turns into a bogus optional argument.
    insta::assert_snapshot!(tree(
        "\\[ \\begin{myaligned}[t] \\bE \\Big [ x + y \\Big ] \\end{myaligned} \\]"
    ));
}

#[test]
fn optional_never_attaches_across_newline() {
    // #43: a next-line `[` is content, never an argument — `\begin{align}`
    // followed by a newline and `[\partial_\mu V]_1` must not pull the bracket
    // into the BEGIN and orphan the subscript.
    insta::assert_snapshot!(tree("\\begin{align}\n[a]_1 & = b\n\\end{align}"));
}

#[test]
fn text_spaced_optional_still_attaches() {
    // The math gate is math-only: in prose a same-line spaced `[…]` after a
    // command still attaches greedily (`\item [label]` stays an optional).
    insta::assert_snapshot!(tree(r"\item [label] text"));
}

#[test]
fn text_unreachable_bracket_stays_plain() {
    // #60: the text-mode bracket gate — a `[` with no reachable `]` (here EOF)
    // is data, not an argument: it stays an ordinary token, no OPTIONAL and no
    // diagnostic, mirroring the `$` shape gate.
    let parsed = parse(r"\cmd[oops");
    assert_eq!(parsed.syntax().to_string(), r"\cmd[oops");
    assert!(parsed.errors.is_empty(), "{:?}", parsed.errors);
    insta::assert_snapshot!(tree(r"\cmd[oops"));
}

#[test]
fn ifnextchar_bracket_stays_plain() {
    // #60: `\@ifnextchar [\@xmpar\@ympar}` inside a `\def` body — the `[` is
    // the character being tested for, and its scan hits the unbalanced `}`
    // before any `]`, so it must not attach and swallow the group closer.
    let src = r"\def\marginpar{\begingroup \@ifnextchar [\@xmpar\@ympar}";
    let parsed = parse(src);
    assert_eq!(parsed.syntax().to_string(), src);
    assert!(parsed.errors.is_empty(), "{:?}", parsed.errors);
    insta::assert_snapshot!(tree(src));
}

#[test]
fn paragraphs_split_on_blank_lines() {
    insta::assert_snapshot!(tree("First line,\nsame paragraph.\n\nSecond paragraph."));
}

#[test]
fn blank_line_in_dollar_math_keeps_the_dollars_plain() {
    // #35: a blank line inside would-be `$…$` in a tabular cell. A blank line
    // bounds the `$` shape gate's closer scan (as it bounds math in TeX), so
    // neither dollar opens math: both stay ordinary tokens, the cell and the
    // tabular parse intact, and nothing downstream is corrupted.
    let text = "\\begin{tabular}{c}\n  $a =\n\n  b$ \\\\\n\\end{tabular}\n";
    let parsed = parse(text);
    assert_eq!(parsed.syntax().to_string(), text);
    assert_eq!(
        parsed.errors,
        [],
        "gated dollars are plain tokens, not errors"
    );
}

#[test]
fn comment_line_does_not_split_paragraph() {
    // `\n %comment \n` is two line-ends around a comment-only line, not a
    // blank line, so it must stay one paragraph (not a `\par` boundary).
    let out = tree("First line.\n% an aside\nSame paragraph.");
    assert_eq!(out.matches("PARAGRAPH@").count(), 1, "{out}");
}

#[test]
fn comment_line_does_not_close_display_math() {
    // A comment line inside `\[ … \]` previously read as a blank line and
    // closed the math early, orphaning the `\]`. It must parse as one block.
    let out = tree("\\[\n  a = b\n  % aligned variant, commented out\n  + c\n\\]");
    assert!(!out.contains("error @"), "unexpected parse error:\n{out}");
    assert_eq!(out.matches("DISPLAY_MATH@").count(), 1, "{out}");
}

#[test]
fn blank_line_before_comment_still_breaks_math() {
    // A genuine blank line preceding a comment line is still a `\par`: the
    // comment-reset must not erase a break already seen, so the `\]` past it
    // is unreachable — the gated `\[` stays a plain token (no math, no
    // diagnostic) and the orphan `\]` diagnoses instead.
    let out = tree("\\[\n  a = b\n\n  % stray\n  c\n\\]");
    assert!(out.contains("unmatched `\\]`"), "{out}");
    assert!(!out.contains("DISPLAY_MATH"), "{out}");
}

// --- leading comment-bind (AGENTS.md #9) ---------------------------------

#[test]
fn comment_binds_leading_into_command() {
    // An own-line `%` run immediately before a command attaches *leading* into
    // the `COMMAND` node, before the control word.
    insta::assert_snapshot!(tree("% c\n\\section{A}"));
}

#[test]
fn comment_binds_leading_into_environment() {
    // The bound comment sits inside `ENVIRONMENT`, before `BEGIN`; a lone block
    // environment stays bare (no `PARAGRAPH` wrapper).
    insta::assert_snapshot!(tree("% caption\n\\begin{figure}\nx\n\\end{figure}"));
}

#[test]
fn comment_run_binds_as_a_whole() {
    // A contiguous run of own-line comments all bind into the construct.
    insta::assert_snapshot!(tree("% a\n% b\n\\foo"));
}

#[test]
fn trailing_same_line_comment_does_not_bind() {
    // `% x` shares `\foo`'s line (no newline before it), so it is a trailing
    // comment and floats — it does not bind into the following `\bar`.
    let out = tree("\\foo % x\n\\bar");
    // The comment is a direct child of the paragraph, not inside the second
    // COMMAND: there are two sibling COMMAND nodes and the COMMENT floats.
    assert_eq!(out.matches("COMMAND@").count(), 2, "{out}");
    insta::assert_snapshot!(out);
}

#[test]
fn blank_line_breaks_the_leading_bind() {
    // A blank line between the comment and the construct breaks the bind: the
    // comment floats at the enclosing level, the `\foo` starts a fresh paragraph.
    insta::assert_snapshot!(tree("% a\n\n\\foo"));
}

#[test]
fn comment_after_blank_line_still_binds() {
    // `%a` floats (blank line before `\foo`), but `%b` — with no blank line
    // between it and `\foo` — binds. The bind is the maximal blank-line-free
    // suffix.
    //
    // This is the deliberate divergence from RA's `n_attached_trivias`: RA would
    // peek past the blank line and attach `%a` too (it treats a trailing outer doc
    // comment `///`/`//!` as continuing across the gap). LaTeX's single catcode-14
    // `%` has no such intent marker, and attaching `%a` would wrongly glue a
    // license/copyright header into the construct, so we stop at the blank line.
    // See AGENTS.md #9 and `Parser::binding_run`.
    insta::assert_snapshot!(tree("%a\n\n%b\n\\foo"));
}

#[test]
fn comment_does_not_bind_into_non_documentable() {
    // Math, words, and other non-command/-environment tokens are not
    // documentable: the comment floats.
    let math = tree("% c\n$x$");
    assert!(
        !math.contains("COMMAND@") && math.contains("INLINE_MATH@"),
        "{math}"
    );
    let word = tree("% c\nword");
    assert!(!word.contains("COMMAND@"), "{word}");
}

#[test]
fn verbatim_environment_is_opaque() {
    insta::assert_snapshot!(tree(
        "\\begin{verbatim}\n\\notacommand $x$ %literal\n\\end{verbatim}"
    ));
}

#[test]
fn inline_verb_is_a_single_token() {
    insta::assert_snapshot!(tree(r"text \verb|$x$| more"));
}

#[test]
fn brace_verbatim_command_is_opaque() {
    // `\code`'s brace argument is verbatim (jss `\@makeother\$`): the `$` is a
    // literal, not math, so no "unclosed `$`" and the body is one VERB token.
    let out = tree(r"\code{$ pip install x_y}");
    assert!(!out.contains("error @"), "{out}");
    assert!(
        !out.contains("DOLLAR@") && !out.contains("INLINE_MATH@"),
        "{out}"
    );
    assert!(out.contains(r#"VERB@5..24 "{$ pip install x_y}""#), "{out}");
}

#[test]
fn braced_only_verbatim_command_without_brace_lexes_normally() {
    // The delimiter form is opt-in per signature. `\code` is braced-only (jss),
    // so a non-brace follower means this `\code` is some unrelated user macro —
    // here the HoTT book's math operator (issue #53). Capturing `:A+B\to\type$ …`
    // as a `:`-delimited verb run would swallow the closing `$` and cascade into
    // unbalanced-delimiter diagnostics.
    let out = tree(r"a family $\code:A+B\to\type$ and $\code(\inl(a))$ here");
    assert!(!out.contains("error @"), "{out}");
    assert!(!out.contains("VERB@"), "{out}");

    // TikZ's `\path (0,0) …` collides with the url package's braced-only `\path`
    // the same way.
    let out = tree(r"\path (0,0) -- (1,1);");
    assert!(!out.contains("error @"), "{out}");
    assert!(!out.contains("VERB@"), "{out}");
}

#[test]
fn delimiter_verbatim_command_is_opaque() {
    // The `VERB` body attaches *into* the command (a child, like any greedy
    // argument — decision #8), not as a stranded sibling.
    insta::assert_snapshot!(tree(r"\lstinline|x_$y$|"));
}

#[test]
fn brace_verbatim_command_argument_is_a_child() {
    // `\url{…}`: the brace-delimited verbatim body is the command's argument, so
    // it nests under the `COMMAND` node rather than floating beside it.
    insta::assert_snapshot!(tree(r"\url{a_$b$}"));
}

#[test]
fn verbatim_command_skips_leading_args() {
    // `\mintinline{lang}{code}`: the language is an ordinary group, only the
    // trailing argument is verbatim. Both the group and the `VERB` body nest
    // under the command, which therefore spans the whole construct.
    let out = tree(r"\mintinline{python}{x = $1}");
    assert!(!out.contains("error @"), "{out}");
    assert!(out.contains(r#"VERB@19..27 "{x = $1}""#), "{out}");
    assert!(out.contains("COMMAND@0..27"), "{out}");
}

#[test]
fn user_defined_verbatim_command_argument_is_opaque() {
    // A document that *defines* a catcode-othering command (`\@makeother\$`) makes
    // its call-site argument verbatim via the second parse pass. `\shellcmd` is not a
    // built-in verbatim command, so the VERB capture proves the definition scan, not
    // the built-in DB, did the work: `$`/`_` inside `{a_$b$}` stay literal.
    let out = tree("\\newcommand\\shellcmd[1]{\\@makeother\\$#1}\n\\shellcmd{a_$b$}\n");
    assert!(!out.contains("error @"), "{out}");
    assert!(
        !out.contains("DOLLAR@") && !out.contains("INLINE_MATH@"),
        "{out}"
    );
    assert!(
        out.contains("VERB@") && out.contains(r#""{a_$b$}""#),
        "{out}"
    );
}

#[test]
fn def_defined_verbatim_command_argument_is_opaque() {
    // The same two-pass protection extends to `\def`-defined commands: the parameter
    // text (`#1`) is scanned for arity and the body for the catcode signal, so the
    // call-site argument of `\shellcmd` is captured verbatim and `$`/`_` stay literal.
    let out = tree("\\def\\shellcmd#1{\\@makeother\\$#1}\n\\shellcmd{a_$b$}\n");
    assert!(!out.contains("error @"), "{out}");
    assert!(
        !out.contains("DOLLAR@") && !out.contains("INLINE_MATH@"),
        "{out}"
    );
    assert!(
        out.contains("VERB@") && out.contains(r#""{a_$b$}""#),
        "{out}"
    );
}

#[test]
fn user_defined_verbatim_environment_body_is_opaque() {
    // A document that defines a catcode-othering *environment* (`\@makeother\$` in its
    // begin-code) makes its `\begin…\end` body verbatim via the second parse pass.
    // `shellenv` is not a built-in verbatim environment, so the VERBATIM_BODY capture
    // proves the definition scan did the work: `$`/`_`/`%` inside stay literal.
    let out = tree(concat!(
        "\\newenvironment{shellenv}{\\@makeother\\$}{}\n",
        "\\begin{shellenv}\na_$b$ % literal\n\\end{shellenv}\n",
    ));
    assert!(!out.contains("error @"), "{out}");
    assert!(
        !out.contains("DOLLAR@") && !out.contains("INLINE_MATH@") && !out.contains("COMMENT@"),
        "{out}"
    );
    assert!(out.contains("VERBATIM_BODY@"), "{out}");
}

#[test]
fn undefined_command_argument_is_not_verbatim() {
    // The fast path: with no catcode-othering definition, the same call site stays
    // ordinary — a single parse pass, and `$b$` lexes as inline math. Guards against
    // the two-pass ever firing (and changing tokenization) when it should not.
    let out = tree("\\shellcmd{a_$b$}\n");
    assert!(out.contains("INLINE_MATH@"), "{out}");
    assert!(!out.contains(r#""{a_$b$}""#), "{out}");
}

#[test]
fn redefined_braced_verbatim_command_lexes_as_group() {
    // A document that *redefines* a built-in braced-verbatim command (`\code`, jss) to
    // an ordinary macro shadows the built-in: `\code{x_y}` must lex as an ordinary group
    // with token children, not an opaque `VERB` (follow-up to issue #53). The second
    // parse pass records the non-verbatim redefinition as a suppression.
    let out = tree("\\newcommand{\\code}{\\ensuremath{\\mathsf{code}}}\n\\code{x_y}\n");
    assert!(!out.contains("error @"), "{out}");
    assert!(!out.contains("VERB@"), "{out}");
    // The argument is a real group: its `_` lexes as an ordinary token, not swallowed.
    assert!(out.contains("UNDERSCORE@"), "{out}");
}

#[test]
fn redefined_delimited_verbatim_command_lexes_normally() {
    // The suppression covers `verbatimDelimited` built-ins too: a redefined `\url`
    // captures neither its braced nor its `\verb`-style delimiter form.
    let out = tree("\\newcommand{\\url}[1]{\\texttt{#1}}\n\\url{a_b} and \\url|a_b|\n");
    assert!(!out.contains("error @"), "{out}");
    assert!(!out.contains("VERB@"), "{out}");
    assert!(out.contains("UNDERSCORE@"), "{out}");
}

#[test]
fn standalone_verb_after_command_is_not_captured() {
    // A self-contained `\verb…` token (text begins with `\`) following another
    // command must stay a sibling — it is no one's argument. Only a verbatim
    // *argument* `VERB` (`{…}` / delimiter run, never `\`-prefixed) is attached.
    insta::assert_snapshot!(tree(r"\foo \verb|x|"));
}

#[test]
fn lstlisting_optional_arg_then_opaque_body() {
    insta::assert_snapshot!(tree(
        "\\begin{lstlisting}[language=Python]\nif x: pass  # $not math$\n\\end{lstlisting}"
    ));
}

#[test]
fn minted_required_arg_then_opaque_body() {
    insta::assert_snapshot!(tree(
        "\\begin{minted}{python}\nprint(\"%not a comment\")\n\\end{minted}"
    ));
}

#[test]
fn minted_optional_and_required_args() {
    insta::assert_snapshot!(tree(
        "\\begin{minted}[frame=single]{python}\ncode\n\\end{minted}"
    ));
}

#[test]
fn verbatim_capital_optional_arg() {
    insta::assert_snapshot!(tree(
        "\\begin{Verbatim}[fontsize=\\small]\nraw  text\n\\end{Verbatim}"
    ));
}

/// An option-free `lstlisting` whose body's first line *is* a bracketed list: the
/// signature has one optional arg, but it sits on the next line, so the `[1,2,3]`
/// belongs to the opaque body, not to an `OPTIONAL` argument node.
#[test]
fn lstlisting_body_starting_with_bracket_is_not_an_argument() {
    insta::assert_snapshot!(tree("\\begin{lstlisting}\n[1,2,3]\n\\end{lstlisting}"));
}

#[test]
fn makeatletter_control_word_with_at() {
    insta::assert_snapshot!(tree(r"\makeatletter\foo@bar\makeatother"));
}

#[test]
fn expl_syntax_control_word_with_underscore_and_colon() {
    insta::assert_snapshot!(tree(r"\ExplSyntaxOn\seq_new:N\ExplSyntaxOff\seq_new:N"));
}

#[test]
fn line_break_groups_star_and_optional_length() {
    // `\\`, `\\*`, `\\[2ex]`, and `\\*[2ex]` each parse to one `LINE_BREAK` node
    // with the `*` / `[len]` bound in; a plain `\\` (here at the end) stays bare.
    insta::assert_snapshot!(tree(r"a \\ b \\* c \\[2ex] d \\*[2ex] e \\"));
}

#[test]
fn line_break_does_not_cross_trivia_for_its_optional() {
    // A `\\` followed by whitespace then `[x]` does NOT absorb the bracket — the
    // modifiers bind only when they directly abut, so a `\\` ending a line stays
    // bare and nothing is pulled across the break.
    insta::assert_snapshot!(tree("row \\\\\n[x] next"));
}

// --- error recovery ------------------------------------------------------

#[test]
fn environment_mismatch_recovers() {
    insta::assert_snapshot!(tree(r"\begin{a}\begin{b}\end{a}"));
}

#[test]
fn unmatched_closing_brace() {
    insta::assert_snapshot!(tree("a } b"));
}

#[test]
fn unclosed_environment_at_eof() {
    insta::assert_snapshot!(tree(r"\begin{proof} text"));
}

#[test]
fn stray_end_at_top_level() {
    let parsed = parse(r"\end{itemize}");
    assert_eq!(parsed.errors.len(), 1);
    assert!(parsed.errors[0].message.contains("without matching"));
    assert_eq!(parsed.syntax().to_string(), r"\end{itemize}");
}

#[test]
fn unclosed_dollar_math_in_group_does_not_escape() {
    // `$`-math cannot span the enclosing group's `}`, so the shape gate never
    // opens math here: the `$` stays a plain token inside the argument group
    // and nothing downstream is corrupted (no spurious "unmatched `}`" /
    // "unclosed environment"). `\foo` is an ordinary (non-verbatim) command,
    // so its argument group is really parsed — contrast `\code`, whose
    // argument is captured verbatim.
    let parsed = parse("\\begin{a}\\foo{$ x}\\end{a}");
    assert_eq!(parsed.syntax().to_string(), "\\begin{a}\\foo{$ x}\\end{a}");
    assert_eq!(parsed.errors, [], "a gated dollar is not an error");
}

#[test]
fn nested_mismatch_unwinds_to_two_errors() {
    // `b` is closed by the mismatch, `a` matches: exactly one "unclosed" error.
    let parsed = parse(r"\begin{a}\begin{b}\end{a}");
    let unclosed = parsed
        .errors
        .iter()
        .filter(|e| e.message.contains("unclosed environment"))
        .count();
    assert_eq!(unclosed, 1, "only `b` is unclosed; `a` matches");
}

// --- environment-definition bodies (issue #45) -------------------------------

#[test]
fn newenvironment_split_begin_end_bodies() {
    // The begin-code opens `center` and the end-code closes it: valid LaTeX
    // (the two need not balance within one group), so `\begin`/`\end` parse as
    // plain commands inside the definition bodies and no errors are reported.
    insta::assert_snapshot!(tree(r"\newenvironment{wrap}{\begin{center}}{\end{center}}"));
}

#[test]
fn xparse_environment_split_bodies_parse_clean() {
    let parsed = parse(r"\NewDocumentEnvironment{wrap}{O{x}}{\begin{center}}{\end{center}}");
    assert_eq!(parsed.errors, vec![]);
}

#[test]
fn newenvironment_split_bodies_in_optional_default() {
    let parsed = parse(r"\newenvironment{w}[1][\begin{center}]{a}{b}");
    assert_eq!(parsed.errors, vec![]);
}

#[test]
fn env_def_body_flag_does_not_leak_to_siblings() {
    // The environment *after* the definition still parses as a real
    // ENVIRONMENT: the definition-body treatment ends with the attached
    // arguments.
    let parsed = parse(
        "\\newenvironment{w}{\\begin{center}}{\\end{center}}\n\\begin{itemize}x\\end{itemize}",
    );
    assert_eq!(parsed.errors, vec![]);
    let kinds: Vec<SyntaxKind> = parsed.syntax().descendants().map(|n| n.kind()).collect();
    assert!(
        kinds.contains(&SyntaxKind::ENVIRONMENT),
        "itemize is a real environment"
    );
}

// --- hook and command-definition bodies (issue #55) --------------------------

#[test]
fn hook_bodies_split_begin_end() {
    // `\AtBeginDocument` opens an environment that `\AtEndDocument` closes:
    // the code arguments run at different points in the document, so
    // `\begin`/`\end` need not balance within either group.
    let parsed = parse("\\AtBeginDocument{\\begin{page}}\n\\AtEndDocument{\\end{page}}");
    assert_eq!(parsed.errors, vec![]);
}

#[test]
fn newcommand_body_splits_end_begin() {
    // A page-break macro closes the current environment and reopens it
    // (dalcde/cam-notes headers): plain commands, no errors.
    insta::assert_snapshot!(tree(r"\newcommand{\newpg}{\end{page}\begin{page}}"));
}

// --- environments never escape their brace group (issue #71) -----------------

#[test]
fn environment_does_not_escape_its_enclosing_group() {
    // array.sty's `\newcolumntype`: the `\begin` sits in one sibling group and
    // its `\end` in another, so neither is document structure. Each `}` closes
    // the group it belongs to and nothing cascades into unmatched-brace noise.
    let parsed = parse(r"\newcolumntype{w}[2]{>{\begin{lrbox}\b}c<{\end{lrbox}}}");
    assert_eq!(parsed.errors, vec![]);
}

#[test]
fn begin_in_a_message_argument_is_a_plain_command() {
    // amstex.sty: `\begin{split}` is prose inside an error message, never
    // executed as structure, and its group closes before any `\end`.
    let parsed = parse(r"\def\s{\PackageError{a}{\begin{split} is not allowed}}");
    assert_eq!(parsed.errors, vec![]);
}

#[test]
fn end_inside_a_group_is_not_stray() {
    // ltxdoc's `\StopEventually{\end{document}}`: this `\end`'s `\begin` is
    // outside the group, so it is macro code, not a stray.
    let parsed = parse(r"\StopEventually{\end{document}}");
    assert_eq!(parsed.errors, vec![]);
}

#[test]
fn unbraced_definee_body_still_gets_the_group_gate() {
    // rotex.tex's `\newcommand\BeginExample{…\begin{VerbatimOut}…}` pairs with
    // a separate `\EndExample`. The body group is not attached to
    // `\newcommand` (the definee is a bare control word), so the definition-body
    // flag never reaches it and only the group gate keeps the parse clean.
    let parsed = parse(r"\newcommand\BeginExample{\begin{VerbatimOut}}");
    assert_eq!(parsed.errors, vec![]);
}

#[test]
fn def_body_begin_is_a_plain_command() {
    // The `\def` family is not in `is_definition_body_command`, so before the
    // group gate this swallowed the closing brace.
    let parsed = parse(r"\def\x{\begin{y}}");
    assert_eq!(parsed.errors, vec![]);
}

#[test]
fn an_end_orphaned_by_the_gate_is_demoted_in_step() {
    // amsldoc.tex (issue #71): `\lowercase{…}` smuggles a literal `}` into text,
    // so the `\begin{error}` sits inside a group its `\end` is outside of and the
    // gate demotes it. Left as a stray `\end`, its partner then unwound every
    // enclosing environment on the way to the root — un-closing the whole
    // `document` and stranding `\end{document}` as a second stray.
    let parsed = parse(concat!(
        r"\begin{document}\lowercase{\begin{error}{Missing @ inserted}}",
        "\nx\n",
        r"\end{error}",
        "\n",
        r"\end{document}",
    ));
    assert_eq!(parsed.errors, vec![]);
}

#[test]
fn an_end_the_gate_never_touched_is_still_stray() {
    // The mirror is scoped to names the gate actually demoted: a plain typo has
    // no demoted partner, so it still reports.
    let parsed = parse(r"egin{itemize}\item a\end{itemiz}");
    assert!(!parsed.errors.is_empty());
}

#[test]
fn a_reachable_end_inside_a_group_still_nests() {
    // The gate only fires when the `\end` is unreachable before the enclosing
    // `}`: a properly paired environment inside a group still builds a real
    // ENVIRONMENT node.
    let parsed = parse(r"{\begin{center}hi\end{center}}");
    assert_eq!(parsed.errors, vec![]);
    let kinds: Vec<SyntaxKind> = parsed.syntax().descendants().map(|n| n.kind()).collect();
    assert!(
        kinds.contains(&SyntaxKind::ENVIRONMENT),
        "a reachable `\\end` still nests as an environment"
    );
}

#[test]
fn unclosed_environment_outside_a_group_still_diagnoses() {
    // The gate is scoped to a *group* boundary. A `\begin` that merely runs out
    // of file has no competing brace, so a genuinely forgotten `\end` in prose
    // keeps its diagnostic.
    let parsed = parse(r"\begin{itemize}\item x");
    assert_eq!(parsed.errors.len(), 1);
    assert!(parsed.errors[0].message.contains("unclosed environment"));
}

// --- nested command-abutting brackets in math (issue #55) --------------------

#[test]
fn math_bracket_claimed_by_inner_command_stays_atom() {
    // The lone `]` is claimed by `\gamma[`, so the outer `\P[` must not
    // attach as an optional argument (it would end up unclosed): it stays an
    // ordinary math atom and the parse is clean.
    insta::assert_snapshot!(tree(
        r"\[0 < \P[\gamma[0, \infty) \cap A = \emptyset] < 1\]"
    ));
}

#[test]
fn math_bracket_with_own_closer_still_attaches_past_interval() {
    // A `[` not abutting a command (the interval `[0, 1)`) claims no `]`, so
    // the outer bracket still reads as an argument and attaches.
    let parsed = parse(r"$\E[[0, 1) \cap A]$");
    assert_eq!(parsed.errors, vec![]);
    let kinds: Vec<SyntaxKind> = parsed.syntax().descendants().map(|n| n.kind()).collect();
    assert!(
        kinds.contains(&SyntaxKind::OPTIONAL),
        "the outer bracket attaches to `\\E`"
    );
}

// --- block-vs-inline paragraph wrapping --------------------------------------

/// The kinds of the root's direct child *nodes* (trivia tokens are skipped, as
/// `SyntaxNode::children` yields only nodes). Used to assert whether a run was
/// wrapped in a `PARAGRAPH` or left as a bare block.
fn root_node_kinds(input: &str) -> Vec<SyntaxKind> {
    // Losslessness must hold for every input.
    let parsed = parse(input);
    assert_eq!(
        parsed.syntax().to_string(),
        input,
        "losslessness violated for {input:?}"
    );
    parsed.syntax().children().map(|n| n.kind()).collect()
}

#[test]
fn lone_block_environment_is_not_wrapped() {
    // A `figure` is a block env (signature DB), so it sits bare under ROOT —
    // no redundant PARAGRAPH. Surrounding single newlines ride as direct
    // children, preserving losslessness.
    insta::assert_snapshot!(tree("\\begin{figure}\nx\n\\end{figure}"));
    assert_eq!(
        root_node_kinds("\\begin{figure}\nx\n\\end{figure}"),
        [SyntaxKind::ENVIRONMENT]
    );
}

#[test]
fn block_environment_with_trailing_text_stays_wrapped() {
    // Not a *lone* env: trailing text makes the run ordinary prose, so the
    // PARAGRAPH wrapper is retained.
    assert_eq!(
        root_node_kinds(r"\begin{center}x\end{center} y"),
        [SyntaxKind::PARAGRAPH]
    );
}

#[test]
fn text_before_block_environment_stays_wrapped() {
    assert_eq!(
        root_node_kinds(r"see \begin{center}x\end{center}"),
        [SyntaxKind::PARAGRAPH]
    );
}

#[test]
fn nested_lone_block_env_drops_inner_paragraph() {
    // The figure body's lone `center` is also left unwrapped.
    insta::assert_snapshot!(tree(
        "\\begin{figure}\n\\begin{center}\nx\n\\end{center}\n\\end{figure}"
    ));
}

#[test]
fn lone_unknown_environment_stays_wrapped() {
    // User/unknown environments are not in the built-in DB, so they are never
    // treated as block: the conservative PARAGRAPH wrapper is kept.
    assert_eq!(
        root_node_kinds("\\begin{myenv}\nx\n\\end{myenv}"),
        [SyntaxKind::PARAGRAPH]
    );
}

#[test]
fn dollar_without_reachable_closer_stays_plain() {
    // The `$` shape gate (smoke-test issue #60): a dollar whose closer is not
    // reachable before the enclosing group closes is macro-code data — the
    // tabular preamble `>{$}` injects math per cell — not a math delimiter.
    // It parses as an ordinary token: no math node, no diagnostic.
    insta::assert_snapshot!(tree(r"\begin{tabular}{>{$}c<{$}} a \end{tabular}"));
}

#[test]
fn lone_dollar_in_group_stays_plain() {
    // An expl3 token list holding a literal dollar (l3htoks: `{ $ }`).
    insta::assert_snapshot!(tree(r"\tl_put:Nn \l_tmpa_tl { $ }"));
}

#[test]
fn unclosed_dollar_before_paragraph_break_stays_plain() {
    // A paragraph break bounds the closer scan: the dollar cannot pair with
    // one in a later paragraph, so it stays plain rather than swallowing the
    // rest of its paragraph as math.
    insta::assert_snapshot!(tree("a $ b\n\nc $ d\n\ne"));
}

#[test]
fn dollar_display_without_closer_gates_each_dollar() {
    // `{ $$ }`: no `$$` closer is reachable, so the display opener is not
    // math; each `$` re-enters the gate independently and both stay plain.
    insta::assert_snapshot!(tree(r"{ $$ }"));
}

#[test]
fn dollar_math_still_pairs_across_groups_and_environments() {
    // The gate must not regress legit math: a closer past balanced `{…}`
    // nesting and a balanced `\begin…\end` still opens math.
    insta::assert_snapshot!(tree(
        r"${a}^2$ and $\begin{smallmatrix} a \end{smallmatrix}$"
    ));
}

#[test]
fn def_control_symbol_name_is_plain() {
    // `\def`-family name isolation (smoke-test issue #65): the control symbol
    // after `\def` is the sequence being defined, not syntax — `\[` is no
    // math opener — and the attached body is a macro-code body, so the
    // trivlist that opens in `\def\[`'s body and closes in `\def\]`'s draws
    // no diagnostics.
    insta::assert_snapshot!(tree(
        "\\def\\[{\\begin{trivlist}\\item[]$\\displaystyle}%\n\\def\\]{$\\end{trivlist}}"
    ));
}

#[test]
fn display_math_without_reachable_closer_stays_plain() {
    // The `\[` shape gate (smoke-test issue #65), mirroring the `$` gate:
    // macro code passes `\[` around as a data token
    // (`\expandafter\@tempa\[\@nil`), so an opener with no reachable closer
    // is an ordinary token: no math node, no diagnostic.
    insta::assert_snapshot!(tree("\\expandafter\\@tempa\\[\\@nil"));
}

#[test]
fn delim_math_still_pairs_across_lines() {
    // The gate must not regress legit display math: a closer on a later line
    // (no blank line between) still opens math.
    insta::assert_snapshot!(tree("\\[\n  x + y\n\\]\nand \\( a \\)"));
}

#[test]
fn delim_math_pairs_across_a_blank_line_inside_a_nested_environment() {
    // Smoke-test issue #70: a display equation laid out from `tikzpicture`
    // cells has blank lines inside the pictures. Those belong to the nested
    // environment — `delim_math` only anchors on a paragraph break between
    // top-level atoms — so the gate must not read them as ending the math.
    // Reading them as blockers dropped the math node and left the real `\]`
    // reported as unmatched.
    insta::assert_snapshot!(tree(
        "\\[\n\\begin{tikzpicture}\na\n\nb\n\\end{tikzpicture}\n\\]"
    ));
}

#[test]
fn delim_math_pairs_across_a_blank_line_inside_a_group() {
    // Same boundary via `{…}`: `math_group` consumes a blank line as ordinary
    // body trivia, so the gate must too.
    insta::assert_snapshot!(tree("\\(\n\\text{a\n\nb}\n\\)"));
}

#[test]
fn unclosed_delim_math_before_paragraph_break_stays_plain() {
    // The other side of that boundary: at the math body's own level a blank
    // line is still a `\par`, so `\[` finds no reachable closer and stays a
    // plain token (the orphaned `\]` carries the diagnostic).
    insta::assert_snapshot!(tree("\\[\n  a\n\n  b\n\\]"));
}

#[test]
fn dollar_math_pairs_across_a_blank_line_inside_a_nested_environment() {
    // The `$$` twin of issue #70: `dollar_closes` mirrors the same anchors.
    insta::assert_snapshot!(tree(
        "$$\n\\begin{tikzpicture}\na\n\nb\n\\end{tikzpicture}\n$$"
    ));
}

#[test]
fn braceless_end_is_a_plain_command() {
    // The `\begin`/`\end` shape gate (smoke-test issue #60): macro code uses
    // the bare TeX primitive (`\let\end\@@end`, docstrip's
    // `\errmessage{…}\end`) and the delimiter pattern
    // (`\long\def\@gobble@nv#1\end#2{…}`), so a `\begin`/`\end` with no
    // reachable `{` is a plain command: no environment, no diagnostic.
    insta::assert_snapshot!(tree("\\let\\end\\@@end\n\\def\\g#1\\end#2{x}"));
}

#[test]
fn braceless_end_does_not_terminate_an_environment_body() {
    // A bare `\end` inside an environment body is body content, not the
    // closer: the environment still pairs with its real `\end{name}`.
    insta::assert_snapshot!(tree("\\begin{myenv}\n\\expandafter\\end\n\\end{myenv}"));
}

#[test]
fn end_with_macro_name_group_is_a_plain_command() {
    // A name group holding a parameter or control word is computed macro
    // data (`\edef\reserved@a{\noexpand\end{\reserved@a}}`, xparse's
    // `\begin \end {#3}`), statically unpairable — a plain command with an
    // ordinary argument, no diagnostic.
    insta::assert_snapshot!(tree(r"\def\x{\end{\reserved@a}} \def\y#1{\end{#1}}"));
}

#[test]
fn spaced_name_group_still_reads_as_an_environment() {
    // The name-shape check must not regress `\begin { longtable }`-style
    // spacing (expl3 sources): a word-only name group still pairs.
    insta::assert_snapshot!(tree(r"\begin { myenv } x \end { myenv }"));
}

#[test]
fn char_constant_backtick_keeps_the_next_character_plain() {
    // TeX char-constant notation (smoke-test issue #60): after `\char`/
    // `\catcode`-family primitives, a backtick makes the next character data —
    // `\char`$` must not open math, and in running text `\char`}` is the
    // close-group *character*, not a group closer. The backtick and its
    // character lex as one plain `WORD` token.
    insta::assert_snapshot!(tree("\\item[\\char`$ or z] and \\char`} too"));
}

#[test]
fn char_constant_backtick_never_hides_a_brace_inside_a_group() {
    // Inside a group the brace wins: whichever balanced-text scan opened it — a
    // `\def` body here — counts brace *tokens* long before `\char` could run,
    // so `` \def\v{\char`} `` closes at that `}` (longtable.dtx), and the
    // `` \ifnum`}=0\fi `` / `` \ifnum`{=\z@\fi `` balance idiom keeps its braces
    // structural instead of stranding the enclosing group (issue #71). The
    // escaped form `` `\} `` is a control symbol, never a delimiter, so it
    // stays data at any depth.
    insta::assert_snapshot!(tree(
        "\\def\\v{\\char`}\n\\def\\w{\\noalign{\\ifnum`}=0\\fi x}\n\\def\\z{\\char`\\}}"
    ));
}

#[test]
fn char_constant_escaped_form_lexes_benignly() {
    // The escaped single-character form is captured as one plain `WORD` too
    // (backtick, backslash, and the escaped character): `\catcode`\%=12`.
    insta::assert_snapshot!(tree("\\catcode`\\%=12"));
}

#[test]
fn char_constant_escaped_bracket_is_not_a_math_delimiter() {
    // A numeric-context primitive followed by an escaped-bracket char constant
    // (`\number`\[`, `\number`\]`) is data, not display math: encguide.tex's
    // char-code table pairs `\relax[ … ]` across table rows, and the `\[`/`\]`
    // must not open/close a `\[…\]` display that swallows the row's `]`
    // (smoke-test issue #71).
    insta::assert_snapshot!(tree("\\relax[ & \\number`\\[ \\\\\n] & \\number`\\]"));
}

#[test]
fn expl3_region_begin_end_is_plain_macro_code() {
    // Inside an expl3 region, token lists pass `\begin`/`\end` around as data
    // (l3prefixes.tex builds a longtable across two token-list bodies, issue
    // #60): both parse as plain commands — no pairing, no diagnostics.
    insta::assert_snapshot!(tree(
        "\\ExplSyntaxOn\n\\tl_set:Nn \\l_tmpa_tl { \\begin { longtable } { ll } }\n\\tl_put_right:Nn \\l_tmpa_tl { \\end { longtable } }\n\\ExplSyntaxOff\n"
    ));
}

#[test]
fn environments_pair_again_after_expl_syntax_off() {
    // The region gate ends at `\ExplSyntaxOff`: document markup after it
    // pairs as usual.
    insta::assert_snapshot!(tree(
        "\\ExplSyntaxOn \\foo:n \\ExplSyntaxOff\n\\begin{itemize}\n\\item x\n\\end{itemize}\n"
    ));
}