desmost 0.1.2 → 0.3.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.
- package/README.md +23 -0
- package/dist/compiler/evaluate.js.map +1 -1
- package/dist/errors.d.ts +6 -7
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -3
- package/dist/errors.js.map +1 -1
- package/dist/magic/expr/latex.d.ts.map +1 -1
- package/dist/magic/expr/text.d.ts.map +1 -1
- package/dist/magic/global/dark-mode.d.ts.map +1 -1
- package/dist/magic/global/desmos.d.ts.map +1 -1
- package/dist/magic/global/viewport.d.ts.map +1 -1
- package/dist/magic/incantation.d.ts.map +1 -1
- package/dist/magic/incantation.js.map +1 -1
- package/dist/magic/local/anim.d.ts.map +1 -1
- package/dist/magic/local/colour.d.ts.map +1 -1
- package/dist/magic/local/colour.js.map +1 -1
- package/dist/magic/local/dashed.d.ts.map +1 -1
- package/dist/magic/local/fill.d.ts.map +1 -1
- package/dist/magic/local/hide.d.ts.map +1 -1
- package/dist/magic/local/line.d.ts.map +1 -1
- package/dist/magic/local/no-line.d.ts.map +1 -1
- package/dist/magic/local/point.d.ts.map +1 -1
- package/dist/magic/local/secret.d.ts.map +1 -1
- package/dist/magic/local/slider.d.ts.map +1 -1
- package/dist/parser/ast.js.map +1 -1
- package/dist/parser/desmost-parser.d.ts +4 -4
- package/dist/parser/desmost-parser.d.ts.map +1 -1
- package/dist/parser/desmost-parser.js +50 -66
- package/dist/parser/desmost-parser.js.map +1 -1
- package/dist/parser/generic-parser.d.ts +5 -4
- package/dist/parser/generic-parser.d.ts.map +1 -1
- package/dist/parser/generic-parser.js +20 -23
- package/dist/parser/generic-parser.js.map +1 -1
- package/package.json +9 -3
- package/src/compiler/evaluate.ts +47 -0
- package/src/compiler/options.ts +8 -0
- package/src/errors.ts +6 -6
- package/src/magic/local/index.ts +6 -4
- package/src/magic/local/label.ts +58 -0
- package/src/parser/desmost-parser.ts +66 -96
- package/src/parser/generic-parser.ts +33 -32
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
1
|
+
import { FAIL, UnrecoverableError } from "../errors";
|
|
2
|
+
import type { RecoverableFail, Unrecoverable } from "../errors";
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
const IGNORED_CHARACTERS = new Set([
|
|
@@ -58,10 +58,13 @@ export class GenericParser
|
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
60
|
* Peek a snippet of the upcoming source text (for error messages).
|
|
61
|
+
*
|
|
62
|
+
* Optionally start from the given `idx`.
|
|
61
63
|
*/
|
|
62
|
-
public preview(): string
|
|
64
|
+
public preview(idx?: number): string
|
|
63
65
|
{
|
|
64
|
-
|
|
66
|
+
idx ??= this.i;
|
|
67
|
+
return this.source.slice(idx, idx + 20) + "...";
|
|
65
68
|
}
|
|
66
69
|
|
|
67
70
|
/**
|
|
@@ -75,25 +78,28 @@ export class GenericParser
|
|
|
75
78
|
throw new UnrecoverableError.UnexpectedEnd(error_msg ?? "Unexpected end of input");
|
|
76
79
|
}
|
|
77
80
|
|
|
78
|
-
this
|
|
79
|
-
|
|
80
|
-
// @ts-expect-error: `this.current == undefined` is a true negative
|
|
81
|
-
if (IGNORED_CHARACTERS.has(this.current)) {
|
|
82
|
-
this.advance();
|
|
83
|
-
}
|
|
81
|
+
this.#advance();
|
|
84
82
|
}
|
|
85
83
|
|
|
86
84
|
/**
|
|
87
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
88
|
*/
|
|
89
|
-
protected try_advance():
|
|
89
|
+
protected try_advance(): void | RecoverableFail
|
|
90
|
+
{
|
|
91
|
+
if (this.out_of_bounds()) return FAIL;
|
|
92
|
+
this.#advance();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#advance(): void
|
|
90
96
|
{
|
|
91
|
-
|
|
97
|
+
this.i++;
|
|
98
|
+
|
|
99
|
+
// @ts-expect-error: `this.current == undefined` is a true negative
|
|
100
|
+
if (IGNORED_CHARACTERS.has(this.current)) {
|
|
92
101
|
this.advance();
|
|
93
102
|
}
|
|
94
|
-
catch {
|
|
95
|
-
throw new RecoverableFail();
|
|
96
|
-
}
|
|
97
103
|
}
|
|
98
104
|
|
|
99
105
|
/**
|
|
@@ -107,11 +113,12 @@ export class GenericParser
|
|
|
107
113
|
);
|
|
108
114
|
}
|
|
109
115
|
|
|
116
|
+
let init = this.i;
|
|
110
117
|
let ii = 0;
|
|
111
118
|
|
|
112
119
|
while (this.current === raw[ii]) {
|
|
113
120
|
this.advance(
|
|
114
|
-
`Unexpected end of input while trying to consume: \`${raw}\``
|
|
121
|
+
`Unexpected end of input while trying to consume: \`${raw}\`, at: \`${this.preview(init)}\``
|
|
115
122
|
);
|
|
116
123
|
|
|
117
124
|
ii++;
|
|
@@ -119,28 +126,29 @@ export class GenericParser
|
|
|
119
126
|
}
|
|
120
127
|
|
|
121
128
|
throw new UnrecoverableError.UnexpectedInput(
|
|
122
|
-
error_msg ?? `Expected: \`${raw}\`, but found: \`${this.preview()}\``
|
|
129
|
+
error_msg ?? `Expected: \`${raw}\`, but found: \`${this.preview(init)}\``
|
|
123
130
|
);
|
|
124
131
|
}
|
|
125
132
|
|
|
126
133
|
/**
|
|
127
|
-
* Attempt to consume `raw`, backtracking
|
|
134
|
+
* Attempt to consume `raw`, backtracking if `raw` was not found.
|
|
128
135
|
*/
|
|
129
|
-
protected try_consume(raw: string):
|
|
136
|
+
protected try_consume(raw: string): void | RecoverableFail
|
|
130
137
|
{
|
|
131
138
|
let init = this.i;
|
|
132
139
|
let ii = 0;
|
|
133
140
|
|
|
134
141
|
while (this.current === raw[ii]) {
|
|
135
|
-
this.try_advance();
|
|
136
|
-
|
|
142
|
+
let r = this.try_advance();
|
|
143
|
+
if (r === FAIL) return FAIL;
|
|
137
144
|
|
|
145
|
+
ii++;
|
|
138
146
|
if (ii === raw.length) return;
|
|
139
147
|
if (this.i === this.length) break;
|
|
140
148
|
}
|
|
141
149
|
|
|
142
150
|
this.i = init;
|
|
143
|
-
|
|
151
|
+
return FAIL;
|
|
144
152
|
}
|
|
145
153
|
|
|
146
154
|
/**
|
|
@@ -177,15 +185,8 @@ export class GenericParser
|
|
|
177
185
|
|
|
178
186
|
this.consume_spaces();
|
|
179
187
|
|
|
180
|
-
|
|
181
|
-
this.
|
|
182
|
-
|
|
183
|
-
catch (e) {
|
|
184
|
-
if (!(e instanceof RecoverableFail)) throw e;
|
|
185
|
-
|
|
186
|
-
throw new UnrecoverableError.ExcessInput(
|
|
187
|
-
error_msg ?? `Expected end of block, but found: \`${this.preview()}\``
|
|
188
|
-
);
|
|
189
|
-
}
|
|
188
|
+
this.consume("\n",
|
|
189
|
+
error_msg ?? `Expected end of block, but found: \`${this.preview()}\``
|
|
190
|
+
);
|
|
190
191
|
}
|
|
191
192
|
}
|