mathhook_core/parser/lexer/
token_maps.rs

1//! Ultra-fast token classification maps
2//!
3//! This module contains optimized HashMap-based token lookups for maximum
4//! performance in implicit multiplication processing.
5
6use std::collections::HashMap;
7use std::sync::LazyLock;
8
9/// Token types for classification
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum TokenType {
12    Number = 0,
13    Identifier = 1,
14    Constant = 2,
15    GreekSymbol = 3,
16    Function = 4,
17    LeftParen = 5,
18    RightParen = 6,
19    Operator = 7,
20    LaTeXCommand = 8,
21    Other = 9,
22}
23
24/// LaTeX token classification map for O(1) lookups
25pub static LATEX_TOKEN_MAP: LazyLock<HashMap<&'static str, TokenType>> = LazyLock::new(|| {
26    let mut map = HashMap::new();
27
28    // LaTeX Constants
29    map.insert("\\pi", TokenType::Constant);
30    map.insert("\\phi", TokenType::Constant);
31    map.insert("\\varphi", TokenType::Constant);
32    map.insert("\\infty", TokenType::Constant);
33    map.insert("\\EulerGamma", TokenType::Constant);
34    map.insert("\\gamma", TokenType::Constant);
35
36    // LaTeX Greek Symbols
37    map.insert("\\alpha", TokenType::GreekSymbol);
38    map.insert("\\beta", TokenType::GreekSymbol);
39    map.insert("\\delta", TokenType::GreekSymbol);
40    map.insert("\\epsilon", TokenType::GreekSymbol);
41    map.insert("\\zeta", TokenType::GreekSymbol);
42    map.insert("\\eta", TokenType::GreekSymbol);
43    map.insert("\\theta", TokenType::GreekSymbol);
44    map.insert("\\iota", TokenType::GreekSymbol);
45    map.insert("\\kappa", TokenType::GreekSymbol);
46    map.insert("\\lambda", TokenType::GreekSymbol);
47    map.insert("\\mu", TokenType::GreekSymbol);
48    map.insert("\\nu", TokenType::GreekSymbol);
49    map.insert("\\xi", TokenType::GreekSymbol);
50    map.insert("\\omicron", TokenType::GreekSymbol);
51    map.insert("\\rho", TokenType::GreekSymbol);
52    map.insert("\\sigma", TokenType::GreekSymbol);
53    map.insert("\\tau", TokenType::GreekSymbol);
54    map.insert("\\upsilon", TokenType::GreekSymbol);
55    map.insert("\\chi", TokenType::GreekSymbol);
56    map.insert("\\psi", TokenType::GreekSymbol);
57    map.insert("\\omega", TokenType::GreekSymbol);
58
59    // LaTeX Functions
60    map.insert("\\sin", TokenType::Function);
61    map.insert("\\cos", TokenType::Function);
62    map.insert("\\tan", TokenType::Function);
63    map.insert("\\ln", TokenType::Function);
64    map.insert("\\log", TokenType::Function);
65    map.insert("\\sqrt", TokenType::Function);
66    map.insert("\\arcsin", TokenType::Function);
67    map.insert("\\arccos", TokenType::Function);
68    map.insert("\\arctan", TokenType::Function);
69    map.insert("\\sinh", TokenType::Function);
70    map.insert("\\cosh", TokenType::Function);
71    map.insert("\\tanh", TokenType::Function);
72    map.insert("\\sec", TokenType::Function);
73    map.insert("\\csc", TokenType::Function);
74    map.insert("\\cot", TokenType::Function);
75    map.insert("\\det", TokenType::Function);
76    map.insert("\\max", TokenType::Function);
77    map.insert("\\min", TokenType::Function);
78    map.insert("\\sup", TokenType::Function);
79    map.insert("\\inf", TokenType::Function);
80    map.insert("\\gcd", TokenType::Function);
81    map.insert("\\lcm", TokenType::Function);
82
83    // LaTeX Operators (should NOT trigger implicit multiplication)
84    map.insert("\\cdot", TokenType::Operator);
85    map.insert("\\times", TokenType::Operator);
86    map.insert("\\div", TokenType::Operator);
87    map.insert("\\pm", TokenType::Operator);
88    map.insert("\\mp", TokenType::Operator);
89    map.insert("\\leq", TokenType::Operator);
90    map.insert("\\geq", TokenType::Operator);
91    map.insert("\\neq", TokenType::Operator);
92    map.insert("\\equiv", TokenType::Operator);
93    map.insert("\\approx", TokenType::Operator);
94
95    map
96});