Repository navigation
Expand file tree
/
Copy pathparser.ts
More file actions
598 lines (543 loc) · 19.2 KB
/
Copy pathparser.ts
File metadata and controls
598 lines (543 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import {
LatexParseError,
type AccentKind,
type MathNode,
type MathVariant,
type MatrixEnvironment,
type ParseOptions,
} from "./types"
import {
assertNestingDepth,
assertSourceLength,
DEFAULT_MAX_NESTING_DEPTH,
DEFAULT_MAX_SOURCE_LENGTH,
resolvePositiveInteger,
} from "./limits"
import { delimiterTable, largeOperators, namedOperators, spacingCommands, symbolTable } from "./symbols"
const matrixEnvironments: MatrixEnvironment[] = [
"matrix",
"pmatrix",
"bmatrix",
"Bmatrix",
"vmatrix",
"Vmatrix",
"cases",
"aligned",
"align",
"gathered",
"gather",
"smallmatrix",
"array",
]
const accents: Readonly<Record<string, AccentKind>> = {
hat: "hat",
widehat: "widehat",
bar: "bar",
overline: "overline",
underline: "underline",
vec: "vec",
tilde: "tilde",
widetilde: "tilde",
dot: "dot",
ddot: "ddot",
}
const variants: Readonly<Record<string, MathVariant>> = {
mathrm: "normal",
textrm: "normal",
mathnormal: "normal",
mathbf: "bold",
boldsymbol: "bold",
bm: "bold",
mathit: "italic",
mathsf: "sans",
mathtt: "monospace",
mathbb: "double-struck",
mathcal: "script",
mathscr: "script",
mathfrak: "fraktur",
}
export function parseLatex(source: string, options: ParseOptions = {}): MathNode {
const expanded = expandLatexMacros(source, options)
const maxDepth = resolvePositiveInteger(options.maxDepth, DEFAULT_MAX_NESTING_DEPTH, "maxDepth")
return new Parser(expanded, options.strict ?? false, maxDepth).parse()
}
export function expandLatexMacros(source: string, options: ParseOptions = {}): string {
const maxSourceLength = resolvePositiveInteger(options.maxSourceLength, DEFAULT_MAX_SOURCE_LENGTH, "maxSourceLength")
const maxExpandedLength = resolvePositiveInteger(options.maxExpandedLength, maxSourceLength, "maxExpandedLength")
const maxExpand = resolvePositiveInteger(options.maxExpand, 100, "maxExpand")
const maxDepth = resolvePositiveInteger(options.maxDepth, DEFAULT_MAX_NESTING_DEPTH, "maxDepth")
assertSourceLength(source, maxSourceLength)
assertNestingDepth(source, maxDepth)
const expanded = expandMacros(source, options.macros, maxExpand, maxExpandedLength)
if (expanded !== source) assertNestingDepth(expanded, maxDepth)
return expanded
}
function expandMacros(
source: string,
macros: ParseOptions["macros"],
maxExpand: number,
maxExpandedLength: number,
): string {
assertSourceLength(source, maxExpandedLength, "Expanded LaTeX source")
if (!macros || Object.keys(macros).length === 0) return source
let result = source
for (let pass = 0; pass < maxExpand; pass++) {
let changed = false
let cursor = 0
let outputLength = 0
const output: string[] = []
const commands = /\\[A-Za-z@]+|\\./g
for (const match of result.matchAll(commands)) {
const command = match[0]
const index = match.index
const replacement = macros[command] ?? macros[command.slice(1)]
if (replacement === undefined) continue
if (typeof replacement !== "string") {
throw new TypeError(`Macro ${command} must expand to a string`)
}
appendWithinLimit(output, result.slice(cursor, index), outputLength, maxExpandedLength)
outputLength += index - cursor
appendWithinLimit(output, replacement, outputLength, maxExpandedLength)
outputLength += replacement.length
cursor = index + command.length
changed = true
}
if (!changed) return result
appendWithinLimit(output, result.slice(cursor), outputLength, maxExpandedLength)
result = output.join("")
}
throw new LatexParseError(`Macro expansion exceeded ${maxExpand} passes`, 0)
}
function appendWithinLimit(output: string[], value: string, currentLength: number, maximum: number): void {
if (currentLength + value.length > maximum) {
throw new LatexParseError(`Expanded LaTeX source exceeds the ${maximum}-character limit`, maximum)
}
output.push(value)
}
class Parser {
private position = 0
private depth = 0
constructor(
private readonly source: string,
private readonly strict: boolean,
private readonly maxDepth: number,
) {}
public parse(): MathNode {
const body = this.parseRow()
this.skipMathWhitespace()
if (!this.done()) this.fail(`Unexpected "${this.peek()}"`)
return row(body)
}
private parseRow(stop?: () => boolean): MathNode[] {
const body: MathNode[] = []
while (!this.done()) {
this.skipMathWhitespace()
if (this.done() || stop?.()) break
const current = this.peek()
if (current === "}") break
if (current === "^" || current === "_") {
this.position++
const script = this.parseArgument()
const previous = body.pop() ?? { type: "row", body: [] }
const existing = previous.type === "scripts" ? previous : { type: "scripts" as const, base: previous }
if (current === "^") existing.superscript = script
else existing.subscript = script
body.push(existing)
continue
}
if (current === "\\" && this.applyLimitsModifier(body)) continue
body.push(this.parseAtom())
}
return body
}
private parseAtom(): MathNode {
this.depth++
if (this.depth > this.maxDepth) {
this.depth--
this.fail(`LaTeX nesting exceeds the ${this.maxDepth}-level limit`)
}
try {
return this.parseAtomInner()
} finally {
this.depth--
}
}
private parseAtomInner(): MathNode {
const current = this.peek()
if (current === "{") return this.parseGroup()
if (current === "\\") return this.parseCommand()
if (current === "~") {
this.position++
return { type: "space", width: 1 }
}
this.position++
return { type: "symbol", value: current, role: inferRole(current) }
}
private parseCommand(): MathNode {
const start = this.position
const command = this.readCommand()
if (command === "\\") return { type: "row", body: [] }
if (command === "begin") return this.parseEnvironment()
if (command === "frac" || command === "dfrac" || command === "tfrac" || command === "cfrac") {
this.skipMathWhitespace()
const alignment =
command === "cfrac" && this.peek() === "[" ? /^\[([lr]?)\]/.exec(this.source.slice(this.position)) : undefined
if (alignment === null) this.fail("Unsupported \\cfrac alignment; expected [l], [r], or []")
if (alignment) this.position += alignment[0].length
return {
type: "fraction",
numerator: this.parseArgument(),
denominator: this.parseArgument(),
bar: true,
...(alignment?.[1] ? { numeratorAlign: alignment[1] === "l" ? "left" : "right" } : {}),
}
}
if (command === "binom" || command === "dbinom" || command === "tbinom") {
const fraction: MathNode = {
type: "fraction",
numerator: this.parseArgument(),
denominator: this.parseArgument(),
bar: false,
}
return { type: "delimited", left: "(", body: fraction, right: ")" }
}
if (command === "sqrt") {
const index = this.parseOptionalArgument()
const result: MathNode = { type: "root", body: this.parseArgument() }
if (index) result.index = index
return result
}
if (command === "left") return this.parseLeftRight()
if (command === "middle") return { type: "symbol", value: this.readDelimiter() }
if (command === "right") {
this.position = start
this.fail("Unexpected \\right")
}
if (command in accents) {
return { type: "accent", accent: accents[command], body: this.parseArgument() }
}
if (command in variants) {
return {
type: "variant",
variant: variants[command],
body: command === "textrm" ? { type: "text", value: this.readTextGroup() } : this.parseArgument(),
}
}
if (command === "text" || command === "mbox") return { type: "text", value: this.readTextGroup() }
if (command === "operatorname") {
const limits = this.peek() === "*"
if (limits) this.position++
return { type: "operator", value: this.readTextGroup(), limits }
}
if (command === "overset" || command === "stackrel") {
const over = this.parseArgument()
const base = this.parseArgument()
return { type: "overunder", base, over }
}
if (command === "underset") {
const under = this.parseArgument()
const base = this.parseArgument()
return { type: "overunder", base, under }
}
if (command === "overbrace" || command === "underbrace") {
return { type: "brace", body: this.parseArgument(), position: command === "overbrace" ? "over" : "under" }
}
if (command === "textcolor") {
const color = this.readRawGroup()
return { type: "color", color, body: this.parseArgument() }
}
if (command === "color") {
const color = this.readRawGroup()
return { type: "color", color, body: row(this.parseRow()) }
}
if (command === "not") {
const target = this.parseAtom()
if (target.type === "symbol") return { ...target, value: negateSymbol(target.value) }
return { type: "row", body: [{ type: "symbol", value: "¬" }, target] }
}
if (command === "pmod") {
return {
type: "row",
body: [
{ type: "space", width: 1 },
{ type: "text", value: "(mod " },
this.parseArgument(),
{ type: "text", value: ")" },
],
}
}
if (command === "mod" || command === "bmod") return { type: "operator", value: "mod", limits: false }
if (command === "displaylines") {
this.skipMathWhitespace()
this.expect("{")
return this.parseMatrix("gathered", "}")
}
if (
command === "limits" ||
command === "nolimits" ||
command === "displaystyle" ||
command === "textstyle" ||
command === "scriptstyle" ||
command === "scriptscriptstyle"
) {
return { type: "row", body: [] }
}
if (/^(?:big|Big|bigg|Bigg)[lrm]?$/.test(command)) {
return { type: "symbol", value: this.readDelimiter() }
}
if (command in spacingCommands) return { type: "space", width: spacingCommands[command] }
if (command in symbolTable) {
const symbol = symbolTable[command]
return { type: "symbol", value: symbol.value, ...(symbol.role ? { role: symbol.role } : {}) }
}
if (command in largeOperators) {
return { type: "operator", value: largeOperators[command], limits: !command.includes("int") }
}
if (namedOperators.has(command)) {
return {
type: "operator",
value: command,
limits: command.startsWith("lim") || command === "min" || command === "max",
}
}
if (command === "backslash") return { type: "symbol", value: "\\" }
const delimiter = delimiterTable[`\\${command}`] ?? delimiterTable[command]
if (delimiter !== undefined) return { type: "symbol", value: delimiter }
if (command === "{" || command === "}") return { type: "symbol", value: command }
if (command === "%" || command === "#" || command === "$" || command === "&" || command === "_") {
return { type: "symbol", value: command }
}
if (this.strict) this.fail(`Unsupported command \\${command}`, start)
return { type: "text", value: `\\${command}` }
}
private parseEnvironment(): MathNode {
const rawEnvironment = this.readRawGroup()
const unstarredEnvironment = rawEnvironment.endsWith("*") ? rawEnvironment.slice(0, -1) : rawEnvironment
const environment = matrixEnvironments.find((name) => name === unstarredEnvironment)
if (!environment) {
if (this.strict) this.fail(`Unsupported environment ${unstarredEnvironment}`)
const content = this.readUntilEnd(rawEnvironment)
return { type: "text", value: content }
}
const columns = environment === "array" ? this.readRawGroup().replace(/\s/g, "") : undefined
if (columns !== undefined && (!/^[lcr|]+$/.test(columns) || !/[lcr]/.test(columns))) {
this.fail("Unsupported array columns; expected l, c, r, and |")
}
return this.parseMatrix(environment, `\\end{${rawEnvironment}}`, columns)
}
private parseMatrix(environment: MatrixEnvironment, end: string, columns?: string): MathNode {
const rows: MathNode[][] = []
let cells: MathNode[] = []
while (!this.done()) {
this.skipMathWhitespace()
if (this.source.startsWith(end, this.position) && cells.length === 0) break
const cellStart = this.position
const cell = row(
this.parseRow(
() =>
this.peek() === "&" ||
this.source.startsWith("\\\\", this.position) ||
this.source.startsWith(end, this.position),
),
)
cells.push(cell)
this.skipMathWhitespace()
if (this.peek() === "&") {
this.position++
continue
}
if (this.source.startsWith("\\\\", this.position)) {
this.position += 2
this.consumeOptionalBracket()
rows.push(cells)
cells = []
continue
}
if (this.source.startsWith(end, this.position)) break
// Empty cells are valid only when a cell, row, or closing delimiter advances the parser.
if (this.position === cellStart) this.fail(`Unexpected "${this.peek()}" in ${environment}`)
}
if (!this.source.startsWith(end, this.position)) this.fail(`Missing ${end}`)
this.expect(end)
if (cells.length > 0 || rows.length === 0) rows.push(cells)
return { type: "matrix", rows, environment, ...(columns !== undefined ? { columns } : {}) }
}
private parseLeftRight(): MathNode {
const left = this.readDelimiter()
const atRight = () =>
this.source.startsWith("\\right", this.position) && !/[A-Za-z@]/.test(this.source[this.position + 6] ?? "")
const body = row(this.parseRow(atRight))
if (!atRight()) this.fail("Missing \\right")
this.readCommand()
const right = this.readDelimiter()
return { type: "delimited", left, body, right }
}
private parseArgument(): MathNode {
this.skipMathWhitespace()
if (this.peek() === "{") return this.parseGroup()
if (this.done()) this.fail("Expected an argument")
return this.parseAtom()
}
private parseGroup(): MathNode {
this.expect("{")
const body = row(this.parseRow())
this.expect("}")
return body
}
private parseOptionalArgument(): MathNode | undefined {
this.skipMathWhitespace()
if (this.peek() !== "[") return undefined
this.position++
const body = row(this.parseRow(() => this.peek() === "]"))
this.expect("]")
return body
}
private consumeOptionalBracket(): void {
this.skipMathWhitespace()
if (this.peek() !== "[") return
let depth = 0
while (!this.done()) {
const char = this.source[this.position++]
if (char === "[") depth++
if (char === "]" && --depth === 0) return
}
}
private readDelimiter(): string {
this.skipMathWhitespace()
if (this.done()) this.fail("Expected a delimiter")
const start = this.position
if (this.peek() === "\\") {
const command = this.readCommand()
const delimiter = delimiterTable[`\\${command}`] ?? delimiterTable[command]
if (delimiter !== undefined) return delimiter
if (this.strict) this.fail(`Unsupported delimiter \\${command}`, start)
return `\\${command}`
}
const token = this.source[this.position++]
return delimiterTable[token] ?? token
}
private readCommand(): string {
this.expect("\\")
if (this.done()) return "\\"
const next = this.peek()
if (!/[A-Za-z@]/.test(next)) {
this.position++
return next
}
const start = this.position
while (!this.done() && /[A-Za-z@]/.test(this.peek())) this.position++
const command = this.source.slice(start, this.position)
if (this.peek() === " ") this.position++
return command
}
private readRawGroup(): string {
this.skipMathWhitespace()
this.expect("{")
const start = this.position
let depth = 1
while (!this.done()) {
const char = this.source[this.position++]
const escaped = (char === "{" || char === "}") && this.isEscaped(this.position - 1)
if (char === "{" && !escaped) depth++
if (char === "}" && !escaped && --depth === 0) return this.source.slice(start, this.position - 1)
}
return this.fail("Unterminated group", start)
}
private applyLimitsModifier(body: MathNode[]): boolean {
const match = /^\\(limits|nolimits)(?![A-Za-z@])/.exec(this.source.slice(this.position))
if (!match) return false
this.position += match[0].length
const target = body.at(-1)
const operator =
target?.type === "operator"
? target
: target?.type === "scripts" && target.base.type === "operator"
? target.base
: undefined
if (operator) operator.limits = match[1] === "limits"
return true
}
private isEscaped(index: number): boolean {
let slashCount = 0
for (let cursor = index - 1; cursor >= 0 && this.source[cursor] === "\\"; cursor--) slashCount++
return slashCount % 2 === 1
}
private readTextGroup(): string {
return this.readRawGroup()
.replace(/\\([A-Za-z@]+|.)/g, (match, command: string) => {
if ("{}%#$&_ ".includes(command)) return command
if (command === "textbackslash") return "\\"
if (command === "!") return ""
if (command in spacingCommands) return " ".repeat(Math.max(1, spacingCommands[command]))
return match
})
.replace(/~/g, " ")
}
private readUntilEnd(environment: string): string {
const marker = `\\end{${environment}}`
const end = this.source.indexOf(marker, this.position)
if (end < 0) this.fail(`Missing ${marker}`)
const content = this.source.slice(this.position, end)
this.position = end + marker.length
return content
}
private skipMathWhitespace(): void {
while (!this.done()) {
if (/\s/.test(this.peek())) {
this.position++
continue
}
if (this.peek() === "%") {
while (!this.done() && this.peek() !== "\n") this.position++
continue
}
break
}
}
private expect(value: string): void {
if (!this.source.startsWith(value, this.position)) this.fail(`Expected "${value}"`)
this.position += value.length
}
private peek(): string {
return this.source[this.position] ?? ""
}
private done(): boolean {
return this.position >= this.source.length
}
private fail(message: string, position = this.position): never {
throw new LatexParseError(message, position)
}
}
function row(body: MathNode[]): MathNode {
if (body.length === 1) return body[0]
return { type: "row", body }
}
function inferRole(value: string): "binary" | "relation" | "punctuation" | "opening" | "closing" | "ordinary" {
if ("+-*/×÷±∓".includes(value)) return "binary"
if ("=<>≤≥≠≈∈∉⊂⊃".includes(value)) return "relation"
if (",;:".includes(value)) return "punctuation"
if ("([{".includes(value)) return "opening"
if (")]}".includes(value)) return "closing"
return "ordinary"
}
function negateSymbol(value: string): string {
const negated: Record<string, string> = {
"=": "≠",
"∈": "∉",
"∋": "∌",
"≡": "≢",
"≈": "≉",
"∼": "≁",
"<": "≮",
">": "≯",
"≤": "≰",
"≥": "≱",
"⊂": "⊄",
"⊃": "⊅",
"⊆": "⊈",
"⊇": "⊉",
"∣": "∤",
"∥": "∦",
}
return negated[value] ?? `${value}̸`
}