use crate::{ EngineCommandName, assignment_scope::AssignmentScope, command::{EngineCommandMeaning, NativeCommandTable}, environment::EnvironmentStack, macros::MacroTable, math_code::{DelimiterCodeTable, MathCodeTable, UnicodeMathCodeTable}, mouth::CatCodeTable, }; /// Mutable TeX language state shared across tokenization and execution phases. /// /// Runtime adapters may hold this state, but the values and group semantics are /// owned by the Rust engine core. #[derive(Default, serde::Serialize, serde::Deserialize)] pub struct EngineSemanticState { pub catcodes: CatCodeTable, pub math_codes: MathCodeTable, pub unicode_math_codes: UnicodeMathCodeTable, pub delimiter_codes: DelimiterCodeTable, pub assignment_scope: AssignmentScope, pub pending_global_assignment: bool, pub environments: EnvironmentStack, pub macros: MacroTable, #[serde(default)] pub native_commands: NativeCommandTable, group_depth: usize, } impl EngineSemanticState { /// Resolve a control sequence using engine-owned macro and primitive meanings. pub fn command_meaning(&self, name: &EngineCommandName) -> Option { if self.macros.get(name).is_some() { Some(EngineCommandMeaning::Macro) } else { self.native_commands .get(name) .map(EngineCommandMeaning::Native) } } pub fn begin_group(&mut self) { self.group_depth += 1; self.catcodes.begin_group(); self.math_codes.begin_group(); self.unicode_math_codes.begin_group(); self.delimiter_codes.begin_group(); self.assignment_scope.begin_group(); self.macros.begin_group(); self.native_commands.begin_group(); } pub fn end_group(&mut self) { self.group_depth = self.group_depth.saturating_sub(1); self.catcodes.end_group(); self.math_codes.end_group(); self.unicode_math_codes.end_group(); self.delimiter_codes.end_group(); self.assignment_scope.end_group(); self.macros.end_group(); self.native_commands.end_group(); } pub fn is_safe_document_checkpoint(&self) -> bool { if self.group_depth != 0 { return false; } match self.environments.as_slice() { [] => true, [environment] => environment == "document", _ => false, } } }