@tylix/compiler 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/package.json +12 -0
- package/src/ast/nodes.js +143 -0
- package/src/codegen/generateExpression.array.test.js +13 -0
- package/src/codegen/generateExpression.callexpr.test.js +19 -0
- package/src/codegen/generateExpression.js +92 -0
- package/src/codegen/generateExpression.test.js +59 -0
- package/src/codegen/generateMethod.asyncawait.test.js +37 -0
- package/src/codegen/generateMethod.ifstatement.test.js +20 -0
- package/src/codegen/generateMethod.js +13 -0
- package/src/codegen/generateMethod.test.js +79 -0
- package/src/codegen/generatePage.js +79 -0
- package/src/codegen/generatePage.onmount.test.js +33 -0
- package/src/codegen/generatePage.test.js +138 -0
- package/src/codegen/generateStatement.js +64 -0
- package/src/codegen/generateStatement.test.js +31 -0
- package/src/codegen/generateTemplate.ifeach.test.js +138 -0
- package/src/codegen/generateTemplate.js +264 -0
- package/src/codegen/generateTemplate.test.js +149 -0
- package/src/codegen/generateTemplateExpression.js +55 -0
- package/src/codegen/generateTemplateExpression.test.js +34 -0
- package/src/compileComponent.js +54 -0
- package/src/compileComponent.test.js +121 -0
- package/src/index.js +15 -0
- package/src/lexer/Lexer.js +336 -0
- package/src/lexer/Lexer.test.js +151 -0
- package/src/lexer/tokenTypes.js +114 -0
- package/src/parser/Parser.array.test.js +37 -0
- package/src/parser/Parser.asyncawait.test.js +33 -0
- package/src/parser/Parser.ifstatement.test.js +20 -0
- package/src/parser/Parser.js +677 -0
- package/src/parser/Parser.onmount.test.js +18 -0
- package/src/parser/Parser.test.js +227 -0
- package/src/parser/parseComponent.js +29 -0
- package/src/parser/parseComponent.test.js +53 -0
- package/src/parser/parseComponentFile.js +55 -0
- package/src/parser/parseExpressionString.js +13 -0
- package/src/parser/parsePageFile.js +94 -0
- package/src/parser/parsePageFile.test.js +101 -0
- package/src/parser/parseTemplate.ifeach.test.js +33 -0
- package/src/parser/parseTemplate.js +319 -0
- package/src/parser/parseTemplate.pre.test.js +39 -0
- package/src/parser/parseTemplate.test.js +96 -0
- package/src/renderPageDocument.js +250 -0
- package/src/renderPageDocument.test.js +125 -0
- package/src/runtime/loadingIndicator.js +35 -0
- package/src/runtime/reactive.js +59 -0
- package/src/runtime/reactive.test.js +106 -0
- package/src/runtime/useApi.js +45 -0
- package/src/runtime/useChannel.js +38 -0
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
import { TokenType } from '../lexer/tokenTypes.js'
|
|
2
|
+
import {
|
|
3
|
+
PageNode,
|
|
4
|
+
PropNode,
|
|
5
|
+
StateNode,
|
|
6
|
+
MethodNode,
|
|
7
|
+
AssignmentExpr,
|
|
8
|
+
BinaryExpr,
|
|
9
|
+
UnaryExpr,
|
|
10
|
+
TernaryExpr,
|
|
11
|
+
ArrowFunctionExpr,
|
|
12
|
+
TemplateLiteralExpr,
|
|
13
|
+
Identifier,
|
|
14
|
+
MemberExpr,
|
|
15
|
+
Literal,
|
|
16
|
+
ReturnStatement,
|
|
17
|
+
ExpressionStatement,
|
|
18
|
+
CallExpr,
|
|
19
|
+
NewExpr,
|
|
20
|
+
AwaitExpr,
|
|
21
|
+
VariableDeclaration,
|
|
22
|
+
ObjectExpr,
|
|
23
|
+
ArrayExpr,
|
|
24
|
+
IfStatement,
|
|
25
|
+
ForInStatement,
|
|
26
|
+
ForRangeStatement,
|
|
27
|
+
RepeatStatement,
|
|
28
|
+
BreakStatement,
|
|
29
|
+
ContinueStatement,
|
|
30
|
+
} from '../ast/nodes.js'
|
|
31
|
+
import { parseExpressionString } from './parseExpressionString.js'
|
|
32
|
+
|
|
33
|
+
const COMPARISON_SYMBOLS = {
|
|
34
|
+
[TokenType.GT]: '>',
|
|
35
|
+
[TokenType.LT]: '<',
|
|
36
|
+
[TokenType.GTE]: '>=',
|
|
37
|
+
[TokenType.LTE]: '<=',
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const ADDITIVE_SYMBOLS = {
|
|
41
|
+
[TokenType.PLUS]: '+',
|
|
42
|
+
[TokenType.MINUS]: '-',
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const MULTIPLICATIVE_SYMBOLS = {
|
|
46
|
+
[TokenType.STAR]: '*',
|
|
47
|
+
[TokenType.SLASH]: '/',
|
|
48
|
+
[TokenType.PERCENT]: '%',
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Compound assignment desugars at parse time into a plain
|
|
52
|
+
// AssignmentExpression whose value is a BinaryExpression -- e.g.
|
|
53
|
+
// `this.count += 1` becomes exactly the same AST as if the dev had
|
|
54
|
+
// written `this.count = this.count + 1`, so no codegen changes are
|
|
55
|
+
// needed at all; generateExpression already knows how to emit both
|
|
56
|
+
// node types.
|
|
57
|
+
const COMPOUND_ASSIGNMENT_OPS = {
|
|
58
|
+
[TokenType.PLUS_EQUALS]: '+',
|
|
59
|
+
[TokenType.MINUS_EQUALS]: '-',
|
|
60
|
+
[TokenType.STAR_EQUALS]: '*',
|
|
61
|
+
[TokenType.SLASH_EQUALS]: '/',
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Parses the token stream from the Lexer into a Page AST.
|
|
66
|
+
*
|
|
67
|
+
* Expressions are parsed with real precedence-climbing, low to high:
|
|
68
|
+
* arrow function (x => expr, (a, b) => expr) -- tried first, with
|
|
69
|
+
* backtracking, since it can start with the same tokens as a
|
|
70
|
+
* grouped expression or a bare identifier
|
|
71
|
+
* assignment (= += -= *= /=)
|
|
72
|
+
* ternary (?:)
|
|
73
|
+
* logical or (or)
|
|
74
|
+
* logical and (and)
|
|
75
|
+
* equality (is / is not)
|
|
76
|
+
* comparison (> < >= <=)
|
|
77
|
+
* predicate (has / exists / missing)
|
|
78
|
+
* additive (+ -)
|
|
79
|
+
* multiplicative (* / %)
|
|
80
|
+
* unary (not, -, await)
|
|
81
|
+
* member/call (a.b, a())
|
|
82
|
+
* primary (literals, identifiers, grouping, arrays, objects,
|
|
83
|
+
* template literals)
|
|
84
|
+
*/
|
|
85
|
+
export class Parser {
|
|
86
|
+
constructor(tokens) {
|
|
87
|
+
this.tokens = tokens
|
|
88
|
+
this.pos = 0
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
parse() {
|
|
92
|
+
const page = {
|
|
93
|
+
props: [],
|
|
94
|
+
state: [],
|
|
95
|
+
computed: [],
|
|
96
|
+
actions: [],
|
|
97
|
+
onMount: null,
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
while (!this.check(TokenType.EOF)) {
|
|
101
|
+
if (this.match(TokenType.PROPS)) {
|
|
102
|
+
page.props = this.parseSectionBlock(this.parsePropEntry.bind(this))
|
|
103
|
+
} else if (this.match(TokenType.STATE)) {
|
|
104
|
+
page.state = this.parseSectionBlock(this.parseStateEntry.bind(this))
|
|
105
|
+
} else if (this.match(TokenType.COMPUTED)) {
|
|
106
|
+
page.computed = this.parseSectionBlock(this.parseMethod.bind(this))
|
|
107
|
+
} else if (this.match(TokenType.ACTION)) {
|
|
108
|
+
page.actions = this.parseSectionBlock(this.parseMethod.bind(this))
|
|
109
|
+
} else if (this.match(TokenType.ONMOUNT)) {
|
|
110
|
+
page.onMount = this.parseOnMountBody()
|
|
111
|
+
} else {
|
|
112
|
+
throw new Error(
|
|
113
|
+
`Unexpected token ${this.peek().type} at line ${this.peek().line}`,
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return PageNode(page)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
parseOnMountBody() {
|
|
122
|
+
const isAsync = this.match(TokenType.ASYNC)
|
|
123
|
+
if (this.check(TokenType.LBRACE)) {
|
|
124
|
+
this.expect(TokenType.LBRACE, "Expected '{' to start onMount body")
|
|
125
|
+
const body = []
|
|
126
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
127
|
+
body.push(this.parseStatement())
|
|
128
|
+
}
|
|
129
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close onMount body")
|
|
130
|
+
return { body, isAsync }
|
|
131
|
+
}
|
|
132
|
+
const body = []
|
|
133
|
+
while (!this.isSectionKeyword(this.peek().type)) {
|
|
134
|
+
body.push(this.parseStatement())
|
|
135
|
+
}
|
|
136
|
+
return { body, isAsync }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
parseSectionBlock(parseEntry) {
|
|
140
|
+
if (this.check(TokenType.LBRACE)) {
|
|
141
|
+
return this.parseBlock(parseEntry)
|
|
142
|
+
}
|
|
143
|
+
return this.parseBareBlock(parseEntry)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
parseBlock(parseEntry) {
|
|
147
|
+
this.expect(TokenType.LBRACE, "Expected '{' to start block")
|
|
148
|
+
const entries = []
|
|
149
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
150
|
+
entries.push(parseEntry())
|
|
151
|
+
this.match(TokenType.COMMA)
|
|
152
|
+
}
|
|
153
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close block")
|
|
154
|
+
return entries
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
isSectionKeyword(type) {
|
|
158
|
+
return (
|
|
159
|
+
type === TokenType.PROPS ||
|
|
160
|
+
type === TokenType.STATE ||
|
|
161
|
+
type === TokenType.COMPUTED ||
|
|
162
|
+
type === TokenType.ACTION ||
|
|
163
|
+
type === TokenType.ONMOUNT ||
|
|
164
|
+
type === TokenType.EOF
|
|
165
|
+
)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
parseBareBlock(parseEntry) {
|
|
169
|
+
const entries = []
|
|
170
|
+
while (!this.isSectionKeyword(this.peek().type)) {
|
|
171
|
+
entries.push(parseEntry())
|
|
172
|
+
this.match(TokenType.COMMA)
|
|
173
|
+
}
|
|
174
|
+
return entries
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
parsePropEntry() {
|
|
178
|
+
const name = this.expect(TokenType.IDENTIFIER, 'Expected prop name').value
|
|
179
|
+
this.expect(TokenType.COLON, "Expected ':' after prop name")
|
|
180
|
+
const propType = this.expect(
|
|
181
|
+
TokenType.IDENTIFIER,
|
|
182
|
+
'Expected prop type',
|
|
183
|
+
).value
|
|
184
|
+
return PropNode(name, propType)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
parseStateEntry() {
|
|
188
|
+
const name = this.expect(TokenType.IDENTIFIER, 'Expected state name').value
|
|
189
|
+
this.expect(TokenType.COLON, "Expected ':' after state name")
|
|
190
|
+
if (this.match(TokenType.MINUS)) {
|
|
191
|
+
const num = this.expect(
|
|
192
|
+
TokenType.NUMBER,
|
|
193
|
+
"Expected a number after '-'",
|
|
194
|
+
).value
|
|
195
|
+
return StateNode(name, Literal(-num))
|
|
196
|
+
}
|
|
197
|
+
const value = this.parsePrimary()
|
|
198
|
+
return StateNode(name, value)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
parseMethod() {
|
|
202
|
+
const isAsync = this.match(TokenType.ASYNC)
|
|
203
|
+
const name = this.expect(TokenType.IDENTIFIER, 'Expected method name').value
|
|
204
|
+
this.expect(TokenType.LPAREN, "Expected '(' after method name")
|
|
205
|
+
const params = []
|
|
206
|
+
while (!this.check(TokenType.RPAREN)) {
|
|
207
|
+
params.push(
|
|
208
|
+
this.expect(TokenType.IDENTIFIER, 'Expected parameter name').value,
|
|
209
|
+
)
|
|
210
|
+
this.match(TokenType.COMMA)
|
|
211
|
+
}
|
|
212
|
+
this.expect(TokenType.RPAREN, "Expected ')' after parameters")
|
|
213
|
+
this.expect(TokenType.LBRACE, "Expected '{' to start method body")
|
|
214
|
+
|
|
215
|
+
const body = []
|
|
216
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
217
|
+
body.push(this.parseStatement())
|
|
218
|
+
}
|
|
219
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close method body")
|
|
220
|
+
|
|
221
|
+
return { ...MethodNode(name, params, body), isAsync }
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
parseStatement() {
|
|
225
|
+
if (this.match(TokenType.RETURN)) {
|
|
226
|
+
const hasValue =
|
|
227
|
+
!this.check(TokenType.SEMICOLON) && !this.check(TokenType.RBRACE)
|
|
228
|
+
const argument = hasValue ? this.parseExpression() : null
|
|
229
|
+
this.match(TokenType.SEMICOLON)
|
|
230
|
+
return ReturnStatement(argument)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (this.match(TokenType.IF)) {
|
|
234
|
+
return this.parseIfStatement()
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (this.match(TokenType.FOR)) {
|
|
238
|
+
return this.parseForStatement()
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (this.match(TokenType.REPEAT)) {
|
|
242
|
+
return this.parseRepeatStatement()
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (this.match(TokenType.BREAK)) {
|
|
246
|
+
this.match(TokenType.SEMICOLON)
|
|
247
|
+
return BreakStatement()
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (this.match(TokenType.CONTINUE)) {
|
|
251
|
+
this.match(TokenType.SEMICOLON)
|
|
252
|
+
return ContinueStatement()
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (this.check(TokenType.CONST) || this.check(TokenType.LET)) {
|
|
256
|
+
const kind = this.advance().type === TokenType.CONST ? 'const' : 'let'
|
|
257
|
+
const name = this.expect(
|
|
258
|
+
TokenType.IDENTIFIER,
|
|
259
|
+
'Expected variable name',
|
|
260
|
+
).value
|
|
261
|
+
this.expect(TokenType.EQUALS, "Expected '=' in variable declaration")
|
|
262
|
+
const init = this.parseExpression()
|
|
263
|
+
this.match(TokenType.SEMICOLON)
|
|
264
|
+
return VariableDeclaration(kind, name, init)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const expression = this.parseExpression()
|
|
268
|
+
this.match(TokenType.SEMICOLON)
|
|
269
|
+
return ExpressionStatement(expression)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
parseIfStatement() {
|
|
273
|
+
const hasParen = this.match(TokenType.LPAREN)
|
|
274
|
+
const condition = this.parseExpression()
|
|
275
|
+
if (hasParen) {
|
|
276
|
+
this.expect(TokenType.RPAREN, "Expected ')' after if condition")
|
|
277
|
+
}
|
|
278
|
+
this.expect(TokenType.LBRACE, "Expected '{' to start if body")
|
|
279
|
+
const consequent = []
|
|
280
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
281
|
+
consequent.push(this.parseStatement())
|
|
282
|
+
}
|
|
283
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close if body")
|
|
284
|
+
|
|
285
|
+
let alternate = null
|
|
286
|
+
if (this.match(TokenType.ELSE)) {
|
|
287
|
+
if (this.check(TokenType.IF)) {
|
|
288
|
+
this.advance()
|
|
289
|
+
alternate = [this.parseIfStatement()]
|
|
290
|
+
} else {
|
|
291
|
+
this.expect(TokenType.LBRACE, "Expected '{' to start else body")
|
|
292
|
+
alternate = []
|
|
293
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
294
|
+
alternate.push(this.parseStatement())
|
|
295
|
+
}
|
|
296
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close else body")
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return IfStatement(condition, consequent, alternate)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
parseForStatement() {
|
|
304
|
+
const varName = this.expect(
|
|
305
|
+
TokenType.IDENTIFIER,
|
|
306
|
+
"Expected a loop variable name after 'for'",
|
|
307
|
+
).value
|
|
308
|
+
|
|
309
|
+
if (this.match(TokenType.IN)) {
|
|
310
|
+
const iterable = this.parseExpression()
|
|
311
|
+
this.expect(TokenType.LBRACE, "Expected '{' to start the loop body")
|
|
312
|
+
const body = []
|
|
313
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
314
|
+
body.push(this.parseStatement())
|
|
315
|
+
}
|
|
316
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close the loop body")
|
|
317
|
+
return ForInStatement(varName, iterable, body)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (this.match(TokenType.FROM)) {
|
|
321
|
+
const start = this.parseExpression()
|
|
322
|
+
this.expect(TokenType.TO, "Expected 'to' after the loop's starting value")
|
|
323
|
+
const end = this.parseExpression()
|
|
324
|
+
this.expect(TokenType.LBRACE, "Expected '{' to start the loop body")
|
|
325
|
+
const body = []
|
|
326
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
327
|
+
body.push(this.parseStatement())
|
|
328
|
+
}
|
|
329
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close the loop body")
|
|
330
|
+
return ForRangeStatement(varName, start, end, body)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
throw new Error(
|
|
334
|
+
`Expected 'in' or 'from' after 'for ${varName}' at line ${this.peek().line}`,
|
|
335
|
+
)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
parseRepeatStatement() {
|
|
339
|
+
const count = this.parseExpression()
|
|
340
|
+
this.expect(TokenType.LBRACE, "Expected '{' to start the repeat body")
|
|
341
|
+
const body = []
|
|
342
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
343
|
+
body.push(this.parseStatement())
|
|
344
|
+
}
|
|
345
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close the repeat body")
|
|
346
|
+
return RepeatStatement(count, body)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
parseExpression() {
|
|
350
|
+
return this.parseAssignment()
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
parseAssignment() {
|
|
354
|
+
const arrow = this.tryParseArrowFunction()
|
|
355
|
+
if (arrow) return arrow
|
|
356
|
+
|
|
357
|
+
const left = this.parseTernary()
|
|
358
|
+
|
|
359
|
+
if (this.match(TokenType.EQUALS)) {
|
|
360
|
+
const value = this.parseAssignment()
|
|
361
|
+
return AssignmentExpr(left, value)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const compoundOp = COMPOUND_ASSIGNMENT_OPS[this.peek().type]
|
|
365
|
+
if (compoundOp) {
|
|
366
|
+
this.advance()
|
|
367
|
+
const value = this.parseAssignment()
|
|
368
|
+
return AssignmentExpr(left, BinaryExpr(compoundOp, left, value))
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return left
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Arrow functions are tried with backtracking before falling
|
|
375
|
+
// through to the normal ternary/assignment chain, since both forms
|
|
376
|
+
// (`x => ...` and `(a, b) => ...`) start with tokens that are
|
|
377
|
+
// otherwise perfectly valid as a bare identifier or a grouped
|
|
378
|
+
// expression. v1 scope is expression-body only (no `{ ... }` block
|
|
379
|
+
// body), matching a single call to parseAssignment for the body --
|
|
380
|
+
// deliberately narrower than full JS arrow functions, to avoid
|
|
381
|
+
// reopening the entire statement grammar inside an expression
|
|
382
|
+
// position.
|
|
383
|
+
tryParseArrowFunction() {
|
|
384
|
+
if (this.check(TokenType.IDENTIFIER)) {
|
|
385
|
+
const next = this.tokens[this.pos + 1]
|
|
386
|
+
if (next && next.type === TokenType.ARROW) {
|
|
387
|
+
const param = this.advance().value
|
|
388
|
+
this.advance() // consume '=>'
|
|
389
|
+
const body = this.parseAssignment()
|
|
390
|
+
return ArrowFunctionExpr([param], body)
|
|
391
|
+
}
|
|
392
|
+
return null
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (this.check(TokenType.LPAREN)) {
|
|
396
|
+
const savedPos = this.pos
|
|
397
|
+
this.pos++ // consume '('
|
|
398
|
+
|
|
399
|
+
const params = []
|
|
400
|
+
let isValidParamList = true
|
|
401
|
+
|
|
402
|
+
if (!this.check(TokenType.RPAREN)) {
|
|
403
|
+
while (true) {
|
|
404
|
+
if (!this.check(TokenType.IDENTIFIER)) {
|
|
405
|
+
isValidParamList = false
|
|
406
|
+
break
|
|
407
|
+
}
|
|
408
|
+
params.push(this.advance().value)
|
|
409
|
+
if (this.match(TokenType.COMMA)) continue
|
|
410
|
+
break
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (isValidParamList && this.check(TokenType.RPAREN)) {
|
|
415
|
+
this.pos++ // consume ')'
|
|
416
|
+
if (this.check(TokenType.ARROW)) {
|
|
417
|
+
this.pos++ // consume '=>'
|
|
418
|
+
const body = this.parseAssignment()
|
|
419
|
+
return ArrowFunctionExpr(params, body)
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Not actually an arrow function (either the parameter list
|
|
424
|
+
// wasn't a plain comma-separated identifier list, or there was
|
|
425
|
+
// no '=>' after the closing paren) -- back out completely so
|
|
426
|
+
// the normal parsePrimary grouped-expression path handles it.
|
|
427
|
+
this.pos = savedPos
|
|
428
|
+
return null
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
return null
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
parseTernary() {
|
|
435
|
+
const condition = this.parseLogicalOr()
|
|
436
|
+
if (this.match(TokenType.QUESTION)) {
|
|
437
|
+
const consequent = this.parseAssignment()
|
|
438
|
+
this.expect(TokenType.COLON, "Expected ':' in ternary expression")
|
|
439
|
+
const alternate = this.parseAssignment()
|
|
440
|
+
return TernaryExpr(condition, consequent, alternate)
|
|
441
|
+
}
|
|
442
|
+
return condition
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
parseLogicalOr() {
|
|
446
|
+
let left = this.parseLogicalAnd()
|
|
447
|
+
while (this.match(TokenType.OR)) {
|
|
448
|
+
const right = this.parseLogicalAnd()
|
|
449
|
+
left = BinaryExpr('||', left, right)
|
|
450
|
+
}
|
|
451
|
+
return left
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
parseLogicalAnd() {
|
|
455
|
+
let left = this.parseEquality()
|
|
456
|
+
while (this.match(TokenType.AND)) {
|
|
457
|
+
const right = this.parseEquality()
|
|
458
|
+
left = BinaryExpr('&&', left, right)
|
|
459
|
+
}
|
|
460
|
+
return left
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
parseEquality() {
|
|
464
|
+
let left = this.parseComparison()
|
|
465
|
+
while (this.check(TokenType.IS)) {
|
|
466
|
+
this.advance()
|
|
467
|
+
const negated = this.match(TokenType.NOT)
|
|
468
|
+
const right = this.parseComparison()
|
|
469
|
+
left = BinaryExpr(negated ? '!==' : '===', left, right)
|
|
470
|
+
}
|
|
471
|
+
return left
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
parseComparison() {
|
|
475
|
+
let left = this.parsePredicate()
|
|
476
|
+
while (COMPARISON_SYMBOLS[this.peek().type]) {
|
|
477
|
+
const operator = COMPARISON_SYMBOLS[this.advance().type]
|
|
478
|
+
const right = this.parsePredicate()
|
|
479
|
+
left = BinaryExpr(operator, left, right)
|
|
480
|
+
}
|
|
481
|
+
return left
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
parsePredicate() {
|
|
485
|
+
const left = this.parseAdditive()
|
|
486
|
+
|
|
487
|
+
if (this.match(TokenType.EXISTS)) {
|
|
488
|
+
return BinaryExpr('!=', left, Literal(null))
|
|
489
|
+
}
|
|
490
|
+
if (this.match(TokenType.MISSING)) {
|
|
491
|
+
return BinaryExpr('==', left, Literal(null))
|
|
492
|
+
}
|
|
493
|
+
if (this.match(TokenType.HAS)) {
|
|
494
|
+
const right = this.parseAdditive()
|
|
495
|
+
return CallExpr(MemberExpr(left, 'includes'), [right])
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return left
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
parseAdditive() {
|
|
502
|
+
let left = this.parseMultiplicative()
|
|
503
|
+
while (ADDITIVE_SYMBOLS[this.peek().type]) {
|
|
504
|
+
const operator = ADDITIVE_SYMBOLS[this.advance().type]
|
|
505
|
+
const right = this.parseMultiplicative()
|
|
506
|
+
left = BinaryExpr(operator, left, right)
|
|
507
|
+
}
|
|
508
|
+
return left
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
parseMultiplicative() {
|
|
512
|
+
let left = this.parseUnary()
|
|
513
|
+
while (MULTIPLICATIVE_SYMBOLS[this.peek().type]) {
|
|
514
|
+
const operator = MULTIPLICATIVE_SYMBOLS[this.advance().type]
|
|
515
|
+
const right = this.parseUnary()
|
|
516
|
+
left = BinaryExpr(operator, left, right)
|
|
517
|
+
}
|
|
518
|
+
return left
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
parseUnary() {
|
|
522
|
+
if (this.match(TokenType.AWAIT)) {
|
|
523
|
+
return AwaitExpr(this.parseUnary())
|
|
524
|
+
}
|
|
525
|
+
if (this.match(TokenType.NOT)) {
|
|
526
|
+
return UnaryExpr('!', this.parseUnary())
|
|
527
|
+
}
|
|
528
|
+
if (this.match(TokenType.MINUS)) {
|
|
529
|
+
return UnaryExpr('-', this.parseUnary())
|
|
530
|
+
}
|
|
531
|
+
return this.parseMemberExpression()
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
parseMemberExpression() {
|
|
535
|
+
let expr = this.parsePrimary()
|
|
536
|
+
while (true) {
|
|
537
|
+
if (this.match(TokenType.DOT)) {
|
|
538
|
+
const property = this.expect(
|
|
539
|
+
TokenType.IDENTIFIER,
|
|
540
|
+
"Expected a property name after '.'",
|
|
541
|
+
).value
|
|
542
|
+
expr = MemberExpr(expr, property)
|
|
543
|
+
continue
|
|
544
|
+
}
|
|
545
|
+
if (this.match(TokenType.LBRACKET)) {
|
|
546
|
+
const property = this.parseExpression()
|
|
547
|
+
this.expect(
|
|
548
|
+
TokenType.RBRACKET,
|
|
549
|
+
"Expected ']' to close computed member access",
|
|
550
|
+
)
|
|
551
|
+
expr = MemberExpr(expr, property, true)
|
|
552
|
+
continue
|
|
553
|
+
}
|
|
554
|
+
if (this.check(TokenType.LPAREN)) {
|
|
555
|
+
expr = CallExpr(expr, this.parseCallArguments())
|
|
556
|
+
continue
|
|
557
|
+
}
|
|
558
|
+
break
|
|
559
|
+
}
|
|
560
|
+
return expr
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
parseCallArguments() {
|
|
564
|
+
this.expect(TokenType.LPAREN, "Expected '(' to start call arguments")
|
|
565
|
+
const args = []
|
|
566
|
+
while (!this.check(TokenType.RPAREN)) {
|
|
567
|
+
args.push(this.parseExpression())
|
|
568
|
+
this.match(TokenType.COMMA)
|
|
569
|
+
}
|
|
570
|
+
this.expect(TokenType.RPAREN, "Expected ')' to close call arguments")
|
|
571
|
+
return args
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
parsePrimary() {
|
|
575
|
+
if (this.match(TokenType.NEW)) {
|
|
576
|
+
let callee = this.parsePrimary()
|
|
577
|
+
while (this.match(TokenType.DOT)) {
|
|
578
|
+
const property = this.expect(
|
|
579
|
+
TokenType.IDENTIFIER,
|
|
580
|
+
"Expected a property name after '.'",
|
|
581
|
+
).value
|
|
582
|
+
callee = MemberExpr(callee, property)
|
|
583
|
+
}
|
|
584
|
+
let args = []
|
|
585
|
+
if (this.check(TokenType.LPAREN)) {
|
|
586
|
+
args = this.parseCallArguments()
|
|
587
|
+
}
|
|
588
|
+
return NewExpr(callee, args)
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
if (this.check(TokenType.NUMBER) || this.check(TokenType.STRING)) {
|
|
592
|
+
return Literal(this.advance().value)
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
if (this.check(TokenType.TEMPLATE_LITERAL)) {
|
|
596
|
+
const rawParts = this.advance().value
|
|
597
|
+
const parts = rawParts.map((p) =>
|
|
598
|
+
p.type === 'text'
|
|
599
|
+
? p
|
|
600
|
+
: { type: 'expr', expression: parseExpressionString(p.source) },
|
|
601
|
+
)
|
|
602
|
+
return TemplateLiteralExpr(parts)
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
if (this.check(TokenType.IDENTIFIER)) {
|
|
606
|
+
return Identifier(this.advance().value)
|
|
607
|
+
}
|
|
608
|
+
if (this.match(TokenType.LPAREN)) {
|
|
609
|
+
const expr = this.parseExpression()
|
|
610
|
+
this.expect(
|
|
611
|
+
TokenType.RPAREN,
|
|
612
|
+
"Expected ')' to close a grouped expression",
|
|
613
|
+
)
|
|
614
|
+
return expr
|
|
615
|
+
}
|
|
616
|
+
if (this.match(TokenType.LBRACE)) {
|
|
617
|
+
return this.parseObjectLiteral()
|
|
618
|
+
}
|
|
619
|
+
if (this.match(TokenType.LBRACKET)) {
|
|
620
|
+
return this.parseArrayLiteral()
|
|
621
|
+
}
|
|
622
|
+
throw new Error(
|
|
623
|
+
`Unexpected token ${this.peek().type} at line ${this.peek().line}`,
|
|
624
|
+
)
|
|
625
|
+
}
|
|
626
|
+
parseArrayLiteral() {
|
|
627
|
+
const elements = []
|
|
628
|
+
while (!this.check(TokenType.RBRACKET)) {
|
|
629
|
+
elements.push(this.parseExpression())
|
|
630
|
+
this.match(TokenType.COMMA)
|
|
631
|
+
}
|
|
632
|
+
this.expect(TokenType.RBRACKET, "Expected ']' to close array literal")
|
|
633
|
+
return ArrayExpr(elements)
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
parseObjectLiteral() {
|
|
637
|
+
const properties = []
|
|
638
|
+
while (!this.check(TokenType.RBRACE)) {
|
|
639
|
+
const key = this.check(TokenType.STRING)
|
|
640
|
+
? this.advance().value
|
|
641
|
+
: this.expect(TokenType.IDENTIFIER, 'Expected an object key').value
|
|
642
|
+
this.expect(TokenType.COLON, "Expected ':' after object key")
|
|
643
|
+
const value = this.parseExpression()
|
|
644
|
+
properties.push({ key, value })
|
|
645
|
+
this.match(TokenType.COMMA)
|
|
646
|
+
}
|
|
647
|
+
this.expect(TokenType.RBRACE, "Expected '}' to close object literal")
|
|
648
|
+
return ObjectExpr(properties)
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
peek() {
|
|
652
|
+
return this.tokens[this.pos]
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
advance() {
|
|
656
|
+
return this.tokens[this.pos++]
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
check(type) {
|
|
660
|
+
return this.peek().type === type
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
match(type) {
|
|
664
|
+
if (this.check(type)) {
|
|
665
|
+
this.pos++
|
|
666
|
+
return true
|
|
667
|
+
}
|
|
668
|
+
return false
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
expect(type, message) {
|
|
672
|
+
if (this.check(type)) return this.advance()
|
|
673
|
+
throw new Error(
|
|
674
|
+
`${message} at line ${this.peek().line}, got ${this.peek().type}`,
|
|
675
|
+
)
|
|
676
|
+
}
|
|
677
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { Lexer } from "../lexer/Lexer.js";
|
|
4
|
+
import { Parser } from "./Parser.js";
|
|
5
|
+
|
|
6
|
+
test("parses a bare onMount body", () => {
|
|
7
|
+
const source = `state\ncount: 0\nonMount\nthis.count = 5`;
|
|
8
|
+
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
9
|
+
assert.ok(page.onMount);
|
|
10
|
+
assert.equal(page.onMount.body.length, 1);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("parses an async onMount body with await", () => {
|
|
14
|
+
const source = `state\nposts: 0\nonMount\nasync {\n const data = await this.load()\n this.posts = data\n}`;
|
|
15
|
+
const page = new Parser(new Lexer(source).tokenize()).parse();
|
|
16
|
+
assert.equal(page.onMount.isAsync, true);
|
|
17
|
+
assert.equal(page.onMount.body.length, 2);
|
|
18
|
+
});
|