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