@yannosay/sinth 1.11.4 → 1.12.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/src/core/expr.ts DELETED
@@ -1,94 +0,0 @@
1
- import { Expression, Child, AssignStmt, IfBlock, ReturnStmt, LitStr, LitNum, LitBool, CompileCtx } from "./types.ts";
2
-
3
- export function compileExprToJS(expr: Expression, loopVars?: Set<string>, namespace?: string, declaredVars?: Set<string>): string {
4
- switch (expr.kind) {
5
- case "literal":
6
- if (!expr.value) return "null";
7
- if (expr.value.kind === "str") {
8
- const sv = (expr.value as LitStr).value;
9
- if (sv.startsWith("__ARR__")) return sv.slice(7);
10
- if (sv.startsWith("__VAR__")) return sv.slice(7);
11
- return JSON.stringify(sv);
12
- }
13
- if (expr.value.kind === "num") return String((expr.value as LitNum).value);
14
- if (expr.value.kind === "bool") return String((expr.value as LitBool).value);
15
- return "null";
16
- case "variable":
17
- if (loopVars && expr.name) {
18
- const dotIdx = expr.name.indexOf('.');
19
- const root = dotIdx !== -1 ? expr.name.substring(0, dotIdx) : expr.name;
20
- if (loopVars.has(root)) {
21
- return `_ctx.${expr.name}`;
22
- }
23
- }
24
- if (namespace && declaredVars && expr.name && declaredVars.has(expr.name)) {
25
- return namespace + "_" + expr.name;
26
- }
27
- return expr.name ?? "undefined";
28
- case "binary": {
29
- const l = compileExprToJS(expr.left!, loopVars, namespace, declaredVars);
30
- const r = compileExprToJS(expr.right!, loopVars, namespace, declaredVars);
31
- const o = expr.op === "and" ? "&&" : expr.op === "or" ? "||" : expr.op!;
32
- return `(${l} ${o} ${r})`;
33
- }
34
- case "unary": {
35
- const o = expr.op === "not" ? "!" : expr.op!;
36
- return `${o}(${compileExprToJS(expr.operand!, loopVars, namespace, declaredVars)})`;
37
- }
38
- case "assign": {
39
- const v = expr.right ? compileExprToJS(expr.right, loopVars, namespace, declaredVars) : "null";
40
- const t = (namespace && declaredVars && expr.target && declaredVars.has(expr.target)) ? namespace + "_" + expr.target : expr.target;
41
- return `${t} ${expr.op} ${v}`;
42
- }
43
- case "index":
44
- return `${compileExprToJS(expr.object!, loopVars, namespace, declaredVars)}[${compileExprToJS(expr.key!, loopVars, namespace, declaredVars)}]`;
45
- case "postfix":
46
- const t2 = (namespace && declaredVars && expr.target && declaredVars.has(expr.target)) ? namespace + "_" + expr.target : expr.target;
47
- return `${t2}${expr.op}`;
48
- case "call": {
49
- const callee = compileExprToJS(expr.callee!, loopVars, namespace, declaredVars);
50
- if (callee === "remove") {
51
- const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars, namespace, declaredVars) : "''";
52
- return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
53
- }
54
- const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars, namespace, declaredVars)).join(", ");
55
- if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
56
- return callee;
57
- }
58
- return `${callee}(${args})`;
59
- }
60
- default:
61
- return "";
62
- }
63
- }
64
-
65
-
66
- export function compileIfToJS(ifBlock: IfBlock): string {
67
- const cond = compileExprToJS(ifBlock.condition);
68
- const ifJS = bodyToJS(ifBlock.body);
69
- const elseJS = ifBlock.elseBody ? bodyToJS(ifBlock.elseBody) : "";
70
- let js = `if (${cond}) {\n${ifJS}}\n`;
71
- if (elseJS) js += `else {\n${elseJS}}\n`;
72
- return js;
73
- }
74
-
75
-
76
-
77
- export function bodyToJS(children: Child[]): string {
78
- return children
79
- .filter(c => c.kind === "assign_stmt" || c.kind === "if" || c.kind === "return" || c.kind === "expr")
80
- .map(c => {
81
- if (c.kind === "assign_stmt") return ` ${compileExprToJS((c as AssignStmt).expression)};\n`;
82
- if (c.kind === "return") return ` return ${(c as ReturnStmt).expression ? compileExprToJS((c as ReturnStmt).expression!) : ""};\n`;
83
- if (c.kind === "if") return compileIfToJS(c as IfBlock).replace(/^/gm, " ") + "\n";
84
- if (c.kind === "expr") return ` ${compileExprToJS((c as any).expression)};\n`;
85
- return "";
86
- })
87
- .join("");
88
- }
89
- export function scopeVariableName(name: string, ctx?: CompileCtx): string {
90
- if (ctx?.scopePrefix && ctx?.scopeVar && ctx?.varDecls?.some(v => v.name === name)) {
91
- return `${ctx.scopePrefix}${name}`;
92
- }
93
- return name;
94
- }
@@ -1,86 +0,0 @@
1
- import * as path from "path";
2
- import { MetaEntry, SinthWarning } from "./types.ts";
3
- import { esc, escAttr, litToString, camelToKebab } from "../utils.ts";
4
-
5
-
6
-
7
- export interface HeadData {
8
- title?: string; fav?: string;
9
- lang: string; charset: string; viewport: string;
10
- metaTags: { name: string; content: string }[];
11
- metaProps: { property: string; content: string }[];
12
- }
13
-
14
- export function buildHeadData(meta: MetaEntry[]): HeadData {
15
- let title: string | undefined, fav: string | undefined;
16
- let lang = "en", charset = "UTF-8", viewport = "width=device-width, initial-scale=1.0";
17
- const metaTags: { name: string; content: string }[] = [];
18
- const metaProps: { property: string; content: string }[] = [];
19
-
20
- for (const m of meta) {
21
- const val = litToString(m.value);
22
- switch (m.key) {
23
- case "title": title = val; break;
24
- case "fav": fav = val; break;
25
- case "lang": lang = val; break;
26
- case "charset": charset = val; break;
27
- case "viewport": viewport = val; break;
28
- case "descr": metaTags.push({ name: "description", content: val }); break;
29
- case "author": metaTags.push({ name: "author", content: val }); break;
30
- case "keywords": metaTags.push({ name: "keywords", content: val }); break;
31
- case "robots": metaTags.push({ name: "robots", content: val }); break;
32
- default:
33
- if (m.key.startsWith("og") || m.key.startsWith("twitter")) {
34
- const prop = m.key
35
- .replace(/^og([A-Z])/, (_: string, c: string) => `og:${c.toLowerCase()}`)
36
- .replace(/^twitter([A-Z])/, (_: string, c: string) => `twitter:${c.toLowerCase()}`);
37
- metaProps.push({ property: prop, content: val });
38
- } else {
39
- metaTags.push({ name: camelToKebab(m.key), content: val });
40
- }
41
- }
42
- }
43
-
44
- return { title, fav, lang, charset, viewport, metaTags, metaProps };
45
- }
46
-
47
- export function faviconType(p: string): string {
48
- switch (path.extname(p).toLowerCase()) {
49
- case ".ico": return "image/x-icon";
50
- case ".png": return "image/png";
51
- case ".svg": return "image/svg+xml";
52
- case ".gif": return "image/gif";
53
- case ".webp": return "image/webp";
54
- case ".jpg":
55
- case ".jpeg": return "image/jpeg";
56
- default:
57
- SinthWarning.emit(`Unknown favicon extension '${path.extname(p)}', defaulting to image/x-icon`);
58
- return "image/x-icon";
59
- }
60
- }
61
-
62
- export function renderHead(
63
- hd: HeadData,
64
- cssLinks: string[],
65
- jsLinks: { src: string; attrs: Record<string, string> }[],
66
- scopedCSS: string,
67
- companionJS: string | undefined,
68
- ): string {
69
- const lines: string[] = [];
70
- lines.push(` <meta charset="${escAttr(hd.charset)}">`);
71
- lines.push(` <meta name="viewport" content="${escAttr(hd.viewport)}">`);
72
- if (hd.title) lines.push(` <title>${esc(hd.title)}</title>`);
73
- if (hd.fav) lines.push(` <link rel="icon" href="${escAttr(hd.fav)}" type="${faviconType(hd.fav)}">`);
74
- for (const m of hd.metaTags) lines.push(` <meta name="${escAttr(m.name)}" content="${escAttr(m.content)}">`);
75
- for (const m of hd.metaProps) lines.push(` <meta property="${escAttr(m.property)}" content="${escAttr(m.content)}">`);
76
- for (const css of cssLinks) lines.push(` <link rel="stylesheet" href="${escAttr(css)}">`);
77
- if (scopedCSS.trim()) lines.push(` <style>\n${scopedCSS}\n </style>`);
78
- for (const js of jsLinks) {
79
- const extra = Object.entries(js.attrs)
80
- .map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
81
- .join(" ");
82
- lines.push(` <script src="${escAttr(js.src)}"${extra ? " " + extra : ""}></script>`);
83
- }
84
- if (companionJS) lines.push(` <script src="${escAttr(companionJS)}"></script>`);
85
- return `<head>\n${lines.join("\n")}\n</head>`;
86
- }
package/src/core/lexer.ts DELETED
@@ -1,276 +0,0 @@
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
- }