badness-parser 0.11.0

Lossless CST parser, semantic model, and command-signature database for LaTeX and BibTeX
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
//! Conservative expl3 call reads shared by semantic lint rules and completion.
//!
//! The CST proves argument attachment, not execution. This index enters only
//! recognized executable bodies, and an unresolved head stops recognition in
//! its surrounding stream. Stored token lists and expansion wrappers stay
//! opaque. Parameter escaping is a source-mapped view of known definition
//! bodies; it never substitutes arguments, expands macros, or changes the tree.

use std::collections::{HashMap, VecDeque};
use std::sync::Arc;

use rowan::{TextRange, TextSize};

use crate::ast::{AstNode, Command, Environment, Group, command_name};
use crate::parser::lexer::{ExplToggle, expl_toggle};
use crate::semantic::expl3::{Expl3Slot, expl3_slots};
use crate::syntax::{SyntaxElement, SyntaxKind, SyntaxNode, is_trivia};

#[derive(Default)]
pub struct Expl3Index {
    calls: HashMap<TextSize, Call>,
}

pub struct Call {
    pub name: String,
    pub arguments: Vec<Argument>,
    /// Actual message parameters after the enclosing definitions' hash escapes.
    pub message_parameters: Vec<(TextRange, u8)>,
}

pub struct Argument {
    pub specifier: u8,
    elements: Vec<SyntaxElement>,
}

pub struct Literal {
    pub text: String,
    pub range: TextRange,
}

struct Stream {
    elements: VecDeque<SyntaxElement>,
    definitions: Arc<[u8]>,
    active: bool,
}

impl Expl3Index {
    /// Recognized calls in source order.
    pub fn calls(&self) -> impl Iterator<Item = &Call> {
        let mut calls: Vec<_> = self.calls.iter().collect();
        calls.sort_unstable_by_key(|(offset, _)| **offset);
        calls.into_iter().map(|(_, call)| call)
    }

    pub fn get(&self, node: &SyntaxNode) -> Option<&Call> {
        self.calls.get(&node.text_range().start())
    }

    pub fn build(root: &SyntaxNode) -> Self {
        let mut index = Self::default();
        let documentation = root
            .descendants_with_tokens()
            .any(|el| el.kind() == SyntaxKind::DOC_MARGIN);
        // Docstrip extracts only macrocode bodies. Documentation can itself
        // contain live-looking expl3 examples, including syntax toggles.
        let mut pending = if documentation {
            root.descendants()
                .filter_map(Environment::cast)
                .filter(|env| {
                    env.name()
                        .is_some_and(|name| matches!(name.as_str(), "macrocode" | "macrocode*"))
                })
                .map(|env| environment_stream(env.syntax(), Arc::from([]), true))
                .collect()
        } else {
            vec![Stream {
                elements: root.children_with_tokens().collect(),
                definitions: Arc::from([]),
                active: false,
            }]
        };
        while let Some(mut stream) = pending.pop() {
            let mut known_boundary = true;
            while let Some(element) = stream.elements.pop_front() {
                let Some(node) = element.as_node() else {
                    // A parameter or expansion token at statement position may
                    // consume subsequent commands. Trivia cannot restore proof.
                    if stream.active && !is_stream_trivia(element.kind()) {
                        known_boundary = false;
                    }
                    continue;
                };
                match node.kind() {
                    SyntaxKind::PARAGRAPH | SyntaxKind::TEXT => {
                        for child in node
                            .children_with_tokens()
                            .collect::<Vec<_>>()
                            .into_iter()
                            .rev()
                        {
                            stream.elements.push_front(child);
                        }
                    }
                    SyntaxKind::DOC_COMMENT => {}
                    SyntaxKind::ENVIRONMENT => {
                        let Some(env) = Environment::cast(node.clone()) else {
                            continue;
                        };
                        let Some(name) = env.name() else { continue };
                        if known_boundary
                            && matches!(name.as_str(), "document" | "macrocode" | "macrocode*")
                        {
                            pending.push(environment_stream(
                                node,
                                stream.definitions.clone(),
                                stream.active || name.starts_with("macrocode"),
                            ));
                        }
                    }
                    SyntaxKind::GROUP if known_boundary => {
                        if let Some(group) = Group::cast(node.clone()).filter(closed_group) {
                            pending.push(body_stream(
                                &group,
                                stream.definitions.clone(),
                                stream.active,
                            ));
                        }
                    }
                    SyntaxKind::COMMAND => {
                        let Some(name) = command_name(node) else {
                            known_boundary &= !stream.active;
                            continue;
                        };
                        if let Some(toggle) = expl_toggle(&format!("\\{name}")) {
                            if (known_boundary || !stream.active) && !toggle_may_be_an_operand(node)
                            {
                                stream.active = toggle == ExplToggle::On;
                                known_boundary = true;
                            } else {
                                // A referenced toggle can still have changed the
                                // lexer mode; that does not prove execution.
                                stream.active = true;
                                known_boundary = false;
                            }
                            continue;
                        }
                        if !known_boundary {
                            continue;
                        }
                        let Some(mut call) = read_call(node) else {
                            known_boundary &= !stream.active;
                            continue;
                        };
                        stream.active = true;
                        if call.name.starts_with("exp_") || call.name.starts_with("use:") {
                            // Expansion drivers can expose a head that consumes
                            // further siblings beyond the driver's own argspec.
                            known_boundary = false;
                            continue;
                        }
                        if let Some((body, count)) = definition_body(&call, &stream.definitions) {
                            let mut definitions = stream.definitions.to_vec();
                            definitions.push(count);
                            pending.push(body_stream(&body, definitions.into(), true));
                        }
                        for argument in &call.arguments {
                            if matches!(argument.specifier, b'T' | b'F')
                                && let Some(group) = argument.group()
                            {
                                pending.push(body_stream(&group, stream.definitions.clone(), true));
                            }
                        }
                        if is_message_definition(&call.name) {
                            for argument in &call.arguments[2..] {
                                if let Some(atoms) = argument.atoms(&stream.definitions) {
                                    call.message_parameters.extend(message_parameters(&atoms));
                                }
                            }
                        }
                        index.calls.insert(node.text_range().start(), call);
                    }
                    _ => {
                        if stream.active {
                            known_boundary = false;
                        }
                    }
                }
            }
        }
        index
    }
}

fn environment_stream(node: &SyntaxNode, definitions: Arc<[u8]>, active: bool) -> Stream {
    Stream {
        elements: node
            .children_with_tokens()
            .filter(|el| !matches!(el.kind(), SyntaxKind::BEGIN | SyntaxKind::END))
            .collect(),
        definitions,
        active,
    }
}

fn toggle_may_be_an_operand(node: &SyntaxNode) -> bool {
    if crate::semantic::define::in_reference_position(node) {
        return true;
    }
    let mut previous = node.prev_sibling_or_token();
    while let Some(element) = previous {
        if !is_trivia(element.kind()) && element.kind() != SyntaxKind::DOC_COMMENT {
            return element.as_node().is_some_and(|previous| {
                previous.kind() == SyntaxKind::COMMAND
                    && read_call(previous).is_none()
                    && !command_name(previous)
                        .is_some_and(|name| expl_toggle(&format!("\\{name}")).is_some())
                    && !previous.children().any(|child| {
                        matches!(child.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL)
                    })
            });
        }
        previous = match element {
            SyntaxElement::Node(node) => node.prev_sibling_or_token(),
            SyntaxElement::Token(token) => token.prev_sibling_or_token(),
        };
    }
    false
}

fn is_stream_trivia(kind: SyntaxKind) -> bool {
    is_trivia(kind)
        || matches!(
            kind,
            SyntaxKind::GUARD | SyntaxKind::DOC_MARGIN | SyntaxKind::TILDE
        )
}

fn closed_group(group: &Group) -> bool {
    group
        .syntax()
        .first_child_or_token()
        .is_some_and(|el| el.kind() == SyntaxKind::L_BRACE)
        && group
            .syntax()
            .last_child_or_token()
            .is_some_and(|el| el.kind() == SyntaxKind::R_BRACE)
}

fn body_stream(group: &Group, definitions: Arc<[u8]>, active: bool) -> Stream {
    let mut elements: VecDeque<_> = group.syntax().children_with_tokens().collect();
    elements.pop_front();
    elements.pop_back();
    Stream {
        elements,
        definitions,
        active,
    }
}

/// Read only arguments attached to this head. The parser's greedy and partial
/// fallbacks do not prove a complete call, so sibling recovery is deliberately
/// absent here.
fn read_call(node: &SyntaxNode) -> Option<Call> {
    let command = Command::cast(node.clone())?;
    let name = command.name()?.to_string();
    let (stem, spec) = name.rsplit_once(':')?;
    if stem.is_empty() || stem.starts_with(':') {
        return None;
    }
    let slots = expl3_slots(&name)?;
    let mut elements: VecDeque<_> = node
        .children_with_tokens()
        .skip_while(|el| el.kind() != SyntaxKind::CONTROL_WORD)
        .skip(1)
        .filter(|el| !is_trivia(el.kind()) && el.kind() != SyntaxKind::TILDE)
        .collect();
    let mut arguments = Vec::new();
    for (specifier, slot) in spec.bytes().zip(slots) {
        let mut argument = Argument {
            specifier,
            elements: Vec::new(),
        };
        match slot {
            Expl3Slot::Group | Expl3Slot::Branch => {
                let el = elements.pop_front()?;
                let group = Group::cast(el.as_node()?.clone())?;
                if !closed_group(&group) {
                    return None;
                }
                argument.elements.push(el);
            }
            Expl3Slot::SingleToken => {
                let el = elements.pop_front()?;
                match &el {
                    SyntaxElement::Node(node) if node.kind() == SyntaxKind::GROUP => {
                        if !closed_group(&Group::cast(node.clone())?) {
                            return None;
                        }
                    }
                    SyntaxElement::Node(node) if node.kind() == SyntaxKind::COMMAND => {
                        if node
                            .children_with_tokens()
                            .filter(|el| !is_trivia(el.kind()))
                            .count()
                            != 1
                        {
                            return None;
                        }
                    }
                    SyntaxElement::Token(token)
                        if matches!(
                            token.kind(),
                            SyntaxKind::CONTROL_WORD | SyntaxKind::CONTROL_SYMBOL
                        ) => {}
                    SyntaxElement::Token(token)
                        if token.kind() == SyntaxKind::WORD
                            && token.text().chars().count() == 1 => {}
                    _ => return None,
                }
                argument.elements.push(el);
            }
            Expl3Slot::ParameterText => {
                while elements.front()?.kind() != SyntaxKind::GROUP {
                    argument.elements.push(elements.pop_front()?);
                }
            }
        }
        arguments.push(argument);
    }
    if !elements.is_empty() {
        return None;
    }
    Some(Call {
        name,
        arguments,
        message_parameters: Vec::new(),
    })
}

impl Argument {
    fn group(&self) -> Option<Group> {
        if self.elements.len() != 1 {
            return None;
        }
        Group::cast(self.elements[0].as_node()?.clone())
    }

    /// Literal `N` and `c` names have the same semantic value, but require
    /// different structural proofs. A command somewhere in a group is not
    /// enough to establish a single control-sequence argument.
    pub fn name(&self) -> Option<String> {
        match self.specifier {
            b'N' => {
                let mut elements = self.elements.clone();
                if let Some(group) = self.group() {
                    elements = group
                        .syntax()
                        .children_with_tokens()
                        .filter(|el| {
                            !is_trivia(el.kind())
                                && !matches!(el.kind(), SyntaxKind::L_BRACE | SyntaxKind::R_BRACE)
                        })
                        .collect();
                }
                if elements.len() != 1 {
                    return None;
                }
                let command = Command::cast(elements[0].as_node()?.clone())?;
                if command.syntax().children_with_tokens().count() != 1 {
                    return None;
                }
                Some(command.name()?.to_string())
            }
            b'c' => Some(self.literal()?.text),
            _ => None,
        }
    }

    pub fn literal(&self) -> Option<Literal> {
        let group = self.group()?;
        let mut text = String::new();
        let mut range = None;
        for el in group.syntax().children_with_tokens() {
            if is_trivia(el.kind())
                || matches!(el.kind(), SyntaxKind::L_BRACE | SyntaxKind::R_BRACE)
            {
                continue;
            }
            let token = el.as_token()?;
            if token.kind() != SyntaxKind::WORD {
                return None;
            }
            text.push_str(token.text());
            range = Some(range.map_or(token.text_range(), |r: TextRange| {
                r.cover(token.text_range())
            }));
        }
        Some(Literal {
            text,
            range: range.unwrap_or_else(|| {
                TextRange::empty(group.syntax().text_range().start() + TextSize::from(1))
            }),
        })
    }

    pub fn list(&self) -> Option<Vec<Literal>> {
        if self.specifier != b'n' {
            return None;
        }
        let atoms = self.atoms(&[])?;
        let mut entries = Vec::new();
        let mut text = String::new();
        let mut range: Option<TextRange> = None;
        for atom in atoms {
            let AtomKind::Character(c) = atom.kind else {
                return None;
            };
            if c == ',' {
                if let Some(range) = range.take() {
                    entries.push(Literal {
                        text: std::mem::take(&mut text),
                        range,
                    });
                }
            } else if c.is_ascii_alphabetic() {
                text.push(c);
                range = Some(range.map_or(atom.range, |r| r.cover(atom.range)));
            } else {
                return None;
            }
        }
        if let Some(range) = range {
            entries.push(Literal { text, range });
        }
        Some(entries)
    }

    fn atoms(&self, definitions: &[u8]) -> Option<Vec<Atom>> {
        let elements = if let Some(group) = self.group() {
            body_stream(&group, Arc::from([]), true)
                .elements
                .into_iter()
                .collect()
        } else {
            self.elements.clone()
        };
        let mut atoms = Vec::new();
        for element in elements {
            match element {
                SyntaxElement::Token(token) => append_atoms(&token, &mut atoms),
                SyntaxElement::Node(node) => {
                    for token in node
                        .descendants_with_tokens()
                        .filter_map(|el| el.into_token())
                    {
                        append_atoms(&token, &mut atoms);
                    }
                }
            }
        }
        for &count in definitions {
            atoms = unescape_parameters(atoms, count)?;
        }
        Some(atoms)
    }
}

/// The direct creator families are a small semantic allowlist. In particular,
/// `*_eq` and expansion variants do not establish executable replacement text.
pub(super) fn definition_kind(stem: &str) -> Option<bool> {
    let (family, tail) = stem.split_once('_')?;
    let tail = ["new", "set", "gset"]
        .into_iter()
        .find_map(|op| tail.strip_prefix(op))?;
    match family {
        "cs" if matches!(tail, "" | "_protected" | "_nopar" | "_protected_nopar") => Some(false),
        "prg" if matches!(tail, "_conditional" | "_protected_conditional") => Some(true),
        _ => None,
    }
}

fn definition_body(call: &Call, definitions: &[u8]) -> Option<(Group, u8)> {
    let (stem, spec) = call.name.rsplit_once(':')?;
    let conditional = definition_kind(stem)?;
    if !(if conditional {
        matches!(spec, "Nnn" | "Npnn" | "cnn" | "cpnn")
    } else {
        matches!(spec, "Nn" | "Npn" | "cn" | "cpn")
    }) {
        return None;
    }
    let name = call.arguments.first()?.name()?;
    let count = if let Some(argument) = call.arguments.iter().find(|a| a.specifier == b'p') {
        let atoms = argument.atoms(definitions)?;
        let mut count = 0;
        let (pairs, remainder) = atoms.as_chunks::<2>();
        for pair in pairs {
            count += 1;
            if count > 9
                || pair[0].kind != AtomKind::Hash
                || pair[1].kind != AtomKind::Character(char::from(b'0' + count))
            {
                return None;
            }
        }
        if !remainder.is_empty() {
            return None;
        }
        count
    } else {
        let slots = expl3_slots(&name)?;
        if slots.len() > 9 || slots.contains(&Expl3Slot::ParameterText) {
            return None;
        }
        slots.len() as u8
    };
    Some((call.arguments.last()?.group()?, count))
}

pub fn is_message_definition(name: &str) -> bool {
    name.rsplit_once(':').is_some_and(|(stem, spec)| {
        matches!(stem, "msg_new" | "msg_set" | "msg_gset") && matches!(spec, "nnn" | "nnnn")
    })
}

pub fn is_protected_conditional(name: &str) -> bool {
    name.rsplit_once(':').is_some_and(|(stem, spec)| {
        matches!(
            stem,
            "prg_new_protected_conditional"
                | "prg_set_protected_conditional"
                | "prg_gset_protected_conditional"
        ) && matches!(spec, "Nnn" | "Npnn" | "cnn" | "cpnn")
    })
}

pub fn is_variant_generation(name: &str) -> bool {
    matches!(
        name,
        "cs_generate_variant:Nn"
            | "cs_generate_variant:cn"
            | "prg_generate_conditional_variant:Nnn"
            | "prg_generate_conditional_variant:cnn"
    )
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum AtomKind {
    Hash,
    Character(char),
    Other,
    Unknown,
}

struct Atom {
    kind: AtomKind,
    range: TextRange,
}

fn append_atoms(token: &crate::syntax::SyntaxToken, atoms: &mut Vec<Atom>) {
    if is_trivia(token.kind()) {
        return;
    }
    if token.kind() == SyntaxKind::WORD {
        for (offset, c) in token.text().char_indices() {
            let start = token.text_range().start() + TextSize::from(offset as u32);
            atoms.push(Atom {
                kind: AtomKind::Character(c),
                range: TextRange::at(start, TextSize::of(c)),
            });
        }
    } else {
        atoms.push(Atom {
            kind: if token.kind() == SyntaxKind::HASH {
                AtomKind::Hash
            } else {
                AtomKind::Other
            },
            range: token.text_range(),
        });
    }
}

fn unescape_parameters(atoms: Vec<Atom>, count: u8) -> Option<Vec<Atom>> {
    let mut input = atoms.into_iter();
    let mut output = Vec::new();
    while let Some(atom) = input.next() {
        if atom.kind != AtomKind::Hash {
            output.push(atom);
            continue;
        }
        let next = input.next()?;
        let kind = match next.kind {
            AtomKind::Hash => AtomKind::Hash,
            AtomKind::Character(c) if c >= '1' && c <= char::from(b'0' + count) => {
                AtomKind::Unknown
            }
            AtomKind::Unknown => AtomKind::Unknown,
            _ => return None,
        };
        output.push(Atom {
            kind,
            range: atom.range.cover(next.range),
        });
    }
    Some(output)
}

fn message_parameters(atoms: &[Atom]) -> Vec<(TextRange, u8)> {
    let mut found = Vec::new();
    let mut atoms = atoms.iter();
    while let Some(atom) = atoms.next() {
        if atom.kind != AtomKind::Hash {
            continue;
        }
        if let Some(next) = atoms.next()
            && let AtomKind::Character(c @ '5'..='9') = next.kind
        {
            found.push((atom.range.cover(next.range), c as u8 - b'0'));
        }
    }
    found
}

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

    fn root(body: &str) -> SyntaxNode {
        let source = format!("\\ExplSyntaxOn\n{body}\n\\ExplSyntaxOff\n");
        let parsed = parse(&source);
        let root = SyntaxNode::new_root(parsed.green);
        assert_eq!(root.text().to_string(), source);
        root
    }

    #[test]
    fn expl3_call_reader_preserves_expansion_letters() {
        let root = root(r"\demo:NVncvoxef \a \b {n}{c}{v}{o}{x}{e}{f}");
        let index = Expl3Index::build(&root);
        let call = index
            .calls
            .values()
            .find(|c| c.name == "demo:NVncvoxef")
            .unwrap();
        assert_eq!(
            call.arguments
                .iter()
                .map(|a| a.specifier)
                .collect::<Vec<_>>(),
            b"NVncvoxef"
        );
    }

    #[test]
    fn expl3_call_reader_declines_partial_and_greedy_shapes() {
        for source in [
            r"\demo:Nn \a",
            "{\\demo:Nn \\a\n\n{n}}",
            r"\demo:w {n}",
            r"\demo:D {n}",
            r"\demo:Tn {a}{b}",
            r"\demo:Nn \a {unclosed",
        ] {
            let root = root(source);
            let index = Expl3Index::build(&root);
            assert!(index.calls.is_empty(), "{source}");
        }
    }

    #[test]
    fn expl3_definees_and_token_lists_are_not_calls() {
        let root = root(
            r"\cs_new_eq:NN \msg_new:nnn \other:nnn
            \tl_set:Nn \l_data_tl {\msg_new:nnn{a}{b}{#5}}
            \msg_new:nnn{a}{b}{#6}",
        );
        let index = Expl3Index::build(&root);
        let calls: Vec<_> = index
            .calls
            .values()
            .filter(|c| c.name == "msg_new:nnn")
            .collect();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].message_parameters[0].1, 6);
    }
}