@tylix/compiler 0.5.1 → 0.6.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 +1 -1
- package/src/ast/nodes.js +91 -10
- package/src/ast/substituteCapabilityRefs.js +135 -58
- package/src/codegen/emitStatementLines.js +42 -48
- package/src/codegen/generateExpression.js +68 -24
- package/src/codegen/generateMethod.js +5 -3
- package/src/codegen/generatePage.js +74 -44
- package/src/codegen/generateStatement.js +56 -12
- package/src/codegen/generateTemplate.js +124 -53
- package/src/codegen/generateTemplateExpression.js +40 -50
- package/src/codegen/jsGlobals.js +151 -0
- package/src/compileComponent.js +2 -2
- package/src/imports/extractImports.js +112 -26
- package/src/imports/resolveImports.js +186 -0
- package/src/index.js +3 -2
- package/src/lexer/Lexer.js +308 -213
- package/src/lexer/tokenTypes.js +32 -7
- package/src/parser/Parser.js +680 -169
- package/src/parser/languageOptions.js +16 -0
- package/src/parser/parseExpressionString.js +18 -7
- package/src/parser/parsePageFile.js +139 -58
- package/src/parser/parseTemplate.js +202 -26
- package/src/renderPageDocument.js +297 -51
- package/src/runtime/capability.js +32 -17
- package/src/runtime/loadingIndicator.js +91 -29
- package/src/runtime/reactive.js +128 -58
- package/src/typecheck/buildVirtualTs.js +13 -12
- package/src/typecheck/typedTemplateExpr.js +115 -36
- package/src/typecheckPage.js +28 -4
- package/src/imports/resolvePackageComponents.js +0 -41
package/src/lexer/Lexer.js
CHANGED
|
@@ -1,15 +1,141 @@
|
|
|
1
1
|
import { TokenType, KEYWORDS } from './tokenTypes.js'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
const T = TokenType
|
|
4
|
+
|
|
5
|
+
// Multi-character operators, matched longest-first. JS-style spellings
|
|
6
|
+
// of Tylix word operators are aliased here:
|
|
7
|
+
// == / === -> is != / !== -> is not
|
|
8
|
+
// && -> and || -> or
|
|
9
|
+
// (equality is always strict.)
|
|
10
|
+
//
|
|
11
|
+
// Shifts (<< >> >>>) and their compound forms are deliberately NOT in
|
|
12
|
+
// this table: they come out as adjacent LT/GT/LTE/GTE tokens so that
|
|
13
|
+
// nested generics like Array<Array<T>> still close, and the Parser
|
|
14
|
+
// recombines them in expression context.
|
|
15
|
+
const MULTI_CHAR_OPERATORS = [
|
|
16
|
+
['??=', [[T.NULLISH_EQUALS, '??=']]],
|
|
17
|
+
['**=', [[T.STAR_STAR_EQUALS, '**=']]],
|
|
18
|
+
['||=', [[T.OR_EQUALS, '||=']]],
|
|
19
|
+
['&&=', [[T.AND_EQUALS, '&&=']]],
|
|
20
|
+
['...', [[T.SPREAD, '...']]],
|
|
21
|
+
['===', [[T.IS, 'is']]],
|
|
22
|
+
[
|
|
23
|
+
'!==',
|
|
24
|
+
[
|
|
25
|
+
[T.IS, 'is'],
|
|
26
|
+
[T.NOT, 'not'],
|
|
27
|
+
],
|
|
28
|
+
],
|
|
29
|
+
['=>', [[T.ARROW, '=>']]],
|
|
30
|
+
['==', [[T.IS, 'is']]],
|
|
31
|
+
[
|
|
32
|
+
'!=',
|
|
33
|
+
[
|
|
34
|
+
[T.IS, 'is'],
|
|
35
|
+
[T.NOT, 'not'],
|
|
36
|
+
],
|
|
37
|
+
],
|
|
38
|
+
['>=', [[T.GTE, '>=']]],
|
|
39
|
+
['<=', [[T.LTE, '<=']]],
|
|
40
|
+
['&&', [[T.AND, 'and']]],
|
|
41
|
+
['||', [[T.OR, 'or']]],
|
|
42
|
+
['??', [[T.NULLISH, '??']]],
|
|
43
|
+
['++', [[T.PLUS_PLUS, '++']]],
|
|
44
|
+
['--', [[T.MINUS_MINUS, '--']]],
|
|
45
|
+
['**', [[T.STAR_STAR, '**']]],
|
|
46
|
+
['+=', [[T.PLUS_EQUALS, '+=']]],
|
|
47
|
+
['-=', [[T.MINUS_EQUALS, '-=']]],
|
|
48
|
+
['*=', [[T.STAR_EQUALS, '*=']]],
|
|
49
|
+
['/=', [[T.SLASH_EQUALS, '/=']]],
|
|
50
|
+
['%=', [[T.PERCENT_EQUALS, '%=']]],
|
|
51
|
+
['&=', [[T.AMP_EQUALS, '&=']]],
|
|
52
|
+
['|=', [[T.PIPE_EQUALS, '|=']]],
|
|
53
|
+
['^=', [[T.CARET_EQUALS, '^=']]],
|
|
54
|
+
].sort((a, b) => b[0].length - a[0].length)
|
|
55
|
+
|
|
56
|
+
const SINGLE_CHAR_TOKENS = new Map([
|
|
57
|
+
['(', [T.LPAREN, '(']],
|
|
58
|
+
[')', [T.RPAREN, ')']],
|
|
59
|
+
['{', [T.LBRACE, '{']],
|
|
60
|
+
['}', [T.RBRACE, '}']],
|
|
61
|
+
['[', [T.LBRACKET, '[']],
|
|
62
|
+
[']', [T.RBRACKET, ']']],
|
|
63
|
+
[':', [T.COLON, ':']],
|
|
64
|
+
[',', [T.COMMA, ',']],
|
|
65
|
+
[';', [T.SEMICOLON, ';']],
|
|
66
|
+
['.', [T.DOT, '.']],
|
|
67
|
+
['=', [T.EQUALS, '=']],
|
|
68
|
+
['+', [T.PLUS, '+']],
|
|
69
|
+
['-', [T.MINUS, '-']],
|
|
70
|
+
['*', [T.STAR, '*']],
|
|
71
|
+
['/', [T.SLASH, '/']],
|
|
72
|
+
['%', [T.PERCENT, '%']],
|
|
73
|
+
['>', [T.GT, '>']],
|
|
74
|
+
['<', [T.LT, '<']],
|
|
75
|
+
['?', [T.QUESTION, '?']],
|
|
76
|
+
['|', [T.PIPE, '|']],
|
|
77
|
+
['&', [T.AMP, '&']],
|
|
78
|
+
['^', [T.CARET, '^']],
|
|
79
|
+
['~', [T.TILDE, '~']],
|
|
80
|
+
['!', [T.NOT, '!']],
|
|
81
|
+
])
|
|
82
|
+
|
|
83
|
+
// Tokens after which a "/" starts a regex literal (or a "." starts a
|
|
84
|
+
// number like .5) rather than being division / member access. After
|
|
85
|
+
// an identifier, number, string, ")" or "]" it is division instead.
|
|
86
|
+
const REGEX_PRECEDERS = new Set([
|
|
87
|
+
T.LPAREN,
|
|
88
|
+
T.LBRACKET,
|
|
89
|
+
T.LBRACE,
|
|
90
|
+
T.COMMA,
|
|
91
|
+
T.COLON,
|
|
92
|
+
T.SEMICOLON,
|
|
93
|
+
T.EQUALS,
|
|
94
|
+
T.ARROW,
|
|
95
|
+
T.QUESTION,
|
|
96
|
+
T.NULLISH,
|
|
97
|
+
T.NULLISH_EQUALS,
|
|
98
|
+
T.PLUS,
|
|
99
|
+
T.MINUS,
|
|
100
|
+
T.STAR,
|
|
101
|
+
T.SLASH,
|
|
102
|
+
T.PERCENT,
|
|
103
|
+
T.STAR_STAR,
|
|
104
|
+
T.GT,
|
|
105
|
+
T.LT,
|
|
106
|
+
T.GTE,
|
|
107
|
+
T.LTE,
|
|
108
|
+
T.PLUS_EQUALS,
|
|
109
|
+
T.MINUS_EQUALS,
|
|
110
|
+
T.STAR_EQUALS,
|
|
111
|
+
T.SLASH_EQUALS,
|
|
112
|
+
T.PERCENT_EQUALS,
|
|
113
|
+
T.STAR_STAR_EQUALS,
|
|
114
|
+
T.OR_EQUALS,
|
|
115
|
+
T.AND_EQUALS,
|
|
116
|
+
T.AMP_EQUALS,
|
|
117
|
+
T.PIPE_EQUALS,
|
|
118
|
+
T.CARET_EQUALS,
|
|
119
|
+
T.PIPE,
|
|
120
|
+
T.AMP,
|
|
121
|
+
T.CARET,
|
|
122
|
+
T.TILDE,
|
|
123
|
+
T.AND,
|
|
124
|
+
T.OR,
|
|
125
|
+
T.NOT,
|
|
126
|
+
T.IS,
|
|
127
|
+
T.HAS,
|
|
128
|
+
T.IN,
|
|
129
|
+
T.OF,
|
|
130
|
+
T.RETURN,
|
|
131
|
+
T.AWAIT,
|
|
132
|
+
T.TYPEOF,
|
|
133
|
+
T.DELETE,
|
|
134
|
+
T.VOID,
|
|
135
|
+
T.THROW,
|
|
136
|
+
T.CASE,
|
|
137
|
+
T.SPREAD,
|
|
138
|
+
])
|
|
13
139
|
|
|
14
140
|
export class Lexer {
|
|
15
141
|
constructor(source) {
|
|
@@ -17,233 +143,89 @@ export class Lexer {
|
|
|
17
143
|
this.pos = 0
|
|
18
144
|
this.line = 1
|
|
19
145
|
this.tokens = []
|
|
146
|
+
this.tokenStart = 0
|
|
20
147
|
}
|
|
21
148
|
|
|
22
149
|
tokenize() {
|
|
23
|
-
|
|
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
|
-
if (char === '|') {
|
|
72
|
-
this.pushToken(TokenType.PIPE, '|')
|
|
73
|
-
this.pos++
|
|
74
|
-
continue
|
|
75
|
-
}
|
|
76
|
-
|
|
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
|
-
}
|
|
150
|
+
const src = this.source
|
|
85
151
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
98
|
-
if (char === '+' && this.source[this.pos + 1] === '=') {
|
|
99
|
-
this.pushToken(TokenType.PLUS_EQUALS, '+=')
|
|
100
|
-
this.pos += 2
|
|
101
|
-
continue
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (char === '-' && this.source[this.pos + 1] === '=') {
|
|
105
|
-
this.pushToken(TokenType.MINUS_EQUALS, '-=')
|
|
106
|
-
this.pos += 2
|
|
107
|
-
continue
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (char === '*' && this.source[this.pos + 1] === '=') {
|
|
111
|
-
this.pushToken(TokenType.STAR_EQUALS, '*=')
|
|
112
|
-
this.pos += 2
|
|
113
|
-
continue
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
if (char === '/' && this.source[this.pos + 1] === '=') {
|
|
117
|
-
this.pushToken(TokenType.SLASH_EQUALS, '/=')
|
|
118
|
-
this.pos += 2
|
|
119
|
-
continue
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (char === '%' && this.source[this.pos + 1] === '=') {
|
|
123
|
-
this.pushToken(TokenType.PERCENT_EQUALS, '%=')
|
|
124
|
-
this.pos += 2
|
|
125
|
-
continue
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (char === '+') {
|
|
129
|
-
this.pushToken(TokenType.PLUS, '+')
|
|
130
|
-
this.pos++
|
|
131
|
-
continue
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
if (char === '-') {
|
|
135
|
-
this.pushToken(TokenType.MINUS, '-')
|
|
136
|
-
this.pos++
|
|
137
|
-
continue
|
|
138
|
-
}
|
|
152
|
+
while (this.pos < src.length) {
|
|
153
|
+
this.skipWhitespaceAndComments()
|
|
154
|
+
if (this.pos >= src.length) break
|
|
139
155
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
this.pos++
|
|
143
|
-
continue
|
|
144
|
-
}
|
|
156
|
+
this.tokenStart = this.pos
|
|
157
|
+
const char = src[this.pos]
|
|
145
158
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
continue
|
|
150
|
-
}
|
|
159
|
+
// NOTE: "{{" and "}}" are intentionally NOT tokenized. Template
|
|
160
|
+
// interpolations are located at the string level by parseTemplate;
|
|
161
|
+
// tokenizing them broke object literals like {a:{b:1}}.
|
|
151
162
|
|
|
152
|
-
if (char === '
|
|
153
|
-
this.
|
|
154
|
-
this.pos++
|
|
163
|
+
if (char === '/' && this.regexAllowed()) {
|
|
164
|
+
this.readRegex()
|
|
155
165
|
continue
|
|
156
166
|
}
|
|
157
167
|
|
|
158
168
|
if (
|
|
159
|
-
char === '
|
|
160
|
-
|
|
161
|
-
this.
|
|
169
|
+
char === '.' &&
|
|
170
|
+
/[0-9]/.test(src[this.pos + 1] ?? '') &&
|
|
171
|
+
this.regexAllowed()
|
|
162
172
|
) {
|
|
163
|
-
this.
|
|
164
|
-
this.pos += 3
|
|
173
|
+
this.readNumber() // .5
|
|
165
174
|
continue
|
|
166
175
|
}
|
|
167
176
|
|
|
168
|
-
if (char === '
|
|
169
|
-
this.
|
|
170
|
-
this.pos += 2
|
|
171
|
-
continue
|
|
172
|
-
}
|
|
173
|
-
if (
|
|
174
|
-
char === '?' &&
|
|
175
|
-
this.source[this.pos + 1] === '.' &&
|
|
176
|
-
!/[0-9]/.test(this.source[this.pos + 2] ?? '')
|
|
177
|
-
) {
|
|
178
|
-
this.pushToken(TokenType.QUESTION_DOT, '?.')
|
|
179
|
-
this.pos += 2
|
|
177
|
+
if (char === '`') {
|
|
178
|
+
this.readTemplateLiteral()
|
|
180
179
|
continue
|
|
181
180
|
}
|
|
182
181
|
|
|
183
|
-
if (char === '
|
|
184
|
-
this.
|
|
185
|
-
this.pos++
|
|
182
|
+
if (char === '"' || char === "'") {
|
|
183
|
+
this.readString(char)
|
|
186
184
|
continue
|
|
187
185
|
}
|
|
188
186
|
|
|
189
|
-
if (char
|
|
190
|
-
this.
|
|
191
|
-
this.pos++
|
|
187
|
+
if (/[0-9]/.test(char)) {
|
|
188
|
+
this.readNumber()
|
|
192
189
|
continue
|
|
193
190
|
}
|
|
194
191
|
|
|
195
|
-
if (char
|
|
196
|
-
this.
|
|
197
|
-
this.pos++
|
|
192
|
+
if (/[a-zA-Z_$]/.test(char)) {
|
|
193
|
+
this.readIdentifierOrKeyword()
|
|
198
194
|
continue
|
|
199
195
|
}
|
|
200
196
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
197
|
+
const multi = MULTI_CHAR_OPERATORS.find(([op]) =>
|
|
198
|
+
src.startsWith(op, this.pos),
|
|
199
|
+
)
|
|
200
|
+
if (multi) {
|
|
201
|
+
for (const [type, value] of multi[1]) this.pushToken(type, value)
|
|
202
|
+
this.pos += multi[0].length
|
|
204
203
|
continue
|
|
205
204
|
}
|
|
206
205
|
|
|
206
|
+
// Optional chaining, but not the ternary in `a?.5:1`
|
|
207
207
|
if (
|
|
208
|
-
char === '
|
|
209
|
-
|
|
210
|
-
|
|
208
|
+
char === '?' &&
|
|
209
|
+
src[this.pos + 1] === '.' &&
|
|
210
|
+
!/[0-9]/.test(src[this.pos + 2] ?? '')
|
|
211
211
|
) {
|
|
212
|
-
this.pushToken(
|
|
213
|
-
this.pos +=
|
|
212
|
+
this.pushToken(T.QUESTION_DOT, '?.')
|
|
213
|
+
this.pos += 2
|
|
214
214
|
continue
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
-
|
|
218
|
-
|
|
217
|
+
const single = SINGLE_CHAR_TOKENS.get(char)
|
|
218
|
+
if (single) {
|
|
219
|
+
this.pushToken(single[0], single[1])
|
|
219
220
|
this.pos++
|
|
220
221
|
continue
|
|
221
222
|
}
|
|
222
223
|
|
|
223
|
-
if (char === '`') {
|
|
224
|
-
this.readTemplateLiteral()
|
|
225
|
-
continue
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (char === '"' || char === "'") {
|
|
229
|
-
this.readString(char)
|
|
230
|
-
continue
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
if (/[0-9]/.test(char)) {
|
|
234
|
-
this.readNumber()
|
|
235
|
-
continue
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (/[a-zA-Z_$]/.test(char)) {
|
|
239
|
-
this.readIdentifierOrKeyword()
|
|
240
|
-
continue
|
|
241
|
-
}
|
|
242
|
-
|
|
243
224
|
throw new Error(`Unexpected character "${char}" at line ${this.line}`)
|
|
244
225
|
}
|
|
245
226
|
|
|
246
|
-
this.
|
|
227
|
+
this.tokenStart = this.pos
|
|
228
|
+
this.pushToken(T.EOF, null)
|
|
247
229
|
return this.tokens
|
|
248
230
|
}
|
|
249
231
|
|
|
@@ -289,12 +271,51 @@ export class Lexer {
|
|
|
289
271
|
}
|
|
290
272
|
}
|
|
291
273
|
|
|
274
|
+
// True when a "/" (or a ".5") at the current position should start a
|
|
275
|
+
// regex literal (or number) based on the previous token.
|
|
276
|
+
regexAllowed() {
|
|
277
|
+
const prev = this.tokens[this.tokens.length - 1]
|
|
278
|
+
return !prev || REGEX_PRECEDERS.has(prev.type)
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
readRegex() {
|
|
282
|
+
const src = this.source
|
|
283
|
+
const startLine = this.line
|
|
284
|
+
this.pos++ // opening "/"
|
|
285
|
+
const bodyStart = this.pos
|
|
286
|
+
let inClass = false
|
|
287
|
+
while (this.pos < src.length) {
|
|
288
|
+
const c = src[this.pos]
|
|
289
|
+
if (c === '\n') break
|
|
290
|
+
if (c === '\\') {
|
|
291
|
+
this.pos += 2
|
|
292
|
+
continue
|
|
293
|
+
}
|
|
294
|
+
if (c === '[') inClass = true
|
|
295
|
+
else if (c === ']') inClass = false
|
|
296
|
+
else if (c === '/' && !inClass) break
|
|
297
|
+
this.pos++
|
|
298
|
+
}
|
|
299
|
+
if (src[this.pos] !== '/') {
|
|
300
|
+
throw new Error(`Unterminated regular expression at line ${startLine}`)
|
|
301
|
+
}
|
|
302
|
+
const pattern = src.slice(bodyStart, this.pos)
|
|
303
|
+
this.pos++ // closing "/"
|
|
304
|
+
const flagStart = this.pos
|
|
305
|
+
while (/[a-z]/.test(src[this.pos] ?? '')) this.pos++
|
|
306
|
+
const flags = src.slice(flagStart, this.pos)
|
|
307
|
+
this.pushToken(T.REGEX, { pattern, flags }, startLine)
|
|
308
|
+
}
|
|
309
|
+
|
|
292
310
|
// Called with this.pos on the backslash. Consumes the escape
|
|
293
|
-
// sequence and returns the character(s) it stands for.
|
|
294
|
-
//
|
|
311
|
+
// sequence and returns the character(s) it stands for. Supports the
|
|
312
|
+
// usual \n \t \r \b \f \v \0, hex \xNN, unicode \uNNNN and \u{...},
|
|
313
|
+
// and line continuations. Unknown escapes (\\, \', \", \`, \$) just
|
|
314
|
+
// yield the escaped character.
|
|
295
315
|
readEscape() {
|
|
316
|
+
const src = this.source
|
|
296
317
|
this.pos++ // skip '\'
|
|
297
|
-
const c =
|
|
318
|
+
const c = src[this.pos]
|
|
298
319
|
if (c === undefined) return ''
|
|
299
320
|
this.pos++
|
|
300
321
|
switch (c) {
|
|
@@ -312,6 +333,41 @@ export class Lexer {
|
|
|
312
333
|
return '\v'
|
|
313
334
|
case '0':
|
|
314
335
|
return '\0'
|
|
336
|
+
case 'x': {
|
|
337
|
+
const hex = src.slice(this.pos, this.pos + 2)
|
|
338
|
+
if (/^[0-9a-fA-F]{2}$/.test(hex)) {
|
|
339
|
+
this.pos += 2
|
|
340
|
+
return String.fromCharCode(parseInt(hex, 16))
|
|
341
|
+
}
|
|
342
|
+
return 'x'
|
|
343
|
+
}
|
|
344
|
+
case 'u': {
|
|
345
|
+
if (src[this.pos] === '{') {
|
|
346
|
+
const close = src.indexOf('}', this.pos)
|
|
347
|
+
const hex = close === -1 ? '' : src.slice(this.pos + 1, close)
|
|
348
|
+
if (/^[0-9a-fA-F]+$/.test(hex)) {
|
|
349
|
+
const code = parseInt(hex, 16)
|
|
350
|
+
if (code > 0x10ffff) {
|
|
351
|
+
throw new Error(
|
|
352
|
+
`Invalid unicode escape \\u{${hex}} at line ${this.line}`,
|
|
353
|
+
)
|
|
354
|
+
}
|
|
355
|
+
this.pos = close + 1
|
|
356
|
+
return String.fromCodePoint(code)
|
|
357
|
+
}
|
|
358
|
+
return 'u'
|
|
359
|
+
}
|
|
360
|
+
const hex = src.slice(this.pos, this.pos + 4)
|
|
361
|
+
if (/^[0-9a-fA-F]{4}$/.test(hex)) {
|
|
362
|
+
this.pos += 4
|
|
363
|
+
return String.fromCharCode(parseInt(hex, 16))
|
|
364
|
+
}
|
|
365
|
+
return 'u'
|
|
366
|
+
}
|
|
367
|
+
case '\r':
|
|
368
|
+
if (src[this.pos] === '\n') this.pos++
|
|
369
|
+
this.line++ // line continuation
|
|
370
|
+
return ''
|
|
315
371
|
case '\n':
|
|
316
372
|
this.line++ // line continuation
|
|
317
373
|
return ''
|
|
@@ -340,7 +396,7 @@ export class Lexer {
|
|
|
340
396
|
throw new Error(`Unterminated string starting at line ${startLine}`)
|
|
341
397
|
}
|
|
342
398
|
this.pos++ // skip closing quote
|
|
343
|
-
this.pushToken(
|
|
399
|
+
this.pushToken(T.STRING, value, startLine)
|
|
344
400
|
}
|
|
345
401
|
|
|
346
402
|
// Scans a backtick template literal into a flat list of parts --
|
|
@@ -348,8 +404,7 @@ export class Lexer {
|
|
|
348
404
|
// for each ${...} span -- rather than re-entering full tokenization
|
|
349
405
|
// mid-string. Each expr span's raw source gets parsed later via
|
|
350
406
|
// parseExpressionString, reusing the same mechanism already used for
|
|
351
|
-
// {{ }} interpolations and #if/#each headers
|
|
352
|
-
// the Lexer to interleave live token scanning inside a string.
|
|
407
|
+
// {{ }} interpolations and #if/#each headers.
|
|
353
408
|
readTemplateLiteral() {
|
|
354
409
|
const startLine = this.line
|
|
355
410
|
this.pos++ // skip opening backtick
|
|
@@ -408,31 +463,68 @@ export class Lexer {
|
|
|
408
463
|
}
|
|
409
464
|
|
|
410
465
|
flushText()
|
|
411
|
-
this.pushToken(
|
|
466
|
+
this.pushToken(T.TEMPLATE_LITERAL, parts, startLine)
|
|
412
467
|
}
|
|
413
468
|
|
|
469
|
+
// Numbers: 42, 3.14, .5, 1_000, 1e6, 2.5e-3, 0xFF, 0b101, 0o17,
|
|
470
|
+
// BigInt (10n, 0xFFn) and durations (500ms, 2s, 5m, 1h, 7d). A "."
|
|
471
|
+
// is only part of the number when a digit follows, so
|
|
472
|
+
// `1.5.toFixed(1)` still works.
|
|
414
473
|
readNumber() {
|
|
474
|
+
const src = this.source
|
|
415
475
|
const start = this.pos
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
476
|
+
const isDigit = (c) => /[0-9_]/.test(c ?? '')
|
|
477
|
+
const isIdentChar = (c) => /[A-Za-z0-9_$]/.test(c ?? '')
|
|
478
|
+
const bigIntSuffix = () =>
|
|
479
|
+
src[this.pos] === 'n' && !isIdentChar(src[this.pos + 1])
|
|
480
|
+
|
|
481
|
+
// 0xFF, 0b101, 0o17
|
|
482
|
+
if (src[this.pos] === '0' && /[xXbBoO]/.test(src[this.pos + 1] ?? '')) {
|
|
483
|
+
this.pos += 2
|
|
484
|
+
while (/[0-9a-fA-F_]/.test(src[this.pos] ?? '')) this.pos++
|
|
485
|
+
const text = src.slice(start, this.pos).replace(/_/g, '')
|
|
486
|
+
if (bigIntSuffix()) {
|
|
487
|
+
this.pos++
|
|
488
|
+
this.pushToken(T.BIGINT, text)
|
|
489
|
+
return
|
|
490
|
+
}
|
|
491
|
+
this.pushToken(T.NUMBER, Number(text))
|
|
492
|
+
return
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
let isInteger = true
|
|
496
|
+
while (isDigit(src[this.pos])) this.pos++
|
|
497
|
+
if (src[this.pos] === '.' && /[0-9]/.test(src[this.pos + 1] ?? '')) {
|
|
498
|
+
isInteger = false
|
|
420
499
|
this.pos++
|
|
500
|
+
while (isDigit(src[this.pos])) this.pos++
|
|
501
|
+
}
|
|
502
|
+
// exponent: 1e6, 2.5e-3
|
|
503
|
+
if (/^[eE][+-]?[0-9]/.test(src.slice(this.pos, this.pos + 3))) {
|
|
504
|
+
isInteger = false
|
|
505
|
+
this.pos += 2
|
|
506
|
+
while (isDigit(src[this.pos])) this.pos++
|
|
507
|
+
}
|
|
508
|
+
const text = src.slice(start, this.pos).replace(/_/g, '')
|
|
509
|
+
|
|
510
|
+
if (isInteger && bigIntSuffix()) {
|
|
511
|
+
this.pos++
|
|
512
|
+
this.pushToken(T.BIGINT, text)
|
|
513
|
+
return
|
|
421
514
|
}
|
|
422
|
-
const text = this.source.slice(start, this.pos)
|
|
423
515
|
|
|
424
|
-
const unitMatch = /^(ms|s|m|h|d)/.exec(
|
|
516
|
+
const unitMatch = /^(ms|s|m|h|d)/.exec(src.slice(this.pos))
|
|
425
517
|
if (
|
|
426
518
|
unitMatch &&
|
|
427
|
-
!/[A-Za-z0-9_$]/.test(
|
|
519
|
+
!/[A-Za-z0-9_$]/.test(src[this.pos + unitMatch[0].length] ?? '')
|
|
428
520
|
) {
|
|
429
521
|
const unit = unitMatch[0]
|
|
430
522
|
this.pos += unit.length
|
|
431
|
-
this.pushToken(
|
|
523
|
+
this.pushToken(T.DURATION, `${text}${unit}`)
|
|
432
524
|
return
|
|
433
525
|
}
|
|
434
526
|
|
|
435
|
-
this.pushToken(
|
|
527
|
+
this.pushToken(T.NUMBER, Number(text))
|
|
436
528
|
}
|
|
437
529
|
|
|
438
530
|
readIdentifierOrKeyword() {
|
|
@@ -444,11 +536,14 @@ export class Lexer {
|
|
|
444
536
|
this.pos++
|
|
445
537
|
}
|
|
446
538
|
const text = this.source.slice(start, this.pos)
|
|
447
|
-
const type = KEYWORDS[text] ??
|
|
539
|
+
const type = KEYWORDS[text] ?? T.IDENTIFIER
|
|
448
540
|
this.pushToken(type, text)
|
|
449
541
|
}
|
|
450
542
|
|
|
543
|
+
// `start` is the source offset where the current token began; the
|
|
544
|
+
// Parser uses it to tell whether two tokens were written adjacent
|
|
545
|
+
// (">" ">" is a shift, "> >" is not).
|
|
451
546
|
pushToken(type, value, line = this.line) {
|
|
452
|
-
this.tokens.push({ type, value, line })
|
|
547
|
+
this.tokens.push({ type, value, line, start: this.tokenStart })
|
|
453
548
|
}
|
|
454
549
|
}
|