badness_parser/semantic/expl3/
mode.rs1use crate::ast::{AstNode, Environment};
4use crate::parser::lexer::{ExplToggle, dtx_has_expl_signal, expl_toggle};
5use crate::syntax::{SyntaxElement, SyntaxKind, SyntaxNode};
6use rowan::{TextSize, WalkEvent};
7
8#[derive(Debug, Default, Clone, PartialEq, Eq)]
9pub struct ModeIndex {
10 commands: Vec<TextSize>,
11}
12
13impl ModeIndex {
14 pub fn build(root: &SyntaxNode, dtx: bool) -> Self {
15 let implicit = dtx && dtx_has_expl_signal(&root.text().to_string());
16 let mut index = Self::default();
17 let mut active = false;
18 let mut doc_line = false;
19 let mut saved = Vec::new();
20 for event in root.preorder_with_tokens() {
21 match event {
22 WalkEvent::Enter(SyntaxElement::Node(node)) if dtx && is_macrocode(&node) => {
23 saved.push(active);
24 active |= implicit;
25 }
26 WalkEvent::Leave(SyntaxElement::Node(node)) if dtx && is_macrocode(&node) => {
27 active = saved.pop().unwrap_or(false);
28 }
29 WalkEvent::Enter(SyntaxElement::Token(token)) => match token.kind() {
30 SyntaxKind::DOC_MARGIN => doc_line = true,
31 SyntaxKind::NEWLINE => doc_line = false,
32 SyntaxKind::CONTROL_WORD | SyntaxKind::CONTROL_SYMBOL => {
33 if active && !doc_line && (!dtx || !saved.is_empty()) {
34 index.commands.push(token.text_range().start());
35 }
36 if token.kind() == SyntaxKind::CONTROL_WORD
37 && let Some(toggle) = expl_toggle(token.text())
38 {
39 active = toggle == ExplToggle::On;
40 }
41 }
42 _ => {}
43 },
44 _ => {}
45 }
46 }
47 index
48 }
49
50 pub fn at_command(&self, start: TextSize) -> bool {
51 self.commands.binary_search(&start).is_ok()
52 }
53}
54
55fn is_macrocode(node: &SyntaxNode) -> bool {
56 Environment::cast(node.clone())
57 .and_then(|env| env.name())
58 .is_some_and(|name| matches!(name.as_str(), "macrocode" | "macrocode*"))
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64 use crate::parser::{LexConfig, parse_with_flavor};
65
66 fn at(text: &str, needle: &str, dtx: bool) -> bool {
67 let parsed = parse_with_flavor(
68 text,
69 LexConfig {
70 dtx,
71 ..Default::default()
72 },
73 );
74 assert_eq!(parsed.syntax().text().to_string(), text);
75 ModeIndex::build(&parsed.syntax(), dtx)
76 .at_command(TextSize::from(text.find(needle).unwrap() as u32))
77 }
78
79 #[test]
80 fn explicit_modes_and_protected_text() {
81 assert!(at("\\ExplSyntaxOn\n\\tl", "\\tl", false));
82 assert!(at(
83 "\\ProvidesExplPackage{foo}{2026/01/01}{1}{Demo}\n\\tl",
84 "\\tl",
85 false
86 ));
87 assert!(!at("\\ExplSyntaxOn\n\\ExplSyntaxOff\n\\tl", "\\tl", false));
88 assert!(!at("% \\ExplSyntaxOn\n\\tl", "\\tl", false));
89 assert!(!at("\\verb|\\ExplSyntaxOn|\n\\tl", "\\tl", false));
90 }
91
92 #[test]
93 fn dtx_implicit_mode_and_chunk_restoration() {
94 let text = "%<@@=demo>\n% \\begin{macrocode}\n\\tl\n\\ExplSyntaxOff\n\\seq\n% \\end{macrocode}\n% Documentation \\int\n% \\begin{macrocode}\n\\cs\n% \\end{macrocode}\n";
95 assert!(at(text, "\\tl", true));
96 assert!(!at(text, "\\seq", true));
97 assert!(!at(text, "\\int", true));
98 assert!(at(text, "\\cs", true));
99 let text = "% \\begin{macrocode}\n\\tl\n% \\end{macrocode}\n% \\ProvidesExplPackage{demo}{}{}{}\n";
100 assert!(at(text, "\\tl", true));
101 }
102}