@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.
Files changed (49) hide show
  1. package/package.json +12 -0
  2. package/src/ast/nodes.js +143 -0
  3. package/src/codegen/generateExpression.array.test.js +13 -0
  4. package/src/codegen/generateExpression.callexpr.test.js +19 -0
  5. package/src/codegen/generateExpression.js +92 -0
  6. package/src/codegen/generateExpression.test.js +59 -0
  7. package/src/codegen/generateMethod.asyncawait.test.js +37 -0
  8. package/src/codegen/generateMethod.ifstatement.test.js +20 -0
  9. package/src/codegen/generateMethod.js +13 -0
  10. package/src/codegen/generateMethod.test.js +79 -0
  11. package/src/codegen/generatePage.js +79 -0
  12. package/src/codegen/generatePage.onmount.test.js +33 -0
  13. package/src/codegen/generatePage.test.js +138 -0
  14. package/src/codegen/generateStatement.js +64 -0
  15. package/src/codegen/generateStatement.test.js +31 -0
  16. package/src/codegen/generateTemplate.ifeach.test.js +138 -0
  17. package/src/codegen/generateTemplate.js +264 -0
  18. package/src/codegen/generateTemplate.test.js +149 -0
  19. package/src/codegen/generateTemplateExpression.js +55 -0
  20. package/src/codegen/generateTemplateExpression.test.js +34 -0
  21. package/src/compileComponent.js +54 -0
  22. package/src/compileComponent.test.js +121 -0
  23. package/src/index.js +15 -0
  24. package/src/lexer/Lexer.js +336 -0
  25. package/src/lexer/Lexer.test.js +151 -0
  26. package/src/lexer/tokenTypes.js +114 -0
  27. package/src/parser/Parser.array.test.js +37 -0
  28. package/src/parser/Parser.asyncawait.test.js +33 -0
  29. package/src/parser/Parser.ifstatement.test.js +20 -0
  30. package/src/parser/Parser.js +677 -0
  31. package/src/parser/Parser.onmount.test.js +18 -0
  32. package/src/parser/Parser.test.js +227 -0
  33. package/src/parser/parseComponent.js +29 -0
  34. package/src/parser/parseComponent.test.js +53 -0
  35. package/src/parser/parseComponentFile.js +55 -0
  36. package/src/parser/parseExpressionString.js +13 -0
  37. package/src/parser/parsePageFile.js +94 -0
  38. package/src/parser/parsePageFile.test.js +101 -0
  39. package/src/parser/parseTemplate.ifeach.test.js +33 -0
  40. package/src/parser/parseTemplate.js +319 -0
  41. package/src/parser/parseTemplate.pre.test.js +39 -0
  42. package/src/parser/parseTemplate.test.js +96 -0
  43. package/src/renderPageDocument.js +250 -0
  44. package/src/renderPageDocument.test.js +125 -0
  45. package/src/runtime/loadingIndicator.js +35 -0
  46. package/src/runtime/reactive.js +59 -0
  47. package/src/runtime/reactive.test.js +106 -0
  48. package/src/runtime/useApi.js +45 -0
  49. package/src/runtime/useChannel.js +38 -0
@@ -0,0 +1,336 @@
1
+ import { TokenType, KEYWORDS } from './tokenTypes.js'
2
+
3
+ const SINGLE_CHAR_TOKENS = {
4
+ '(': TokenType.LPAREN,
5
+ ')': TokenType.RPAREN,
6
+ ':': TokenType.COLON,
7
+ ',': TokenType.COMMA,
8
+ ';': TokenType.SEMICOLON,
9
+ '.': TokenType.DOT,
10
+ '[': TokenType.LBRACKET,
11
+ ']': TokenType.RBRACKET,
12
+ }
13
+
14
+ export class Lexer {
15
+ constructor(source) {
16
+ this.source = source
17
+ this.pos = 0
18
+ this.line = 1
19
+ this.tokens = []
20
+ }
21
+
22
+ tokenize() {
23
+ while (this.pos < this.source.length) {
24
+ this.skipWhitespaceAndComments()
25
+ if (this.pos >= this.source.length) break
26
+
27
+ const char = this.source[this.pos]
28
+
29
+ if (char === '{' && this.source[this.pos + 1] === '{') {
30
+ this.pushToken(TokenType.INTERP_START, '{{')
31
+ this.pos += 2
32
+ continue
33
+ }
34
+
35
+ if (char === '}' && this.source[this.pos + 1] === '}') {
36
+ this.pushToken(TokenType.INTERP_END, '}}')
37
+ this.pos += 2
38
+ continue
39
+ }
40
+
41
+ if (char === '{') {
42
+ this.pushToken(TokenType.LBRACE, '{')
43
+ this.pos++
44
+ continue
45
+ }
46
+
47
+ if (char === '}') {
48
+ this.pushToken(TokenType.RBRACE, '}')
49
+ this.pos++
50
+ continue
51
+ }
52
+
53
+ if (char === '=' && this.source[this.pos + 1] === '>') {
54
+ this.pushToken(TokenType.ARROW, '=>')
55
+ this.pos += 2
56
+ continue
57
+ }
58
+
59
+ if (char === '>' && this.source[this.pos + 1] === '=') {
60
+ this.pushToken(TokenType.GTE, '>=')
61
+ this.pos += 2
62
+ continue
63
+ }
64
+
65
+ if (char === '<' && this.source[this.pos + 1] === '=') {
66
+ this.pushToken(TokenType.LTE, '<=')
67
+ this.pos += 2
68
+ continue
69
+ }
70
+
71
+ // Compound assignment operators must be checked before their
72
+ // single-char fallbacks below, same lookahead-before-fallback
73
+ // pattern already used for '=>', '>=', '<=' above.
74
+ if (char === '+' && this.source[this.pos + 1] === '=') {
75
+ this.pushToken(TokenType.PLUS_EQUALS, '+=')
76
+ this.pos += 2
77
+ continue
78
+ }
79
+
80
+ if (char === '-' && this.source[this.pos + 1] === '=') {
81
+ this.pushToken(TokenType.MINUS_EQUALS, '-=')
82
+ this.pos += 2
83
+ continue
84
+ }
85
+
86
+ if (char === '*' && this.source[this.pos + 1] === '=') {
87
+ this.pushToken(TokenType.STAR_EQUALS, '*=')
88
+ this.pos += 2
89
+ continue
90
+ }
91
+
92
+ if (char === '/' && this.source[this.pos + 1] === '=') {
93
+ this.pushToken(TokenType.SLASH_EQUALS, '/=')
94
+ this.pos += 2
95
+ continue
96
+ }
97
+
98
+ if (char === '+') {
99
+ this.pushToken(TokenType.PLUS, '+')
100
+ this.pos++
101
+ continue
102
+ }
103
+
104
+ if (char === '-') {
105
+ this.pushToken(TokenType.MINUS, '-')
106
+ this.pos++
107
+ continue
108
+ }
109
+
110
+ if (char === '*') {
111
+ this.pushToken(TokenType.STAR, '*')
112
+ this.pos++
113
+ continue
114
+ }
115
+
116
+ if (char === '/') {
117
+ this.pushToken(TokenType.SLASH, '/')
118
+ this.pos++
119
+ continue
120
+ }
121
+
122
+ if (char === '%') {
123
+ this.pushToken(TokenType.PERCENT, '%')
124
+ this.pos++
125
+ continue
126
+ }
127
+
128
+ if (char === '?') {
129
+ this.pushToken(TokenType.QUESTION, '?')
130
+ this.pos++
131
+ continue
132
+ }
133
+
134
+ if (char === '>') {
135
+ this.pushToken(TokenType.GT, '>')
136
+ this.pos++
137
+ continue
138
+ }
139
+
140
+ if (char === '<') {
141
+ this.pushToken(TokenType.LT, '<')
142
+ this.pos++
143
+ continue
144
+ }
145
+
146
+ if (char === '=') {
147
+ this.pushToken(TokenType.EQUALS, '=')
148
+ this.pos++
149
+ continue
150
+ }
151
+
152
+ if (char in SINGLE_CHAR_TOKENS) {
153
+ this.pushToken(SINGLE_CHAR_TOKENS[char], char)
154
+ this.pos++
155
+ continue
156
+ }
157
+
158
+ if (char === '`') {
159
+ this.readTemplateLiteral()
160
+ continue
161
+ }
162
+
163
+ if (char === '"' || char === "'") {
164
+ this.readString(char)
165
+ continue
166
+ }
167
+
168
+ if (/[0-9]/.test(char)) {
169
+ this.readNumber()
170
+ continue
171
+ }
172
+
173
+ if (/[a-zA-Z_$]/.test(char)) {
174
+ this.readIdentifierOrKeyword()
175
+ continue
176
+ }
177
+
178
+ throw new Error(`Unexpected character "${char}" at line ${this.line}`)
179
+ }
180
+
181
+ this.pushToken(TokenType.EOF, null)
182
+ return this.tokens
183
+ }
184
+
185
+ skipWhitespaceAndComments() {
186
+ while (this.pos < this.source.length) {
187
+ const char = this.source[this.pos]
188
+
189
+ if (char === '\n') {
190
+ this.line++
191
+ this.pos++
192
+ continue
193
+ }
194
+
195
+ if (/\s/.test(char)) {
196
+ this.pos++
197
+ continue
198
+ }
199
+
200
+ if (char === '/' && this.source[this.pos + 1] === '/') {
201
+ while (
202
+ this.pos < this.source.length &&
203
+ this.source[this.pos] !== '\n'
204
+ ) {
205
+ this.pos++
206
+ }
207
+ continue
208
+ }
209
+
210
+ if (char === '/' && this.source[this.pos + 1] === '*') {
211
+ this.pos += 2
212
+ while (
213
+ this.pos < this.source.length &&
214
+ !(this.source[this.pos] === '*' && this.source[this.pos + 1] === '/')
215
+ ) {
216
+ if (this.source[this.pos] === '\n') this.line++
217
+ this.pos++
218
+ }
219
+ this.pos += 2
220
+ continue
221
+ }
222
+
223
+ break
224
+ }
225
+ }
226
+
227
+ readString(quoteChar) {
228
+ const startLine = this.line
229
+ this.pos++ // skip opening quote
230
+ let value = ''
231
+ while (
232
+ this.pos < this.source.length &&
233
+ this.source[this.pos] !== quoteChar
234
+ ) {
235
+ value += this.source[this.pos]
236
+ this.pos++
237
+ }
238
+ if (this.pos >= this.source.length) {
239
+ throw new Error(`Unterminated string starting at line ${startLine}`)
240
+ }
241
+ this.pos++ // skip closing quote
242
+ this.pushToken(TokenType.STRING, value, startLine)
243
+ }
244
+
245
+ // Scans a backtick template literal into a flat list of parts --
246
+ // { type: "text", value } for literal text, { type: "expr", source }
247
+ // for each ${...} span -- rather than re-entering full tokenization
248
+ // mid-string. Each expr span's raw source gets parsed later via
249
+ // parseExpressionString, reusing the same mechanism already used for
250
+ // {{ }} interpolations and #if/#each headers, instead of teaching
251
+ // the Lexer to interleave live token scanning inside a string.
252
+ readTemplateLiteral() {
253
+ const startLine = this.line
254
+ this.pos++ // skip opening backtick
255
+ const parts = []
256
+ let textBuf = ''
257
+
258
+ const flushText = () => {
259
+ if (textBuf.length > 0) {
260
+ parts.push({ type: 'text', value: textBuf })
261
+ textBuf = ''
262
+ }
263
+ }
264
+
265
+ while (true) {
266
+ if (this.pos >= this.source.length) {
267
+ throw new Error(
268
+ `Unterminated template literal starting at line ${startLine}`,
269
+ )
270
+ }
271
+ const char = this.source[this.pos]
272
+
273
+ if (char === '`') {
274
+ this.pos++ // skip closing backtick
275
+ break
276
+ }
277
+
278
+ if (char === '$' && this.source[this.pos + 1] === '{') {
279
+ flushText()
280
+ this.pos += 2 // skip "${"
281
+ const exprStart = this.pos
282
+ let depth = 1
283
+ while (this.pos < this.source.length && depth > 0) {
284
+ if (this.source[this.pos] === '{') depth++
285
+ else if (this.source[this.pos] === '}') depth--
286
+ if (depth > 0) this.pos++
287
+ }
288
+ if (depth !== 0) {
289
+ throw new Error(
290
+ `Unterminated \${...} in template literal starting at line ${startLine}`,
291
+ )
292
+ }
293
+ const exprSource = this.source.slice(exprStart, this.pos)
294
+ this.pos++ // skip closing "}"
295
+ parts.push({ type: 'expr', source: exprSource })
296
+ continue
297
+ }
298
+
299
+ if (char === '\n') this.line++
300
+ textBuf += char
301
+ this.pos++
302
+ }
303
+
304
+ flushText()
305
+ this.pushToken(TokenType.TEMPLATE_LITERAL, parts, startLine)
306
+ }
307
+
308
+ readNumber() {
309
+ const start = this.pos
310
+ while (
311
+ this.pos < this.source.length &&
312
+ /[0-9.]/.test(this.source[this.pos])
313
+ ) {
314
+ this.pos++
315
+ }
316
+ const text = this.source.slice(start, this.pos)
317
+ this.pushToken(TokenType.NUMBER, Number(text))
318
+ }
319
+
320
+ readIdentifierOrKeyword() {
321
+ const start = this.pos
322
+ while (
323
+ this.pos < this.source.length &&
324
+ /[a-zA-Z0-9_$]/.test(this.source[this.pos])
325
+ ) {
326
+ this.pos++
327
+ }
328
+ const text = this.source.slice(start, this.pos)
329
+ const type = KEYWORDS[text] ?? TokenType.IDENTIFIER
330
+ this.pushToken(type, text)
331
+ }
332
+
333
+ pushToken(type, value, line = this.line) {
334
+ this.tokens.push({ type, value, line })
335
+ }
336
+ }
@@ -0,0 +1,151 @@
1
+ import { test } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { Lexer } from "./Lexer.js";
4
+ import { TokenType } from "./tokenTypes.js";
5
+
6
+ function tokenTypes(source) {
7
+ return new Lexer(source).tokenize().map((t) => t.type);
8
+ }
9
+
10
+ test("tokenizes keywords", () => {
11
+ const types = tokenTypes("page state action computed watch template props");
12
+ assert.deepEqual(types, [
13
+ TokenType.PAGE,
14
+ TokenType.STATE,
15
+ TokenType.ACTION,
16
+ TokenType.COMPUTED,
17
+ TokenType.WATCH,
18
+ TokenType.TEMPLATE,
19
+ TokenType.PROPS,
20
+ TokenType.EOF,
21
+ ]);
22
+ });
23
+
24
+ test("tokenizes an identifier that is not a keyword", () => {
25
+ const tokens = new Lexer("count").tokenize();
26
+ assert.equal(tokens[0].type, TokenType.IDENTIFIER);
27
+ assert.equal(tokens[0].value, "count");
28
+ });
29
+
30
+ test("tokenizes a number literal", () => {
31
+ const tokens = new Lexer("42").tokenize();
32
+ assert.equal(tokens[0].type, TokenType.NUMBER);
33
+ assert.equal(tokens[0].value, 42);
34
+ });
35
+
36
+ test("tokenizes a decimal number literal", () => {
37
+ const tokens = new Lexer("3.14").tokenize();
38
+ assert.equal(tokens[0].value, 3.14);
39
+ });
40
+
41
+ test("tokenizes single and double quoted strings", () => {
42
+ const tokens = new Lexer(`"hello" 'world'`).tokenize();
43
+ assert.equal(tokens[0].type, TokenType.STRING);
44
+ assert.equal(tokens[0].value, "hello");
45
+ assert.equal(tokens[1].type, TokenType.STRING);
46
+ assert.equal(tokens[1].value, "world");
47
+ });
48
+
49
+ test("tokenizes braces, parens, and punctuation", () => {
50
+ const types = tokenTypes("{ } ( ) : , ; . =");
51
+ assert.deepEqual(types, [
52
+ TokenType.LBRACE,
53
+ TokenType.RBRACE,
54
+ TokenType.LPAREN,
55
+ TokenType.RPAREN,
56
+ TokenType.COLON,
57
+ TokenType.COMMA,
58
+ TokenType.SEMICOLON,
59
+ TokenType.DOT,
60
+ TokenType.EQUALS,
61
+ TokenType.EOF,
62
+ ]);
63
+ });
64
+
65
+ test("tokenizes the arrow operator distinctly from equals", () => {
66
+ const types = tokenTypes("=>");
67
+ assert.deepEqual(types, [TokenType.ARROW, TokenType.EOF]);
68
+ });
69
+
70
+ test("tokenizes interpolation markers distinctly from braces", () => {
71
+ const types = tokenTypes("{{ count }}");
72
+ assert.deepEqual(types, [
73
+ TokenType.INTERP_START,
74
+ TokenType.IDENTIFIER,
75
+ TokenType.INTERP_END,
76
+ TokenType.EOF,
77
+ ]);
78
+ });
79
+
80
+ test("skips single-line comments", () => {
81
+ const types = tokenTypes("count // this is a comment\naction");
82
+ assert.deepEqual(types, [TokenType.IDENTIFIER, TokenType.ACTION, TokenType.EOF]);
83
+ });
84
+
85
+ test("skips multi-line comments", () => {
86
+ const types = tokenTypes("count /* skip\nthis */ action");
87
+ assert.deepEqual(types, [TokenType.IDENTIFIER, TokenType.ACTION, TokenType.EOF]);
88
+ });
89
+
90
+ test("tokenizes a realistic state block", () => {
91
+ const source = `state { count: 0 }`;
92
+ const types = tokenTypes(source);
93
+ assert.deepEqual(types, [
94
+ TokenType.STATE,
95
+ TokenType.LBRACE,
96
+ TokenType.IDENTIFIER,
97
+ TokenType.COLON,
98
+ TokenType.NUMBER,
99
+ TokenType.RBRACE,
100
+ TokenType.EOF,
101
+ ]);
102
+ });
103
+
104
+ test("tokenizes a realistic action block with a method call body", () => {
105
+ const source = `action { increment() { count = count + 1; } }`;
106
+ const types = tokenTypes(source);
107
+ assert.deepEqual(types, [
108
+ TokenType.ACTION,
109
+ TokenType.LBRACE,
110
+ TokenType.IDENTIFIER,
111
+ TokenType.LPAREN,
112
+ TokenType.RPAREN,
113
+ TokenType.LBRACE,
114
+ TokenType.IDENTIFIER,
115
+ TokenType.EQUALS,
116
+ TokenType.IDENTIFIER,
117
+ TokenType.PLUS,
118
+ TokenType.NUMBER,
119
+ TokenType.SEMICOLON,
120
+ TokenType.RBRACE,
121
+ TokenType.RBRACE,
122
+ TokenType.EOF,
123
+ ]);
124
+ });
125
+
126
+ test("tokenizes arithmetic and comparison operators", () => {
127
+ const types = tokenTypes("+ - * / > <");
128
+ assert.deepEqual(types, [
129
+ TokenType.PLUS,
130
+ TokenType.MINUS,
131
+ TokenType.STAR,
132
+ TokenType.SLASH,
133
+ TokenType.GT,
134
+ TokenType.LT,
135
+ TokenType.EOF,
136
+ ]);
137
+ });
138
+
139
+ test("throws a clear error on an unexpected character", () => {
140
+ assert.throws(() => new Lexer("state # invalid").tokenize(), /Unexpected character "#"/);
141
+ });
142
+
143
+ test("throws a clear error on an unterminated string", () => {
144
+ assert.throws(() => new Lexer(`"never closed`).tokenize(), /Unterminated string/);
145
+ });
146
+
147
+ test("tracks line numbers across newlines", () => {
148
+ const tokens = new Lexer("count\naction").tokenize();
149
+ assert.equal(tokens[0].line, 1);
150
+ assert.equal(tokens[1].line, 2);
151
+ });
@@ -0,0 +1,114 @@
1
+ export const TokenType = {
2
+ // Keywords
3
+ PAGE: 'PAGE',
4
+ LAYOUT: 'LAYOUT',
5
+ STATE: 'STATE',
6
+ COMPUTED: 'COMPUTED',
7
+ WATCH: 'WATCH',
8
+ ACTION: 'ACTION',
9
+ TEMPLATE: 'TEMPLATE',
10
+ PROPS: 'PROPS',
11
+ ONMOUNT: 'ONMOUNT',
12
+ NEW: 'NEW',
13
+
14
+ // Literals
15
+ IDENTIFIER: 'IDENTIFIER',
16
+ NUMBER: 'NUMBER',
17
+ STRING: 'STRING',
18
+
19
+ // Punctuation
20
+ LBRACE: 'LBRACE', // {
21
+ RBRACE: 'RBRACE', // }
22
+ LPAREN: 'LPAREN', // (
23
+ RPAREN: 'RPAREN', // )
24
+ COLON: 'COLON', // :
25
+ COMMA: 'COMMA', // ,
26
+ EQUALS: 'EQUALS', // =
27
+ SEMICOLON: 'SEMICOLON', // ;
28
+ DOT: 'DOT', // .
29
+ LBRACKET: 'LBRACKET', // [
30
+ RBRACKET: 'RBRACKET', // ]
31
+ ARROW: 'ARROW', // =>
32
+ QUESTION: 'QUESTION', // ?
33
+ RETURN: 'RETURN',
34
+ ASYNC: 'ASYNC',
35
+ AWAIT: 'AWAIT',
36
+ CONST: 'CONST',
37
+ IF: 'IF',
38
+ ELSE: 'ELSE',
39
+ LET: 'LET',
40
+ PLUS: 'PLUS',
41
+ MINUS: 'MINUS',
42
+ STAR: 'STAR',
43
+ SLASH: 'SLASH',
44
+ PERCENT: 'PERCENT', // %
45
+ GT: 'GT',
46
+ LT: 'LT',
47
+ GTE: 'GTE', // >=
48
+ LTE: 'LTE', // <=
49
+
50
+ // Word operators (Tylix-native, not symbol-based)
51
+ AND: 'AND', // and
52
+ OR: 'OR', // or
53
+ NOT: 'NOT', // not
54
+ IS: 'IS', // is / is not
55
+ HAS: 'HAS', // has (membership)
56
+ EXISTS: 'EXISTS', // exists (postfix, != null)
57
+ MISSING: 'MISSING', // missing (postfix, == null)
58
+
59
+ // Loops
60
+ FOR: 'FOR',
61
+ IN: 'IN',
62
+ FROM: 'FROM',
63
+ TO: 'TO',
64
+ REPEAT: 'REPEAT',
65
+ BREAK: 'BREAK',
66
+ CONTINUE: 'CONTINUE',
67
+
68
+ PLUS_EQUALS: 'PLUS_EQUALS', // +=
69
+ MINUS_EQUALS: 'MINUS_EQUALS', // -=
70
+ STAR_EQUALS: 'STAR_EQUALS', // *=
71
+ SLASH_EQUALS: 'SLASH_EQUALS', // /=
72
+ TEMPLATE_LITERAL: 'TEMPLATE_LITERAL', // `text ${expr} text`
73
+
74
+ // Template-specific
75
+ TEMPLATE_TEXT: 'TEMPLATE_TEXT', // raw HTML-like text between tags
76
+ INTERP_START: 'INTERP_START', // {{
77
+ INTERP_END: 'INTERP_END', // }}
78
+
79
+ EOF: 'EOF',
80
+ }
81
+
82
+ export const KEYWORDS = {
83
+ page: TokenType.PAGE,
84
+ layout: TokenType.LAYOUT,
85
+ state: TokenType.STATE,
86
+ computed: TokenType.COMPUTED,
87
+ watch: TokenType.WATCH,
88
+ action: TokenType.ACTION,
89
+ template: TokenType.TEMPLATE,
90
+ props: TokenType.PROPS,
91
+ onMount: TokenType.ONMOUNT,
92
+ new: TokenType.NEW,
93
+ return: TokenType.RETURN,
94
+ async: TokenType.ASYNC,
95
+ await: TokenType.AWAIT,
96
+ const: TokenType.CONST,
97
+ if: TokenType.IF,
98
+ else: TokenType.ELSE,
99
+ let: TokenType.LET,
100
+ and: TokenType.AND,
101
+ or: TokenType.OR,
102
+ not: TokenType.NOT,
103
+ is: TokenType.IS,
104
+ has: TokenType.HAS,
105
+ exists: TokenType.EXISTS,
106
+ missing: TokenType.MISSING,
107
+ for: TokenType.FOR,
108
+ in: TokenType.IN,
109
+ from: TokenType.FROM,
110
+ to: TokenType.TO,
111
+ repeat: TokenType.REPEAT,
112
+ break: TokenType.BREAK,
113
+ continue: TokenType.CONTINUE,
114
+ }
@@ -0,0 +1,37 @@
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
+ function parse(source) {
7
+ return new Parser(new Lexer(source).tokenize()).parse();
8
+ }
9
+
10
+ test("parses an empty array literal as a state initial value", () => {
11
+ const page = parse(`state\nproducts: []`);
12
+ assert.equal(page.state[0].value.type, "ArrayExpression");
13
+ assert.deepEqual(page.state[0].value.elements, []);
14
+ });
15
+
16
+ test("parses an array literal with elements", () => {
17
+ const page = parse(`state\nids: [1, 2, 3]`);
18
+ const elements = page.state[0].value.elements;
19
+ assert.equal(elements.length, 3);
20
+ assert.deepEqual(elements.map((e) => e.value), [1, 2, 3]);
21
+ });
22
+
23
+ test("parses an array literal assigned inside an action body", () => {
24
+ const page = parse(`action\nclear() {\n this.items = []\n}`);
25
+ const assign = page.actions[0].body[0].expression;
26
+ assert.equal(assign.type, "AssignmentExpression");
27
+ assert.equal(assign.value.type, "ArrayExpression");
28
+ });
29
+
30
+ test("parses a bare state block with inconsistent trailing commas between entries", () => {
31
+ const source = `state
32
+ products: []
33
+ title: "",
34
+ price: ""`;
35
+ const page = parse(source);
36
+ assert.deepEqual(page.state.map((s) => s.name), ["products", "title", "price"]);
37
+ });
@@ -0,0 +1,33 @@
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 an async method with await and const declarations", () => {
7
+ const source = `
8
+ action
9
+ async load() {
10
+ const response = await fetch("/api/products")
11
+ const data = await response.json()
12
+ this.products = data
13
+ }`;
14
+ const page = new Parser(new Lexer(source).tokenize()).parse();
15
+ const method = page.actions[0];
16
+ assert.equal(method.isAsync, true);
17
+ assert.equal(method.body[0].type, "VariableDeclaration");
18
+ assert.equal(method.body[0].kind, "const");
19
+ assert.equal(method.body[0].init.type, "AwaitExpression");
20
+ });
21
+
22
+ test("parses an object literal expression", () => {
23
+ const source = `
24
+ action
25
+ async save() {
26
+ await fetch("/api/products", { method: "POST", body: this.name })
27
+ }`;
28
+ const page = new Parser(new Lexer(source).tokenize()).parse();
29
+ const call = page.actions[0].body[0].expression.argument;
30
+ assert.equal(call.type, "CallExpression");
31
+ assert.equal(call.args[1].type, "ObjectExpression");
32
+ assert.equal(call.args[1].properties[0].key, "method");
33
+ });
@@ -0,0 +1,20 @@
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 an if statement with no else", () => {
7
+ const source = `action\ncheck() {\n if (this.ok) {\n this.result = 1\n }\n}`;
8
+ const page = new Parser(new Lexer(source).tokenize()).parse();
9
+ const stmt = page.actions[0].body[0];
10
+ assert.equal(stmt.type, "IfStatement");
11
+ assert.equal(stmt.alternate, null);
12
+ });
13
+
14
+ test("parses an if/else statement", () => {
15
+ const source = `action\ncheck() {\n if (this.ok) {\n this.result = 1\n } else {\n this.result = 0\n }\n}`;
16
+ const page = new Parser(new Lexer(source).tokenize()).parse();
17
+ const stmt = page.actions[0].body[0];
18
+ assert.ok(stmt.alternate);
19
+ assert.equal(stmt.alternate.length, 1);
20
+ });