pure-dango 1.8.3 → 1.8.5
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/dist/lib/main.js +481 -0
- package/dist/lib/parser/main.js +481 -0
- package/dist/lib/tokenizer/tokenizer.js +298 -0
- package/dist/lib/tokenizer.js +298 -0
- package/package.json +2 -2
- package/src/index.ts +1 -1
package/dist/lib/main.js
ADDED
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var main_exports = {};
|
|
30
|
+
__export(main_exports, {
|
|
31
|
+
OPERATORS: () => OPERATORS,
|
|
32
|
+
next: () => next,
|
|
33
|
+
parseExpression: () => parseExpression,
|
|
34
|
+
parseStatement: () => parseStatement,
|
|
35
|
+
parser: () => parser,
|
|
36
|
+
peek: () => peek
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(main_exports);
|
|
39
|
+
var import_errors = require("../../runtime/errors");
|
|
40
|
+
var import_stdlib = require("../../runtime/stdlib");
|
|
41
|
+
var handlers = __toESM(require("./handlers"), 1);
|
|
42
|
+
var helpers = __toESM(require("./helpers"), 1);
|
|
43
|
+
const OPERATORS = {
|
|
44
|
+
// operators
|
|
45
|
+
"+": { prec: 10, assoc: "left", type: "binary" },
|
|
46
|
+
"-": { prec: 10, assoc: "left", type: "binary" },
|
|
47
|
+
"*": { prec: 20, assoc: "left", type: "binary" },
|
|
48
|
+
"/": { prec: 20, assoc: "left", type: "binary" },
|
|
49
|
+
"%": { prec: 20, assoc: "left", type: "binary" },
|
|
50
|
+
// assignment operators
|
|
51
|
+
"=": { prec: 5, assoc: "right", type: "assignment" },
|
|
52
|
+
"-=": { prec: 5, assoc: "right", type: "assignment" },
|
|
53
|
+
"+=": { prec: 5, assoc: "right", type: "assignment" },
|
|
54
|
+
"*=": { prec: 5, assoc: "right", type: "assignment" },
|
|
55
|
+
"/=": { prec: 5, assoc: "right", type: "assignment" },
|
|
56
|
+
// equality operators
|
|
57
|
+
"==": { prec: 7, assoc: "left", type: "binary" },
|
|
58
|
+
"!=": { prec: 7, assoc: "left", type: "binary" },
|
|
59
|
+
">": { prec: 8, assoc: "left", type: "binary" },
|
|
60
|
+
"<": { prec: 8, assoc: "left", type: "binary" },
|
|
61
|
+
">=": { prec: 8, assoc: "left", type: "binary" },
|
|
62
|
+
"<=": { prec: 8, assoc: "left", type: "binary" },
|
|
63
|
+
// logical operators
|
|
64
|
+
"&&": { prec: 3, assoc: "left", type: "logical" },
|
|
65
|
+
"||": { prec: 2, assoc: "left", type: "logical" },
|
|
66
|
+
"??": { prec: 1, assoc: "left", type: "logical" },
|
|
67
|
+
// unary operators
|
|
68
|
+
"!": { prec: 30, assoc: "right", type: "unary", fix: "prefix" },
|
|
69
|
+
"~": { prec: 30, assoc: "right", type: "unary", fix: "prefix" },
|
|
70
|
+
"+u": { prec: 30, assoc: "right", type: "unary", fix: "prefix" },
|
|
71
|
+
"-u": { prec: 30, assoc: "right", type: "unary", fix: "prefix" },
|
|
72
|
+
"++": { prec: 30, assoc: "right", type: "unary", fix: "both" },
|
|
73
|
+
"--": { prec: 30, assoc: "right", type: "unary", fix: "both" },
|
|
74
|
+
// bitwise operators
|
|
75
|
+
"&": { prec: 9, assoc: "left", type: "binary" },
|
|
76
|
+
"^": { prec: 8, assoc: "left", type: "binary" },
|
|
77
|
+
"|": { prec: 7, assoc: "left", type: "binary" },
|
|
78
|
+
"<<": { prec: 12, assoc: "left", type: "binary" },
|
|
79
|
+
">>": { prec: 12, assoc: "left", type: "binary" },
|
|
80
|
+
">>>": { prec: 12, assoc: "left", type: "binary" },
|
|
81
|
+
"&=": { prec: 5, assoc: "right", type: "assignment" },
|
|
82
|
+
"|=": { prec: 5, assoc: "right", type: "assignment" },
|
|
83
|
+
"^=": { prec: 5, assoc: "right", type: "assignment" },
|
|
84
|
+
"<<=": { prec: 5, assoc: "right", type: "assignment" },
|
|
85
|
+
">>=": { prec: 5, assoc: "right", type: "assignment" },
|
|
86
|
+
">>>=": { prec: 5, assoc: "right", type: "assignment" }
|
|
87
|
+
};
|
|
88
|
+
function peek(tokens, state) {
|
|
89
|
+
return tokens[state.position];
|
|
90
|
+
}
|
|
91
|
+
function next(tokens, state) {
|
|
92
|
+
if (state.position >= tokens.length)
|
|
93
|
+
return null;
|
|
94
|
+
const token = tokens[state.position++];
|
|
95
|
+
state.lastToken = token;
|
|
96
|
+
return token;
|
|
97
|
+
}
|
|
98
|
+
function isUnaryPrefix(operator) {
|
|
99
|
+
return OPERATORS[operator]?.type === "unary" && OPERATORS[operator].fix !== "postfix";
|
|
100
|
+
}
|
|
101
|
+
function parseStartingToken(token, tokens, state) {
|
|
102
|
+
if (!token)
|
|
103
|
+
(0, import_stdlib.errorTemplate)("parseStartingToken", `Unexpected end of input`);
|
|
104
|
+
let {
|
|
105
|
+
type,
|
|
106
|
+
value,
|
|
107
|
+
row,
|
|
108
|
+
column
|
|
109
|
+
} = token;
|
|
110
|
+
if (type === "Keyword" && value === "function")
|
|
111
|
+
return handlers.parseFunctionNode(tokens, state, true);
|
|
112
|
+
if (type === "Keyword" && value === "inst")
|
|
113
|
+
return handlers.parseInstantationExpression(token, tokens, state);
|
|
114
|
+
if (type === "Operator" && (value === "+" || value === "-")) {
|
|
115
|
+
const previousToken = state.lastToken;
|
|
116
|
+
const isUnaryContext = !previousToken || previousToken.type === "Keyword" || previousToken.value === "(" || previousToken.value === "," || previousToken.type === "Operator" && previousToken.value !== ")" && previousToken.value !== "++" && previousToken.value !== "--";
|
|
117
|
+
if (isUnaryContext)
|
|
118
|
+
value += "u";
|
|
119
|
+
}
|
|
120
|
+
if (type === "Literal" || type === "StringLiteral") {
|
|
121
|
+
return {
|
|
122
|
+
type,
|
|
123
|
+
value,
|
|
124
|
+
row,
|
|
125
|
+
column
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if (isUnaryPrefix(value)) {
|
|
129
|
+
const valueObject = OPERATORS[value];
|
|
130
|
+
const operand = parseExpression(tokens, valueObject.prec, state);
|
|
131
|
+
if (valueObject.fix === "both") {
|
|
132
|
+
return {
|
|
133
|
+
type: "UnaryExpression",
|
|
134
|
+
value: value.toString(),
|
|
135
|
+
argument: operand,
|
|
136
|
+
row,
|
|
137
|
+
column
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
if (valueObject.fix === "prefix") {
|
|
141
|
+
return {
|
|
142
|
+
type: "UnaryExpression",
|
|
143
|
+
value,
|
|
144
|
+
argument: operand,
|
|
145
|
+
row,
|
|
146
|
+
column
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
throw new import_errors.parseErrors.UnaryOperatorError(value, row, column);
|
|
150
|
+
}
|
|
151
|
+
if (type === "Identifier") {
|
|
152
|
+
const peekToken = peek(tokens, state);
|
|
153
|
+
if (peekToken && peekToken.value === "(")
|
|
154
|
+
return parseFunctionCall(value, tokens, state);
|
|
155
|
+
return {
|
|
156
|
+
type: "VariableReference",
|
|
157
|
+
value,
|
|
158
|
+
row,
|
|
159
|
+
column
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
if (value === "(") {
|
|
163
|
+
const expression = parseExpression(tokens, 0, state);
|
|
164
|
+
const peekToken = peek(tokens, state);
|
|
165
|
+
if (!peekToken || peekToken.value !== ")")
|
|
166
|
+
throw new import_errors.parseErrors.MissingTokenError(")", expression.row, expression.column);
|
|
167
|
+
next(tokens, state);
|
|
168
|
+
return expression;
|
|
169
|
+
}
|
|
170
|
+
if (value === "[") {
|
|
171
|
+
const elements = [];
|
|
172
|
+
while (peek(tokens, state) && peek(tokens, state).value !== "]") {
|
|
173
|
+
const element = parseExpression(tokens, 0, state, true);
|
|
174
|
+
if (element)
|
|
175
|
+
elements.push(element);
|
|
176
|
+
const peekToken = peek(tokens, state);
|
|
177
|
+
if (peekToken && peekToken.value === ",")
|
|
178
|
+
next(tokens, state);
|
|
179
|
+
else if (peekToken && peekToken.value !== "]")
|
|
180
|
+
throw new import_errors.parseErrors.UnexpectedTokenError(peekToken.value, peekToken.row, peekToken.column);
|
|
181
|
+
}
|
|
182
|
+
if (!peek(tokens, state) || peek(tokens, state).value !== "]")
|
|
183
|
+
throw new import_errors.parseErrors.MissingTokenError("]", token.row, token.column);
|
|
184
|
+
next(tokens, state);
|
|
185
|
+
return {
|
|
186
|
+
type: "ArrayLiteral",
|
|
187
|
+
elements,
|
|
188
|
+
row,
|
|
189
|
+
column
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (value === "{") {
|
|
193
|
+
const properties = [];
|
|
194
|
+
while (peek(tokens, state) && peek(tokens, state).value !== "}") {
|
|
195
|
+
const keyToken = next(tokens, state);
|
|
196
|
+
const isInvalid = !keyToken || !(keyToken.type === "Identifier" || keyToken.type === "StringLiteral" || keyToken.type === "Literal");
|
|
197
|
+
if (isInvalid)
|
|
198
|
+
throw new import_errors.parseErrors.UnexpectedTokenError(keyToken?.value, keyToken?.row ?? row, keyToken?.column ?? column);
|
|
199
|
+
if (!peek(tokens, state) || peek(tokens, state).value !== ":")
|
|
200
|
+
throw new import_errors.parseErrors.MissingTokenError(":", keyToken.row, keyToken.column);
|
|
201
|
+
next(tokens, state);
|
|
202
|
+
const value2 = parseExpression(tokens, 0, state, true);
|
|
203
|
+
properties.unshift({ key: keyToken.value, value: value2 });
|
|
204
|
+
const peekToken = peek(tokens, state);
|
|
205
|
+
if (peekToken && peekToken.value === ",")
|
|
206
|
+
next(tokens, state);
|
|
207
|
+
else if (peekToken && peekToken.value !== "}")
|
|
208
|
+
throw new import_errors.parseErrors.UnexpectedTokenError(peekToken.value, peekToken.row, peekToken.column);
|
|
209
|
+
}
|
|
210
|
+
if (!peek(tokens, state) || peek(tokens, state).value !== "}")
|
|
211
|
+
throw new import_errors.parseErrors.MissingTokenError("}", token.row, token.column);
|
|
212
|
+
next(tokens, state);
|
|
213
|
+
const node = {
|
|
214
|
+
type: "ObjectLiteral",
|
|
215
|
+
properties,
|
|
216
|
+
row,
|
|
217
|
+
column
|
|
218
|
+
};
|
|
219
|
+
return node;
|
|
220
|
+
}
|
|
221
|
+
if (value === ";")
|
|
222
|
+
return null;
|
|
223
|
+
throw new import_errors.parseErrors.UnexpectedTokenError(value, row, column);
|
|
224
|
+
}
|
|
225
|
+
function parseExpression(tokens, minimumPrecedence = 0, state, stopAtComma = false) {
|
|
226
|
+
let operatorStack = [];
|
|
227
|
+
let outputStack = [];
|
|
228
|
+
let token = next(tokens, state);
|
|
229
|
+
if (!token)
|
|
230
|
+
return null;
|
|
231
|
+
let node = parseStartingToken(token, tokens, state);
|
|
232
|
+
node = helpers.attach(node, tokens, state, stopAtComma);
|
|
233
|
+
outputStack.push(node);
|
|
234
|
+
while (true) {
|
|
235
|
+
let token2 = peek(tokens, state);
|
|
236
|
+
if (helpers.stoppingCheck(token2, stopAtComma))
|
|
237
|
+
break;
|
|
238
|
+
const operatorInfo = OPERATORS[token2.value];
|
|
239
|
+
if (!operatorInfo || operatorInfo.prec < minimumPrecedence)
|
|
240
|
+
break;
|
|
241
|
+
const operator = next(tokens, state);
|
|
242
|
+
while (outputStack.length > 0) {
|
|
243
|
+
const topOperator = operatorStack[operatorStack.length - 1];
|
|
244
|
+
const topInfo = OPERATORS[topOperator?.value] ?? null;
|
|
245
|
+
if (!topInfo)
|
|
246
|
+
break;
|
|
247
|
+
if (topInfo.type !== "assignment" && (operatorInfo.assoc === "left" && operatorInfo.prec <= topInfo.prec || operatorInfo.assoc === "right" && operatorInfo.prec < topInfo.prec)) {
|
|
248
|
+
if (topInfo.type === "unary") {
|
|
249
|
+
const operand = outputStack.pop();
|
|
250
|
+
outputStack.push(
|
|
251
|
+
{
|
|
252
|
+
type: "UnaryExpression",
|
|
253
|
+
value: topOperator.value,
|
|
254
|
+
argument: operand,
|
|
255
|
+
row: topOperator.row,
|
|
256
|
+
column: topOperator.column
|
|
257
|
+
}
|
|
258
|
+
);
|
|
259
|
+
} else {
|
|
260
|
+
const rightNode = outputStack.pop();
|
|
261
|
+
const leftNode = outputStack.pop();
|
|
262
|
+
outputStack.push(
|
|
263
|
+
{
|
|
264
|
+
type: topInfo.type === "logical" ? "LogicalExpression" : "BinaryExpression",
|
|
265
|
+
operator: topOperator.value,
|
|
266
|
+
left: leftNode,
|
|
267
|
+
right: rightNode,
|
|
268
|
+
row: topOperator.row,
|
|
269
|
+
column: topOperator.column
|
|
270
|
+
}
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
operatorStack.pop();
|
|
274
|
+
} else
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
if (operatorInfo.type === "assignment") {
|
|
278
|
+
const leftNode = outputStack.pop();
|
|
279
|
+
if (leftNode.type !== "VariableReference" && leftNode.type !== "ArrayAccess" && leftNode.type !== "MemberExpression")
|
|
280
|
+
throw new import_errors.parseErrors.AssignmentError(operator.value, operator.row, operator.column);
|
|
281
|
+
const rightNode = parseExpression(tokens, operatorInfo.prec, state, stopAtComma);
|
|
282
|
+
const baseOperators = {
|
|
283
|
+
"+=": "+",
|
|
284
|
+
"-=": "-",
|
|
285
|
+
"*=": "*",
|
|
286
|
+
"/=": "/",
|
|
287
|
+
"%=": "%",
|
|
288
|
+
"&=": "&",
|
|
289
|
+
"|=": "|",
|
|
290
|
+
"^=": "^",
|
|
291
|
+
"<<=": "<<",
|
|
292
|
+
">>=": ">>",
|
|
293
|
+
">>>=": ">>>"
|
|
294
|
+
};
|
|
295
|
+
const name = leftNode.type === "ArrayAccess" || leftNode.type === "MemberExpression" ? leftNode : leftNode.value;
|
|
296
|
+
const valueType = OPERATORS[baseOperators[operator.value]]?.type === "logical" ? "LogicalExpression" : "BinaryExpression";
|
|
297
|
+
outputStack.push(
|
|
298
|
+
{
|
|
299
|
+
type: "Assignment",
|
|
300
|
+
name,
|
|
301
|
+
value: operator.value === "=" ? rightNode : {
|
|
302
|
+
type: valueType,
|
|
303
|
+
operator: baseOperators[operator.value],
|
|
304
|
+
left: leftNode,
|
|
305
|
+
right: rightNode
|
|
306
|
+
},
|
|
307
|
+
row: operator.row,
|
|
308
|
+
column: operator.column
|
|
309
|
+
}
|
|
310
|
+
);
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
operatorStack.push(operator);
|
|
314
|
+
let nextToken = next(tokens, state);
|
|
315
|
+
if (!nextToken)
|
|
316
|
+
throw new import_errors.parseErrors.UnexpectedTokenError("end of input", operator.row, operator.column);
|
|
317
|
+
let node2 = parseStartingToken(nextToken, tokens, state);
|
|
318
|
+
if (!node2)
|
|
319
|
+
throw new import_errors.parseErrors.UnexpectedTokenError(nextToken.value, nextToken.row, nextToken.column);
|
|
320
|
+
node2 = helpers.attach(node2, tokens, state, stopAtComma);
|
|
321
|
+
outputStack.push(node2);
|
|
322
|
+
}
|
|
323
|
+
while (operatorStack.length) {
|
|
324
|
+
const operator = operatorStack.pop();
|
|
325
|
+
const operatorInfo = OPERATORS[operator.value];
|
|
326
|
+
if (operatorInfo.type === "unary") {
|
|
327
|
+
const operand = outputStack.pop();
|
|
328
|
+
outputStack.push(
|
|
329
|
+
{
|
|
330
|
+
type: "UnaryExpression",
|
|
331
|
+
value: operator.value,
|
|
332
|
+
argument: operand,
|
|
333
|
+
row: operator.row,
|
|
334
|
+
column: operator.column
|
|
335
|
+
}
|
|
336
|
+
);
|
|
337
|
+
} else {
|
|
338
|
+
const rightNode = outputStack.pop();
|
|
339
|
+
const leftNode = outputStack.pop();
|
|
340
|
+
outputStack.push(
|
|
341
|
+
{
|
|
342
|
+
type: operatorInfo.type === "logical" ? "LogicalExpression" : "BinaryExpression",
|
|
343
|
+
operator: operator.value,
|
|
344
|
+
left: leftNode,
|
|
345
|
+
right: rightNode,
|
|
346
|
+
row: operator.row,
|
|
347
|
+
column: operator.column
|
|
348
|
+
}
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
const maybeTernary = peek(tokens, state);
|
|
353
|
+
if (maybeTernary && maybeTernary.value === "?") {
|
|
354
|
+
next(tokens, state);
|
|
355
|
+
const thenBranch = parseExpression(tokens, 0, state, stopAtComma);
|
|
356
|
+
const elseColon = peek(tokens, state);
|
|
357
|
+
if (!elseColon || elseColon.value !== ":")
|
|
358
|
+
throw new import_errors.parseErrors.MissingTokenError(":", elseColon?.row ?? 0, elseColon?.column ?? 0);
|
|
359
|
+
next(tokens, state);
|
|
360
|
+
const elseBranch = parseExpression(tokens, 0, state, stopAtComma);
|
|
361
|
+
return {
|
|
362
|
+
type: "TernaryExpression",
|
|
363
|
+
condition: outputStack[0],
|
|
364
|
+
then: thenBranch,
|
|
365
|
+
else: elseBranch,
|
|
366
|
+
row: maybeTernary.row,
|
|
367
|
+
column: maybeTernary.column
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
return outputStack[0];
|
|
371
|
+
}
|
|
372
|
+
function parseFunctionCall(name, tokens, state) {
|
|
373
|
+
const row = state.lastToken?.row ?? 0;
|
|
374
|
+
const column = state.lastToken?.column ?? 0;
|
|
375
|
+
next(tokens, state);
|
|
376
|
+
let args = [];
|
|
377
|
+
const peekToken = peek(tokens, state);
|
|
378
|
+
if (peekToken && peekToken.value === ")") {
|
|
379
|
+
next(tokens, state);
|
|
380
|
+
return {
|
|
381
|
+
type: "FunctionCall",
|
|
382
|
+
name,
|
|
383
|
+
args,
|
|
384
|
+
row,
|
|
385
|
+
column
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
while (peek(tokens, state) && peek(tokens, state).value !== ")") {
|
|
389
|
+
if (peek(tokens, state)?.value === "...") {
|
|
390
|
+
next(tokens, state);
|
|
391
|
+
const expression = parseExpression(tokens, 0, state, true);
|
|
392
|
+
args.push(
|
|
393
|
+
{
|
|
394
|
+
type: "SpreadElement",
|
|
395
|
+
argument: expression,
|
|
396
|
+
row,
|
|
397
|
+
column
|
|
398
|
+
}
|
|
399
|
+
);
|
|
400
|
+
} else {
|
|
401
|
+
const expression = parseExpression(tokens, 0, state, true);
|
|
402
|
+
if (!expression) {
|
|
403
|
+
console.warn(`
|
|
404
|
+
Expected expression in function call "${name}"`);
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
args.push(expression);
|
|
408
|
+
}
|
|
409
|
+
const peekToken2 = peek(tokens, state);
|
|
410
|
+
if (peekToken2 && peekToken2.value === ",")
|
|
411
|
+
next(tokens, state);
|
|
412
|
+
else if (peekToken2 && peekToken2.value !== ")")
|
|
413
|
+
throw new import_errors.parseErrors.UnexpectedTokenError(peekToken2.value, peekToken2.row, peekToken2.column);
|
|
414
|
+
}
|
|
415
|
+
if (!peek(tokens, state) || peek(tokens, state).value !== ")") {
|
|
416
|
+
const lastToken = tokens[state.position - 1];
|
|
417
|
+
const row2 = lastToken?.row ?? 0;
|
|
418
|
+
const column2 = lastToken?.column ?? 0;
|
|
419
|
+
throw new import_errors.parseErrors.FunctionCallError(`Missing ")" in function call for name "${name}"`, row2, column2);
|
|
420
|
+
}
|
|
421
|
+
next(tokens, state);
|
|
422
|
+
if (peek(tokens, state) && peek(tokens, state).value === "(")
|
|
423
|
+
throw new import_errors.parseErrors.ChainedFunctionCallError(name, peek(tokens, state).row, peek(tokens, state).column);
|
|
424
|
+
return {
|
|
425
|
+
type: "FunctionCall",
|
|
426
|
+
name,
|
|
427
|
+
args,
|
|
428
|
+
row,
|
|
429
|
+
column
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
function parseStatement(ast, tokens, state) {
|
|
433
|
+
const token = peek(tokens, state);
|
|
434
|
+
if (!token)
|
|
435
|
+
return null;
|
|
436
|
+
if (token.type === "Keyword") {
|
|
437
|
+
if (handlers.variableHandler(ast, token, tokens, state)) return true;
|
|
438
|
+
else if (handlers.ifHandler(ast, token, tokens, state)) return true;
|
|
439
|
+
else if (handlers.doWhileHandler(ast, token, tokens, state)) return true;
|
|
440
|
+
else if (handlers.whileHandler(ast, token, tokens, state)) return true;
|
|
441
|
+
else if (handlers.loopControlHandler(ast, token, tokens, state)) return true;
|
|
442
|
+
else if (handlers.forHandler(ast, token, tokens, state)) return true;
|
|
443
|
+
else if (handlers.functionHandler(ast, token, tokens, state)) return true;
|
|
444
|
+
else if (handlers.returnHandler(ast, token, tokens, state)) return true;
|
|
445
|
+
else if (handlers.importHandler(ast, token, tokens, state)) return true;
|
|
446
|
+
else if (handlers.classHandler(ast, token, tokens, state)) return true;
|
|
447
|
+
else if (handlers.tryHandler(ast, token, tokens, state)) return true;
|
|
448
|
+
else if (handlers.switchHandler(ast, token, tokens, state)) return true;
|
|
449
|
+
}
|
|
450
|
+
const expression = parseExpression(tokens, 0, state);
|
|
451
|
+
if (expression)
|
|
452
|
+
ast.body.push(expression);
|
|
453
|
+
return expression;
|
|
454
|
+
}
|
|
455
|
+
function parser(tokens) {
|
|
456
|
+
let state = {
|
|
457
|
+
position: 0,
|
|
458
|
+
time: 0,
|
|
459
|
+
lastToken: null
|
|
460
|
+
};
|
|
461
|
+
let ast = {
|
|
462
|
+
type: "Program",
|
|
463
|
+
body: []
|
|
464
|
+
};
|
|
465
|
+
while (state.position < tokens.length) {
|
|
466
|
+
const token = peek(tokens, state);
|
|
467
|
+
if (!token)
|
|
468
|
+
break;
|
|
469
|
+
parseStatement(ast, tokens, state);
|
|
470
|
+
}
|
|
471
|
+
return ast;
|
|
472
|
+
}
|
|
473
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
474
|
+
0 && (module.exports = {
|
|
475
|
+
OPERATORS,
|
|
476
|
+
next,
|
|
477
|
+
parseExpression,
|
|
478
|
+
parseStatement,
|
|
479
|
+
parser,
|
|
480
|
+
peek
|
|
481
|
+
});
|