@cortex-js/compute-engine 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/dist/compute-engine.esm.js +25479 -24875
  2. package/dist/compute-engine.js +25480 -24876
  3. package/dist/compute-engine.min.esm.js +13 -13
  4. package/dist/compute-engine.min.js +13 -13
  5. package/dist/math-json.esm.js +6 -3
  6. package/dist/math-json.js +6 -3
  7. package/dist/math-json.min.esm.js +6 -3
  8. package/dist/math-json.min.js +2 -2
  9. package/dist/types/common/ansi-codes.d.ts +1 -1
  10. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  11. package/dist/types/common/signals.d.ts +1 -1
  12. package/dist/types/common/utils.d.ts +1 -1
  13. package/dist/types/compute-engine/assume.d.ts +1 -1
  14. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +30 -19
  15. package/dist/types/compute-engine/boxed-expression/box.d.ts +9 -9
  16. package/dist/types/compute-engine/boxed-expression/boxed-dictionary.d.ts +4 -4
  17. package/dist/types/compute-engine/boxed-expression/boxed-domain.d.ts +3 -2
  18. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +2 -1
  19. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +9 -10
  20. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +5 -4
  21. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +2 -15
  22. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +3 -4
  23. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +1 -1
  24. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +7 -8
  25. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +6 -7
  26. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +2 -2
  27. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  28. package/dist/types/compute-engine/boxed-expression/factor.d.ts +11 -0
  29. package/dist/types/compute-engine/boxed-expression/match.d.ts +14 -0
  30. package/dist/types/compute-engine/boxed-expression/numerics.d.ts +41 -0
  31. package/dist/types/compute-engine/boxed-expression/order.d.ts +6 -6
  32. package/dist/types/compute-engine/boxed-expression/public.d.ts +1964 -0
  33. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +3 -15
  34. package/dist/types/compute-engine/boxed-expression/utils.d.ts +5 -2
  35. package/dist/types/compute-engine/boxed-expression/validate.d.ts +7 -6
  36. package/dist/types/compute-engine/collection-utils.d.ts +1 -1
  37. package/dist/types/compute-engine/compile.d.ts +2 -2
  38. package/dist/types/compute-engine/compute-engine.d.ts +83 -60
  39. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  40. package/dist/types/compute-engine/domain-utils.d.ts +3 -2
  41. package/dist/types/compute-engine/function-utils.d.ts +2 -2
  42. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  43. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  44. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  45. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  46. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  47. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  48. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  49. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  50. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -3
  51. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  52. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  53. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  54. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  55. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +5 -4
  56. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +1 -1
  57. package/dist/types/compute-engine/latex-syntax/parse.d.ts +71 -29
  58. package/dist/types/compute-engine/latex-syntax/public.d.ts +264 -252
  59. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +9 -3
  60. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +1 -1
  61. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +6 -11
  62. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +3 -2
  63. package/dist/types/compute-engine/library/arithmetic-add.d.ts +5 -5
  64. package/dist/types/compute-engine/library/arithmetic-divide.d.ts +2 -1
  65. package/dist/types/compute-engine/library/arithmetic-multiply.d.ts +4 -4
  66. package/dist/types/compute-engine/library/arithmetic-power.d.ts +2 -1
  67. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  68. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  69. package/dist/types/compute-engine/library/collections.d.ts +1 -1
  70. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  71. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  72. package/dist/types/compute-engine/library/core.d.ts +3 -2
  73. package/dist/types/compute-engine/library/domains.d.ts +1 -1
  74. package/dist/types/compute-engine/library/library.d.ts +1 -1
  75. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  76. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  77. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  78. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  79. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -2
  80. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  81. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  82. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  83. package/dist/types/compute-engine/library/utils.d.ts +2 -2
  84. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +7 -1
  85. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -1
  86. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  87. package/dist/types/compute-engine/numerics/numeric.d.ts +7 -17
  88. package/dist/types/compute-engine/numerics/primes.d.ts +6 -1
  89. package/dist/types/compute-engine/numerics/rationals.d.ts +4 -13
  90. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  91. package/dist/types/compute-engine/numerics/terms.d.ts +7 -4
  92. package/dist/types/compute-engine/public.d.ts +3 -1839
  93. package/dist/types/compute-engine/rules.d.ts +1 -1
  94. package/dist/types/compute-engine/simplify-rules.d.ts +1 -1
  95. package/dist/types/compute-engine/solve.d.ts +4 -3
  96. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  97. package/dist/types/compute-engine/symbolic/expand.d.ts +2 -2
  98. package/dist/types/compute-engine/symbolic/flatten.d.ts +6 -4
  99. package/dist/types/compute-engine/symbolic/negate.d.ts +1 -1
  100. package/dist/types/compute-engine/symbolic/polynomials.d.ts +1 -1
  101. package/dist/types/compute-engine/symbolic/product.d.ts +4 -3
  102. package/dist/types/compute-engine/symbolic/tensor-fields.d.ts +1 -1
  103. package/dist/types/compute-engine/symbolic/tensors.d.ts +1 -1
  104. package/dist/types/compute-engine/symbolic/utils.d.ts +5 -10
  105. package/dist/types/compute-engine.d.ts +2 -2
  106. package/dist/types/math-json/math-json-format.d.ts +3 -3
  107. package/dist/types/math-json/utils.d.ts +4 -6
  108. package/dist/types/math-json.d.ts +2 -2
  109. package/package.json +1 -1
  110. package/dist/types/compute-engine/latex-syntax/latex-syntax.d.ts +0 -28
  111. package/dist/types/compute-engine/numerics/factor.d.ts +0 -27
@@ -1,26 +1,36 @@
1
- /* 0.24.0 */
2
- import { Expression } from '../../math-json/math-json-format';
3
- import type { IComputeEngine } from '../public';
1
+ /* 0.25.0 */
2
+ export type SymbolType = 'symbol' | 'function' | 'unknown';
3
+ export type SymbolTable = {
4
+ parent: SymbolTable | null;
5
+ ids: {
6
+ [id: string]: SymbolType;
7
+ };
8
+ };
4
9
  /**
5
- * A `LatexToken` is a token as returned by `Scanner.peek`.
10
+ * A `LatexToken` is a token as returned by `Parser.peek`.
6
11
  *
7
12
  * It can be one of the indicated tokens, or a string that starts with a
8
13
  * `\` for LaTeX commands, or a LaTeX character which includes digits,
9
14
  * letters and punctuation.
15
+ *
10
16
  * @category Latex Parsing and Serialization
11
17
  */
12
18
  export type LatexToken = string | '<{>' | '<}>' | '<space>' | '<$>' | '<$$>';
13
19
  /** A LatexString is a regular string of LaTeX, for example:
14
20
  * `\frac{\pi}{2}`
21
+ *
15
22
  * @category Latex Parsing and Serialization
16
23
  */
17
24
  export type LatexString = string;
18
25
  /**
19
26
  * Open and close delimiters that can be used with {@linkcode MatchfixEntry}
20
27
  * record to define new LaTeX dictionary entries.
28
+ *
21
29
  * @category Latex Parsing and Serialization
22
30
  */
23
31
  export type Delimiter = ')' | '(' | ']' | '[' | '{' /** \lbrace */ | '}' /** \rbrace */ | '<' /** \langle */ | '>' /** \rangle */ | '|' | '||' | '\\lceil' | '\\rceil' | '\\lfloor' | '\\rfloor';
32
+ /** @category Latex Parsing and Serialization */
33
+ export type DelimiterScale = 'normal' | 'scaled' | 'big' | 'none';
24
34
  /**
25
35
  * @category Latex Parsing and Serialization
26
36
  */
@@ -96,7 +106,8 @@ export declare const POSTFIX_PRECEDENCE: Precedence;
96
106
  * This indicates a condition under which parsing should stop:
97
107
  * - an operator of a precedence higher than specified has been encountered
98
108
  * - the last token has been reached
99
- * - or if a condition is provided, the condition returns true;
109
+ * - or if a condition is provided, the condition returns true
110
+ *
100
111
  * @category Latex Parsing and Serialization
101
112
  */
102
113
  export type Terminator = {
@@ -106,11 +117,11 @@ export type Terminator = {
106
117
  /**
107
118
  * Custom parsing handler.
108
119
  *
109
- * When invoked the scanner points right after the LaTeX fragment that triggered
120
+ * When invoked the parser points right after the LaTeX fragment that triggered
110
121
  * this parsing handler.
111
122
  *
112
- * The scanner should be moved, by calling `scanner.next()` for every consumed
113
- * token.
123
+ * The parser should be moved, by calling `parser.nextToken()` for
124
+ * every consumed token.
114
125
  *
115
126
  * If it was in an infix or postfix context, `lhs` will represent the
116
127
  * left-hand side argument. In a prefix or matchfix context, `lhs` is `null`.
@@ -159,15 +170,14 @@ export type MatchfixParseHandler = (parser: Parser, body: Expression) => Express
159
170
  */
160
171
  export type LatexArgumentType = '{expression}' /** A required math mode expression */ | '[expression]' /** An optional math mode expression */ | '{text}' /** A required expression in text mode */ | '[text]' /** An optional expression in text mode */ | '{unit}' /** A required unit expression, e.g. `3em` */ | '[unit]' /** An optional unit expression, e.g. `3em` */ | '{glue}' /** A required glue expression, e.g. `25 mu plus 3em ` */ | '[glue]' /** An optional glue expression, e.g. `25 mu plus 3em ` */ | '{string}' /** A required text string, terminated by a non-literal token */ | '[string]' /** An optional text string, terminated by a non-literal token */ | '{color}' /** A required color expression, e.g. `red` or `#00ff00` */ | '[color]'; /** An optional color expression, e.g. `red` or `#00ff00` */
161
172
  /**
162
- * The trigger is the set of tokens that will make this record eligible to
163
- * parse the stream and generate an expression. If the trigger matches,
164
- * the `parse` handler is called, if available.
173
+ * A trigger is the set of tokens that will make an entry in the
174
+ * LaTeX dictionary eligible to parse the stream and generate an expression.
175
+ * If the trigger matches, the `parse` handler is called, if available.
165
176
  *
166
177
  * The trigger can be specified either as a LaTeX string (`latexTrigger`) or
167
- * as an identifier (`identifierTrigger`), which can be wrapped in a LaTeX
168
- * command, for example `\operatorname{mod}` or `\mathbin{gcd}`, with `"gcd"`
169
- * being the `identifierTrigger`.
170
- *
178
+ * as an identifier (`identifierTrigger`). An identifier match several
179
+ * LaTeEx expressions that are equivalent, for example `\operatorname{gcd}` or
180
+ * `\mathbin{gcd}`, match the `"gcd"` identifier
171
181
  *
172
182
  * `matchfix` operators use `openTrigger` and `closeTrigger` instead.
173
183
  *
@@ -183,14 +193,16 @@ export type Trigger = {
183
193
  */
184
194
  export type BaseEntry = {
185
195
  /**
186
- * Map a MathJSON function or symbol name to this entry.
196
+ * Map a MathJSON identifier to this entry.
187
197
  *
188
198
  * Each entry should have at least a `name` or a `parse` handler.
189
199
  *
190
200
  * An entry with no `name` cannot be serialized: the `name` is used to map
191
- * a MathJSON function or symbol name to the appropriate entry for serializing.
192
- * However, an entry with no `name` can be used to define a synonym (for example
193
- * for the symbol `\varnothing` which is a synonym for `\emptyset`).
201
+ * a MathJSON function or symbol name to the appropriate entry for
202
+ * serializing.
203
+ *
204
+ * However, an entry with no `name` can be used to define a synonym (for
205
+ * example for the symbol `\varnothing` which is a synonym for `\emptyset`).
194
206
  *
195
207
  * If no `parse` handler is provided, only the trigger is used to select this
196
208
  * entry. Otherwise, if the trigger of the entry matches the current
@@ -199,7 +211,7 @@ export type BaseEntry = {
199
211
  name?: string;
200
212
  /**
201
213
  * Transform an expression into a LaTeX string.
202
- * If no `serialize` handler is provided, the `trigger` property is used
214
+ * If no `serialize` handler is provided, the trigger is used.
203
215
  */
204
216
  serialize?: LatexString | SerializeHandler;
205
217
  };
@@ -245,16 +257,16 @@ export type InfixEntry = BaseEntry & Trigger & {
245
257
  */
246
258
  kind: 'infix';
247
259
  /**
248
- * - **`both`**: a + b + c +(a, b, c)
260
+ * - **`none`**: a ? b ? c -> syntax error
261
+ * - **`any`**: a + b + c -> +(a, b, c)
249
262
  * - **`left`**: a / b / c -> /(/(a, b), c)
250
263
  * - **`right`**: a = b = c -> =(a, =(b, c))
251
- * - **`non`**: a < b < c -> syntax error
252
264
  *
253
- * - a `both`-associative operator has an unlimited number of arguments
254
- * - a `left`, `right` or `non` associative operator has at most two arguments
265
+ * - `any`-associative operators have an unlimited number of arguments
266
+ * - `left`, `right` or `none` associative operators have two arguments
255
267
  *
256
268
  */
257
- associativity?: 'right' | 'left' | 'non' | 'both';
269
+ associativity?: 'right' | 'left' | 'none' | 'any';
258
270
  precedence?: Precedence;
259
271
  parse?: string | InfixParseHandler;
260
272
  };
@@ -320,7 +332,7 @@ export type FunctionEntry = BaseEntry & Trigger & {
320
332
  /**
321
333
  *
322
334
  * A dictionary entry is a record that maps a LaTeX token or string of tokens
323
- * to a MathJSON expression or to a parsing handler.
335
+ * ( a trigger) to a MathJSON expression or to a parsing handler.
324
336
  *
325
337
  * Set the {@linkcode ComputeEngine.latexDictionary} property to an array of
326
338
  * dictionary entries to define custom LaTeX parsing and serialization.
@@ -346,19 +358,93 @@ export declare function isPostfixEntry(entry: LatexDictionaryEntry): entry is Po
346
358
  /** @internal */
347
359
  export declare function isEnvironmentEntry(entry: LatexDictionaryEntry): entry is EnvironmentEntry;
348
360
  /**
361
+ * A LaTeX dictionary is a collection of LaTeX dictionary entries.
362
+ *
363
+ * Each entry in the dictionary indicates how to parse and serialize a
364
+ * particular LaTeX token or string of tokens.
349
365
  *
350
366
  * @category Latex Parsing and Serialization
351
367
  * @internal
352
368
  */
353
- export type LatexDictionary = Array<Partial<LatexDictionaryEntry>>;
369
+ export type LatexDictionary = ReadonlyArray<Partial<LatexDictionaryEntry>>;
370
+ /**
371
+ * These options control how numbers are parsed and serialized.
372
+ */
373
+ export type NumberFormat = {
374
+ positiveInfinity: LatexString;
375
+ negativeInfinity: LatexString;
376
+ notANumber: LatexString;
377
+ imaginaryUnit: LatexString;
378
+ /**
379
+ * A string representing the decimal separator, the string separating
380
+ * the whole portion of a number from the fractional portion, i.e.
381
+ * the "." in "3.1415".
382
+ *
383
+ * Some countries use a comma rather than a dot. In this case it is
384
+ * recommended to use `"{,}"` as the separator: the surrounding brackets
385
+ * ensure there is no additional gap after the comma.
386
+ *
387
+ * **Default**: `"."`
388
+ */
389
+ decimalSeparator: LatexString;
390
+ /**
391
+ * A string representing the separator between groups of digits,
392
+ * to make numbers with many digits easier to read.
393
+ *
394
+ * If a single string is provided, it is used to group digits in the
395
+ * whole and the fractional part of the number. If two strings are provided,
396
+ * the first is used for the whole part and the second for the fractional
397
+ * part.
398
+ *
399
+ * Caution: some values may lead to unexpected results.
400
+ *
401
+ * For example, if the `digitGroupSeparator` is `,` (comma) the expression
402
+ * `\operatorname{Hypot}(1,2)` will parse as `["Hypot", 1.2]` rather than
403
+ * `["Hypot", 1, 2]`. You can however use `{,}` which will avoid this issue
404
+ * and display with correct spacing.
405
+ *
406
+ * **Default**: `"\\,"` (thin space, 3/18mu) (Resolution 7 of the 1948 CGPM)
407
+ */
408
+ digitGroupSeparator: LatexString | [LatexString, LatexString];
409
+ /**
410
+ * Maximum length of digits between digit group separators.
411
+ *
412
+ * If a single number is provided, it is used for the whole and the fractional
413
+ * part of the number. If two numbers are provided, the first is used for the
414
+ * whole part and the second for the fractional part.
415
+ *
416
+ * If '`"lakh"`' is provided, the number is grouped in groups of 2 digits,
417
+ * except for the last group which has 3 digits. For example: `1,00,00,000`.
418
+ *
419
+ *
420
+ * **Default**: `3`
421
+ */
422
+ digitGroup: 'lakh' | number | [number | 'lakh', number];
423
+ exponentProduct: LatexString;
424
+ beginExponentMarker: LatexString;
425
+ endExponentMarker: LatexString;
426
+ truncationMarker: LatexString;
427
+ repeatingDecimal: 'auto' | 'vinculum' | 'dots' | 'parentheses' | 'arc' | 'none';
428
+ };
429
+ export type NumberSerializationFormat = NumberFormat & {
430
+ /**
431
+ * The maximum number of significant digits in serialized numbers.
432
+ * - `"max"`: all availabe digits are serialized.
433
+ * - `"auto"`: use the same precision as the compute engine.
434
+ *
435
+ * Default: `"auto"`
436
+ */
437
+ fractionalDigits: 'auto' | 'max' | number;
438
+ notation: 'auto' | 'engineering' | 'scientific';
439
+ avoidExponentsInRange: undefined | null | [negativeExponent: number, positiveExponent: number];
440
+ };
354
441
  /**
355
442
  *
356
- * The LaTeX parsing options can be set using the
357
- * {@linkcode ComputeEngine.latexOptions} property.
443
+ * The LaTeX parsing options can be used with the `ce.parse()` method.
358
444
  *
359
445
  * @category Latex Parsing and Serialization
360
446
  */
361
- export type ParseLatexOptions = {
447
+ export type ParseLatexOptions = NumberFormat & {
362
448
  /**
363
449
  * If true, ignore space characters in math mode.
364
450
  *
@@ -367,18 +453,9 @@ export type ParseLatexOptions = {
367
453
  */
368
454
  skipSpace: boolean;
369
455
  /**
370
- * When an unknown LaTeX command is encountered, attempt to parse
371
- * any arguments it may have.
456
+ * When parsing a decimal number, e.g. `3.1415`:
372
457
  *
373
- * For example, `\foo{x+1}` would produce `['\foo', ['Add', 'x', 1]]` if
374
- * this property is true, `['LatexSymbols', '\foo', '<{>', 'x', '+', 1, '<{>']`
375
- * otherwise.
376
- */
377
- parseArgumentsOfUnknownLatexCommands: boolean;
378
- /**
379
- * When parsing a decimal number (e.g. `3.1415`):
380
- *
381
- * - `"auto"` or `"decimal"`: if a decimal number parse it as an approximate
458
+ * - `"auto"` or `"decimal"`: if a decimal number, parse it as an approximate
382
459
  * decimal number with a whole part and a fractional part
383
460
  * - `"rational"`: if a decimal number, parse it as an exact rational number
384
461
  * with a numerator and a denominator. If not a decimal number, parse
@@ -395,21 +472,32 @@ export type ParseLatexOptions = {
395
472
  parseNumbers: 'auto' | 'rational' | 'decimal' | 'never';
396
473
  /**
397
474
  * This handler is invoked when the parser encounters an identifier
398
- * that does not have a corresponding entry in the dictionary.
475
+ * that has not yet been declared.
399
476
  *
400
477
  * The `identifier` argument is a (valid identifier)[/math-json/#identifiers].
401
478
  *
402
479
  * The handler can return:
403
480
  *
404
- * - `"symbol"`: the identifier is a constant or variable name.
405
- *
481
+ * - `"variable"`: the identifier is a variable
406
482
  * - `"function"`: the identifier is a function name. If an apply
407
483
  * function operator (typically, parentheses) follow, they will be parsed
408
484
  * as arguments to the function.
409
485
  *
410
486
  * - `"unknown"`: the identifier is not recognized.
411
487
  */
412
- parseUnknownIdentifier: (identifier: string, parser: Parser) => 'symbol' | 'function' | 'unknown';
488
+ getIdentifierType: (identifier: string) => SymbolType;
489
+ /** This handler is invoked when the parser encounters an unexpected token.
490
+ *
491
+ * The `lhs` argument is the left-hand side of the token, if any.
492
+ *
493
+ * The handler can access the unexpected token with `parser.peek`. If
494
+ * it is a token that should be recognized, the handler can consume it
495
+ * by calling `parser.nextToken()`.
496
+ *
497
+ * The handler should return an expression or `null` if the token is not
498
+ * recognized.
499
+ */
500
+ parseUnexpectedToken: (lhs: Expression | null, parser: Parser) => Expression | null;
413
501
  /**
414
502
  * If true, the expression will be decorated with the LaTeX
415
503
  * fragments corresponding to each elements of the expression.
@@ -424,213 +512,13 @@ export type ParseLatexOptions = {
424
512
  */
425
513
  preserveLatex: boolean;
426
514
  };
427
- /** @category Latex Parsing and Serialization */
428
- export type DelimiterScale = 'normal' | 'scaled' | 'big' | 'none';
429
- /**
430
- *
431
- * The LaTeX serialization options can be set using the
432
- * {@linkcode ComputeEngine.latexOptions} property.
433
- *
434
- * @category Latex Parsing and Serialization
435
- */
436
- export type SerializeLatexOptions = {
437
- /**
438
- * LaTeX string used to render an invisible multiply, e.g. in '2x'.
439
- *
440
- * Leave it empty to join the adjacent terms, i.e. `2x`.
441
- *
442
- * Use `\cdot` to insert a `\cdot` operator between them, i.e. `2\cdot x`.
443
- *
444
- * Empty by default.
445
- */
446
- invisibleMultiply: LatexString;
447
- /**
448
- * LaTeX string used for an invisible plus with mixed numbers e.g. in '1 3/4'.
449
- *
450
- * Leave it empty to join the main number and the fraction, i.e. render it
451
- * as `1\frac{3}{4}`.
452
- *
453
- * Use `+` to insert an explicit `+` operator between them,
454
- * i.e. `1+\frac{3}{4}`
455
- *
456
- * Empty by default.
457
- */
458
- invisiblePlus: LatexString;
459
- /**
460
- * LaTeX string used for an explicit multiply operator,
461
- *
462
- * Default: `\times`
463
- */
464
- multiply: LatexString;
465
- /**
466
- * When an expression contains the error expression `["Error", 'missing']`,
467
- * serialize it with this LaTeX string
468
- */
469
- missingSymbol: LatexString;
470
- applyFunctionStyle: (expr: Expression, level: number) => DelimiterScale;
471
- groupStyle: (expr: Expression, level: number) => DelimiterScale;
472
- rootStyle: (expr: Expression, level: number) => 'radical' | 'quotient' | 'solidus';
473
- fractionStyle: (expr: Expression, level: number) => 'quotient' | 'block-quotient' | 'inline-quotient' | 'inline-solidus' | 'nice-solidus' | 'reciprocal' | 'factor';
474
- logicStyle: (expr: Expression, level: number) => 'word' | 'boolean' | 'uppercase-word' | 'punctuation';
475
- powerStyle: (expr: Expression, level: number) => 'root' | 'solidus' | 'quotient';
476
- numericSetStyle: (expr: Expression, level: number) => 'compact' | 'regular' | 'interval' | 'set-builder';
477
- };
478
- /**
479
- *
480
- * The options to format numbers can be set using the
481
- * {@linkcode ComputeEngine.latexOptions} property.
482
- *
483
- * @category Latex Parsing and Serialization
484
- */
485
- export type NumberFormattingOptions = {
486
- precision: number;
487
- positiveInfinity: LatexString;
488
- negativeInfinity: LatexString;
489
- notANumber: LatexString;
490
- /**
491
- * A string representing the decimal marker, the string separating
492
- * the whole portion of a number from the fractional portion, i.e.
493
- * the '.' in '3.1415'.
494
- *
495
- * Some countries use a comma rather than a dot. In this case it is
496
- * recommended to use `"{,}"` as the marker: the surrounding brackets ensure
497
- * there is no additional gap after the comma.
498
- *
499
- * **Default**: `"."`
500
- */
501
- decimalMarker: LatexString;
502
- /**
503
- * A string representing the separator between groups of digits,
504
- * used to improve readability of numbers with lots of digits.
505
- *
506
- * If you change it to another value, be aware that this may lead to
507
- * unexpected results. For example, if changing it to `,` the expression
508
- * `\operatorname{Hypot}(1,2)` will parse as `["Hypot", 1.2]` rather than
509
- * `["Hypot", 1, 2]`.
510
- *
511
- * **Default**: `"\\,"` (thin space, 3/18mu) (Resolution 7 of the 1948 CGPM)
512
- */
513
- groupSeparator: LatexString;
514
- exponentProduct: LatexString;
515
- beginExponentMarker: LatexString;
516
- endExponentMarker: LatexString;
517
- notation: 'engineering' | 'auto' | 'scientific';
518
- truncationMarker: LatexString;
519
- beginRepeatingDigits: LatexString;
520
- endRepeatingDigits: LatexString;
521
- imaginaryUnit: LatexString;
522
- avoidExponentsInRange: undefined | null | [negativeExponent: number, positiveExponent: number];
523
- };
524
515
  /**
525
- * The {@linkcode ComputeEngine.latexSyntax} property is an instance of
526
- * `LatexSyntax` that can be used to parse and serialize LaTeX strings.
527
- *
528
- * Its methods are exposed with `ce.parse`, `ce.serialize` and
529
- * `ce.getLatexDictionary`.
530
- *
531
- * @category Latex Parsing and Serialization
532
- * @internal
533
- */
534
- export declare class LatexSyntax {
535
- /**
536
- *
537
- * @param options.onError - Called when a non-fatal error is encountered. When parsing,
538
- * the parser will attempt to recover and continue.
539
- *
540
- */
541
- constructor(options?: NumberFormattingOptions & ParseLatexOptions & SerializeLatexOptions & {
542
- onError?: WarningSignalHandler;
543
- dictionary?: LatexDictionary;
544
- });
545
- /**
546
- * Return a LaTeX dictionary suitable for the specified category, or `"all"`
547
- * for all categories (`"arithmetic"`, `"algebra"`, etc...).
548
- *
549
- * A LaTeX dictionary is needed to translate between LaTeX and MathJSON.
550
- *
551
- * Each entry in the dictionary indicate how a LaTeX token (or string of
552
- * tokens) should be parsed into a MathJSON expression.
553
- *
554
- * For example an entry can define that the `\pi` LaTeX token should map to the
555
- * symbol `"Pi"`, or that the token `-` should map to the function
556
- * `["Negate",...]` when in a prefix position and to the function
557
- * `["Subtract", ...]` when in an infix position.
558
- *
559
- * Furthermore, the information in each dictionary entry is used to serialize
560
- * the LaTeX string corresponding to a MathJSON expression.
561
- *
562
- * Use the value returned by this function to the `options` argument of the
563
- * constructor.
564
- */
565
- static getDictionary(domain?: LibraryCategory | 'all'): Readonly<LatexDictionary>;
566
- /** Parse a */
567
- parse(latex: LatexString): Expression;
568
- serialize(expr: Expression): LatexString;
569
- }
570
- /**
571
- *
572
- * An instance of `Serializer` is provided to the `serialize` handlers of custom
573
- * LaTeX dictionary entries.
574
516
  *
575
- * @category Latex Parsing and Serialization
576
- *
577
- */
578
- export interface Serializer {
579
- readonly onError: WarningSignalHandler;
580
- readonly options: Required<SerializeLatexOptions>;
581
- /** "depth" of the expression:
582
- * - 0 for the root
583
- * - 1 for the arguments of the root
584
- * - 2 for the arguments of the arguments of the root
585
- * - etc...
586
- *
587
- * This allows for variation of the LaTeX serialized based
588
- * on the depth of the expression, for example using `\Bigl(`
589
- * for the top level, and `\bigl(` or `(` for others.
590
- */
591
- level: number;
592
- /** If true, apply transformations to the expression so the output
593
- * doesn't necesarily match the raw MathJSON, but is more visually pleasing
594
- * and easier to read. If false, output the raw MathJSON. */
595
- canonical?: boolean;
596
- /** Output a LaTeX string representing the expression */
597
- serialize: (expr: Expression | null) => string;
598
- /** Output `s` surrounded by delimiters.
599
- *
600
- * If `delimiters` is not specified, use `()`
601
- *
602
- */
603
- wrapString(s: string, style: DelimiterScale, delimiters?: string): string;
604
- /** A string with the arguments of expr fenced appropriately and separated by
605
- * commas.
606
- */
607
- wrapArguments(expr: Expression): string;
608
- /** Add a group fence around the expression if it is
609
- * an operator of precedence less than or equal to `prec`.
610
- */
611
- wrap: (expr: Expression | null, prec?: number) => string;
612
- /** Add a group fence around the expression if it is
613
- * short (not a function)
614
- */
615
- wrapShort(expr: Expression | null): string;
616
- /** Styles */
617
- applyFunctionStyle: (expr: Expression, level: number) => DelimiterScale;
618
- groupStyle: (expr: Expression, level: number) => DelimiterScale;
619
- rootStyle: (expr: Expression, level: number) => 'radical' | 'quotient' | 'solidus';
620
- fractionStyle: (expr: Expression, level: number) => 'quotient' | 'block-quotient' | 'inline-quotient' | 'inline-solidus' | 'nice-solidus' | 'reciprocal' | 'factor';
621
- logicStyle: (expr: Expression, level: number) => 'word' | 'boolean' | 'uppercase-word' | 'punctuation';
622
- powerStyle: (expr: Expression, level: number) => 'root' | 'solidus' | 'quotient';
623
- numericSetStyle: (expr: Expression, level: number) => 'compact' | 'regular' | 'interval' | 'set-builder';
624
- serializeFunction(expr: Expression): LatexString;
625
- serializeSymbol(expr: Expression): LatexString;
626
- }
627
- /** The `serialize` handler of a custom LaTeX dictionary entry can be
628
- * a function of this type.
517
+ * The expected format of numbers in the LaTeX string can be specified with
518
+ * the `ce.parse()` method.
629
519
  *
630
520
  * @category Latex Parsing and Serialization
631
- *
632
521
  */
633
- export type SerializeHandler = (serializer: Serializer, expr: Expression) => string;
634
522
  /**
635
523
  * An instance of `Parser` is provided to the `parse` handlers of custom
636
524
  * LaTeX dictionary entries.
@@ -639,7 +527,10 @@ export type SerializeHandler = (serializer: Serializer, expr: Expression) => str
639
527
  */
640
528
  export interface Parser {
641
529
  readonly options: Required<ParseLatexOptions>;
642
- readonly computeEngine?: IComputeEngine;
530
+ getIdentifierType(id: string): SymbolType;
531
+ pushSymbolTable(): void;
532
+ popSymbolTable(): void;
533
+ addSymbol(id: string, type: SymbolType): void;
643
534
  /** The index of the current token */
644
535
  index: number;
645
536
  /** True if the last token has been reached.
@@ -655,7 +546,10 @@ export interface Parser {
655
546
  /** Return the next token and advance the index */
656
547
  nextToken(): LatexToken;
657
548
  /** Return a string representation of the expression
658
- between `start` and `end` (default: the whole expression) */
549
+ * between `start` and `end` (default: the whole expression)
550
+ *
551
+ *
552
+ */
659
553
  latex(start: number, end?: number): string;
660
554
  /** Return an error expression with the specified code and arguments */
661
555
  error(code: string | [string, ...Expression[]], fromToken: number): Expression;
@@ -665,11 +559,14 @@ export interface Parser {
665
559
  includes space tokens, empty groups `{}`, and commands such as `\,` and `\!` */
666
560
  skipVisualSpace(): void;
667
561
  /** If the next token matches the target advance and return true. Otherwise
668
- * return false */
562
+ * return false
563
+ */
669
564
  match(token: LatexToken): boolean;
670
- /** Return true if the next tokens match the argument, an array of tokens, or null otherwise */
565
+ /** Return true if the next tokens match the argument, an array of tokens, or null otherwise
566
+ */
671
567
  matchAll(tokens: LatexToken[]): boolean;
672
- /** Return the next token if it matches any of the token in the argument or null otherwise */
568
+ /** Return the next token if it matches any of the token in the argument or null otherwise
569
+ */
673
570
  matchAny(tokens: LatexToken[]): LatexToken;
674
571
  /** If the next token is a character, return it and advance the index
675
572
  * This includes plain characters (e.g. 'a', '+'...), characters
@@ -806,3 +703,118 @@ export interface Parser {
806
703
  matchBoundary(): boolean;
807
704
  boundaryError(msg: string | [string, ...Expression[]]): Expression;
808
705
  }
706
+ /**
707
+ *
708
+ * The LaTeX serialization options can used with the `expr.toLatex()` method.
709
+ *
710
+ * @category Latex Parsing and Serialization
711
+ */
712
+ export type SerializeLatexOptions = NumberSerializationFormat & {
713
+ /**
714
+ * If true, prettify the LaTeX output.
715
+ *
716
+ * For example, render `\frac{a}{b}\frac{c}{d}` as `\frac{ac}{bd}`
717
+ *
718
+ */
719
+ prettify: boolean;
720
+ /**
721
+ * LaTeX string used to render an invisible multiply, e.g. in '2x'.
722
+ *
723
+ * If empty, both operands are concatenated, i.e. `2x`.
724
+ *
725
+ * Use `\cdot` to insert a `\cdot` operator between them, i.e. `2 \cdot x`.
726
+ *
727
+ * Empty by default.
728
+ */
729
+ invisibleMultiply: LatexString;
730
+ /**
731
+ * LaTeX string used to render mixed numbers e.g. in '1 3/4'.
732
+ *
733
+ * Leave it empty to join the main number and the fraction, i.e. render it
734
+ * as `1\frac{3}{4}`.
735
+ *
736
+ * Use `+` to insert an explicit `+` operator between them,
737
+ * i.e. `1+\frac{3}{4}`
738
+ *
739
+ * Empty by default.
740
+ */
741
+ invisiblePlus: LatexString;
742
+ /**
743
+ * LaTeX string used to render an explicit multiply operator.
744
+ *
745
+ * Default: `\times`
746
+ */
747
+ multiply: LatexString;
748
+ /**
749
+ * Serialize the expression `["Error", "'missing'"]`, with this LaTeX string
750
+ *
751
+ */
752
+ missingSymbol: LatexString;
753
+ applyFunctionStyle: (expr: Expression, level: number) => DelimiterScale;
754
+ groupStyle: (expr: Expression, level: number) => DelimiterScale;
755
+ rootStyle: (expr: Expression, level: number) => 'radical' | 'quotient' | 'solidus';
756
+ fractionStyle: (expr: Expression, level: number) => 'quotient' | 'block-quotient' | 'inline-quotient' | 'inline-solidus' | 'nice-solidus' | 'reciprocal' | 'factor';
757
+ logicStyle: (expr: Expression, level: number) => 'word' | 'boolean' | 'uppercase-word' | 'punctuation';
758
+ powerStyle: (expr: Expression, level: number) => 'root' | 'solidus' | 'quotient';
759
+ numericSetStyle: (expr: Expression, level: number) => 'compact' | 'regular' | 'interval' | 'set-builder';
760
+ };
761
+ /**
762
+ *
763
+ * An instance of `Serializer` is provided to the `serialize` handlers of custom
764
+ * LaTeX dictionary entries.
765
+ *
766
+ * @category Latex Parsing and Serialization
767
+ *
768
+ */
769
+ export interface Serializer {
770
+ readonly options: Required<SerializeLatexOptions>;
771
+ /** "depth" of the expression:
772
+ * - 0 for the root
773
+ * - 1 for the arguments of the root
774
+ * - 2 for the arguments of the arguments of the root
775
+ * - etc...
776
+ *
777
+ * This allows the serialized LaTeX to vary depending on the depth of the
778
+ * expression.
779
+ *
780
+ * For example use `\Bigl(` for the top level, and `\bigl(` or `(` for others.
781
+ */
782
+ level: number;
783
+ /** Output a LaTeX string representing the expression */
784
+ serialize: (expr: Expression | null) => string;
785
+ serializeFunction(expr: Expression): LatexString;
786
+ serializeSymbol(expr: Expression): LatexString;
787
+ /** Output `s` surrounded by delimiters.
788
+ *
789
+ * If `delimiters` is not specified, use `()`
790
+ *
791
+ */
792
+ wrapString(s: LatexString, style: DelimiterScale, delimiters?: string): LatexString;
793
+ /** A string with the arguments of expr fenced appropriately and separated by
794
+ * commas.
795
+ */
796
+ wrapArguments(expr: Expression): LatexString;
797
+ /** Add a group fence around the expression if it is
798
+ * an operator of precedence less than or equal to `prec`.
799
+ */
800
+ wrap: (expr: Expression | null, prec?: number) => LatexString;
801
+ /** Add a group fence around the expression if it is
802
+ * short (not a function)
803
+ */
804
+ wrapShort(expr: Expression | null): LatexString;
805
+ /** Styles */
806
+ applyFunctionStyle: (expr: Expression, level: number) => DelimiterScale;
807
+ groupStyle: (expr: Expression, level: number) => DelimiterScale;
808
+ rootStyle: (expr: Expression, level: number) => 'radical' | 'quotient' | 'solidus';
809
+ fractionStyle: (expr: Expression, level: number) => 'quotient' | 'block-quotient' | 'inline-quotient' | 'inline-solidus' | 'nice-solidus' | 'reciprocal' | 'factor';
810
+ logicStyle: (expr: Expression, level: number) => 'word' | 'boolean' | 'uppercase-word' | 'punctuation';
811
+ powerStyle: (expr: Expression, level: number) => 'root' | 'solidus' | 'quotient';
812
+ numericSetStyle: (expr: Expression, level: number) => 'compact' | 'regular' | 'interval' | 'set-builder';
813
+ }
814
+ /** The `serialize` handler of a custom LaTeX dictionary entry can be
815
+ * a function of this type.
816
+ *
817
+ * @category Latex Parsing and Serialization
818
+ *
819
+ */
820
+ export type SerializeHandler = (serializer: Serializer, expr: Expression) => string;