import { TOK_EOF, TOK_NUM, TOK_IDENT, TOK_OP, TOK_CMD, TOK_SUB, TOK_LBRACE, TOK_RBRACE, } from "./const/TOK.js"; const skip = (str, idx) => (str.charCodeAt(idx) <= 32 ? skip(str, idx + 1) : idx), NUM_RE = /\d+(?:\.\d+)?|\.\d+/y, CMD_RE = /\\(?:[a-zA-Z]+|.)/y; export default (str) => { const res = [], len = str.length; let idx = 0; while (idx < len) { idx = skip(str, idx); if (idx >= len) break; CMD_RE.lastIndex = idx; let m = CMD_RE.exec(str); if (m) { let cmd = m[0]; idx += cmd.length; // \textrm 等 6 种 text 命令行为一致,统一归一为 \text,省去 CMD_MAP 同义键 const is_text = /^\\text(?:rm|bf|it|sf|tt)?$/.test(cmd); if (is_text) cmd = "\\text"; res.push(TOK_CMD, cmd); if (is_text) { const pos = skip(str, idx); if (str.charCodeAt(pos) === 123) { let braces = 1, t_idx = pos; while (t_idx < len && braces) { const cc = str.charCodeAt(++t_idx); if (cc === 123) ++braces; else if (cc === 125) --braces; } if (!braces) { // LBRACE/RBRACE 的值不会被读取,填空串省体积 res.push(TOK_LBRACE, "", TOK_IDENT, str.slice(pos + 1, t_idx), TOK_RBRACE, ""); idx = t_idx + 1; } } } continue; } NUM_RE.lastIndex = idx; m = NUM_RE.exec(str); if (m) { const num = m[0]; res.push(TOK_NUM, num); idx += num.length; continue; } const char = str[idx], pos = "_^{}".indexOf(char); res.push( ((char.charCodeAt(0) | 32) - 97) >>> 0 < 26 ? TOK_IDENT : pos >= 0 ? pos + TOK_SUB : TOK_OP, char, ); ++idx; } res.push(TOK_EOF, ""); return res; };