desmost 0.7.2 → 0.8.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 (132) hide show
  1. package/dist/compiler/compile.d.ts.map +1 -1
  2. package/dist/compiler/compile.js.map +1 -1
  3. package/dist/compiler/evaluate.d.ts.map +1 -1
  4. package/dist/compiler/evaluate.js +2 -14
  5. package/dist/compiler/evaluate.js.map +1 -1
  6. package/dist/compiler/format.d.ts +2 -0
  7. package/dist/compiler/format.d.ts.map +1 -1
  8. package/dist/compiler/format.js +11 -0
  9. package/dist/compiler/format.js.map +1 -1
  10. package/dist/compiler/options.d.ts.map +1 -1
  11. package/dist/compiler/options.js.map +1 -1
  12. package/dist/decompiler/decompile.d.ts +9 -0
  13. package/dist/decompiler/decompile.d.ts.map +1 -0
  14. package/dist/decompiler/decompile.js +21 -0
  15. package/dist/decompiler/decompile.js.map +1 -0
  16. package/dist/decompiler/emit.d.ts +4 -0
  17. package/dist/decompiler/emit.d.ts.map +1 -0
  18. package/dist/decompiler/emit.js +28 -0
  19. package/dist/decompiler/emit.js.map +1 -0
  20. package/dist/decompiler/extract.d.ts +5 -0
  21. package/dist/decompiler/extract.d.ts.map +1 -0
  22. package/dist/decompiler/extract.js +53 -0
  23. package/dist/decompiler/extract.js.map +1 -0
  24. package/dist/decompiler/format.d.ts +3 -0
  25. package/dist/decompiler/format.d.ts.map +1 -0
  26. package/dist/decompiler/format.js +11 -0
  27. package/dist/decompiler/format.js.map +1 -0
  28. package/dist/decompiler/index.d.ts +2 -0
  29. package/dist/decompiler/index.d.ts.map +1 -0
  30. package/dist/decompiler/index.js +2 -0
  31. package/dist/decompiler/index.js.map +1 -0
  32. package/dist/desmos.d.ts.map +1 -1
  33. package/dist/desmos.js.map +1 -1
  34. package/dist/errors.d.ts.map +1 -1
  35. package/dist/errors.js.map +1 -1
  36. package/dist/index.d.ts +1 -0
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.internal.d.ts +1 -0
  39. package/dist/index.internal.d.ts.map +1 -1
  40. package/dist/index.internal.js +1 -0
  41. package/dist/index.internal.js.map +1 -1
  42. package/dist/index.js +1 -0
  43. package/dist/index.js.map +1 -1
  44. package/dist/magic/expr/index.js.map +1 -1
  45. package/dist/magic/expr/latex.d.ts.map +1 -1
  46. package/dist/magic/expr/latex.js.map +1 -1
  47. package/dist/magic/expr/text.d.ts.map +1 -1
  48. package/dist/magic/expr/text.js.map +1 -1
  49. package/dist/magic/global/dark-mode.d.ts.map +1 -1
  50. package/dist/magic/global/dark-mode.js.map +1 -1
  51. package/dist/magic/global/desmos.d.ts.map +1 -1
  52. package/dist/magic/global/desmos.js.map +1 -1
  53. package/dist/magic/global/index.d.ts +3 -0
  54. package/dist/magic/global/index.d.ts.map +1 -1
  55. package/dist/magic/global/index.js +3 -0
  56. package/dist/magic/global/index.js.map +1 -1
  57. package/dist/magic/global/viewport.d.ts +4 -4
  58. package/dist/magic/global/viewport.d.ts.map +1 -1
  59. package/dist/magic/global/viewport.js +3 -1
  60. package/dist/magic/global/viewport.js.map +1 -1
  61. package/dist/magic/incantation.d.ts.map +1 -1
  62. package/dist/magic/incantation.js.map +1 -1
  63. package/dist/magic/local/anim.d.ts.map +1 -1
  64. package/dist/magic/local/anim.js.map +1 -1
  65. package/dist/magic/local/colour.d.ts.map +1 -1
  66. package/dist/magic/local/colour.js.map +1 -1
  67. package/dist/magic/local/dashed.d.ts.map +1 -1
  68. package/dist/magic/local/dashed.js.map +1 -1
  69. package/dist/magic/local/fill.d.ts.map +1 -1
  70. package/dist/magic/local/fill.js.map +1 -1
  71. package/dist/magic/local/hide.d.ts.map +1 -1
  72. package/dist/magic/local/hide.js.map +1 -1
  73. package/dist/magic/local/index.js.map +1 -1
  74. package/dist/magic/local/label.d.ts.map +1 -1
  75. package/dist/magic/local/label.js.map +1 -1
  76. package/dist/magic/local/line.d.ts.map +1 -1
  77. package/dist/magic/local/line.js.map +1 -1
  78. package/dist/magic/local/no-line.d.ts.map +1 -1
  79. package/dist/magic/local/no-line.js.map +1 -1
  80. package/dist/magic/local/point.d.ts.map +1 -1
  81. package/dist/magic/local/point.js.map +1 -1
  82. package/dist/magic/local/secret.d.ts.map +1 -1
  83. package/dist/magic/local/secret.js.map +1 -1
  84. package/dist/magic/local/slider.d.ts.map +1 -1
  85. package/dist/magic/local/slider.js.map +1 -1
  86. package/dist/parser/ast.d.ts.map +1 -1
  87. package/dist/parser/ast.js.map +1 -1
  88. package/dist/parser/desmost-parser.d.ts.map +1 -1
  89. package/dist/parser/desmost-parser.js +1 -1
  90. package/dist/parser/desmost-parser.js.map +1 -1
  91. package/dist/parser/generic-parser.d.ts.map +1 -1
  92. package/dist/parser/generic-parser.js.map +1 -1
  93. package/dist/utils.d.ts.map +1 -1
  94. package/dist/utils.js.map +1 -1
  95. package/package.json +1 -1
  96. package/src/compiler/compile.ts +124 -124
  97. package/src/compiler/evaluate.ts +86 -120
  98. package/src/compiler/format.ts +39 -6
  99. package/src/compiler/options.ts +78 -75
  100. package/src/decompiler/decompile.ts +48 -0
  101. package/src/decompiler/emit.ts +47 -0
  102. package/src/decompiler/extract.ts +100 -0
  103. package/src/decompiler/format.ts +20 -0
  104. package/src/decompiler/index.ts +1 -0
  105. package/src/desmos.ts +6 -6
  106. package/src/errors.ts +12 -12
  107. package/src/index.internal.ts +2 -0
  108. package/src/index.ts +1 -0
  109. package/src/magic/expr/index.ts +2 -2
  110. package/src/magic/expr/latex.ts +16 -16
  111. package/src/magic/expr/text.ts +13 -13
  112. package/src/magic/global/dark-mode.ts +10 -10
  113. package/src/magic/global/desmos.ts +15 -15
  114. package/src/magic/global/index.ts +8 -3
  115. package/src/magic/global/viewport.ts +21 -14
  116. package/src/magic/incantation.ts +94 -94
  117. package/src/magic/local/anim.ts +10 -10
  118. package/src/magic/local/colour.ts +26 -26
  119. package/src/magic/local/dashed.ts +9 -9
  120. package/src/magic/local/fill.ts +12 -12
  121. package/src/magic/local/hide.ts +9 -9
  122. package/src/magic/local/index.ts +11 -11
  123. package/src/magic/local/label.ts +51 -51
  124. package/src/magic/local/line.ts +16 -16
  125. package/src/magic/local/no-line.ts +9 -9
  126. package/src/magic/local/point.ts +16 -16
  127. package/src/magic/local/secret.ts +9 -9
  128. package/src/magic/local/slider.ts +15 -15
  129. package/src/parser/ast.ts +45 -45
  130. package/src/parser/desmost-parser.ts +550 -547
  131. package/src/parser/generic-parser.ts +176 -176
  132. package/src/utils.ts +33 -33
@@ -3,7 +3,7 @@ import type { NoMatch, Unrecoverable } from "../errors";
3
3
 
4
4
 
5
5
  const IGNORED_CHARACTERS = new Set([
6
- "\r",
6
+ "\r",
7
7
  ]);
8
8
 
9
9
 
@@ -14,179 +14,179 @@ const IGNORED_CHARACTERS = new Set([
14
14
  */
15
15
  export class GenericParser
16
16
  {
17
- /** The source code this parser is parsing. */
18
- protected source: string;
19
-
20
- /** The current position in the source code the parser is pointing to. */
21
- protected i: number = 0;
22
-
23
- /** The number of characters in the source code. */
24
- protected length: number;
25
-
26
-
27
- /** Create a parser for parsing `source`. */
28
- constructor(source: string)
29
- {
30
- this.source = source;
31
- this.length = source.length;
32
- }
33
-
34
-
35
- /**
36
- * Is the parser currently pointing at an invalid character?
37
- */
38
- protected out_of_bounds(): boolean
39
- {
40
- return this.i >= this.length;
41
- }
42
-
43
- /**
44
- * Get the character the parser is currently pointing at, or `undefined` if the parser is out-of-bounds.
45
- */
46
- protected get current(): string | undefined
47
- {
48
- return this.source.at(this.i);
49
- }
50
-
51
- /**
52
- * Get the character *after* the current one that the parser points at.
53
- */
54
- protected next(): string | undefined
55
- {
56
- return this.source.at(this.i + 1);
57
- }
58
-
59
- /**
60
- * Peek a snippet of the upcoming source text.
61
- *
62
- * Optionally start from the given `idx`.
63
- */
64
- public preview(idx?: number): string
65
- {
66
- idx ??= this.i;
67
- return this.source.slice(idx, idx + 20) + "...";
68
- }
69
-
70
- /**
71
- * Advance to the next character, skipping ignored characters.
72
- *
73
- * Errors if the parser is the out-of-bounds *before* advancing.
74
- */
75
- protected advance(error_msg?: string): Unrecoverable<void>
76
- {
77
- if (this.out_of_bounds()) {
78
- throw new UnrecoverableError.UnexpectedEnd(error_msg ?? "Unexpected end of input");
79
- }
80
-
81
- this.#advance();
82
- }
83
-
84
- /**
85
- * Attempt to advance to the next character, skipping ignored characters.
86
- *
87
- * Fails if the parser is the out-of-bounds *before* advancing.
88
- */
89
- protected try_advance(): void | NoMatch
90
- {
91
- if (this.out_of_bounds()) return NO_MATCH;
92
- this.#advance();
93
- }
94
-
95
- #advance(): void
96
- {
97
- this.i++;
98
-
99
- // @ts-expect-error: `this.current == undefined` is a true negative
100
- if (IGNORED_CHARACTERS.has(this.current)) {
101
- this.advance();
102
- }
103
- }
104
-
105
- /**
106
- * Consume `raw`, erroring if `raw` was not found.
107
- */
108
- protected consume(raw: string, error_msg?: string): Unrecoverable<void>
109
- {
110
- if (this.out_of_bounds()) {
111
- throw new UnrecoverableError.UnexpectedEnd(
112
- `Unexpected end of input while trying to consume: \`${raw}\``
113
- );
114
- }
115
-
116
- let init = this.i;
117
- let ii = 0;
118
-
119
- while (this.current === raw[ii]) {
120
- this.advance(
121
- `Unexpected end of input while trying to consume: \`${raw}\`, at: \`${this.preview(init)}\``
122
- );
123
-
124
- ii++;
125
- if (ii === raw.length) return;
126
- }
127
-
128
- throw new UnrecoverableError.UnexpectedInput(
129
- error_msg ?? `Expected: \`${raw}\`, but found: \`${this.preview(init)}\``
130
- );
131
- }
132
-
133
- /**
134
- * Attempt to consume `raw`, backtracking if `raw` was not found.
135
- */
136
- protected try_consume(raw: string): void | NoMatch
137
- {
138
- let init = this.i;
139
- let ii = 0;
140
-
141
- while (this.current === raw[ii]) {
142
- let r = this.try_advance();
143
- if (r === NO_MATCH) return NO_MATCH;
144
-
145
- ii++;
146
- if (ii === raw.length) return;
147
- if (this.i === this.length) break;
148
- }
149
-
150
- this.i = init;
151
- return NO_MATCH;
152
- }
153
-
154
- /**
155
- * Consume 0 or more space characters.
156
- */
157
- protected consume_spaces(): void
158
- {
159
- /* NOTE: Callers should be able to assume this is safe to call even when at end of input, since it should just match 0 characters */
160
- if (this.out_of_bounds()) return;
161
-
162
- while (this.current === " ") {
163
- this.advance();
164
- }
165
- }
166
-
167
- /**
168
- * Consume 0 or more whitespace characters.
169
- *
170
- * Same as `.consume_spaces()`, but allows newlines.
171
- */
172
- protected consume_whitespace(): void
173
- {
174
- /* NOTE: Callers should be able to assume this is safe to call even when at end of input, since it should just match 0 characters */
175
- if (this.i >= this.length) return;
176
-
177
- while (this.current === " " || this.current === "\n") {
178
- this.advance();
179
- }
180
- }
181
-
182
- protected consume_end_of_block(error_msg?: string): Unrecoverable<void>
183
- {
184
- if (this.i >= this.length) return;
185
-
186
- this.consume_spaces();
187
-
188
- this.consume("\n",
189
- error_msg ?? `Expected end of block, but found: \`${this.preview()}\``
190
- );
191
- }
17
+ /** The source code this parser is parsing. */
18
+ protected source: string;
19
+
20
+ /** The current position in the source code the parser is pointing to. */
21
+ protected i: number = 0;
22
+
23
+ /** The number of characters in the source code. */
24
+ protected length: number;
25
+
26
+
27
+ /** Create a parser for parsing `source`. */
28
+ constructor(source: string)
29
+ {
30
+ this.source = source;
31
+ this.length = source.length;
32
+ }
33
+
34
+
35
+ /**
36
+ * Is the parser currently pointing at an invalid character?
37
+ */
38
+ protected out_of_bounds(): boolean
39
+ {
40
+ return this.i >= this.length;
41
+ }
42
+
43
+ /**
44
+ * Get the character the parser is currently pointing at, or `undefined` if the parser is out-of-bounds.
45
+ */
46
+ protected get current(): string | undefined
47
+ {
48
+ return this.source.at(this.i);
49
+ }
50
+
51
+ /**
52
+ * Get the character *after* the current one that the parser points at.
53
+ */
54
+ protected next(): string | undefined
55
+ {
56
+ return this.source.at(this.i + 1);
57
+ }
58
+
59
+ /**
60
+ * Peek a snippet of the upcoming source text.
61
+ *
62
+ * Optionally start from the given `idx`.
63
+ */
64
+ public preview(idx?: number): string
65
+ {
66
+ idx ??= this.i;
67
+ return this.source.slice(idx, idx + 20) + "...";
68
+ }
69
+
70
+ /**
71
+ * Advance to the next character, skipping ignored characters.
72
+ *
73
+ * Errors if the parser is the out-of-bounds *before* advancing.
74
+ */
75
+ protected advance(error_msg?: string): Unrecoverable<void>
76
+ {
77
+ if (this.out_of_bounds()) {
78
+ throw new UnrecoverableError.UnexpectedEnd(error_msg ?? "Unexpected end of input");
79
+ }
80
+
81
+ this.#advance();
82
+ }
83
+
84
+ /**
85
+ * Attempt to advance to the next character, skipping ignored characters.
86
+ *
87
+ * Fails if the parser is the out-of-bounds *before* advancing.
88
+ */
89
+ protected try_advance(): void | NoMatch
90
+ {
91
+ if (this.out_of_bounds()) return NO_MATCH;
92
+ this.#advance();
93
+ }
94
+
95
+ #advance(): void
96
+ {
97
+ this.i++;
98
+
99
+ // @ts-expect-error: `this.current == undefined` is a true negative
100
+ if (IGNORED_CHARACTERS.has(this.current)) {
101
+ this.advance();
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Consume `raw`, erroring if `raw` was not found.
107
+ */
108
+ protected consume(raw: string, error_msg?: string): Unrecoverable<void>
109
+ {
110
+ if (this.out_of_bounds()) {
111
+ throw new UnrecoverableError.UnexpectedEnd(
112
+ `Unexpected end of input while trying to consume: \`${raw}\``
113
+ );
114
+ }
115
+
116
+ let init = this.i;
117
+ let ii = 0;
118
+
119
+ while (this.current === raw[ii]) {
120
+ this.advance(
121
+ `Unexpected end of input while trying to consume: \`${raw}\`, at: \`${this.preview(init)}\``
122
+ );
123
+
124
+ ii++;
125
+ if (ii === raw.length) return;
126
+ }
127
+
128
+ throw new UnrecoverableError.UnexpectedInput(
129
+ error_msg ?? `Expected: \`${raw}\`, but found: \`${this.preview(init)}\``
130
+ );
131
+ }
132
+
133
+ /**
134
+ * Attempt to consume `raw`, backtracking if `raw` was not found.
135
+ */
136
+ protected try_consume(raw: string): void | NoMatch
137
+ {
138
+ let init = this.i;
139
+ let ii = 0;
140
+
141
+ while (this.current === raw[ii]) {
142
+ let r = this.try_advance();
143
+ if (r === NO_MATCH) return NO_MATCH;
144
+
145
+ ii++;
146
+ if (ii === raw.length) return;
147
+ if (this.i === this.length) break;
148
+ }
149
+
150
+ this.i = init;
151
+ return NO_MATCH;
152
+ }
153
+
154
+ /**
155
+ * Consume 0 or more space characters.
156
+ */
157
+ protected consume_spaces(): void
158
+ {
159
+ /* NOTE: Callers should be able to assume this is safe to call even when at end of input, since it should just match 0 characters */
160
+ if (this.out_of_bounds()) return;
161
+
162
+ while (this.current === " ") {
163
+ this.advance();
164
+ }
165
+ }
166
+
167
+ /**
168
+ * Consume 0 or more whitespace characters.
169
+ *
170
+ * Same as `.consume_spaces()`, but allows newlines.
171
+ */
172
+ protected consume_whitespace(): void
173
+ {
174
+ /* NOTE: Callers should be able to assume this is safe to call even when at end of input, since it should just match 0 characters */
175
+ if (this.i >= this.length) return;
176
+
177
+ while (this.current === " " || this.current === "\n") {
178
+ this.advance();
179
+ }
180
+ }
181
+
182
+ protected consume_end_of_block(error_msg?: string): Unrecoverable<void>
183
+ {
184
+ if (this.i >= this.length) return;
185
+
186
+ this.consume_spaces();
187
+
188
+ this.consume("\n",
189
+ error_msg ?? `Expected end of block, but found: \`${this.preview()}\``
190
+ );
191
+ }
192
192
  }
package/src/utils.ts CHANGED
@@ -1,54 +1,54 @@
1
1
  export function* reversing<T>(items: ArrayLike<T>): Generator<T>
2
2
  {
3
- for (let i = items.length - 1; i >= 0; i--) {
4
- yield items[i];
5
- }
3
+ for (let i = items.length - 1; i >= 0; i--) {
4
+ yield items[i];
5
+ }
6
6
  }
7
7
 
8
8
  export function paired<T>(items: Iterable<T>): Array<[left: T, right: T]>
9
9
  {
10
- return pairing(items).toArray();
10
+ return pairing(items).toArray();
11
11
  }
12
12
 
13
13
  export function* pairing<T>(items: Iterable<T>): Generator<[left: T, right: T]>
14
14
  {
15
- let previous = null;
16
-
17
- for (let item of items) {
18
- if (previous === null) {
19
- previous = item;
20
- continue;
21
- }
22
- else {
23
- yield [previous, item];
24
- previous = null;
25
- }
26
- }
27
-
28
- if (previous !== null) {
29
- return previous;
30
- }
15
+ let previous = null;
16
+
17
+ for (let item of items) {
18
+ if (previous === null) {
19
+ previous = item;
20
+ continue;
21
+ }
22
+ else {
23
+ yield [previous, item];
24
+ previous = null;
25
+ }
26
+ }
27
+
28
+ if (previous !== null) {
29
+ return previous;
30
+ }
31
31
  }
32
32
 
33
33
 
34
34
  export function chars(s: string, start?: number, stop?: number): string[]
35
35
  {
36
- return new Range(start ?? 0, stop ?? s.length).map(i => s[i]).toArray();
36
+ return new Range(start ?? 0, stop ?? s.length).map(i => s[i]).toArray();
37
37
  }
38
38
 
39
39
 
40
40
  export class Range
41
41
  {
42
- constructor(
43
- protected start: number,
44
- protected stop: number,
45
- )
46
- {}
47
-
48
- *map<T>(callback: (n: number) => T): Generator<T>
49
- {
50
- for (let n = this.start; n < this.stop; n++) {
51
- yield callback(n);
52
- }
53
- }
42
+ constructor(
43
+ protected start: number,
44
+ protected stop: number,
45
+ )
46
+ {}
47
+
48
+ *map<T>(callback: (n: number) => T): Generator<T>
49
+ {
50
+ for (let n = this.start; n < this.stop; n++) {
51
+ yield callback(n);
52
+ }
53
+ }
54
54
  }