vba-interpreter 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +96 -2
- package/dist/contract-CBGNXslV.d.ts +226 -0
- package/dist/exec/index.d.ts +12 -0
- package/dist/exec/index.js +1 -0
- package/dist/exec-DYWJe-eU.js +306 -0
- package/dist/exec-DYWJe-eU.js.map +1 -0
- package/dist/index-DnrotnmB.d.ts +2015 -0
- package/dist/index-DtaSNo0V.d.ts +111 -0
- package/dist/index-DyoT9JOJ.d.ts +4 -0
- package/dist/index-gM7FYTaq.d.ts +1353 -0
- package/dist/index-zKYgEJdV.d.ts +900 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +3 -0
- package/dist/interp/index.d.ts +10 -0
- package/dist/interp/index.js +1 -0
- package/dist/interp-ChrsgBCf.js +3433 -0
- package/dist/interp-ChrsgBCf.js.map +1 -0
- package/dist/lang/index.d.ts +10 -0
- package/dist/lang/index.js +1 -0
- package/dist/lang-Bo63dJb9.js +4740 -0
- package/dist/lang-Bo63dJb9.js.map +1 -0
- package/dist/runtime/index.d.ts +33 -0
- package/dist/runtime/index.js +1 -0
- package/dist/runtime-C8-PPxmH.js +8284 -0
- package/dist/runtime-C8-PPxmH.js.map +1 -0
- package/dist/semantic/index.d.ts +12 -0
- package/dist/semantic/index.js +1 -0
- package/dist/semantic-CAVxm72C.js +2877 -0
- package/dist/semantic-CAVxm72C.js.map +1 -0
- package/package.json +91 -5
|
@@ -0,0 +1,4740 @@
|
|
|
1
|
+
// src/lang/limits.ts
|
|
2
|
+
/**
|
|
3
|
+
* The limits used for every field a caller leaves out: 4 MiB of source, a million tokens, a
|
|
4
|
+
* nesting depth of 256, 24 line continuations and 1000 diagnostics.
|
|
5
|
+
*/
|
|
6
|
+
const DEFAULT_VBA_LANG_LIMITS = Object.freeze({
|
|
7
|
+
maxSourceLength: 4194304,
|
|
8
|
+
maxTokens: 1e6,
|
|
9
|
+
maxNestingDepth: 256,
|
|
10
|
+
maxLineContinuations: 24,
|
|
11
|
+
maxDiagnostics: 1e3
|
|
12
|
+
});
|
|
13
|
+
/** Merges caller overrides over the defaults, ignoring values that are not positive integers. */
|
|
14
|
+
function resolveLimits(overrides) {
|
|
15
|
+
if (!overrides) return DEFAULT_VBA_LANG_LIMITS;
|
|
16
|
+
const pick = (key) => {
|
|
17
|
+
const value = overrides[key];
|
|
18
|
+
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : DEFAULT_VBA_LANG_LIMITS[key];
|
|
19
|
+
};
|
|
20
|
+
return {
|
|
21
|
+
maxSourceLength: pick("maxSourceLength"),
|
|
22
|
+
maxTokens: pick("maxTokens"),
|
|
23
|
+
maxNestingDepth: pick("maxNestingDepth"),
|
|
24
|
+
maxLineContinuations: pick("maxLineContinuations"),
|
|
25
|
+
maxDiagnostics: pick("maxDiagnostics")
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Thrown when a source exceeds a hard limit of {@link VbaLangLimits}. */
|
|
29
|
+
var VbaLangLimitError = class extends Error {
|
|
30
|
+
/** The limit that was exceeded. */
|
|
31
|
+
limit;
|
|
32
|
+
/** The source length, or for `maxTokens` the token count reached (one over the limit). */
|
|
33
|
+
value;
|
|
34
|
+
constructor(limit, value, max) {
|
|
35
|
+
super(limit === "maxSourceLength" ? `VBA source is ${value} characters long, over the limit of ${max}` : `VBA source produces more than ${max} tokens`);
|
|
36
|
+
this.name = "VbaLangLimitError";
|
|
37
|
+
this.limit = limit;
|
|
38
|
+
this.value = value;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
// src/lang/constants.ts
|
|
42
|
+
/**
|
|
43
|
+
* The conditional compilation constants VBA predefines, as 64-bit Office for Windows sets them
|
|
44
|
+
* (`True` is -1). Override any of them with `ParseOptions.constants`, for example
|
|
45
|
+
* `{ Win64: false }` for 32-bit Office or `{ Mac: true, Win32: false, Win64: false }`.
|
|
46
|
+
*/
|
|
47
|
+
const DEFAULT_CONDITIONAL_CONSTANTS = Object.freeze({
|
|
48
|
+
VBA6: -1,
|
|
49
|
+
VBA7: -1,
|
|
50
|
+
Win16: 0,
|
|
51
|
+
Win32: -1,
|
|
52
|
+
Win64: -1,
|
|
53
|
+
Mac: 0
|
|
54
|
+
});
|
|
55
|
+
/** A constant table keyed by lower-cased name, booleans as -1 and 0. */
|
|
56
|
+
function constantTable(overrides) {
|
|
57
|
+
const table = /* @__PURE__ */ new Map();
|
|
58
|
+
const add = (entries) => {
|
|
59
|
+
for (const [name, value] of Object.entries(entries)) if (typeof value === "boolean") table.set(name.toLowerCase(), value ? -1 : 0);
|
|
60
|
+
else if (typeof value === "number" || typeof value === "string") table.set(name.toLowerCase(), value);
|
|
61
|
+
};
|
|
62
|
+
add(DEFAULT_CONDITIONAL_CONSTANTS);
|
|
63
|
+
if (overrides) add(overrides);
|
|
64
|
+
return table;
|
|
65
|
+
}
|
|
66
|
+
// src/lang/options.ts
|
|
67
|
+
/** The two-digit year window the VBA editor used on the reference machine (1950 to 2049). */
|
|
68
|
+
const DEFAULT_TWO_DIGIT_YEAR_MAX = 2049;
|
|
69
|
+
/**
|
|
70
|
+
* The date reading order observed in the VBA editor on a machine whose short date is
|
|
71
|
+
* `yyyy-MM-dd`: month/day/year, then year/month/day, then day/month/year. Other locales may try
|
|
72
|
+
* their own order second; pass it explicitly for them.
|
|
73
|
+
*/
|
|
74
|
+
const DEFAULT_DATE_ORDER = Object.freeze([
|
|
75
|
+
"mdy",
|
|
76
|
+
"ymd",
|
|
77
|
+
"dmy"
|
|
78
|
+
]);
|
|
79
|
+
const ORDERS = /* @__PURE__ */ new Set([
|
|
80
|
+
"mdy",
|
|
81
|
+
"ymd",
|
|
82
|
+
"dmy"
|
|
83
|
+
]);
|
|
84
|
+
/**
|
|
85
|
+
* Applies the defaults. This is the one place the front end reads the clock, and only when the
|
|
86
|
+
* caller gives no `currentYear`.
|
|
87
|
+
*/
|
|
88
|
+
function resolveLexicalOptions(options = {}) {
|
|
89
|
+
const year = (value) => typeof value === "number" && Number.isInteger(value) && value >= 100 && value <= 9999;
|
|
90
|
+
const order = (options.dateOrder ?? []).filter((item) => ORDERS.has(item));
|
|
91
|
+
return {
|
|
92
|
+
limits: resolveLimits(options.limits),
|
|
93
|
+
dates: {
|
|
94
|
+
currentYear: year(options.currentYear) ? options.currentYear : (/* @__PURE__ */ new Date()).getFullYear(),
|
|
95
|
+
twoDigitYearMax: year(options.twoDigitYearMax) && options.twoDigitYearMax >= 199 ? options.twoDigitYearMax : DEFAULT_TWO_DIGIT_YEAR_MAX,
|
|
96
|
+
dateOrder: order.length > 0 ? order : DEFAULT_DATE_ORDER
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
// src/lang/header.ts
|
|
101
|
+
function* lines(source) {
|
|
102
|
+
let offset = 0;
|
|
103
|
+
let line = 1;
|
|
104
|
+
while (offset < source.length) {
|
|
105
|
+
let end = offset;
|
|
106
|
+
while (end < source.length && source.charAt(end) !== "\n" && source.charAt(end) !== "\r") end++;
|
|
107
|
+
let next = end;
|
|
108
|
+
if (source.charAt(next) === "\r") next++;
|
|
109
|
+
if (source.charAt(next) === "\n") next++;
|
|
110
|
+
yield {
|
|
111
|
+
text: source.slice(offset, end),
|
|
112
|
+
start: {
|
|
113
|
+
offset,
|
|
114
|
+
line,
|
|
115
|
+
column: 1
|
|
116
|
+
},
|
|
117
|
+
end: {
|
|
118
|
+
offset: end,
|
|
119
|
+
line,
|
|
120
|
+
column: end - offset + 1
|
|
121
|
+
},
|
|
122
|
+
next: {
|
|
123
|
+
offset: next,
|
|
124
|
+
line: line + 1,
|
|
125
|
+
column: 1
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
offset = next;
|
|
129
|
+
line++;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/** The text before a `'` comment that is outside quotes. */
|
|
133
|
+
function stripComment(value) {
|
|
134
|
+
let quoted = false;
|
|
135
|
+
for (let i = 0; i < value.length; i++) {
|
|
136
|
+
const ch = value.charAt(i);
|
|
137
|
+
if (ch === "\"") quoted = !quoted;
|
|
138
|
+
else if (ch === "'" && !quoted) return value.slice(0, i).trimEnd();
|
|
139
|
+
}
|
|
140
|
+
return value.trimEnd();
|
|
141
|
+
}
|
|
142
|
+
function property(line) {
|
|
143
|
+
const match = /^\s*([\w.$]+)\s*=\s*(.*)$/.exec(line.text);
|
|
144
|
+
if (!match) return null;
|
|
145
|
+
return {
|
|
146
|
+
kind: "HeaderProperty",
|
|
147
|
+
span: {
|
|
148
|
+
start: line.start,
|
|
149
|
+
end: line.end
|
|
150
|
+
},
|
|
151
|
+
name: match[1] ?? "",
|
|
152
|
+
value: stripComment(match[2] ?? "")
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const BEGIN = /^Begin(?:Property)?(?:\s+(\S+))?(?:\s+(\S+))?\s*$/i;
|
|
156
|
+
const END = /^End(?:Property)?$/i;
|
|
157
|
+
/**
|
|
158
|
+
* Reads the `VERSION` line and `BEGIN ... END` block that exported `.cls` and `.frm` files (and
|
|
159
|
+
* some module streams) start with. Without a `VERSION` line there is no header. Blocks nest (form
|
|
160
|
+
* controls) up to `maxNestingDepth`; deeper blocks are reported and left out of the tree.
|
|
161
|
+
*/
|
|
162
|
+
var HeaderReader = class {
|
|
163
|
+
source;
|
|
164
|
+
diagnostics;
|
|
165
|
+
limits;
|
|
166
|
+
#stack = [];
|
|
167
|
+
#properties = [];
|
|
168
|
+
#root = null;
|
|
169
|
+
/** How many `Begin` lines past the depth limit are open. */
|
|
170
|
+
#skippedDepth = 0;
|
|
171
|
+
constructor(source, diagnostics, limits) {
|
|
172
|
+
this.source = source;
|
|
173
|
+
this.diagnostics = diagnostics;
|
|
174
|
+
this.limits = limits;
|
|
175
|
+
}
|
|
176
|
+
read() {
|
|
177
|
+
const start = {
|
|
178
|
+
offset: 0,
|
|
179
|
+
line: 1,
|
|
180
|
+
column: 1
|
|
181
|
+
};
|
|
182
|
+
const iterator = lines(this.source);
|
|
183
|
+
let current = iterator.next();
|
|
184
|
+
while (!current.done && current.value.text.trim() === "") current = iterator.next();
|
|
185
|
+
if (current.done) return {
|
|
186
|
+
header: null,
|
|
187
|
+
end: start
|
|
188
|
+
};
|
|
189
|
+
const versionLine = current.value;
|
|
190
|
+
const version = /^\s*VERSION\s+(\S+)(\s+CLASS)?\s*$/i.exec(versionLine.text);
|
|
191
|
+
if (!version) return {
|
|
192
|
+
header: null,
|
|
193
|
+
end: start
|
|
194
|
+
};
|
|
195
|
+
let end = versionLine.next;
|
|
196
|
+
let lastEnd = versionLine.end;
|
|
197
|
+
for (current = iterator.next(); !current.done; current = iterator.next()) {
|
|
198
|
+
const line = current.value;
|
|
199
|
+
if (!this.#line(line)) break;
|
|
200
|
+
end = line.next;
|
|
201
|
+
lastEnd = line.end;
|
|
202
|
+
if (this.#root && this.#stack.length === 0 && this.#skippedDepth === 0) break;
|
|
203
|
+
}
|
|
204
|
+
if (this.#stack.length > 0) this.diagnostics.add("invalid-header", "Expected: End", {
|
|
205
|
+
start: versionLine.start,
|
|
206
|
+
end: lastEnd
|
|
207
|
+
});
|
|
208
|
+
return {
|
|
209
|
+
header: {
|
|
210
|
+
kind: "ModuleHeader",
|
|
211
|
+
span: {
|
|
212
|
+
start: versionLine.start,
|
|
213
|
+
end: lastEnd
|
|
214
|
+
},
|
|
215
|
+
version: version[1] ?? "",
|
|
216
|
+
isClass: version[2] !== void 0,
|
|
217
|
+
properties: this.#properties,
|
|
218
|
+
block: this.#root
|
|
219
|
+
},
|
|
220
|
+
end: end.offset <= this.source.length ? end : lastEnd
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
/** Takes one header line; false when the line is not part of the header. */
|
|
224
|
+
#line(line) {
|
|
225
|
+
const text = line.text.trim();
|
|
226
|
+
const begin = BEGIN.exec(text);
|
|
227
|
+
const top = this.#stack.at(-1);
|
|
228
|
+
if (begin) return this.#begin(line, begin, /^BeginProperty/i.test(text));
|
|
229
|
+
if (END.test(text) && this.#skippedDepth > 0) {
|
|
230
|
+
this.#skippedDepth--;
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
if (END.test(text) && top) {
|
|
234
|
+
top.span.end = line.end;
|
|
235
|
+
this.#stack.pop();
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
if (this.#skippedDepth > 0) return true;
|
|
239
|
+
if (top) {
|
|
240
|
+
const parsed = property(line);
|
|
241
|
+
if (parsed) top.properties.push(parsed);
|
|
242
|
+
else if (text !== "") this.diagnostics.add("invalid-header", "Invalid line in the module header", {
|
|
243
|
+
start: line.start,
|
|
244
|
+
end: line.end
|
|
245
|
+
}, "warning");
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
if (this.#root || /^Attribute\s/i.test(text)) return false;
|
|
249
|
+
if (text === "") return true;
|
|
250
|
+
const parsed = property(line);
|
|
251
|
+
if (parsed) this.#properties.push(parsed);
|
|
252
|
+
return parsed !== null;
|
|
253
|
+
}
|
|
254
|
+
#begin(line, match, isProperty) {
|
|
255
|
+
if (this.#skippedDepth > 0 || this.#stack.length >= this.limits.maxNestingDepth) {
|
|
256
|
+
if (this.#skippedDepth === 0) this.diagnostics.add("too-complex", "Header blocks nested too deeply", {
|
|
257
|
+
start: line.start,
|
|
258
|
+
end: line.end
|
|
259
|
+
});
|
|
260
|
+
this.#skippedDepth++;
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
const block = {
|
|
264
|
+
kind: "HeaderBlock",
|
|
265
|
+
span: {
|
|
266
|
+
start: line.start,
|
|
267
|
+
end: line.end
|
|
268
|
+
},
|
|
269
|
+
type: isProperty ? "Property" : match[1] ?? null,
|
|
270
|
+
name: isProperty ? match[1] ?? null : match[2] ?? null,
|
|
271
|
+
properties: [],
|
|
272
|
+
children: []
|
|
273
|
+
};
|
|
274
|
+
const parent = this.#stack.at(-1);
|
|
275
|
+
if (parent) parent.children.push(block);
|
|
276
|
+
else if (!this.#root) this.#root = block;
|
|
277
|
+
else return false;
|
|
278
|
+
this.#stack.push(block);
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
/** Reads a module header; see {@link HeaderReader}. */
|
|
283
|
+
function parseHeader(source, diagnostics, limits) {
|
|
284
|
+
return new HeaderReader(source, diagnostics, limits).read();
|
|
285
|
+
}
|
|
286
|
+
// src/lang/module-kind.ts
|
|
287
|
+
const CLASS_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
288
|
+
"vb_predeclaredid",
|
|
289
|
+
"vb_exposed",
|
|
290
|
+
"vb_creatable",
|
|
291
|
+
"vb_globalnamespace"
|
|
292
|
+
]);
|
|
293
|
+
/** The string value of the module attribute `name` (lower-cased), if set. */
|
|
294
|
+
function attributeText(members, name) {
|
|
295
|
+
for (const member of members) {
|
|
296
|
+
if (member.kind !== "AttributeStatement" || member.name.toLowerCase() !== name) continue;
|
|
297
|
+
const value = member.values[0];
|
|
298
|
+
if (value?.kind === "StringLiteral") return value.value;
|
|
299
|
+
}
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* The kind of a module whose caller does not know it: a `Begin {clsid}` form header makes a
|
|
304
|
+
* form, `VB_Base` a document module, and `VERSION ... CLASS` or class attributes a class.
|
|
305
|
+
*/
|
|
306
|
+
function inferModuleKind(module) {
|
|
307
|
+
if (module.header?.block?.type?.startsWith("{")) return "form";
|
|
308
|
+
if (attributeText(module.members, "vb_base") !== null) return "document";
|
|
309
|
+
if (module.header?.isClass) return "class";
|
|
310
|
+
return module.members.some((m) => m.kind === "AttributeStatement" && CLASS_ATTRIBUTES.has(m.name.toLowerCase())) ? "class" : "standard";
|
|
311
|
+
}
|
|
312
|
+
// src/lang/span.ts
|
|
313
|
+
/** An empty span at `position`, used for what is missing (an expected token, an omitted argument). */
|
|
314
|
+
function emptySpanAt(position) {
|
|
315
|
+
return {
|
|
316
|
+
start: position,
|
|
317
|
+
end: position
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
/** Whether `offset` falls inside `span`. */
|
|
321
|
+
function spanContains(span, offset) {
|
|
322
|
+
return offset >= span.start.offset && offset < span.end.offset;
|
|
323
|
+
}
|
|
324
|
+
// src/lang/parser/cursor.ts
|
|
325
|
+
/** Thrown when nesting passes the limit; caught at the statement that contains it. */
|
|
326
|
+
var NestingLimit = class extends Error {
|
|
327
|
+
constructor() {
|
|
328
|
+
super("Nesting limit");
|
|
329
|
+
this.name = "NestingLimit";
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
/**
|
|
333
|
+
* A cursor over the token stream, with the diagnostic bag and the nesting state of one parse.
|
|
334
|
+
* The token list is never changed: the shared `Next` of `Next j, i` is held as a pending token
|
|
335
|
+
* that is read once before the current one.
|
|
336
|
+
*/
|
|
337
|
+
var TokenCursor = class {
|
|
338
|
+
tokens;
|
|
339
|
+
diagnostics;
|
|
340
|
+
limits;
|
|
341
|
+
#index = 0;
|
|
342
|
+
#pending = null;
|
|
343
|
+
#last = null;
|
|
344
|
+
#consumed = 0;
|
|
345
|
+
#stalls = 0;
|
|
346
|
+
/** The current nesting depth (see {@link VbaLangLimits.maxNestingDepth}). */
|
|
347
|
+
depth = 0;
|
|
348
|
+
/** How many single-line `If` statements enclose the current position (`Else` ends a list). */
|
|
349
|
+
singleLineIf = 0;
|
|
350
|
+
blocks = [];
|
|
351
|
+
constructor(tokens, diagnostics, limits) {
|
|
352
|
+
this.tokens = tokens;
|
|
353
|
+
this.diagnostics = diagnostics;
|
|
354
|
+
this.limits = limits;
|
|
355
|
+
}
|
|
356
|
+
get token() {
|
|
357
|
+
return this.peek(0);
|
|
358
|
+
}
|
|
359
|
+
/** The token `ahead` places on (0 is the current token); the last token is `eof`. */
|
|
360
|
+
peek(ahead) {
|
|
361
|
+
let offset = ahead;
|
|
362
|
+
if (this.#pending) {
|
|
363
|
+
if (offset === 0) return this.#pending;
|
|
364
|
+
offset--;
|
|
365
|
+
}
|
|
366
|
+
return this.tokens[Math.min(this.#index + offset, this.tokens.length - 1)];
|
|
367
|
+
}
|
|
368
|
+
/** How many tokens have been consumed: it only grows, so loops can check their progress. */
|
|
369
|
+
get consumed() {
|
|
370
|
+
return this.#consumed;
|
|
371
|
+
}
|
|
372
|
+
/** How many times a parse loop made no progress and the cursor had to skip a token. */
|
|
373
|
+
get stalls() {
|
|
374
|
+
return this.#stalls;
|
|
375
|
+
}
|
|
376
|
+
/** Consumes the current token. The end of the tokens is never consumed. */
|
|
377
|
+
advance() {
|
|
378
|
+
const token = this.token;
|
|
379
|
+
if (this.#pending) this.#pending = null;
|
|
380
|
+
else if (token.kind === "eof") return token;
|
|
381
|
+
else this.#index++;
|
|
382
|
+
this.#consumed++;
|
|
383
|
+
this.#last = token;
|
|
384
|
+
return token;
|
|
385
|
+
}
|
|
386
|
+
/** Makes `token` the next token read, before the current one; the token list is unchanged. */
|
|
387
|
+
readNext(token) {
|
|
388
|
+
this.#pending = token;
|
|
389
|
+
}
|
|
390
|
+
mark() {
|
|
391
|
+
return {
|
|
392
|
+
index: this.#index,
|
|
393
|
+
pending: this.#pending,
|
|
394
|
+
last: this.#last,
|
|
395
|
+
consumed: this.#consumed,
|
|
396
|
+
diagnostics: this.diagnostics.items.length
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
/** Goes back to `mark`, dropping the diagnostics reported since. */
|
|
400
|
+
reset(mark) {
|
|
401
|
+
this.#index = mark.index;
|
|
402
|
+
this.#pending = mark.pending;
|
|
403
|
+
this.#last = mark.last;
|
|
404
|
+
this.#consumed = mark.consumed;
|
|
405
|
+
this.diagnostics.items.length = mark.diagnostics;
|
|
406
|
+
}
|
|
407
|
+
/** The end of the last consumed token, or the start of the current one at the beginning. */
|
|
408
|
+
get previousEnd() {
|
|
409
|
+
return this.#last ? this.#last.span.end : this.token.span.start;
|
|
410
|
+
}
|
|
411
|
+
/** The span from `start` to the end of the last consumed token. */
|
|
412
|
+
spanFrom(start) {
|
|
413
|
+
const first = "span" in start ? start.span : start;
|
|
414
|
+
const end = this.previousEnd;
|
|
415
|
+
return {
|
|
416
|
+
start: first.start,
|
|
417
|
+
end: end.offset >= first.start.offset ? end : first.start
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
/** Whether the token `ahead` is the plain word `lower` (not bracketed, no type suffix). */
|
|
421
|
+
isWord(lower, ahead = 0) {
|
|
422
|
+
const token = this.peek(ahead);
|
|
423
|
+
return token.kind === "word" && !token.bracketed && token.suffix === null && token.lower === lower;
|
|
424
|
+
}
|
|
425
|
+
isPunct(value, ahead = 0) {
|
|
426
|
+
const token = this.peek(ahead);
|
|
427
|
+
return token.kind === "punct" && token.value === value;
|
|
428
|
+
}
|
|
429
|
+
eatWord(lower) {
|
|
430
|
+
return this.isWord(lower) ? this.advance() : null;
|
|
431
|
+
}
|
|
432
|
+
eatPunct(value) {
|
|
433
|
+
if (!this.isPunct(value)) return false;
|
|
434
|
+
this.advance();
|
|
435
|
+
return true;
|
|
436
|
+
}
|
|
437
|
+
expectWord(lower, display) {
|
|
438
|
+
if (this.eatWord(lower)) return true;
|
|
439
|
+
this.expected(display);
|
|
440
|
+
return false;
|
|
441
|
+
}
|
|
442
|
+
expectPunct(value) {
|
|
443
|
+
if (this.eatPunct(value)) return true;
|
|
444
|
+
this.expected(value);
|
|
445
|
+
return false;
|
|
446
|
+
}
|
|
447
|
+
/** Reports `Expected: what` at the current token. */
|
|
448
|
+
expected(what) {
|
|
449
|
+
const token = this.token;
|
|
450
|
+
const span = token.kind === "newline" || token.kind === "eof" ? emptySpanAt(token.span.start) : token.span;
|
|
451
|
+
this.error("expected", `Expected: ${what}`, span);
|
|
452
|
+
}
|
|
453
|
+
error(code, message, span) {
|
|
454
|
+
this.diagnostics.add(code, message, span);
|
|
455
|
+
}
|
|
456
|
+
/** The end of a physical statement list: a line break or the end of the source. */
|
|
457
|
+
atLineEnd() {
|
|
458
|
+
const { kind } = this.token;
|
|
459
|
+
return kind === "newline" || kind === "eof";
|
|
460
|
+
}
|
|
461
|
+
/** The end of a statement: line end, `:`, `Else` inside a single-line `If`, or a shared `Next`. */
|
|
462
|
+
atStatementEnd() {
|
|
463
|
+
const token = this.token;
|
|
464
|
+
if (this.atLineEnd() || token === this.#pending) return true;
|
|
465
|
+
if (token.kind === "punct" && token.value === ":") return true;
|
|
466
|
+
return this.singleLineIf > 0 && this.isWord("else");
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Whether the last consumed token was a line break or `:`: a block statement that ran into an
|
|
470
|
+
* enclosing terminator (`If` without `End If` before `Next`) already crossed its statement end.
|
|
471
|
+
*/
|
|
472
|
+
afterSeparator() {
|
|
473
|
+
const last = this.#last;
|
|
474
|
+
return last !== null && (last.kind === "newline" || last.kind === "punct" && last.value === ":");
|
|
475
|
+
}
|
|
476
|
+
/** Skips to the end of the current statement, leaving the separator in place. */
|
|
477
|
+
skipToStatementEnd() {
|
|
478
|
+
while (!this.atStatementEnd()) this.advance();
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Skips a statement that cannot be parsed: consumes the current token (unless it ends the
|
|
482
|
+
* line), then the rest of the statement. Every recovery uses it, so a loop that recovers
|
|
483
|
+
* always moves on.
|
|
484
|
+
*/
|
|
485
|
+
skipStatement() {
|
|
486
|
+
if (!this.atLineEnd()) this.advance();
|
|
487
|
+
this.skipToStatementEnd();
|
|
488
|
+
}
|
|
489
|
+
/** Skips line breaks and `:` separators. */
|
|
490
|
+
skipSeparators() {
|
|
491
|
+
while (this.token.kind === "newline" || this.isPunct(":")) this.advance();
|
|
492
|
+
}
|
|
493
|
+
/** Enters one level of nesting, throwing {@link NestingLimit} past the limit. */
|
|
494
|
+
enter() {
|
|
495
|
+
if (++this.depth > this.limits.maxNestingDepth) {
|
|
496
|
+
this.depth--;
|
|
497
|
+
throw new NestingLimit();
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
leave() {
|
|
501
|
+
this.depth--;
|
|
502
|
+
}
|
|
503
|
+
/** Runs `parse` one nesting level deeper. */
|
|
504
|
+
nested(parse) {
|
|
505
|
+
this.enter();
|
|
506
|
+
try {
|
|
507
|
+
return parse();
|
|
508
|
+
} finally {
|
|
509
|
+
this.leave();
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
/** A progress guard for one parse loop. */
|
|
513
|
+
loop() {
|
|
514
|
+
return new ParseLoop(this);
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Called by a {@link ParseLoop} whose last iteration consumed nothing: reports the token and
|
|
518
|
+
* skips it. Returns false at the end of the tokens, where the loop must stop.
|
|
519
|
+
*/
|
|
520
|
+
recoverStall() {
|
|
521
|
+
this.#stalls++;
|
|
522
|
+
if (this.token.kind === "eof") return false;
|
|
523
|
+
this.error("unexpected", "Syntax error", this.token.span);
|
|
524
|
+
this.advance();
|
|
525
|
+
return true;
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
/**
|
|
529
|
+
* Guarantees that a parse loop moves: each iteration must consume a token. Every parser loop
|
|
530
|
+
* checks `continues()` at its head; a stalled loop skips one token (reported) instead of spinning,
|
|
531
|
+
* so no input can hang the parser, and `TokenCursor.stalls` lets the tests prove it never has to.
|
|
532
|
+
*/
|
|
533
|
+
var ParseLoop = class {
|
|
534
|
+
cursor;
|
|
535
|
+
#last = -1;
|
|
536
|
+
constructor(cursor) {
|
|
537
|
+
this.cursor = cursor;
|
|
538
|
+
}
|
|
539
|
+
continues() {
|
|
540
|
+
if (this.cursor.consumed === this.#last && !this.cursor.recoverStall()) return false;
|
|
541
|
+
this.#last = this.cursor.consumed;
|
|
542
|
+
return true;
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
// src/lang/parser/names.ts
|
|
546
|
+
/** The name a word token spells. */
|
|
547
|
+
function nameRefOf(token) {
|
|
548
|
+
return {
|
|
549
|
+
kind: "NameRef",
|
|
550
|
+
span: token.span,
|
|
551
|
+
name: token.name,
|
|
552
|
+
suffix: token.suffix,
|
|
553
|
+
bracketed: token.bracketed
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
// src/lang/parser/part.ts
|
|
557
|
+
/**
|
|
558
|
+
* A parser for one family of constructs (expressions, blocks, declarations...). The families
|
|
559
|
+
* share one {@link Parser}, through which they call each other, and its cursor.
|
|
560
|
+
*/
|
|
561
|
+
var ParserPart = class {
|
|
562
|
+
parser;
|
|
563
|
+
constructor(parser) {
|
|
564
|
+
this.parser = parser;
|
|
565
|
+
}
|
|
566
|
+
get p() {
|
|
567
|
+
return this.parser.cursor;
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
// src/lang/parser/arguments.ts
|
|
571
|
+
/** Argument lists ([MS-VBAL] 5.6.16.8): positional, omitted, named (`name:=`) and `ByVal`. */
|
|
572
|
+
var ArgumentParser = class extends ParserPart {
|
|
573
|
+
/**
|
|
574
|
+
* Parses arguments: inside parentheses up to `)` when `parenthesised`, else up to the end of
|
|
575
|
+
* the statement. Omitted arguments (`f(1, , 3)`) are kept with a null value; an omitted last
|
|
576
|
+
* argument (`f(1, )`) is reported as the Syntax error VBA gives. `fileNumbers` allows `#n`
|
|
577
|
+
* arguments (`Input(5, #1)`).
|
|
578
|
+
*/
|
|
579
|
+
list(parenthesised, fileNumbers = false) {
|
|
580
|
+
const p = this.p;
|
|
581
|
+
const args = [];
|
|
582
|
+
if (parenthesised && p.eatPunct(")")) return args;
|
|
583
|
+
const atListEnd = () => parenthesised ? p.isPunct(")") || p.atLineEnd() : p.atStatementEnd();
|
|
584
|
+
do {
|
|
585
|
+
const start = p.token;
|
|
586
|
+
if (p.isPunct(",") || atListEnd()) {
|
|
587
|
+
if (args.length > 0 && atListEnd()) p.error("unexpected", "Syntax error", emptySpanAt(start.span.start));
|
|
588
|
+
const span = emptySpanAt(start.span.start);
|
|
589
|
+
args.push({
|
|
590
|
+
kind: "Argument",
|
|
591
|
+
span,
|
|
592
|
+
value: null,
|
|
593
|
+
name: null,
|
|
594
|
+
byVal: false
|
|
595
|
+
});
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
let name = null;
|
|
599
|
+
if (start.kind === "word" && p.isPunct(":=", 1)) {
|
|
600
|
+
name = nameRefOf(start);
|
|
601
|
+
p.advance();
|
|
602
|
+
p.advance();
|
|
603
|
+
}
|
|
604
|
+
const byVal = p.eatWord("byval") !== null;
|
|
605
|
+
const value = fileNumbers && p.isPunct("#") ? this.#fileNumber() : this.parser.expressions.parse();
|
|
606
|
+
args.push({
|
|
607
|
+
kind: "Argument",
|
|
608
|
+
span: p.spanFrom(start),
|
|
609
|
+
value,
|
|
610
|
+
name,
|
|
611
|
+
byVal
|
|
612
|
+
});
|
|
613
|
+
} while (p.eatPunct(","));
|
|
614
|
+
if (parenthesised) p.expectPunct(")");
|
|
615
|
+
return args;
|
|
616
|
+
}
|
|
617
|
+
/** `#n` as an argument of `Input` or `InputB`. */
|
|
618
|
+
#fileNumber() {
|
|
619
|
+
const p = this.p;
|
|
620
|
+
const start = p.advance();
|
|
621
|
+
const expression = p.nested(() => this.parser.expressions.postfix());
|
|
622
|
+
return {
|
|
623
|
+
kind: "FileNumber",
|
|
624
|
+
span: p.spanFrom(start),
|
|
625
|
+
expression
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
// src/lang/parser/terminators.ts
|
|
630
|
+
const END_TARGETS = {
|
|
631
|
+
if: "endif",
|
|
632
|
+
select: "endselect",
|
|
633
|
+
with: "endwith",
|
|
634
|
+
sub: "endsub",
|
|
635
|
+
function: "endfunction",
|
|
636
|
+
property: "endproperty",
|
|
637
|
+
type: "endtype",
|
|
638
|
+
enum: "endenum"
|
|
639
|
+
};
|
|
640
|
+
const MODIFIERS = /* @__PURE__ */ new Set([
|
|
641
|
+
"public",
|
|
642
|
+
"private",
|
|
643
|
+
"friend",
|
|
644
|
+
"static",
|
|
645
|
+
"global"
|
|
646
|
+
]);
|
|
647
|
+
const ACCEPTED = {
|
|
648
|
+
if: /* @__PURE__ */ new Set([
|
|
649
|
+
"elseif",
|
|
650
|
+
"else",
|
|
651
|
+
"endif"
|
|
652
|
+
]),
|
|
653
|
+
select: /* @__PURE__ */ new Set(["case", "endselect"]),
|
|
654
|
+
for: /* @__PURE__ */ new Set(["next"]),
|
|
655
|
+
do: /* @__PURE__ */ new Set(["loop"]),
|
|
656
|
+
while: /* @__PURE__ */ new Set(["wend"]),
|
|
657
|
+
with: /* @__PURE__ */ new Set(["endwith"]),
|
|
658
|
+
procedure: /* @__PURE__ */ new Set([
|
|
659
|
+
"endsub",
|
|
660
|
+
"endfunction",
|
|
661
|
+
"endproperty",
|
|
662
|
+
"procedure"
|
|
663
|
+
])
|
|
664
|
+
};
|
|
665
|
+
const STRAY = {
|
|
666
|
+
elseif: "Else without If",
|
|
667
|
+
else: "Else without If",
|
|
668
|
+
endif: "End If without block If",
|
|
669
|
+
case: "Case without Select Case",
|
|
670
|
+
endselect: "End Select without Select Case",
|
|
671
|
+
next: "Next without For",
|
|
672
|
+
loop: "Loop without Do",
|
|
673
|
+
wend: "Wend without While",
|
|
674
|
+
endwith: "End With without With",
|
|
675
|
+
endsub: "End Sub without Sub",
|
|
676
|
+
endfunction: "End Function without Function",
|
|
677
|
+
endproperty: "End Property without Property",
|
|
678
|
+
procedure: "Expected: End Sub",
|
|
679
|
+
endtype: "End Type without Type",
|
|
680
|
+
endenum: "End Enum without Enum"
|
|
681
|
+
};
|
|
682
|
+
/** Whether a `Sub`, `Function` or `Property Get/Let/Set` header starts `ahead` tokens on. */
|
|
683
|
+
function procedureKeywordAt(p, ahead) {
|
|
684
|
+
if (p.isWord("sub", ahead) || p.isWord("function", ahead)) return true;
|
|
685
|
+
return p.isWord("property", ahead) && (p.isWord("get", ahead + 1) || p.isWord("let", ahead + 1) || p.isWord("set", ahead + 1));
|
|
686
|
+
}
|
|
687
|
+
/** Whether a procedure header (with optional scope and `Static`) starts here. */
|
|
688
|
+
function procedureStartsHere(p) {
|
|
689
|
+
let ahead = 0;
|
|
690
|
+
while (ahead < 2) {
|
|
691
|
+
const token = p.peek(ahead);
|
|
692
|
+
if (token.kind !== "word" || !MODIFIERS.has(token.lower) || token.bracketed) break;
|
|
693
|
+
ahead++;
|
|
694
|
+
}
|
|
695
|
+
return procedureKeywordAt(p, ahead);
|
|
696
|
+
}
|
|
697
|
+
/** The terminator at the current token, if any. */
|
|
698
|
+
function terminatorAt(p) {
|
|
699
|
+
const token = p.token;
|
|
700
|
+
if (token.kind !== "word" || token.bracketed || token.suffix !== null) return null;
|
|
701
|
+
switch (token.lower) {
|
|
702
|
+
case "elseif":
|
|
703
|
+
case "else":
|
|
704
|
+
case "endif":
|
|
705
|
+
case "case":
|
|
706
|
+
case "next":
|
|
707
|
+
case "loop":
|
|
708
|
+
case "wend": return token.lower;
|
|
709
|
+
case "end": {
|
|
710
|
+
const next = p.peek(1);
|
|
711
|
+
return next.kind === "word" && !next.bracketed ? END_TARGETS[next.lower] ?? null : null;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
return procedureStartsHere(p) ? "procedure" : null;
|
|
715
|
+
}
|
|
716
|
+
/** The message for a terminator no open block accepts (`Loop without Do`). */
|
|
717
|
+
function strayMessage(terminator) {
|
|
718
|
+
return STRAY[terminator];
|
|
719
|
+
}
|
|
720
|
+
/** Whether an enclosing block accepts `terminator`, so the current block should stop there. */
|
|
721
|
+
function enclosingAccepts(p, terminator) {
|
|
722
|
+
return p.blocks.some((block) => ACCEPTED[block].has(terminator));
|
|
723
|
+
}
|
|
724
|
+
// src/lang/parser/blocks.ts
|
|
725
|
+
/** Statement lists, labels and the recovery shared by every block. */
|
|
726
|
+
var BlockParser = class extends ParserPart {
|
|
727
|
+
/**
|
|
728
|
+
* Parses statements until a terminator that this or an enclosing block accepts. A terminator no
|
|
729
|
+
* block accepts (`Loop` without `Do`) is reported and skipped.
|
|
730
|
+
*/
|
|
731
|
+
parse(kind) {
|
|
732
|
+
const p = this.p;
|
|
733
|
+
p.enter();
|
|
734
|
+
p.blocks.push(kind);
|
|
735
|
+
const body = [];
|
|
736
|
+
try {
|
|
737
|
+
for (const loop = p.loop(); loop.continues();) {
|
|
738
|
+
p.skipSeparators();
|
|
739
|
+
if (p.token.kind === "eof") break;
|
|
740
|
+
if (this.labelStartsHere()) {
|
|
741
|
+
body.push(this.label());
|
|
742
|
+
continue;
|
|
743
|
+
}
|
|
744
|
+
const terminator = terminatorAt(p);
|
|
745
|
+
if (terminator && enclosingAccepts(p, terminator)) break;
|
|
746
|
+
if (terminator) body.push(this.skipInvalid(strayMessage(terminator), "block-mismatch"));
|
|
747
|
+
else {
|
|
748
|
+
const statement = this.parser.statements.parse();
|
|
749
|
+
if (statement) body.push(statement);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
} finally {
|
|
753
|
+
p.blocks.pop();
|
|
754
|
+
p.leave();
|
|
755
|
+
}
|
|
756
|
+
return body;
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Reports the statement at the cursor and skips it, always consuming at least one token. The
|
|
760
|
+
* one recovery every block and statement list uses.
|
|
761
|
+
*/
|
|
762
|
+
skipInvalid(message, code, start = this.p.token) {
|
|
763
|
+
const p = this.p;
|
|
764
|
+
p.skipStatement();
|
|
765
|
+
p.error(code, message, p.spanFrom(start));
|
|
766
|
+
return {
|
|
767
|
+
kind: "InvalidStatement",
|
|
768
|
+
span: p.spanFrom(start)
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
/** Consumes `End <word>` (or `EndIf`) when it is the given terminator. */
|
|
772
|
+
eatEnd(terminator) {
|
|
773
|
+
const p = this.p;
|
|
774
|
+
if (terminatorAt(p) !== terminator) return false;
|
|
775
|
+
const first = p.advance();
|
|
776
|
+
if (first.kind === "word" && first.lower === "end") p.advance();
|
|
777
|
+
return true;
|
|
778
|
+
}
|
|
779
|
+
/** Whether a label definition (`Name:` or a line number) starts here. */
|
|
780
|
+
labelStartsHere() {
|
|
781
|
+
const p = this.p;
|
|
782
|
+
const token = p.token;
|
|
783
|
+
if (!token.lineStart) return false;
|
|
784
|
+
if (token.kind === "number") return token.radix === 10 && token.integral && token.suffix === null;
|
|
785
|
+
return token.kind === "word" && token.suffix === null && (token.keyword === null || token.bracketed) && p.isPunct(":", 1);
|
|
786
|
+
}
|
|
787
|
+
/** Parses a label definition at the start of a line; the `:` after it is consumed. */
|
|
788
|
+
label() {
|
|
789
|
+
const p = this.p;
|
|
790
|
+
const token = p.advance();
|
|
791
|
+
const isLineNumber = token.kind === "number";
|
|
792
|
+
const name = isLineNumber ? token.text : token.kind === "word" ? token.name : token.text;
|
|
793
|
+
const label = {
|
|
794
|
+
kind: "LabelRef",
|
|
795
|
+
span: token.span,
|
|
796
|
+
name,
|
|
797
|
+
isLineNumber
|
|
798
|
+
};
|
|
799
|
+
p.eatPunct(":");
|
|
800
|
+
return {
|
|
801
|
+
kind: "Label",
|
|
802
|
+
span: p.spanFrom(token),
|
|
803
|
+
label
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
/** Parses a label reference after `GoTo`, `GoSub`, `Resume` or `On ... GoTo`. */
|
|
807
|
+
labelRef() {
|
|
808
|
+
const p = this.p;
|
|
809
|
+
const token = p.token;
|
|
810
|
+
if (token.kind === "number" && token.integral && token.radix === 10) {
|
|
811
|
+
p.advance();
|
|
812
|
+
return {
|
|
813
|
+
kind: "LabelRef",
|
|
814
|
+
span: token.span,
|
|
815
|
+
name: token.text,
|
|
816
|
+
isLineNumber: true
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
if (token.kind === "word") {
|
|
820
|
+
p.advance();
|
|
821
|
+
return {
|
|
822
|
+
kind: "LabelRef",
|
|
823
|
+
span: token.span,
|
|
824
|
+
name: token.name,
|
|
825
|
+
isLineNumber: false
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
p.expected("label");
|
|
829
|
+
return {
|
|
830
|
+
kind: "LabelRef",
|
|
831
|
+
span: emptySpanAt(token.span.start),
|
|
832
|
+
name: "",
|
|
833
|
+
isLineNumber: false
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
// src/lang/parser/conditionals.ts
|
|
838
|
+
const COMPARISONS = /* @__PURE__ */ new Set([
|
|
839
|
+
"=",
|
|
840
|
+
"<>",
|
|
841
|
+
"<",
|
|
842
|
+
">",
|
|
843
|
+
"<=",
|
|
844
|
+
">="
|
|
845
|
+
]);
|
|
846
|
+
/** `If` (block and single-line) and `Select Case` ([MS-VBAL] 5.4.2.8 to 5.4.2.10). */
|
|
847
|
+
var ConditionalParser = class extends ParserPart {
|
|
848
|
+
parseIf() {
|
|
849
|
+
return this.p.nested(() => this.#if());
|
|
850
|
+
}
|
|
851
|
+
#if() {
|
|
852
|
+
const p = this.p;
|
|
853
|
+
const start = p.advance();
|
|
854
|
+
const condition = this.parser.expressions.parse();
|
|
855
|
+
p.expectWord("then", "Then");
|
|
856
|
+
if (!p.atLineEnd()) return this.#singleLineIf(start, condition);
|
|
857
|
+
return this.#withSingleLineIfReset(() => {
|
|
858
|
+
const blocks = this.parser.blocks;
|
|
859
|
+
const branches = [{
|
|
860
|
+
kind: "IfBranch",
|
|
861
|
+
span: p.spanFrom(start),
|
|
862
|
+
condition,
|
|
863
|
+
body: blocks.parse("if")
|
|
864
|
+
}];
|
|
865
|
+
let elseBody = null;
|
|
866
|
+
for (const loop = p.loop(); loop.continues();) {
|
|
867
|
+
const terminator = terminatorAt(p);
|
|
868
|
+
if (terminator === "elseif" && elseBody === null) {
|
|
869
|
+
const branchStart = p.advance();
|
|
870
|
+
const branchCondition = this.parser.expressions.parse();
|
|
871
|
+
p.expectWord("then", "Then");
|
|
872
|
+
const body = blocks.parse("if");
|
|
873
|
+
const span = p.spanFrom(branchStart);
|
|
874
|
+
branches.push({
|
|
875
|
+
kind: "IfBranch",
|
|
876
|
+
span,
|
|
877
|
+
condition: branchCondition,
|
|
878
|
+
body
|
|
879
|
+
});
|
|
880
|
+
} else if (terminator === "else" && elseBody === null) {
|
|
881
|
+
p.advance();
|
|
882
|
+
elseBody = blocks.parse("if");
|
|
883
|
+
} else if (blocks.eatEnd("endif")) break;
|
|
884
|
+
else if (terminator === "elseif" || terminator === "else") blocks.skipInvalid("Else without If", "block-mismatch");
|
|
885
|
+
else {
|
|
886
|
+
p.error("block-mismatch", "Block If without End If", start.span);
|
|
887
|
+
break;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
return {
|
|
891
|
+
kind: "If",
|
|
892
|
+
span: p.spanFrom(start),
|
|
893
|
+
form: "block",
|
|
894
|
+
branches,
|
|
895
|
+
elseBody
|
|
896
|
+
};
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
#singleLineIf(start, condition) {
|
|
900
|
+
const p = this.p;
|
|
901
|
+
p.singleLineIf++;
|
|
902
|
+
try {
|
|
903
|
+
const body = this.#lineStatements();
|
|
904
|
+
const branch = {
|
|
905
|
+
kind: "IfBranch",
|
|
906
|
+
span: p.spanFrom(start),
|
|
907
|
+
condition,
|
|
908
|
+
body
|
|
909
|
+
};
|
|
910
|
+
const elseBody = p.eatWord("else") ? this.#lineStatements() : null;
|
|
911
|
+
return {
|
|
912
|
+
kind: "If",
|
|
913
|
+
span: p.spanFrom(start),
|
|
914
|
+
form: "singleLine",
|
|
915
|
+
branches: [branch],
|
|
916
|
+
elseBody
|
|
917
|
+
};
|
|
918
|
+
} finally {
|
|
919
|
+
p.singleLineIf--;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
/** Statements on one line, separated by `:`, up to the line end or the `Else` of a single-line If. */
|
|
923
|
+
#lineStatements() {
|
|
924
|
+
const p = this.p;
|
|
925
|
+
const body = [];
|
|
926
|
+
const first = p.token;
|
|
927
|
+
if (first.kind === "number" && first.integral) {
|
|
928
|
+
const label = this.parser.blocks.labelRef();
|
|
929
|
+
body.push({
|
|
930
|
+
kind: "GoTo",
|
|
931
|
+
span: label.span,
|
|
932
|
+
label
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
for (const loop = p.loop(); loop.continues();) {
|
|
936
|
+
while (p.eatPunct(":"));
|
|
937
|
+
if (p.atLineEnd() || p.isWord("else")) break;
|
|
938
|
+
const terminator = terminatorAt(p);
|
|
939
|
+
if (terminator) {
|
|
940
|
+
body.push(this.parser.blocks.skipInvalid(strayMessage(terminator), "block-mismatch"));
|
|
941
|
+
continue;
|
|
942
|
+
}
|
|
943
|
+
const statement = this.parser.statements.parse();
|
|
944
|
+
if (statement) body.push(statement);
|
|
945
|
+
}
|
|
946
|
+
return body;
|
|
947
|
+
}
|
|
948
|
+
/** Runs `parse` outside any single-line If: a block statement starts a new statement list. */
|
|
949
|
+
#withSingleLineIfReset(parse) {
|
|
950
|
+
const p = this.p;
|
|
951
|
+
const saved = p.singleLineIf;
|
|
952
|
+
p.singleLineIf = 0;
|
|
953
|
+
try {
|
|
954
|
+
return parse();
|
|
955
|
+
} finally {
|
|
956
|
+
p.singleLineIf = saved;
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
parseSelect() {
|
|
960
|
+
const p = this.p;
|
|
961
|
+
const blocks = this.parser.blocks;
|
|
962
|
+
const start = p.advance();
|
|
963
|
+
p.expectWord("case", "Case");
|
|
964
|
+
const subject = this.parser.expressions.parse();
|
|
965
|
+
const cases = [];
|
|
966
|
+
let elseBody = null;
|
|
967
|
+
return this.#withSingleLineIfReset(() => {
|
|
968
|
+
if (!p.atStatementEnd()) {
|
|
969
|
+
p.expected("end of statement");
|
|
970
|
+
p.skipToStatementEnd();
|
|
971
|
+
}
|
|
972
|
+
for (const loop = p.loop(); loop.continues();) {
|
|
973
|
+
p.skipSeparators();
|
|
974
|
+
const terminator = terminatorAt(p);
|
|
975
|
+
if (terminator === "case") {
|
|
976
|
+
const caseStart = p.advance();
|
|
977
|
+
if (p.eatWord("else")) {
|
|
978
|
+
if (elseBody !== null) p.error("block-mismatch", "Case Else already given", caseStart.span);
|
|
979
|
+
elseBody = blocks.parse("select");
|
|
980
|
+
continue;
|
|
981
|
+
}
|
|
982
|
+
const tests = this.#caseTests();
|
|
983
|
+
if (elseBody !== null) p.error("block-mismatch", "Case after Case Else", caseStart.span);
|
|
984
|
+
const body = blocks.parse("select");
|
|
985
|
+
cases.push({
|
|
986
|
+
kind: "CaseClause",
|
|
987
|
+
span: p.spanFrom(caseStart),
|
|
988
|
+
tests,
|
|
989
|
+
body
|
|
990
|
+
});
|
|
991
|
+
} else if (blocks.eatEnd("endselect")) break;
|
|
992
|
+
else if (p.token.kind === "eof" || terminator && enclosingAccepts(p, terminator)) {
|
|
993
|
+
p.error("block-mismatch", "Select Case without End Select", start.span);
|
|
994
|
+
break;
|
|
995
|
+
} else blocks.skipInvalid("Expected: Case", "expected");
|
|
996
|
+
}
|
|
997
|
+
return {
|
|
998
|
+
kind: "SelectCase",
|
|
999
|
+
span: p.spanFrom(start),
|
|
1000
|
+
subject,
|
|
1001
|
+
cases,
|
|
1002
|
+
elseBody
|
|
1003
|
+
};
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
#caseTests() {
|
|
1007
|
+
const p = this.p;
|
|
1008
|
+
const expressions = this.parser.expressions;
|
|
1009
|
+
const tests = [];
|
|
1010
|
+
do {
|
|
1011
|
+
const start = p.token;
|
|
1012
|
+
const hadIs = p.eatWord("is") !== null;
|
|
1013
|
+
const token = p.token;
|
|
1014
|
+
if (token.kind === "punct" && COMPARISONS.has(token.value)) {
|
|
1015
|
+
p.advance();
|
|
1016
|
+
const value = expressions.parse();
|
|
1017
|
+
const operator = token.value;
|
|
1018
|
+
tests.push({
|
|
1019
|
+
kind: "CaseIs",
|
|
1020
|
+
span: p.spanFrom(start),
|
|
1021
|
+
operator,
|
|
1022
|
+
value
|
|
1023
|
+
});
|
|
1024
|
+
continue;
|
|
1025
|
+
}
|
|
1026
|
+
if (hadIs) p.expected("comparison operator");
|
|
1027
|
+
const value = expressions.parse();
|
|
1028
|
+
if (p.eatWord("to")) {
|
|
1029
|
+
const to = expressions.parse();
|
|
1030
|
+
tests.push({
|
|
1031
|
+
kind: "CaseRange",
|
|
1032
|
+
span: p.spanFrom(start),
|
|
1033
|
+
from: value,
|
|
1034
|
+
to
|
|
1035
|
+
});
|
|
1036
|
+
} else tests.push({
|
|
1037
|
+
kind: "CaseValue",
|
|
1038
|
+
span: p.spanFrom(start),
|
|
1039
|
+
value
|
|
1040
|
+
});
|
|
1041
|
+
} while (p.eatPunct(","));
|
|
1042
|
+
return tests;
|
|
1043
|
+
}
|
|
1044
|
+
};
|
|
1045
|
+
// src/lang/parser/declarations.ts
|
|
1046
|
+
/** Declarations inside and outside procedures: variables, constants, parameters, attributes. */
|
|
1047
|
+
var DeclarationParser = class extends ParserPart {
|
|
1048
|
+
/** Parses a declared name. A reserved word is reported but kept, so parsing can go on. */
|
|
1049
|
+
nameRef(what = "identifier") {
|
|
1050
|
+
const p = this.p;
|
|
1051
|
+
const token = p.token;
|
|
1052
|
+
if (token.kind === "word") {
|
|
1053
|
+
if (token.keyword !== null) p.error("expected", `Expected: ${what}`, token.span);
|
|
1054
|
+
p.advance();
|
|
1055
|
+
return nameRefOf(token);
|
|
1056
|
+
}
|
|
1057
|
+
p.expected(what);
|
|
1058
|
+
return {
|
|
1059
|
+
kind: "NameRef",
|
|
1060
|
+
span: emptySpanAt(token.span.start),
|
|
1061
|
+
name: "",
|
|
1062
|
+
suffix: null,
|
|
1063
|
+
bracketed: false
|
|
1064
|
+
};
|
|
1065
|
+
}
|
|
1066
|
+
/** Parses `As [New] Type [* length] [()]` when the current word is `As`. */
|
|
1067
|
+
asClause() {
|
|
1068
|
+
const p = this.p;
|
|
1069
|
+
const start = p.token;
|
|
1070
|
+
if (!p.eatWord("as")) return null;
|
|
1071
|
+
const isNew = p.eatWord("new") !== null;
|
|
1072
|
+
const type = this.parser.expressions.typeRef();
|
|
1073
|
+
const fixedLength = p.eatPunct("*") ? this.parser.expressions.parse() : null;
|
|
1074
|
+
let isArray = false;
|
|
1075
|
+
if (p.isPunct("(") && p.isPunct(")", 1)) {
|
|
1076
|
+
p.advance();
|
|
1077
|
+
p.advance();
|
|
1078
|
+
isArray = true;
|
|
1079
|
+
}
|
|
1080
|
+
return {
|
|
1081
|
+
kind: "AsClause",
|
|
1082
|
+
span: p.spanFrom(start),
|
|
1083
|
+
type,
|
|
1084
|
+
isNew,
|
|
1085
|
+
fixedLength,
|
|
1086
|
+
isArray
|
|
1087
|
+
};
|
|
1088
|
+
}
|
|
1089
|
+
/** Parses the bounds inside `( ... )`, the `(` already consumed, up to and including `)`. */
|
|
1090
|
+
bounds() {
|
|
1091
|
+
const p = this.p;
|
|
1092
|
+
const bounds = [];
|
|
1093
|
+
if (p.eatPunct(")")) return bounds;
|
|
1094
|
+
do {
|
|
1095
|
+
const start = p.token;
|
|
1096
|
+
const first = this.parser.expressions.parse();
|
|
1097
|
+
let lower = null;
|
|
1098
|
+
let upper = first;
|
|
1099
|
+
if (p.eatWord("to")) {
|
|
1100
|
+
lower = first;
|
|
1101
|
+
upper = this.parser.expressions.parse();
|
|
1102
|
+
}
|
|
1103
|
+
bounds.push({
|
|
1104
|
+
kind: "ArrayBound",
|
|
1105
|
+
span: p.spanFrom(start),
|
|
1106
|
+
lower,
|
|
1107
|
+
upper
|
|
1108
|
+
});
|
|
1109
|
+
} while (p.eatPunct(","));
|
|
1110
|
+
p.expectPunct(")");
|
|
1111
|
+
return bounds;
|
|
1112
|
+
}
|
|
1113
|
+
#dimensions() {
|
|
1114
|
+
const p = this.p;
|
|
1115
|
+
const start = p.token;
|
|
1116
|
+
if (!p.eatPunct("(")) return null;
|
|
1117
|
+
const bounds = this.bounds();
|
|
1118
|
+
return {
|
|
1119
|
+
kind: "ArrayDimensions",
|
|
1120
|
+
span: p.spanFrom(start),
|
|
1121
|
+
bounds
|
|
1122
|
+
};
|
|
1123
|
+
}
|
|
1124
|
+
/** Parses the declarators of `Dim`, `Static`, `Private`, `Public` or `Global`, the keyword consumed. */
|
|
1125
|
+
variables(start, keyword) {
|
|
1126
|
+
const p = this.p;
|
|
1127
|
+
const declarators = [];
|
|
1128
|
+
do {
|
|
1129
|
+
const begin = p.token;
|
|
1130
|
+
const withEvents = p.eatWord("withevents") !== null;
|
|
1131
|
+
const name = this.nameRef();
|
|
1132
|
+
const dimensions = this.#dimensions();
|
|
1133
|
+
const asClause = this.asClause();
|
|
1134
|
+
declarators.push({
|
|
1135
|
+
kind: "VariableDeclarator",
|
|
1136
|
+
span: p.spanFrom(begin),
|
|
1137
|
+
name,
|
|
1138
|
+
withEvents,
|
|
1139
|
+
dimensions,
|
|
1140
|
+
asClause
|
|
1141
|
+
});
|
|
1142
|
+
} while (p.eatPunct(","));
|
|
1143
|
+
return {
|
|
1144
|
+
kind: "VariableDeclaration",
|
|
1145
|
+
span: p.spanFrom(start.span),
|
|
1146
|
+
keyword,
|
|
1147
|
+
declarators
|
|
1148
|
+
};
|
|
1149
|
+
}
|
|
1150
|
+
/** Parses `Const a [As T] = value, ...`, the `Const` keyword current. */
|
|
1151
|
+
constants(start, scope) {
|
|
1152
|
+
const p = this.p;
|
|
1153
|
+
p.advance();
|
|
1154
|
+
const declarators = [];
|
|
1155
|
+
do {
|
|
1156
|
+
const begin = p.token;
|
|
1157
|
+
const name = this.nameRef();
|
|
1158
|
+
const asClause = this.asClause();
|
|
1159
|
+
p.expectPunct("=");
|
|
1160
|
+
const value = this.parser.expressions.parse();
|
|
1161
|
+
declarators.push({
|
|
1162
|
+
kind: "ConstantDeclarator",
|
|
1163
|
+
span: p.spanFrom(begin),
|
|
1164
|
+
name,
|
|
1165
|
+
asClause,
|
|
1166
|
+
value
|
|
1167
|
+
});
|
|
1168
|
+
} while (p.eatPunct(","));
|
|
1169
|
+
return {
|
|
1170
|
+
kind: "ConstDeclaration",
|
|
1171
|
+
span: p.spanFrom(start.span),
|
|
1172
|
+
scope,
|
|
1173
|
+
declarators
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
/** Parses a parenthesised parameter list when `(` is current; an absent list is empty. */
|
|
1177
|
+
parameters() {
|
|
1178
|
+
const p = this.p;
|
|
1179
|
+
const parameters = [];
|
|
1180
|
+
if (!p.eatPunct("(")) return parameters;
|
|
1181
|
+
if (p.eatPunct(")")) return parameters;
|
|
1182
|
+
do {
|
|
1183
|
+
const start = p.token;
|
|
1184
|
+
const optional = p.eatWord("optional") !== null;
|
|
1185
|
+
const passing = p.eatWord("byval") ? "ByVal" : p.eatWord("byref") ? "ByRef" : null;
|
|
1186
|
+
const paramArray = p.eatWord("paramarray") !== null;
|
|
1187
|
+
const name = this.nameRef();
|
|
1188
|
+
let isArray = false;
|
|
1189
|
+
if (p.isPunct("(")) {
|
|
1190
|
+
p.advance();
|
|
1191
|
+
p.expectPunct(")");
|
|
1192
|
+
isArray = true;
|
|
1193
|
+
}
|
|
1194
|
+
const asClause = this.asClause();
|
|
1195
|
+
const defaultValue = p.eatPunct("=") ? this.parser.expressions.parse() : null;
|
|
1196
|
+
if (defaultValue && !optional) p.error("unexpected", "Only Optional parameters can have a default value", p.spanFrom(start));
|
|
1197
|
+
parameters.push({
|
|
1198
|
+
kind: "Parameter",
|
|
1199
|
+
span: p.spanFrom(start),
|
|
1200
|
+
name,
|
|
1201
|
+
optional,
|
|
1202
|
+
passing,
|
|
1203
|
+
paramArray,
|
|
1204
|
+
isArray,
|
|
1205
|
+
asClause,
|
|
1206
|
+
defaultValue
|
|
1207
|
+
});
|
|
1208
|
+
} while (p.eatPunct(","));
|
|
1209
|
+
p.expectPunct(")");
|
|
1210
|
+
return parameters;
|
|
1211
|
+
}
|
|
1212
|
+
/** Parses `Attribute Name[.Member] = value[, value]`, the keyword current. */
|
|
1213
|
+
attribute() {
|
|
1214
|
+
const p = this.p;
|
|
1215
|
+
const start = p.advance();
|
|
1216
|
+
const parts = [];
|
|
1217
|
+
do {
|
|
1218
|
+
const token = p.token;
|
|
1219
|
+
if (token.kind !== "word") {
|
|
1220
|
+
p.expected("identifier");
|
|
1221
|
+
break;
|
|
1222
|
+
}
|
|
1223
|
+
p.advance();
|
|
1224
|
+
parts.push(token.name);
|
|
1225
|
+
} while (p.eatPunct("."));
|
|
1226
|
+
p.expectPunct("=");
|
|
1227
|
+
const values = [];
|
|
1228
|
+
do
|
|
1229
|
+
values.push(this.parser.expressions.parse());
|
|
1230
|
+
while (p.eatPunct(","));
|
|
1231
|
+
return {
|
|
1232
|
+
kind: "AttributeStatement",
|
|
1233
|
+
span: p.spanFrom(start),
|
|
1234
|
+
name: parts.join("."),
|
|
1235
|
+
values
|
|
1236
|
+
};
|
|
1237
|
+
}
|
|
1238
|
+
};
|
|
1239
|
+
// src/lang/keywords.ts
|
|
1240
|
+
const words = (list) => new Set(list.toLowerCase().split(/\s+/));
|
|
1241
|
+
/** Statement keywords. */
|
|
1242
|
+
const STATEMENT_KEYWORDS = words(`Call Case Close Const Declare DefBool DefByte DefCur DefDate
|
|
1243
|
+
DefDbl DefInt DefLng DefLngLng DefLngPtr DefObj DefSng DefStr DefVar Dim Do Else ElseIf End EndIf
|
|
1244
|
+
Enum Erase Event Exit For Friend Function Get Global GoSub GoTo If Implements Input Let Lock Loop
|
|
1245
|
+
LSet Next On Open Option Print Private Public Put RaiseEvent ReDim Resume Return RSet Seek Select
|
|
1246
|
+
Set Static Stop Sub Type Unlock Wend While With Write`);
|
|
1247
|
+
/** Marker keywords. */
|
|
1248
|
+
const MARKER_KEYWORDS = words(`Any As ByRef ByVal Case Each Else In New Shared Until WithEvents
|
|
1249
|
+
Write Optional ParamArray Preserve Spc Tab Then To`);
|
|
1250
|
+
/** Operator identifiers. */
|
|
1251
|
+
const OPERATOR_KEYWORDS = words("AddressOf And Eqv Imp Is Like New Mod Not Or TypeOf Xor");
|
|
1252
|
+
/** Special forms: reserved, but used in expressions as function names. */
|
|
1253
|
+
const SPECIAL_FORMS = words("Array Circle Input InputB LBound Scale UBound");
|
|
1254
|
+
/** Reserved type names. */
|
|
1255
|
+
const TYPE_KEYWORDS = words(`Boolean Byte Currency Date Double Integer Long LongLong LongPtr
|
|
1256
|
+
Single String Variant`);
|
|
1257
|
+
/** Literal identifiers. */
|
|
1258
|
+
const LITERAL_KEYWORDS = words("True False Nothing Empty Null");
|
|
1259
|
+
/** Reserved names: built-in functions and objects, valid as simple names in expressions. */
|
|
1260
|
+
const RESERVED_NAMES = words(`Abs CBool CByte CCur CDate CDbl CDec CInt CLng CLngLng CLngPtr
|
|
1261
|
+
CSng CStr CVar CVErr Date Debug DoEvents Fix Int Len LenB Me PSet Sgn String`);
|
|
1262
|
+
words(`Attribute LINEINPUT VB_Base VB_Control VB_Creatable
|
|
1263
|
+
VB_Customizable VB_Description VB_Exposed VB_Ext_KEY VB_GlobalNameSpace VB_HelpID VB_Invoke_Func
|
|
1264
|
+
VB_Invoke_Property VB_Invoke_PropertyPut VB_Invoke_PropertyPutRef VB_MemberFlags VB_Name
|
|
1265
|
+
VB_PredeclaredId VB_ProcData VB_TemplateDerived VB_UserMemId VB_VarDescription VB_VarHelpID
|
|
1266
|
+
VB_VarMemberFlags VB_VarProcData VB_VarUserMemId CDecl Decimal DefDec Rem`);
|
|
1267
|
+
const ALL = /* @__PURE__ */ new Set([
|
|
1268
|
+
...STATEMENT_KEYWORDS,
|
|
1269
|
+
...MARKER_KEYWORDS,
|
|
1270
|
+
...OPERATOR_KEYWORDS,
|
|
1271
|
+
...SPECIAL_FORMS,
|
|
1272
|
+
...TYPE_KEYWORDS,
|
|
1273
|
+
...LITERAL_KEYWORDS,
|
|
1274
|
+
...RESERVED_NAMES,
|
|
1275
|
+
"attribute",
|
|
1276
|
+
"cdecl",
|
|
1277
|
+
"decimal",
|
|
1278
|
+
"defdec",
|
|
1279
|
+
"rem"
|
|
1280
|
+
]);
|
|
1281
|
+
/** The keyword a word is, lower-cased, or undefined when it is not reserved. */
|
|
1282
|
+
function keywordOf(lowerName) {
|
|
1283
|
+
return ALL.has(lowerName) ? lowerName : void 0;
|
|
1284
|
+
}
|
|
1285
|
+
/**
|
|
1286
|
+
* Reserved words that are still valid as a simple name in an expression: built-in functions,
|
|
1287
|
+
* special forms and the type names that double as conversion or constant functions.
|
|
1288
|
+
*/
|
|
1289
|
+
const EXPRESSION_NAMES = /* @__PURE__ */ new Set([
|
|
1290
|
+
...SPECIAL_FORMS,
|
|
1291
|
+
...RESERVED_NAMES,
|
|
1292
|
+
"string",
|
|
1293
|
+
"date",
|
|
1294
|
+
"seek",
|
|
1295
|
+
"decimal"
|
|
1296
|
+
]);
|
|
1297
|
+
/** The built-in type names an `As` clause recognises, by lower-cased name. */
|
|
1298
|
+
const BUILTIN_TYPES = new Map([
|
|
1299
|
+
"Boolean",
|
|
1300
|
+
"Byte",
|
|
1301
|
+
"Currency",
|
|
1302
|
+
"Date",
|
|
1303
|
+
"Decimal",
|
|
1304
|
+
"Double",
|
|
1305
|
+
"Integer",
|
|
1306
|
+
"Long",
|
|
1307
|
+
"LongLong",
|
|
1308
|
+
"LongPtr",
|
|
1309
|
+
"Object",
|
|
1310
|
+
"Single",
|
|
1311
|
+
"String",
|
|
1312
|
+
"Variant",
|
|
1313
|
+
"Any"
|
|
1314
|
+
].map((name) => [name.toLowerCase(), name]));
|
|
1315
|
+
// src/lang/parser/operators.ts
|
|
1316
|
+
const PRECEDENCE = {
|
|
1317
|
+
Imp: 1,
|
|
1318
|
+
Eqv: 2,
|
|
1319
|
+
Xor: 3,
|
|
1320
|
+
Or: 4,
|
|
1321
|
+
And: 5,
|
|
1322
|
+
"=": 7,
|
|
1323
|
+
"<>": 7,
|
|
1324
|
+
"<": 7,
|
|
1325
|
+
">": 7,
|
|
1326
|
+
"<=": 7,
|
|
1327
|
+
">=": 7,
|
|
1328
|
+
Like: 7,
|
|
1329
|
+
Is: 7,
|
|
1330
|
+
"&": 8,
|
|
1331
|
+
"+": 9,
|
|
1332
|
+
"-": 9,
|
|
1333
|
+
Mod: 10,
|
|
1334
|
+
"\\": 11,
|
|
1335
|
+
"*": 12,
|
|
1336
|
+
"/": 12,
|
|
1337
|
+
"^": 14
|
|
1338
|
+
};
|
|
1339
|
+
const WORD_OPERATORS = {
|
|
1340
|
+
mod: "Mod",
|
|
1341
|
+
like: "Like",
|
|
1342
|
+
is: "Is",
|
|
1343
|
+
and: "And",
|
|
1344
|
+
or: "Or",
|
|
1345
|
+
xor: "Xor",
|
|
1346
|
+
eqv: "Eqv",
|
|
1347
|
+
imp: "Imp"
|
|
1348
|
+
};
|
|
1349
|
+
function binaryOperatorAt(token) {
|
|
1350
|
+
if (token.kind === "punct") return token.value in PRECEDENCE ? token.value : null;
|
|
1351
|
+
if (token.kind === "word" && !token.bracketed && token.suffix === null) return WORD_OPERATORS[token.lower] ?? null;
|
|
1352
|
+
return null;
|
|
1353
|
+
}
|
|
1354
|
+
// src/lang/parser/expressions.ts
|
|
1355
|
+
/** The functions whose arguments may be written `#n` ([MS-VBAL] 6.1.2.6.1.11: `Input(5, #1)`). */
|
|
1356
|
+
const FILE_NUMBER_FUNCTIONS$1 = /* @__PURE__ */ new Set(["input", "inputb"]);
|
|
1357
|
+
/** Expressions ([MS-VBAL] 5.6): operators by precedence, literals, names and postfix chains. */
|
|
1358
|
+
var ExpressionParser = class extends ParserPart {
|
|
1359
|
+
/** Parses a full expression. */
|
|
1360
|
+
parse() {
|
|
1361
|
+
return this.p.nested(() => this.#binary(0));
|
|
1362
|
+
}
|
|
1363
|
+
#binary(minPrecedence) {
|
|
1364
|
+
const p = this.p;
|
|
1365
|
+
let left = this.#unary();
|
|
1366
|
+
let links = 0;
|
|
1367
|
+
try {
|
|
1368
|
+
for (;;) {
|
|
1369
|
+
const operator = binaryOperatorAt(p.token);
|
|
1370
|
+
if (!operator) return left;
|
|
1371
|
+
const precedence = PRECEDENCE[operator];
|
|
1372
|
+
if (precedence < minPrecedence) return left;
|
|
1373
|
+
p.enter();
|
|
1374
|
+
links++;
|
|
1375
|
+
p.advance();
|
|
1376
|
+
const right = this.#binary(precedence + 1);
|
|
1377
|
+
left = {
|
|
1378
|
+
kind: "Binary",
|
|
1379
|
+
span: {
|
|
1380
|
+
start: left.span.start,
|
|
1381
|
+
end: right.span.end
|
|
1382
|
+
},
|
|
1383
|
+
operator,
|
|
1384
|
+
left,
|
|
1385
|
+
right
|
|
1386
|
+
};
|
|
1387
|
+
}
|
|
1388
|
+
} finally {
|
|
1389
|
+
p.depth -= links;
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
#unary() {
|
|
1393
|
+
const p = this.p;
|
|
1394
|
+
const start = p.token;
|
|
1395
|
+
const negation = p.isPunct("-") || p.isPunct("+");
|
|
1396
|
+
if (negation || p.isWord("not")) {
|
|
1397
|
+
const operator = !negation ? "Not" : start.text === "+" ? "+" : "-";
|
|
1398
|
+
return p.nested(() => {
|
|
1399
|
+
p.advance();
|
|
1400
|
+
const operand = this.#binary(negation ? 13 : 7);
|
|
1401
|
+
return {
|
|
1402
|
+
kind: "Unary",
|
|
1403
|
+
span: p.spanFrom(start),
|
|
1404
|
+
operator,
|
|
1405
|
+
operand
|
|
1406
|
+
};
|
|
1407
|
+
});
|
|
1408
|
+
}
|
|
1409
|
+
if (p.eatWord("typeof")) {
|
|
1410
|
+
const expression = this.postfix();
|
|
1411
|
+
p.expectWord("is", "Is");
|
|
1412
|
+
const type = this.typeRef();
|
|
1413
|
+
return {
|
|
1414
|
+
kind: "TypeOf",
|
|
1415
|
+
span: p.spanFrom(start),
|
|
1416
|
+
expression,
|
|
1417
|
+
type
|
|
1418
|
+
};
|
|
1419
|
+
}
|
|
1420
|
+
if (p.eatWord("new")) return {
|
|
1421
|
+
kind: "New",
|
|
1422
|
+
span: p.spanFrom(start),
|
|
1423
|
+
type: this.typeRef()
|
|
1424
|
+
};
|
|
1425
|
+
if (p.eatWord("addressof")) return {
|
|
1426
|
+
kind: "AddressOf",
|
|
1427
|
+
span: p.spanFrom(start),
|
|
1428
|
+
procedure: this.postfix()
|
|
1429
|
+
};
|
|
1430
|
+
return this.postfix();
|
|
1431
|
+
}
|
|
1432
|
+
/** Parses a literal, name, parenthesised or `With`-relative expression. */
|
|
1433
|
+
#primary() {
|
|
1434
|
+
const p = this.p;
|
|
1435
|
+
const token = p.token;
|
|
1436
|
+
switch (token.kind) {
|
|
1437
|
+
case "number": {
|
|
1438
|
+
p.advance();
|
|
1439
|
+
const { value, bigValue, exact, literalType, radix, suffix, text } = token;
|
|
1440
|
+
return {
|
|
1441
|
+
kind: "NumberLiteral",
|
|
1442
|
+
span: token.span,
|
|
1443
|
+
value,
|
|
1444
|
+
bigValue,
|
|
1445
|
+
exact,
|
|
1446
|
+
literalType,
|
|
1447
|
+
radix,
|
|
1448
|
+
suffix,
|
|
1449
|
+
text
|
|
1450
|
+
};
|
|
1451
|
+
}
|
|
1452
|
+
case "string":
|
|
1453
|
+
p.advance();
|
|
1454
|
+
return {
|
|
1455
|
+
kind: "StringLiteral",
|
|
1456
|
+
span: token.span,
|
|
1457
|
+
value: token.value
|
|
1458
|
+
};
|
|
1459
|
+
case "date":
|
|
1460
|
+
p.advance();
|
|
1461
|
+
return {
|
|
1462
|
+
kind: "DateLiteral",
|
|
1463
|
+
span: token.span,
|
|
1464
|
+
value: token.value,
|
|
1465
|
+
parts: token.parts
|
|
1466
|
+
};
|
|
1467
|
+
case "invalid":
|
|
1468
|
+
p.advance();
|
|
1469
|
+
return {
|
|
1470
|
+
kind: "InvalidExpression",
|
|
1471
|
+
span: token.span
|
|
1472
|
+
};
|
|
1473
|
+
case "word": return this.#word() ?? this.#missing();
|
|
1474
|
+
case "punct":
|
|
1475
|
+
if (token.value === "(") {
|
|
1476
|
+
p.advance();
|
|
1477
|
+
const expression = this.parse();
|
|
1478
|
+
p.expectPunct(")");
|
|
1479
|
+
return {
|
|
1480
|
+
kind: "Paren",
|
|
1481
|
+
span: p.spanFrom(token),
|
|
1482
|
+
expression
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
if (token.value === "." || token.value === "!") {
|
|
1486
|
+
p.advance();
|
|
1487
|
+
const member = this.memberName();
|
|
1488
|
+
return token.value === "." ? {
|
|
1489
|
+
kind: "MemberAccess",
|
|
1490
|
+
span: p.spanFrom(token),
|
|
1491
|
+
object: null,
|
|
1492
|
+
member
|
|
1493
|
+
} : {
|
|
1494
|
+
kind: "DictionaryAccess",
|
|
1495
|
+
span: p.spanFrom(token),
|
|
1496
|
+
object: null,
|
|
1497
|
+
key: member
|
|
1498
|
+
};
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
return this.#missing();
|
|
1502
|
+
}
|
|
1503
|
+
/** A literal keyword, `Me` or a name; null for a reserved word that cannot start an expression. */
|
|
1504
|
+
#word() {
|
|
1505
|
+
const p = this.p;
|
|
1506
|
+
const token = p.token;
|
|
1507
|
+
if (token.kind !== "word") return null;
|
|
1508
|
+
const { keyword, span } = token;
|
|
1509
|
+
if (keyword === "true" || keyword === "false") {
|
|
1510
|
+
p.advance();
|
|
1511
|
+
return {
|
|
1512
|
+
kind: "BooleanLiteral",
|
|
1513
|
+
span,
|
|
1514
|
+
value: keyword === "true"
|
|
1515
|
+
};
|
|
1516
|
+
}
|
|
1517
|
+
if (keyword === "nothing" || keyword === "empty" || keyword === "null") {
|
|
1518
|
+
p.advance();
|
|
1519
|
+
return {
|
|
1520
|
+
kind: keyword === "nothing" ? "NothingLiteral" : keyword === "empty" ? "EmptyLiteral" : "NullLiteral",
|
|
1521
|
+
span
|
|
1522
|
+
};
|
|
1523
|
+
}
|
|
1524
|
+
if (keyword === "me") {
|
|
1525
|
+
p.advance();
|
|
1526
|
+
return {
|
|
1527
|
+
kind: "Me",
|
|
1528
|
+
span
|
|
1529
|
+
};
|
|
1530
|
+
}
|
|
1531
|
+
if (keyword !== null && !EXPRESSION_NAMES.has(keyword)) return null;
|
|
1532
|
+
p.advance();
|
|
1533
|
+
return {
|
|
1534
|
+
kind: "Name",
|
|
1535
|
+
span,
|
|
1536
|
+
name: nameRefOf(token)
|
|
1537
|
+
};
|
|
1538
|
+
}
|
|
1539
|
+
#missing() {
|
|
1540
|
+
this.p.expected("expression");
|
|
1541
|
+
return {
|
|
1542
|
+
kind: "InvalidExpression",
|
|
1543
|
+
span: emptySpanAt(this.p.token.span.start)
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
/** The name after `.` or `!`: any word, keywords included (`Range.End`, `rs!Name`). */
|
|
1547
|
+
memberName() {
|
|
1548
|
+
const p = this.p;
|
|
1549
|
+
const token = p.token;
|
|
1550
|
+
if (token.kind === "word") {
|
|
1551
|
+
p.advance();
|
|
1552
|
+
return nameRefOf(token);
|
|
1553
|
+
}
|
|
1554
|
+
p.expected("identifier");
|
|
1555
|
+
return {
|
|
1556
|
+
kind: "NameRef",
|
|
1557
|
+
span: emptySpanAt(token.span.start),
|
|
1558
|
+
name: "",
|
|
1559
|
+
suffix: null,
|
|
1560
|
+
bracketed: false
|
|
1561
|
+
};
|
|
1562
|
+
}
|
|
1563
|
+
/** Parses a primary expression and its `.member`, `!key` and `(arguments)` suffixes. */
|
|
1564
|
+
postfix(options = {}) {
|
|
1565
|
+
return this.continuePostfix(this.#primary(), options);
|
|
1566
|
+
}
|
|
1567
|
+
/** Continues a postfix chain after `expression`. */
|
|
1568
|
+
continuePostfix(expression, options = {}) {
|
|
1569
|
+
const p = this.p;
|
|
1570
|
+
let result = expression;
|
|
1571
|
+
let links = 0;
|
|
1572
|
+
try {
|
|
1573
|
+
for (;;) {
|
|
1574
|
+
const token = p.token;
|
|
1575
|
+
if (token.kind !== "punct") return result;
|
|
1576
|
+
const spaced = token.spaceBefore && !token.afterContinuation;
|
|
1577
|
+
const member = (token.value === "." || token.value === "!") && !spaced;
|
|
1578
|
+
const call = token.value === "(" && !(options.stopAtSpacedParen && token.spaceBefore);
|
|
1579
|
+
if (!member && !call) return result;
|
|
1580
|
+
p.enter();
|
|
1581
|
+
links++;
|
|
1582
|
+
result = this.#link(result, token.value === "(" ? "(" : token.value === "." ? "." : "!");
|
|
1583
|
+
}
|
|
1584
|
+
} finally {
|
|
1585
|
+
p.depth -= links;
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
/** Parses one `.member`, `!key` or `(arguments)` after `result`. */
|
|
1589
|
+
#link(result, link) {
|
|
1590
|
+
const p = this.p;
|
|
1591
|
+
p.advance();
|
|
1592
|
+
if (link === "(") {
|
|
1593
|
+
const fileNumbers = result.kind === "Name" && FILE_NUMBER_FUNCTIONS$1.has(result.name.name.toLowerCase());
|
|
1594
|
+
const args = this.parser.arguments.list(true, fileNumbers);
|
|
1595
|
+
return {
|
|
1596
|
+
kind: "CallOrIndex",
|
|
1597
|
+
span: {
|
|
1598
|
+
start: result.span.start,
|
|
1599
|
+
end: p.previousEnd
|
|
1600
|
+
},
|
|
1601
|
+
target: result,
|
|
1602
|
+
args
|
|
1603
|
+
};
|
|
1604
|
+
}
|
|
1605
|
+
const member = this.memberName();
|
|
1606
|
+
const span = {
|
|
1607
|
+
start: result.span.start,
|
|
1608
|
+
end: p.previousEnd
|
|
1609
|
+
};
|
|
1610
|
+
return link === "." ? {
|
|
1611
|
+
kind: "MemberAccess",
|
|
1612
|
+
span,
|
|
1613
|
+
object: result,
|
|
1614
|
+
member
|
|
1615
|
+
} : {
|
|
1616
|
+
kind: "DictionaryAccess",
|
|
1617
|
+
span,
|
|
1618
|
+
object: result,
|
|
1619
|
+
key: member
|
|
1620
|
+
};
|
|
1621
|
+
}
|
|
1622
|
+
/** Parses a possibly qualified type name: `Long`, `Collection`, `Excel.Worksheet`. */
|
|
1623
|
+
typeRef() {
|
|
1624
|
+
const p = this.p;
|
|
1625
|
+
const start = p.token;
|
|
1626
|
+
const parts = [];
|
|
1627
|
+
for (;;) {
|
|
1628
|
+
const token = p.token;
|
|
1629
|
+
if (token.kind !== "word") {
|
|
1630
|
+
p.expected("identifier");
|
|
1631
|
+
break;
|
|
1632
|
+
}
|
|
1633
|
+
p.advance();
|
|
1634
|
+
parts.push(nameRefOf(token));
|
|
1635
|
+
if (!p.isPunct(".") || p.peek(1).kind !== "word") break;
|
|
1636
|
+
p.advance();
|
|
1637
|
+
}
|
|
1638
|
+
const first = parts[0];
|
|
1639
|
+
const builtin = parts.length === 1 && first && !first.bracketed ? BUILTIN_TYPES.get(first.name.toLowerCase()) ?? null : null;
|
|
1640
|
+
return {
|
|
1641
|
+
kind: "TypeRef",
|
|
1642
|
+
span: parts.length ? p.spanFrom(start) : emptySpanAt(start.span.start),
|
|
1643
|
+
name: parts.map((part) => part.name).join("."),
|
|
1644
|
+
parts,
|
|
1645
|
+
builtin
|
|
1646
|
+
};
|
|
1647
|
+
}
|
|
1648
|
+
};
|
|
1649
|
+
// src/lang/parser/file-statements.ts
|
|
1650
|
+
const OPEN_MODES = {
|
|
1651
|
+
append: "Append",
|
|
1652
|
+
binary: "Binary",
|
|
1653
|
+
input: "Input",
|
|
1654
|
+
output: "Output",
|
|
1655
|
+
random: "Random"
|
|
1656
|
+
};
|
|
1657
|
+
/** File statements ([MS-VBAL] 5.4.5): `Open`, `Close`, `Print #`, `Write #`, `Input #`, `Get`... */
|
|
1658
|
+
var FileStatementParser = class extends ParserPart {
|
|
1659
|
+
/** A file number, `#n` or `n`; the statement keeps the number expression without the `#`. */
|
|
1660
|
+
#fileNumber() {
|
|
1661
|
+
this.p.eatPunct("#");
|
|
1662
|
+
return this.#expression();
|
|
1663
|
+
}
|
|
1664
|
+
#expression() {
|
|
1665
|
+
return this.parser.expressions.parse();
|
|
1666
|
+
}
|
|
1667
|
+
/** Parses a `Print`/`Write` list: values, `Spc(n)`, `Tab[(n)]`, separated by `;` or `,`. */
|
|
1668
|
+
#outputList() {
|
|
1669
|
+
const p = this.p;
|
|
1670
|
+
const items = [];
|
|
1671
|
+
for (const loop = p.loop(); !p.atStatementEnd() && loop.continues();) {
|
|
1672
|
+
const start = p.token;
|
|
1673
|
+
const before = p.consumed;
|
|
1674
|
+
let form = "value";
|
|
1675
|
+
let value = null;
|
|
1676
|
+
if (p.isWord("spc") && p.isPunct("(", 1)) {
|
|
1677
|
+
p.advance();
|
|
1678
|
+
p.advance();
|
|
1679
|
+
form = "spc";
|
|
1680
|
+
value = this.#expression();
|
|
1681
|
+
p.expectPunct(")");
|
|
1682
|
+
} else if (p.isWord("tab")) {
|
|
1683
|
+
p.advance();
|
|
1684
|
+
form = "tab";
|
|
1685
|
+
if (p.eatPunct("(")) {
|
|
1686
|
+
value = this.#expression();
|
|
1687
|
+
p.expectPunct(")");
|
|
1688
|
+
}
|
|
1689
|
+
} else if (!p.isPunct(";") && !p.isPunct(",")) value = this.#expression();
|
|
1690
|
+
const separator = p.eatPunct(";") ? ";" : p.eatPunct(",") ? "," : null;
|
|
1691
|
+
items.push({
|
|
1692
|
+
kind: "OutputItem",
|
|
1693
|
+
span: p.spanFrom(start),
|
|
1694
|
+
form,
|
|
1695
|
+
value,
|
|
1696
|
+
separator
|
|
1697
|
+
});
|
|
1698
|
+
if (p.consumed === before) break;
|
|
1699
|
+
}
|
|
1700
|
+
return items;
|
|
1701
|
+
}
|
|
1702
|
+
#open() {
|
|
1703
|
+
const p = this.p;
|
|
1704
|
+
const start = p.advance();
|
|
1705
|
+
const path = this.#expression();
|
|
1706
|
+
let mode = null;
|
|
1707
|
+
if (p.eatWord("for")) {
|
|
1708
|
+
const token = p.token;
|
|
1709
|
+
mode = token.kind === "word" ? OPEN_MODES[token.lower] ?? null : null;
|
|
1710
|
+
if (mode) p.advance();
|
|
1711
|
+
else p.expected("Append or Binary or Input or Output or Random");
|
|
1712
|
+
}
|
|
1713
|
+
let access = null;
|
|
1714
|
+
if (p.eatWord("access")) {
|
|
1715
|
+
if (p.eatWord("read")) access = p.eatWord("write") ? "ReadWrite" : "Read";
|
|
1716
|
+
else if (p.eatWord("write")) access = "Write";
|
|
1717
|
+
else p.expected("Read or Write");
|
|
1718
|
+
}
|
|
1719
|
+
let lock = null;
|
|
1720
|
+
if (p.eatWord("shared")) lock = "Shared";
|
|
1721
|
+
else if (p.eatWord("lock")) {
|
|
1722
|
+
if (p.eatWord("read")) lock = p.eatWord("write") ? "LockReadWrite" : "LockRead";
|
|
1723
|
+
else if (p.eatWord("write")) lock = "LockWrite";
|
|
1724
|
+
else p.expected("Read or Write");
|
|
1725
|
+
}
|
|
1726
|
+
p.expectWord("as", "As");
|
|
1727
|
+
const fileNumber = this.#fileNumber();
|
|
1728
|
+
let recordLength = null;
|
|
1729
|
+
if (p.eatWord("len")) {
|
|
1730
|
+
p.expectPunct("=");
|
|
1731
|
+
recordLength = this.#expression();
|
|
1732
|
+
}
|
|
1733
|
+
return {
|
|
1734
|
+
kind: "Open",
|
|
1735
|
+
span: p.spanFrom(start),
|
|
1736
|
+
path,
|
|
1737
|
+
mode,
|
|
1738
|
+
access,
|
|
1739
|
+
lock,
|
|
1740
|
+
fileNumber,
|
|
1741
|
+
recordLength
|
|
1742
|
+
};
|
|
1743
|
+
}
|
|
1744
|
+
/** Parses the file statement whose keyword (`lower`) is current. */
|
|
1745
|
+
parse(lower) {
|
|
1746
|
+
const p = this.p;
|
|
1747
|
+
const start = p.token;
|
|
1748
|
+
switch (lower) {
|
|
1749
|
+
case "open": return this.#open();
|
|
1750
|
+
case "close": {
|
|
1751
|
+
p.advance();
|
|
1752
|
+
const fileNumbers = [];
|
|
1753
|
+
if (!p.atStatementEnd()) do
|
|
1754
|
+
fileNumbers.push(this.#fileNumber());
|
|
1755
|
+
while (p.eatPunct(","));
|
|
1756
|
+
return {
|
|
1757
|
+
kind: "Close",
|
|
1758
|
+
span: p.spanFrom(start),
|
|
1759
|
+
fileNumbers
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
case "print": {
|
|
1763
|
+
p.advance();
|
|
1764
|
+
if (!p.isPunct("#")) p.expected("#");
|
|
1765
|
+
const fileNumber = this.#fileNumber();
|
|
1766
|
+
const items = p.eatPunct(",") ? this.#outputList() : [];
|
|
1767
|
+
return {
|
|
1768
|
+
kind: "Print",
|
|
1769
|
+
span: p.spanFrom(start),
|
|
1770
|
+
channel: "file",
|
|
1771
|
+
fileNumber,
|
|
1772
|
+
items
|
|
1773
|
+
};
|
|
1774
|
+
}
|
|
1775
|
+
case "write": {
|
|
1776
|
+
p.advance();
|
|
1777
|
+
const fileNumber = this.#fileNumber();
|
|
1778
|
+
const items = p.eatPunct(",") ? this.#outputList() : [];
|
|
1779
|
+
return {
|
|
1780
|
+
kind: "Write",
|
|
1781
|
+
span: p.spanFrom(start),
|
|
1782
|
+
fileNumber,
|
|
1783
|
+
items
|
|
1784
|
+
};
|
|
1785
|
+
}
|
|
1786
|
+
case "input": {
|
|
1787
|
+
p.advance();
|
|
1788
|
+
const fileNumber = this.#fileNumber();
|
|
1789
|
+
p.expectPunct(",");
|
|
1790
|
+
const targets = [];
|
|
1791
|
+
do
|
|
1792
|
+
targets.push(this.parser.expressions.postfix());
|
|
1793
|
+
while (p.eatPunct(","));
|
|
1794
|
+
return {
|
|
1795
|
+
kind: "Input",
|
|
1796
|
+
span: p.spanFrom(start),
|
|
1797
|
+
fileNumber,
|
|
1798
|
+
targets
|
|
1799
|
+
};
|
|
1800
|
+
}
|
|
1801
|
+
case "line": {
|
|
1802
|
+
p.advance();
|
|
1803
|
+
p.expectWord("input", "Input");
|
|
1804
|
+
const fileNumber = this.#fileNumber();
|
|
1805
|
+
p.expectPunct(",");
|
|
1806
|
+
return {
|
|
1807
|
+
kind: "LineInput",
|
|
1808
|
+
span: p.spanFrom(start),
|
|
1809
|
+
fileNumber,
|
|
1810
|
+
target: this.parser.expressions.postfix()
|
|
1811
|
+
};
|
|
1812
|
+
}
|
|
1813
|
+
case "get":
|
|
1814
|
+
case "put": {
|
|
1815
|
+
p.advance();
|
|
1816
|
+
const fileNumber = this.#fileNumber();
|
|
1817
|
+
p.expectPunct(",");
|
|
1818
|
+
const recordNumber = p.isPunct(",") ? null : this.#expression();
|
|
1819
|
+
p.expectPunct(",");
|
|
1820
|
+
const target = this.#expression();
|
|
1821
|
+
return {
|
|
1822
|
+
kind: lower === "get" ? "Get" : "Put",
|
|
1823
|
+
span: p.spanFrom(start),
|
|
1824
|
+
fileNumber,
|
|
1825
|
+
recordNumber,
|
|
1826
|
+
target
|
|
1827
|
+
};
|
|
1828
|
+
}
|
|
1829
|
+
case "seek": {
|
|
1830
|
+
p.advance();
|
|
1831
|
+
const fileNumber = this.#fileNumber();
|
|
1832
|
+
p.expectPunct(",");
|
|
1833
|
+
return {
|
|
1834
|
+
kind: "Seek",
|
|
1835
|
+
span: p.spanFrom(start),
|
|
1836
|
+
fileNumber,
|
|
1837
|
+
position: this.#expression()
|
|
1838
|
+
};
|
|
1839
|
+
}
|
|
1840
|
+
case "lock":
|
|
1841
|
+
case "unlock": {
|
|
1842
|
+
p.advance();
|
|
1843
|
+
const fileNumber = this.#fileNumber();
|
|
1844
|
+
let from = null;
|
|
1845
|
+
let to = null;
|
|
1846
|
+
if (p.eatPunct(",")) {
|
|
1847
|
+
if (!p.isWord("to")) from = this.#expression();
|
|
1848
|
+
if (p.eatWord("to")) to = this.#expression();
|
|
1849
|
+
}
|
|
1850
|
+
return {
|
|
1851
|
+
kind: lower === "lock" ? "Lock" : "Unlock",
|
|
1852
|
+
span: p.spanFrom(start),
|
|
1853
|
+
fileNumber,
|
|
1854
|
+
from,
|
|
1855
|
+
to
|
|
1856
|
+
};
|
|
1857
|
+
}
|
|
1858
|
+
case "width": {
|
|
1859
|
+
p.advance();
|
|
1860
|
+
const fileNumber = this.#fileNumber();
|
|
1861
|
+
p.expectPunct(",");
|
|
1862
|
+
return {
|
|
1863
|
+
kind: "Width",
|
|
1864
|
+
span: p.spanFrom(start),
|
|
1865
|
+
fileNumber,
|
|
1866
|
+
width: this.#expression()
|
|
1867
|
+
};
|
|
1868
|
+
}
|
|
1869
|
+
}
|
|
1870
|
+
return this.parser.blocks.skipInvalid("Syntax error", "unexpected");
|
|
1871
|
+
}
|
|
1872
|
+
/** `Debug.Print items`, with `Debug` current. */
|
|
1873
|
+
debugPrint() {
|
|
1874
|
+
const p = this.p;
|
|
1875
|
+
const start = p.advance();
|
|
1876
|
+
p.advance();
|
|
1877
|
+
p.advance();
|
|
1878
|
+
return {
|
|
1879
|
+
kind: "Print",
|
|
1880
|
+
span: p.spanFrom(start),
|
|
1881
|
+
channel: "debug",
|
|
1882
|
+
fileNumber: null,
|
|
1883
|
+
items: this.#outputList()
|
|
1884
|
+
};
|
|
1885
|
+
}
|
|
1886
|
+
};
|
|
1887
|
+
// src/lang/parser/jumps.ts
|
|
1888
|
+
const EXIT_TARGETS = {
|
|
1889
|
+
do: "Do",
|
|
1890
|
+
for: "For",
|
|
1891
|
+
function: "Function",
|
|
1892
|
+
property: "Property",
|
|
1893
|
+
sub: "Sub"
|
|
1894
|
+
};
|
|
1895
|
+
/**
|
|
1896
|
+
* Jumps (`Exit`, `GoTo`, `GoSub`, `On ... GoTo`, `On Error`, `Resume`) and the simple keyword
|
|
1897
|
+
* statements (`ReDim`, `Erase`, `RaiseEvent`, `LSet`, `RSet`, `Set`, `Let`, `Name`, `Error`).
|
|
1898
|
+
*/
|
|
1899
|
+
var JumpParser = class extends ParserPart {
|
|
1900
|
+
/** Parses the statement `lower` starts, or returns null when it starts none of these. */
|
|
1901
|
+
parse(lower) {
|
|
1902
|
+
switch (lower) {
|
|
1903
|
+
case "exit": return this.#exit();
|
|
1904
|
+
case "goto": return this.#goTo("GoTo");
|
|
1905
|
+
case "gosub": return this.#goTo("GoSub");
|
|
1906
|
+
case "on": return this.#on();
|
|
1907
|
+
case "resume": return this.#resume();
|
|
1908
|
+
case "redim": return this.#reDim();
|
|
1909
|
+
case "erase": return this.#keywordAnd((start) => ({
|
|
1910
|
+
kind: "Erase",
|
|
1911
|
+
span: start.span,
|
|
1912
|
+
targets: this.#list(() => this.parser.expressions.postfix())
|
|
1913
|
+
}));
|
|
1914
|
+
case "raiseevent": return this.#keywordAnd((start) => {
|
|
1915
|
+
const name = this.parser.declarations.nameRef();
|
|
1916
|
+
const args = this.p.eatPunct("(") ? this.parser.arguments.list(true) : [];
|
|
1917
|
+
return {
|
|
1918
|
+
kind: "RaiseEvent",
|
|
1919
|
+
span: start.span,
|
|
1920
|
+
name,
|
|
1921
|
+
args
|
|
1922
|
+
};
|
|
1923
|
+
});
|
|
1924
|
+
case "lset":
|
|
1925
|
+
case "rset": return this.#keywordAnd((start) => {
|
|
1926
|
+
const { target, value } = this.#targetEqualsValue();
|
|
1927
|
+
return {
|
|
1928
|
+
kind: lower === "lset" ? "LSet" : "RSet",
|
|
1929
|
+
span: start.span,
|
|
1930
|
+
target,
|
|
1931
|
+
value
|
|
1932
|
+
};
|
|
1933
|
+
});
|
|
1934
|
+
case "set":
|
|
1935
|
+
case "let": return this.#keywordAnd((start) => ({
|
|
1936
|
+
kind: "Assignment",
|
|
1937
|
+
span: start.span,
|
|
1938
|
+
assignment: lower === "set" ? "Set" : "Let",
|
|
1939
|
+
explicitLet: lower === "let",
|
|
1940
|
+
...this.#targetEqualsValue()
|
|
1941
|
+
}));
|
|
1942
|
+
case "name": return this.#keywordAnd((start) => {
|
|
1943
|
+
const oldPath = this.parser.expressions.parse();
|
|
1944
|
+
this.p.expectWord("as", "As");
|
|
1945
|
+
const newPath = this.parser.expressions.parse();
|
|
1946
|
+
return {
|
|
1947
|
+
kind: "Name",
|
|
1948
|
+
span: start.span,
|
|
1949
|
+
oldPath,
|
|
1950
|
+
newPath
|
|
1951
|
+
};
|
|
1952
|
+
});
|
|
1953
|
+
case "error": return this.#keywordAnd((start) => ({
|
|
1954
|
+
kind: "Error",
|
|
1955
|
+
span: start.span,
|
|
1956
|
+
number: this.parser.expressions.parse()
|
|
1957
|
+
}));
|
|
1958
|
+
}
|
|
1959
|
+
return null;
|
|
1960
|
+
}
|
|
1961
|
+
/** Consumes the keyword, builds the statement, and gives it the span up to its last token. */
|
|
1962
|
+
#keywordAnd(build) {
|
|
1963
|
+
const p = this.p;
|
|
1964
|
+
const start = p.advance();
|
|
1965
|
+
return {
|
|
1966
|
+
...build(start),
|
|
1967
|
+
span: p.spanFrom(start)
|
|
1968
|
+
};
|
|
1969
|
+
}
|
|
1970
|
+
#list(item) {
|
|
1971
|
+
const items = [];
|
|
1972
|
+
do
|
|
1973
|
+
items.push(item());
|
|
1974
|
+
while (this.p.eatPunct(","));
|
|
1975
|
+
return items;
|
|
1976
|
+
}
|
|
1977
|
+
#targetEqualsValue() {
|
|
1978
|
+
const target = this.parser.expressions.postfix();
|
|
1979
|
+
this.p.expectPunct("=");
|
|
1980
|
+
return {
|
|
1981
|
+
target,
|
|
1982
|
+
value: this.parser.expressions.parse()
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1985
|
+
#exit() {
|
|
1986
|
+
const p = this.p;
|
|
1987
|
+
const start = p.advance();
|
|
1988
|
+
const token = p.token;
|
|
1989
|
+
const target = token.kind === "word" && !token.bracketed ? EXIT_TARGETS[token.lower] : void 0;
|
|
1990
|
+
if (target) p.advance();
|
|
1991
|
+
else p.expected("Do or For or Sub or Function or Property");
|
|
1992
|
+
return {
|
|
1993
|
+
kind: "Exit",
|
|
1994
|
+
span: p.spanFrom(start),
|
|
1995
|
+
target: target ?? "Sub"
|
|
1996
|
+
};
|
|
1997
|
+
}
|
|
1998
|
+
#goTo(kind) {
|
|
1999
|
+
const p = this.p;
|
|
2000
|
+
const start = p.advance();
|
|
2001
|
+
const label = this.parser.blocks.labelRef();
|
|
2002
|
+
return {
|
|
2003
|
+
kind,
|
|
2004
|
+
span: p.spanFrom(start),
|
|
2005
|
+
label
|
|
2006
|
+
};
|
|
2007
|
+
}
|
|
2008
|
+
#on() {
|
|
2009
|
+
const p = this.p;
|
|
2010
|
+
const start = p.advance();
|
|
2011
|
+
const local = p.eatWord("local") !== null;
|
|
2012
|
+
if (local || p.isWord("error")) {
|
|
2013
|
+
p.expectWord("error", "Error");
|
|
2014
|
+
const onError = (action, label = null) => ({
|
|
2015
|
+
kind: "OnError",
|
|
2016
|
+
span: p.spanFrom(start),
|
|
2017
|
+
local,
|
|
2018
|
+
action,
|
|
2019
|
+
label
|
|
2020
|
+
});
|
|
2021
|
+
if (p.eatWord("resume")) {
|
|
2022
|
+
p.expectWord("next", "Next");
|
|
2023
|
+
return onError("resumeNext");
|
|
2024
|
+
}
|
|
2025
|
+
p.expectWord("goto", "GoTo");
|
|
2026
|
+
const token = p.token;
|
|
2027
|
+
if (token.kind === "number" && token.value === 0 && token.integral) {
|
|
2028
|
+
p.advance();
|
|
2029
|
+
return onError("disable");
|
|
2030
|
+
}
|
|
2031
|
+
if (p.eatPunct("-")) {
|
|
2032
|
+
const one = p.token;
|
|
2033
|
+
if (one.kind === "number" && one.value === 1) p.advance();
|
|
2034
|
+
else p.expected("1");
|
|
2035
|
+
return onError("clear");
|
|
2036
|
+
}
|
|
2037
|
+
return onError("goto", this.parser.blocks.labelRef());
|
|
2038
|
+
}
|
|
2039
|
+
const selector = this.parser.expressions.parse();
|
|
2040
|
+
const gosub = p.isWord("gosub");
|
|
2041
|
+
if (!p.eatWord("goto") && !p.eatWord("gosub")) p.expected("GoTo or GoSub");
|
|
2042
|
+
const labels = this.#list(() => this.parser.blocks.labelRef());
|
|
2043
|
+
return {
|
|
2044
|
+
kind: "OnGoTo",
|
|
2045
|
+
span: p.spanFrom(start),
|
|
2046
|
+
selector,
|
|
2047
|
+
gosub,
|
|
2048
|
+
labels
|
|
2049
|
+
};
|
|
2050
|
+
}
|
|
2051
|
+
#resume() {
|
|
2052
|
+
const p = this.p;
|
|
2053
|
+
const start = p.advance();
|
|
2054
|
+
const resume = (action, label = null) => ({
|
|
2055
|
+
kind: "Resume",
|
|
2056
|
+
span: p.spanFrom(start),
|
|
2057
|
+
action,
|
|
2058
|
+
label
|
|
2059
|
+
});
|
|
2060
|
+
if (p.atStatementEnd()) return resume("retry");
|
|
2061
|
+
if (p.eatWord("next")) return resume("next");
|
|
2062
|
+
const token = p.token;
|
|
2063
|
+
if (token.kind === "number" && token.value === 0 && token.integral) {
|
|
2064
|
+
p.advance();
|
|
2065
|
+
return resume("retry");
|
|
2066
|
+
}
|
|
2067
|
+
return resume("label", this.parser.blocks.labelRef());
|
|
2068
|
+
}
|
|
2069
|
+
/** The array a `ReDim` names: a variable, or a member of one, before its bounds. */
|
|
2070
|
+
#reDimTarget() {
|
|
2071
|
+
const p = this.p;
|
|
2072
|
+
const expressions = this.parser.expressions;
|
|
2073
|
+
const token = p.token;
|
|
2074
|
+
let target;
|
|
2075
|
+
if (p.eatPunct(".")) {
|
|
2076
|
+
const member = expressions.memberName();
|
|
2077
|
+
target = {
|
|
2078
|
+
kind: "MemberAccess",
|
|
2079
|
+
span: p.spanFrom(token),
|
|
2080
|
+
object: null,
|
|
2081
|
+
member
|
|
2082
|
+
};
|
|
2083
|
+
} else {
|
|
2084
|
+
const name = this.parser.declarations.nameRef();
|
|
2085
|
+
target = {
|
|
2086
|
+
kind: "Name",
|
|
2087
|
+
span: name.span,
|
|
2088
|
+
name
|
|
2089
|
+
};
|
|
2090
|
+
}
|
|
2091
|
+
while (p.eatPunct(".")) {
|
|
2092
|
+
const member = expressions.memberName();
|
|
2093
|
+
target = {
|
|
2094
|
+
kind: "MemberAccess",
|
|
2095
|
+
span: p.spanFrom(token),
|
|
2096
|
+
object: target,
|
|
2097
|
+
member
|
|
2098
|
+
};
|
|
2099
|
+
}
|
|
2100
|
+
return target;
|
|
2101
|
+
}
|
|
2102
|
+
#reDim() {
|
|
2103
|
+
const p = this.p;
|
|
2104
|
+
const declarations = this.parser.declarations;
|
|
2105
|
+
const start = p.advance();
|
|
2106
|
+
const preserve = p.eatWord("preserve") !== null;
|
|
2107
|
+
const clauses = this.#list(() => {
|
|
2108
|
+
const begin = p.token;
|
|
2109
|
+
const target = this.#reDimTarget();
|
|
2110
|
+
const bounds = p.expectPunct("(") ? declarations.bounds() : [];
|
|
2111
|
+
const asClause = declarations.asClause();
|
|
2112
|
+
return {
|
|
2113
|
+
kind: "ReDimClause",
|
|
2114
|
+
span: p.spanFrom(begin),
|
|
2115
|
+
target,
|
|
2116
|
+
bounds,
|
|
2117
|
+
asClause
|
|
2118
|
+
};
|
|
2119
|
+
});
|
|
2120
|
+
return {
|
|
2121
|
+
kind: "ReDim",
|
|
2122
|
+
span: p.spanFrom(start),
|
|
2123
|
+
preserve,
|
|
2124
|
+
clauses
|
|
2125
|
+
};
|
|
2126
|
+
}
|
|
2127
|
+
};
|
|
2128
|
+
// src/lang/parser/loops.ts
|
|
2129
|
+
/** A comparable key for a loop variable (`i`, `Me.i`, `obj.Count`), or null when it has none. */
|
|
2130
|
+
function variableKey(expression) {
|
|
2131
|
+
switch (expression.kind) {
|
|
2132
|
+
case "Name": return expression.name.name.toLowerCase();
|
|
2133
|
+
case "Me": return "me";
|
|
2134
|
+
case "MemberAccess": {
|
|
2135
|
+
const object = expression.object ? variableKey(expression.object) : "";
|
|
2136
|
+
return object === null ? null : `${object}.${expression.member.name.toLowerCase()}`;
|
|
2137
|
+
}
|
|
2138
|
+
default: return null;
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
/** `For`, `For Each`, `Do`, `While` and `With` blocks ([MS-VBAL] 5.4.2.3 to 5.4.2.7, 5.4.2.21). */
|
|
2142
|
+
var LoopParser = class extends ParserPart {
|
|
2143
|
+
parseFor() {
|
|
2144
|
+
const p = this.p;
|
|
2145
|
+
const expressions = this.parser.expressions;
|
|
2146
|
+
const start = p.advance();
|
|
2147
|
+
if (p.eatWord("each")) {
|
|
2148
|
+
const element = expressions.postfix();
|
|
2149
|
+
p.expectWord("in", "In");
|
|
2150
|
+
const collection = expressions.parse();
|
|
2151
|
+
const body = this.parser.blocks.parse("for");
|
|
2152
|
+
const next = this.#next(start, element);
|
|
2153
|
+
return {
|
|
2154
|
+
kind: "ForEach",
|
|
2155
|
+
span: p.spanFrom(start),
|
|
2156
|
+
element,
|
|
2157
|
+
collection,
|
|
2158
|
+
body,
|
|
2159
|
+
nextCounter: next.counter,
|
|
2160
|
+
sharedNext: next.shared
|
|
2161
|
+
};
|
|
2162
|
+
}
|
|
2163
|
+
const counter = expressions.postfix();
|
|
2164
|
+
p.expectPunct("=");
|
|
2165
|
+
const from = expressions.parse();
|
|
2166
|
+
p.expectWord("to", "To");
|
|
2167
|
+
const to = expressions.parse();
|
|
2168
|
+
const step = p.eatWord("step") ? expressions.parse() : null;
|
|
2169
|
+
const body = this.parser.blocks.parse("for");
|
|
2170
|
+
const next = this.#next(start, counter);
|
|
2171
|
+
return {
|
|
2172
|
+
kind: "For",
|
|
2173
|
+
span: p.spanFrom(start),
|
|
2174
|
+
counter,
|
|
2175
|
+
start: from,
|
|
2176
|
+
end: to,
|
|
2177
|
+
step,
|
|
2178
|
+
body,
|
|
2179
|
+
nextCounter: next.counter,
|
|
2180
|
+
sharedNext: next.shared
|
|
2181
|
+
};
|
|
2182
|
+
}
|
|
2183
|
+
/**
|
|
2184
|
+
* Consumes the `Next [counter]` that closes a `For`. In `Next j, i` the comma closes the
|
|
2185
|
+
* outer loop: the cursor reads a `Next` in its place (the token list is not changed). A
|
|
2186
|
+
* counter that is not the loop's is VBA's "Invalid Next control variable reference".
|
|
2187
|
+
*/
|
|
2188
|
+
#next(start, loopCounter) {
|
|
2189
|
+
const p = this.p;
|
|
2190
|
+
if (terminatorAt(p) !== "next") {
|
|
2191
|
+
p.error("block-mismatch", "For without Next", start.span);
|
|
2192
|
+
return {
|
|
2193
|
+
counter: null,
|
|
2194
|
+
shared: false
|
|
2195
|
+
};
|
|
2196
|
+
}
|
|
2197
|
+
const shared = p.advance().synthetic === true;
|
|
2198
|
+
if (p.atStatementEnd()) return {
|
|
2199
|
+
counter: null,
|
|
2200
|
+
shared
|
|
2201
|
+
};
|
|
2202
|
+
const counter = this.parser.expressions.postfix();
|
|
2203
|
+
const expected = variableKey(loopCounter);
|
|
2204
|
+
const actual = variableKey(counter);
|
|
2205
|
+
if (expected !== null && actual !== null && expected !== actual) p.error("block-mismatch", "Invalid Next control variable reference", counter.span);
|
|
2206
|
+
const comma = p.token;
|
|
2207
|
+
if (comma.kind === "punct" && comma.value === ",") {
|
|
2208
|
+
p.advance();
|
|
2209
|
+
const { text, span, spaceBefore, afterContinuation } = comma;
|
|
2210
|
+
p.readNext({
|
|
2211
|
+
kind: "word",
|
|
2212
|
+
text,
|
|
2213
|
+
span,
|
|
2214
|
+
spaceBefore,
|
|
2215
|
+
afterContinuation,
|
|
2216
|
+
lineStart: false,
|
|
2217
|
+
name: "Next",
|
|
2218
|
+
lower: "next",
|
|
2219
|
+
suffix: null,
|
|
2220
|
+
bracketed: false,
|
|
2221
|
+
keyword: "next",
|
|
2222
|
+
synthetic: true
|
|
2223
|
+
});
|
|
2224
|
+
}
|
|
2225
|
+
return {
|
|
2226
|
+
counter,
|
|
2227
|
+
shared
|
|
2228
|
+
};
|
|
2229
|
+
}
|
|
2230
|
+
#condition(position) {
|
|
2231
|
+
const p = this.p;
|
|
2232
|
+
const token = p.token;
|
|
2233
|
+
const mode = p.isWord("while") ? "While" : p.isWord("until") ? "Until" : null;
|
|
2234
|
+
if (!mode) return null;
|
|
2235
|
+
p.advance();
|
|
2236
|
+
const expression = this.parser.expressions.parse();
|
|
2237
|
+
return {
|
|
2238
|
+
kind: "LoopCondition",
|
|
2239
|
+
span: p.spanFrom(token),
|
|
2240
|
+
mode,
|
|
2241
|
+
position,
|
|
2242
|
+
expression
|
|
2243
|
+
};
|
|
2244
|
+
}
|
|
2245
|
+
parseDo() {
|
|
2246
|
+
const p = this.p;
|
|
2247
|
+
const start = p.advance();
|
|
2248
|
+
let condition = this.#condition("pre");
|
|
2249
|
+
const body = this.parser.blocks.parse("do");
|
|
2250
|
+
if (terminatorAt(p) === "loop") {
|
|
2251
|
+
const loop = p.advance();
|
|
2252
|
+
const post = this.#condition("post");
|
|
2253
|
+
if (post && condition) p.error("unexpected", "Do and Loop cannot both have a condition", loop.span);
|
|
2254
|
+
condition ??= post;
|
|
2255
|
+
} else p.error("block-mismatch", "Do without Loop", start.span);
|
|
2256
|
+
return {
|
|
2257
|
+
kind: "DoLoop",
|
|
2258
|
+
span: p.spanFrom(start),
|
|
2259
|
+
condition,
|
|
2260
|
+
body
|
|
2261
|
+
};
|
|
2262
|
+
}
|
|
2263
|
+
parseWhile() {
|
|
2264
|
+
const p = this.p;
|
|
2265
|
+
const start = p.advance();
|
|
2266
|
+
const condition = this.parser.expressions.parse();
|
|
2267
|
+
const body = this.parser.blocks.parse("while");
|
|
2268
|
+
if (terminatorAt(p) === "wend") p.advance();
|
|
2269
|
+
else p.error("block-mismatch", "While without Wend", start.span);
|
|
2270
|
+
return {
|
|
2271
|
+
kind: "While",
|
|
2272
|
+
span: p.spanFrom(start),
|
|
2273
|
+
condition,
|
|
2274
|
+
body
|
|
2275
|
+
};
|
|
2276
|
+
}
|
|
2277
|
+
parseWith() {
|
|
2278
|
+
const p = this.p;
|
|
2279
|
+
const start = p.advance();
|
|
2280
|
+
const object = this.parser.expressions.parse();
|
|
2281
|
+
const body = this.parser.blocks.parse("with");
|
|
2282
|
+
if (!this.parser.blocks.eatEnd("endwith")) p.error("block-mismatch", "With without End With", start.span);
|
|
2283
|
+
return {
|
|
2284
|
+
kind: "With",
|
|
2285
|
+
span: p.spanFrom(start),
|
|
2286
|
+
object,
|
|
2287
|
+
body
|
|
2288
|
+
};
|
|
2289
|
+
}
|
|
2290
|
+
};
|
|
2291
|
+
// src/lang/parser/module-declarations.ts
|
|
2292
|
+
const DEF_TYPES = {
|
|
2293
|
+
defbool: "Boolean",
|
|
2294
|
+
defbyte: "Byte",
|
|
2295
|
+
defcur: "Currency",
|
|
2296
|
+
defdate: "Date",
|
|
2297
|
+
defdbl: "Double",
|
|
2298
|
+
defint: "Integer",
|
|
2299
|
+
deflng: "Long",
|
|
2300
|
+
deflnglng: "LongLong",
|
|
2301
|
+
deflngptr: "LongPtr",
|
|
2302
|
+
defobj: "Object",
|
|
2303
|
+
defsng: "Single",
|
|
2304
|
+
defstr: "String",
|
|
2305
|
+
defvar: "Variant",
|
|
2306
|
+
defdec: "Decimal"
|
|
2307
|
+
};
|
|
2308
|
+
/** Whether `lower` is a `DefType` keyword (`DefInt`, `DefStr`, ...). */
|
|
2309
|
+
function isDefType(lower) {
|
|
2310
|
+
return lower in DEF_TYPES;
|
|
2311
|
+
}
|
|
2312
|
+
/** Module-level declarations: `Type`, `Enum`, `Declare`, `Event`, `Implements`, `DefType`, `Option`. */
|
|
2313
|
+
var ModuleDeclarationParser = class extends ParserPart {
|
|
2314
|
+
typeDeclaration(start, scope) {
|
|
2315
|
+
const p = this.p;
|
|
2316
|
+
const declarations = this.parser.declarations;
|
|
2317
|
+
const name = this.#header();
|
|
2318
|
+
const members = [];
|
|
2319
|
+
for (const loop = p.loop(); loop.continues();) {
|
|
2320
|
+
if (this.#atEnd("endtype", start, "Expected: End Type")) break;
|
|
2321
|
+
const begin = p.token;
|
|
2322
|
+
const memberName = declarations.nameRef();
|
|
2323
|
+
let dimensions = null;
|
|
2324
|
+
if (p.isPunct("(")) {
|
|
2325
|
+
const open = p.advance();
|
|
2326
|
+
const bounds = declarations.bounds();
|
|
2327
|
+
dimensions = {
|
|
2328
|
+
kind: "ArrayDimensions",
|
|
2329
|
+
span: p.spanFrom(open),
|
|
2330
|
+
bounds
|
|
2331
|
+
};
|
|
2332
|
+
}
|
|
2333
|
+
const asClause = declarations.asClause();
|
|
2334
|
+
if (!asClause) p.expected("As");
|
|
2335
|
+
const span = p.spanFrom(begin);
|
|
2336
|
+
members.push({
|
|
2337
|
+
kind: "TypeMember",
|
|
2338
|
+
span,
|
|
2339
|
+
name: memberName,
|
|
2340
|
+
dimensions,
|
|
2341
|
+
asClause
|
|
2342
|
+
});
|
|
2343
|
+
this.#endMemberLine();
|
|
2344
|
+
}
|
|
2345
|
+
return {
|
|
2346
|
+
kind: "TypeDeclaration",
|
|
2347
|
+
span: p.spanFrom(start),
|
|
2348
|
+
scope,
|
|
2349
|
+
name,
|
|
2350
|
+
members
|
|
2351
|
+
};
|
|
2352
|
+
}
|
|
2353
|
+
enumDeclaration(start, scope) {
|
|
2354
|
+
const p = this.p;
|
|
2355
|
+
const name = this.#header();
|
|
2356
|
+
const members = [];
|
|
2357
|
+
for (const loop = p.loop(); loop.continues();) {
|
|
2358
|
+
if (this.#atEnd("endenum", start, "Expected: End Enum")) break;
|
|
2359
|
+
const begin = p.token;
|
|
2360
|
+
const memberName = this.parser.declarations.nameRef();
|
|
2361
|
+
const value = p.eatPunct("=") ? this.parser.expressions.parse() : null;
|
|
2362
|
+
members.push({
|
|
2363
|
+
kind: "EnumMember",
|
|
2364
|
+
span: p.spanFrom(begin),
|
|
2365
|
+
name: memberName,
|
|
2366
|
+
value
|
|
2367
|
+
});
|
|
2368
|
+
this.#endMemberLine();
|
|
2369
|
+
}
|
|
2370
|
+
return {
|
|
2371
|
+
kind: "EnumDeclaration",
|
|
2372
|
+
span: p.spanFrom(start),
|
|
2373
|
+
scope,
|
|
2374
|
+
name,
|
|
2375
|
+
members
|
|
2376
|
+
};
|
|
2377
|
+
}
|
|
2378
|
+
/** The `Type` or `Enum` keyword and name, and the rest of that line. */
|
|
2379
|
+
#header() {
|
|
2380
|
+
this.p.advance();
|
|
2381
|
+
const name = this.parser.declarations.nameRef();
|
|
2382
|
+
this.#endMemberLine();
|
|
2383
|
+
return name;
|
|
2384
|
+
}
|
|
2385
|
+
/** Consumes the `End Type`/`End Enum`, or reports a block that runs into a procedure or the end. */
|
|
2386
|
+
#atEnd(terminator, start, message) {
|
|
2387
|
+
const p = this.p;
|
|
2388
|
+
if (this.parser.blocks.eatEnd(terminator)) return true;
|
|
2389
|
+
if (p.token.kind !== "eof" && terminatorAt(p) !== "procedure") return false;
|
|
2390
|
+
p.error("block-mismatch", message, start.span);
|
|
2391
|
+
return true;
|
|
2392
|
+
}
|
|
2393
|
+
/** Reports what is left on a member line of a `Type` or `Enum` and skips the separators. */
|
|
2394
|
+
#endMemberLine() {
|
|
2395
|
+
const p = this.p;
|
|
2396
|
+
if (!p.atStatementEnd()) {
|
|
2397
|
+
p.expected("end of statement");
|
|
2398
|
+
p.skipToStatementEnd();
|
|
2399
|
+
}
|
|
2400
|
+
p.skipSeparators();
|
|
2401
|
+
}
|
|
2402
|
+
#stringConstant(what) {
|
|
2403
|
+
const p = this.p;
|
|
2404
|
+
const token = p.token;
|
|
2405
|
+
if (token.kind === "string") {
|
|
2406
|
+
p.advance();
|
|
2407
|
+
return token.value;
|
|
2408
|
+
}
|
|
2409
|
+
p.expected(what);
|
|
2410
|
+
return "";
|
|
2411
|
+
}
|
|
2412
|
+
declare(start, scope) {
|
|
2413
|
+
const p = this.p;
|
|
2414
|
+
const declarations = this.parser.declarations;
|
|
2415
|
+
p.advance();
|
|
2416
|
+
const ptrSafe = p.eatWord("ptrsafe") !== null;
|
|
2417
|
+
const procedureKind = p.eatWord("sub") ? "Sub" : p.eatWord("function") ? "Function" : null;
|
|
2418
|
+
if (!procedureKind) p.expected("Sub or Function");
|
|
2419
|
+
const name = declarations.nameRef();
|
|
2420
|
+
const cdecl = p.eatWord("cdecl") !== null;
|
|
2421
|
+
p.expectWord("lib", "Lib");
|
|
2422
|
+
const lib = this.#stringConstant("string constant");
|
|
2423
|
+
const alias = p.eatWord("alias") ? this.#stringConstant("string constant") : null;
|
|
2424
|
+
const parameters = declarations.parameters();
|
|
2425
|
+
const returnType = declarations.asClause();
|
|
2426
|
+
return {
|
|
2427
|
+
kind: "DeclareStatement",
|
|
2428
|
+
span: p.spanFrom(start),
|
|
2429
|
+
scope,
|
|
2430
|
+
ptrSafe,
|
|
2431
|
+
cdecl,
|
|
2432
|
+
procedureKind: procedureKind ?? "Sub",
|
|
2433
|
+
name,
|
|
2434
|
+
lib,
|
|
2435
|
+
alias,
|
|
2436
|
+
parameters,
|
|
2437
|
+
returnType
|
|
2438
|
+
};
|
|
2439
|
+
}
|
|
2440
|
+
event(start, scope) {
|
|
2441
|
+
const p = this.p;
|
|
2442
|
+
p.advance();
|
|
2443
|
+
const name = this.parser.declarations.nameRef();
|
|
2444
|
+
const parameters = this.parser.declarations.parameters();
|
|
2445
|
+
return {
|
|
2446
|
+
kind: "EventDeclaration",
|
|
2447
|
+
span: p.spanFrom(start),
|
|
2448
|
+
scope,
|
|
2449
|
+
name,
|
|
2450
|
+
parameters
|
|
2451
|
+
};
|
|
2452
|
+
}
|
|
2453
|
+
implementsStatement() {
|
|
2454
|
+
const p = this.p;
|
|
2455
|
+
const start = p.advance();
|
|
2456
|
+
const type = this.parser.expressions.typeRef();
|
|
2457
|
+
return {
|
|
2458
|
+
kind: "ImplementsStatement",
|
|
2459
|
+
span: p.spanFrom(start),
|
|
2460
|
+
type
|
|
2461
|
+
};
|
|
2462
|
+
}
|
|
2463
|
+
defType() {
|
|
2464
|
+
const p = this.p;
|
|
2465
|
+
const start = p.advance();
|
|
2466
|
+
const type = DEF_TYPES[start.kind === "word" ? start.lower : ""] ?? "Variant";
|
|
2467
|
+
const ranges = [];
|
|
2468
|
+
const letter = () => {
|
|
2469
|
+
const token = p.token;
|
|
2470
|
+
if (token.kind === "word" && token.name.length === 1 && /[A-Za-z]/.test(token.name)) {
|
|
2471
|
+
p.advance();
|
|
2472
|
+
return token.name.toUpperCase();
|
|
2473
|
+
}
|
|
2474
|
+
p.expected("letter");
|
|
2475
|
+
return "A";
|
|
2476
|
+
};
|
|
2477
|
+
do {
|
|
2478
|
+
const begin = p.token;
|
|
2479
|
+
const from = letter();
|
|
2480
|
+
const to = p.eatPunct("-") ? letter() : from;
|
|
2481
|
+
ranges.push({
|
|
2482
|
+
kind: "LetterRange",
|
|
2483
|
+
span: p.spanFrom(begin),
|
|
2484
|
+
from,
|
|
2485
|
+
to
|
|
2486
|
+
});
|
|
2487
|
+
} while (p.eatPunct(","));
|
|
2488
|
+
return {
|
|
2489
|
+
kind: "DefTypeStatement",
|
|
2490
|
+
span: p.spanFrom(start),
|
|
2491
|
+
type,
|
|
2492
|
+
ranges
|
|
2493
|
+
};
|
|
2494
|
+
}
|
|
2495
|
+
option() {
|
|
2496
|
+
const p = this.p;
|
|
2497
|
+
const start = p.advance();
|
|
2498
|
+
if (p.eatWord("explicit")) return {
|
|
2499
|
+
kind: "OptionExplicit",
|
|
2500
|
+
span: p.spanFrom(start)
|
|
2501
|
+
};
|
|
2502
|
+
if (p.eatWord("base")) {
|
|
2503
|
+
const token = p.token;
|
|
2504
|
+
const base = token.kind === "number" && token.integral ? token.value : -1;
|
|
2505
|
+
if (base === 0 || base === 1) p.advance();
|
|
2506
|
+
else p.expected("0 or 1");
|
|
2507
|
+
return {
|
|
2508
|
+
kind: "OptionBase",
|
|
2509
|
+
span: p.spanFrom(start),
|
|
2510
|
+
base: base === 1 ? 1 : 0
|
|
2511
|
+
};
|
|
2512
|
+
}
|
|
2513
|
+
if (p.eatWord("compare")) {
|
|
2514
|
+
const mode = p.eatWord("binary") ? "Binary" : p.eatWord("text") ? "Text" : p.eatWord("database") ? "Database" : null;
|
|
2515
|
+
if (!mode) p.expected("Binary or Text");
|
|
2516
|
+
return {
|
|
2517
|
+
kind: "OptionCompare",
|
|
2518
|
+
span: p.spanFrom(start),
|
|
2519
|
+
mode: mode ?? "Binary"
|
|
2520
|
+
};
|
|
2521
|
+
}
|
|
2522
|
+
if (p.eatWord("private")) {
|
|
2523
|
+
p.expectWord("module", "Module");
|
|
2524
|
+
return {
|
|
2525
|
+
kind: "OptionPrivateModule",
|
|
2526
|
+
span: p.spanFrom(start)
|
|
2527
|
+
};
|
|
2528
|
+
}
|
|
2529
|
+
p.expected("Base or Compare or Explicit or Private");
|
|
2530
|
+
return {
|
|
2531
|
+
kind: "OptionExplicit",
|
|
2532
|
+
span: p.spanFrom(start)
|
|
2533
|
+
};
|
|
2534
|
+
}
|
|
2535
|
+
};
|
|
2536
|
+
// src/lang/parser/module.ts
|
|
2537
|
+
const SCOPES = {
|
|
2538
|
+
public: "Public",
|
|
2539
|
+
private: "Private",
|
|
2540
|
+
global: "Global",
|
|
2541
|
+
friend: "Friend"
|
|
2542
|
+
};
|
|
2543
|
+
const OUTSIDE = "Invalid outside procedure";
|
|
2544
|
+
/** The module body ([MS-VBAL] 4.2): declarations, options, attributes and procedures. */
|
|
2545
|
+
var ModuleParser = class extends ParserPart {
|
|
2546
|
+
/** Parses the members of a module up to the end of the tokens. */
|
|
2547
|
+
parseMembers() {
|
|
2548
|
+
const p = this.p;
|
|
2549
|
+
const members = [];
|
|
2550
|
+
for (const loop = p.loop(); loop.continues();) {
|
|
2551
|
+
p.skipSeparators();
|
|
2552
|
+
if (p.token.kind === "eof") break;
|
|
2553
|
+
const start = p.token;
|
|
2554
|
+
try {
|
|
2555
|
+
const member = this.#member();
|
|
2556
|
+
if (member) members.push(member);
|
|
2557
|
+
} catch (error) {
|
|
2558
|
+
if (!(error instanceof NestingLimit)) throw error;
|
|
2559
|
+
this.parser.blocks.skipInvalid("Expression too complex", "too-complex", start);
|
|
2560
|
+
}
|
|
2561
|
+
if (!p.atStatementEnd() && terminatorAt(p) !== "procedure") {
|
|
2562
|
+
p.expected("end of statement");
|
|
2563
|
+
p.skipStatement();
|
|
2564
|
+
}
|
|
2565
|
+
}
|
|
2566
|
+
return members;
|
|
2567
|
+
}
|
|
2568
|
+
/** Parses one module-level item, or reports it and returns null. */
|
|
2569
|
+
#member() {
|
|
2570
|
+
const p = this.p;
|
|
2571
|
+
const { blocks, declarations, moduleDeclarations } = this.parser;
|
|
2572
|
+
const start = p.token;
|
|
2573
|
+
const invalid = (message, code) => {
|
|
2574
|
+
blocks.skipInvalid(message, code, start);
|
|
2575
|
+
return null;
|
|
2576
|
+
};
|
|
2577
|
+
if (start.kind !== "word" || start.bracketed || blocks.labelStartsHere()) return invalid(OUTSIDE, "invalid-outside-procedure");
|
|
2578
|
+
const terminator = terminatorAt(p);
|
|
2579
|
+
if (terminator && terminator !== "procedure") return invalid(strayMessage(terminator), "block-mismatch");
|
|
2580
|
+
let scope = null;
|
|
2581
|
+
let isStatic = false;
|
|
2582
|
+
for (let i = 0; i < 2; i++) {
|
|
2583
|
+
const word = p.token;
|
|
2584
|
+
if (word.kind !== "word" || word.bracketed) break;
|
|
2585
|
+
if (scope === null && word.lower in SCOPES) {
|
|
2586
|
+
scope = SCOPES[word.lower] ?? null;
|
|
2587
|
+
p.advance();
|
|
2588
|
+
} else if (!isStatic && word.lower === "static" && procedureStartsHere(p)) {
|
|
2589
|
+
isStatic = true;
|
|
2590
|
+
p.advance();
|
|
2591
|
+
} else break;
|
|
2592
|
+
}
|
|
2593
|
+
const token = p.token;
|
|
2594
|
+
const lower = token.kind === "word" && !token.bracketed ? token.lower : "";
|
|
2595
|
+
if (lower === "sub" || lower === "function" || lower === "property" && procedureStartsHere(p)) {
|
|
2596
|
+
const procedureScope = scope === "Global" ? "Public" : scope;
|
|
2597
|
+
return this.parser.procedures.parse(start, procedureScope, isStatic);
|
|
2598
|
+
}
|
|
2599
|
+
switch (lower) {
|
|
2600
|
+
case "dim":
|
|
2601
|
+
p.advance();
|
|
2602
|
+
return declarations.variables(start, "Dim");
|
|
2603
|
+
case "const": return declarations.constants(start, scope);
|
|
2604
|
+
case "type": return moduleDeclarations.typeDeclaration(start, scope);
|
|
2605
|
+
case "enum": return moduleDeclarations.enumDeclaration(start, scope);
|
|
2606
|
+
case "declare": return moduleDeclarations.declare(start, scope);
|
|
2607
|
+
case "event": return moduleDeclarations.event(start, scope);
|
|
2608
|
+
case "implements": return moduleDeclarations.implementsStatement();
|
|
2609
|
+
case "option": return moduleDeclarations.option();
|
|
2610
|
+
case "attribute": return declarations.attribute();
|
|
2611
|
+
case "static": return invalid(OUTSIDE, "invalid-outside-procedure");
|
|
2612
|
+
}
|
|
2613
|
+
if (scope !== null && scope !== "Friend") return declarations.variables(start, scope);
|
|
2614
|
+
if (isDefType(lower)) return moduleDeclarations.defType();
|
|
2615
|
+
return invalid(OUTSIDE, "invalid-outside-procedure");
|
|
2616
|
+
}
|
|
2617
|
+
};
|
|
2618
|
+
// src/lang/parser/procedures.ts
|
|
2619
|
+
const END_OF = {
|
|
2620
|
+
Sub: "endsub",
|
|
2621
|
+
Function: "endfunction",
|
|
2622
|
+
Property: "endproperty"
|
|
2623
|
+
};
|
|
2624
|
+
/** `Sub`, `Function` and `Property` procedures ([MS-VBAL] 5.3.1). */
|
|
2625
|
+
var ProcedureParser = class extends ParserPart {
|
|
2626
|
+
/** Parses a procedure whose `Sub`, `Function` or `Property` keyword is current. */
|
|
2627
|
+
parse(start, scope, isStatic) {
|
|
2628
|
+
const p = this.p;
|
|
2629
|
+
const { declarations, blocks } = this.parser;
|
|
2630
|
+
const keyword = p.advance();
|
|
2631
|
+
let procedureKind = "Sub";
|
|
2632
|
+
let family = "Sub";
|
|
2633
|
+
if (keyword.kind === "word" && keyword.lower === "function") procedureKind = family = "Function";
|
|
2634
|
+
else if (keyword.kind === "word" && keyword.lower === "property") {
|
|
2635
|
+
family = "Property";
|
|
2636
|
+
const accessor = p.advance();
|
|
2637
|
+
procedureKind = accessor.kind === "word" && accessor.lower === "let" ? "PropertyLet" : accessor.kind === "word" && accessor.lower === "set" ? "PropertySet" : "PropertyGet";
|
|
2638
|
+
}
|
|
2639
|
+
const name = declarations.nameRef("identifier");
|
|
2640
|
+
const parameters = declarations.parameters();
|
|
2641
|
+
const returnType = declarations.asClause();
|
|
2642
|
+
if (returnType && (procedureKind === "Sub" || procedureKind === "PropertyLet" || procedureKind === "PropertySet")) p.error("unexpected", "Expected: end of statement", returnType.span);
|
|
2643
|
+
if (!p.atStatementEnd()) {
|
|
2644
|
+
p.expected("end of statement");
|
|
2645
|
+
p.skipToStatementEnd();
|
|
2646
|
+
}
|
|
2647
|
+
const statements = blocks.parse("procedure");
|
|
2648
|
+
let endSpan = null;
|
|
2649
|
+
const terminator = terminatorAt(p);
|
|
2650
|
+
if (terminator === "endsub" || terminator === "endfunction" || terminator === "endproperty") {
|
|
2651
|
+
const endStart = p.token;
|
|
2652
|
+
if (terminator !== END_OF[family]) p.error("block-mismatch", `Expected: End ${family}`, endStart.span);
|
|
2653
|
+
blocks.eatEnd(terminator);
|
|
2654
|
+
endSpan = p.spanFrom(endStart);
|
|
2655
|
+
} else p.error("block-mismatch", `Expected: End ${family}`, start.span);
|
|
2656
|
+
const attributes = [];
|
|
2657
|
+
const body = [];
|
|
2658
|
+
for (const statement of statements) if (statement.kind === "AttributeStatement") attributes.push(statement);
|
|
2659
|
+
else body.push(statement);
|
|
2660
|
+
return {
|
|
2661
|
+
kind: "ProcedureDeclaration",
|
|
2662
|
+
span: p.spanFrom(start),
|
|
2663
|
+
procedureKind,
|
|
2664
|
+
scope,
|
|
2665
|
+
isStatic,
|
|
2666
|
+
name,
|
|
2667
|
+
parameters,
|
|
2668
|
+
returnType,
|
|
2669
|
+
body,
|
|
2670
|
+
attributes,
|
|
2671
|
+
endSpan
|
|
2672
|
+
};
|
|
2673
|
+
}
|
|
2674
|
+
};
|
|
2675
|
+
// src/lang/parser/statements.ts
|
|
2676
|
+
/** `Foo(x)` written as a statement passes `x` by value: the argument becomes a `Paren`. */
|
|
2677
|
+
function parenthesise(argument) {
|
|
2678
|
+
if (!argument.value || argument.name) return argument;
|
|
2679
|
+
return {
|
|
2680
|
+
...argument,
|
|
2681
|
+
value: {
|
|
2682
|
+
kind: "Paren",
|
|
2683
|
+
span: argument.span,
|
|
2684
|
+
expression: argument.value
|
|
2685
|
+
}
|
|
2686
|
+
};
|
|
2687
|
+
}
|
|
2688
|
+
const SCOPE_WORDS = /* @__PURE__ */ new Set([
|
|
2689
|
+
"public",
|
|
2690
|
+
"private",
|
|
2691
|
+
"global",
|
|
2692
|
+
"friend"
|
|
2693
|
+
]);
|
|
2694
|
+
const FILE_KEYWORDS = /* @__PURE__ */ new Set([
|
|
2695
|
+
"open",
|
|
2696
|
+
"close",
|
|
2697
|
+
"print",
|
|
2698
|
+
"write",
|
|
2699
|
+
"input",
|
|
2700
|
+
"get",
|
|
2701
|
+
"put",
|
|
2702
|
+
"seek",
|
|
2703
|
+
"lock",
|
|
2704
|
+
"unlock"
|
|
2705
|
+
]);
|
|
2706
|
+
/** Statements inside a procedure ([MS-VBAL] 5.4): dispatch on the first word, calls and assignments. */
|
|
2707
|
+
var StatementParser = class extends ParserPart {
|
|
2708
|
+
/**
|
|
2709
|
+
* Parses one statement and checks that its end follows. Errors are reported and skipped to the
|
|
2710
|
+
* end of the statement, so a bad line never stops the parse.
|
|
2711
|
+
*/
|
|
2712
|
+
parse() {
|
|
2713
|
+
const p = this.p;
|
|
2714
|
+
const start = p.token;
|
|
2715
|
+
let statement;
|
|
2716
|
+
try {
|
|
2717
|
+
statement = this.#dispatch();
|
|
2718
|
+
} catch (error) {
|
|
2719
|
+
if (!(error instanceof NestingLimit)) throw error;
|
|
2720
|
+
p.error("too-complex", "Expression too complex", start.span);
|
|
2721
|
+
p.skipStatement();
|
|
2722
|
+
return {
|
|
2723
|
+
kind: "InvalidStatement",
|
|
2724
|
+
span: p.spanFrom(start)
|
|
2725
|
+
};
|
|
2726
|
+
}
|
|
2727
|
+
if (!p.atStatementEnd() && !p.afterSeparator()) {
|
|
2728
|
+
const extra = p.token;
|
|
2729
|
+
p.expected("end of statement");
|
|
2730
|
+
p.skipStatement();
|
|
2731
|
+
if (extra === start) return {
|
|
2732
|
+
kind: "InvalidStatement",
|
|
2733
|
+
span: p.spanFrom(start)
|
|
2734
|
+
};
|
|
2735
|
+
}
|
|
2736
|
+
return statement;
|
|
2737
|
+
}
|
|
2738
|
+
/** Dispatches on the first word of a statement. */
|
|
2739
|
+
#dispatch() {
|
|
2740
|
+
const p = this.p;
|
|
2741
|
+
const token = p.token;
|
|
2742
|
+
if (token.kind !== "word" || token.bracketed || token.suffix !== null) return this.#assignmentOrCall();
|
|
2743
|
+
const { conditionals, loops, jumps, files, declarations } = this.parser;
|
|
2744
|
+
const next = p.peek(1);
|
|
2745
|
+
const assigned = next.kind === "punct" && (next.value === "=" || next.value === "." || next.value === "(" || next.value === "!");
|
|
2746
|
+
switch (token.lower) {
|
|
2747
|
+
case "dim":
|
|
2748
|
+
case "static":
|
|
2749
|
+
p.advance();
|
|
2750
|
+
return declarations.variables(token, token.lower === "dim" ? "Dim" : "Static");
|
|
2751
|
+
case "const": return declarations.constants(token, null);
|
|
2752
|
+
case "if": return conditionals.parseIf();
|
|
2753
|
+
case "select": return conditionals.parseSelect();
|
|
2754
|
+
case "for": return loops.parseFor();
|
|
2755
|
+
case "do": return loops.parseDo();
|
|
2756
|
+
case "while": return loops.parseWhile();
|
|
2757
|
+
case "with": return loops.parseWith();
|
|
2758
|
+
case "return":
|
|
2759
|
+
case "stop":
|
|
2760
|
+
case "end":
|
|
2761
|
+
p.advance();
|
|
2762
|
+
return {
|
|
2763
|
+
kind: token.lower === "return" ? "Return" : token.lower === "stop" ? "Stop" : "End",
|
|
2764
|
+
span: token.span
|
|
2765
|
+
};
|
|
2766
|
+
case "call": return this.#call();
|
|
2767
|
+
case "attribute": return declarations.attribute();
|
|
2768
|
+
case "debug":
|
|
2769
|
+
if (p.isPunct(".", 1) && p.isWord("print", 2)) return files.debugPrint();
|
|
2770
|
+
break;
|
|
2771
|
+
case "line":
|
|
2772
|
+
if (p.isWord("input", 1)) return files.parse("line");
|
|
2773
|
+
break;
|
|
2774
|
+
case "width":
|
|
2775
|
+
if (p.isPunct("#", 1)) return files.parse("width");
|
|
2776
|
+
break;
|
|
2777
|
+
case "error":
|
|
2778
|
+
case "name":
|
|
2779
|
+
if (!assigned) return jumps.parse(token.lower);
|
|
2780
|
+
break;
|
|
2781
|
+
default: {
|
|
2782
|
+
if (FILE_KEYWORDS.has(token.lower)) {
|
|
2783
|
+
if (!assigned || token.lower === "print" || token.lower === "input") return files.parse(token.lower);
|
|
2784
|
+
break;
|
|
2785
|
+
}
|
|
2786
|
+
const statement = jumps.parse(token.lower);
|
|
2787
|
+
if (statement) return statement;
|
|
2788
|
+
}
|
|
2789
|
+
}
|
|
2790
|
+
if (SCOPE_WORDS.has(token.lower)) return this.parser.blocks.skipInvalid("Invalid inside procedure", "invalid-inside-procedure");
|
|
2791
|
+
return this.#assignmentOrCall();
|
|
2792
|
+
}
|
|
2793
|
+
/** An assignment (`x = 1`, `a(1) = 2`) or a call statement without `Call`. */
|
|
2794
|
+
#assignmentOrCall() {
|
|
2795
|
+
const p = this.p;
|
|
2796
|
+
const expressions = this.parser.expressions;
|
|
2797
|
+
const start = p.token;
|
|
2798
|
+
let target = expressions.postfix({ stopAtSpacedParen: true });
|
|
2799
|
+
if (p.isPunct("(") && p.token.spaceBefore) {
|
|
2800
|
+
const mark = p.mark();
|
|
2801
|
+
const full = expressions.continuePostfix(target);
|
|
2802
|
+
if (p.isPunct("=")) target = full;
|
|
2803
|
+
else p.reset(mark);
|
|
2804
|
+
}
|
|
2805
|
+
if (p.eatPunct("=")) {
|
|
2806
|
+
const value = expressions.parse();
|
|
2807
|
+
const mid = target.kind === "CallOrIndex" ? this.#midAssignment(target, value, start) : null;
|
|
2808
|
+
if (mid) return mid;
|
|
2809
|
+
return {
|
|
2810
|
+
kind: "Assignment",
|
|
2811
|
+
span: p.spanFrom(start),
|
|
2812
|
+
assignment: "Let",
|
|
2813
|
+
explicitLet: false,
|
|
2814
|
+
target,
|
|
2815
|
+
value
|
|
2816
|
+
};
|
|
2817
|
+
}
|
|
2818
|
+
if (target.kind === "InvalidExpression") {
|
|
2819
|
+
p.skipStatement();
|
|
2820
|
+
return {
|
|
2821
|
+
kind: "InvalidStatement",
|
|
2822
|
+
span: p.spanFrom(start)
|
|
2823
|
+
};
|
|
2824
|
+
}
|
|
2825
|
+
if (p.atStatementEnd()) {
|
|
2826
|
+
if (target.kind !== "CallOrIndex") return {
|
|
2827
|
+
kind: "Call",
|
|
2828
|
+
span: p.spanFrom(start),
|
|
2829
|
+
callee: target,
|
|
2830
|
+
args: [],
|
|
2831
|
+
explicitCall: false
|
|
2832
|
+
};
|
|
2833
|
+
if (target.args.length > 1) p.error("expected", "Expected: =", target.span);
|
|
2834
|
+
const only = target.args.length === 1 ? target.args[0] : void 0;
|
|
2835
|
+
const args = only ? [parenthesise(only)] : target.args;
|
|
2836
|
+
return {
|
|
2837
|
+
kind: "Call",
|
|
2838
|
+
span: p.spanFrom(start),
|
|
2839
|
+
callee: target.target,
|
|
2840
|
+
args,
|
|
2841
|
+
explicitCall: false
|
|
2842
|
+
};
|
|
2843
|
+
}
|
|
2844
|
+
let callee = target;
|
|
2845
|
+
const leading = [];
|
|
2846
|
+
const only = target.kind === "CallOrIndex" && target.args.length === 1 ? target.args[0] : void 0;
|
|
2847
|
+
if (target.kind === "CallOrIndex" && only && p.isPunct(",")) {
|
|
2848
|
+
callee = target.target;
|
|
2849
|
+
leading.push(parenthesise(only));
|
|
2850
|
+
p.advance();
|
|
2851
|
+
}
|
|
2852
|
+
const args = [...leading, ...this.parser.arguments.list(false)];
|
|
2853
|
+
return {
|
|
2854
|
+
kind: "Call",
|
|
2855
|
+
span: p.spanFrom(start),
|
|
2856
|
+
callee,
|
|
2857
|
+
args,
|
|
2858
|
+
explicitCall: false
|
|
2859
|
+
};
|
|
2860
|
+
}
|
|
2861
|
+
/** Turns `Mid$(s, 2, 1) = v` into its own statement. */
|
|
2862
|
+
#midAssignment(target, value, start) {
|
|
2863
|
+
const callee = target.target;
|
|
2864
|
+
if (callee.kind !== "Name" || start.kind !== "word") return null;
|
|
2865
|
+
const name = callee.name.name.toLowerCase();
|
|
2866
|
+
const [first, second, third] = target.args;
|
|
2867
|
+
if (name !== "mid" && name !== "midb" || callee.name.suffix === "%" || target.args.length > 3 || !first?.value || !second?.value) return null;
|
|
2868
|
+
return {
|
|
2869
|
+
kind: "MidAssignment",
|
|
2870
|
+
span: this.p.spanFrom(start),
|
|
2871
|
+
function: name === "mid" ? "Mid" : "MidB",
|
|
2872
|
+
target: first.value,
|
|
2873
|
+
start: second.value,
|
|
2874
|
+
length: third?.value ?? null,
|
|
2875
|
+
value
|
|
2876
|
+
};
|
|
2877
|
+
}
|
|
2878
|
+
#call() {
|
|
2879
|
+
const p = this.p;
|
|
2880
|
+
const start = p.advance();
|
|
2881
|
+
const expression = this.parser.expressions.postfix();
|
|
2882
|
+
const call = expression.kind === "CallOrIndex";
|
|
2883
|
+
return {
|
|
2884
|
+
kind: "Call",
|
|
2885
|
+
span: p.spanFrom(start),
|
|
2886
|
+
callee: call ? expression.target : expression,
|
|
2887
|
+
args: call ? expression.args : [],
|
|
2888
|
+
explicitCall: true
|
|
2889
|
+
};
|
|
2890
|
+
}
|
|
2891
|
+
};
|
|
2892
|
+
// src/lang/parser/parser.ts
|
|
2893
|
+
/**
|
|
2894
|
+
* The recursive-descent parser of [MS-VBAL] 5: one cursor over the tokens and one part per
|
|
2895
|
+
* family of constructs. Construct one per token list.
|
|
2896
|
+
*/
|
|
2897
|
+
var Parser = class {
|
|
2898
|
+
cursor;
|
|
2899
|
+
expressions = new ExpressionParser(this);
|
|
2900
|
+
arguments = new ArgumentParser(this);
|
|
2901
|
+
declarations = new DeclarationParser(this);
|
|
2902
|
+
moduleDeclarations = new ModuleDeclarationParser(this);
|
|
2903
|
+
blocks = new BlockParser(this);
|
|
2904
|
+
conditionals = new ConditionalParser(this);
|
|
2905
|
+
loops = new LoopParser(this);
|
|
2906
|
+
statements = new StatementParser(this);
|
|
2907
|
+
jumps = new JumpParser(this);
|
|
2908
|
+
files = new FileStatementParser(this);
|
|
2909
|
+
procedures = new ProcedureParser(this);
|
|
2910
|
+
module = new ModuleParser(this);
|
|
2911
|
+
constructor(tokens, diagnostics, limits) {
|
|
2912
|
+
this.cursor = new TokenCursor(tokens, diagnostics, limits);
|
|
2913
|
+
}
|
|
2914
|
+
};
|
|
2915
|
+
// src/lang/preprocess/cc-eval.ts
|
|
2916
|
+
/** A conditional compilation expression that cannot be evaluated. */
|
|
2917
|
+
var ConditionalEvalError = class extends Error {
|
|
2918
|
+
constructor(message) {
|
|
2919
|
+
super(message);
|
|
2920
|
+
this.name = "ConditionalEvalError";
|
|
2921
|
+
}
|
|
2922
|
+
};
|
|
2923
|
+
const typed = (value) => ({
|
|
2924
|
+
value,
|
|
2925
|
+
variant: false
|
|
2926
|
+
});
|
|
2927
|
+
const truth = (value) => typed(value ? -1 : 0);
|
|
2928
|
+
const mismatch = () => new ConditionalEvalError("Type mismatch");
|
|
2929
|
+
/** The number a numeric string spells (`" 12 "`, `"1e3"`, `"True"`), or null. */
|
|
2930
|
+
function numericText(text) {
|
|
2931
|
+
const trimmed = text.trim();
|
|
2932
|
+
if (/^true$/i.test(trimmed)) return -1;
|
|
2933
|
+
if (/^false$/i.test(trimmed)) return 0;
|
|
2934
|
+
if (!/^[+-]?(?:\d+\.?\d*|\.\d+)(?:[eEdD][+-]?\d+)?$/.test(trimmed)) return null;
|
|
2935
|
+
return Number(trimmed.replace(/[dD]/, "e"));
|
|
2936
|
+
}
|
|
2937
|
+
function toNumber({ value }) {
|
|
2938
|
+
if (typeof value === "number") return value;
|
|
2939
|
+
const number = numericText(value);
|
|
2940
|
+
if (number === null) throw mismatch();
|
|
2941
|
+
return number;
|
|
2942
|
+
}
|
|
2943
|
+
/** Rounds half to even, as `CInt`, `CLng` and `CByte` do (`CInt(2.5)` is 2). */
|
|
2944
|
+
function roundHalfEven(value) {
|
|
2945
|
+
const floor = Math.floor(value);
|
|
2946
|
+
if (value - floor !== .5) return Math.round(value);
|
|
2947
|
+
return floor % 2 === 0 ? floor : floor + 1;
|
|
2948
|
+
}
|
|
2949
|
+
function toWhole(value, min, max) {
|
|
2950
|
+
const number = roundHalfEven(toNumber(value));
|
|
2951
|
+
if (number < min || number > max) throw new ConditionalEvalError("Overflow");
|
|
2952
|
+
return number;
|
|
2953
|
+
}
|
|
2954
|
+
const toLong = (value) => toWhole(value, -0x80000000, 0x7fffffff);
|
|
2955
|
+
function toText({ value }) {
|
|
2956
|
+
return typeof value === "string" ? value : String(value);
|
|
2957
|
+
}
|
|
2958
|
+
/** Whether a condition is true: any non-zero number. */
|
|
2959
|
+
function isTruthy(value) {
|
|
2960
|
+
return toNumber(typed(value)) !== 0;
|
|
2961
|
+
}
|
|
2962
|
+
const FUNCTIONS = {
|
|
2963
|
+
abs: (v) => Math.abs(toNumber(v)),
|
|
2964
|
+
sgn: (v) => Math.sign(toNumber(v)),
|
|
2965
|
+
int: (v) => Math.floor(toNumber(v)),
|
|
2966
|
+
fix: (v) => Math.trunc(toNumber(v)),
|
|
2967
|
+
len: (v) => toText(v).length,
|
|
2968
|
+
cbool: (v) => toNumber(v) !== 0 ? -1 : 0,
|
|
2969
|
+
cint: (v) => toWhole(v, -32768, 32767),
|
|
2970
|
+
clng: toLong,
|
|
2971
|
+
cbyte: (v) => toWhole(v, 0, 255),
|
|
2972
|
+
cdbl: toNumber,
|
|
2973
|
+
csng: (v) => Math.fround(toNumber(v)),
|
|
2974
|
+
cstr: toText
|
|
2975
|
+
};
|
|
2976
|
+
/**
|
|
2977
|
+
* Compares two values by VBA's rules for typed and Variant operands: a String and a Variant
|
|
2978
|
+
* compare as text (`Undefined = ""` is false: the Variant reads "0"); a String and a number
|
|
2979
|
+
* compare as numbers or fail with Type mismatch (`"abc" = 0`); two Variants put numbers first.
|
|
2980
|
+
*/
|
|
2981
|
+
function compare(left, right) {
|
|
2982
|
+
const order = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
2983
|
+
const l = left.value;
|
|
2984
|
+
const r = right.value;
|
|
2985
|
+
if (typeof l === typeof r) return order(l, r);
|
|
2986
|
+
const textual = typeof l === "string" ? left : right;
|
|
2987
|
+
const numeric = textual === left ? right : left;
|
|
2988
|
+
const sign = textual === left ? 1 : -1;
|
|
2989
|
+
if (numeric.variant && textual.variant) return sign;
|
|
2990
|
+
if (numeric.variant) return order(toText(left), toText(right));
|
|
2991
|
+
return order(toNumber(left), toNumber(right));
|
|
2992
|
+
}
|
|
2993
|
+
const BINARY = {
|
|
2994
|
+
"+": (l, r) => typeof l.value === "string" && typeof r.value === "string" ? l.value + r.value : toNumber(l) + toNumber(r),
|
|
2995
|
+
"-": (l, r) => toNumber(l) - toNumber(r),
|
|
2996
|
+
"*": (l, r) => toNumber(l) * toNumber(r),
|
|
2997
|
+
"/": (l, r) => {
|
|
2998
|
+
const divisor = toNumber(r);
|
|
2999
|
+
if (divisor === 0) throw new ConditionalEvalError("Division by zero");
|
|
3000
|
+
return toNumber(l) / divisor;
|
|
3001
|
+
},
|
|
3002
|
+
"\\": (l, r) => Math.trunc(toLong(l) / nonZero(toLong(r))),
|
|
3003
|
+
Mod: (l, r) => toLong(l) % nonZero(toLong(r)),
|
|
3004
|
+
"^": (l, r) => toNumber(l) ** toNumber(r),
|
|
3005
|
+
"&": (l, r) => toText(l) + toText(r),
|
|
3006
|
+
"=": (l, r) => truth(compare(l, r) === 0).value,
|
|
3007
|
+
"<>": (l, r) => truth(compare(l, r) !== 0).value,
|
|
3008
|
+
"<": (l, r) => truth(compare(l, r) < 0).value,
|
|
3009
|
+
">": (l, r) => truth(compare(l, r) > 0).value,
|
|
3010
|
+
"<=": (l, r) => truth(compare(l, r) <= 0).value,
|
|
3011
|
+
">=": (l, r) => truth(compare(l, r) >= 0).value,
|
|
3012
|
+
And: (l, r) => toLong(l) & toLong(r),
|
|
3013
|
+
Or: (l, r) => toLong(l) | toLong(r),
|
|
3014
|
+
Xor: (l, r) => toLong(l) ^ toLong(r),
|
|
3015
|
+
Eqv: (l, r) => ~(toLong(l) ^ toLong(r)),
|
|
3016
|
+
Imp: (l, r) => ~toLong(l) | toLong(r)
|
|
3017
|
+
};
|
|
3018
|
+
function nonZero(divisor) {
|
|
3019
|
+
if (divisor === 0) throw new ConditionalEvalError("Division by zero");
|
|
3020
|
+
return divisor;
|
|
3021
|
+
}
|
|
3022
|
+
/** Evaluates `#If` and `#Const` expressions against a table of constants. */
|
|
3023
|
+
var ConditionalEvaluator = class {
|
|
3024
|
+
constants;
|
|
3025
|
+
constructor(constants) {
|
|
3026
|
+
this.constants = constants;
|
|
3027
|
+
}
|
|
3028
|
+
/** The value of `expression`; throws {@link ConditionalEvalError} for what VBA rejects. */
|
|
3029
|
+
evaluate(expression) {
|
|
3030
|
+
return this.#value(expression).value;
|
|
3031
|
+
}
|
|
3032
|
+
#value(node) {
|
|
3033
|
+
switch (node.kind) {
|
|
3034
|
+
case "NumberLiteral":
|
|
3035
|
+
case "DateLiteral":
|
|
3036
|
+
case "StringLiteral": return typed(node.value);
|
|
3037
|
+
case "BooleanLiteral": return truth(node.value);
|
|
3038
|
+
case "EmptyLiteral":
|
|
3039
|
+
case "NullLiteral": return typed(0);
|
|
3040
|
+
case "Name": return {
|
|
3041
|
+
value: this.constants.get(node.name.name.toLowerCase()) ?? 0,
|
|
3042
|
+
variant: true
|
|
3043
|
+
};
|
|
3044
|
+
case "Paren": return this.#value(node.expression);
|
|
3045
|
+
case "Unary": {
|
|
3046
|
+
const operand = this.#value(node.operand);
|
|
3047
|
+
if (node.operator === "Not") return typed(~toLong(operand));
|
|
3048
|
+
return typed(node.operator === "-" ? -toNumber(operand) : toNumber(operand));
|
|
3049
|
+
}
|
|
3050
|
+
case "CallOrIndex": {
|
|
3051
|
+
const fn = node.target.kind === "Name" ? FUNCTIONS[node.target.name.name.toLowerCase()] : void 0;
|
|
3052
|
+
const arg = node.args[0]?.value;
|
|
3053
|
+
if (fn && node.args.length === 1 && arg) return typed(fn(this.#value(arg)));
|
|
3054
|
+
break;
|
|
3055
|
+
}
|
|
3056
|
+
case "Binary": {
|
|
3057
|
+
const rule = BINARY[node.operator];
|
|
3058
|
+
if (rule) return typed(rule(this.#value(node.left), this.#value(node.right)));
|
|
3059
|
+
break;
|
|
3060
|
+
}
|
|
3061
|
+
}
|
|
3062
|
+
throw new ConditionalEvalError("Constant expression required");
|
|
3063
|
+
}
|
|
3064
|
+
};
|
|
3065
|
+
// src/lang/preprocess/preprocessor.ts
|
|
3066
|
+
const DIRECTIVE_NAMES = {
|
|
3067
|
+
if: "If",
|
|
3068
|
+
elseif: "ElseIf",
|
|
3069
|
+
else: "Else",
|
|
3070
|
+
end: "EndIf",
|
|
3071
|
+
endif: "EndIf",
|
|
3072
|
+
const: "Const"
|
|
3073
|
+
};
|
|
3074
|
+
/**
|
|
3075
|
+
* Applies `#If`/`#ElseIf`/`#Else`/`#End If` and `#Const` ([MS-VBAL] 3.4) to a token stream: the
|
|
3076
|
+
* tokens of inactive branches are dropped and their ranges recorded. `#Const` lines of active
|
|
3077
|
+
* branches update `constants`. One instance processes one token stream.
|
|
3078
|
+
*/
|
|
3079
|
+
var Preprocessor = class {
|
|
3080
|
+
constants;
|
|
3081
|
+
diagnostics;
|
|
3082
|
+
limits;
|
|
3083
|
+
#output = [];
|
|
3084
|
+
#directives = [];
|
|
3085
|
+
#inactiveRanges = [];
|
|
3086
|
+
#stack = [];
|
|
3087
|
+
#evaluator;
|
|
3088
|
+
#inactiveUntil = -1;
|
|
3089
|
+
constructor(constants, diagnostics, limits) {
|
|
3090
|
+
this.constants = constants;
|
|
3091
|
+
this.diagnostics = diagnostics;
|
|
3092
|
+
this.limits = limits;
|
|
3093
|
+
this.#evaluator = new ConditionalEvaluator(constants);
|
|
3094
|
+
}
|
|
3095
|
+
run(tokens, bodyStart) {
|
|
3096
|
+
let lineStart = bodyStart;
|
|
3097
|
+
for (let i = 0; i < tokens.length;) {
|
|
3098
|
+
let end = i;
|
|
3099
|
+
while (end < tokens.length - 1 && tokens[end]?.kind !== "newline" && tokens[end]?.kind !== "eof") end++;
|
|
3100
|
+
const line = tokens.slice(i, end + 1);
|
|
3101
|
+
const first = line[0];
|
|
3102
|
+
const last = line.at(-1);
|
|
3103
|
+
if (first.kind === "directive") this.#directives.push(this.#directive(first, line.slice(1, -1), last));
|
|
3104
|
+
else if (this.#active() || first.kind === "eof") for (const token of line) this.#output.push(token);
|
|
3105
|
+
else this.#markInactive(lineStart, last);
|
|
3106
|
+
lineStart = last.span.end;
|
|
3107
|
+
i = end + 1;
|
|
3108
|
+
}
|
|
3109
|
+
for (const frame of this.#stack) this.diagnostics.add("conditional-compilation", "#If block without #End If", frame.start);
|
|
3110
|
+
return {
|
|
3111
|
+
tokens: this.#output,
|
|
3112
|
+
directives: this.#directives,
|
|
3113
|
+
inactiveRanges: this.#inactiveRanges
|
|
3114
|
+
};
|
|
3115
|
+
}
|
|
3116
|
+
#active() {
|
|
3117
|
+
return this.#stack.at(-1)?.active ?? true;
|
|
3118
|
+
}
|
|
3119
|
+
/** Records a dropped line, merging it with the range of the line before when they touch. */
|
|
3120
|
+
#markInactive(lineStart, last) {
|
|
3121
|
+
const ranges = this.#inactiveRanges;
|
|
3122
|
+
const previous = ranges.at(-1);
|
|
3123
|
+
const lineEnd = last.kind === "newline" ? last.span.start : last.span.end;
|
|
3124
|
+
if (previous && this.#inactiveUntil === lineStart.offset) ranges[ranges.length - 1] = {
|
|
3125
|
+
start: previous.start,
|
|
3126
|
+
end: lineEnd
|
|
3127
|
+
};
|
|
3128
|
+
else ranges.push({
|
|
3129
|
+
start: lineStart,
|
|
3130
|
+
end: lineEnd
|
|
3131
|
+
});
|
|
3132
|
+
this.#inactiveUntil = last.span.end.offset;
|
|
3133
|
+
}
|
|
3134
|
+
#directive(token, rest, terminator) {
|
|
3135
|
+
const eof = {
|
|
3136
|
+
...terminator,
|
|
3137
|
+
kind: "eof"
|
|
3138
|
+
};
|
|
3139
|
+
const parser = new Parser([...rest, eof], this.diagnostics, this.limits);
|
|
3140
|
+
const span = {
|
|
3141
|
+
start: token.span.start,
|
|
3142
|
+
end: rest.at(-1)?.span.end ?? token.span.end
|
|
3143
|
+
};
|
|
3144
|
+
const line = {
|
|
3145
|
+
token,
|
|
3146
|
+
parser,
|
|
3147
|
+
hasRest: rest.length > 0,
|
|
3148
|
+
span
|
|
3149
|
+
};
|
|
3150
|
+
let name = null;
|
|
3151
|
+
let evaluated = {
|
|
3152
|
+
expression: null,
|
|
3153
|
+
value: null
|
|
3154
|
+
};
|
|
3155
|
+
switch (token.directive) {
|
|
3156
|
+
case "const":
|
|
3157
|
+
({name, evaluated} = this.#const(line));
|
|
3158
|
+
break;
|
|
3159
|
+
case "if":
|
|
3160
|
+
evaluated = this.#if(line);
|
|
3161
|
+
break;
|
|
3162
|
+
case "elseif":
|
|
3163
|
+
evaluated = this.#elseIf(line);
|
|
3164
|
+
break;
|
|
3165
|
+
case "else":
|
|
3166
|
+
this.#else(line);
|
|
3167
|
+
break;
|
|
3168
|
+
case "end":
|
|
3169
|
+
case "endif": this.#end(line);
|
|
3170
|
+
}
|
|
3171
|
+
const p = parser.cursor;
|
|
3172
|
+
const checked = evaluated.value !== null || token.directive === "end" || token.directive === "endif";
|
|
3173
|
+
if (p.token.kind !== "eof" && checked) p.expected("end of statement");
|
|
3174
|
+
return {
|
|
3175
|
+
kind: "ConditionalDirective",
|
|
3176
|
+
span,
|
|
3177
|
+
directive: DIRECTIVE_NAMES[token.directive],
|
|
3178
|
+
name,
|
|
3179
|
+
...evaluated
|
|
3180
|
+
};
|
|
3181
|
+
}
|
|
3182
|
+
/** Parses and evaluates the line's expression (and `Then`, except after `#Const`). */
|
|
3183
|
+
#evaluate(line) {
|
|
3184
|
+
const p = line.parser.cursor;
|
|
3185
|
+
let expression = null;
|
|
3186
|
+
try {
|
|
3187
|
+
expression = line.parser.expressions.parse();
|
|
3188
|
+
if (line.token.directive !== "const") p.expectWord("then", "Then");
|
|
3189
|
+
if (expression.kind === "InvalidExpression") return {
|
|
3190
|
+
expression,
|
|
3191
|
+
value: null
|
|
3192
|
+
};
|
|
3193
|
+
return {
|
|
3194
|
+
expression,
|
|
3195
|
+
value: this.#evaluator.evaluate(expression)
|
|
3196
|
+
};
|
|
3197
|
+
} catch (error) {
|
|
3198
|
+
if (error instanceof ConditionalEvalError) this.diagnostics.add("conditional-compilation", error.message, line.span);
|
|
3199
|
+
else if (error instanceof NestingLimit) this.diagnostics.add("too-complex", "Expression too complex", line.span);
|
|
3200
|
+
else throw error;
|
|
3201
|
+
return {
|
|
3202
|
+
expression,
|
|
3203
|
+
value: null
|
|
3204
|
+
};
|
|
3205
|
+
}
|
|
3206
|
+
}
|
|
3207
|
+
#const(line) {
|
|
3208
|
+
const p = line.parser.cursor;
|
|
3209
|
+
const nameToken = p.token;
|
|
3210
|
+
let name = null;
|
|
3211
|
+
if (nameToken.kind === "word") {
|
|
3212
|
+
p.advance();
|
|
3213
|
+
name = nameToken.name;
|
|
3214
|
+
} else p.expected("identifier");
|
|
3215
|
+
p.expectPunct("=");
|
|
3216
|
+
if (!this.#active()) {
|
|
3217
|
+
p.skipToStatementEnd();
|
|
3218
|
+
return {
|
|
3219
|
+
name,
|
|
3220
|
+
evaluated: {
|
|
3221
|
+
expression: null,
|
|
3222
|
+
value: null
|
|
3223
|
+
}
|
|
3224
|
+
};
|
|
3225
|
+
}
|
|
3226
|
+
const evaluated = this.#evaluate(line);
|
|
3227
|
+
if (name !== null && evaluated.value !== null) this.constants.set(name.toLowerCase(), evaluated.value);
|
|
3228
|
+
return {
|
|
3229
|
+
name,
|
|
3230
|
+
evaluated
|
|
3231
|
+
};
|
|
3232
|
+
}
|
|
3233
|
+
#if(line) {
|
|
3234
|
+
const parentActive = this.#active();
|
|
3235
|
+
const evaluated = parentActive ? this.#evaluate(line) : {
|
|
3236
|
+
expression: null,
|
|
3237
|
+
value: null
|
|
3238
|
+
};
|
|
3239
|
+
const taken = evaluated.value !== null && isTruthy(evaluated.value);
|
|
3240
|
+
this.#stack.push({
|
|
3241
|
+
parentActive,
|
|
3242
|
+
active: taken,
|
|
3243
|
+
taken,
|
|
3244
|
+
sawElse: false,
|
|
3245
|
+
start: line.token.span
|
|
3246
|
+
});
|
|
3247
|
+
return evaluated;
|
|
3248
|
+
}
|
|
3249
|
+
#elseIf(line) {
|
|
3250
|
+
const top = this.#stack.at(-1);
|
|
3251
|
+
if (!top || top.sawElse) {
|
|
3252
|
+
this.diagnostics.add("conditional-compilation", "#ElseIf without #If", line.token.span);
|
|
3253
|
+
return {
|
|
3254
|
+
expression: null,
|
|
3255
|
+
value: null
|
|
3256
|
+
};
|
|
3257
|
+
}
|
|
3258
|
+
const evaluated = top.parentActive && !top.taken ? this.#evaluate(line) : {
|
|
3259
|
+
expression: null,
|
|
3260
|
+
value: null
|
|
3261
|
+
};
|
|
3262
|
+
top.active = evaluated.value !== null && isTruthy(evaluated.value);
|
|
3263
|
+
top.taken ||= top.active;
|
|
3264
|
+
return evaluated;
|
|
3265
|
+
}
|
|
3266
|
+
/**
|
|
3267
|
+
* `#Else`. Followed by anything (`#Else If x Then`, `#Else Foo`), the VBA editor reads a branch
|
|
3268
|
+
* that is never compiled and that does not count as the `#Else` (a later `#ElseIf` or `#Else`
|
|
3269
|
+
* still works), and reports a Syntax error when an earlier branch was taken.
|
|
3270
|
+
*/
|
|
3271
|
+
#else(line) {
|
|
3272
|
+
const top = this.#stack.at(-1);
|
|
3273
|
+
if (!top || top.sawElse) {
|
|
3274
|
+
this.diagnostics.add("conditional-compilation", "#Else without #If", line.token.span);
|
|
3275
|
+
return;
|
|
3276
|
+
}
|
|
3277
|
+
line.parser.cursor.skipToStatementEnd();
|
|
3278
|
+
if (line.hasRest) {
|
|
3279
|
+
if (top.parentActive && top.taken) this.diagnostics.add("conditional-compilation", "Syntax error", line.span);
|
|
3280
|
+
top.active = false;
|
|
3281
|
+
return;
|
|
3282
|
+
}
|
|
3283
|
+
top.sawElse = true;
|
|
3284
|
+
top.active = top.parentActive && !top.taken;
|
|
3285
|
+
top.taken = true;
|
|
3286
|
+
}
|
|
3287
|
+
#end(line) {
|
|
3288
|
+
if (line.token.directive === "end") line.parser.cursor.expectWord("if", "If");
|
|
3289
|
+
if (!this.#stack.pop()) this.diagnostics.add("conditional-compilation", "#End If without #If", line.token.span);
|
|
3290
|
+
}
|
|
3291
|
+
};
|
|
3292
|
+
// src/lang/diagnostics.ts
|
|
3293
|
+
/** Collects diagnostics up to a cap, so hostile input cannot grow the list without bound. */
|
|
3294
|
+
var DiagnosticBag = class {
|
|
3295
|
+
max;
|
|
3296
|
+
items = [];
|
|
3297
|
+
#dropped = 0;
|
|
3298
|
+
#lastDropped;
|
|
3299
|
+
constructor(max) {
|
|
3300
|
+
this.max = max;
|
|
3301
|
+
}
|
|
3302
|
+
add(code, message, span, severity = "error") {
|
|
3303
|
+
if (this.items.length < this.max) this.items.push({
|
|
3304
|
+
code,
|
|
3305
|
+
message,
|
|
3306
|
+
severity,
|
|
3307
|
+
span
|
|
3308
|
+
});
|
|
3309
|
+
else {
|
|
3310
|
+
this.#dropped++;
|
|
3311
|
+
this.#lastDropped = span;
|
|
3312
|
+
}
|
|
3313
|
+
}
|
|
3314
|
+
/** The diagnostics, with a final note when some were dropped. */
|
|
3315
|
+
finish() {
|
|
3316
|
+
if (this.#dropped > 0 && this.#lastDropped) return [...this.items, {
|
|
3317
|
+
code: "too-many-diagnostics",
|
|
3318
|
+
message: `${this.#dropped} more problems were not reported`,
|
|
3319
|
+
severity: "error",
|
|
3320
|
+
span: this.#lastDropped
|
|
3321
|
+
}];
|
|
3322
|
+
return this.items;
|
|
3323
|
+
}
|
|
3324
|
+
};
|
|
3325
|
+
// src/lang/lexer/calendar.ts
|
|
3326
|
+
const MONTH_DAYS = [
|
|
3327
|
+
31,
|
|
3328
|
+
28,
|
|
3329
|
+
31,
|
|
3330
|
+
30,
|
|
3331
|
+
31,
|
|
3332
|
+
30,
|
|
3333
|
+
31,
|
|
3334
|
+
31,
|
|
3335
|
+
30,
|
|
3336
|
+
31,
|
|
3337
|
+
30,
|
|
3338
|
+
31
|
|
3339
|
+
];
|
|
3340
|
+
function isLeap(year) {
|
|
3341
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
3342
|
+
}
|
|
3343
|
+
function daysIn(year, month) {
|
|
3344
|
+
return month === 2 && isLeap(year) ? 29 : MONTH_DAYS[month - 1] ?? 0;
|
|
3345
|
+
}
|
|
3346
|
+
/** Whether a calendar date is in VBA's Date range (100-01-01 to 9999-12-31) and exists. */
|
|
3347
|
+
function isValidDate(year, month, day) {
|
|
3348
|
+
return Number.isInteger(year) && year >= 100 && year <= 9999 && month >= 1 && month <= 12 && day >= 1 && day <= daysIn(year, month);
|
|
3349
|
+
}
|
|
3350
|
+
/**
|
|
3351
|
+
* The year a date literal's year part means. One or two digits fall in the hundred years ending
|
|
3352
|
+
* at `twoDigitYearMax` (1950-2049 by default, as the VBA editor reads `#1/2/49#` and `#1/2/50#`);
|
|
3353
|
+
* three or more digits are taken as written.
|
|
3354
|
+
*/
|
|
3355
|
+
function windowYear(value, digits, twoDigitYearMax) {
|
|
3356
|
+
if (digits > 2) return value;
|
|
3357
|
+
const first = twoDigitYearMax - 99;
|
|
3358
|
+
return first + ((value - first) % 100 + 100) % 100;
|
|
3359
|
+
}
|
|
3360
|
+
/** Days from 1970-01-01 to a proleptic Gregorian date (any year). */
|
|
3361
|
+
function daysFromCivil(year, month, day) {
|
|
3362
|
+
const y = month <= 2 ? year - 1 : year;
|
|
3363
|
+
const era = Math.floor(y / 400);
|
|
3364
|
+
const yoe = y - era * 400;
|
|
3365
|
+
const doy = Math.floor((153 * (month + (month > 2 ? -3 : 9)) + 2) / 5) + day - 1;
|
|
3366
|
+
const doe = yoe * 365 + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy;
|
|
3367
|
+
return era * 146097 + doe - 719468;
|
|
3368
|
+
}
|
|
3369
|
+
const OLE_EPOCH = daysFromCivil(1899, 12, 30);
|
|
3370
|
+
/** The OLE Automation serial of a date and time. Dates before 1899-12-30 count the time back. */
|
|
3371
|
+
function dateSerial(parts) {
|
|
3372
|
+
const days = daysFromCivil(parts.year, parts.month, parts.day) - OLE_EPOCH;
|
|
3373
|
+
const fraction = (parts.hour * 3600 + parts.minute * 60 + parts.second) / 86400;
|
|
3374
|
+
return days >= 0 ? days + fraction : days - fraction;
|
|
3375
|
+
}
|
|
3376
|
+
const MONTHS = [
|
|
3377
|
+
"january",
|
|
3378
|
+
"february",
|
|
3379
|
+
"march",
|
|
3380
|
+
"april",
|
|
3381
|
+
"may",
|
|
3382
|
+
"june",
|
|
3383
|
+
"july",
|
|
3384
|
+
"august",
|
|
3385
|
+
"september",
|
|
3386
|
+
"october",
|
|
3387
|
+
"november",
|
|
3388
|
+
"december"
|
|
3389
|
+
];
|
|
3390
|
+
/** The month (1 to 12) a full or abbreviated English month name gives, or 0. */
|
|
3391
|
+
function monthOf(word) {
|
|
3392
|
+
const lower = word.toLowerCase();
|
|
3393
|
+
if (lower.length < 3) return 0;
|
|
3394
|
+
return MONTHS.findIndex((name) => name === lower || name.slice(0, 3) === lower || lower === "sept" && name === "september") + 1;
|
|
3395
|
+
}
|
|
3396
|
+
const AM_PM = /^(?:am|pm|a|p)$/i;
|
|
3397
|
+
function splitItems(text) {
|
|
3398
|
+
const items = [];
|
|
3399
|
+
const pattern = /\s*(?:(\d+)|([A-Za-z]+)|([/,-])|([:.]))/y;
|
|
3400
|
+
let at = 0;
|
|
3401
|
+
while (at < text.length) {
|
|
3402
|
+
pattern.lastIndex = at;
|
|
3403
|
+
const match = pattern.exec(text);
|
|
3404
|
+
if (!match) {
|
|
3405
|
+
if (/^\s*$/.test(text.slice(at))) break;
|
|
3406
|
+
return null;
|
|
3407
|
+
}
|
|
3408
|
+
at = pattern.lastIndex;
|
|
3409
|
+
if (match[1] !== void 0) items.push({
|
|
3410
|
+
kind: "number",
|
|
3411
|
+
text: match[1]
|
|
3412
|
+
});
|
|
3413
|
+
else if (match[2] !== void 0) items.push({
|
|
3414
|
+
kind: "word",
|
|
3415
|
+
text: match[2]
|
|
3416
|
+
});
|
|
3417
|
+
else if (match[3] !== void 0) items.push({
|
|
3418
|
+
kind: "sep",
|
|
3419
|
+
text: match[3]
|
|
3420
|
+
});
|
|
3421
|
+
else items.push({
|
|
3422
|
+
kind: "timesep",
|
|
3423
|
+
text: match[4] ?? ":"
|
|
3424
|
+
});
|
|
3425
|
+
}
|
|
3426
|
+
return items;
|
|
3427
|
+
}
|
|
3428
|
+
/** Reads the text between the `#`s of a date literal with fixed options. */
|
|
3429
|
+
var DateLiteralReader = class {
|
|
3430
|
+
options;
|
|
3431
|
+
constructor(options) {
|
|
3432
|
+
this.options = options;
|
|
3433
|
+
}
|
|
3434
|
+
/** The date and time the text names, or null when it is not a valid date literal. */
|
|
3435
|
+
read(text) {
|
|
3436
|
+
if (text.length === 0 || text.length > 64) return null;
|
|
3437
|
+
const items = splitItems(text);
|
|
3438
|
+
if (!items || items.length === 0) return null;
|
|
3439
|
+
let timeStart = items.length;
|
|
3440
|
+
for (let i = 0; i < items.length; i++) {
|
|
3441
|
+
const item = items[i];
|
|
3442
|
+
const marker = item?.kind === "word" && AM_PM.test(item.text) && i === items.length - 1;
|
|
3443
|
+
if (item?.kind === "timesep" || marker) {
|
|
3444
|
+
timeStart = items[i - 1]?.kind === "number" ? i - 1 : -1;
|
|
3445
|
+
break;
|
|
3446
|
+
}
|
|
3447
|
+
}
|
|
3448
|
+
if (timeStart < 0) return null;
|
|
3449
|
+
const dateItems = items.slice(0, timeStart);
|
|
3450
|
+
const timeItems = items.slice(timeStart);
|
|
3451
|
+
const date = dateItems.length > 0 ? this.#date(dateItems) : {
|
|
3452
|
+
year: 1899,
|
|
3453
|
+
month: 12,
|
|
3454
|
+
day: 30
|
|
3455
|
+
};
|
|
3456
|
+
const time = timeItems.length > 0 ? readTime(timeItems) : {
|
|
3457
|
+
hour: 0,
|
|
3458
|
+
minute: 0,
|
|
3459
|
+
second: 0
|
|
3460
|
+
};
|
|
3461
|
+
if (!date || !time) return null;
|
|
3462
|
+
return {
|
|
3463
|
+
...date,
|
|
3464
|
+
...time,
|
|
3465
|
+
hasDate: dateItems.length > 0,
|
|
3466
|
+
hasTime: timeItems.length > 0
|
|
3467
|
+
};
|
|
3468
|
+
}
|
|
3469
|
+
#date(items) {
|
|
3470
|
+
const parts = [];
|
|
3471
|
+
let expectPart = true;
|
|
3472
|
+
for (const item of items) {
|
|
3473
|
+
if (item.kind === "sep") {
|
|
3474
|
+
if (expectPart) return null;
|
|
3475
|
+
expectPart = true;
|
|
3476
|
+
continue;
|
|
3477
|
+
}
|
|
3478
|
+
const month = item.kind === "word" ? monthOf(item.text) : 0;
|
|
3479
|
+
if (item.kind === "number") parts.push({
|
|
3480
|
+
kind: "number",
|
|
3481
|
+
value: Number(item.text),
|
|
3482
|
+
digits: item.text.length
|
|
3483
|
+
});
|
|
3484
|
+
else if (month) parts.push({
|
|
3485
|
+
kind: "month",
|
|
3486
|
+
value: month
|
|
3487
|
+
});
|
|
3488
|
+
else return null;
|
|
3489
|
+
expectPart = false;
|
|
3490
|
+
}
|
|
3491
|
+
if (expectPart || parts.length < 2 || parts.length > 3) return null;
|
|
3492
|
+
const months = parts.filter((part) => part.kind === "month");
|
|
3493
|
+
if (months.length > 1) return null;
|
|
3494
|
+
const numbers = parts.filter((part) => part.kind === "number");
|
|
3495
|
+
const month = months[0];
|
|
3496
|
+
const date = month ? this.#withMonthName(month.value, numbers) : numbers.length === 3 ? this.#threeNumbers(numbers) : this.#twoNumbers(numbers);
|
|
3497
|
+
return date && isValidDate(date.year, date.month, date.day) ? date : null;
|
|
3498
|
+
}
|
|
3499
|
+
#year(part) {
|
|
3500
|
+
return windowYear(part.value, part.digits, this.options.twoDigitYearMax);
|
|
3501
|
+
}
|
|
3502
|
+
/** `Jan 2 2003`, `2-Jan-03`, `Jan 2003`, `January 2`, `Jan 32` (the year 2032). */
|
|
3503
|
+
#withMonthName(month, numbers) {
|
|
3504
|
+
const [x, y] = numbers;
|
|
3505
|
+
if (!x) return null;
|
|
3506
|
+
const { currentYear } = this.options;
|
|
3507
|
+
if (!y) {
|
|
3508
|
+
if (x.digits <= 2 && x.value <= 31 && isValidDate(currentYear, month, x.value)) return {
|
|
3509
|
+
year: currentYear,
|
|
3510
|
+
month,
|
|
3511
|
+
day: x.value
|
|
3512
|
+
};
|
|
3513
|
+
return {
|
|
3514
|
+
year: this.#year(x),
|
|
3515
|
+
month,
|
|
3516
|
+
day: 1
|
|
3517
|
+
};
|
|
3518
|
+
}
|
|
3519
|
+
if (x.digits > 2 || x.value > 31) return {
|
|
3520
|
+
year: this.#year(x),
|
|
3521
|
+
month,
|
|
3522
|
+
day: y.value
|
|
3523
|
+
};
|
|
3524
|
+
return {
|
|
3525
|
+
year: this.#year(y),
|
|
3526
|
+
month,
|
|
3527
|
+
day: x.value
|
|
3528
|
+
};
|
|
3529
|
+
}
|
|
3530
|
+
/** Tries each configured order; the first that names a real date wins. */
|
|
3531
|
+
#threeNumbers(numbers) {
|
|
3532
|
+
for (const order of this.options.dateOrder) {
|
|
3533
|
+
const date = this.#inOrder(order, numbers);
|
|
3534
|
+
if (date && isValidDate(date.year, date.month, date.day)) return date;
|
|
3535
|
+
}
|
|
3536
|
+
return null;
|
|
3537
|
+
}
|
|
3538
|
+
#inOrder(order, [a, b, c]) {
|
|
3539
|
+
if (!a || !b || !c) return null;
|
|
3540
|
+
switch (order) {
|
|
3541
|
+
case "mdy": return {
|
|
3542
|
+
year: this.#year(c),
|
|
3543
|
+
month: a.value,
|
|
3544
|
+
day: b.value
|
|
3545
|
+
};
|
|
3546
|
+
case "dmy": return {
|
|
3547
|
+
year: this.#year(c),
|
|
3548
|
+
month: b.value,
|
|
3549
|
+
day: a.value
|
|
3550
|
+
};
|
|
3551
|
+
case "ymd": return {
|
|
3552
|
+
year: this.#year(a),
|
|
3553
|
+
month: b.value,
|
|
3554
|
+
day: c.value
|
|
3555
|
+
};
|
|
3556
|
+
}
|
|
3557
|
+
}
|
|
3558
|
+
/** Month and day of the current year (`12/31`, `31/12`), else month and year (`3/2003`). */
|
|
3559
|
+
#twoNumbers([l, m]) {
|
|
3560
|
+
if (!l || !m) return null;
|
|
3561
|
+
const { currentYear } = this.options;
|
|
3562
|
+
if (l.value <= 12 && m.digits <= 2 && isValidDate(currentYear, l.value, m.value)) return {
|
|
3563
|
+
year: currentYear,
|
|
3564
|
+
month: l.value,
|
|
3565
|
+
day: m.value
|
|
3566
|
+
};
|
|
3567
|
+
if (l.value <= 12) return {
|
|
3568
|
+
year: this.#year(m),
|
|
3569
|
+
month: l.value,
|
|
3570
|
+
day: 1
|
|
3571
|
+
};
|
|
3572
|
+
if (l.digits <= 2 && m.value <= 12 && isValidDate(currentYear, m.value, l.value)) return {
|
|
3573
|
+
year: currentYear,
|
|
3574
|
+
month: m.value,
|
|
3575
|
+
day: l.value
|
|
3576
|
+
};
|
|
3577
|
+
if (m.value <= 12) return {
|
|
3578
|
+
year: this.#year(l),
|
|
3579
|
+
month: m.value,
|
|
3580
|
+
day: 1
|
|
3581
|
+
};
|
|
3582
|
+
return null;
|
|
3583
|
+
}
|
|
3584
|
+
};
|
|
3585
|
+
/** The time part (`10:30`, `10:30:15 PM`, `3 pm`, `10.30`, `0:00 PM`) as hours, minutes, seconds. */
|
|
3586
|
+
function readTime(items) {
|
|
3587
|
+
const values = [];
|
|
3588
|
+
let half = null;
|
|
3589
|
+
for (let i = 0; i < items.length; i++) {
|
|
3590
|
+
const item = items[i];
|
|
3591
|
+
if (!item) return null;
|
|
3592
|
+
if (item.kind === "number") {
|
|
3593
|
+
if (items[i - 1]?.kind === "number") return null;
|
|
3594
|
+
values.push(Number(item.text));
|
|
3595
|
+
} else if (item.kind === "timesep") {
|
|
3596
|
+
if (items[i - 1]?.kind !== "number" || items[i + 1]?.kind !== "number") return null;
|
|
3597
|
+
} else if (item.kind === "word" && i === items.length - 1 && AM_PM.test(item.text)) half = item.text.charAt(0).toLowerCase() === "p" ? "p" : "a";
|
|
3598
|
+
else return null;
|
|
3599
|
+
}
|
|
3600
|
+
const [hourValue, minute = 0, second = 0] = values;
|
|
3601
|
+
if (hourValue === void 0 || values.length > 3) return null;
|
|
3602
|
+
if (values.length === 1 && half === null) return null;
|
|
3603
|
+
let hour = hourValue;
|
|
3604
|
+
if (half !== null && hour <= 12) hour = half === "p" ? hour % 12 + 12 : hour % 12;
|
|
3605
|
+
if (hour > 23 || minute > 59 || second > 59) return null;
|
|
3606
|
+
return {
|
|
3607
|
+
hour,
|
|
3608
|
+
minute,
|
|
3609
|
+
second
|
|
3610
|
+
};
|
|
3611
|
+
}
|
|
3612
|
+
/** The offset of the `#` closing a date literal opened at `open`, within bounds, or -1. */
|
|
3613
|
+
function findDateClose(source, open) {
|
|
3614
|
+
const limit = Math.min(source.length, open + 64 + 2);
|
|
3615
|
+
for (let i = open + 1; i < limit; i++) {
|
|
3616
|
+
const ch = source.charAt(i);
|
|
3617
|
+
if (ch === "#") return i;
|
|
3618
|
+
if (ch === "\n" || ch === "\r") return -1;
|
|
3619
|
+
}
|
|
3620
|
+
return -1;
|
|
3621
|
+
}
|
|
3622
|
+
// src/lang/lexer/literal-types.ts
|
|
3623
|
+
/** The message VBA gives for every malformed or out-of-range number literal. */
|
|
3624
|
+
const LITERAL_SYNTAX_ERROR = "Syntax error";
|
|
3625
|
+
const SUFFIX_TYPE = {
|
|
3626
|
+
"%": "Integer",
|
|
3627
|
+
"&": "Long",
|
|
3628
|
+
"^": "LongLong",
|
|
3629
|
+
"!": "Single",
|
|
3630
|
+
"#": "Double",
|
|
3631
|
+
"@": "Currency",
|
|
3632
|
+
$: null
|
|
3633
|
+
};
|
|
3634
|
+
const INTEGER_BITS = {
|
|
3635
|
+
Integer: 16,
|
|
3636
|
+
Long: 32,
|
|
3637
|
+
LongLong: 64
|
|
3638
|
+
};
|
|
3639
|
+
/** Significant digits past which no literal of the radix fits 64 bits (bounds BigInt parsing). */
|
|
3640
|
+
const MAX_DIGITS = {
|
|
3641
|
+
8: 22,
|
|
3642
|
+
10: 20,
|
|
3643
|
+
16: 16
|
|
3644
|
+
};
|
|
3645
|
+
/** The largest Currency, in ten-thousandths. */
|
|
3646
|
+
const MAX_CURRENCY = 9223372036854775807n;
|
|
3647
|
+
/** Types a scanned number literal. */
|
|
3648
|
+
function typeNumberLiteral(text) {
|
|
3649
|
+
return text.kind === "radix" ? typeRadix(text) : typeDecimal(text);
|
|
3650
|
+
}
|
|
3651
|
+
function failed(text, error) {
|
|
3652
|
+
return {
|
|
3653
|
+
value: 0,
|
|
3654
|
+
bigValue: null,
|
|
3655
|
+
exact: null,
|
|
3656
|
+
literalType: (text.suffix === null ? null : SUFFIX_TYPE[text.suffix]) ?? (text.kind === "radix" ? "Long" : "Double"),
|
|
3657
|
+
radix: text.kind === "radix" ? text.radix : 10,
|
|
3658
|
+
suffix: text.suffix,
|
|
3659
|
+
integral: text.kind === "radix" || text.fraction === null && text.exponent === null,
|
|
3660
|
+
error
|
|
3661
|
+
};
|
|
3662
|
+
}
|
|
3663
|
+
function integer(value, literalType, radix, suffix) {
|
|
3664
|
+
const n = Number(value);
|
|
3665
|
+
return {
|
|
3666
|
+
value: n,
|
|
3667
|
+
bigValue: Number.isSafeInteger(n) ? null : value.toString(),
|
|
3668
|
+
exact: null,
|
|
3669
|
+
literalType,
|
|
3670
|
+
radix,
|
|
3671
|
+
suffix,
|
|
3672
|
+
integral: true,
|
|
3673
|
+
error: null
|
|
3674
|
+
};
|
|
3675
|
+
}
|
|
3676
|
+
/** The unsigned value of a digit string, or null when it has more digits than any type holds. */
|
|
3677
|
+
function unsignedValue(digits, radix) {
|
|
3678
|
+
const significant = digits.replace(/^0+/, "") || "0";
|
|
3679
|
+
if (significant.length > MAX_DIGITS[radix]) return null;
|
|
3680
|
+
return BigInt((radix === 16 ? "0x" : radix === 8 ? "0o" : "") + significant);
|
|
3681
|
+
}
|
|
3682
|
+
/** Two's complement: the signed value of the low `bits` bits. */
|
|
3683
|
+
function wrap(value, bits) {
|
|
3684
|
+
const size = 1n << BigInt(bits);
|
|
3685
|
+
return value >= size >> 1n ? value - size : value;
|
|
3686
|
+
}
|
|
3687
|
+
function typeRadix(text) {
|
|
3688
|
+
const declared = text.suffix === null ? null : SUFFIX_TYPE[text.suffix];
|
|
3689
|
+
const bits = declared ? INTEGER_BITS[declared] : void 0;
|
|
3690
|
+
if (text.suffix !== null && bits === void 0) return failed(text, "invalid-number");
|
|
3691
|
+
const unsigned = unsignedValue(text.digits, text.radix);
|
|
3692
|
+
if (unsigned === null) return failed(text, "overflow");
|
|
3693
|
+
const literalType = declared ?? (unsigned < 1n << 16n ? "Integer" : unsigned < 1n << 32n ? "Long" : null);
|
|
3694
|
+
const width = literalType ? INTEGER_BITS[literalType] : void 0;
|
|
3695
|
+
if (!literalType || !width) return failed(text, "overflow");
|
|
3696
|
+
if (unsigned >= (width === 64 && text.radix === 8 ? 1n << 63n : 1n << BigInt(width))) return failed(text, "overflow");
|
|
3697
|
+
return integer(wrap(unsigned, width), literalType, text.radix, text.suffix);
|
|
3698
|
+
}
|
|
3699
|
+
function typeDecimal(text) {
|
|
3700
|
+
const integral = text.fraction === null && text.exponent === null;
|
|
3701
|
+
if (text.suffix === "$") return failed(text, "invalid-number");
|
|
3702
|
+
const declared = text.suffix === null ? null : SUFFIX_TYPE[text.suffix];
|
|
3703
|
+
const bits = declared ? INTEGER_BITS[declared] : void 0;
|
|
3704
|
+
if (declared && bits) {
|
|
3705
|
+
if (!integral) return failed(text, "invalid-number");
|
|
3706
|
+
const value = unsignedValue(text.integer, 10);
|
|
3707
|
+
if (value === null || value >= 1n << BigInt(bits - 1)) return failed(text, "overflow");
|
|
3708
|
+
return integer(value, declared, 10, text.suffix);
|
|
3709
|
+
}
|
|
3710
|
+
if (declared === "Currency") return currency(text);
|
|
3711
|
+
if (declared === null && integral) {
|
|
3712
|
+
const value = unsignedValue(text.integer, 10);
|
|
3713
|
+
if (value !== null && value < 1n << 31n) return integer(value, value < 32768n ? "Integer" : "Long", 10, null);
|
|
3714
|
+
}
|
|
3715
|
+
const value = Number(floatingText(text));
|
|
3716
|
+
const literalType = declared ?? "Double";
|
|
3717
|
+
const stored = literalType === "Single" ? Math.fround(value) : value;
|
|
3718
|
+
if (!Number.isFinite(stored)) return failed(text, "overflow");
|
|
3719
|
+
return {
|
|
3720
|
+
value: stored,
|
|
3721
|
+
bigValue: null,
|
|
3722
|
+
exact: null,
|
|
3723
|
+
literalType,
|
|
3724
|
+
radix: 10,
|
|
3725
|
+
suffix: text.suffix,
|
|
3726
|
+
integral,
|
|
3727
|
+
error: null
|
|
3728
|
+
};
|
|
3729
|
+
}
|
|
3730
|
+
function floatingText(text) {
|
|
3731
|
+
const fraction = text.fraction ? `.${text.fraction}` : "";
|
|
3732
|
+
return `${text.integer || "0"}${fraction}${text.exponent ? `e${text.exponent.value}` : ""}`;
|
|
3733
|
+
}
|
|
3734
|
+
function currency(text) {
|
|
3735
|
+
const scaled = currencyUnits(text);
|
|
3736
|
+
if (scaled === null) return failed(text, "overflow");
|
|
3737
|
+
return {
|
|
3738
|
+
value: Number(scaled) / 1e4,
|
|
3739
|
+
bigValue: null,
|
|
3740
|
+
exact: {
|
|
3741
|
+
mantissa: scaled.toString(),
|
|
3742
|
+
scale: 4
|
|
3743
|
+
},
|
|
3744
|
+
literalType: "Currency",
|
|
3745
|
+
radix: 10,
|
|
3746
|
+
suffix: text.suffix,
|
|
3747
|
+
integral: text.fraction === null && text.exponent === null,
|
|
3748
|
+
error: null
|
|
3749
|
+
};
|
|
3750
|
+
}
|
|
3751
|
+
/**
|
|
3752
|
+
* The literal in ten-thousandths, rounded half to even as the VBA editor does (`1.23465@` is
|
|
3753
|
+
* 1.2346), or null when it is beyond the Currency range.
|
|
3754
|
+
*/
|
|
3755
|
+
function currencyUnits(text) {
|
|
3756
|
+
const fraction = text.fraction ?? "";
|
|
3757
|
+
const digits = (text.integer + fraction).replace(/^0+/, "");
|
|
3758
|
+
if (digits === "") return 0n;
|
|
3759
|
+
const shift = Number(text.exponent?.value ?? "0") - fraction.length + 4;
|
|
3760
|
+
let units;
|
|
3761
|
+
if (shift >= 0) {
|
|
3762
|
+
if (digits.length + shift > 19) return null;
|
|
3763
|
+
units = BigInt(digits) * 10n ** BigInt(shift);
|
|
3764
|
+
} else {
|
|
3765
|
+
const drop = -shift;
|
|
3766
|
+
if (drop > digits.length) return 0n;
|
|
3767
|
+
const kept = BigInt(digits.slice(0, digits.length - drop) || "0");
|
|
3768
|
+
const rest = digits.slice(digits.length - drop);
|
|
3769
|
+
const first = rest.charAt(0);
|
|
3770
|
+
units = first > "5" || first === "5" && (/[1-9]/.test(rest.slice(1)) || kept % 2n === 1n) ? kept + 1n : kept;
|
|
3771
|
+
}
|
|
3772
|
+
return units > MAX_CURRENCY ? null : units;
|
|
3773
|
+
}
|
|
3774
|
+
// src/lang/lexer/scanner.ts
|
|
3775
|
+
/** A cursor over the source that keeps the line and column of its position. */
|
|
3776
|
+
var Scanner = class {
|
|
3777
|
+
source;
|
|
3778
|
+
pos;
|
|
3779
|
+
line;
|
|
3780
|
+
column;
|
|
3781
|
+
constructor(source, start) {
|
|
3782
|
+
this.source = source;
|
|
3783
|
+
this.pos = start.offset;
|
|
3784
|
+
this.line = start.line;
|
|
3785
|
+
this.column = start.column;
|
|
3786
|
+
}
|
|
3787
|
+
position() {
|
|
3788
|
+
return {
|
|
3789
|
+
offset: this.pos,
|
|
3790
|
+
line: this.line,
|
|
3791
|
+
column: this.column
|
|
3792
|
+
};
|
|
3793
|
+
}
|
|
3794
|
+
/** The character `ahead` code units on, or '' past the end. */
|
|
3795
|
+
peek(ahead = 0) {
|
|
3796
|
+
return this.source.charAt(this.pos + ahead);
|
|
3797
|
+
}
|
|
3798
|
+
/** Moves over one code unit, counting a line break (`\r\n`, `\r` or `\n`) once. */
|
|
3799
|
+
advance() {
|
|
3800
|
+
const ch = this.source.charAt(this.pos);
|
|
3801
|
+
this.pos++;
|
|
3802
|
+
if (ch === "\n" || ch === "\r" && this.source.charAt(this.pos) !== "\n") {
|
|
3803
|
+
this.line++;
|
|
3804
|
+
this.column = 1;
|
|
3805
|
+
} else this.column++;
|
|
3806
|
+
}
|
|
3807
|
+
/** Moves over `count` code units known to hold no line break. */
|
|
3808
|
+
advanceBy(count) {
|
|
3809
|
+
this.pos += count;
|
|
3810
|
+
this.column += count;
|
|
3811
|
+
}
|
|
3812
|
+
/** Moves over one line break, if there is one here. */
|
|
3813
|
+
skipLineBreak() {
|
|
3814
|
+
const ch = this.peek();
|
|
3815
|
+
if (ch === "\r") {
|
|
3816
|
+
this.advance();
|
|
3817
|
+
if (this.peek() === "\n") this.advance();
|
|
3818
|
+
return true;
|
|
3819
|
+
}
|
|
3820
|
+
if (ch === "\n") {
|
|
3821
|
+
this.advance();
|
|
3822
|
+
return true;
|
|
3823
|
+
}
|
|
3824
|
+
return false;
|
|
3825
|
+
}
|
|
3826
|
+
/** The offset of the end of the current physical line (before its line break). */
|
|
3827
|
+
lineEnd() {
|
|
3828
|
+
let i = this.pos;
|
|
3829
|
+
const { source } = this;
|
|
3830
|
+
while (i < source.length) {
|
|
3831
|
+
const ch = source.charCodeAt(i);
|
|
3832
|
+
if (ch === 10 || ch === 13) break;
|
|
3833
|
+
i++;
|
|
3834
|
+
}
|
|
3835
|
+
return i;
|
|
3836
|
+
}
|
|
3837
|
+
};
|
|
3838
|
+
/** Whitespace inside a line: space, tab and the Unicode space separators ([MS-VBAL] 3.2.2). */
|
|
3839
|
+
function isSpace(ch) {
|
|
3840
|
+
return ch === " " || ch === " " || ch === " " || ch === "\xA0" || ch === "" || ch > "" && /\p{Zs}/u.test(ch);
|
|
3841
|
+
}
|
|
3842
|
+
function isLineBreak(ch) {
|
|
3843
|
+
return ch === "\n" || ch === "\r";
|
|
3844
|
+
}
|
|
3845
|
+
function isDigit(ch) {
|
|
3846
|
+
return ch >= "0" && ch <= "9";
|
|
3847
|
+
}
|
|
3848
|
+
/** A character that may start a name: an ASCII or Unicode letter. */
|
|
3849
|
+
function isNameStart(ch) {
|
|
3850
|
+
if (ch >= "a" && ch <= "z" || ch >= "A" && ch <= "Z") return true;
|
|
3851
|
+
return ch > "" && /\p{L}/u.test(ch);
|
|
3852
|
+
}
|
|
3853
|
+
/** A character that may continue a name. */
|
|
3854
|
+
function isNamePart(ch) {
|
|
3855
|
+
return isNameStart(ch) || isDigit(ch) || ch === "_" || ch > "" && /[\p{Mn}\p{Mc}\p{Nd}]/u.test(ch);
|
|
3856
|
+
}
|
|
3857
|
+
/** Whether only spaces lie between `from` and the end of its physical line. */
|
|
3858
|
+
function restOfLineIsBlank(source, from) {
|
|
3859
|
+
for (let i = from; i < source.length; i++) {
|
|
3860
|
+
const ch = source.charAt(i);
|
|
3861
|
+
if (isLineBreak(ch)) return true;
|
|
3862
|
+
if (!isSpace(ch)) return false;
|
|
3863
|
+
}
|
|
3864
|
+
return true;
|
|
3865
|
+
}
|
|
3866
|
+
// src/lang/lexer/suffix.ts
|
|
3867
|
+
/**
|
|
3868
|
+
* Whether the character after a `&` or `^` that follows a literal or name starts an operand, in
|
|
3869
|
+
* which case the character is the operator, not a type suffix (`a&b` is concatenation).
|
|
3870
|
+
*/
|
|
3871
|
+
function startsOperand(ch) {
|
|
3872
|
+
return /^[\p{L}\p{N}_(.[\]"&#]$/u.test(ch);
|
|
3873
|
+
}
|
|
3874
|
+
/** The type-declaration character ([MS-VBAL] 3.3.5.3) at `at`, if the character there is one. */
|
|
3875
|
+
function readSuffix(source, at) {
|
|
3876
|
+
const ch = source.charAt(at);
|
|
3877
|
+
if (ch === "%" || ch === "@" || ch === "$" || ch === "#") return ch;
|
|
3878
|
+
if (ch === "!") return /^[\p{L}_[]$/u.test(source.charAt(at + 1)) ? null : "!";
|
|
3879
|
+
if (ch === "&" || ch === "^") return startsOperand(source.charAt(at + 1)) ? null : ch;
|
|
3880
|
+
return null;
|
|
3881
|
+
}
|
|
3882
|
+
// src/lang/lexer/numbers.ts
|
|
3883
|
+
const HEX_DIGIT = /[0-9a-fA-F]/;
|
|
3884
|
+
const OCTAL_DIGIT = /[0-7]/;
|
|
3885
|
+
/** Scans `&H...`, `&O...` or `&...` (octal) at `start` (the `&`); null when it is not one. */
|
|
3886
|
+
function scanRadixNumber(source, start) {
|
|
3887
|
+
const marker = source.charAt(start + 1).toUpperCase();
|
|
3888
|
+
const prefixed = marker === "H" || marker === "O";
|
|
3889
|
+
if (!prefixed && !OCTAL_DIGIT.test(marker)) return null;
|
|
3890
|
+
const radix = marker === "H" ? 16 : 8;
|
|
3891
|
+
const pattern = radix === 16 ? HEX_DIGIT : OCTAL_DIGIT;
|
|
3892
|
+
const digitsStart = prefixed ? start + 2 : start + 1;
|
|
3893
|
+
let i = digitsStart;
|
|
3894
|
+
while (i < source.length && pattern.test(source.charAt(i))) i++;
|
|
3895
|
+
if (i === digitsStart) return null;
|
|
3896
|
+
const suffix = readSuffix(source, i);
|
|
3897
|
+
return {
|
|
3898
|
+
kind: "radix",
|
|
3899
|
+
radix,
|
|
3900
|
+
digits: source.slice(digitsStart, i),
|
|
3901
|
+
suffix,
|
|
3902
|
+
end: suffix ? i + 1 : i
|
|
3903
|
+
};
|
|
3904
|
+
}
|
|
3905
|
+
/** Scans a decimal literal at `start` (a digit, or `.` before a digit). */
|
|
3906
|
+
function scanDecimalNumber(source, start) {
|
|
3907
|
+
let i = start;
|
|
3908
|
+
const digits = () => {
|
|
3909
|
+
const from = i;
|
|
3910
|
+
while (i < source.length && isDigit(source.charAt(i))) i++;
|
|
3911
|
+
return source.slice(from, i);
|
|
3912
|
+
};
|
|
3913
|
+
/** Whether an exponent (`E` or `D`, an optional sign, a digit) starts at `at`. */
|
|
3914
|
+
const exponentAt = (at) => {
|
|
3915
|
+
if (!/[eEdD]/.test(source.charAt(at))) return false;
|
|
3916
|
+
const sign = source.charAt(at + 1) === "+" || source.charAt(at + 1) === "-" ? 1 : 0;
|
|
3917
|
+
return isDigit(source.charAt(at + 1 + sign));
|
|
3918
|
+
};
|
|
3919
|
+
const integer = digits();
|
|
3920
|
+
let fraction = null;
|
|
3921
|
+
if (source.charAt(i) === ".") {
|
|
3922
|
+
const after = source.charAt(i + 1);
|
|
3923
|
+
if (isDigit(after) || integer !== "" && (!/[\p{L}_[]/u.test(after) || exponentAt(i + 1))) {
|
|
3924
|
+
i++;
|
|
3925
|
+
fraction = digits();
|
|
3926
|
+
}
|
|
3927
|
+
}
|
|
3928
|
+
let exponent = null;
|
|
3929
|
+
if (exponentAt(i)) {
|
|
3930
|
+
const marker = source.charAt(i).toUpperCase() === "D" ? "D" : "E";
|
|
3931
|
+
i++;
|
|
3932
|
+
const from = i;
|
|
3933
|
+
if (source.charAt(i) === "+" || source.charAt(i) === "-") i++;
|
|
3934
|
+
digits();
|
|
3935
|
+
exponent = {
|
|
3936
|
+
marker,
|
|
3937
|
+
value: source.slice(from, i)
|
|
3938
|
+
};
|
|
3939
|
+
}
|
|
3940
|
+
const suffix = readSuffix(source, i);
|
|
3941
|
+
return {
|
|
3942
|
+
kind: "decimal",
|
|
3943
|
+
integer,
|
|
3944
|
+
fraction,
|
|
3945
|
+
exponent,
|
|
3946
|
+
suffix,
|
|
3947
|
+
end: suffix ? i + 1 : i
|
|
3948
|
+
};
|
|
3949
|
+
}
|
|
3950
|
+
// src/lang/lexer/statement-context.ts
|
|
3951
|
+
/** Statements whose `#` (right after the keyword) is a file number, never a date literal. */
|
|
3952
|
+
const FILE_STATEMENTS = /* @__PURE__ */ new Set([
|
|
3953
|
+
"print",
|
|
3954
|
+
"write",
|
|
3955
|
+
"input",
|
|
3956
|
+
"get",
|
|
3957
|
+
"put",
|
|
3958
|
+
"seek",
|
|
3959
|
+
"close",
|
|
3960
|
+
"lock",
|
|
3961
|
+
"unlock",
|
|
3962
|
+
"width"
|
|
3963
|
+
]);
|
|
3964
|
+
/** Functions whose second argument may be written `#n`: `Input(5, #1)`. */
|
|
3965
|
+
const FILE_NUMBER_FUNCTIONS = /* @__PURE__ */ new Set(["input", "inputb"]);
|
|
3966
|
+
/**
|
|
3967
|
+
* What the lexer needs to know about the statement it is in, where the meaning of a character
|
|
3968
|
+
* depends on it: `#` is a file number only after a file statement's keyword (`Print #1`, not
|
|
3969
|
+
* `Debug.Print #1/2/2003#`), after `As` in `Open`, after `,` in `Close` and in `Input(n, #f)`;
|
|
3970
|
+
* `Rem` is a comment only where a statement starts. It is fed every token in order.
|
|
3971
|
+
*/
|
|
3972
|
+
var StatementContext = class {
|
|
3973
|
+
#previous = null;
|
|
3974
|
+
#beforePrevious = null;
|
|
3975
|
+
#atStart = true;
|
|
3976
|
+
/** The keyword of the current statement when it is a file statement (`line` for Line Input). */
|
|
3977
|
+
#fileStatement = null;
|
|
3978
|
+
/** Open parentheses: true for one that opened the arguments of `Input(` or `InputB(`. */
|
|
3979
|
+
#parens = [];
|
|
3980
|
+
/** Whether the next token would be the first of a statement. */
|
|
3981
|
+
get atStatementStart() {
|
|
3982
|
+
return this.#atStart;
|
|
3983
|
+
}
|
|
3984
|
+
observe(token) {
|
|
3985
|
+
const startsStatement = this.#atStart;
|
|
3986
|
+
if (token.kind === "newline" || token.kind === "eof") {
|
|
3987
|
+
this.#parens.length = 0;
|
|
3988
|
+
this.#fileStatement = null;
|
|
3989
|
+
} else if (startsStatement && token.kind === "word" && !token.bracketed) this.#fileStatement = FILE_STATEMENTS.has(token.lower) || token.lower === "open" || token.lower === "line" ? token.lower : null;
|
|
3990
|
+
else if (token.kind === "punct" && token.value === "(") {
|
|
3991
|
+
const callee = this.#previous?.token;
|
|
3992
|
+
this.#parens.push(callee?.kind === "word" && !callee.bracketed && FILE_NUMBER_FUNCTIONS.has(callee.lower));
|
|
3993
|
+
} else if (token.kind === "punct" && token.value === ")") this.#parens.pop();
|
|
3994
|
+
this.#beforePrevious = this.#previous;
|
|
3995
|
+
this.#previous = {
|
|
3996
|
+
token,
|
|
3997
|
+
startsStatement
|
|
3998
|
+
};
|
|
3999
|
+
this.#atStart = this.#endsStatementPart(token, startsStatement);
|
|
4000
|
+
}
|
|
4001
|
+
/** Whether a statement starts after `token`: after a line end, `:`, `Then`, `Else` or a line number. */
|
|
4002
|
+
#endsStatementPart(token, startsStatement) {
|
|
4003
|
+
switch (token.kind) {
|
|
4004
|
+
case "newline": return true;
|
|
4005
|
+
case "punct": return token.value === ":";
|
|
4006
|
+
case "word": return token.keyword === "then" || token.keyword === "else";
|
|
4007
|
+
case "number": return startsStatement && token.lineStart && token.radix === 10 && token.integral;
|
|
4008
|
+
default: return false;
|
|
4009
|
+
}
|
|
4010
|
+
}
|
|
4011
|
+
/** Whether a `#` here starts a file number rather than a date literal. */
|
|
4012
|
+
fileNumberAllowed() {
|
|
4013
|
+
const previous = this.#previous;
|
|
4014
|
+
if (!previous) return false;
|
|
4015
|
+
const { token } = previous;
|
|
4016
|
+
if (token.kind === "word" && !token.bracketed) {
|
|
4017
|
+
if (previous.startsStatement) return FILE_STATEMENTS.has(token.lower);
|
|
4018
|
+
const before = this.#beforePrevious;
|
|
4019
|
+
return token.lower === "input" && before?.startsStatement === true && before.token.kind === "word" && before.token.lower === "line" || token.lower === "as" && this.#fileStatement === "open";
|
|
4020
|
+
}
|
|
4021
|
+
if (token.kind === "punct" && token.value === ",") return this.#parens.at(-1) ?? this.#fileStatement === "close";
|
|
4022
|
+
return false;
|
|
4023
|
+
}
|
|
4024
|
+
/**
|
|
4025
|
+
* Whether a file statement keyword lexed now keeps a following `#` for its file number
|
|
4026
|
+
* (`Print#1, x`), instead of taking it as a type suffix.
|
|
4027
|
+
*/
|
|
4028
|
+
keywordTakesFileNumber(lower) {
|
|
4029
|
+
if (this.#atStart) return FILE_STATEMENTS.has(lower);
|
|
4030
|
+
const previous = this.#previous;
|
|
4031
|
+
return lower === "input" && previous?.startsStatement === true && previous.token.kind === "word" && previous.token.lower === "line";
|
|
4032
|
+
}
|
|
4033
|
+
};
|
|
4034
|
+
// src/lang/lexer/lexer.ts
|
|
4035
|
+
/** The longest foreign name (`[...]`) the lexer looks for a closing bracket in. */
|
|
4036
|
+
const MAX_FOREIGN_NAME = 255;
|
|
4037
|
+
const TWO_CHAR = {
|
|
4038
|
+
":=": ":=",
|
|
4039
|
+
"<>": "<>",
|
|
4040
|
+
"<=": "<=",
|
|
4041
|
+
">=": ">=",
|
|
4042
|
+
"=<": "<=",
|
|
4043
|
+
"=>": ">=",
|
|
4044
|
+
"><": "<>"
|
|
4045
|
+
};
|
|
4046
|
+
const ONE_CHAR = /* @__PURE__ */ new Set("(),;:.!#=<>+-*/\\^&");
|
|
4047
|
+
const DIRECTIVES = {
|
|
4048
|
+
if: "if",
|
|
4049
|
+
elseif: "elseif",
|
|
4050
|
+
else: "else",
|
|
4051
|
+
end: "end",
|
|
4052
|
+
endif: "endif",
|
|
4053
|
+
const: "const"
|
|
4054
|
+
};
|
|
4055
|
+
/**
|
|
4056
|
+
* Splits module source into tokens ([MS-VBAL] 3.3), tolerating errors: every problem becomes a
|
|
4057
|
+
* diagnostic and the scan goes on. One instance reads one source once.
|
|
4058
|
+
*/
|
|
4059
|
+
var Lexer = class {
|
|
4060
|
+
source;
|
|
4061
|
+
options;
|
|
4062
|
+
diagnostics;
|
|
4063
|
+
#scanner;
|
|
4064
|
+
#dates;
|
|
4065
|
+
#context = new StatementContext();
|
|
4066
|
+
#tokens = [];
|
|
4067
|
+
#trivia = [];
|
|
4068
|
+
#spaceBefore = false;
|
|
4069
|
+
#afterContinuation = false;
|
|
4070
|
+
#lineStart = true;
|
|
4071
|
+
#continuations = 0;
|
|
4072
|
+
#last = null;
|
|
4073
|
+
constructor(source, start, options, diagnostics) {
|
|
4074
|
+
this.source = source;
|
|
4075
|
+
this.options = options;
|
|
4076
|
+
this.diagnostics = diagnostics;
|
|
4077
|
+
this.#scanner = new Scanner(source, start);
|
|
4078
|
+
this.#dates = new DateLiteralReader(options.dates);
|
|
4079
|
+
}
|
|
4080
|
+
run() {
|
|
4081
|
+
while (this.#step());
|
|
4082
|
+
return {
|
|
4083
|
+
tokens: this.#tokens,
|
|
4084
|
+
trivia: this.#trivia
|
|
4085
|
+
};
|
|
4086
|
+
}
|
|
4087
|
+
/** Reads one token or piece of trivia; false at the end of the source. */
|
|
4088
|
+
#step() {
|
|
4089
|
+
const scanner = this.#scanner;
|
|
4090
|
+
const ch = scanner.peek();
|
|
4091
|
+
const start = scanner.position();
|
|
4092
|
+
if (ch === "") {
|
|
4093
|
+
if (this.#last && this.#last.kind !== "newline") this.#push(start, { kind: "newline" });
|
|
4094
|
+
this.#push(start, { kind: "eof" });
|
|
4095
|
+
return false;
|
|
4096
|
+
}
|
|
4097
|
+
if (isSpace(ch)) {
|
|
4098
|
+
scanner.advance();
|
|
4099
|
+
this.#spaceBefore = true;
|
|
4100
|
+
} else if (isLineBreak(ch)) {
|
|
4101
|
+
scanner.skipLineBreak();
|
|
4102
|
+
this.#push(start, { kind: "newline" });
|
|
4103
|
+
} else if (ch === "_" && this.#continuationHere()) this.#readContinuation(start);
|
|
4104
|
+
else if (ch === "'") this.#readComment(start, "comment");
|
|
4105
|
+
else if (ch === "\"") this.#readString(start);
|
|
4106
|
+
else if (isDigit(ch) || ch === "." && isDigit(scanner.peek(1)) && !this.#memberDot()) this.#readNumber(start, scanDecimalNumber(this.source, scanner.pos));
|
|
4107
|
+
else if (!this.#readRadixNumber(start)) {
|
|
4108
|
+
if (ch === "[") this.#readForeignName(start);
|
|
4109
|
+
else if (isNameStart(ch)) this.#readName(start);
|
|
4110
|
+
else if (ch !== "#" || !this.#readHash(start)) this.#readPunctuation(start);
|
|
4111
|
+
}
|
|
4112
|
+
return true;
|
|
4113
|
+
}
|
|
4114
|
+
#span(start) {
|
|
4115
|
+
return {
|
|
4116
|
+
start,
|
|
4117
|
+
end: this.#scanner.position()
|
|
4118
|
+
};
|
|
4119
|
+
}
|
|
4120
|
+
#push(start, body) {
|
|
4121
|
+
const { maxTokens } = this.options.limits;
|
|
4122
|
+
if (this.#tokens.length >= maxTokens) throw new VbaLangLimitError("maxTokens", this.#tokens.length + 1, maxTokens);
|
|
4123
|
+
const token = {
|
|
4124
|
+
...body,
|
|
4125
|
+
text: this.source.slice(start.offset, this.#scanner.pos),
|
|
4126
|
+
span: this.#span(start),
|
|
4127
|
+
spaceBefore: this.#spaceBefore,
|
|
4128
|
+
afterContinuation: this.#afterContinuation,
|
|
4129
|
+
lineStart: this.#lineStart
|
|
4130
|
+
};
|
|
4131
|
+
this.#tokens.push(token);
|
|
4132
|
+
this.#context.observe(token);
|
|
4133
|
+
this.#last = token;
|
|
4134
|
+
this.#lineStart = token.kind === "newline";
|
|
4135
|
+
this.#spaceBefore = false;
|
|
4136
|
+
this.#afterContinuation = false;
|
|
4137
|
+
if (token.kind === "newline") this.#continuations = 0;
|
|
4138
|
+
}
|
|
4139
|
+
#report(code, message, start) {
|
|
4140
|
+
this.diagnostics.add(code, message, this.#span(start));
|
|
4141
|
+
}
|
|
4142
|
+
/** A `_` continues the line only after whitespace and before the end of the physical line. */
|
|
4143
|
+
#continuationHere() {
|
|
4144
|
+
const { pos } = this.#scanner;
|
|
4145
|
+
const before = this.source.charAt(pos - 1);
|
|
4146
|
+
return (before === "" || isSpace(before) || isLineBreak(before)) && restOfLineIsBlank(this.source, pos + 1);
|
|
4147
|
+
}
|
|
4148
|
+
#readContinuation(start) {
|
|
4149
|
+
const scanner = this.#scanner;
|
|
4150
|
+
scanner.advanceBy(scanner.lineEnd() - scanner.pos);
|
|
4151
|
+
scanner.skipLineBreak();
|
|
4152
|
+
this.#addTrivia("continuation", start);
|
|
4153
|
+
if (++this.#continuations === this.options.limits.maxLineContinuations + 1) this.#report("too-many-line-continuations", "Too many line continuations", start);
|
|
4154
|
+
this.#spaceBefore = true;
|
|
4155
|
+
this.#afterContinuation = true;
|
|
4156
|
+
}
|
|
4157
|
+
/** Consumes a comment to its end, following line continuations as VBA does. */
|
|
4158
|
+
#readComment(start, kind) {
|
|
4159
|
+
const scanner = this.#scanner;
|
|
4160
|
+
for (;;) {
|
|
4161
|
+
const end = scanner.lineEnd();
|
|
4162
|
+
const line = this.source.slice(scanner.pos, end);
|
|
4163
|
+
scanner.advanceBy(end - scanner.pos);
|
|
4164
|
+
if (!/(?:^|\s)_\s*$/.test(line) || !scanner.skipLineBreak()) break;
|
|
4165
|
+
}
|
|
4166
|
+
this.#addTrivia(kind, start);
|
|
4167
|
+
}
|
|
4168
|
+
#addTrivia(kind, start) {
|
|
4169
|
+
const text = this.source.slice(start.offset, this.#scanner.pos);
|
|
4170
|
+
this.#trivia.push({
|
|
4171
|
+
kind,
|
|
4172
|
+
text,
|
|
4173
|
+
span: this.#span(start)
|
|
4174
|
+
});
|
|
4175
|
+
}
|
|
4176
|
+
#readString(start) {
|
|
4177
|
+
const scanner = this.#scanner;
|
|
4178
|
+
scanner.advance();
|
|
4179
|
+
let value = "";
|
|
4180
|
+
let closed = false;
|
|
4181
|
+
while (scanner.pos < this.source.length && !isLineBreak(scanner.peek())) {
|
|
4182
|
+
const c = scanner.peek();
|
|
4183
|
+
scanner.advance();
|
|
4184
|
+
if (c === "\"") {
|
|
4185
|
+
if (scanner.peek() !== "\"") {
|
|
4186
|
+
closed = true;
|
|
4187
|
+
break;
|
|
4188
|
+
}
|
|
4189
|
+
scanner.advance();
|
|
4190
|
+
}
|
|
4191
|
+
value += c;
|
|
4192
|
+
}
|
|
4193
|
+
if (!closed) this.#report("unterminated-string", "Expected: \"", start);
|
|
4194
|
+
this.#push(start, {
|
|
4195
|
+
kind: "string",
|
|
4196
|
+
value
|
|
4197
|
+
});
|
|
4198
|
+
}
|
|
4199
|
+
/** A `.` right after a name or `)` is member access, so `a.5` is not the number `.5`. */
|
|
4200
|
+
#memberDot() {
|
|
4201
|
+
const last = this.#last;
|
|
4202
|
+
return !this.#spaceBefore && (last?.kind === "word" || last?.kind === "punct" && last.value === ")");
|
|
4203
|
+
}
|
|
4204
|
+
#readRadixNumber(start) {
|
|
4205
|
+
if (this.#scanner.peek() !== "&") return false;
|
|
4206
|
+
const text = scanRadixNumber(this.source, this.#scanner.pos);
|
|
4207
|
+
if (text) this.#readNumber(start, text);
|
|
4208
|
+
return text !== null;
|
|
4209
|
+
}
|
|
4210
|
+
#readNumber(start, text) {
|
|
4211
|
+
this.#scanner.advanceBy(text.end - this.#scanner.pos);
|
|
4212
|
+
const typed = typeNumberLiteral(text);
|
|
4213
|
+
if (typed.error) this.#report(typed.error, LITERAL_SYNTAX_ERROR, start);
|
|
4214
|
+
const { value, bigValue, exact, literalType, radix, suffix, integral } = typed;
|
|
4215
|
+
this.#push(start, {
|
|
4216
|
+
kind: "number",
|
|
4217
|
+
value,
|
|
4218
|
+
bigValue,
|
|
4219
|
+
exact,
|
|
4220
|
+
literalType,
|
|
4221
|
+
radix,
|
|
4222
|
+
suffix,
|
|
4223
|
+
integral
|
|
4224
|
+
});
|
|
4225
|
+
}
|
|
4226
|
+
#readForeignName(start) {
|
|
4227
|
+
const scanner = this.#scanner;
|
|
4228
|
+
const { source } = this;
|
|
4229
|
+
let i = scanner.pos + 1;
|
|
4230
|
+
const limit = Math.min(source.length, i + MAX_FOREIGN_NAME + 1);
|
|
4231
|
+
while (i < limit && source.charAt(i) !== "]" && !isLineBreak(source.charAt(i))) i++;
|
|
4232
|
+
if (source.charAt(i) !== "]") {
|
|
4233
|
+
scanner.advance();
|
|
4234
|
+
this.#report("unterminated-identifier", "Expected: ]", start);
|
|
4235
|
+
this.#push(start, { kind: "invalid" });
|
|
4236
|
+
return;
|
|
4237
|
+
}
|
|
4238
|
+
const name = source.slice(scanner.pos + 1, i);
|
|
4239
|
+
scanner.advanceBy(i + 1 - scanner.pos);
|
|
4240
|
+
const suffix = readSuffix(source, scanner.pos);
|
|
4241
|
+
if (suffix) scanner.advance();
|
|
4242
|
+
const lower = name.toLowerCase();
|
|
4243
|
+
this.#push(start, {
|
|
4244
|
+
kind: "word",
|
|
4245
|
+
name,
|
|
4246
|
+
lower,
|
|
4247
|
+
suffix,
|
|
4248
|
+
bracketed: true,
|
|
4249
|
+
keyword: null
|
|
4250
|
+
});
|
|
4251
|
+
}
|
|
4252
|
+
#readName(start) {
|
|
4253
|
+
const scanner = this.#scanner;
|
|
4254
|
+
const { source } = this;
|
|
4255
|
+
let i = scanner.pos + 1;
|
|
4256
|
+
while (i < source.length && isNamePart(source.charAt(i))) i++;
|
|
4257
|
+
const name = source.slice(scanner.pos, i);
|
|
4258
|
+
const lower = name.toLowerCase();
|
|
4259
|
+
const after = source.charAt(i);
|
|
4260
|
+
if (lower === "rem" && (after === "" || isSpace(after) || isLineBreak(after)) && this.#context.atStatementStart) {
|
|
4261
|
+
this.#readComment(start, "rem");
|
|
4262
|
+
return;
|
|
4263
|
+
}
|
|
4264
|
+
scanner.advanceBy(i - scanner.pos);
|
|
4265
|
+
const keyword = keywordOf(lower) ?? null;
|
|
4266
|
+
let suffix = readSuffix(source, scanner.pos);
|
|
4267
|
+
if (suffix === "#" && keyword !== null && this.#context.keywordTakesFileNumber(lower)) suffix = null;
|
|
4268
|
+
if (suffix) scanner.advance();
|
|
4269
|
+
this.#push(start, {
|
|
4270
|
+
kind: "word",
|
|
4271
|
+
name,
|
|
4272
|
+
lower,
|
|
4273
|
+
suffix,
|
|
4274
|
+
bracketed: false,
|
|
4275
|
+
keyword: suffix ? null : keyword
|
|
4276
|
+
});
|
|
4277
|
+
}
|
|
4278
|
+
/**
|
|
4279
|
+
* Reads a `#` that starts a directive or a date literal; false leaves it to be punctuation (a
|
|
4280
|
+
* file number, or a stray `#` the parser reports). A closed `#...#` that is not a valid date
|
|
4281
|
+
* is a Syntax error, as in VBA (`#2/29/2001#`).
|
|
4282
|
+
*/
|
|
4283
|
+
#readHash(start) {
|
|
4284
|
+
const scanner = this.#scanner;
|
|
4285
|
+
const { source } = this;
|
|
4286
|
+
if (this.#lineStart) {
|
|
4287
|
+
const match = /^#[ \t]*([A-Za-z]+)/.exec(source.slice(scanner.pos, scanner.pos + 16));
|
|
4288
|
+
const directive = match?.[1] ? DIRECTIVES[match[1].toLowerCase()] : void 0;
|
|
4289
|
+
if (match && directive && !isNamePart(source.charAt(scanner.pos + match[0].length))) {
|
|
4290
|
+
scanner.advanceBy(match[0].length);
|
|
4291
|
+
this.#push(start, {
|
|
4292
|
+
kind: "directive",
|
|
4293
|
+
directive
|
|
4294
|
+
});
|
|
4295
|
+
return true;
|
|
4296
|
+
}
|
|
4297
|
+
}
|
|
4298
|
+
if (this.#context.fileNumberAllowed()) return false;
|
|
4299
|
+
const close = findDateClose(source, scanner.pos);
|
|
4300
|
+
if (close < 0) return false;
|
|
4301
|
+
const parts = this.#dates.read(source.slice(scanner.pos + 1, close));
|
|
4302
|
+
scanner.advanceBy(close + 1 - scanner.pos);
|
|
4303
|
+
if (parts) this.#push(start, {
|
|
4304
|
+
kind: "date",
|
|
4305
|
+
value: dateSerial(parts),
|
|
4306
|
+
parts
|
|
4307
|
+
});
|
|
4308
|
+
else {
|
|
4309
|
+
this.#report("invalid-date", "Syntax error", start);
|
|
4310
|
+
this.#push(start, { kind: "invalid" });
|
|
4311
|
+
}
|
|
4312
|
+
return true;
|
|
4313
|
+
}
|
|
4314
|
+
#readPunctuation(start) {
|
|
4315
|
+
const scanner = this.#scanner;
|
|
4316
|
+
const pair = TWO_CHAR[this.source.slice(scanner.pos, scanner.pos + 2)];
|
|
4317
|
+
const ch = scanner.peek();
|
|
4318
|
+
if (pair) {
|
|
4319
|
+
scanner.advanceBy(2);
|
|
4320
|
+
this.#push(start, {
|
|
4321
|
+
kind: "punct",
|
|
4322
|
+
value: pair
|
|
4323
|
+
});
|
|
4324
|
+
} else if (ONE_CHAR.has(ch)) {
|
|
4325
|
+
scanner.advance();
|
|
4326
|
+
this.#push(start, {
|
|
4327
|
+
kind: "punct",
|
|
4328
|
+
value: ch
|
|
4329
|
+
});
|
|
4330
|
+
} else {
|
|
4331
|
+
const code = this.source.codePointAt(scanner.pos) ?? 0;
|
|
4332
|
+
scanner.advanceBy(code > 65535 ? 2 : 1);
|
|
4333
|
+
this.#report("invalid-character", "Invalid character", start);
|
|
4334
|
+
this.#push(start, { kind: "invalid" });
|
|
4335
|
+
}
|
|
4336
|
+
}
|
|
4337
|
+
};
|
|
4338
|
+
/** Lexes `source` from `start` (after a module header, or the beginning). */
|
|
4339
|
+
function lex(source, start, options, diagnostics) {
|
|
4340
|
+
return new Lexer(source, start, options, diagnostics).run();
|
|
4341
|
+
}
|
|
4342
|
+
// src/lang/source-unit.ts
|
|
4343
|
+
/** The start of every source. */
|
|
4344
|
+
const SOURCE_START = Object.freeze({
|
|
4345
|
+
offset: 0,
|
|
4346
|
+
line: 1,
|
|
4347
|
+
column: 1
|
|
4348
|
+
});
|
|
4349
|
+
/**
|
|
4350
|
+
* One source on its way through the front end: options resolved once, the length limit checked,
|
|
4351
|
+
* and one diagnostic bag for every stage. Each entry point (`parseModule`, `tokenize`,
|
|
4352
|
+
* `tokenizeForHighlight`, ...) starts with one.
|
|
4353
|
+
*/
|
|
4354
|
+
var SourceUnit = class {
|
|
4355
|
+
source;
|
|
4356
|
+
options;
|
|
4357
|
+
diagnostics;
|
|
4358
|
+
constructor(source, options = {}, maxDiagnostics) {
|
|
4359
|
+
this.source = source;
|
|
4360
|
+
this.options = resolveLexicalOptions(options);
|
|
4361
|
+
const { limits } = this.options;
|
|
4362
|
+
if (source.length > limits.maxSourceLength) throw new VbaLangLimitError("maxSourceLength", source.length, limits.maxSourceLength);
|
|
4363
|
+
this.diagnostics = new DiagnosticBag(maxDiagnostics ?? limits.maxDiagnostics);
|
|
4364
|
+
}
|
|
4365
|
+
/** Lexes the source from `start` (after the module header, or the beginning). */
|
|
4366
|
+
lex(start = SOURCE_START) {
|
|
4367
|
+
return lex(this.source, start, this.options, this.diagnostics);
|
|
4368
|
+
}
|
|
4369
|
+
};
|
|
4370
|
+
// src/lang/parse.ts
|
|
4371
|
+
/** Splits source into tokens without the header or conditional compilation, tolerating errors. */
|
|
4372
|
+
function tokenize(source, options = {}) {
|
|
4373
|
+
const unit = new SourceUnit(source, options);
|
|
4374
|
+
const { tokens, trivia } = unit.lex();
|
|
4375
|
+
return {
|
|
4376
|
+
tokens,
|
|
4377
|
+
trivia,
|
|
4378
|
+
diagnostics: unit.diagnostics.finish()
|
|
4379
|
+
};
|
|
4380
|
+
}
|
|
4381
|
+
/**
|
|
4382
|
+
* Parses a VBA module: an exported `.bas`/`.cls`/`.frm` text or a decompressed module stream.
|
|
4383
|
+
* Never throws for bad code: problems come back as diagnostics with a partial tree. Throws
|
|
4384
|
+
* {@link VbaLangLimitError} only when the source exceeds a hard limit.
|
|
4385
|
+
*/
|
|
4386
|
+
function parseModule(source, options = {}) {
|
|
4387
|
+
const unit = new SourceUnit(source, options);
|
|
4388
|
+
const { diagnostics } = unit;
|
|
4389
|
+
const { limits } = unit.options;
|
|
4390
|
+
const { header, end } = parseHeader(source, diagnostics, limits);
|
|
4391
|
+
const lexed = unit.lex(end);
|
|
4392
|
+
const pre = new Preprocessor(constantTable(options.constants), diagnostics, limits).run(lexed.tokens, end);
|
|
4393
|
+
const members = new Parser(pre.tokens, diagnostics, limits).module.parseMembers();
|
|
4394
|
+
const inactive = (span) => pre.inactiveRanges.some((range) => spanContains(range, span.start.offset));
|
|
4395
|
+
return {
|
|
4396
|
+
module: {
|
|
4397
|
+
kind: "Module",
|
|
4398
|
+
span: {
|
|
4399
|
+
start: SOURCE_START,
|
|
4400
|
+
end: lexed.tokens.at(-1)?.span.end ?? end
|
|
4401
|
+
},
|
|
4402
|
+
moduleKind: options.moduleKind ?? inferModuleKind({
|
|
4403
|
+
header,
|
|
4404
|
+
members
|
|
4405
|
+
}),
|
|
4406
|
+
name: attributeText(members, "vb_name"),
|
|
4407
|
+
header,
|
|
4408
|
+
members,
|
|
4409
|
+
trivia: lexed.trivia.filter((item) => !inactive(item.span)),
|
|
4410
|
+
directives: pre.directives,
|
|
4411
|
+
inactiveRanges: pre.inactiveRanges
|
|
4412
|
+
},
|
|
4413
|
+
diagnostics: diagnostics.finish().filter((diagnostic) => !inactive(diagnostic.span))
|
|
4414
|
+
};
|
|
4415
|
+
}
|
|
4416
|
+
/**
|
|
4417
|
+
* Parses a single expression (a watch, the Immediate window, a form control's formula). Returns
|
|
4418
|
+
* the expression and the problems found; text after the expression is reported as an error.
|
|
4419
|
+
*/
|
|
4420
|
+
function parseExpression(source, options = {}) {
|
|
4421
|
+
const unit = new SourceUnit(source, options);
|
|
4422
|
+
const { tokens } = unit.lex();
|
|
4423
|
+
const parser = new Parser(tokens, unit.diagnostics, unit.options.limits);
|
|
4424
|
+
const p = parser.cursor;
|
|
4425
|
+
let expression;
|
|
4426
|
+
try {
|
|
4427
|
+
expression = parser.expressions.parse();
|
|
4428
|
+
if (!p.atLineEnd()) p.expected("end of statement");
|
|
4429
|
+
else {
|
|
4430
|
+
p.skipSeparators();
|
|
4431
|
+
if (p.token.kind !== "eof") p.expected("end of expression");
|
|
4432
|
+
}
|
|
4433
|
+
} catch (error) {
|
|
4434
|
+
if (!(error instanceof NestingLimit)) throw error;
|
|
4435
|
+
const span = {
|
|
4436
|
+
start: SOURCE_START,
|
|
4437
|
+
end: tokens.at(-1)?.span.end ?? SOURCE_START
|
|
4438
|
+
};
|
|
4439
|
+
unit.diagnostics.add("too-complex", "Expression too complex", span);
|
|
4440
|
+
expression = {
|
|
4441
|
+
kind: "InvalidExpression",
|
|
4442
|
+
span: {
|
|
4443
|
+
start: SOURCE_START,
|
|
4444
|
+
end: SOURCE_START
|
|
4445
|
+
}
|
|
4446
|
+
};
|
|
4447
|
+
}
|
|
4448
|
+
return {
|
|
4449
|
+
expression,
|
|
4450
|
+
diagnostics: unit.diagnostics.finish()
|
|
4451
|
+
};
|
|
4452
|
+
}
|
|
4453
|
+
// src/lang/highlight.ts
|
|
4454
|
+
const OPERATORS = /* @__PURE__ */ new Set([
|
|
4455
|
+
"=",
|
|
4456
|
+
"<>",
|
|
4457
|
+
"<",
|
|
4458
|
+
">",
|
|
4459
|
+
"<=",
|
|
4460
|
+
">=",
|
|
4461
|
+
"+",
|
|
4462
|
+
"-",
|
|
4463
|
+
"*",
|
|
4464
|
+
"/",
|
|
4465
|
+
"\\",
|
|
4466
|
+
"^",
|
|
4467
|
+
"&",
|
|
4468
|
+
":="
|
|
4469
|
+
]);
|
|
4470
|
+
/**
|
|
4471
|
+
* Classifies the source for syntax highlighting, in source order. It never fails on bad code
|
|
4472
|
+
* (bad characters come back as `invalid`); only a source over a hard limit throws
|
|
4473
|
+
* {@link VbaLangLimitError}. Conditional compilation is not applied: every branch is coloured.
|
|
4474
|
+
*/
|
|
4475
|
+
function tokenizeForHighlight(source, options = {}) {
|
|
4476
|
+
const unit = new SourceUnit(source, options, 1);
|
|
4477
|
+
const { header, end } = parseHeader(source, unit.diagnostics, unit.options.limits);
|
|
4478
|
+
const { tokens, trivia } = unit.lex(end);
|
|
4479
|
+
const result = [];
|
|
4480
|
+
if (header) result.push({
|
|
4481
|
+
kind: "header",
|
|
4482
|
+
span: header.span
|
|
4483
|
+
});
|
|
4484
|
+
for (const token of tokens) {
|
|
4485
|
+
let kind = null;
|
|
4486
|
+
switch (token.kind) {
|
|
4487
|
+
case "word":
|
|
4488
|
+
if (token.keyword === null) kind = "identifier";
|
|
4489
|
+
else if (TYPE_KEYWORDS.has(token.keyword)) kind = "type";
|
|
4490
|
+
else kind = EXPRESSION_NAMES.has(token.keyword) && token.keyword !== "me" ? "identifier" : "keyword";
|
|
4491
|
+
break;
|
|
4492
|
+
case "number":
|
|
4493
|
+
case "string":
|
|
4494
|
+
case "date":
|
|
4495
|
+
case "directive":
|
|
4496
|
+
case "invalid":
|
|
4497
|
+
kind = token.kind;
|
|
4498
|
+
break;
|
|
4499
|
+
case "punct": kind = OPERATORS.has(token.value) ? "operator" : "punctuation";
|
|
4500
|
+
}
|
|
4501
|
+
if (kind) result.push({
|
|
4502
|
+
kind,
|
|
4503
|
+
span: token.span
|
|
4504
|
+
});
|
|
4505
|
+
}
|
|
4506
|
+
for (const item of trivia) result.push({
|
|
4507
|
+
kind: item.kind === "continuation" ? "continuation" : "comment",
|
|
4508
|
+
span: item.span
|
|
4509
|
+
});
|
|
4510
|
+
return result.sort((a, b) => a.span.start.offset - b.span.start.offset);
|
|
4511
|
+
}
|
|
4512
|
+
// src/lang/macros/auto-exec.ts
|
|
4513
|
+
const AUTO_MACROS = new Map([
|
|
4514
|
+
"Auto_Open",
|
|
4515
|
+
"Auto_Close",
|
|
4516
|
+
"Auto_Activate",
|
|
4517
|
+
"Auto_Deactivate",
|
|
4518
|
+
"AutoOpen",
|
|
4519
|
+
"AutoClose",
|
|
4520
|
+
"AutoExec",
|
|
4521
|
+
"AutoNew",
|
|
4522
|
+
"AutoExit"
|
|
4523
|
+
].map((name) => [name.toLowerCase(), name]));
|
|
4524
|
+
const AUTO_MODULES = new Map([
|
|
4525
|
+
"AutoExec",
|
|
4526
|
+
"AutoOpen",
|
|
4527
|
+
"AutoNew",
|
|
4528
|
+
"AutoClose",
|
|
4529
|
+
"AutoExit"
|
|
4530
|
+
].map((name) => [name.toLowerCase(), name]));
|
|
4531
|
+
const DOCUMENT_EVENT = /^(Workbook|Worksheet|Chart|Document|Presentation)_([A-Za-z]\w*)$/i;
|
|
4532
|
+
/**
|
|
4533
|
+
* ActiveX control events that can fire with no user action while a document opens or draws.
|
|
4534
|
+
* Form modules are left out: their controls run only once code shows the form.
|
|
4535
|
+
*/
|
|
4536
|
+
const AUTO_FIRING_CONTROL_EVENTS = new Map([
|
|
4537
|
+
"Change",
|
|
4538
|
+
"GotFocus",
|
|
4539
|
+
"Layout",
|
|
4540
|
+
"Painted",
|
|
4541
|
+
"Resize"
|
|
4542
|
+
].map((name) => [name.toLowerCase(), name]));
|
|
4543
|
+
const CONTROL_EVENT = /^([A-Za-z]\w*)_([A-Za-z]+)$/;
|
|
4544
|
+
/** Detects the procedures of one module that run without a click. */
|
|
4545
|
+
var AutoExecDetector = class {
|
|
4546
|
+
moduleKind;
|
|
4547
|
+
moduleName;
|
|
4548
|
+
host;
|
|
4549
|
+
/** The module's `WithEvents` variables, longest first. */
|
|
4550
|
+
#withEvents;
|
|
4551
|
+
constructor(members, moduleKind, moduleName, host) {
|
|
4552
|
+
this.moduleKind = moduleKind;
|
|
4553
|
+
this.moduleName = moduleName;
|
|
4554
|
+
this.host = host;
|
|
4555
|
+
const names = [];
|
|
4556
|
+
for (const member of members) if (member.kind === "VariableDeclaration") {
|
|
4557
|
+
for (const declarator of member.declarators) if (declarator.withEvents) names.push(declarator.name.name);
|
|
4558
|
+
}
|
|
4559
|
+
this.#withEvents = names.sort((a, b) => b.length - a.length);
|
|
4560
|
+
}
|
|
4561
|
+
detect(procedure) {
|
|
4562
|
+
if (procedure.procedureKind !== "Sub") return null;
|
|
4563
|
+
const name = procedure.name.name;
|
|
4564
|
+
return this.#autoMacro(name) ?? this.#withEventsHandler(name) ?? this.#documentEvent(name) ?? this.#controlEvent(name);
|
|
4565
|
+
}
|
|
4566
|
+
#autoMacro(name) {
|
|
4567
|
+
const { moduleKind, moduleName } = this;
|
|
4568
|
+
const lower = name.toLowerCase();
|
|
4569
|
+
const auto = AUTO_MACROS.get(lower);
|
|
4570
|
+
if (auto && (moduleKind === "standard" || moduleKind === "document")) {
|
|
4571
|
+
const runs = !(this.host === "excel" && moduleKind === "document" && auto.startsWith("Auto_"));
|
|
4572
|
+
return {
|
|
4573
|
+
kind: "auto-macro",
|
|
4574
|
+
object: moduleName ?? "",
|
|
4575
|
+
event: auto,
|
|
4576
|
+
runs
|
|
4577
|
+
};
|
|
4578
|
+
}
|
|
4579
|
+
const autoModule = moduleName ? AUTO_MODULES.get(moduleName.toLowerCase()) : void 0;
|
|
4580
|
+
if (autoModule && lower === "main" && moduleKind === "standard") return {
|
|
4581
|
+
kind: "auto-macro",
|
|
4582
|
+
object: autoModule,
|
|
4583
|
+
event: "Main",
|
|
4584
|
+
runs: true
|
|
4585
|
+
};
|
|
4586
|
+
return null;
|
|
4587
|
+
}
|
|
4588
|
+
/** `<variable>_<event>`, matching the longest `WithEvents` variable (`my_app_NewWorkbook`). */
|
|
4589
|
+
#withEventsHandler(name) {
|
|
4590
|
+
if (this.moduleKind === "standard") return null;
|
|
4591
|
+
const lower = name.toLowerCase();
|
|
4592
|
+
for (const variable of this.#withEvents) {
|
|
4593
|
+
const prefix = `${variable.toLowerCase()}_`;
|
|
4594
|
+
if (lower.startsWith(prefix) && lower.length > prefix.length) return {
|
|
4595
|
+
kind: "withevents-handler",
|
|
4596
|
+
object: variable,
|
|
4597
|
+
event: name.slice(prefix.length),
|
|
4598
|
+
runs: true
|
|
4599
|
+
};
|
|
4600
|
+
}
|
|
4601
|
+
return null;
|
|
4602
|
+
}
|
|
4603
|
+
#documentEvent(name) {
|
|
4604
|
+
const event = DOCUMENT_EVENT.exec(name);
|
|
4605
|
+
if (!event || this.moduleKind !== "document") return null;
|
|
4606
|
+
const object = event[1] ?? "";
|
|
4607
|
+
return {
|
|
4608
|
+
kind: "document-event",
|
|
4609
|
+
object: object.charAt(0).toUpperCase() + object.slice(1).toLowerCase(),
|
|
4610
|
+
event: event[2] ?? "",
|
|
4611
|
+
runs: true
|
|
4612
|
+
};
|
|
4613
|
+
}
|
|
4614
|
+
#controlEvent(name) {
|
|
4615
|
+
const match = CONTROL_EVENT.exec(name);
|
|
4616
|
+
const event = match?.[2] ? AUTO_FIRING_CONTROL_EVENTS.get(match[2].toLowerCase()) : void 0;
|
|
4617
|
+
if (!match || !event || this.moduleKind !== "document") return null;
|
|
4618
|
+
return {
|
|
4619
|
+
kind: "control-event",
|
|
4620
|
+
object: match[1] ?? "",
|
|
4621
|
+
event,
|
|
4622
|
+
runs: true
|
|
4623
|
+
};
|
|
4624
|
+
}
|
|
4625
|
+
};
|
|
4626
|
+
// src/lang/macros/host-rules.ts
|
|
4627
|
+
const RULES = {
|
|
4628
|
+
excel: {
|
|
4629
|
+
documentMacros: true,
|
|
4630
|
+
qualifyDocumentMacros: true
|
|
4631
|
+
},
|
|
4632
|
+
word: {
|
|
4633
|
+
documentMacros: true,
|
|
4634
|
+
qualifyDocumentMacros: false
|
|
4635
|
+
},
|
|
4636
|
+
powerpoint: {
|
|
4637
|
+
documentMacros: false,
|
|
4638
|
+
qualifyDocumentMacros: false
|
|
4639
|
+
},
|
|
4640
|
+
visio: {
|
|
4641
|
+
documentMacros: true,
|
|
4642
|
+
qualifyDocumentMacros: false
|
|
4643
|
+
},
|
|
4644
|
+
generic: {
|
|
4645
|
+
documentMacros: true,
|
|
4646
|
+
qualifyDocumentMacros: false
|
|
4647
|
+
}
|
|
4648
|
+
};
|
|
4649
|
+
/** The Macros dialog rules of one host, for one module. */
|
|
4650
|
+
var MacroListing = class {
|
|
4651
|
+
moduleKind;
|
|
4652
|
+
moduleName;
|
|
4653
|
+
#rules;
|
|
4654
|
+
#listsModule;
|
|
4655
|
+
constructor(host, moduleKind, moduleName, privateModule) {
|
|
4656
|
+
this.moduleKind = moduleKind;
|
|
4657
|
+
this.moduleName = moduleName;
|
|
4658
|
+
this.#rules = RULES[host];
|
|
4659
|
+
this.#listsModule = moduleKind === "standard" && !privateModule || moduleKind === "document" && this.#rules.documentMacros;
|
|
4660
|
+
}
|
|
4661
|
+
/**
|
|
4662
|
+
* The name the Macros dialog shows for the procedure, or null when it is not listed. A macro is
|
|
4663
|
+
* a Sub that is not Private and can be called without arguments: every parameter is Optional
|
|
4664
|
+
* and none is a ParamArray (Excel, Word and PowerPoint all list `Sub A(Optional x)`).
|
|
4665
|
+
*/
|
|
4666
|
+
macroName(procedure) {
|
|
4667
|
+
const callable = procedure.parameters.every((p) => p.optional && !p.paramArray);
|
|
4668
|
+
if (!this.#listsModule || procedure.kind !== "Sub" || procedure.scope === "Private" || !callable) return null;
|
|
4669
|
+
return this.moduleKind === "document" && this.#rules.qualifyDocumentMacros && this.moduleName ? `${this.moduleName}.${procedure.name}` : procedure.name;
|
|
4670
|
+
}
|
|
4671
|
+
};
|
|
4672
|
+
// src/lang/macros/procedures.ts
|
|
4673
|
+
function parameterInfo(parameter) {
|
|
4674
|
+
return {
|
|
4675
|
+
name: parameter.name.name,
|
|
4676
|
+
optional: parameter.optional,
|
|
4677
|
+
passing: parameter.passing ?? "ByRef",
|
|
4678
|
+
paramArray: parameter.paramArray,
|
|
4679
|
+
isArray: parameter.isArray,
|
|
4680
|
+
type: parameter.asClause?.type.name ?? null,
|
|
4681
|
+
hasDefault: parameter.defaultValue !== null
|
|
4682
|
+
};
|
|
4683
|
+
}
|
|
4684
|
+
/**
|
|
4685
|
+
* Lists the procedures of a module with what the Macros dialog and the trust UI need: which are
|
|
4686
|
+
* macros under the host's rules, the name the dialog shows, and which run without a click.
|
|
4687
|
+
*/
|
|
4688
|
+
function listProcedures(input, options = {}) {
|
|
4689
|
+
const module = typeof input === "string" ? parseModule(input, options.parse).module : input;
|
|
4690
|
+
const moduleKind = options.moduleKind ?? module.moduleKind;
|
|
4691
|
+
const moduleName = options.moduleName ?? module.name;
|
|
4692
|
+
const host = options.host ?? "generic";
|
|
4693
|
+
const listing = new MacroListing(host, moduleKind, moduleName, module.members.some((member) => member.kind === "OptionPrivateModule"));
|
|
4694
|
+
const autoExec = new AutoExecDetector(module.members, moduleKind, moduleName, host);
|
|
4695
|
+
const result = [];
|
|
4696
|
+
for (const member of module.members) {
|
|
4697
|
+
if (member.kind !== "ProcedureDeclaration") continue;
|
|
4698
|
+
const scope = member.scope ?? "Public";
|
|
4699
|
+
const name = member.name.name;
|
|
4700
|
+
const macroName = listing.macroName({
|
|
4701
|
+
kind: member.procedureKind,
|
|
4702
|
+
scope,
|
|
4703
|
+
name,
|
|
4704
|
+
parameters: member.parameters
|
|
4705
|
+
});
|
|
4706
|
+
result.push({
|
|
4707
|
+
name,
|
|
4708
|
+
kind: member.procedureKind,
|
|
4709
|
+
scope,
|
|
4710
|
+
isStatic: member.isStatic,
|
|
4711
|
+
span: member.span,
|
|
4712
|
+
nameSpan: member.name.span,
|
|
4713
|
+
parameters: member.parameters.map(parameterInfo),
|
|
4714
|
+
returnType: member.returnType?.type.name ?? null,
|
|
4715
|
+
isMacro: macroName !== null,
|
|
4716
|
+
macroName,
|
|
4717
|
+
autoExec: autoExec.detect(member)
|
|
4718
|
+
});
|
|
4719
|
+
}
|
|
4720
|
+
return result;
|
|
4721
|
+
}
|
|
4722
|
+
// src/lang/walk.ts
|
|
4723
|
+
function isNode(value) {
|
|
4724
|
+
return typeof value === "object" && value !== null && typeof value.kind === "string" && "span" in value;
|
|
4725
|
+
}
|
|
4726
|
+
/**
|
|
4727
|
+
* Calls `visit` for each direct child node of `node` (sub-expressions, statements, declarators,
|
|
4728
|
+
* names), in property order. Iterative callers use it to walk the tree without recursion.
|
|
4729
|
+
*/
|
|
4730
|
+
function forEachChild(node, visit) {
|
|
4731
|
+
for (const [key, value] of Object.entries(node)) {
|
|
4732
|
+
if (key === "span") continue;
|
|
4733
|
+
if (Array.isArray(value)) {
|
|
4734
|
+
for (const item of value) if (isNode(item)) visit(item);
|
|
4735
|
+
} else if (isNode(value)) visit(value);
|
|
4736
|
+
}
|
|
4737
|
+
}
|
|
4738
|
+
export { parseModule as a, DEFAULT_DATE_ORDER as c, DEFAULT_VBA_LANG_LIMITS as d, VbaLangLimitError as f, parseExpression as i, DEFAULT_TWO_DIGIT_YEAR_MAX as l, listProcedures as n, tokenize as o, tokenizeForHighlight as r, dateSerial as s, forEachChild as t, DEFAULT_CONDITIONAL_CONSTANTS as u };
|
|
4739
|
+
|
|
4740
|
+
//# sourceMappingURL=lang-Bo63dJb9.js.map
|