@tylix/compiler 0.5.2 → 0.7.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/LICENSE +21 -0
- package/package.json +30 -2
- package/src/ast/nodes.js +219 -16
- package/src/ast/substituteCapabilityRefs.js +185 -61
- package/src/codegen/emitStatementLines.js +98 -54
- package/src/codegen/generateExpression.js +339 -36
- package/src/codegen/generateMethod.js +5 -3
- package/src/codegen/generatePage.js +74 -44
- package/src/codegen/generateStatement.js +169 -20
- package/src/codegen/generateTemplate.js +183 -101
- package/src/codegen/generateTemplateExpression.js +102 -59
- package/src/codegen/jsGlobals.js +211 -0
- package/src/compileComponent.js +39 -8
- 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 +430 -235
- package/src/lexer/tokenTypes.js +32 -7
- package/src/parser/Parser.js +2176 -325
- package/src/parser/languageOptions.js +16 -0
- package/src/parser/parseExpressionString.js +23 -7
- package/src/parser/parsePageFile.js +139 -58
- package/src/parser/parseTemplate.js +350 -62
- package/src/renderPageDocument.js +338 -56
- package/src/runtime/capability.js +39 -17
- package/src/runtime/loadingIndicator.js +91 -29
- package/src/runtime/reactive.js +362 -64
- package/src/runtime/useApi.js +74 -13
- package/src/runtime/useChannel.js +27 -6
- package/src/typecheck/buildVirtualTs.js +23 -12
- package/src/typecheck/tylix-globals.d.ts +23 -4
- package/src/typecheck/typedTemplateExpr.js +195 -53
- package/src/typecheckPage.js +28 -4
- package/src/codegen/generateExpression.array.test.js +0 -13
- package/src/codegen/generateExpression.callexpr.test.js +0 -19
- package/src/codegen/generateExpression.test.js +0 -59
- package/src/codegen/generateMethod.asyncawait.test.js +0 -37
- package/src/codegen/generateMethod.ifstatement.test.js +0 -20
- package/src/codegen/generateMethod.test.js +0 -81
- package/src/codegen/generatePage.onmount.test.js +0 -46
- package/src/codegen/generatePage.test.js +0 -138
- package/src/codegen/generateStatement.test.js +0 -31
- package/src/codegen/generateTemplate.ifeach.test.js +0 -138
- package/src/codegen/generateTemplate.test.js +0 -149
- package/src/codegen/generateTemplateExpression.test.js +0 -34
- package/src/compileComponent.test.js +0 -121
- package/src/imports/resolvePackageComponents.js +0 -41
- package/src/lexer/Lexer.test.js +0 -151
- package/src/parser/Parser.array.test.js +0 -37
- package/src/parser/Parser.asyncawait.test.js +0 -33
- package/src/parser/Parser.ifstatement.test.js +0 -20
- package/src/parser/Parser.onmount.test.js +0 -18
- package/src/parser/Parser.test.js +0 -315
- package/src/parser/parseComponent.test.js +0 -53
- package/src/parser/parsePageFile.test.js +0 -101
- package/src/parser/parseTemplate.ifeach.test.js +0 -33
- package/src/parser/parseTemplate.pre.test.js +0 -39
- package/src/parser/parseTemplate.test.js +0 -112
- package/src/renderPageDocument.test.js +0 -125
- package/src/runtime/reactive.test.js +0 -106
- package/test/dashboard-syntax.test.js +0 -116
package/src/lexer/Lexer.js
CHANGED
|
@@ -1,249 +1,254 @@
|
|
|
1
1
|
import { TokenType, KEYWORDS } from './tokenTypes.js'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
const T = TokenType
|
|
4
|
+
|
|
5
|
+
// Identifier characters follow the JS spec (Unicode ID_Start /
|
|
6
|
+
// ID_Continue plus $ and _), so names like `café` or `変数` work.
|
|
7
|
+
const ID_START = /^[\p{ID_Start}_$]$/u
|
|
8
|
+
const ID_PART = /^[\p{ID_Continue}$\u200c\u200d]$/u
|
|
9
|
+
|
|
10
|
+
// The whole code point at index i (handles surrogate pairs), or ''.
|
|
11
|
+
function codePointStringAt(src, i) {
|
|
12
|
+
const cp = src.codePointAt(i)
|
|
13
|
+
return cp === undefined ? '' : String.fromCodePoint(cp)
|
|
12
14
|
}
|
|
13
15
|
|
|
16
|
+
// Multi-character operators, matched longest-first. JS-style spellings
|
|
17
|
+
// of Tylix word operators are aliased here:
|
|
18
|
+
// == / === -> is != / !== -> is not
|
|
19
|
+
// && -> and || -> or
|
|
20
|
+
// (equality is always strict.)
|
|
21
|
+
//
|
|
22
|
+
// Shifts (<< >> >>>) and their compound forms are deliberately NOT in
|
|
23
|
+
// this table: they come out as adjacent LT/GT/LTE/GTE tokens so that
|
|
24
|
+
// nested generics like Array<Array<T>> still close, and the Parser
|
|
25
|
+
// recombines them in expression context.
|
|
26
|
+
const MULTI_CHAR_OPERATORS = [
|
|
27
|
+
['??=', [[T.NULLISH_EQUALS, '??=']]],
|
|
28
|
+
['**=', [[T.STAR_STAR_EQUALS, '**=']]],
|
|
29
|
+
['||=', [[T.OR_EQUALS, '||=']]],
|
|
30
|
+
['&&=', [[T.AND_EQUALS, '&&=']]],
|
|
31
|
+
['...', [[T.SPREAD, '...']]],
|
|
32
|
+
['===', [[T.IS, 'is']]],
|
|
33
|
+
[
|
|
34
|
+
'!==',
|
|
35
|
+
[
|
|
36
|
+
[T.IS, 'is'],
|
|
37
|
+
[T.NOT, 'not'],
|
|
38
|
+
],
|
|
39
|
+
],
|
|
40
|
+
['=>', [[T.ARROW, '=>']]],
|
|
41
|
+
['==', [[T.IS, 'is']]],
|
|
42
|
+
[
|
|
43
|
+
'!=',
|
|
44
|
+
[
|
|
45
|
+
[T.IS, 'is'],
|
|
46
|
+
[T.NOT, 'not'],
|
|
47
|
+
],
|
|
48
|
+
],
|
|
49
|
+
['>=', [[T.GTE, '>=']]],
|
|
50
|
+
['<=', [[T.LTE, '<=']]],
|
|
51
|
+
['&&', [[T.AND, 'and']]],
|
|
52
|
+
['||', [[T.OR, 'or']]],
|
|
53
|
+
['??', [[T.NULLISH, '??']]],
|
|
54
|
+
['++', [[T.PLUS_PLUS, '++']]],
|
|
55
|
+
['--', [[T.MINUS_MINUS, '--']]],
|
|
56
|
+
['**', [[T.STAR_STAR, '**']]],
|
|
57
|
+
['+=', [[T.PLUS_EQUALS, '+=']]],
|
|
58
|
+
['-=', [[T.MINUS_EQUALS, '-=']]],
|
|
59
|
+
['*=', [[T.STAR_EQUALS, '*=']]],
|
|
60
|
+
['/=', [[T.SLASH_EQUALS, '/=']]],
|
|
61
|
+
['%=', [[T.PERCENT_EQUALS, '%=']]],
|
|
62
|
+
['&=', [[T.AMP_EQUALS, '&=']]],
|
|
63
|
+
['|=', [[T.PIPE_EQUALS, '|=']]],
|
|
64
|
+
['^=', [[T.CARET_EQUALS, '^=']]],
|
|
65
|
+
].sort((a, b) => b[0].length - a[0].length)
|
|
66
|
+
|
|
67
|
+
const SINGLE_CHAR_TOKENS = new Map([
|
|
68
|
+
['(', [T.LPAREN, '(']],
|
|
69
|
+
[')', [T.RPAREN, ')']],
|
|
70
|
+
['{', [T.LBRACE, '{']],
|
|
71
|
+
['}', [T.RBRACE, '}']],
|
|
72
|
+
['[', [T.LBRACKET, '[']],
|
|
73
|
+
[']', [T.RBRACKET, ']']],
|
|
74
|
+
[':', [T.COLON, ':']],
|
|
75
|
+
[',', [T.COMMA, ',']],
|
|
76
|
+
[';', [T.SEMICOLON, ';']],
|
|
77
|
+
['.', [T.DOT, '.']],
|
|
78
|
+
['=', [T.EQUALS, '=']],
|
|
79
|
+
['+', [T.PLUS, '+']],
|
|
80
|
+
['-', [T.MINUS, '-']],
|
|
81
|
+
['*', [T.STAR, '*']],
|
|
82
|
+
['/', [T.SLASH, '/']],
|
|
83
|
+
['%', [T.PERCENT, '%']],
|
|
84
|
+
['>', [T.GT, '>']],
|
|
85
|
+
['<', [T.LT, '<']],
|
|
86
|
+
['?', [T.QUESTION, '?']],
|
|
87
|
+
['|', [T.PIPE, '|']],
|
|
88
|
+
['&', [T.AMP, '&']],
|
|
89
|
+
['^', [T.CARET, '^']],
|
|
90
|
+
['~', [T.TILDE, '~']],
|
|
91
|
+
['!', [T.NOT, '!']],
|
|
92
|
+
])
|
|
93
|
+
|
|
94
|
+
// Tokens after which a "/" starts a regex literal (or a "." starts a
|
|
95
|
+
// number like .5) rather than being division / member access. After
|
|
96
|
+
// an identifier, number, string, ")" or "]" it is division instead.
|
|
97
|
+
const REGEX_PRECEDERS = new Set([
|
|
98
|
+
T.LPAREN,
|
|
99
|
+
T.LBRACKET,
|
|
100
|
+
T.LBRACE,
|
|
101
|
+
T.COMMA,
|
|
102
|
+
T.COLON,
|
|
103
|
+
T.SEMICOLON,
|
|
104
|
+
T.EQUALS,
|
|
105
|
+
T.ARROW,
|
|
106
|
+
T.QUESTION,
|
|
107
|
+
T.NULLISH,
|
|
108
|
+
T.NULLISH_EQUALS,
|
|
109
|
+
T.PLUS,
|
|
110
|
+
T.MINUS,
|
|
111
|
+
T.STAR,
|
|
112
|
+
T.SLASH,
|
|
113
|
+
T.PERCENT,
|
|
114
|
+
T.STAR_STAR,
|
|
115
|
+
T.GT,
|
|
116
|
+
T.LT,
|
|
117
|
+
T.GTE,
|
|
118
|
+
T.LTE,
|
|
119
|
+
T.PLUS_EQUALS,
|
|
120
|
+
T.MINUS_EQUALS,
|
|
121
|
+
T.STAR_EQUALS,
|
|
122
|
+
T.SLASH_EQUALS,
|
|
123
|
+
T.PERCENT_EQUALS,
|
|
124
|
+
T.STAR_STAR_EQUALS,
|
|
125
|
+
T.OR_EQUALS,
|
|
126
|
+
T.AND_EQUALS,
|
|
127
|
+
T.AMP_EQUALS,
|
|
128
|
+
T.PIPE_EQUALS,
|
|
129
|
+
T.CARET_EQUALS,
|
|
130
|
+
T.PIPE,
|
|
131
|
+
T.AMP,
|
|
132
|
+
T.CARET,
|
|
133
|
+
T.TILDE,
|
|
134
|
+
T.AND,
|
|
135
|
+
T.OR,
|
|
136
|
+
T.NOT,
|
|
137
|
+
T.IS,
|
|
138
|
+
T.HAS,
|
|
139
|
+
T.IN,
|
|
140
|
+
T.OF,
|
|
141
|
+
T.IF,
|
|
142
|
+
T.ELSE,
|
|
143
|
+
T.DO,
|
|
144
|
+
T.WHILE,
|
|
145
|
+
T.SWITCH,
|
|
146
|
+
T.INSTANCEOF,
|
|
147
|
+
T.RETURN,
|
|
148
|
+
T.AWAIT,
|
|
149
|
+
T.TYPEOF,
|
|
150
|
+
T.DELETE,
|
|
151
|
+
T.VOID,
|
|
152
|
+
T.THROW,
|
|
153
|
+
T.CASE,
|
|
154
|
+
T.SPREAD,
|
|
155
|
+
])
|
|
156
|
+
|
|
14
157
|
export class Lexer {
|
|
15
158
|
constructor(source) {
|
|
16
159
|
this.source = source
|
|
17
160
|
this.pos = 0
|
|
18
161
|
this.line = 1
|
|
19
162
|
this.tokens = []
|
|
163
|
+
this.tokenStart = 0
|
|
20
164
|
}
|
|
21
165
|
|
|
22
166
|
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
|
-
}
|
|
167
|
+
const src = this.source
|
|
52
168
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
}
|
|
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
|
-
|
|
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
|
-
}
|
|
169
|
+
while (this.pos < src.length) {
|
|
170
|
+
this.skipWhitespaceAndComments()
|
|
171
|
+
if (this.pos >= src.length) break
|
|
109
172
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
this.pos += 2
|
|
113
|
-
continue
|
|
114
|
-
}
|
|
173
|
+
this.tokenStart = this.pos
|
|
174
|
+
const char = src[this.pos]
|
|
115
175
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
continue
|
|
120
|
-
}
|
|
176
|
+
// NOTE: "{{" and "}}" are intentionally NOT tokenized. Template
|
|
177
|
+
// interpolations are located at the string level by parseTemplate;
|
|
178
|
+
// tokenizing them broke object literals like {a:{b:1}}.
|
|
121
179
|
|
|
122
|
-
if (char === '
|
|
123
|
-
this.
|
|
124
|
-
this.pos += 2
|
|
180
|
+
if (char === '/' && this.regexAllowed()) {
|
|
181
|
+
this.readRegex()
|
|
125
182
|
continue
|
|
126
183
|
}
|
|
127
184
|
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
this.pos
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (char === '-') {
|
|
135
|
-
this.pushToken(TokenType.MINUS, '-')
|
|
136
|
-
this.pos++
|
|
185
|
+
if (
|
|
186
|
+
char === '.' &&
|
|
187
|
+
/[0-9]/.test(src[this.pos + 1] ?? '') &&
|
|
188
|
+
this.regexAllowed()
|
|
189
|
+
) {
|
|
190
|
+
this.readNumber() // .5
|
|
137
191
|
continue
|
|
138
192
|
}
|
|
139
193
|
|
|
140
|
-
if (char === '
|
|
141
|
-
this.
|
|
142
|
-
this.pos++
|
|
194
|
+
if (char === '`') {
|
|
195
|
+
this.readTemplateLiteral()
|
|
143
196
|
continue
|
|
144
197
|
}
|
|
145
198
|
|
|
146
|
-
if (char === '
|
|
147
|
-
this.
|
|
148
|
-
this.pos++
|
|
199
|
+
if (char === '"' || char === "'") {
|
|
200
|
+
this.readString(char)
|
|
149
201
|
continue
|
|
150
202
|
}
|
|
151
203
|
|
|
152
|
-
if (char
|
|
153
|
-
this.
|
|
154
|
-
this.pos++
|
|
204
|
+
if (/[0-9]/.test(char)) {
|
|
205
|
+
this.readNumber()
|
|
155
206
|
continue
|
|
156
207
|
}
|
|
157
208
|
|
|
209
|
+
// Identifiers: any Unicode ID_Start character, or `#name` (a
|
|
210
|
+
// class private name, lexed as one identifier token).
|
|
211
|
+
const ch = codePointStringAt(src, this.pos)
|
|
158
212
|
if (
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
this.source[this.pos + 2] === '='
|
|
213
|
+
ID_START.test(ch) ||
|
|
214
|
+
(ch === '#' && ID_START.test(codePointStringAt(src, this.pos + 1)))
|
|
162
215
|
) {
|
|
163
|
-
this.
|
|
164
|
-
this.pos += 3
|
|
216
|
+
this.readIdentifierOrKeyword()
|
|
165
217
|
continue
|
|
166
218
|
}
|
|
167
219
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
220
|
+
const multi = MULTI_CHAR_OPERATORS.find(([op]) =>
|
|
221
|
+
src.startsWith(op, this.pos),
|
|
222
|
+
)
|
|
223
|
+
if (multi) {
|
|
224
|
+
for (const [type, value] of multi[1]) this.pushToken(type, value)
|
|
225
|
+
this.pos += multi[0].length
|
|
171
226
|
continue
|
|
172
227
|
}
|
|
228
|
+
|
|
229
|
+
// Optional chaining, but not the ternary in `a?.5:1`
|
|
173
230
|
if (
|
|
174
231
|
char === '?' &&
|
|
175
|
-
|
|
176
|
-
!/[0-9]/.test(
|
|
232
|
+
src[this.pos + 1] === '.' &&
|
|
233
|
+
!/[0-9]/.test(src[this.pos + 2] ?? '')
|
|
177
234
|
) {
|
|
178
|
-
this.pushToken(
|
|
235
|
+
this.pushToken(T.QUESTION_DOT, '?.')
|
|
179
236
|
this.pos += 2
|
|
180
237
|
continue
|
|
181
238
|
}
|
|
182
239
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
this.
|
|
186
|
-
continue
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (char === '>') {
|
|
190
|
-
this.pushToken(TokenType.GT, '>')
|
|
240
|
+
const single = SINGLE_CHAR_TOKENS.get(char)
|
|
241
|
+
if (single) {
|
|
242
|
+
this.pushToken(single[0], single[1])
|
|
191
243
|
this.pos++
|
|
192
244
|
continue
|
|
193
245
|
}
|
|
194
246
|
|
|
195
|
-
if (char === '<') {
|
|
196
|
-
this.pushToken(TokenType.LT, '<')
|
|
197
|
-
this.pos++
|
|
198
|
-
continue
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (char === '=') {
|
|
202
|
-
this.pushToken(TokenType.EQUALS, '=')
|
|
203
|
-
this.pos++
|
|
204
|
-
continue
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (
|
|
208
|
-
char === '.' &&
|
|
209
|
-
this.source[this.pos + 1] === '.' &&
|
|
210
|
-
this.source[this.pos + 2] === '.'
|
|
211
|
-
) {
|
|
212
|
-
this.pushToken(TokenType.SPREAD, '...')
|
|
213
|
-
this.pos += 3
|
|
214
|
-
continue
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (char in SINGLE_CHAR_TOKENS) {
|
|
218
|
-
this.pushToken(SINGLE_CHAR_TOKENS[char], char)
|
|
219
|
-
this.pos++
|
|
220
|
-
continue
|
|
221
|
-
}
|
|
222
|
-
|
|
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
247
|
throw new Error(`Unexpected character "${char}" at line ${this.line}`)
|
|
244
248
|
}
|
|
245
249
|
|
|
246
|
-
this.
|
|
250
|
+
this.tokenStart = this.pos
|
|
251
|
+
this.pushToken(T.EOF, null)
|
|
247
252
|
return this.tokens
|
|
248
253
|
}
|
|
249
254
|
|
|
@@ -289,12 +294,60 @@ export class Lexer {
|
|
|
289
294
|
}
|
|
290
295
|
}
|
|
291
296
|
|
|
297
|
+
// True when a "/" (or a ".5") at the current position should start a
|
|
298
|
+
// regex literal (or number) based on the previous token.
|
|
299
|
+
regexAllowed() {
|
|
300
|
+
const prev = this.tokens[this.tokens.length - 1]
|
|
301
|
+
if (!prev) return true
|
|
302
|
+
// A keyword right after "." is a property name (`obj.in / 2`),
|
|
303
|
+
// so the "/" is division, not the start of a regex.
|
|
304
|
+
const before = this.tokens[this.tokens.length - 2]
|
|
305
|
+
if (before && (before.type === T.DOT || before.type === T.QUESTION_DOT)) {
|
|
306
|
+
return false
|
|
307
|
+
}
|
|
308
|
+
// `yield /re/` -- yield is a plain identifier token.
|
|
309
|
+
if (prev.type === T.IDENTIFIER && prev.value === 'yield') return true
|
|
310
|
+
return REGEX_PRECEDERS.has(prev.type)
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
readRegex() {
|
|
314
|
+
const src = this.source
|
|
315
|
+
const startLine = this.line
|
|
316
|
+
this.pos++ // opening "/"
|
|
317
|
+
const bodyStart = this.pos
|
|
318
|
+
let inClass = false
|
|
319
|
+
while (this.pos < src.length) {
|
|
320
|
+
const c = src[this.pos]
|
|
321
|
+
if (c === '\n') break
|
|
322
|
+
if (c === '\\') {
|
|
323
|
+
this.pos += 2
|
|
324
|
+
continue
|
|
325
|
+
}
|
|
326
|
+
if (c === '[') inClass = true
|
|
327
|
+
else if (c === ']') inClass = false
|
|
328
|
+
else if (c === '/' && !inClass) break
|
|
329
|
+
this.pos++
|
|
330
|
+
}
|
|
331
|
+
if (src[this.pos] !== '/') {
|
|
332
|
+
throw new Error(`Unterminated regular expression at line ${startLine}`)
|
|
333
|
+
}
|
|
334
|
+
const pattern = src.slice(bodyStart, this.pos)
|
|
335
|
+
this.pos++ // closing "/"
|
|
336
|
+
const flagStart = this.pos
|
|
337
|
+
while (/[a-z]/.test(src[this.pos] ?? '')) this.pos++
|
|
338
|
+
const flags = src.slice(flagStart, this.pos)
|
|
339
|
+
this.pushToken(T.REGEX, { pattern, flags }, startLine)
|
|
340
|
+
}
|
|
341
|
+
|
|
292
342
|
// Called with this.pos on the backslash. Consumes the escape
|
|
293
|
-
// sequence and returns the character(s) it stands for.
|
|
294
|
-
//
|
|
343
|
+
// sequence and returns the character(s) it stands for. Supports the
|
|
344
|
+
// usual \n \t \r \b \f \v \0, hex \xNN, unicode \uNNNN and \u{...},
|
|
345
|
+
// and line continuations. Unknown escapes (\\, \', \", \`, \$) just
|
|
346
|
+
// yield the escaped character.
|
|
295
347
|
readEscape() {
|
|
348
|
+
const src = this.source
|
|
296
349
|
this.pos++ // skip '\'
|
|
297
|
-
const c =
|
|
350
|
+
const c = src[this.pos]
|
|
298
351
|
if (c === undefined) return ''
|
|
299
352
|
this.pos++
|
|
300
353
|
switch (c) {
|
|
@@ -312,6 +365,41 @@ export class Lexer {
|
|
|
312
365
|
return '\v'
|
|
313
366
|
case '0':
|
|
314
367
|
return '\0'
|
|
368
|
+
case 'x': {
|
|
369
|
+
const hex = src.slice(this.pos, this.pos + 2)
|
|
370
|
+
if (/^[0-9a-fA-F]{2}$/.test(hex)) {
|
|
371
|
+
this.pos += 2
|
|
372
|
+
return String.fromCharCode(parseInt(hex, 16))
|
|
373
|
+
}
|
|
374
|
+
return 'x'
|
|
375
|
+
}
|
|
376
|
+
case 'u': {
|
|
377
|
+
if (src[this.pos] === '{') {
|
|
378
|
+
const close = src.indexOf('}', this.pos)
|
|
379
|
+
const hex = close === -1 ? '' : src.slice(this.pos + 1, close)
|
|
380
|
+
if (/^[0-9a-fA-F]+$/.test(hex)) {
|
|
381
|
+
const code = parseInt(hex, 16)
|
|
382
|
+
if (code > 0x10ffff) {
|
|
383
|
+
throw new Error(
|
|
384
|
+
`Invalid unicode escape \\u{${hex}} at line ${this.line}`,
|
|
385
|
+
)
|
|
386
|
+
}
|
|
387
|
+
this.pos = close + 1
|
|
388
|
+
return String.fromCodePoint(code)
|
|
389
|
+
}
|
|
390
|
+
return 'u'
|
|
391
|
+
}
|
|
392
|
+
const hex = src.slice(this.pos, this.pos + 4)
|
|
393
|
+
if (/^[0-9a-fA-F]{4}$/.test(hex)) {
|
|
394
|
+
this.pos += 4
|
|
395
|
+
return String.fromCharCode(parseInt(hex, 16))
|
|
396
|
+
}
|
|
397
|
+
return 'u'
|
|
398
|
+
}
|
|
399
|
+
case '\r':
|
|
400
|
+
if (src[this.pos] === '\n') this.pos++
|
|
401
|
+
this.line++ // line continuation
|
|
402
|
+
return ''
|
|
315
403
|
case '\n':
|
|
316
404
|
this.line++ // line continuation
|
|
317
405
|
return ''
|
|
@@ -340,36 +428,39 @@ export class Lexer {
|
|
|
340
428
|
throw new Error(`Unterminated string starting at line ${startLine}`)
|
|
341
429
|
}
|
|
342
430
|
this.pos++ // skip closing quote
|
|
343
|
-
this.pushToken(
|
|
431
|
+
this.pushToken(T.STRING, value, startLine)
|
|
344
432
|
}
|
|
345
433
|
|
|
346
434
|
// Scans a backtick template literal into a flat list of parts --
|
|
347
|
-
// { type: "text", value } for literal text, { type: "expr",
|
|
348
|
-
// for each ${...} span -- rather than re-entering full
|
|
349
|
-
// mid-string.
|
|
350
|
-
//
|
|
351
|
-
//
|
|
352
|
-
//
|
|
435
|
+
// { type: "text", value, raw } for literal text, { type: "expr",
|
|
436
|
+
// source } for each ${...} span -- rather than re-entering full
|
|
437
|
+
// tokenization mid-string. `value` is the cooked text (escapes
|
|
438
|
+
// resolved); `raw` is the text exactly as written, which tagged
|
|
439
|
+
// templates need for String.raw. Each expr span's raw source gets
|
|
440
|
+
// parsed later via parseExpressionString.
|
|
353
441
|
readTemplateLiteral() {
|
|
442
|
+
const src = this.source
|
|
354
443
|
const startLine = this.line
|
|
355
444
|
this.pos++ // skip opening backtick
|
|
356
445
|
const parts = []
|
|
357
446
|
let textBuf = ''
|
|
447
|
+
let rawBuf = ''
|
|
358
448
|
|
|
359
449
|
const flushText = () => {
|
|
360
|
-
if (textBuf.length > 0) {
|
|
361
|
-
parts.push({ type: 'text', value: textBuf })
|
|
450
|
+
if (textBuf.length > 0 || rawBuf.length > 0) {
|
|
451
|
+
parts.push({ type: 'text', value: textBuf, raw: rawBuf })
|
|
362
452
|
textBuf = ''
|
|
453
|
+
rawBuf = ''
|
|
363
454
|
}
|
|
364
455
|
}
|
|
365
456
|
|
|
366
457
|
while (true) {
|
|
367
|
-
if (this.pos >=
|
|
458
|
+
if (this.pos >= src.length) {
|
|
368
459
|
throw new Error(
|
|
369
460
|
`Unterminated template literal starting at line ${startLine}`,
|
|
370
461
|
)
|
|
371
462
|
}
|
|
372
|
-
const char =
|
|
463
|
+
const char = src[this.pos]
|
|
373
464
|
|
|
374
465
|
if (char === '`') {
|
|
375
466
|
this.pos++ // skip closing backtick
|
|
@@ -377,78 +468,182 @@ export class Lexer {
|
|
|
377
468
|
}
|
|
378
469
|
|
|
379
470
|
if (char === '\\') {
|
|
471
|
+
const escStart = this.pos
|
|
380
472
|
textBuf += this.readEscape()
|
|
473
|
+
rawBuf += src.slice(escStart, this.pos)
|
|
381
474
|
continue
|
|
382
475
|
}
|
|
383
476
|
|
|
384
|
-
if (char === '$' &&
|
|
477
|
+
if (char === '$' && src[this.pos + 1] === '{') {
|
|
385
478
|
flushText()
|
|
386
479
|
this.pos += 2 // skip "${"
|
|
387
480
|
const exprStart = this.pos
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
if (this.source[this.pos] === '{') depth++
|
|
391
|
-
else if (this.source[this.pos] === '}') depth--
|
|
392
|
-
if (depth > 0) this.pos++
|
|
393
|
-
}
|
|
394
|
-
if (depth !== 0) {
|
|
481
|
+
const exprEnd = this.findTemplateExprEnd(exprStart)
|
|
482
|
+
if (exprEnd === -1) {
|
|
395
483
|
throw new Error(
|
|
396
484
|
`Unterminated \${...} in template literal starting at line ${startLine}`,
|
|
397
485
|
)
|
|
398
486
|
}
|
|
399
|
-
const exprSource =
|
|
400
|
-
|
|
487
|
+
const exprSource = src.slice(exprStart, exprEnd)
|
|
488
|
+
// Newlines inside the span still count toward line numbers.
|
|
489
|
+
this.line += (exprSource.match(/\n/g) ?? []).length
|
|
490
|
+
this.pos = exprEnd + 1 // skip closing "}"
|
|
401
491
|
parts.push({ type: 'expr', source: exprSource })
|
|
402
492
|
continue
|
|
403
493
|
}
|
|
404
494
|
|
|
405
495
|
if (char === '\n') this.line++
|
|
406
496
|
textBuf += char
|
|
497
|
+
rawBuf += char
|
|
407
498
|
this.pos++
|
|
408
499
|
}
|
|
409
500
|
|
|
410
501
|
flushText()
|
|
411
|
-
this.pushToken(
|
|
502
|
+
this.pushToken(T.TEMPLATE_LITERAL, parts, startLine)
|
|
412
503
|
}
|
|
413
504
|
|
|
505
|
+
// Index of the "}" that closes a ${ ... } span starting at `from`.
|
|
506
|
+
// Braces inside strings and nested template literals are skipped,
|
|
507
|
+
// so ${ "}" } and ${ `a${b}` } work.
|
|
508
|
+
findTemplateExprEnd(from) {
|
|
509
|
+
const src = this.source
|
|
510
|
+
let depth = 1
|
|
511
|
+
let i = from
|
|
512
|
+
while (i < src.length) {
|
|
513
|
+
const c = src[i]
|
|
514
|
+
if (c === '"' || c === "'") {
|
|
515
|
+
i++
|
|
516
|
+
while (i < src.length && src[i] !== c) {
|
|
517
|
+
if (src[i] === '\\') i++
|
|
518
|
+
i++
|
|
519
|
+
}
|
|
520
|
+
i++
|
|
521
|
+
continue
|
|
522
|
+
}
|
|
523
|
+
if (c === '`') {
|
|
524
|
+
i = this.skipNestedTemplate(i)
|
|
525
|
+
if (i === -1) return -1
|
|
526
|
+
continue
|
|
527
|
+
}
|
|
528
|
+
if (c === '{') {
|
|
529
|
+
depth++
|
|
530
|
+
} else if (c === '}') {
|
|
531
|
+
depth--
|
|
532
|
+
if (depth === 0) return i
|
|
533
|
+
}
|
|
534
|
+
i++
|
|
535
|
+
}
|
|
536
|
+
return -1
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// `i` points at an opening backtick. Returns the index just past
|
|
540
|
+
// the matching closing backtick, or -1 if it never closes.
|
|
541
|
+
skipNestedTemplate(i) {
|
|
542
|
+
const src = this.source
|
|
543
|
+
i++
|
|
544
|
+
while (i < src.length) {
|
|
545
|
+
const c = src[i]
|
|
546
|
+
if (c === '\\') {
|
|
547
|
+
i += 2
|
|
548
|
+
continue
|
|
549
|
+
}
|
|
550
|
+
if (c === '`') return i + 1
|
|
551
|
+
if (c === '$' && src[i + 1] === '{') {
|
|
552
|
+
const end = this.findTemplateExprEnd(i + 2)
|
|
553
|
+
if (end === -1) return -1
|
|
554
|
+
i = end + 1
|
|
555
|
+
continue
|
|
556
|
+
}
|
|
557
|
+
i++
|
|
558
|
+
}
|
|
559
|
+
return -1
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// Numbers: 42, 3.14, .5, 1_000, 1e6, 2.5e-3, 0xFF, 0b101, 0o17,
|
|
563
|
+
// BigInt (10n, 0xFFn) and durations (500ms, 2s, 5m, 1h, 7d). A "."
|
|
564
|
+
// is only part of the number when a digit follows, so
|
|
565
|
+
// `1.5.toFixed(1)` still works.
|
|
414
566
|
readNumber() {
|
|
567
|
+
const src = this.source
|
|
415
568
|
const start = this.pos
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
569
|
+
const isDigit = (c) => /[0-9_]/.test(c ?? '')
|
|
570
|
+
const isIdentChar = (c) => /[A-Za-z0-9_$]/.test(c ?? '')
|
|
571
|
+
const bigIntSuffix = () =>
|
|
572
|
+
src[this.pos] === 'n' && !isIdentChar(src[this.pos + 1])
|
|
573
|
+
|
|
574
|
+
// 0xFF, 0b101, 0o17
|
|
575
|
+
if (src[this.pos] === '0' && /[xXbBoO]/.test(src[this.pos + 1] ?? '')) {
|
|
576
|
+
this.pos += 2
|
|
577
|
+
while (/[0-9a-fA-F_]/.test(src[this.pos] ?? '')) this.pos++
|
|
578
|
+
const text = src.slice(start, this.pos).replace(/_/g, '')
|
|
579
|
+
if (bigIntSuffix()) {
|
|
580
|
+
this.pos++
|
|
581
|
+
this.pushToken(T.BIGINT, text)
|
|
582
|
+
return
|
|
583
|
+
}
|
|
584
|
+
this.pushToken(T.NUMBER, Number(text))
|
|
585
|
+
return
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
let isInteger = true
|
|
589
|
+
while (isDigit(src[this.pos])) this.pos++
|
|
590
|
+
if (src[this.pos] === '.' && /[0-9]/.test(src[this.pos + 1] ?? '')) {
|
|
591
|
+
isInteger = false
|
|
420
592
|
this.pos++
|
|
593
|
+
while (isDigit(src[this.pos])) this.pos++
|
|
594
|
+
}
|
|
595
|
+
// exponent: 1e6, 2.5e-3
|
|
596
|
+
if (/^[eE][+-]?[0-9]/.test(src.slice(this.pos, this.pos + 3))) {
|
|
597
|
+
isInteger = false
|
|
598
|
+
this.pos += 2
|
|
599
|
+
while (isDigit(src[this.pos])) this.pos++
|
|
421
600
|
}
|
|
422
|
-
const text =
|
|
601
|
+
const text = src.slice(start, this.pos).replace(/_/g, '')
|
|
423
602
|
|
|
424
|
-
|
|
603
|
+
if (isInteger && bigIntSuffix()) {
|
|
604
|
+
this.pos++
|
|
605
|
+
this.pushToken(T.BIGINT, text)
|
|
606
|
+
return
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
const unitMatch = /^(ms|s|m|h|d)/.exec(src.slice(this.pos))
|
|
425
610
|
if (
|
|
426
611
|
unitMatch &&
|
|
427
|
-
!/[A-Za-z0-9_$]/.test(
|
|
612
|
+
!/[A-Za-z0-9_$]/.test(src[this.pos + unitMatch[0].length] ?? '')
|
|
428
613
|
) {
|
|
429
614
|
const unit = unitMatch[0]
|
|
430
615
|
this.pos += unit.length
|
|
431
|
-
this.pushToken(
|
|
616
|
+
this.pushToken(T.DURATION, `${text}${unit}`)
|
|
432
617
|
return
|
|
433
618
|
}
|
|
434
619
|
|
|
435
|
-
this.pushToken(
|
|
620
|
+
this.pushToken(T.NUMBER, Number(text))
|
|
436
621
|
}
|
|
437
622
|
|
|
438
623
|
readIdentifierOrKeyword() {
|
|
624
|
+
const src = this.source
|
|
439
625
|
const start = this.pos
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
this.pos
|
|
626
|
+
if (src[this.pos] === '#') this.pos++ // private name: #field
|
|
627
|
+
while (this.pos < src.length) {
|
|
628
|
+
const ch = codePointStringAt(src, this.pos)
|
|
629
|
+
if (!ID_PART.test(ch)) break
|
|
630
|
+
this.pos += ch.length
|
|
445
631
|
}
|
|
446
|
-
const text =
|
|
447
|
-
|
|
632
|
+
const text = src.slice(start, this.pos)
|
|
633
|
+
// Own-property lookup only: a plain `KEYWORDS[text]` also finds
|
|
634
|
+
// inherited names, so `toString`, `constructor` and
|
|
635
|
+
// `hasOwnProperty` used to come out as function "token types".
|
|
636
|
+
const type =
|
|
637
|
+
!text.startsWith('#') && Object.hasOwn(KEYWORDS, text)
|
|
638
|
+
? KEYWORDS[text]
|
|
639
|
+
: T.IDENTIFIER
|
|
448
640
|
this.pushToken(type, text)
|
|
449
641
|
}
|
|
450
642
|
|
|
643
|
+
// `start` is the source offset where the current token began; the
|
|
644
|
+
// Parser uses it to tell whether two tokens were written adjacent
|
|
645
|
+
// (">" ">" is a shift, "> >" is not).
|
|
451
646
|
pushToken(type, value, line = this.line) {
|
|
452
|
-
this.tokens.push({ type, value, line })
|
|
647
|
+
this.tokens.push({ type, value, line, start: this.tokenStart })
|
|
453
648
|
}
|
|
454
649
|
}
|