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