Skip to main content

jsslint_core/tex/
debug.rs

1//! Debug dump: one line per node, pre-order, in the exact walk order
2//! `prose::walk` visits them — the Phase 1 acceptance artifact per the
3//! plan ("a debug dump of (node-kind, span, prose?) for every fixture
4//! must match a parallel dump from the Python walker"). Format:
5//!
6//! ```text
7//! <depth>\t<pos>\t<end>\t<kind>\t<prose>
8//! ```
9//!
10//! `kind` encodes the node's identifying detail (macro/environment
11//! name, group delimiter, math kind) so a mismatch is diagnosable from
12//! the dump alone, without needing the original source alongside it.
13//! Compared against `tools/dump_tex_nodes.py`'s identical format.
14
15use super::node::{GroupDelims, MathKind, Node};
16use super::prose;
17
18fn kind_label(node: &Node) -> String {
19    match node {
20        Node::Chars(_) => "Chars".to_string(),
21        Node::Comment(_) => "Comment".to_string(),
22        Node::Macro(m) => format!("Macro:{}", m.macroname),
23        Node::Group(g) => match g.delims {
24            GroupDelims::Brace => "Group:{".to_string(),
25            GroupDelims::Bracket => "Group:[".to_string(),
26        },
27        Node::Environment(e) => format!("Environment:{}", e.environmentname),
28        Node::Math(m) => match m.kind {
29            MathKind::Inline => "Math:inline".to_string(),
30            MathKind::Display => "Math:display".to_string(),
31        },
32        Node::Specials(s) => format!("Specials:{}", s.chars),
33    }
34}
35
36/// Dumps `nodes` in the same pre-order `prose::walk` visits them.
37pub fn dump(nodes: &[Node]) -> String {
38    let mut out = String::new();
39    prose::walk(nodes, &mut |node, ancestors| {
40        let depth = ancestors.len();
41        let span = node.span();
42        let prose = prose::is_in_prose_context(ancestors);
43        out.push_str(&format!(
44            "{depth}\t{pos}\t{end}\t{kind}\t{prose}\n",
45            pos = span.pos,
46            end = span.end(),
47            kind = kind_label(node),
48            prose = prose,
49        ));
50    });
51    out
52}