desmost 0.9.0 → 0.10.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 (104) hide show
  1. package/README.md +87 -11
  2. package/dist/compiler/compile.d.ts +1 -7
  3. package/dist/compiler/compile.d.ts.map +1 -1
  4. package/dist/compiler/compile.js +3 -94
  5. package/dist/compiler/compile.js.map +1 -1
  6. package/dist/compiler/compiler.d.ts +56 -0
  7. package/dist/compiler/compiler.d.ts.map +1 -0
  8. package/dist/compiler/compiler.js +209 -0
  9. package/dist/compiler/compiler.js.map +1 -0
  10. package/dist/compiler/format.d.ts +2 -2
  11. package/dist/compiler/format.d.ts.map +1 -1
  12. package/dist/compiler/format.js +18 -9
  13. package/dist/compiler/format.js.map +1 -1
  14. package/dist/decompiler/decompile.d.ts +1 -1
  15. package/dist/decompiler/decompile.d.ts.map +1 -1
  16. package/dist/decompiler/decompile.js +1 -3
  17. package/dist/decompiler/decompile.js.map +1 -1
  18. package/dist/decompiler/extract.d.ts.map +1 -1
  19. package/dist/decompiler/extract.js +7 -3
  20. package/dist/decompiler/extract.js.map +1 -1
  21. package/dist/decompiler/format.d.ts.map +1 -1
  22. package/dist/decompiler/format.js +0 -1
  23. package/dist/decompiler/format.js.map +1 -1
  24. package/dist/errors.d.ts +35 -27
  25. package/dist/errors.d.ts.map +1 -1
  26. package/dist/errors.js +46 -48
  27. package/dist/errors.js.map +1 -1
  28. package/dist/index.internal.d.ts +1 -1
  29. package/dist/index.internal.d.ts.map +1 -1
  30. package/dist/magic/global/desmos.d.ts +2 -2
  31. package/dist/magic/global/desmos.d.ts.map +1 -1
  32. package/dist/magic/global/desmos.js +3 -2
  33. package/dist/magic/global/desmos.js.map +1 -1
  34. package/dist/magic/global/viewport.d.ts +2 -2
  35. package/dist/magic/global/viewport.d.ts.map +1 -1
  36. package/dist/magic/global/viewport.js +5 -5
  37. package/dist/magic/global/viewport.js.map +1 -1
  38. package/dist/magic/incantation.d.ts +4 -4
  39. package/dist/magic/incantation.d.ts.map +1 -1
  40. package/dist/magic/incantation.js +15 -8
  41. package/dist/magic/incantation.js.map +1 -1
  42. package/dist/magic/local/colour.d.ts +3 -1
  43. package/dist/magic/local/colour.d.ts.map +1 -1
  44. package/dist/magic/local/colour.js +15 -11
  45. package/dist/magic/local/colour.js.map +1 -1
  46. package/dist/magic/local/label.d.ts +2 -2
  47. package/dist/magic/local/label.d.ts.map +1 -1
  48. package/dist/magic/local/label.js +8 -7
  49. package/dist/magic/local/label.js.map +1 -1
  50. package/dist/magic/local/line.d.ts +2 -2
  51. package/dist/magic/local/line.d.ts.map +1 -1
  52. package/dist/magic/local/line.js +0 -1
  53. package/dist/magic/local/line.js.map +1 -1
  54. package/dist/options.d.ts +59 -27
  55. package/dist/options.d.ts.map +1 -1
  56. package/dist/options.js +2 -1
  57. package/dist/options.js.map +1 -1
  58. package/dist/parser/ast.d.ts +3 -17
  59. package/dist/parser/ast.d.ts.map +1 -1
  60. package/dist/parser/ast.js +0 -2
  61. package/dist/parser/ast.js.map +1 -1
  62. package/dist/parser/desmost-parser.d.ts +33 -22
  63. package/dist/parser/desmost-parser.d.ts.map +1 -1
  64. package/dist/parser/desmost-parser.js +106 -92
  65. package/dist/parser/desmost-parser.js.map +1 -1
  66. package/dist/parser/generic-parser.d.ts +19 -10
  67. package/dist/parser/generic-parser.d.ts.map +1 -1
  68. package/dist/parser/generic-parser.js +56 -16
  69. package/dist/parser/generic-parser.js.map +1 -1
  70. package/dist/utils.d.ts +9 -2
  71. package/dist/utils.d.ts.map +1 -1
  72. package/dist/utils.js +13 -2
  73. package/dist/utils.js.map +1 -1
  74. package/package.json +4 -3
  75. package/src/compiler/compile.ts +7 -120
  76. package/src/compiler/compiler.ts +275 -0
  77. package/src/compiler/format.ts +19 -7
  78. package/src/decompiler/decompile.ts +1 -7
  79. package/src/decompiler/extract.ts +3 -1
  80. package/src/decompiler/format.ts +0 -1
  81. package/src/errors.ts +40 -24
  82. package/src/index.internal.ts +1 -1
  83. package/src/magic/.template.ts +2 -2
  84. package/src/magic/global/desmos.ts +7 -9
  85. package/src/magic/global/viewport.ts +11 -17
  86. package/src/magic/incantation.ts +21 -22
  87. package/src/magic/local/colour.ts +23 -13
  88. package/src/magic/local/label.ts +14 -19
  89. package/src/magic/local/line.ts +2 -2
  90. package/src/options.ts +83 -51
  91. package/src/parser/ast.ts +1 -21
  92. package/src/parser/desmost-parser.ts +131 -135
  93. package/src/parser/generic-parser.ts +71 -29
  94. package/src/utils.ts +25 -3
  95. package/dist/compiler/evaluate.d.ts +0 -18
  96. package/dist/compiler/evaluate.d.ts.map +0 -1
  97. package/dist/compiler/evaluate.js +0 -93
  98. package/dist/compiler/evaluate.js.map +0 -1
  99. package/dist/desmos.d.ts +0 -16
  100. package/dist/desmos.d.ts.map +0 -1
  101. package/dist/desmos.js +0 -19
  102. package/dist/desmos.js.map +0 -1
  103. package/src/compiler/evaluate.ts +0 -128
  104. package/src/desmos.ts +0 -22
@@ -1,93 +0,0 @@
1
- import dedent from "dedent";
2
- import { format_error, normalise_latex, prettify_latex } from "./format.js";
3
- import { Ast } from "../parser/index.js";
4
- import { UnrecoverableError } from "../errors.js";
5
- import { is_latex } from "../desmos.js";
6
- /**
7
- * Evaluate arguments (if any) to a global incantation `invocation`, then apply the incantation to `desmos`.
8
- */
9
- export function evaluate_global_incantation(invocation, desmos, options) {
10
- let data = undefined;
11
- if ("arg_raw" in invocation && invocation.arg_raw != undefined) {
12
- try {
13
- data = invocation.incantation.evaluate_arg(invocation.arg_raw, options);
14
- }
15
- catch (e) {
16
- let msg = evaluate_error(e, desmos, options);
17
- return msg;
18
- }
19
- }
20
- invocation.incantation.apply(desmos, data);
21
- }
22
- /**
23
- * Evaluate local incantation invocations on `expr`, then add `expr` to `desmos`.
24
- */
25
- export function evaluate_expr(expr, desmos, options) {
26
- let errors = [];
27
- for (let invocation of expr.incantations) {
28
- if (invocation.kind === Ast.Kind.INVALID_INCANTATION) {
29
- let msg = evaluate_error(invocation.error, desmos, { ...options, place_errors: "start" });
30
- if (msg != undefined) {
31
- errors.push(msg);
32
- }
33
- continue;
34
- }
35
- let data = undefined;
36
- if ("arg_raw" in invocation && invocation.arg_raw != undefined) {
37
- try {
38
- data = invocation.incantation.evaluate_arg(invocation.arg_raw, options);
39
- }
40
- catch (e) {
41
- errors.push(format_error(e, options));
42
- // if it needs the arg, then we can't press on
43
- if (invocation.incantation.requires_arg)
44
- continue;
45
- }
46
- }
47
- try {
48
- invocation.incantation.apply(expr.data, data);
49
- }
50
- catch (e) {
51
- errors.push(format_error(e, options));
52
- }
53
- }
54
- if (errors.length > 0) {
55
- desmos.setExpression({ type: "text", text: errors.join("\n\n") });
56
- }
57
- if (is_latex(expr.data)) {
58
- expr.data.latex = normalise_latex(expr.data.latex);
59
- if (options.prettify) {
60
- expr.data.latex = prettify_latex(expr.data.latex);
61
- }
62
- }
63
- else if (expr.data.type === "text" && options.dedent_text) {
64
- expr.data.text = dedent(expr.data.text ?? "");
65
- }
66
- desmos.setExpression(expr.data);
67
- }
68
- /**
69
- * Handle an `InvalidInvocation`, respecting the user's preferences in `options`.
70
- *
71
- * If the user set `place_errors: start` or `place_errors: end`, this returns the formatted error message for deferred aggregation.
72
- */
73
- export function evaluate_error(error, desmos, options) {
74
- switch (options.errors) {
75
- case "crash":
76
- throw error;
77
- case "suppress":
78
- console.error(error);
79
- break;
80
- default: switch (options.place_errors) {
81
- case "end":
82
- case "start":
83
- return format_error(error, options);
84
- default:
85
- desmos.setExpression({
86
- type: "text",
87
- text: format_error(error, options),
88
- });
89
- break;
90
- }
91
- }
92
- }
93
- //# sourceMappingURL=evaluate.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"evaluate.js","sourceRoot":"","sources":["../../src/compiler/evaluate.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAGzE,O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MAAM,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;QAC3D,IAAI,CAAC,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC;IAC/C,CAAC;IAED,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC;AAGD;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAC7B,KAAyB,EACzB,MAAyB,EACzB,OAAuB;IAGvB,QAAQ,OAAO,CAAC,MAAM,EAAE,CAAC;QACxB,KAAK,OAAO;YACX,MAAM,KAAK,CAAC;QAEb,KAAK,UAAU;YACd,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACrB,MAAM;QAEP,OAAO,CAAC,CAAC,QAAQ,OAAO,CAAC,YAAY,EAAE,CAAC;YACvC,KAAK,KAAK,CAAC;YACX,KAAK,OAAO;gBACX,OAAO,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;YAErC;gBACC,MAAM,CAAC,aAAa,CAAC;oBACpB,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,YAAY,CAAC,KAAK,EAAE,OAAO,CAAC;iBAClC,CAAC,CAAC;gBACH,MAAM;QACR,CAAC;IACF,CAAC;AACF,CAAC"}
package/dist/desmos.d.ts DELETED
@@ -1,16 +0,0 @@
1
- /**
2
- * The Desmos API (currently) is browser-only, so we polyfill the API objects we need here for the server-side.
3
- */
4
- export declare enum DesmosColour {
5
- RED = "#c74440",
6
- BLUE = "#2d70b3",
7
- GREEN = "#388c46",
8
- PURPLE = "#6042a6",
9
- ORANGE = "#fa7e19",
10
- BLACK = "#000000"
11
- }
12
- /**
13
- * Is `expr` a Desmos LaTeX expression (as opposed to a note or table)?
14
- */
15
- export declare function is_latex(expr: Desmos.ExpressionState): expr is Desmos.Expression;
16
- //# sourceMappingURL=desmos.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"desmos.d.ts","sourceRoot":"","sources":["../src/desmos.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,oBAAY,YAAY;IAEvB,GAAG,YAAe;IAClB,IAAI,YAAc;IAClB,KAAK,YAAa;IAClB,MAAM,YAAY;IAClB,MAAM,YAAY;IAClB,KAAK,YAAa;CAClB;AAGD;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,eAAe,GAAG,IAAI,IAAI,MAAM,CAAC,UAAU,CAGhF"}
package/dist/desmos.js DELETED
@@ -1,19 +0,0 @@
1
- /**
2
- * The Desmos API (currently) is browser-only, so we polyfill the API objects we need here for the server-side.
3
- */
4
- export var DesmosColour;
5
- (function (DesmosColour) {
6
- DesmosColour["RED"] = "#c74440";
7
- DesmosColour["BLUE"] = "#2d70b3";
8
- DesmosColour["GREEN"] = "#388c46";
9
- DesmosColour["PURPLE"] = "#6042a6";
10
- DesmosColour["ORANGE"] = "#fa7e19";
11
- DesmosColour["BLACK"] = "#000000";
12
- })(DesmosColour || (DesmosColour = {}));
13
- /**
14
- * Is `expr` a Desmos LaTeX expression (as opposed to a note or table)?
15
- */
16
- export function is_latex(expr) {
17
- return (expr.type === undefined || expr.type === "expression");
18
- }
19
- //# sourceMappingURL=desmos.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"desmos.js","sourceRoot":"","sources":["../src/desmos.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAN,IAAY,YAQX;AARD,WAAY,YAAY;IAEvB,+BAAkB,CAAA;IAClB,gCAAkB,CAAA;IAClB,iCAAkB,CAAA;IAClB,kCAAkB,CAAA;IAClB,kCAAkB,CAAA;IAClB,iCAAkB,CAAA;AACnB,CAAC,EARW,YAAY,KAAZ,YAAY,QAQvB;AAGD;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,IAA4B;IAEpD,OAAO,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC;AAChE,CAAC"}
@@ -1,128 +0,0 @@
1
- import dedent from "dedent";
2
-
3
- import { format_error, normalise_latex, prettify_latex } from "./format";
4
- import type { DesmostOptions } from "../options";
5
-
6
- import { Ast } from "../parser";
7
- import { UnrecoverableError, type Unrecoverable } from "../errors";
8
- import { is_latex } from "../desmos";
9
-
10
-
11
- /**
12
- * Evaluate arguments (if any) to a global incantation `invocation`, then apply the incantation to `desmos`.
13
- */
14
- export function evaluate_global_incantation(
15
- invocation: Ast.IncantationInvocation,
16
- desmos: Desmos.Calculator,
17
- options: DesmostOptions,
18
- ): Unrecoverable<void | string>
19
- {
20
- let data = undefined;
21
-
22
- if ("arg_raw" in invocation && invocation.arg_raw != undefined) {
23
- try {
24
- data = invocation.incantation.evaluate_arg(invocation.arg_raw, options);
25
- }
26
- catch (e) {
27
- let msg = evaluate_error(e as Error, desmos, options);
28
- return msg;
29
- }
30
- }
31
-
32
- invocation.incantation.apply(desmos, data);
33
- }
34
-
35
-
36
- /**
37
- * Evaluate local incantation invocations on `expr`, then add `expr` to `desmos`.
38
- */
39
- export function evaluate_expr(
40
- expr: Ast.Expression,
41
- desmos: Desmos.Calculator,
42
- options: DesmostOptions,
43
- ): Unrecoverable<void | string>
44
- {
45
- let errors: string[] = [];
46
-
47
- for (let invocation of expr.incantations) {
48
- if (invocation.kind === Ast.Kind.INVALID_INCANTATION) {
49
- let msg = evaluate_error(invocation.error, desmos, { ...options, place_errors: "start" });
50
- if (msg != undefined) {
51
- errors.push(msg);
52
- }
53
- continue;
54
- }
55
-
56
- let data = undefined;
57
-
58
- if ("arg_raw" in invocation && invocation.arg_raw != undefined) {
59
- try {
60
- data = invocation.incantation.evaluate_arg(invocation.arg_raw, options);
61
- }
62
- catch (e) {
63
- errors.push(format_error(e as Error, options));
64
- // if it needs the arg, then we can't press on
65
- if (invocation.incantation.requires_arg) continue;
66
- }
67
- }
68
-
69
- try {
70
- invocation.incantation.apply(expr.data, data);
71
- }
72
- catch (e) {
73
- errors.push(format_error(e as Error, options));
74
- }
75
- }
76
-
77
- if (errors.length > 0) {
78
- desmos.setExpression({ type: "text", text: errors.join("\n\n") });
79
- }
80
-
81
- if (is_latex(expr.data)) {
82
- expr.data.latex = normalise_latex(expr.data.latex!);
83
-
84
- if (options.prettify) {
85
- expr.data.latex = prettify_latex(expr.data.latex);
86
- }
87
- }
88
- else if (expr.data.type === "text" && options.dedent_text) {
89
- expr.data.text = dedent(expr.data.text ?? "");
90
- }
91
-
92
- desmos.setExpression(expr.data);
93
- }
94
-
95
-
96
- /**
97
- * Handle an `InvalidInvocation`, respecting the user's preferences in `options`.
98
- *
99
- * If the user set `place_errors: start` or `place_errors: end`, this returns the formatted error message for deferred aggregation.
100
- */
101
- export function evaluate_error(
102
- error: UnrecoverableError,
103
- desmos: Desmos.Calculator,
104
- options: DesmostOptions,
105
- ): Unrecoverable<void | string>
106
- {
107
- switch (options.errors) {
108
- case "crash":
109
- throw error;
110
-
111
- case "suppress":
112
- console.error(error);
113
- break;
114
-
115
- default: switch (options.place_errors) {
116
- case "end":
117
- case "start":
118
- return format_error(error, options);
119
-
120
- default:
121
- desmos.setExpression({
122
- type: "text",
123
- text: format_error(error, options),
124
- });
125
- break;
126
- }
127
- }
128
- }
package/src/desmos.ts DELETED
@@ -1,22 +0,0 @@
1
- /**
2
- * The Desmos API (currently) is browser-only, so we polyfill the API objects we need here for the server-side.
3
- */
4
-
5
- export enum DesmosColour
6
- {
7
- RED = "#c74440",
8
- BLUE = "#2d70b3",
9
- GREEN = "#388c46",
10
- PURPLE = "#6042a6",
11
- ORANGE = "#fa7e19",
12
- BLACK = "#000000",
13
- }
14
-
15
-
16
- /**
17
- * Is `expr` a Desmos LaTeX expression (as opposed to a note or table)?
18
- */
19
- export function is_latex(expr: Desmos.ExpressionState): expr is Desmos.Expression
20
- {
21
- return (expr.type === undefined || expr.type === "expression");
22
- }