char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Aleksander Grygierui: Constants refactor (#26908)e21152dc9

char/aliceai-t5-moe
5.0 KiB125 linesraw
1/**
2 * Matches common Markdown code blocks to exclude them from further processing (e.g. LaTeX).
3 * - Fenced: ```...```
4 * - Inline: `...` (does NOT support nested backticks or multi-backtick syntax)
5 *
6 * Note: This pattern does not handle advanced cases like:
7 *       `` `code with `backticks` `` or \\``...\\``
8 */
9export const CODE_BLOCK_REGEXP = /(```[\s\S]*?```|`[^`\n]+`)/g;
10
11/**
12 * Matches LaTeX math delimiters \(...\) and \[...\] only when not preceded by a backslash (i.e., not escaped),
13 * while also capturing code blocks (```, `...`) so they can be skipped during processing.
14 *
15 * Uses negative lookbehind `(?<!\\)` to avoid matching \\( or \\[.
16 * Using the look‑behind pattern `(?<!\\)` we skip matches
17 * that are preceded by a backslash, e.g.
18 * `Definitions\\(also called macros)` (title of chapter 20 in The TeXbook)
19 * or `\\[4pt]` (LaTeX line-break).
20 *
21 * group 1: code-block
22 * group 2: square-bracket
23 * group 3: round-bracket
24 */
25export const LATEX_MATH_AND_CODE_PATTERN =
26	/(```[\S\s]*?```|`.*?`)|(?<!\\)\\\[([\S\s]*?[^\\])\\]|(?<!\\)\\\((.*?)\\\)/g;
27
28/** Regex to capture the content of a $$...\\\\...$$ block (display-formula with line-break) */
29export const LATEX_LINEBREAK_REGEXP = /\$\$([\s\S]*?\\\\[\s\S]*?)\$\$/;
30
31/**
32 * Matches the unescaped `\[...\]` display-math delimiter and surrounding
33 * context so callers can insert line-breaks around the placeholder or convert
34 * to inline when the formula has a non-empty trailing context (e.g. a table
35 * cell that opens with `\[` and closes with content after `\]`).
36 *
37 * group 1: prefix before `\[`
38 * group 2: formula body
39 * group 3: trailing context after `\]`
40 */
41export const LATEX_DISPLAY_BLOCK_REGEXP = /([\S].*?)\\\[([\s\S]*?)\\\](.*)/g;
42
43/**
44 * Cheap gate for `preprocessLaTeX`. Every transformation it performs is triggered
45 * by a `$` (inline/display math, currency escaping) or a backslash escape
46 * (`\(`, `\[`, `\ce{`, `\pu{`). Text containing neither is returned untouched, so
47 * this lets the caller skip the whole protect/restore pipeline.
48 */
49export const LATEX_TRIGGER_REGEXP = /[$\\]/;
50
51/** Inline LaTeX math delimiter (the dollar sign). */
52export const LATEX_INLINE_DELIMITER = '$';
53
54/** Display LaTeX math delimiter (paired dollar signs). */
55export const LATEX_DISPLAY_DELIMITER = '$$';
56
57/** Matches a single non-whitespace character. */
58export const LATEX_NON_WHITESPACE_REGEXP = /\S/;
59
60/** Matches a character that may appear adjacent to `$`, indicating a non-TeX
61 * context such as an identifier (`var$`, `$var`), currency ($5), or code. */
62export const LATEX_NEIGHBOR_CHAR_REGEXP = /[A-Za-z0-9_$-]/;
63
64/** Matches a single digit (used to detect currency-like `$5`). */
65export const LATEX_DIGIT_REGEXP = /[0-9]/;
66
67/** Matches the leading blockquote prefix (`> ` or `>`) on a markdown line. */
68export const LATEX_BLOCKQUOTE_PREFIX_REGEXP = /^(>\s*)/;
69
70/** Matches the placeholder inserted by the protect/restore pipeline for a
71 * protected LaTeX expression. Group 1 is the index into `latexExpressions`. */
72export const LATEX_PLACEHOLDER_REGEXP = /<<LATEX_(\d+)>>/g;
73
74/** Matches the placeholder inserted by the protect/restore pipeline for a
75 * protected code block. Group 1 is the index into `codeBlocks`. */
76export const CODE_BLOCK_PLACEHOLDER_REGEXP = /<<CODE_BLOCK_(\d+)>>/g;
77
78/** Matches a `$` immediately followed by a digit, which is treated as a
79 * currency amount (e.g. `$5`) and escaped to `\$5` so it isn't parsed as math. */
80export const LATEX_CURRENCY_DOLLAR_REGEXP = /\$(?=\d)/g;
81
82/** Captures remaining `$$...$$`, `\[...\]`, `\(...\)` (only unescaped via
83 * `(?<!\\)`) after the display-block pass has run. Group 1 holds the
84 * matched formula. */
85export const LATEX_PROTECT_REGEXP =
86	/(\$\$[\s\S]*?\$\$|(?<!\\)\\\[[\s\S]*?\\\]|(?<!\\)\\\(.*?\\\))/g;
87
88/** Matches unescaped inline `\(...\)` (at least one char inside) used to
89 * convert `\(` → `$` after the protect pass. */
90export const LATEX_INLINE_CONVERT_REGEXP = /(?<!\\)\\\((.+?)\\\)/g;
91
92/** Matches unescaped display `\[...\]` used to convert `\[` → `$$`
93 * after the protect pass. */
94export const LATEX_DISPLAY_CONVERT_REGEXP = /(?<!\\)\\\[([\s\S]*?)\\\]/g;
95
96/** `\(` — opens an inline LaTeX math block. */
97export const LATEX_INLINE_OPEN = '\\(';
98
99/** `\)` — closes an inline LaTeX math block. */
100export const LATEX_INLINE_CLOSE = '\\)';
101
102/** `\[` — opens a display LaTeX math block. */
103export const LATEX_DISPLAY_OPEN = '\\[';
104
105/** `\]` — closes a display LaTeX math block. */
106export const LATEX_DISPLAY_CLOSE = '\\]';
107
108/** `\` — the LaTeX escape character. */
109export const LATEX_BACKSLASH = '\\';
110
111/** `\$` — dollar sign escaped so it isn't parsed as math (used to disambiguate
112 * currency amounts like `$5`). */
113export const LATEX_CURRENCY_ESCAPE = '\\$';
114
115/** `\ce{` — mhchem chemistry command prefix. */
116export const LATEX_MHCHEM_CE = '\\ce{';
117
118/** `\pu{` — mhchem physics-unit command prefix. */
119export const LATEX_MHCHEM_PU = '\\pu{';
120
121/** map from mchem-regexp to replacement */
122export const MHCHEM_PATTERN_MAP: readonly [RegExp, string][] = [
123	[/(\s)\$\\ce{/g, '$1$\\\\ce{'],
124	[/(\s)\$\\pu{/g, '$1$\\\\pu{']
125] as const;