@cortex-js/compute-engine 0.26.4 → 0.28.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 (145) hide show
  1. package/dist/compute-engine.esm.js +9785 -8316
  2. package/dist/compute-engine.js +9785 -8316
  3. package/dist/compute-engine.min.esm.js +57 -50
  4. package/dist/compute-engine.min.js +57 -50
  5. package/dist/math-json.esm.js +287 -7
  6. package/dist/math-json.js +287 -7
  7. package/dist/math-json.min.esm.js +287 -7
  8. package/dist/math-json.min.js +4 -2
  9. package/dist/types/common/ansi-codes.d.ts +1 -1
  10. package/dist/types/common/buffer.d.ts +1 -1
  11. package/dist/types/common/fuzzy-string-match.d.ts +2 -0
  12. package/dist/types/common/grapheme-splitter.d.ts +1 -1
  13. package/dist/types/common/interruptible.d.ts +20 -0
  14. package/dist/types/common/json5.d.ts +3 -0
  15. package/dist/types/common/one-of.d.ts +1 -1
  16. package/dist/types/common/signals.d.ts +1 -1
  17. package/dist/types/common/styled-text.d.ts +1 -1
  18. package/dist/types/common/syntax-highlighter.d.ts +1 -1
  19. package/dist/types/common/terminal.d.ts +1 -1
  20. package/dist/types/common/type/boxed-type.d.ts +13 -0
  21. package/dist/types/common/type/parse.d.ts +1 -1
  22. package/dist/types/common/type/primitive.d.ts +2 -1
  23. package/dist/types/common/type/serialize.d.ts +1 -1
  24. package/dist/types/common/type/subtype.d.ts +1 -1
  25. package/dist/types/common/type/types.d.ts +39 -49
  26. package/dist/types/common/type/utils.d.ts +3 -7
  27. package/dist/types/common/utils.d.ts +1 -1
  28. package/dist/types/compute-engine/assume.d.ts +1 -1
  29. package/dist/types/compute-engine/boxed-expression/abstract-boxed-expression.d.ts +16 -9
  30. package/dist/types/compute-engine/boxed-expression/apply.d.ts +1 -1
  31. package/dist/types/compute-engine/boxed-expression/arithmetic-add.d.ts +3 -2
  32. package/dist/types/compute-engine/boxed-expression/arithmetic-divide.d.ts +1 -1
  33. package/dist/types/compute-engine/boxed-expression/arithmetic-multiply.d.ts +1 -1
  34. package/dist/types/compute-engine/boxed-expression/arithmetic-power.d.ts +1 -1
  35. package/dist/types/compute-engine/boxed-expression/ascii-math.d.ts +1 -1
  36. package/dist/types/compute-engine/boxed-expression/box.d.ts +1 -1
  37. package/dist/types/compute-engine/boxed-expression/boxed-function-definition.d.ts +13 -6
  38. package/dist/types/compute-engine/boxed-expression/boxed-function.d.ts +12 -8
  39. package/dist/types/compute-engine/boxed-expression/boxed-number.d.ts +4 -4
  40. package/dist/types/compute-engine/boxed-expression/boxed-patterns.d.ts +1 -1
  41. package/dist/types/compute-engine/boxed-expression/boxed-string.d.ts +3 -3
  42. package/dist/types/compute-engine/boxed-expression/boxed-symbol-definition.d.ts +5 -4
  43. package/dist/types/compute-engine/boxed-expression/boxed-symbol.d.ts +7 -6
  44. package/dist/types/compute-engine/boxed-expression/boxed-tensor.d.ts +4 -4
  45. package/dist/types/compute-engine/boxed-expression/cache.d.ts +2 -1
  46. package/dist/types/compute-engine/boxed-expression/canonical.d.ts +1 -1
  47. package/dist/types/compute-engine/boxed-expression/compare.d.ts +3 -3
  48. package/dist/types/compute-engine/boxed-expression/expand.d.ts +1 -1
  49. package/dist/types/compute-engine/boxed-expression/expression-map.d.ts +1 -1
  50. package/dist/types/compute-engine/boxed-expression/factor.d.ts +1 -1
  51. package/dist/types/compute-engine/boxed-expression/flatten.d.ts +1 -1
  52. package/dist/types/compute-engine/boxed-expression/hold.d.ts +3 -2
  53. package/dist/types/compute-engine/boxed-expression/match.d.ts +1 -1
  54. package/dist/types/compute-engine/boxed-expression/negate.d.ts +1 -1
  55. package/dist/types/compute-engine/boxed-expression/numerics.d.ts +1 -1
  56. package/dist/types/compute-engine/boxed-expression/order.d.ts +1 -1
  57. package/dist/types/compute-engine/boxed-expression/polynomials.d.ts +1 -1
  58. package/dist/types/compute-engine/boxed-expression/product.d.ts +1 -1
  59. package/dist/types/compute-engine/boxed-expression/public.d.ts +109 -64
  60. package/dist/types/compute-engine/boxed-expression/rules.d.ts +1 -1
  61. package/dist/types/compute-engine/boxed-expression/serialize.d.ts +1 -1
  62. package/dist/types/compute-engine/boxed-expression/sgn.d.ts +1 -1
  63. package/dist/types/compute-engine/boxed-expression/simplify.d.ts +1 -1
  64. package/dist/types/compute-engine/boxed-expression/solve.d.ts +1 -1
  65. package/dist/types/compute-engine/boxed-expression/tensor-fields.d.ts +1 -1
  66. package/dist/types/compute-engine/boxed-expression/terms.d.ts +1 -1
  67. package/dist/types/compute-engine/boxed-expression/trigonometry.d.ts +7 -1
  68. package/dist/types/compute-engine/boxed-expression/utils.d.ts +1 -1
  69. package/dist/types/compute-engine/boxed-expression/validate.d.ts +3 -3
  70. package/dist/types/compute-engine/collection-utils.d.ts +3 -1
  71. package/dist/types/compute-engine/compile.d.ts +9 -8
  72. package/dist/types/compute-engine/compute-engine.d.ts +29 -16
  73. package/dist/types/compute-engine/cost-function.d.ts +1 -1
  74. package/dist/types/compute-engine/debug.d.ts +3 -0
  75. package/dist/types/compute-engine/function-utils.d.ts +1 -1
  76. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-algebra.d.ts +1 -1
  77. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-arithmetic.d.ts +1 -1
  78. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-calculus.d.ts +1 -1
  79. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-complex.d.ts +1 -1
  80. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-core.d.ts +1 -1
  81. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-linear-algebra.d.ts +1 -1
  82. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-logic.d.ts +1 -1
  83. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-other.d.ts +1 -1
  84. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-relational-operators.d.ts +1 -1
  85. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-sets.d.ts +1 -1
  86. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-statistics.d.ts +1 -1
  87. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-symbols.d.ts +1 -1
  88. package/dist/types/compute-engine/latex-syntax/dictionary/definitions-trigonometry.d.ts +1 -1
  89. package/dist/types/compute-engine/latex-syntax/dictionary/definitions.d.ts +1 -1
  90. package/dist/types/compute-engine/latex-syntax/parse-identifier.d.ts +1 -1
  91. package/dist/types/compute-engine/latex-syntax/parse.d.ts +1 -1
  92. package/dist/types/compute-engine/latex-syntax/public.d.ts +11 -6
  93. package/dist/types/compute-engine/latex-syntax/serialize-number.d.ts +1 -1
  94. package/dist/types/compute-engine/latex-syntax/serializer-style.d.ts +2 -1
  95. package/dist/types/compute-engine/latex-syntax/serializer.d.ts +3 -3
  96. package/dist/types/compute-engine/latex-syntax/tokenizer.d.ts +2 -2
  97. package/dist/types/compute-engine/library/arithmetic.d.ts +1 -1
  98. package/dist/types/compute-engine/library/calculus.d.ts +1 -1
  99. package/dist/types/compute-engine/library/collections.d.ts +3 -2
  100. package/dist/types/compute-engine/library/complex.d.ts +1 -1
  101. package/dist/types/compute-engine/library/control-structures.d.ts +1 -1
  102. package/dist/types/compute-engine/library/core.d.ts +1 -1
  103. package/dist/types/compute-engine/library/invisible-operator.d.ts +1 -1
  104. package/dist/types/compute-engine/library/library.d.ts +1 -1
  105. package/dist/types/compute-engine/library/linear-algebra.d.ts +1 -1
  106. package/dist/types/compute-engine/library/logic.d.ts +1 -1
  107. package/dist/types/compute-engine/library/polynomials.d.ts +1 -1
  108. package/dist/types/compute-engine/library/random-expression.d.ts +1 -1
  109. package/dist/types/compute-engine/library/relational-operator.d.ts +1 -1
  110. package/dist/types/compute-engine/library/sets.d.ts +1 -1
  111. package/dist/types/compute-engine/library/statistics.d.ts +1 -1
  112. package/dist/types/compute-engine/library/trigonometry.d.ts +1 -1
  113. package/dist/types/compute-engine/library/utils.d.ts +3 -5
  114. package/dist/types/compute-engine/numeric-value/big-numeric-value.d.ts +1 -1
  115. package/dist/types/compute-engine/numeric-value/exact-numeric-value.d.ts +1 -1
  116. package/dist/types/compute-engine/numeric-value/machine-numeric-value.d.ts +1 -1
  117. package/dist/types/compute-engine/numeric-value/public.d.ts +1 -1
  118. package/dist/types/compute-engine/numerics/bigint.d.ts +1 -1
  119. package/dist/types/compute-engine/numerics/bignum.d.ts +1 -1
  120. package/dist/types/compute-engine/numerics/expression.d.ts +1 -1
  121. package/dist/types/compute-engine/numerics/interval.d.ts +1 -1
  122. package/dist/types/compute-engine/numerics/monte-carlo.d.ts +1 -1
  123. package/dist/types/compute-engine/numerics/numeric-bigint.d.ts +9 -1
  124. package/dist/types/compute-engine/numerics/numeric-bignum.d.ts +1 -2
  125. package/dist/types/compute-engine/numerics/numeric-complex.d.ts +1 -1
  126. package/dist/types/compute-engine/numerics/numeric.d.ts +2 -3
  127. package/dist/types/compute-engine/numerics/primes.d.ts +1 -1
  128. package/dist/types/compute-engine/numerics/rationals.d.ts +1 -1
  129. package/dist/types/compute-engine/numerics/richardson.d.ts +1 -1
  130. package/dist/types/compute-engine/numerics/special-functions.d.ts +1 -1
  131. package/dist/types/compute-engine/numerics/statistics.d.ts +24 -0
  132. package/dist/types/compute-engine/numerics/strings.d.ts +1 -1
  133. package/dist/types/compute-engine/private.d.ts +29 -0
  134. package/dist/types/compute-engine/public.d.ts +1 -1
  135. package/dist/types/compute-engine/symbolic/derivative.d.ts +1 -1
  136. package/dist/types/compute-engine/symbolic/distribute.d.ts +1 -1
  137. package/dist/types/compute-engine/symbolic/simplify-rules.d.ts +1 -1
  138. package/dist/types/compute-engine/tensor/tensors.d.ts +2 -2
  139. package/dist/types/compute-engine.d.ts +3 -2
  140. package/dist/types/math-json/identifiers.d.ts +1 -1
  141. package/dist/types/math-json/types.d.ts +6 -6
  142. package/dist/types/math-json/utils.d.ts +3 -3
  143. package/dist/types/math-json.d.ts +3 -3
  144. package/package.json +3 -3
  145. package/dist/types/common/suggest.d.ts +0 -1
@@ -1,4 +1,275 @@
1
- /** Compute Engine 0.26.4 */
1
+ /** Compute Engine 0.28.0 */
2
+
3
+ // src/common/json5.ts
4
+ var JSON5 = class {
5
+ static parse(input) {
6
+ const parser = new JSON5Parser(input);
7
+ const value = parser.parseValue();
8
+ parser.skipWhitespace();
9
+ if (!parser.isAtEnd()) {
10
+ throw parser.error(
11
+ `Unexpected token '${parser.currentChar()}' after parsing complete value`
12
+ );
13
+ }
14
+ return value;
15
+ }
16
+ };
17
+ var JSON5Parser = class {
18
+ constructor(input) {
19
+ this.index = 0;
20
+ this.text = input;
21
+ }
22
+ parseValue() {
23
+ this.skipWhitespace();
24
+ if (this.isAtEnd()) {
25
+ throw this.error("Unexpected end of input");
26
+ }
27
+ const ch = this.currentChar();
28
+ if (ch === "{") return this.parseObject();
29
+ if (ch === "[") return this.parseArray();
30
+ if (ch === '"' || ch === "'") return this.parseString();
31
+ if (ch === "-" || ch === "+" || ch >= "0" && ch <= "9" || ch === ".")
32
+ return this.parseNumber();
33
+ return this.parseIdentifier();
34
+ }
35
+ parseObject() {
36
+ const obj = {};
37
+ this.expectChar("{");
38
+ this.skipWhitespace();
39
+ if (this.currentChar() === "}") {
40
+ this.index++;
41
+ return obj;
42
+ }
43
+ while (true) {
44
+ this.skipWhitespace();
45
+ let key;
46
+ const ch = this.currentChar();
47
+ if (ch === '"' || ch === "'") {
48
+ key = this.parseString();
49
+ } else {
50
+ key = this.parseIdentifier();
51
+ }
52
+ this.skipWhitespace();
53
+ this.expectChar(":");
54
+ this.skipWhitespace();
55
+ const value = this.parseValue();
56
+ obj[key] = value;
57
+ this.skipWhitespace();
58
+ if (this.currentChar() === ",") {
59
+ this.index++;
60
+ this.skipWhitespace();
61
+ if (this.currentChar() === "}") {
62
+ this.index++;
63
+ break;
64
+ }
65
+ } else if (this.currentChar() === "}") {
66
+ this.index++;
67
+ break;
68
+ } else {
69
+ throw this.error(
70
+ `Expected ',' or '}' in object but found '${this.currentChar()}'`
71
+ );
72
+ }
73
+ }
74
+ return obj;
75
+ }
76
+ parseArray() {
77
+ const arr = [];
78
+ this.expectChar("[");
79
+ this.skipWhitespace();
80
+ if (this.currentChar() === "]") {
81
+ this.index++;
82
+ return arr;
83
+ }
84
+ while (true) {
85
+ this.skipWhitespace();
86
+ arr.push(this.parseValue());
87
+ this.skipWhitespace();
88
+ if (this.currentChar() === ",") {
89
+ this.index++;
90
+ this.skipWhitespace();
91
+ if (this.currentChar() === "]") {
92
+ this.index++;
93
+ break;
94
+ }
95
+ } else if (this.currentChar() === "]") {
96
+ this.index++;
97
+ break;
98
+ } else {
99
+ throw this.error(
100
+ `Expected ',' or ']' in array but found '${this.currentChar()}'`
101
+ );
102
+ }
103
+ }
104
+ return arr;
105
+ }
106
+ parseString() {
107
+ const quote = this.currentChar();
108
+ if (quote !== '"' && quote !== "'") {
109
+ throw this.error(`String should start with a quote, got '${quote}'`);
110
+ }
111
+ this.index++;
112
+ let result = "";
113
+ while (!this.isAtEnd()) {
114
+ const ch = this.currentChar();
115
+ if (ch === quote) {
116
+ this.index++;
117
+ return result;
118
+ }
119
+ if (ch === "\\") {
120
+ this.index++;
121
+ if (this.isAtEnd()) {
122
+ throw this.error("Unterminated escape sequence in string");
123
+ }
124
+ const esc = this.currentChar();
125
+ switch (esc) {
126
+ case "b":
127
+ result += "\b";
128
+ break;
129
+ case "f":
130
+ result += "\f";
131
+ break;
132
+ case "n":
133
+ result += "\n";
134
+ break;
135
+ case "r":
136
+ result += "\r";
137
+ break;
138
+ case "t":
139
+ result += " ";
140
+ break;
141
+ case "v":
142
+ result += "\v";
143
+ break;
144
+ case "\\":
145
+ result += "\\";
146
+ break;
147
+ case "'":
148
+ result += "'";
149
+ break;
150
+ case '"':
151
+ result += '"';
152
+ break;
153
+ case "0":
154
+ result += "\0";
155
+ break;
156
+ case "u": {
157
+ this.index++;
158
+ const hex = this.text.substr(this.index, 4);
159
+ if (!/^[0-9a-fA-F]{4}$/.test(hex)) {
160
+ throw this.error(`Invalid Unicode escape sequence: \\u${hex}`);
161
+ }
162
+ result += String.fromCharCode(parseInt(hex, 16));
163
+ this.index += 3;
164
+ break;
165
+ }
166
+ default:
167
+ result += esc;
168
+ }
169
+ this.index++;
170
+ } else {
171
+ result += ch;
172
+ this.index++;
173
+ }
174
+ }
175
+ throw this.error("Unterminated string literal");
176
+ }
177
+ parseNumber() {
178
+ const start = this.index;
179
+ if (this.text.startsWith("-Infinity", this.index)) {
180
+ this.index += "-Infinity".length;
181
+ return -Infinity;
182
+ }
183
+ if (this.text.startsWith("+Infinity", this.index)) {
184
+ this.index += "+Infinity".length;
185
+ return Infinity;
186
+ }
187
+ if (this.text.startsWith("Infinity", this.index)) {
188
+ this.index += "Infinity".length;
189
+ return Infinity;
190
+ }
191
+ while (!this.isAtEnd() && /[0-9+\-_.eE]/.test(this.currentChar())) {
192
+ this.index++;
193
+ }
194
+ const token = this.text.slice(start, this.index);
195
+ const normalized = token.replace(/_/g, "");
196
+ const num = Number(normalized);
197
+ if (isNaN(num)) {
198
+ throw this.error(`Invalid number: ${token}`);
199
+ }
200
+ return num;
201
+ }
202
+ parseIdentifier() {
203
+ const start = this.index;
204
+ const firstChar = this.currentChar();
205
+ if (!/[a-zA-Z$_]/.test(firstChar)) {
206
+ throw this.error(`Unexpected token '${firstChar}'`);
207
+ }
208
+ this.index++;
209
+ while (!this.isAtEnd()) {
210
+ const ch = this.currentChar();
211
+ if (!/[a-zA-Z0-9$_]/.test(ch)) break;
212
+ this.index++;
213
+ }
214
+ const token = this.text.slice(start, this.index);
215
+ if (token === "true") return true;
216
+ if (token === "false") return false;
217
+ if (token === "null") return null;
218
+ if (token === "Infinity") return Infinity;
219
+ if (token === "NaN") return NaN;
220
+ return token;
221
+ }
222
+ skipWhitespace() {
223
+ while (!this.isAtEnd()) {
224
+ const ch = this.currentChar();
225
+ if (/\s/.test(ch)) {
226
+ this.index++;
227
+ continue;
228
+ }
229
+ if (ch === "/") {
230
+ const next = this.peekChar(1);
231
+ if (next === "/") {
232
+ this.index += 2;
233
+ while (!this.isAtEnd() && this.currentChar() !== "\n") {
234
+ this.index++;
235
+ }
236
+ continue;
237
+ } else if (next === "*") {
238
+ this.index += 2;
239
+ while (!this.isAtEnd() && !(this.currentChar() === "*" && this.peekChar(1) === "/")) {
240
+ this.index++;
241
+ }
242
+ if (this.isAtEnd()) {
243
+ throw this.error("Unterminated multi-line comment");
244
+ }
245
+ this.index += 2;
246
+ continue;
247
+ }
248
+ }
249
+ break;
250
+ }
251
+ }
252
+ expectChar(expected) {
253
+ if (this.currentChar() !== expected) {
254
+ throw this.error(
255
+ `Expected '${expected}' but found '${this.currentChar()}'`
256
+ );
257
+ }
258
+ this.index++;
259
+ }
260
+ currentChar() {
261
+ return this.text[this.index];
262
+ }
263
+ peekChar(offset) {
264
+ return this.text[this.index + offset];
265
+ }
266
+ isAtEnd() {
267
+ return this.index >= this.text.length;
268
+ }
269
+ error(message) {
270
+ return new Error(`${message} at position ${this.index}`);
271
+ }
272
+ };
2
273
 
3
274
  // src/math-json/utils.ts
4
275
  function isSymbolObject(expr) {
@@ -62,12 +333,21 @@ function keyValuePair(expr) {
62
333
  }
63
334
  return null;
64
335
  }
65
- function dictionary(expr) {
336
+ function dictionaryFromExpression(expr) {
66
337
  if (expr === null) return null;
338
+ if (typeof expr === "object" && !("sym" in expr) && !("num" in expr) && !("str" in expr) && !("fn" in expr)) {
339
+ return expr;
340
+ }
341
+ if (typeof expr === "string" && expr[0] === "{" && expr[expr.length - 1] === "}") {
342
+ try {
343
+ return JSON5.parse(expr);
344
+ } catch {
345
+ return null;
346
+ }
347
+ }
67
348
  const kv = keyValuePair(expr);
68
349
  if (kv) return { [kv[0]]: kv[1] };
69
- const h = operator(expr);
70
- if (h === "Dictionary") {
350
+ if (operator(expr) === "Dictionary") {
71
351
  const result = {};
72
352
  const ops = operands(expr);
73
353
  for (let i = 1; i < nops(expr); i++) {
@@ -93,16 +373,16 @@ function mapArgs(expr, fn) {
93
373
  }
94
374
 
95
375
  // src/math-json.ts
96
- var version = "0.26.4";
376
+ var version = "0.28.0";
97
377
  export {
98
- dictionary as getDictionary,
99
- stringValue as getStringValue,
378
+ dictionaryFromExpression,
100
379
  isFunctionObject,
101
380
  isStringObject,
102
381
  isSymbolObject,
103
382
  mapArgs,
104
383
  operand,
105
384
  operator,
385
+ stringValue,
106
386
  symbol,
107
387
  version
108
388
  };
package/dist/math-json.js CHANGED
@@ -1,4 +1,4 @@
1
- /** MathJSON 0.26.4 */
1
+ /** MathJSON 0.28.0 */
2
2
  (function(global,factory){typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'],factory):(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.MathJson = {}));})(this, (function (exports) { 'use strict';
3
3
  var MathJson = (() => {
4
4
  var __defProp = Object.defineProperty;
@@ -22,18 +22,289 @@ var MathJson = (() => {
22
22
  // src/math-json.ts
23
23
  var math_json_exports = {};
24
24
  __export(math_json_exports, {
25
- getDictionary: () => dictionary,
26
- getStringValue: () => stringValue,
25
+ dictionaryFromExpression: () => dictionaryFromExpression,
27
26
  isFunctionObject: () => isFunctionObject,
28
27
  isStringObject: () => isStringObject,
29
28
  isSymbolObject: () => isSymbolObject,
30
29
  mapArgs: () => mapArgs,
31
30
  operand: () => operand,
32
31
  operator: () => operator,
32
+ stringValue: () => stringValue,
33
33
  symbol: () => symbol,
34
34
  version: () => version
35
35
  });
36
36
 
37
+ // src/common/json5.ts
38
+ var JSON5 = class {
39
+ static parse(input) {
40
+ const parser = new JSON5Parser(input);
41
+ const value = parser.parseValue();
42
+ parser.skipWhitespace();
43
+ if (!parser.isAtEnd()) {
44
+ throw parser.error(
45
+ `Unexpected token '${parser.currentChar()}' after parsing complete value`
46
+ );
47
+ }
48
+ return value;
49
+ }
50
+ };
51
+ var JSON5Parser = class {
52
+ constructor(input) {
53
+ this.index = 0;
54
+ this.text = input;
55
+ }
56
+ parseValue() {
57
+ this.skipWhitespace();
58
+ if (this.isAtEnd()) {
59
+ throw this.error("Unexpected end of input");
60
+ }
61
+ const ch = this.currentChar();
62
+ if (ch === "{") return this.parseObject();
63
+ if (ch === "[") return this.parseArray();
64
+ if (ch === '"' || ch === "'") return this.parseString();
65
+ if (ch === "-" || ch === "+" || ch >= "0" && ch <= "9" || ch === ".")
66
+ return this.parseNumber();
67
+ return this.parseIdentifier();
68
+ }
69
+ parseObject() {
70
+ const obj = {};
71
+ this.expectChar("{");
72
+ this.skipWhitespace();
73
+ if (this.currentChar() === "}") {
74
+ this.index++;
75
+ return obj;
76
+ }
77
+ while (true) {
78
+ this.skipWhitespace();
79
+ let key;
80
+ const ch = this.currentChar();
81
+ if (ch === '"' || ch === "'") {
82
+ key = this.parseString();
83
+ } else {
84
+ key = this.parseIdentifier();
85
+ }
86
+ this.skipWhitespace();
87
+ this.expectChar(":");
88
+ this.skipWhitespace();
89
+ const value = this.parseValue();
90
+ obj[key] = value;
91
+ this.skipWhitespace();
92
+ if (this.currentChar() === ",") {
93
+ this.index++;
94
+ this.skipWhitespace();
95
+ if (this.currentChar() === "}") {
96
+ this.index++;
97
+ break;
98
+ }
99
+ } else if (this.currentChar() === "}") {
100
+ this.index++;
101
+ break;
102
+ } else {
103
+ throw this.error(
104
+ `Expected ',' or '}' in object but found '${this.currentChar()}'`
105
+ );
106
+ }
107
+ }
108
+ return obj;
109
+ }
110
+ parseArray() {
111
+ const arr = [];
112
+ this.expectChar("[");
113
+ this.skipWhitespace();
114
+ if (this.currentChar() === "]") {
115
+ this.index++;
116
+ return arr;
117
+ }
118
+ while (true) {
119
+ this.skipWhitespace();
120
+ arr.push(this.parseValue());
121
+ this.skipWhitespace();
122
+ if (this.currentChar() === ",") {
123
+ this.index++;
124
+ this.skipWhitespace();
125
+ if (this.currentChar() === "]") {
126
+ this.index++;
127
+ break;
128
+ }
129
+ } else if (this.currentChar() === "]") {
130
+ this.index++;
131
+ break;
132
+ } else {
133
+ throw this.error(
134
+ `Expected ',' or ']' in array but found '${this.currentChar()}'`
135
+ );
136
+ }
137
+ }
138
+ return arr;
139
+ }
140
+ parseString() {
141
+ const quote = this.currentChar();
142
+ if (quote !== '"' && quote !== "'") {
143
+ throw this.error(`String should start with a quote, got '${quote}'`);
144
+ }
145
+ this.index++;
146
+ let result = "";
147
+ while (!this.isAtEnd()) {
148
+ const ch = this.currentChar();
149
+ if (ch === quote) {
150
+ this.index++;
151
+ return result;
152
+ }
153
+ if (ch === "\\") {
154
+ this.index++;
155
+ if (this.isAtEnd()) {
156
+ throw this.error("Unterminated escape sequence in string");
157
+ }
158
+ const esc = this.currentChar();
159
+ switch (esc) {
160
+ case "b":
161
+ result += "\b";
162
+ break;
163
+ case "f":
164
+ result += "\f";
165
+ break;
166
+ case "n":
167
+ result += "\n";
168
+ break;
169
+ case "r":
170
+ result += "\r";
171
+ break;
172
+ case "t":
173
+ result += " ";
174
+ break;
175
+ case "v":
176
+ result += "\v";
177
+ break;
178
+ case "\\":
179
+ result += "\\";
180
+ break;
181
+ case "'":
182
+ result += "'";
183
+ break;
184
+ case '"':
185
+ result += '"';
186
+ break;
187
+ case "0":
188
+ result += "\0";
189
+ break;
190
+ case "u": {
191
+ this.index++;
192
+ const hex = this.text.substr(this.index, 4);
193
+ if (!/^[0-9a-fA-F]{4}$/.test(hex)) {
194
+ throw this.error(`Invalid Unicode escape sequence: \\u${hex}`);
195
+ }
196
+ result += String.fromCharCode(parseInt(hex, 16));
197
+ this.index += 3;
198
+ break;
199
+ }
200
+ default:
201
+ result += esc;
202
+ }
203
+ this.index++;
204
+ } else {
205
+ result += ch;
206
+ this.index++;
207
+ }
208
+ }
209
+ throw this.error("Unterminated string literal");
210
+ }
211
+ parseNumber() {
212
+ const start = this.index;
213
+ if (this.text.startsWith("-Infinity", this.index)) {
214
+ this.index += "-Infinity".length;
215
+ return -Infinity;
216
+ }
217
+ if (this.text.startsWith("+Infinity", this.index)) {
218
+ this.index += "+Infinity".length;
219
+ return Infinity;
220
+ }
221
+ if (this.text.startsWith("Infinity", this.index)) {
222
+ this.index += "Infinity".length;
223
+ return Infinity;
224
+ }
225
+ while (!this.isAtEnd() && /[0-9+\-_.eE]/.test(this.currentChar())) {
226
+ this.index++;
227
+ }
228
+ const token = this.text.slice(start, this.index);
229
+ const normalized = token.replace(/_/g, "");
230
+ const num = Number(normalized);
231
+ if (isNaN(num)) {
232
+ throw this.error(`Invalid number: ${token}`);
233
+ }
234
+ return num;
235
+ }
236
+ parseIdentifier() {
237
+ const start = this.index;
238
+ const firstChar = this.currentChar();
239
+ if (!/[a-zA-Z$_]/.test(firstChar)) {
240
+ throw this.error(`Unexpected token '${firstChar}'`);
241
+ }
242
+ this.index++;
243
+ while (!this.isAtEnd()) {
244
+ const ch = this.currentChar();
245
+ if (!/[a-zA-Z0-9$_]/.test(ch)) break;
246
+ this.index++;
247
+ }
248
+ const token = this.text.slice(start, this.index);
249
+ if (token === "true") return true;
250
+ if (token === "false") return false;
251
+ if (token === "null") return null;
252
+ if (token === "Infinity") return Infinity;
253
+ if (token === "NaN") return NaN;
254
+ return token;
255
+ }
256
+ skipWhitespace() {
257
+ while (!this.isAtEnd()) {
258
+ const ch = this.currentChar();
259
+ if (/\s/.test(ch)) {
260
+ this.index++;
261
+ continue;
262
+ }
263
+ if (ch === "/") {
264
+ const next = this.peekChar(1);
265
+ if (next === "/") {
266
+ this.index += 2;
267
+ while (!this.isAtEnd() && this.currentChar() !== "\n") {
268
+ this.index++;
269
+ }
270
+ continue;
271
+ } else if (next === "*") {
272
+ this.index += 2;
273
+ while (!this.isAtEnd() && !(this.currentChar() === "*" && this.peekChar(1) === "/")) {
274
+ this.index++;
275
+ }
276
+ if (this.isAtEnd()) {
277
+ throw this.error("Unterminated multi-line comment");
278
+ }
279
+ this.index += 2;
280
+ continue;
281
+ }
282
+ }
283
+ break;
284
+ }
285
+ }
286
+ expectChar(expected) {
287
+ if (this.currentChar() !== expected) {
288
+ throw this.error(
289
+ `Expected '${expected}' but found '${this.currentChar()}'`
290
+ );
291
+ }
292
+ this.index++;
293
+ }
294
+ currentChar() {
295
+ return this.text[this.index];
296
+ }
297
+ peekChar(offset) {
298
+ return this.text[this.index + offset];
299
+ }
300
+ isAtEnd() {
301
+ return this.index >= this.text.length;
302
+ }
303
+ error(message) {
304
+ return new Error(`${message} at position ${this.index}`);
305
+ }
306
+ };
307
+
37
308
  // src/math-json/utils.ts
38
309
  function isSymbolObject(expr) {
39
310
  return expr !== null && typeof expr === "object" && "sym" in expr;
@@ -96,12 +367,21 @@ var MathJson = (() => {
96
367
  }
97
368
  return null;
98
369
  }
99
- function dictionary(expr) {
370
+ function dictionaryFromExpression(expr) {
100
371
  if (expr === null) return null;
372
+ if (typeof expr === "object" && !("sym" in expr) && !("num" in expr) && !("str" in expr) && !("fn" in expr)) {
373
+ return expr;
374
+ }
375
+ if (typeof expr === "string" && expr[0] === "{" && expr[expr.length - 1] === "}") {
376
+ try {
377
+ return JSON5.parse(expr);
378
+ } catch {
379
+ return null;
380
+ }
381
+ }
101
382
  const kv = keyValuePair(expr);
102
383
  if (kv) return { [kv[0]]: kv[1] };
103
- const h = operator(expr);
104
- if (h === "Dictionary") {
384
+ if (operator(expr) === "Dictionary") {
105
385
  const result = {};
106
386
  const ops = operands(expr);
107
387
  for (let i = 1; i < nops(expr); i++) {
@@ -127,7 +407,7 @@ var MathJson = (() => {
127
407
  }
128
408
 
129
409
  // src/math-json.ts
130
- var version = "0.26.4";
410
+ var version = "0.28.0";
131
411
  return __toCommonJS(math_json_exports);
132
412
  })();
133
413
  Object.assign(exports, MathJson); Object.defineProperty(exports, '__esModule', { value: true });}));