@tylix/compiler 0.4.2 → 0.5.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 +2 -2
- package/src/ast/nodes.js +31 -2
- package/src/codegen/emitStatementLines.js +108 -22
- package/src/codegen/generateExpression.js +42 -17
- package/src/codegen/generateStatement.js +54 -57
- package/src/codegen/generateTemplateExpression.js +110 -16
- package/src/lexer/Lexer.js +69 -5
- package/src/lexer/tokenTypes.js +19 -2
- package/src/parser/Parser.js +265 -34
- package/src/renderPageDocument.js +16 -2
- package/src/typecheck/typedTemplateExpr.js +70 -6
|
@@ -1,5 +1,52 @@
|
|
|
1
1
|
import { generateExpression } from './generateExpression.js'
|
|
2
2
|
|
|
3
|
+
// Plain JS globals that templates may use directly. Everything else
|
|
4
|
+
// that isn't a local (each variable, arrow param, $event) is treated
|
|
5
|
+
// as page state/capability and prefixed with "instance.".
|
|
6
|
+
const JS_GLOBALS = new Set([
|
|
7
|
+
'null',
|
|
8
|
+
'true',
|
|
9
|
+
'false',
|
|
10
|
+
'undefined',
|
|
11
|
+
'NaN',
|
|
12
|
+
'Infinity',
|
|
13
|
+
'Math',
|
|
14
|
+
'JSON',
|
|
15
|
+
'Date',
|
|
16
|
+
'Number',
|
|
17
|
+
'String',
|
|
18
|
+
'Boolean',
|
|
19
|
+
'Array',
|
|
20
|
+
'Object',
|
|
21
|
+
'Set',
|
|
22
|
+
'Map',
|
|
23
|
+
'Intl',
|
|
24
|
+
'Promise',
|
|
25
|
+
'RegExp',
|
|
26
|
+
'Symbol',
|
|
27
|
+
'Error',
|
|
28
|
+
'URL',
|
|
29
|
+
'parseInt',
|
|
30
|
+
'parseFloat',
|
|
31
|
+
'isNaN',
|
|
32
|
+
'isFinite',
|
|
33
|
+
'encodeURIComponent',
|
|
34
|
+
'decodeURIComponent',
|
|
35
|
+
'console',
|
|
36
|
+
'window',
|
|
37
|
+
])
|
|
38
|
+
|
|
39
|
+
function escapeTemplateLiteralText(text) {
|
|
40
|
+
return text
|
|
41
|
+
.replace(/\\/g, '\\\\')
|
|
42
|
+
.replace(/`/g, '\\`')
|
|
43
|
+
.replace(/\$\{/g, '\\${')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function unaryPrefix(operator) {
|
|
47
|
+
return /^[a-z]/i.test(operator) ? `${operator} ` : operator
|
|
48
|
+
}
|
|
49
|
+
|
|
3
50
|
export function generateTemplateExpression(node, scope = new Set()) {
|
|
4
51
|
if (node.type === 'Identifier') {
|
|
5
52
|
if (node.name === '$event' || scope.has(node.name)) {
|
|
@@ -8,19 +55,7 @@ export function generateTemplateExpression(node, scope = new Set()) {
|
|
|
8
55
|
if (node.name === 'this') {
|
|
9
56
|
return 'instance'
|
|
10
57
|
}
|
|
11
|
-
|
|
12
|
-
// instance properties -- if the parser is tokenizing them as bare
|
|
13
|
-
// Identifier nodes (rather than Literal nodes), they must NOT be
|
|
14
|
-
// prefixed with "instance.", or comparisons like
|
|
15
|
-
// `serverId is not null` silently compile to
|
|
16
|
-
// `instance.serverId !== instance.null` (always undefined) instead
|
|
17
|
-
// of `instance.serverId !== null`.
|
|
18
|
-
if (
|
|
19
|
-
node.name === 'null' ||
|
|
20
|
-
node.name === 'true' ||
|
|
21
|
-
node.name === 'false' ||
|
|
22
|
-
node.name === 'undefined'
|
|
23
|
-
) {
|
|
58
|
+
if (JS_GLOBALS.has(node.name)) {
|
|
24
59
|
return node.name
|
|
25
60
|
}
|
|
26
61
|
return `instance.${node.name}`
|
|
@@ -46,27 +81,86 @@ export function generateTemplateExpression(node, scope = new Set()) {
|
|
|
46
81
|
}
|
|
47
82
|
|
|
48
83
|
if (node.type === 'UnaryExpression') {
|
|
49
|
-
return `(${node.operator}${generateTemplateExpression(node.argument, scope)})`
|
|
84
|
+
return `(${unaryPrefix(node.operator)}${generateTemplateExpression(node.argument, scope)})`
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Only valid inside an `async` arrow (`{{ async () => await ... }}`);
|
|
88
|
+
// a bare `{{ await x }}` would land in a non-async render effect and
|
|
89
|
+
// fail when the generated code is loaded.
|
|
90
|
+
if (node.type === 'AwaitExpression') {
|
|
91
|
+
return `(await ${generateTemplateExpression(node.argument, scope)})`
|
|
50
92
|
}
|
|
93
|
+
|
|
51
94
|
if (node.type === 'AssignmentExpression') {
|
|
52
95
|
return `${generateTemplateExpression(node.target, scope)} = ${generateTemplateExpression(node.value, scope)}`
|
|
53
96
|
}
|
|
97
|
+
|
|
54
98
|
if (node.type === 'CallExpression') {
|
|
55
99
|
const args = node.args
|
|
56
100
|
.map((a) => generateTemplateExpression(a, scope))
|
|
57
101
|
.join(', ')
|
|
58
|
-
return `${generateTemplateExpression(node.callee, scope)}(${args})`
|
|
102
|
+
return `${generateTemplateExpression(node.callee, scope)}${node.optional ? '?.' : ''}(${args})`
|
|
59
103
|
}
|
|
104
|
+
|
|
60
105
|
if (node.type === 'NewExpression') {
|
|
61
106
|
const args = node.args
|
|
62
107
|
.map((a) => generateTemplateExpression(a, scope))
|
|
63
108
|
.join(', ')
|
|
64
109
|
return `new ${generateTemplateExpression(node.callee, scope)}(${args})`
|
|
65
110
|
}
|
|
111
|
+
|
|
112
|
+
// (a, b) => expr -- params become locals for the body, so they are
|
|
113
|
+
// referenced bare instead of being prefixed with "instance.". The
|
|
114
|
+
// whole function is parenthesized so it is safe both as a call
|
|
115
|
+
// argument and as an event handler that gets invoked immediately.
|
|
116
|
+
if (node.type === 'ArrowFunctionExpression') {
|
|
117
|
+
const innerScope = new Set([...scope, ...node.params])
|
|
118
|
+
const params = node.params.join(', ')
|
|
119
|
+
const body = generateTemplateExpression(node.body, innerScope)
|
|
120
|
+
const asyncPrefix = node.isAsync ? 'async ' : ''
|
|
121
|
+
return `(${asyncPrefix}(${params}) => ${body})`
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (node.type === 'ArrayExpression') {
|
|
125
|
+
const elements = node.elements
|
|
126
|
+
.map((e) => generateTemplateExpression(e, scope))
|
|
127
|
+
.join(', ')
|
|
128
|
+
return `[${elements}]`
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Parenthesized so an object literal is never mistaken for a block
|
|
132
|
+
// when it ends up as an arrow function body: x => ({ ... })
|
|
133
|
+
if (node.type === 'ObjectExpression') {
|
|
134
|
+
const properties = node.properties
|
|
135
|
+
.map((p) =>
|
|
136
|
+
p.spread
|
|
137
|
+
? `...${generateTemplateExpression(p.value, scope)}`
|
|
138
|
+
: `${JSON.stringify(p.key)}: ${generateTemplateExpression(p.value, scope)}`,
|
|
139
|
+
)
|
|
140
|
+
.join(', ')
|
|
141
|
+
return `({ ${properties} })`
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (node.type === 'SpreadElement') {
|
|
145
|
+
return `...${generateTemplateExpression(node.argument, scope)}`
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (node.type === 'TemplateLiteralExpression') {
|
|
149
|
+
const body = node.parts
|
|
150
|
+
.map((part) =>
|
|
151
|
+
part.type === 'text'
|
|
152
|
+
? escapeTemplateLiteralText(part.value)
|
|
153
|
+
: `\${${generateTemplateExpression(part.expression, scope)}}`,
|
|
154
|
+
)
|
|
155
|
+
.join('')
|
|
156
|
+
return `\`${body}\``
|
|
157
|
+
}
|
|
158
|
+
|
|
66
159
|
if (node.type === 'Literal') {
|
|
67
160
|
return generateExpression(node)
|
|
68
161
|
}
|
|
162
|
+
|
|
69
163
|
throw new Error(
|
|
70
164
|
`generateTemplateExpression: unsupported node type "${node.type}" in template expression`,
|
|
71
165
|
)
|
|
72
|
-
}
|
|
166
|
+
}
|
package/src/lexer/Lexer.js
CHANGED
|
@@ -9,7 +9,6 @@ const SINGLE_CHAR_TOKENS = {
|
|
|
9
9
|
'.': TokenType.DOT,
|
|
10
10
|
'[': TokenType.LBRACKET,
|
|
11
11
|
']': TokenType.RBRACKET,
|
|
12
|
-
'.': TokenType.DOT,
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
export class Lexer {
|
|
@@ -75,9 +74,27 @@ export class Lexer {
|
|
|
75
74
|
continue
|
|
76
75
|
}
|
|
77
76
|
|
|
78
|
-
//
|
|
79
|
-
//
|
|
80
|
-
//
|
|
77
|
+
// Multi-char operators are checked before their single-char
|
|
78
|
+
// fallbacks, same lookahead-before-fallback pattern as '=>',
|
|
79
|
+
// '>=', '<=' above.
|
|
80
|
+
if (char === '+' && this.source[this.pos + 1] === '+') {
|
|
81
|
+
this.pushToken(TokenType.PLUS_PLUS, '++')
|
|
82
|
+
this.pos += 2
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (char === '-' && this.source[this.pos + 1] === '-') {
|
|
87
|
+
this.pushToken(TokenType.MINUS_MINUS, '--')
|
|
88
|
+
this.pos += 2
|
|
89
|
+
continue
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (char === '*' && this.source[this.pos + 1] === '*') {
|
|
93
|
+
this.pushToken(TokenType.STAR_STAR, '**')
|
|
94
|
+
this.pos += 2
|
|
95
|
+
continue
|
|
96
|
+
}
|
|
97
|
+
|
|
81
98
|
if (char === '+' && this.source[this.pos + 1] === '=') {
|
|
82
99
|
this.pushToken(TokenType.PLUS_EQUALS, '+=')
|
|
83
100
|
this.pos += 2
|
|
@@ -102,6 +119,12 @@ export class Lexer {
|
|
|
102
119
|
continue
|
|
103
120
|
}
|
|
104
121
|
|
|
122
|
+
if (char === '%' && this.source[this.pos + 1] === '=') {
|
|
123
|
+
this.pushToken(TokenType.PERCENT_EQUALS, '%=')
|
|
124
|
+
this.pos += 2
|
|
125
|
+
continue
|
|
126
|
+
}
|
|
127
|
+
|
|
105
128
|
if (char === '+') {
|
|
106
129
|
this.pushToken(TokenType.PLUS, '+')
|
|
107
130
|
this.pos++
|
|
@@ -266,6 +289,37 @@ export class Lexer {
|
|
|
266
289
|
}
|
|
267
290
|
}
|
|
268
291
|
|
|
292
|
+
// Called with this.pos on the backslash. Consumes the escape
|
|
293
|
+
// sequence and returns the character(s) it stands for. Unknown
|
|
294
|
+
// escapes (\\, \', \", \`, \$) just yield the escaped character.
|
|
295
|
+
readEscape() {
|
|
296
|
+
this.pos++ // skip '\'
|
|
297
|
+
const c = this.source[this.pos]
|
|
298
|
+
if (c === undefined) return ''
|
|
299
|
+
this.pos++
|
|
300
|
+
switch (c) {
|
|
301
|
+
case 'n':
|
|
302
|
+
return '\n'
|
|
303
|
+
case 't':
|
|
304
|
+
return '\t'
|
|
305
|
+
case 'r':
|
|
306
|
+
return '\r'
|
|
307
|
+
case 'b':
|
|
308
|
+
return '\b'
|
|
309
|
+
case 'f':
|
|
310
|
+
return '\f'
|
|
311
|
+
case 'v':
|
|
312
|
+
return '\v'
|
|
313
|
+
case '0':
|
|
314
|
+
return '\0'
|
|
315
|
+
case '\n':
|
|
316
|
+
this.line++ // line continuation
|
|
317
|
+
return ''
|
|
318
|
+
default:
|
|
319
|
+
return c
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
269
323
|
readString(quoteChar) {
|
|
270
324
|
const startLine = this.line
|
|
271
325
|
this.pos++ // skip opening quote
|
|
@@ -274,6 +328,11 @@ export class Lexer {
|
|
|
274
328
|
this.pos < this.source.length &&
|
|
275
329
|
this.source[this.pos] !== quoteChar
|
|
276
330
|
) {
|
|
331
|
+
if (this.source[this.pos] === '\\') {
|
|
332
|
+
value += this.readEscape()
|
|
333
|
+
continue
|
|
334
|
+
}
|
|
335
|
+
if (this.source[this.pos] === '\n') this.line++
|
|
277
336
|
value += this.source[this.pos]
|
|
278
337
|
this.pos++
|
|
279
338
|
}
|
|
@@ -317,6 +376,11 @@ export class Lexer {
|
|
|
317
376
|
break
|
|
318
377
|
}
|
|
319
378
|
|
|
379
|
+
if (char === '\\') {
|
|
380
|
+
textBuf += this.readEscape()
|
|
381
|
+
continue
|
|
382
|
+
}
|
|
383
|
+
|
|
320
384
|
if (char === '$' && this.source[this.pos + 1] === '{') {
|
|
321
385
|
flushText()
|
|
322
386
|
this.pos += 2 // skip "${"
|
|
@@ -387,4 +451,4 @@ export class Lexer {
|
|
|
387
451
|
pushToken(type, value, line = this.line) {
|
|
388
452
|
this.tokens.push({ type, value, line })
|
|
389
453
|
}
|
|
390
|
-
}
|
|
454
|
+
}
|
package/src/lexer/tokenTypes.js
CHANGED
|
@@ -23,6 +23,13 @@ export const TokenType = {
|
|
|
23
23
|
TRY: 'TRY',
|
|
24
24
|
CATCH: 'CATCH',
|
|
25
25
|
FINALLY: 'FINALLY',
|
|
26
|
+
TYPEOF: 'TYPEOF',
|
|
27
|
+
INSTANCEOF: 'INSTANCEOF',
|
|
28
|
+
THROW: 'THROW',
|
|
29
|
+
WHILE: 'WHILE',
|
|
30
|
+
SWITCH: 'SWITCH',
|
|
31
|
+
CASE: 'CASE',
|
|
32
|
+
DEFAULT: 'DEFAULT',
|
|
26
33
|
|
|
27
34
|
// Literals
|
|
28
35
|
IDENTIFIER: 'IDENTIFIER',
|
|
@@ -59,6 +66,9 @@ export const TokenType = {
|
|
|
59
66
|
STAR: 'STAR',
|
|
60
67
|
SLASH: 'SLASH',
|
|
61
68
|
PERCENT: 'PERCENT', // %
|
|
69
|
+
STAR_STAR: 'STAR_STAR', // **
|
|
70
|
+
PLUS_PLUS: 'PLUS_PLUS', // ++
|
|
71
|
+
MINUS_MINUS: 'MINUS_MINUS', // --
|
|
62
72
|
GT: 'GT',
|
|
63
73
|
LT: 'LT',
|
|
64
74
|
GTE: 'GTE', // >=
|
|
@@ -87,6 +97,7 @@ export const TokenType = {
|
|
|
87
97
|
MINUS_EQUALS: 'MINUS_EQUALS', // -=
|
|
88
98
|
STAR_EQUALS: 'STAR_EQUALS', // *=
|
|
89
99
|
SLASH_EQUALS: 'SLASH_EQUALS', // /=
|
|
100
|
+
PERCENT_EQUALS: 'PERCENT_EQUALS', // %=
|
|
90
101
|
TEMPLATE_LITERAL: 'TEMPLATE_LITERAL', // `text ${expr} text`
|
|
91
102
|
|
|
92
103
|
// Template-specific
|
|
@@ -110,7 +121,6 @@ export const KEYWORDS = {
|
|
|
110
121
|
capability: TokenType.CAPABILITY,
|
|
111
122
|
init: TokenType.INIT,
|
|
112
123
|
title: TokenType.TITLE,
|
|
113
|
-
layout: TokenType.LAYOUT,
|
|
114
124
|
needs: TokenType.NEEDS,
|
|
115
125
|
prefetch: TokenType.PREFETCH,
|
|
116
126
|
background: TokenType.BACKGROUND,
|
|
@@ -143,4 +153,11 @@ export const KEYWORDS = {
|
|
|
143
153
|
try: TokenType.TRY,
|
|
144
154
|
catch: TokenType.CATCH,
|
|
145
155
|
finally: TokenType.FINALLY,
|
|
146
|
-
|
|
156
|
+
typeof: TokenType.TYPEOF,
|
|
157
|
+
instanceof: TokenType.INSTANCEOF,
|
|
158
|
+
throw: TokenType.THROW,
|
|
159
|
+
while: TokenType.WHILE,
|
|
160
|
+
switch: TokenType.SWITCH,
|
|
161
|
+
case: TokenType.CASE,
|
|
162
|
+
default: TokenType.DEFAULT,
|
|
163
|
+
}
|