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
//! The Phase 1 recursive-descent grammar for LaTeX surface syntax.
//!
//! The parser walks the full token stream (trivia included) and emits a flat
//! list of [`Event`]s — `Start(kind)` / `Tok(idx)` / `Finish` — that
//! [`super::tree_builder`] replays into a green tree. Because every token is
//! emitted exactly once, in order, via [`Parser::bump`], losslessness holds by
//! construction: `pos` only ever advances through `bump`, and nothing else
//! touches it.
//!
//! It is **error-tolerant**: a malformed construct never aborts the parse. Each
//! recovery records a [`SyntaxError`] on the side channel and either closes the
//! current node gracefully or skips a single token, always making progress.
//! Recovery anchors are the LaTeX-natural ones: `\end`, `}`, `]`, `$`, blank
//! lines, and end of input.
use crate::parser::core::SyntaxError;
use crate::parser::events::Event;
use crate::parser::lexer::{Token, VerbCtx, is_block_environment};
use crate::syntax::SyntaxKind;
const BEGIN_CMD: &str = "\\begin";
const END_CMD: &str = "\\end";
const LEFT_CMD: &str = "\\left";
const RIGHT_CMD: &str = "\\right";
/// A content region that groups its children into `PARAGRAPH` nodes separated
/// by blank lines. Differs only in how the region terminates.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Block {
/// The whole document; ends at EOF.
Document,
/// An environment body; ends at the next `\end` (any name — the caller
/// checks the name and decides whether to consume it).
Environment,
}
/// Parse a token stream into parser events and a list of syntax errors.
pub(crate) fn parse(tokens: &[Token], ctx: &VerbCtx) -> (Vec<Event>, Vec<SyntaxError>) {
let mut p = Parser::new(tokens, ctx);
p.document();
(p.events, p.errors)
}
struct Parser<'t> {
tokens: &'t [Token],
/// User-defined verbatim constructs, consulted to route a verbatim environment to
/// its raw-body branch (its body is already one `VERBATIM_BODY` token from the
/// lexer; the grammar must not try to parse it structurally).
ctx: &'t VerbCtx,
/// `starts[i]` is the byte offset of token `i`; `starts[len]` is the total
/// length. Used to give syntax errors byte ranges.
starts: Vec<usize>,
pos: usize,
events: Vec<Event>,
errors: Vec<SyntaxError>,
}
impl<'t> Parser<'t> {
fn new(tokens: &'t [Token], ctx: &'t VerbCtx) -> Self {
let mut starts = Vec::with_capacity(tokens.len() + 1);
let mut off = 0;
for t in tokens {
starts.push(off);
off += t.text.len();
}
starts.push(off);
Self {
tokens,
ctx,
starts,
pos: 0,
events: Vec::new(),
errors: Vec::new(),
}
}
// --- cursor primitives -------------------------------------------------
fn kind(&self) -> Option<SyntaxKind> {
self.tokens.get(self.pos).map(|t| t.kind)
}
fn nth_kind(&self, n: usize) -> Option<SyntaxKind> {
self.tokens.get(self.pos + n).map(|t| t.kind)
}
fn text(&self) -> &str {
self.tokens
.get(self.pos)
.map(|t| t.text.as_str())
.unwrap_or("")
}
fn at_end(&self) -> bool {
self.pos >= self.tokens.len()
}
fn at_command(&self, name: &str) -> bool {
self.kind() == Some(SyntaxKind::CONTROL_WORD) && self.text() == name
}
fn is_trivia(k: SyntaxKind) -> bool {
matches!(
k,
SyntaxKind::WHITESPACE | SyntaxKind::NEWLINE | SyntaxKind::COMMENT
)
}
// --- event emission ----------------------------------------------------
fn bump(&mut self) {
debug_assert!(!self.at_end(), "bump past end of input");
self.events.push(Event::Tok(self.pos));
self.pos += 1;
}
fn open(&mut self, kind: SyntaxKind) {
self.events.push(Event::Start(kind));
}
fn close(&mut self) {
self.events.push(Event::Finish);
}
fn error(&mut self, message: impl Into<String>) {
let (start, end) = if self.at_end() {
let end = *self.starts.last().expect("starts is non-empty");
(end, end)
} else {
(self.starts[self.pos], self.starts[self.pos + 1])
};
self.errors.push(SyntaxError {
message: message.into(),
start,
end,
});
}
fn skip_trivia(&mut self) {
while self.kind().is_some_and(Self::is_trivia) {
self.bump();
}
}
/// Peek the kind of the next non-trivia token and whether the intervening
/// trivia contains a paragraph break (a blank line, i.e. ≥2 newlines).
/// Does not consume.
fn peek_meaningful(&self) -> (Option<SyntaxKind>, bool) {
let mut i = self.pos;
let mut newlines = 0;
let mut had_break = false;
while let Some(t) = self.tokens.get(i) {
match t.kind {
SyntaxKind::NEWLINE => {
newlines += 1;
had_break |= newlines >= 2;
}
SyntaxKind::WHITESPACE => {}
// A comment occupies its own line: it is content, not blank
// space, so it resets the run that detects a blank line. It
// does not undo a blank line already seen before it.
SyntaxKind::COMMENT => newlines = 0,
k => return (Some(k), had_break),
}
i += 1;
}
(None, had_break)
}
/// Text of the next non-trivia token at/after `self.pos`, if any. Does not
/// consume. Used to distinguish a verbatim-argument `VERB` from a standalone
/// `\verb…` token (see `attach_arguments`).
fn peek_meaningful_text(&self) -> Option<&str> {
let mut i = self.pos;
while let Some(t) = self.tokens.get(i) {
if !Self::is_trivia(t.kind) {
return Some(t.text.as_str());
}
i += 1;
}
None
}
/// True if a paragraph break (blank line) begins at the current position.
fn at_paragraph_break(&self) -> bool {
let mut i = self.pos;
let mut newlines = 0;
while let Some(t) = self.tokens.get(i) {
match t.kind {
SyntaxKind::NEWLINE => {
newlines += 1;
if newlines >= 2 {
return true;
}
}
SyntaxKind::WHITESPACE => {}
// A comment-only line is content, not a blank line: it breaks
// the run of newlines that would otherwise read as a `\par`.
SyntaxKind::COMMENT => newlines = 0,
_ => return false,
}
i += 1;
}
false
}
/// True if the comment at `pos` starts its own line: scanning back over
/// inline whitespace only, the preceding token is a `NEWLINE` or the start of
/// input. A same-line trailing comment (`\foo % x`) returns `false` and never
/// binds forward (see [`Self::binding_run`]).
fn comment_starts_line(&self, pos: usize) -> bool {
let mut i = pos;
while i > 0 {
i -= 1;
match self.tokens[i].kind {
SyntaxKind::WHITESPACE => continue,
SyntaxKind::NEWLINE => return true,
_ => return false,
}
}
true
}
/// If the trivia run at `from` ends in a `%` comment run that binds *leading*
/// into a following documentable construct, return
/// `(comment_start, construct_pos, construct_kind)`:
/// - `comment_start` — index of the first own-line comment of the binding run
/// (the maximal blank-line-free suffix; trivia before it floats),
/// - `construct_pos` — index of the construct's control word,
/// - `construct_kind` — `ENVIRONMENT` for `\begin`, otherwise `COMMAND`.
///
/// Returns `None` when the run has no own-line comment, a blank line separates
/// the comment from the construct, or the next non-trivia token is not a
/// documentable construct. Mirrors rust-analyzer's `n_attached_trivias`
/// (AGENTS.md #9): comments bind forward to the item they annotate, a blank
/// line breaks the bind, and a same-line trailing comment never binds.
fn binding_run(&self, from: usize) -> Option<(usize, usize, SyntaxKind)> {
let mut i = from;
let mut newlines = 0;
let mut comment_start: Option<usize> = None;
while let Some(t) = self.tokens.get(i) {
match t.kind {
SyntaxKind::NEWLINE => {
newlines += 1;
// A blank line breaks the bind: only a comment *after* it can
// still bind, so drop any comment seen before it.
if newlines >= 2 {
comment_start = None;
}
}
SyntaxKind::WHITESPACE => {}
SyntaxKind::COMMENT => {
newlines = 0;
if comment_start.is_none() && self.comment_starts_line(i) {
comment_start = Some(i);
}
}
SyntaxKind::CONTROL_WORD => {
let start = comment_start?;
let kind = match self.tokens[i].text.as_str() {
BEGIN_CMD => SyntaxKind::ENVIRONMENT,
END_CMD => return None,
_ => SyntaxKind::COMMAND,
};
return Some((start, i, kind));
}
_ => return None,
}
i += 1;
}
None
}
// --- grammar -----------------------------------------------------------
fn document(&mut self) {
self.parse_block(Block::Document);
}
/// Parse a content region, grouping runs of content into `PARAGRAPH` nodes
/// delimited by blank lines (the TeX `\par` boundary). Blank-line trivia
/// (and any trailing trivia) sits between paragraphs as direct children of
/// the enclosing node, not inside a paragraph.
fn parse_block(&mut self, block: Block) {
loop {
if self.at_block_end(block) {
break;
}
// Separator trivia (blank lines / trailing whitespace) is emitted
// directly, never wrapped in a paragraph — except a trailing own-line
// comment run that binds into the construct after it: stop before that
// comment so the construct (next iteration) absorbs it as leading.
if self.kind().is_some_and(Self::is_trivia) && self.trivia_run_is_separator(block) {
let stop = self
.binding_run(self.pos)
.map_or(self.tokens.len(), |(comment_start, ..)| comment_start);
while self.pos < stop && self.kind().is_some_and(Self::is_trivia) {
self.bump();
}
continue;
}
// Otherwise we're at paragraph content (guaranteed ≥1 token, so no
// empty paragraph and no infinite loop). Parse the run first, then
// splice in the `PARAGRAPH` wrapper afterwards (the `precede` idiom,
// cf. `math_scripted`) — unless the run's only non-trivia element is a
// lone block environment, which we leave bare. Block-ness is read from
// the built-in signature DB (`is_block_environment`).
let checkpoint = self.events.len();
let mut nontrivia_count = 0usize;
let mut lone_block_env = false;
loop {
if self.at_block_end(block) {
break;
}
if self.kind().is_some_and(Self::is_trivia) && self.trivia_run_is_separator(block) {
break;
}
// Leading comment-bind: an own-line `%` run immediately before a
// documentable construct attaches *leading* into it. Float any
// trivia before the comment run, then wrap the comments + construct
// in the construct's node (the `precede` idiom: the construct
// self-opens, then its `Start` is pulled back over the comments).
if let Some((comment_start, construct_pos, _)) = self.binding_run(self.pos) {
while self.pos < comment_start {
self.bump();
}
let checkpoint = self.events.len();
while self.pos < construct_pos {
self.bump();
}
let starts_block_env = self.tokens[construct_pos].text == BEGIN_CMD
&& peek_begin_name(self.tokens, construct_pos)
.as_deref()
.is_some_and(is_block_environment);
let construct_start = self.events.len();
self.element();
if let Event::Start(kind) = self.events[construct_start] {
self.events.remove(construct_start);
self.events.insert(checkpoint, Event::Start(kind));
}
nontrivia_count += 1;
lone_block_env = nontrivia_count == 1 && starts_block_env;
continue;
}
let is_nontrivia = !self.kind().is_some_and(Self::is_trivia);
// Peek block-env status *before* consuming (the name is only
// available while still on the `\begin`).
let starts_block_env = self.at_command(BEGIN_CMD)
&& peek_begin_name(self.tokens, self.pos)
.as_deref()
.is_some_and(is_block_environment);
self.element();
if is_nontrivia {
nontrivia_count += 1;
lone_block_env = nontrivia_count == 1 && starts_block_env;
}
}
if !lone_block_env {
self.events
.insert(checkpoint, Event::Start(SyntaxKind::PARAGRAPH));
self.close(); // matching Finish for PARAGRAPH
}
}
}
fn at_block_end(&self, block: Block) -> bool {
self.at_end() || (block == Block::Environment && self.at_command(END_CMD))
}
/// True if the contiguous trivia run at the current position should separate
/// paragraphs: it contains a blank line, or only trivia remains before the
/// block terminator (the `\end`, or EOF).
fn trivia_run_is_separator(&self, block: Block) -> bool {
let mut i = self.pos;
let mut newlines = 0;
let mut had_break = false;
while let Some(t) = self.tokens.get(i) {
match t.kind {
SyntaxKind::NEWLINE => {
newlines += 1;
had_break |= newlines >= 2;
}
SyntaxKind::WHITESPACE => {}
// A comment line is content: it resets the blank-line run but
// keeps any blank line already seen before it.
SyntaxKind::COMMENT => newlines = 0,
SyntaxKind::CONTROL_WORD if block == Block::Environment && t.text == END_CMD => {
return true;
}
_ => return had_break,
}
i += 1;
}
true
}
/// One element in text mode. Always consumes at least one token.
fn element(&mut self) {
let Some(k) = self.kind() else { return };
match k {
SyntaxKind::WHITESPACE | SyntaxKind::NEWLINE | SyntaxKind::COMMENT => self.bump(),
SyntaxKind::CONTROL_WORD => {
if self.at_command(BEGIN_CMD) {
self.environment();
} else if self.at_command(END_CMD) {
self.stray_end();
} else {
self.command();
}
}
SyntaxKind::CONTROL_SYMBOL => {
let sym = self.text().to_owned();
match sym.as_str() {
"\\[" => self.delim_math(SyntaxKind::DISPLAY_MATH, "\\[", "\\]"),
"\\(" => self.delim_math(SyntaxKind::INLINE_MATH, "\\(", "\\)"),
"\\]" | "\\)" => {
self.error(format!("unmatched `{sym}`"));
self.bump();
}
// `\\` line break, with its tightly-bound `*` / `[len]`.
"\\\\" => self.line_break(),
// Any other bare control symbol (`\,`, `\%`, `\;`, …). Surface
// model: emit as a token; these take no arguments.
_ => self.bump(),
}
}
SyntaxKind::L_BRACE => self.group(),
SyntaxKind::R_BRACE => {
self.error("unmatched `}`");
self.bump();
}
SyntaxKind::DOLLAR => self.dollar_math(),
// WORD, brackets, & # ^ _ ~, ERROR: ordinary tokens in text mode.
_ => self.bump(),
}
}
/// `\foo` followed by its greedily-attached argument groups.
///
/// Arity is unknown without the semantic layer, so we attach every trailing
/// `{…}` / `[…]` group, allowing intervening trivia but stopping at a
/// paragraph break (see `AGENTS.md`, Core decision #8).
fn command(&mut self) {
self.open(SyntaxKind::COMMAND);
self.bump(); // the control word
self.attach_arguments();
self.close();
}
/// The `\\` line break and its tightly-bound modifiers: an optional `*`
/// (no-page-break variant) and an optional `[length]` (`\\`, `\\*`,
/// `\\[2ex]`, `\\*[2ex]`). These bind to the `\\` only when they *directly*
/// abut it — no intervening trivia is crossed — so a lone `\\` at end of line
/// stays bare and the modifiers are never pulled across a break. Grouping
/// them into one `LINE_BREAK` node (rather than leaving loose tokens) is what
/// lets the formatter treat `\\[2ex]` as one unit instead of stranding the
/// `[2ex]` on the next line.
///
/// Unlike `command`, this attaches *no* `{…}` arguments (`\\` takes none) and
/// does not skip trivia. The `*` is recognized only as its own `WORD` token
/// (the lexer glues `*` into following letters, so `\\*foo` keeps the star on
/// the word — a vanishingly rare form we deliberately leave alone).
fn line_break(&mut self) {
self.open(SyntaxKind::LINE_BREAK);
self.bump(); // \\
if self.kind() == Some(SyntaxKind::WORD) && self.text() == "*" {
self.bump(); // *
}
if self.kind() == Some(SyntaxKind::L_BRACKET) {
self.optional(); // [length]
}
self.close();
}
/// Greedily attach trailing `{…}` / `[…]` argument groups to the currently
/// open node, allowing intervening trivia but stopping at a paragraph break.
/// Shared by `\foo` commands and `\begin{env}` (see `AGENTS.md`, Core
/// decision #8). Arity is unknown without the semantic layer.
fn attach_arguments(&mut self) {
loop {
let (next, paragraph_break) = self.peek_meaningful();
if paragraph_break {
break;
}
match next {
Some(SyntaxKind::L_BRACE) => {
self.skip_trivia();
self.group();
}
Some(SyntaxKind::L_BRACKET) => {
self.skip_trivia();
self.optional();
}
// A verbatim-argument command's body (`\url{…}`, `\lstinline|…|`,
// the final arg of `\mintinline{lang}{code}`) is lexed as a single
// `VERB` token immediately following the command, so attach it as a
// child like any other argument (decision #8) instead of leaving it
// a sibling. A *standalone* `\verb…`/`\verb*…` token (its text starts
// with `\`) is self-contained and belongs to no command — never
// capture it. Only `lex_verbatim_command` emits a non-`\` `VERB`, and
// always directly after its own command, so the open node here owns it.
Some(SyntaxKind::VERB)
if !self
.peek_meaningful_text()
.is_some_and(|t| t.starts_with('\\')) =>
{
self.skip_trivia();
self.bump(); // the VERB argument
}
_ => break,
}
}
}
/// A brace group `{ … }`.
fn group(&mut self) {
debug_assert_eq!(self.kind(), Some(SyntaxKind::L_BRACE));
self.open(SyntaxKind::GROUP);
self.bump(); // {
loop {
match self.kind() {
None => {
self.error("unclosed `{`");
break;
}
Some(SyntaxKind::R_BRACE) => {
self.bump();
break;
}
_ => self.element(),
}
}
self.close();
}
/// An optional-argument group `[ … ]`.
///
/// `[` and `]` are not real grouping in TeX, so this is heuristic: it ends
/// at the first `]`, and bails defensively (rather than swallowing the
/// document) on a `}`, a `\begin`/`\end`, a paragraph break, or EOF.
fn optional(&mut self) {
debug_assert_eq!(self.kind(), Some(SyntaxKind::L_BRACKET));
self.open(SyntaxKind::OPTIONAL);
self.bump(); // [
loop {
match self.kind() {
None | Some(SyntaxKind::R_BRACE) => {
self.error("unclosed `[`");
break;
}
Some(SyntaxKind::R_BRACKET) => {
self.bump();
break;
}
Some(SyntaxKind::CONTROL_WORD)
if self.at_command(BEGIN_CMD) || self.at_command(END_CMD) =>
{
self.error("unclosed `[`");
break;
}
_ => {
if self.at_paragraph_break() {
self.error("unclosed `[`");
break;
}
self.element();
}
}
}
self.close();
}
/// Inline `$ … $` or display `$$ … $$` math. The body's atoms are wrapped in
/// a `MATH` node (the delimiters stay direct children of the math node); the
/// atoms themselves are parsed in math mode (see [`Self::math_element`]).
fn dollar_math(&mut self) {
let display = self.nth_kind(1) == Some(SyntaxKind::DOLLAR);
let (kind, label) = if display {
(SyntaxKind::DISPLAY_MATH, "$$")
} else {
(SyntaxKind::INLINE_MATH, "$")
};
self.open(kind);
self.bump(); // $
if display {
self.bump(); // second $
}
self.open(SyntaxKind::MATH);
loop {
match self.kind() {
None => {
self.error(format!("unclosed `{label}`"));
break;
}
// `}` and `\end` are recovery anchors: `$`-math cannot span a
// group or environment boundary, so a `}` here closes the
// enclosing group (a math subgroup would have entered via `{`)
// and a `\end` belongs to an enclosing environment. Leave the
// token for the caller and report the unclosed math.
Some(SyntaxKind::R_BRACE) => {
self.error(format!("unclosed `{label}`"));
break;
}
Some(SyntaxKind::CONTROL_WORD) if self.at_command(END_CMD) => {
self.error(format!("unclosed `{label}`"));
break;
}
Some(SyntaxKind::DOLLAR) => {
if display && self.nth_kind(1) != Some(SyntaxKind::DOLLAR) {
// A lone `$` inside `$$`: malformed; emit and continue.
self.bump();
continue;
}
// The closing delimiter belongs to the math node, not its
// body: break and bump it after closing `MATH`.
break;
}
_ => {
if self.at_paragraph_break() {
self.error(format!("unclosed `{label}`"));
break;
}
self.math_element();
}
}
}
self.close(); // MATH
if self.kind() == Some(SyntaxKind::DOLLAR) {
self.bump(); // closing $
if display {
self.bump(); // second closing $
}
}
self.close(); // INLINE_MATH / DISPLAY_MATH
}
/// Delimited math: `\[ … \]` (display) or `\( … \)` (inline). As with
/// [`Self::dollar_math`], the body's atoms are wrapped in a `MATH` node and
/// parsed in math mode.
fn delim_math(&mut self, kind: SyntaxKind, opener: &str, closer: &str) {
self.open(kind);
self.bump(); // \[ or \(
self.open(SyntaxKind::MATH);
loop {
match self.kind() {
None => {
self.error(format!("unclosed `{opener}`"));
break;
}
Some(SyntaxKind::CONTROL_SYMBOL) if self.text() == closer => {
// The closer belongs to the math node, not its body.
break;
}
// A `}` closes an enclosing group: it cannot belong to this
// math (a subgroup would have entered via `{`). Leave it for
// the caller and report the unclosed math.
Some(SyntaxKind::R_BRACE) => {
self.error(format!("unclosed `{opener}`"));
break;
}
Some(SyntaxKind::CONTROL_WORD) if self.at_command(END_CMD) => {
self.error(format!("unclosed `{opener}`"));
break;
}
_ => {
if self.at_paragraph_break() {
self.error(format!("unclosed `{opener}`"));
break;
}
self.math_element();
}
}
}
self.close(); // MATH
if self.kind() == Some(SyntaxKind::CONTROL_SYMBOL) && self.text() == closer {
self.bump(); // \] or \)
}
self.close(); // INLINE_MATH / DISPLAY_MATH
}
/// One element inside a math body. Trivia is emitted inline (for
/// losslessness); everything else is an atom, possibly carrying `^`/`_`
/// scripts (see [`Self::math_scripted`]). Callers guard the math closers and
/// recovery anchors before invoking this, so the cursor is at body content.
fn math_element(&mut self) {
match self.kind() {
Some(SyntaxKind::WHITESPACE | SyntaxKind::NEWLINE | SyntaxKind::COMMENT) => self.bump(),
_ => self.math_scripted(),
}
}
/// A base atom with any tightly-bound `^`/`_` scripts — the one sanctioned
/// Pratt site (`AGENTS.md`, decision #3). Sub/superscripts are postfix with a
/// single-atom right operand, so this is a base atom followed by a postfix
/// loop, not full precedence climbing.
///
/// We only wrap the base in a `SCRIPTED` node when a script actually
/// attaches, so an unscripted atom stays a bare token/node (matching the
/// `LINE_BREAK`-only-when-modifiers idiom). Because the base atom's extent is
/// not known until parsed (a command greedily attaches its args), we parse it
/// first and, if a script follows, retroactively splice a `SCRIPTED` start
/// event in front of it — the event-stream analog of rust-analyzer's
/// `precede`, done locally without touching the event layer.
fn math_scripted(&mut self) {
let checkpoint = self.events.len();
self.math_atom();
if !self.at_script() {
return; // bare atom, no wrapper
}
self.events
.insert(checkpoint, Event::Start(SyntaxKind::SCRIPTED));
while self.at_script() {
self.skip_trivia(); // trivia between base/scripts rides inside SCRIPTED
let sub = self.kind() == Some(SyntaxKind::UNDERSCORE);
self.open(if sub {
SyntaxKind::SUBSCRIPT
} else {
SyntaxKind::SUPERSCRIPT
});
self.bump(); // `_` or `^`
self.math_script_arg();
self.close();
}
self.close(); // SCRIPTED
}
/// True if a `^`/`_` script operator directly follows, skipping only
/// `WHITESPACE`/`NEWLINE` (not a comment, which must end its line — so a
/// script never binds across a comment) and not a blank line (a paragraph
/// break ends the math).
fn at_script(&self) -> bool {
let mut i = self.pos;
let mut newlines = 0;
while let Some(t) = self.tokens.get(i) {
match t.kind {
SyntaxKind::NEWLINE => {
newlines += 1;
if newlines >= 2 {
return false;
}
i += 1;
}
SyntaxKind::WHITESPACE => i += 1,
SyntaxKind::CARET | SyntaxKind::UNDERSCORE => return true,
_ => return false,
}
}
false
}
/// A single base atom: a `{…}` group (parsed in math mode), a command with
/// its greedily-attached arguments, an environment, a `\\` line break, or one
/// ordinary token. Always consumes ≥1 token when the cursor is at content.
fn math_atom(&mut self) {
match self.kind() {
Some(SyntaxKind::L_BRACE) => self.math_group(),
Some(SyntaxKind::CONTROL_WORD) => {
if self.at_command(BEGIN_CMD) {
self.environment();
} else if self.at_command(END_CMD) {
self.stray_end();
} else if self.at_command(LEFT_CMD) {
self.left_right();
} else if self.at_command(RIGHT_CMD) {
self.stray_right();
} else {
self.command();
}
}
// `\\` line break (with its tightly-bound `*`/`[len]`) vs. a bare
// control symbol (`\,`, `\;`, `\!`, spacing) — emit the latter as a
// single token.
Some(SyntaxKind::CONTROL_SYMBOL) if self.text() == "\\\\" => self.line_break(),
// Any other single token (WORD, digit, `&`, `~`, `#`, brackets, a
// bare control symbol, or a `^`/`_` with no base): one token, so the
// loop always makes progress.
Some(_) => self.bump(),
None => {}
}
}
/// One script argument: a single atom (a `{…}` group, a command with its
/// args, or one token). A missing argument (the next meaningful token is a
/// closer, `\end`, a paragraph break, or EOF) is reported, not consumed —
/// the closer must stay for the enclosing math loop.
fn math_script_arg(&mut self) {
if self.at_paragraph_break() {
self.error("missing argument after `^`/`_`");
return;
}
self.skip_trivia();
let missing = match self.kind() {
None | Some(SyntaxKind::R_BRACE | SyntaxKind::DOLLAR) => true,
Some(SyntaxKind::CONTROL_SYMBOL) => matches!(self.text(), "\\]" | "\\)"),
Some(SyntaxKind::CONTROL_WORD) => self.at_command(END_CMD),
_ => false,
};
if missing {
self.error("missing argument after `^`/`_`");
return;
}
self.math_atom();
}
/// A brace group `{ … }` whose body is parsed in math mode (so `x^{a_b}`
/// nests). Recovery mirrors [`Self::group`].
fn math_group(&mut self) {
debug_assert_eq!(self.kind(), Some(SyntaxKind::L_BRACE));
self.open(SyntaxKind::GROUP);
self.bump(); // {
loop {
match self.kind() {
None => {
self.error("unclosed `{`");
break;
}
Some(SyntaxKind::R_BRACE) => {
self.bump();
break;
}
_ => self.math_element(),
}
}
self.close();
}
/// A `\left<delim> … \right<delim>` matched delimiter pair (`AGENTS.md`,
/// decision #3: the one precedence-climbing site — here just balanced
/// matching by *count*, which is exactly how TeX pairs them, so a mismatched
/// `\left( … \right]` still nests correctly). The `\left`/`\right` control
/// words and their delimiter tokens are direct children (mirroring how `$` /
/// `\[` delimiters stay direct children of the math node); the enclosed atoms
/// are wrapped in a `MATH` body. Nested pairs recurse via [`Self::math_atom`].
///
/// An unclosed `\left` recovers at the enclosing math/group/environment
/// closer (the same anchors the surrounding math loop uses), leaving that
/// token for the caller.
fn left_right(&mut self) {
debug_assert!(self.at_command(LEFT_CMD));
self.open(SyntaxKind::LEFT_RIGHT);
self.bump(); // \left
self.math_delim(LEFT_CMD);
self.open(SyntaxKind::MATH);
loop {
match self.kind() {
None => {
self.error("unclosed `\\left`");
break;
}
Some(SyntaxKind::CONTROL_WORD) if self.at_command(RIGHT_CMD) => break,
// Enclosing-scope closers: `\left … \right` cannot span a group,
// math, or environment boundary, so hand the token back.
Some(SyntaxKind::R_BRACE | SyntaxKind::DOLLAR) => {
self.error("unclosed `\\left`");
break;
}
Some(SyntaxKind::CONTROL_SYMBOL) if matches!(self.text(), "\\]" | "\\)") => {
self.error("unclosed `\\left`");
break;
}
Some(SyntaxKind::CONTROL_WORD) if self.at_command(END_CMD) => {
self.error("unclosed `\\left`");
break;
}
_ => {
if self.at_paragraph_break() {
self.error("unclosed `\\left`");
break;
}
self.math_element();
}
}
}
self.close(); // MATH
if self.at_command(RIGHT_CMD) {
self.bump(); // \right
self.math_delim(RIGHT_CMD);
}
self.close(); // LEFT_RIGHT
}
/// Consume the single delimiter token following `\left`/`\right`: skip inline
/// trivia (it rides as a direct child of the pair for losslessness; the
/// formatter drops it), then take one token. The lexer has already isolated a
/// word-character delimiter (`(`, `|`, `.`, …) into its own token, so a single
/// `bump` suffices. A missing delimiter — the next meaningful token is a
/// closer, another `\left`/`\right`, `\end`, a paragraph break, or EOF — is
/// reported, not consumed.
fn math_delim(&mut self, after: &str) {
self.skip_trivia();
let missing = match self.kind() {
None | Some(SyntaxKind::R_BRACE | SyntaxKind::DOLLAR) => true,
Some(SyntaxKind::CONTROL_SYMBOL) => matches!(self.text(), "\\]" | "\\)"),
Some(SyntaxKind::CONTROL_WORD) => {
self.at_command(END_CMD) || self.at_command(LEFT_CMD) || self.at_command(RIGHT_CMD)
}
_ => false,
};
if missing {
self.error(format!("missing delimiter after `{after}`"));
return;
}
self.bump();
}
/// A `\right` with no open `\left` (the math loop only reaches one here when
/// it is unmatched). Report it and consume it with its delimiter so the parse
/// stays lossless and makes progress.
fn stray_right(&mut self) {
debug_assert!(self.at_command(RIGHT_CMD));
self.error("`\\right` without matching `\\left`");
self.bump(); // \right
self.math_delim(RIGHT_CMD);
}
/// `\begin{name} … \end{name}`, with environment-mismatch recovery.
fn environment(&mut self) {
self.open(SyntaxKind::ENVIRONMENT);
self.open(SyntaxKind::BEGIN);
self.bump(); // \begin
let name = self.name_group();
self.attach_arguments(); // `\begin{tabular}{ll}`, `[options]`, etc.
self.close(); // BEGIN
if name
.as_deref()
.is_some_and(|n| self.ctx.is_verbatim_environment(n))
{
self.verbatim_body(name.as_deref().expect("verbatim name"));
} else {
self.parse_block(Block::Environment);
}
self.finish_environment(&name);
}
/// Consume the matching `\end`, or recover. `parse_block` / `verbatim_body`
/// leave the cursor at a `\end` or at EOF.
fn finish_environment(&mut self, name: &Option<String>) {
match self.kind() {
None => {
self.error(format!(
"unclosed environment `{}`",
name.as_deref().unwrap_or("")
));
}
// The cursor is at a `\end` (the only non-EOF stop condition).
Some(_) => {
let end_name = peek_end_name(self.tokens, self.pos);
if name.is_none() || *name == end_name {
// Matching \end: consume it as our END.
self.open(SyntaxKind::END);
self.bump(); // \end
self.name_group();
self.close();
} else {
// Mismatched \end: it belongs to an enclosing environment.
// Close this one with a diagnostic and leave the \end for
// the caller (this unwinds the stack until some level
// matches, or it becomes a stray \end at the root).
self.error(format!(
"unclosed environment `{}` (found `\\end{{{}}}`)",
name.as_deref().unwrap_or(""),
end_name.as_deref().unwrap_or("")
));
}
}
}
self.close(); // ENVIRONMENT
}
/// The raw body of a verbatim-like environment: consume tokens unstructured
/// until the matching `\end{name}`. The lexer has already collapsed the body
/// into a single `VERBATIM_BODY` token; this loop also serves as a fallback.
fn verbatim_body(&mut self, name: &str) {
loop {
match self.kind() {
None => break,
Some(SyntaxKind::CONTROL_WORD)
if self.at_command(END_CMD)
&& peek_end_name(self.tokens, self.pos).as_deref() == Some(name) =>
{
break;
}
_ => self.bump(),
}
}
}
/// A `\end` with no matching open environment at this level.
fn stray_end(&mut self) {
self.error("`\\end` without matching `\\begin`");
self.open(SyntaxKind::END);
self.bump(); // \end
self.name_group();
self.close();
}
/// The `{name}` group following `\begin` / `\end`. Returns the trimmed name.
fn name_group(&mut self) -> Option<String> {
self.skip_trivia();
if self.kind() != Some(SyntaxKind::L_BRACE) {
self.error("expected `{` for environment name");
return None;
}
self.open(SyntaxKind::NAME_GROUP);
self.bump(); // {
let mut name = String::new();
loop {
match self.kind() {
None => {
self.error("unclosed environment name");
break;
}
Some(SyntaxKind::R_BRACE) => {
self.bump();
break;
}
_ => {
name.push_str(self.text());
self.bump();
}
}
}
self.close();
Some(name.trim().to_owned())
}
}
/// Read the environment name from a `\begin{…}` at `begin_pos` without consuming.
/// Identical in shape to [`peek_end_name`] (skip the control word and trivia, then
/// read the `{name}` group); named separately for call-site clarity.
fn peek_begin_name(tokens: &[Token], begin_pos: usize) -> Option<String> {
peek_end_name(tokens, begin_pos)
}
/// Read the environment name from a `\end{…}` at `end_pos` without consuming.
fn peek_end_name(tokens: &[Token], end_pos: usize) -> Option<String> {
let mut i = end_pos + 1; // past the \end control word
while tokens.get(i).is_some_and(|t| Parser::is_trivia(t.kind)) {
i += 1;
}
if tokens.get(i).map(|t| t.kind) != Some(SyntaxKind::L_BRACE) {
return None;
}
i += 1;
let mut name = String::new();
while let Some(t) = tokens.get(i) {
if t.kind == SyntaxKind::R_BRACE {
break;
}
name.push_str(&t.text);
i += 1;
}
Some(name.trim().to_owned())
}