@yannosay/sinth 1.8.0 → 1.8.2
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 +660 -660
- package/README.md +548 -548
- package/package.json +45 -45
- package/src/cli.ts +338 -338
- package/src/core/builtins.ts +116 -116
- package/src/core/compiler.ts +1706 -1706
- package/src/core/expr.ts +88 -88
- package/src/core/head-builder.ts +86 -86
- package/src/core/lexer.ts +275 -275
- package/src/core/parser.ts +1523 -1523
- package/src/core/runtime/custom-element.ts +134 -134
- package/src/core/runtime/functions.ts +46 -46
- package/src/core/runtime/helpers.ts +332 -332
- package/src/core/runtime/render.ts +136 -136
- package/src/core/style-processor.ts +299 -299
- package/src/core/types.ts +192 -192
- package/src/resolver.ts +149 -149
- package/src/server.ts +128 -128
- package/src/utils.ts +76 -76
- package/tsconfig.json +26 -26
package/src/core/lexer.ts
CHANGED
|
@@ -1,276 +1,276 @@
|
|
|
1
|
-
import { TT, Token, Loc, SinthError } from "./types.ts";
|
|
2
|
-
|
|
3
|
-
// lexer
|
|
4
|
-
const KEYWORDS: Record<string, TT> = {
|
|
5
|
-
"import": TT.KW_IMPORT,
|
|
6
|
-
"as": TT.KW_AS,
|
|
7
|
-
"css": TT.KW_CSS,
|
|
8
|
-
"component": TT.KW_COMPONENT,
|
|
9
|
-
"style": TT.KW_STYLE,
|
|
10
|
-
"script": TT.KW_SCRIPT,
|
|
11
|
-
"global": TT.KW_GLOBAL,
|
|
12
|
-
"page": TT.KW_PAGE,
|
|
13
|
-
"lang": TT.KW_LANG,
|
|
14
|
-
"true": TT.BOOL_TRUE,
|
|
15
|
-
"false": TT.BOOL_FALSE,
|
|
16
|
-
"null": TT.NULL_LIT,
|
|
17
|
-
"custom-element": TT.KW_CUSTOM_EL,
|
|
18
|
-
"custom": TT.KW_CUSTOM,
|
|
19
|
-
"if": TT.KW_IF,
|
|
20
|
-
"else": TT.KW_ELSE,
|
|
21
|
-
"elif": TT.KW_ELSE,
|
|
22
|
-
"for": TT.KW_FOR,
|
|
23
|
-
"in": TT.KW_IN,
|
|
24
|
-
"remove": TT.KW_REMOVE,
|
|
25
|
-
"function": TT.KW_FUNCTION,
|
|
26
|
-
"return": TT.KW_RETURN,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export class Lexer {
|
|
30
|
-
private pos = 0;
|
|
31
|
-
private line = 1;
|
|
32
|
-
private col = 1;
|
|
33
|
-
private rawBlockPending = false;
|
|
34
|
-
|
|
35
|
-
constructor(public readonly src: string, private readonly file: string) {}
|
|
36
|
-
|
|
37
|
-
tokenize(): Token[] {
|
|
38
|
-
const tokens: Token[] = [];
|
|
39
|
-
while (true) {
|
|
40
|
-
this.skipWS();
|
|
41
|
-
if (this.pos >= this.src.length) break;
|
|
42
|
-
|
|
43
|
-
const loc = this.loc();
|
|
44
|
-
const ch = this.src[this.pos];
|
|
45
|
-
|
|
46
|
-
if (ch === "(") { tokens.push(this.single(TT.LPAREN, loc)); continue; }
|
|
47
|
-
if (ch === ")") { tokens.push(this.single(TT.RPAREN, loc)); continue; }
|
|
48
|
-
if (ch === "}") { tokens.push(this.single(TT.RBRACE, loc)); continue; }
|
|
49
|
-
if (ch === ",") { tokens.push(this.single(TT.COMMA, loc)); continue; }
|
|
50
|
-
if (ch === ".") { tokens.push(this.single(TT.DOT, loc)); continue; }
|
|
51
|
-
if (ch === ":") { tokens.push(this.single(TT.COLON, loc)); continue; }
|
|
52
|
-
if (ch === "[") { tokens.push(this.single(TT.LBRACKET, loc)); continue; }
|
|
53
|
-
if (ch === "]") { tokens.push(this.single(TT.RBRACKET, loc)); continue; }
|
|
54
|
-
|
|
55
|
-
// two-character operators ->
|
|
56
|
-
if (ch === "=" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_EQEQ, value: "==", loc }); continue; }
|
|
57
|
-
if (ch === "!" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_NEQ, value: "!=", loc }); continue; }
|
|
58
|
-
if (ch === "!") { tokens.push(this.single(TT.OP_NOT, loc)); continue; }
|
|
59
|
-
if (ch === ">" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_GTEQ, value: ">=", loc }); continue; }
|
|
60
|
-
if (ch === "<" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_LTEQ, value: "<=", loc }); continue; }
|
|
61
|
-
|
|
62
|
-
if (ch === "=") { tokens.push(this.single(TT.EQUALS, loc)); continue; }
|
|
63
|
-
if (ch === "+") { tokens.push(this.single(TT.OP_PLUS, loc)); continue; }
|
|
64
|
-
if (ch === "*") { tokens.push(this.single(TT.OP_STAR, loc)); continue; }
|
|
65
|
-
if (ch === "/") { tokens.push(this.single(TT.OP_SLASH, loc)); continue; }
|
|
66
|
-
if (ch === "%") { tokens.push(this.single(TT.OP_MOD, loc)); continue; }
|
|
67
|
-
if (ch === ";") { tokens.push(this.single(TT.OP_SEMI, loc)); continue; }
|
|
68
|
-
if (ch === "<") { tokens.push(this.single(TT.OP_LT, loc)); continue; }
|
|
69
|
-
if (ch === ">") { tokens.push(this.single(TT.OP_GT, loc)); continue; }
|
|
70
|
-
if (ch === "-" && this.src[this.pos + 1] === ">") {
|
|
71
|
-
this.adv(); this.adv();
|
|
72
|
-
tokens.push({ type: TT.OP_ARROW, value: "->", loc });
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
if (ch === "-" && !this.isDigit(this.src[this.pos + 1] ?? "")) {
|
|
76
|
-
tokens.push(this.single(TT.OP_MINUS, loc)); continue;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (ch === "{") {
|
|
80
|
-
if (this.rawBlockPending) {
|
|
81
|
-
this.rawBlockPending = false;
|
|
82
|
-
tokens.push(this.readRawBlock(loc));
|
|
83
|
-
} else {
|
|
84
|
-
tokens.push(this.single(TT.LBRACE, loc));
|
|
85
|
-
}
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (ch === '"' || ch === "'") { tokens.push(this.readString(loc)); continue; }
|
|
90
|
-
|
|
91
|
-
if (this.isDigit(ch) || (ch === "-" && this.isDigit(this.src[this.pos + 1] ?? ""))) {
|
|
92
|
-
tokens.push(this.readNumber(loc)); continue;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (this.isIdentStart(ch)) {
|
|
96
|
-
const tok = this.readIdent(loc);
|
|
97
|
-
if (
|
|
98
|
-
tok.type === TT.KW_STYLE ||
|
|
99
|
-
tok.type === TT.KW_SCRIPT ||
|
|
100
|
-
tok.type === TT.KW_COMPONENT ||
|
|
101
|
-
tok.type === TT.KW_CUSTOM_EL ||
|
|
102
|
-
tok.type === TT.KW_CUSTOM
|
|
103
|
-
) {
|
|
104
|
-
this.rawBlockPending = true;
|
|
105
|
-
}
|
|
106
|
-
tokens.push(tok);
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
throw new SinthError(`Unexpected character '${ch}'`, loc);
|
|
111
|
-
}
|
|
112
|
-
tokens.push({ type: TT.EOF, value: "", loc: this.loc() });
|
|
113
|
-
return tokens;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
private skipWS(): void {
|
|
117
|
-
while (this.pos < this.src.length) {
|
|
118
|
-
const ch = this.src[this.pos];
|
|
119
|
-
if (ch === " " || ch === "\t" || ch === "\r" || ch === "\n") { this.adv(); continue; }
|
|
120
|
-
|
|
121
|
-
// sinth comments: -- line or --[ nestable block for when doing longgerrr comments]--
|
|
122
|
-
if (ch === "-" && this.src[this.pos + 1] === "-") {
|
|
123
|
-
if (this.src[this.pos + 2] === "[") {
|
|
124
|
-
this.adv(); this.adv(); this.adv();
|
|
125
|
-
let depth = 1;
|
|
126
|
-
while (this.pos < this.src.length && depth > 0) {
|
|
127
|
-
if (this.src[this.pos] === "-" && this.src[this.pos + 1] === "-" && this.src[this.pos + 2] === "[") {
|
|
128
|
-
depth++; this.adv(); this.adv(); this.adv();
|
|
129
|
-
} else if (this.src[this.pos] === "]" && this.src[this.pos + 1] === "-" && this.src[this.pos + 2] === "-") {
|
|
130
|
-
depth--; this.adv(); this.adv(); this.adv();
|
|
131
|
-
} else {
|
|
132
|
-
this.adv();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (depth > 0) throw new SinthError("Unterminated block comment --[ ... ]--", this.loc());
|
|
136
|
-
} else {
|
|
137
|
-
while (this.pos < this.src.length && this.src[this.pos] !== "\n") this.adv();
|
|
138
|
-
}
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
break;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private readRawBlock(loc: Loc): Token {
|
|
146
|
-
this.adv(); // consume '{'
|
|
147
|
-
const start = this.pos;
|
|
148
|
-
let depth = 1;
|
|
149
|
-
|
|
150
|
-
while (this.pos < this.src.length && depth > 0) {
|
|
151
|
-
const ch = this.src[this.pos];
|
|
152
|
-
if (ch === '"' || ch === "'" || ch === "`") { this.skipRawString(ch); continue; }
|
|
153
|
-
if (ch === "/" && this.src[this.pos + 1] === "/") {
|
|
154
|
-
while (this.pos < this.src.length && this.src[this.pos] !== "\n") this.adv();
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
if (ch === "/" && this.src[this.pos + 1] === "*") {
|
|
158
|
-
this.adv(); this.adv();
|
|
159
|
-
while (this.pos < this.src.length - 1 &&
|
|
160
|
-
!(this.src[this.pos] === "*" && this.src[this.pos + 1] === "/")) { this.adv(); }
|
|
161
|
-
this.adv(); this.adv();
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
if (ch === "{") { depth++; this.adv(); }
|
|
165
|
-
else if (ch === "}") {
|
|
166
|
-
depth--;
|
|
167
|
-
if (depth > 0) this.adv();
|
|
168
|
-
} else {
|
|
169
|
-
this.adv();
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const content = this.src.substring(start, this.pos);
|
|
174
|
-
this.adv(); // consume closing '}'
|
|
175
|
-
return { type: TT.RAW_BLOCK, value: content, loc };
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
private skipRawString(quote: string): void {
|
|
179
|
-
this.adv();
|
|
180
|
-
while (this.pos < this.src.length) {
|
|
181
|
-
const c = this.src[this.pos];
|
|
182
|
-
if (c === "\\") { this.adv(); this.adv(); continue; }
|
|
183
|
-
if (c === quote) { this.adv(); return; }
|
|
184
|
-
if (quote === "`" && c === "$" && this.src[this.pos + 1] === "{") {
|
|
185
|
-
this.adv(); this.adv();
|
|
186
|
-
let d = 1;
|
|
187
|
-
while (this.pos < this.src.length && d > 0) {
|
|
188
|
-
if (this.src[this.pos] === "{") d++;
|
|
189
|
-
else if (this.src[this.pos] === "}") d--;
|
|
190
|
-
this.adv();
|
|
191
|
-
}
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
this.adv();
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
private readString(loc: Loc): Token {
|
|
199
|
-
const quote = this.src[this.pos];
|
|
200
|
-
this.adv();
|
|
201
|
-
let result = "";
|
|
202
|
-
while (this.pos < this.src.length && this.src[this.pos] !== quote) {
|
|
203
|
-
if (this.src[this.pos] === "\\") {
|
|
204
|
-
this.adv();
|
|
205
|
-
const e = this.src[this.pos];
|
|
206
|
-
switch (e) {
|
|
207
|
-
case "\\": result += "\\"; break;
|
|
208
|
-
case '"': result += '"'; break;
|
|
209
|
-
case "'": result += "'"; break;
|
|
210
|
-
case "n": result += "\n"; break;
|
|
211
|
-
case "r": result += "\r"; break;
|
|
212
|
-
case "t": result += "\t"; break;
|
|
213
|
-
case "$": result += "$"; break;
|
|
214
|
-
case "{": result += "{"; break;
|
|
215
|
-
case "u": {
|
|
216
|
-
this.adv();
|
|
217
|
-
if (this.src[this.pos] === "{") {
|
|
218
|
-
this.adv();
|
|
219
|
-
let hex = "";
|
|
220
|
-
while (this.src[this.pos] !== "}") { hex += this.src[this.pos]; this.adv(); }
|
|
221
|
-
result += String.fromCodePoint(parseInt(hex, 16));
|
|
222
|
-
} else {
|
|
223
|
-
const hex = this.src.substring(this.pos, this.pos + 4);
|
|
224
|
-
result += String.fromCharCode(parseInt(hex, 16));
|
|
225
|
-
this.pos += 3; this.col += 3;
|
|
226
|
-
}
|
|
227
|
-
break;
|
|
228
|
-
}
|
|
229
|
-
default: throw new SinthError(`Unknown escape sequence \\${e}`, loc);
|
|
230
|
-
}
|
|
231
|
-
this.adv();
|
|
232
|
-
} else {
|
|
233
|
-
result += this.src[this.pos];
|
|
234
|
-
this.adv();
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
if (this.pos >= this.src.length) throw new SinthError("Unterminated string literal", loc);
|
|
238
|
-
this.adv();
|
|
239
|
-
return { type: TT.STRING, value: result, loc };
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
private readNumber(loc: Loc): Token {
|
|
243
|
-
let s = "";
|
|
244
|
-
if (this.src[this.pos] === "-") { s += "-"; this.adv(); }
|
|
245
|
-
while (this.isDigit(this.src[this.pos] ?? "")) { s += this.src[this.pos]; this.adv(); }
|
|
246
|
-
if (this.src[this.pos] === "." && this.isDigit(this.src[this.pos + 1] ?? "")) {
|
|
247
|
-
s += "."; this.adv();
|
|
248
|
-
while (this.isDigit(this.src[this.pos] ?? "")) { s += this.src[this.pos]; this.adv(); }
|
|
249
|
-
}
|
|
250
|
-
return { type: TT.NUMBER, value: s, loc };
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
private readIdent(loc: Loc): Token {
|
|
254
|
-
let s = "";
|
|
255
|
-
while (this.pos < this.src.length && this.isIdentCont(this.src[this.pos])) {
|
|
256
|
-
s += this.src[this.pos]; this.adv();
|
|
257
|
-
}
|
|
258
|
-
const kw = KEYWORDS[s];
|
|
259
|
-
return { type: kw !== undefined ? kw : TT.IDENT, value: s, loc };
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
private adv(): void {
|
|
263
|
-
const ch = this.src[this.pos++];
|
|
264
|
-
if (ch === "\n") { this.line++; this.col = 1; } else { this.col++; }
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
private single(type: TT, loc: Loc): Token {
|
|
268
|
-
this.adv();
|
|
269
|
-
return { type, value: this.src[this.pos - 1], loc };
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
private loc(): Loc { return { file: this.file, line: this.line, col: this.col }; }
|
|
273
|
-
private isDigit(c: string) { return c >= "0" && c <= "9"; }
|
|
274
|
-
private isIdentStart(c: string) { return /[a-zA-Z_$\u00C0-\uFFFF]/.test(c); }
|
|
275
|
-
private isIdentCont(c: string) { return /[a-zA-Z0-9_$\-\u00C0-\uFFFF]/.test(c); }
|
|
1
|
+
import { TT, Token, Loc, SinthError } from "./types.ts";
|
|
2
|
+
|
|
3
|
+
// lexer
|
|
4
|
+
const KEYWORDS: Record<string, TT> = {
|
|
5
|
+
"import": TT.KW_IMPORT,
|
|
6
|
+
"as": TT.KW_AS,
|
|
7
|
+
"css": TT.KW_CSS,
|
|
8
|
+
"component": TT.KW_COMPONENT,
|
|
9
|
+
"style": TT.KW_STYLE,
|
|
10
|
+
"script": TT.KW_SCRIPT,
|
|
11
|
+
"global": TT.KW_GLOBAL,
|
|
12
|
+
"page": TT.KW_PAGE,
|
|
13
|
+
"lang": TT.KW_LANG,
|
|
14
|
+
"true": TT.BOOL_TRUE,
|
|
15
|
+
"false": TT.BOOL_FALSE,
|
|
16
|
+
"null": TT.NULL_LIT,
|
|
17
|
+
"custom-element": TT.KW_CUSTOM_EL,
|
|
18
|
+
"custom": TT.KW_CUSTOM,
|
|
19
|
+
"if": TT.KW_IF,
|
|
20
|
+
"else": TT.KW_ELSE,
|
|
21
|
+
"elif": TT.KW_ELSE,
|
|
22
|
+
"for": TT.KW_FOR,
|
|
23
|
+
"in": TT.KW_IN,
|
|
24
|
+
"remove": TT.KW_REMOVE,
|
|
25
|
+
"function": TT.KW_FUNCTION,
|
|
26
|
+
"return": TT.KW_RETURN,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export class Lexer {
|
|
30
|
+
private pos = 0;
|
|
31
|
+
private line = 1;
|
|
32
|
+
private col = 1;
|
|
33
|
+
private rawBlockPending = false;
|
|
34
|
+
|
|
35
|
+
constructor(public readonly src: string, private readonly file: string) {}
|
|
36
|
+
|
|
37
|
+
tokenize(): Token[] {
|
|
38
|
+
const tokens: Token[] = [];
|
|
39
|
+
while (true) {
|
|
40
|
+
this.skipWS();
|
|
41
|
+
if (this.pos >= this.src.length) break;
|
|
42
|
+
|
|
43
|
+
const loc = this.loc();
|
|
44
|
+
const ch = this.src[this.pos];
|
|
45
|
+
|
|
46
|
+
if (ch === "(") { tokens.push(this.single(TT.LPAREN, loc)); continue; }
|
|
47
|
+
if (ch === ")") { tokens.push(this.single(TT.RPAREN, loc)); continue; }
|
|
48
|
+
if (ch === "}") { tokens.push(this.single(TT.RBRACE, loc)); continue; }
|
|
49
|
+
if (ch === ",") { tokens.push(this.single(TT.COMMA, loc)); continue; }
|
|
50
|
+
if (ch === ".") { tokens.push(this.single(TT.DOT, loc)); continue; }
|
|
51
|
+
if (ch === ":") { tokens.push(this.single(TT.COLON, loc)); continue; }
|
|
52
|
+
if (ch === "[") { tokens.push(this.single(TT.LBRACKET, loc)); continue; }
|
|
53
|
+
if (ch === "]") { tokens.push(this.single(TT.RBRACKET, loc)); continue; }
|
|
54
|
+
|
|
55
|
+
// two-character operators ->
|
|
56
|
+
if (ch === "=" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_EQEQ, value: "==", loc }); continue; }
|
|
57
|
+
if (ch === "!" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_NEQ, value: "!=", loc }); continue; }
|
|
58
|
+
if (ch === "!") { tokens.push(this.single(TT.OP_NOT, loc)); continue; }
|
|
59
|
+
if (ch === ">" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_GTEQ, value: ">=", loc }); continue; }
|
|
60
|
+
if (ch === "<" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_LTEQ, value: "<=", loc }); continue; }
|
|
61
|
+
|
|
62
|
+
if (ch === "=") { tokens.push(this.single(TT.EQUALS, loc)); continue; }
|
|
63
|
+
if (ch === "+") { tokens.push(this.single(TT.OP_PLUS, loc)); continue; }
|
|
64
|
+
if (ch === "*") { tokens.push(this.single(TT.OP_STAR, loc)); continue; }
|
|
65
|
+
if (ch === "/") { tokens.push(this.single(TT.OP_SLASH, loc)); continue; }
|
|
66
|
+
if (ch === "%") { tokens.push(this.single(TT.OP_MOD, loc)); continue; }
|
|
67
|
+
if (ch === ";") { tokens.push(this.single(TT.OP_SEMI, loc)); continue; }
|
|
68
|
+
if (ch === "<") { tokens.push(this.single(TT.OP_LT, loc)); continue; }
|
|
69
|
+
if (ch === ">") { tokens.push(this.single(TT.OP_GT, loc)); continue; }
|
|
70
|
+
if (ch === "-" && this.src[this.pos + 1] === ">") {
|
|
71
|
+
this.adv(); this.adv();
|
|
72
|
+
tokens.push({ type: TT.OP_ARROW, value: "->", loc });
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (ch === "-" && !this.isDigit(this.src[this.pos + 1] ?? "")) {
|
|
76
|
+
tokens.push(this.single(TT.OP_MINUS, loc)); continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (ch === "{") {
|
|
80
|
+
if (this.rawBlockPending) {
|
|
81
|
+
this.rawBlockPending = false;
|
|
82
|
+
tokens.push(this.readRawBlock(loc));
|
|
83
|
+
} else {
|
|
84
|
+
tokens.push(this.single(TT.LBRACE, loc));
|
|
85
|
+
}
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (ch === '"' || ch === "'") { tokens.push(this.readString(loc)); continue; }
|
|
90
|
+
|
|
91
|
+
if (this.isDigit(ch) || (ch === "-" && this.isDigit(this.src[this.pos + 1] ?? ""))) {
|
|
92
|
+
tokens.push(this.readNumber(loc)); continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (this.isIdentStart(ch)) {
|
|
96
|
+
const tok = this.readIdent(loc);
|
|
97
|
+
if (
|
|
98
|
+
tok.type === TT.KW_STYLE ||
|
|
99
|
+
tok.type === TT.KW_SCRIPT ||
|
|
100
|
+
tok.type === TT.KW_COMPONENT ||
|
|
101
|
+
tok.type === TT.KW_CUSTOM_EL ||
|
|
102
|
+
tok.type === TT.KW_CUSTOM
|
|
103
|
+
) {
|
|
104
|
+
this.rawBlockPending = true;
|
|
105
|
+
}
|
|
106
|
+
tokens.push(tok);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
throw new SinthError(`Unexpected character '${ch}'`, loc);
|
|
111
|
+
}
|
|
112
|
+
tokens.push({ type: TT.EOF, value: "", loc: this.loc() });
|
|
113
|
+
return tokens;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private skipWS(): void {
|
|
117
|
+
while (this.pos < this.src.length) {
|
|
118
|
+
const ch = this.src[this.pos];
|
|
119
|
+
if (ch === " " || ch === "\t" || ch === "\r" || ch === "\n") { this.adv(); continue; }
|
|
120
|
+
|
|
121
|
+
// sinth comments: -- line or --[ nestable block for when doing longgerrr comments]--
|
|
122
|
+
if (ch === "-" && this.src[this.pos + 1] === "-") {
|
|
123
|
+
if (this.src[this.pos + 2] === "[") {
|
|
124
|
+
this.adv(); this.adv(); this.adv();
|
|
125
|
+
let depth = 1;
|
|
126
|
+
while (this.pos < this.src.length && depth > 0) {
|
|
127
|
+
if (this.src[this.pos] === "-" && this.src[this.pos + 1] === "-" && this.src[this.pos + 2] === "[") {
|
|
128
|
+
depth++; this.adv(); this.adv(); this.adv();
|
|
129
|
+
} else if (this.src[this.pos] === "]" && this.src[this.pos + 1] === "-" && this.src[this.pos + 2] === "-") {
|
|
130
|
+
depth--; this.adv(); this.adv(); this.adv();
|
|
131
|
+
} else {
|
|
132
|
+
this.adv();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (depth > 0) throw new SinthError("Unterminated block comment --[ ... ]--", this.loc());
|
|
136
|
+
} else {
|
|
137
|
+
while (this.pos < this.src.length && this.src[this.pos] !== "\n") this.adv();
|
|
138
|
+
}
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private readRawBlock(loc: Loc): Token {
|
|
146
|
+
this.adv(); // consume '{'
|
|
147
|
+
const start = this.pos;
|
|
148
|
+
let depth = 1;
|
|
149
|
+
|
|
150
|
+
while (this.pos < this.src.length && depth > 0) {
|
|
151
|
+
const ch = this.src[this.pos];
|
|
152
|
+
if (ch === '"' || ch === "'" || ch === "`") { this.skipRawString(ch); continue; }
|
|
153
|
+
if (ch === "/" && this.src[this.pos + 1] === "/") {
|
|
154
|
+
while (this.pos < this.src.length && this.src[this.pos] !== "\n") this.adv();
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (ch === "/" && this.src[this.pos + 1] === "*") {
|
|
158
|
+
this.adv(); this.adv();
|
|
159
|
+
while (this.pos < this.src.length - 1 &&
|
|
160
|
+
!(this.src[this.pos] === "*" && this.src[this.pos + 1] === "/")) { this.adv(); }
|
|
161
|
+
this.adv(); this.adv();
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (ch === "{") { depth++; this.adv(); }
|
|
165
|
+
else if (ch === "}") {
|
|
166
|
+
depth--;
|
|
167
|
+
if (depth > 0) this.adv();
|
|
168
|
+
} else {
|
|
169
|
+
this.adv();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const content = this.src.substring(start, this.pos);
|
|
174
|
+
this.adv(); // consume closing '}'
|
|
175
|
+
return { type: TT.RAW_BLOCK, value: content, loc };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private skipRawString(quote: string): void {
|
|
179
|
+
this.adv();
|
|
180
|
+
while (this.pos < this.src.length) {
|
|
181
|
+
const c = this.src[this.pos];
|
|
182
|
+
if (c === "\\") { this.adv(); this.adv(); continue; }
|
|
183
|
+
if (c === quote) { this.adv(); return; }
|
|
184
|
+
if (quote === "`" && c === "$" && this.src[this.pos + 1] === "{") {
|
|
185
|
+
this.adv(); this.adv();
|
|
186
|
+
let d = 1;
|
|
187
|
+
while (this.pos < this.src.length && d > 0) {
|
|
188
|
+
if (this.src[this.pos] === "{") d++;
|
|
189
|
+
else if (this.src[this.pos] === "}") d--;
|
|
190
|
+
this.adv();
|
|
191
|
+
}
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
this.adv();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private readString(loc: Loc): Token {
|
|
199
|
+
const quote = this.src[this.pos];
|
|
200
|
+
this.adv();
|
|
201
|
+
let result = "";
|
|
202
|
+
while (this.pos < this.src.length && this.src[this.pos] !== quote) {
|
|
203
|
+
if (this.src[this.pos] === "\\") {
|
|
204
|
+
this.adv();
|
|
205
|
+
const e = this.src[this.pos];
|
|
206
|
+
switch (e) {
|
|
207
|
+
case "\\": result += "\\"; break;
|
|
208
|
+
case '"': result += '"'; break;
|
|
209
|
+
case "'": result += "'"; break;
|
|
210
|
+
case "n": result += "\n"; break;
|
|
211
|
+
case "r": result += "\r"; break;
|
|
212
|
+
case "t": result += "\t"; break;
|
|
213
|
+
case "$": result += "$"; break;
|
|
214
|
+
case "{": result += "{"; break;
|
|
215
|
+
case "u": {
|
|
216
|
+
this.adv();
|
|
217
|
+
if (this.src[this.pos] === "{") {
|
|
218
|
+
this.adv();
|
|
219
|
+
let hex = "";
|
|
220
|
+
while (this.src[this.pos] !== "}") { hex += this.src[this.pos]; this.adv(); }
|
|
221
|
+
result += String.fromCodePoint(parseInt(hex, 16));
|
|
222
|
+
} else {
|
|
223
|
+
const hex = this.src.substring(this.pos, this.pos + 4);
|
|
224
|
+
result += String.fromCharCode(parseInt(hex, 16));
|
|
225
|
+
this.pos += 3; this.col += 3;
|
|
226
|
+
}
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
default: throw new SinthError(`Unknown escape sequence \\${e}`, loc);
|
|
230
|
+
}
|
|
231
|
+
this.adv();
|
|
232
|
+
} else {
|
|
233
|
+
result += this.src[this.pos];
|
|
234
|
+
this.adv();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
if (this.pos >= this.src.length) throw new SinthError("Unterminated string literal", loc);
|
|
238
|
+
this.adv();
|
|
239
|
+
return { type: TT.STRING, value: result, loc };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private readNumber(loc: Loc): Token {
|
|
243
|
+
let s = "";
|
|
244
|
+
if (this.src[this.pos] === "-") { s += "-"; this.adv(); }
|
|
245
|
+
while (this.isDigit(this.src[this.pos] ?? "")) { s += this.src[this.pos]; this.adv(); }
|
|
246
|
+
if (this.src[this.pos] === "." && this.isDigit(this.src[this.pos + 1] ?? "")) {
|
|
247
|
+
s += "."; this.adv();
|
|
248
|
+
while (this.isDigit(this.src[this.pos] ?? "")) { s += this.src[this.pos]; this.adv(); }
|
|
249
|
+
}
|
|
250
|
+
return { type: TT.NUMBER, value: s, loc };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
private readIdent(loc: Loc): Token {
|
|
254
|
+
let s = "";
|
|
255
|
+
while (this.pos < this.src.length && this.isIdentCont(this.src[this.pos])) {
|
|
256
|
+
s += this.src[this.pos]; this.adv();
|
|
257
|
+
}
|
|
258
|
+
const kw = KEYWORDS[s];
|
|
259
|
+
return { type: kw !== undefined ? kw : TT.IDENT, value: s, loc };
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private adv(): void {
|
|
263
|
+
const ch = this.src[this.pos++];
|
|
264
|
+
if (ch === "\n") { this.line++; this.col = 1; } else { this.col++; }
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private single(type: TT, loc: Loc): Token {
|
|
268
|
+
this.adv();
|
|
269
|
+
return { type, value: this.src[this.pos - 1], loc };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private loc(): Loc { return { file: this.file, line: this.line, col: this.col }; }
|
|
273
|
+
private isDigit(c: string) { return c >= "0" && c <= "9"; }
|
|
274
|
+
private isIdentStart(c: string) { return /[a-zA-Z_$\u00C0-\uFFFF]/.test(c); }
|
|
275
|
+
private isIdentCont(c: string) { return /[a-zA-Z0-9_$\-\u00C0-\uFFFF]/.test(c); }
|
|
276
276
|
}
|