paseo-plugin-genui 0.1.1 → 0.1.2

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.
@@ -7,9 +7,17 @@
7
7
  // than its input, so the data loader's size limit bounds the work.
8
8
  import { LIMITS } from "./catalog";
9
9
  import type { Value } from "./types";
10
+ import { defineClass } from "./define-class";
10
11
 
11
12
  /** A reason the file is not a readable JUnit report, already phrased for the user. */
12
- export class JUnitError extends Error {}
13
+ export const JUnitError = defineClass(
14
+ function (this: Error, message: string): void {
15
+ this.message = message;
16
+ },
17
+ undefined,
18
+ Error,
19
+ );
20
+ export type JUnitError = InstanceType<typeof JUnitError>;
13
21
 
14
22
  interface XmlElement {
15
23
  readonly name: string;
@@ -1,4 +1,5 @@
1
1
  import type { DiagnosticList, Pos } from "./diagnostics";
2
+ import { defineClass } from "./define-class";
2
3
 
3
4
  export type Token =
4
5
  | (Pos & { readonly t: "num"; readonly value: number })
@@ -52,203 +53,212 @@ export function tokenize(source: string, diagnostics: DiagnosticList): Token[] {
52
53
  return new Lexer(source, diagnostics).run(false);
53
54
  }
54
55
 
55
- class Lexer {
56
- private i = 0;
57
- private line = 1;
58
- private column = 1;
59
-
60
- constructor(
61
- private readonly src: string,
62
- private readonly diagnostics: DiagnosticList,
63
- ) {}
56
+ interface LexerFields {
57
+ i: number;
58
+ line: number;
59
+ column: number;
60
+ src: string;
61
+ diagnostics: DiagnosticList;
62
+ }
64
63
 
65
- private advance(count = 1): void {
66
- for (let n = 0; n < count; n++) {
67
- if (this.src[this.i] === "\n") {
68
- this.line++;
69
- this.column = 1;
70
- } else {
71
- this.column++;
64
+ const Lexer = defineClass(
65
+ function (this: LexerFields, src: string, diagnostics: DiagnosticList): void {
66
+ this.src = src;
67
+ this.diagnostics = diagnostics;
68
+ this.i = 0;
69
+ this.line = 1;
70
+ this.column = 1;
71
+ },
72
+ {
73
+ advance(count = 1): void {
74
+ for (let n = 0; n < count; n++) {
75
+ if (this.src[this.i] === "\n") {
76
+ this.line++;
77
+ this.column = 1;
78
+ } else {
79
+ this.column++;
80
+ }
81
+ this.i++;
72
82
  }
73
- this.i++;
74
- }
75
- }
83
+ },
76
84
 
77
- private match(re: RegExp): string | null {
78
- re.lastIndex = this.i;
79
- const m = re.exec(this.src);
80
- return m ? m[0] : null;
81
- }
85
+ match(re: RegExp): string | null {
86
+ re.lastIndex = this.i;
87
+ const m = re.exec(this.src);
88
+ return m ? m[0] : null;
89
+ },
82
90
 
83
- /**
84
- * Top level: tokens until end of input, separators at bracket depth 0.
85
- * Inside a template `${`: tokens until the matching `}`, no separators.
86
- */
87
- run(inTemplate: boolean): Token[] {
88
- const out: Token[] = [];
89
- let depth = 0;
90
- for (;;) {
91
- this.skipBlank();
92
- const pos = { line: this.line, column: this.column };
93
- const c = this.src[this.i];
94
- if (c === undefined) {
95
- if (inTemplate) this.diagnostics.report(pos, "unterminated template: `${` is missing its `}`");
96
- out.push({ ...pos, t: "eof" });
97
- return inTemplate ? out : joinContinuations(out);
98
- }
99
- if (c === "\n") {
100
- this.advance();
101
- if (!inTemplate && depth === 0) out.push({ ...pos, t: "sep", value: "\n" });
102
- continue;
103
- }
104
- if (c === ";") {
105
- this.advance();
106
- out.push({ ...pos, t: "sep", value: ";" });
107
- continue;
108
- }
109
- if (inTemplate && c === "}" && depth === 0) {
110
- out.push({ ...pos, t: "eof" });
111
- this.advance();
112
- return out;
113
- }
114
- const number = this.match(NUMBER);
115
- if (number !== null) {
116
- this.advance(number.length);
117
- if (IDENT_CHAR.test(this.src[this.i] ?? "")) {
118
- this.diagnostics.report(pos, `invalid number \`${number}${this.src[this.i]}\``);
91
+ /**
92
+ * Top level: tokens until end of input, separators at bracket depth 0.
93
+ * Inside a template `${`: tokens until the matching `}`, no separators.
94
+ */
95
+ run(inTemplate: boolean): Token[] {
96
+ const out: Token[] = [];
97
+ let depth = 0;
98
+ for (;;) {
99
+ this.skipBlank();
100
+ const pos = { line: this.line, column: this.column };
101
+ const c = this.src[this.i];
102
+ if (c === undefined) {
103
+ if (inTemplate) this.diagnostics.report(pos, "unterminated template: `${` is missing its `}`");
104
+ out.push({ ...pos, t: "eof" });
105
+ return inTemplate ? out : joinContinuations(out);
119
106
  }
120
- out.push({ ...pos, t: "num", value: Number(number.replaceAll("_", "")) });
121
- continue;
122
- }
123
- const ident = this.match(IDENT);
124
- if (ident !== null) {
125
- this.advance(ident.length);
126
- out.push({ ...pos, t: "ident", value: ident });
127
- continue;
128
- }
129
- if (c === "$") {
130
- this.advance();
131
- while (this.src[this.i] === " " || this.src[this.i] === "\t") this.advance();
132
- const name = this.match(IDENT);
133
- if (name === null) {
134
- this.diagnostics.report(pos, "expected a state name after `$`, as in `$count`");
107
+ if (c === "\n") {
108
+ this.advance();
109
+ if (!inTemplate && depth === 0) out.push({ ...pos, t: "sep", value: "\n" });
135
110
  continue;
136
111
  }
137
- this.advance(name.length);
138
- out.push({ ...pos, t: "state", value: name });
139
- continue;
140
- }
141
- if (c === '"' || c === "'") {
142
- const value = this.string(c, pos);
143
- if (value !== null) out.push({ ...pos, t: "str", value });
144
- continue;
145
- }
146
- if (c === "`") {
147
- out.push(this.template(pos));
148
- continue;
149
- }
150
- const punct = PUNCTUATION.find((p) => this.src.startsWith(p, this.i));
151
- if (punct === undefined) {
152
- this.diagnostics.report(pos, `unexpected character \`${c}\``);
153
- this.advance();
154
- continue;
112
+ if (c === ";") {
113
+ this.advance();
114
+ out.push({ ...pos, t: "sep", value: ";" });
115
+ continue;
116
+ }
117
+ if (inTemplate && c === "}" && depth === 0) {
118
+ out.push({ ...pos, t: "eof" });
119
+ this.advance();
120
+ return out;
121
+ }
122
+ const number = this.match(NUMBER);
123
+ if (number !== null) {
124
+ this.advance(number.length);
125
+ if (IDENT_CHAR.test(this.src[this.i] ?? "")) {
126
+ this.diagnostics.report(pos, `invalid number \`${number}${this.src[this.i]}\``);
127
+ }
128
+ out.push({ ...pos, t: "num", value: Number(number.replaceAll("_", "")) });
129
+ continue;
130
+ }
131
+ const ident = this.match(IDENT);
132
+ if (ident !== null) {
133
+ this.advance(ident.length);
134
+ out.push({ ...pos, t: "ident", value: ident });
135
+ continue;
136
+ }
137
+ if (c === "$") {
138
+ this.advance();
139
+ while (this.src[this.i] === " " || this.src[this.i] === "\t") this.advance();
140
+ const name = this.match(IDENT);
141
+ if (name === null) {
142
+ this.diagnostics.report(pos, "expected a state name after `$`, as in `$count`");
143
+ continue;
144
+ }
145
+ this.advance(name.length);
146
+ out.push({ ...pos, t: "state", value: name });
147
+ continue;
148
+ }
149
+ if (c === '"' || c === "'") {
150
+ const value = this.string(c, pos);
151
+ if (value !== null) out.push({ ...pos, t: "str", value });
152
+ continue;
153
+ }
154
+ if (c === "`") {
155
+ out.push(this.template(pos));
156
+ continue;
157
+ }
158
+ const punct = PUNCTUATION.find((p) => this.src.startsWith(p, this.i));
159
+ if (punct === undefined) {
160
+ this.diagnostics.report(pos, `unexpected character \`${c}\``);
161
+ this.advance();
162
+ continue;
163
+ }
164
+ if (punct === "(" || punct === "[" || punct === "{") depth++;
165
+ if ((punct === ")" || punct === "]" || punct === "}") && depth > 0) depth--;
166
+ this.advance(punct.length);
167
+ out.push({ ...pos, t: "punct", value: punct });
155
168
  }
156
- if (punct === "(" || punct === "[" || punct === "{") depth++;
157
- if ((punct === ")" || punct === "]" || punct === "}") && depth > 0) depth--;
158
- this.advance(punct.length);
159
- out.push({ ...pos, t: "punct", value: punct });
160
- }
161
- }
169
+ },
162
170
 
163
- private skipBlank(): void {
164
- for (;;) {
165
- const c = this.src[this.i];
166
- if (c === " " || c === "\t" || c === "\r") {
167
- this.advance();
168
- } else if (c === "/" && this.src[this.i + 1] === "/") {
169
- while (this.i < this.src.length && this.src[this.i] !== "\n") this.advance();
170
- } else {
171
- return;
171
+ skipBlank(): void {
172
+ for (;;) {
173
+ const c = this.src[this.i];
174
+ if (c === " " || c === "\t" || c === "\r") {
175
+ this.advance();
176
+ } else if (c === "/" && this.src[this.i + 1] === "/") {
177
+ while (this.i < this.src.length && this.src[this.i] !== "\n") this.advance();
178
+ } else {
179
+ return;
180
+ }
172
181
  }
173
- }
174
- }
182
+ },
175
183
 
176
- /** Reads one escape after `\`; returns the decoded text or null after reporting. */
177
- private escape(): string | null {
178
- const pos = { line: this.line, column: this.column };
179
- this.advance();
180
- const c = this.src[this.i];
181
- if (c !== undefined && ESCAPES[c] !== undefined) {
184
+ /** Reads one escape after `\`; returns the decoded text or null after reporting. */
185
+ escape(): string | null {
186
+ const pos = { line: this.line, column: this.column };
182
187
  this.advance();
183
- return ESCAPES[c];
184
- }
185
- if (c === "u") {
186
- const braced = this.src[this.i + 1] === "{";
187
- const end = braced ? this.src.indexOf("}", this.i + 2) : this.i + 5;
188
- const hex = braced ? this.src.slice(this.i + 2, end) : this.src.slice(this.i + 1, end);
189
- const code = parseInt(hex, 16);
190
- if (end > 0 && HEX.test(hex) && (braced || hex.length === 4) && code <= 0x10ffff) {
191
- this.advance(end - this.i + (braced ? 1 : 0));
192
- return String.fromCodePoint(code);
193
- }
194
- }
195
- this.diagnostics.report(pos, `unknown escape \`\\${c ?? ""}\`; use \\n \\t \\\\ \\" \\' \\$ or \\u{hex}`);
196
- return null;
197
- }
198
-
199
- private string(quote: string, pos: Pos): string | null {
200
- this.advance();
201
- let value = "";
202
- for (;;) {
203
188
  const c = this.src[this.i];
204
- if (c === undefined || c === "\n") {
205
- this.diagnostics.report(pos, `unterminated string: close it with ${quote} on the same line`);
206
- return null;
207
- }
208
- if (c === quote) {
189
+ if (c !== undefined && ESCAPES[c] !== undefined) {
209
190
  this.advance();
210
- return value;
191
+ return ESCAPES[c];
211
192
  }
212
- if (c === "\\") {
213
- value += this.escape() ?? "";
214
- continue;
193
+ if (c === "u") {
194
+ const braced = this.src[this.i + 1] === "{";
195
+ const end = braced ? this.src.indexOf("}", this.i + 2) : this.i + 5;
196
+ const hex = braced ? this.src.slice(this.i + 2, end) : this.src.slice(this.i + 1, end);
197
+ const code = parseInt(hex, 16);
198
+ if (end > 0 && HEX.test(hex) && (braced || hex.length === 4) && code <= 0x10ffff) {
199
+ this.advance(end - this.i + (braced ? 1 : 0));
200
+ return String.fromCodePoint(code);
201
+ }
215
202
  }
216
- value += c;
217
- this.advance();
218
- }
219
- }
203
+ this.diagnostics.report(pos, `unknown escape \`\\${c ?? ""}\`; use \\n \\t \\\\ \\" \\' \\$ or \\u{hex}`);
204
+ return null;
205
+ },
220
206
 
221
- private template(pos: Pos): Token {
222
- this.advance();
223
- const quasis: string[] = [];
224
- const parts: Token[][] = [];
225
- let text = "";
226
- for (;;) {
227
- const c = this.src[this.i];
228
- if (c === undefined) {
229
- this.diagnostics.report(pos, "unterminated template: close it with `");
230
- quasis.push(text);
231
- return { ...pos, t: "tpl", quasis, parts };
232
- }
233
- if (c === "`") {
207
+ string(quote: string, pos: Pos): string | null {
208
+ this.advance();
209
+ let value = "";
210
+ for (;;) {
211
+ const c = this.src[this.i];
212
+ if (c === undefined || c === "\n") {
213
+ this.diagnostics.report(pos, `unterminated string: close it with ${quote} on the same line`);
214
+ return null;
215
+ }
216
+ if (c === quote) {
217
+ this.advance();
218
+ return value;
219
+ }
220
+ if (c === "\\") {
221
+ value += this.escape() ?? "";
222
+ continue;
223
+ }
224
+ value += c;
234
225
  this.advance();
235
- quasis.push(text);
236
- return { ...pos, t: "tpl", quasis, parts };
237
226
  }
238
- if (c === "\\") {
239
- text += this.escape() ?? "";
240
- } else if (c === "$" && this.src[this.i + 1] === "{") {
241
- this.advance(2);
242
- quasis.push(text);
243
- text = "";
244
- parts.push(this.run(true));
245
- } else {
246
- text += c;
247
- this.advance();
227
+ },
228
+
229
+ template(pos: Pos): Token {
230
+ this.advance();
231
+ const quasis: string[] = [];
232
+ const parts: Token[][] = [];
233
+ let text = "";
234
+ for (;;) {
235
+ const c = this.src[this.i];
236
+ if (c === undefined) {
237
+ this.diagnostics.report(pos, "unterminated template: close it with `");
238
+ quasis.push(text);
239
+ return { ...pos, t: "tpl", quasis, parts };
240
+ }
241
+ if (c === "`") {
242
+ this.advance();
243
+ quasis.push(text);
244
+ return { ...pos, t: "tpl", quasis, parts };
245
+ }
246
+ if (c === "\\") {
247
+ text += this.escape() ?? "";
248
+ } else if (c === "$" && this.src[this.i + 1] === "{") {
249
+ this.advance(2);
250
+ quasis.push(text);
251
+ text = "";
252
+ parts.push(this.run(true));
253
+ } else {
254
+ text += c;
255
+ this.advance();
256
+ }
248
257
  }
249
- }
250
- }
251
- }
258
+ },
259
+ },
260
+ );
261
+ type Lexer = InstanceType<typeof Lexer>;
252
262
 
253
263
  /**
254
264
  * Drops redundant separators, and newlines that sit between an operator and