@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
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { VarDeclaration, MixedBlockEntry, SinthWarning } from "./types.ts";
|
|
2
|
+
import { litToString } from "../utils.ts";
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export function buildRuntime(opts: {
|
|
7
|
+
varDecls: VarDeclaration[];
|
|
8
|
+
bodyHTML: string;
|
|
9
|
+
logicBlocks: string[];
|
|
10
|
+
mixedBlocks: MixedBlockEntry[];
|
|
11
|
+
assignedVars: Set<string>;
|
|
12
|
+
exprRegistry: string[];
|
|
13
|
+
sharedRuntime: boolean;
|
|
14
|
+
functionsJS: string; // ← new
|
|
15
|
+
}): string | { page: string; shared: string } {
|
|
16
|
+
const { varDecls, bodyHTML, logicBlocks, mixedBlocks, assignedVars, exprRegistry, functionsJS } = opts;
|
|
17
|
+
|
|
18
|
+
const needsExpr = bodyHTML.includes("sinth-expr");
|
|
19
|
+
const needsIf = bodyHTML.includes("data-sinth-if");
|
|
20
|
+
const needsFor = bodyHTML.includes("data-sinth-for");
|
|
21
|
+
const needsDelay = bodyHTML.includes("data-sinth-delay") || bodyHTML.includes("data-sinth-delay-expr-id") || mixedBlocks.some(mb => mb.ifHTML.includes("data-sinth-delay") || mb.ifHTML.includes("data-sinth-delay-expr-id") || mb.elseHTML.includes("data-sinth-delay") || mb.elseHTML.includes("data-sinth-delay-expr-id"));
|
|
22
|
+
const needsMixed = mixedBlocks.length > 0;
|
|
23
|
+
const needsLogic = logicBlocks.length > 0;
|
|
24
|
+
const needsRender = needsExpr || needsIf || needsFor || needsMixed || needsLogic;
|
|
25
|
+
|
|
26
|
+
const varLines = varDecls.map(v => {
|
|
27
|
+
if (!v.value) {
|
|
28
|
+
if (!assignedVars.has(v.name)) {
|
|
29
|
+
const defaults: Record<string, string> = { str: '""', int: "0", bool: "false", "str[]": "[]" };
|
|
30
|
+
SinthWarning.emit(
|
|
31
|
+
`Variable '${v.name}' is declared but never assigned. Defaulting to ${defaults[v.varType] ?? "undefined"}.`,
|
|
32
|
+
v.loc,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
const defaults: Record<string, string> = { str: '""', int: "0", bool: "false", "str[]": "[]" };
|
|
36
|
+
return `var ${v.name} = ${defaults[v.varType] ?? "undefined"};`;
|
|
37
|
+
}
|
|
38
|
+
const val = litToString(v.value);
|
|
39
|
+
if (val.startsWith("__VAR__")) return `var ${v.name} = ${val.slice(7)};`;
|
|
40
|
+
if (val.startsWith("__ARR__")) return `var ${v.name} = ${val.slice(7)};`;
|
|
41
|
+
if (v.varType === "obj") return `var ${v.name} = ${val};`;
|
|
42
|
+
if (v.varType === "str") return `var ${v.name} = ${JSON.stringify(val)};`;
|
|
43
|
+
if (v.varType === "str[]" && typeof val === 'string' && val.startsWith("__ARR__")) {
|
|
44
|
+
try {
|
|
45
|
+
const arr = JSON.parse(val.slice(7));
|
|
46
|
+
return `var ${v.name} = ${JSON.stringify(arr)};`;
|
|
47
|
+
} catch { return `var ${v.name} = ${val.slice(7)};`; }
|
|
48
|
+
}
|
|
49
|
+
return `var ${v.name} = ${val};`;
|
|
50
|
+
}).join("\n");
|
|
51
|
+
|
|
52
|
+
if (!needsRender && !needsDelay) {
|
|
53
|
+
return varLines ? `// Sinth compiled runtime\n${varLines}` : "";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let helpers = "";
|
|
57
|
+
|
|
58
|
+
if (needsExpr || needsIf || needsFor) {
|
|
59
|
+
helpers += `
|
|
60
|
+
function sinthExpr(el) {
|
|
61
|
+
try {
|
|
62
|
+
var exprFn = __X[el.dataset.exprId];
|
|
63
|
+
if (exprFn) el.textContent = exprFn({});
|
|
64
|
+
} catch(e) {}
|
|
65
|
+
}
|
|
66
|
+
`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (needsIf || needsMixed) {
|
|
70
|
+
helpers += `
|
|
71
|
+
function sinthReplaceInsert(t, anchor, ifId, replaceId) {
|
|
72
|
+
if (anchor) {
|
|
73
|
+
var cur = anchor.nextSibling;
|
|
74
|
+
while (cur && cur !== t) { var nx = cur.nextSibling; cur.remove(); cur = nx; }
|
|
75
|
+
} else {
|
|
76
|
+
anchor = document.createElement('span');
|
|
77
|
+
anchor.style.display = 'none';
|
|
78
|
+
anchor.dataset.sinthIfAnchor = ifId;
|
|
79
|
+
t.parentNode.insertBefore(anchor, t);
|
|
80
|
+
}
|
|
81
|
+
var _rp = null, _rpParent = null, _rpNext = null;
|
|
82
|
+
if (replaceId) {
|
|
83
|
+
_rp = document.getElementById(replaceId);
|
|
84
|
+
if (_rp) {
|
|
85
|
+
_rpParent = _rp.parentNode;
|
|
86
|
+
_rpNext = _rp.nextSibling;
|
|
87
|
+
_rp.parentNode.removeChild(_rp);
|
|
88
|
+
anchor._sinthReplaced = _rp;
|
|
89
|
+
anchor._sinthReplacedParent = _rpParent;
|
|
90
|
+
anchor._sinthReplacedNext = _rpNext;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
94
|
+
frag.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
95
|
+
if (${needsDelay}) {
|
|
96
|
+
frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
97
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
98
|
+
}
|
|
99
|
+
var fragFirst = frag.firstChild;
|
|
100
|
+
var fragLast = frag.lastChild;
|
|
101
|
+
if (_rpParent && _rpNext) {
|
|
102
|
+
_rpParent.insertBefore(frag, _rpNext);
|
|
103
|
+
} else if (_rpParent) {
|
|
104
|
+
_rpParent.appendChild(frag);
|
|
105
|
+
} else {
|
|
106
|
+
t.parentNode.insertBefore(frag, t);
|
|
107
|
+
}
|
|
108
|
+
if (replaceId && anchor) {
|
|
109
|
+
anchor._sinthInsertedFirst = fragFirst;
|
|
110
|
+
anchor._sinthInsertedLast = fragLast;
|
|
111
|
+
}
|
|
112
|
+
return anchor;
|
|
113
|
+
}
|
|
114
|
+
`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (needsDelay) {
|
|
118
|
+
helpers += `
|
|
119
|
+
function sinthDelay(el) {
|
|
120
|
+
if (el.dataset.sinthDelayDone) { el.style.display = ''; return; }
|
|
121
|
+
el.dataset.sinthDelayDone = '1';
|
|
122
|
+
var ms = parseInt(el.dataset.sinthDelay) || 0;
|
|
123
|
+
el.style.display = 'none';
|
|
124
|
+
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
125
|
+
else el.style.display = '';
|
|
126
|
+
}
|
|
127
|
+
function sinthDelayExpr(el) {
|
|
128
|
+
try {
|
|
129
|
+
var fn = __X[el.dataset.sinthDelayExprId];
|
|
130
|
+
var ms = fn ? parseInt(fn()) || 0 : 0;
|
|
131
|
+
el.style.display = '';
|
|
132
|
+
if (ms > 0) setTimeout(function() { el.style.display = ''; }, ms);
|
|
133
|
+
} catch(e) {}
|
|
134
|
+
}
|
|
135
|
+
`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (needsIf) {
|
|
139
|
+
helpers += `
|
|
140
|
+
function sinthIfBlock(t) {
|
|
141
|
+
var ifId = t.dataset.sinthIfId;
|
|
142
|
+
var anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
|
|
143
|
+
var condFn = __X[t.dataset.sinthIfExpr];
|
|
144
|
+
var cond = condFn ? condFn() : false;
|
|
145
|
+
if (cond) {
|
|
146
|
+
anchor = sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
|
|
147
|
+
} else {
|
|
148
|
+
if (anchor) {
|
|
149
|
+
if (anchor._sinthReplaced) {
|
|
150
|
+
var insFirst = anchor._sinthInsertedFirst;
|
|
151
|
+
var insLast = anchor._sinthInsertedLast;
|
|
152
|
+
var rpParent = anchor._sinthReplacedParent;
|
|
153
|
+
var rpNext = anchor._sinthReplacedNext;
|
|
154
|
+
if (insFirst && insLast) {
|
|
155
|
+
var cur = insFirst;
|
|
156
|
+
while (cur && cur !== insLast) {
|
|
157
|
+
var next = cur.nextSibling;
|
|
158
|
+
cur.remove();
|
|
159
|
+
cur = next;
|
|
160
|
+
}
|
|
161
|
+
if (insLast) insLast.remove();
|
|
162
|
+
}
|
|
163
|
+
if (rpParent && rpNext) {
|
|
164
|
+
rpParent.insertBefore(anchor._sinthReplaced, rpNext);
|
|
165
|
+
} else if (rpParent) {
|
|
166
|
+
rpParent.appendChild(anchor._sinthReplaced);
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
var cur2 = anchor.nextSibling;
|
|
170
|
+
while (cur2 && cur2 !== t) { var nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
|
171
|
+
}
|
|
172
|
+
anchor.remove();
|
|
173
|
+
}
|
|
174
|
+
var elseT = t.nextElementSibling;
|
|
175
|
+
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
176
|
+
var elseIfId = elseT.dataset.sinthIfId;
|
|
177
|
+
var ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + elseIfId + '"]');
|
|
178
|
+
if (ea) {
|
|
179
|
+
var cur3 = ea.nextSibling;
|
|
180
|
+
while (cur3 && cur3 !== t) { var nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
|
|
181
|
+
} else {
|
|
182
|
+
ea = document.createElement('span');
|
|
183
|
+
ea.style.display = 'none';
|
|
184
|
+
ea.dataset.sinthIfAnchor = '__else__' + elseIfId;
|
|
185
|
+
t.parentNode.insertBefore(ea, t);
|
|
186
|
+
}
|
|
187
|
+
var ef = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
188
|
+
ef.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
189
|
+
if (${needsDelay}) {
|
|
190
|
+
ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
191
|
+
ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
192
|
+
}
|
|
193
|
+
t.parentNode.insertBefore(ef, t);
|
|
194
|
+
} else {
|
|
195
|
+
var ea2 = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
|
|
196
|
+
if (ea2) {
|
|
197
|
+
var ec = ea2.nextSibling;
|
|
198
|
+
while (ec && ec !== t) { var en = ec.nextSibling; ec.remove(); ec = en; }
|
|
199
|
+
ea2.remove();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (needsFor) {
|
|
208
|
+
helpers += `
|
|
209
|
+
function hashString(str) {
|
|
210
|
+
var hash = 0, i, chr;
|
|
211
|
+
for (i = 0; i < str.length; i++) {
|
|
212
|
+
chr = str.charCodeAt(i);
|
|
213
|
+
hash = ((hash << 5) - hash) + chr;
|
|
214
|
+
hash |= 0;
|
|
215
|
+
}
|
|
216
|
+
return String(hash);
|
|
217
|
+
}
|
|
218
|
+
function sinthForBlock(t) {
|
|
219
|
+
var source = window[t.dataset.sinthFor];
|
|
220
|
+
if (source === undefined) source = [];
|
|
221
|
+
var newHash = '';
|
|
222
|
+
try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
|
|
223
|
+
if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
t.dataset.sinthForHash = newHash;
|
|
227
|
+
var isObj = (typeof source === 'object' && source !== null && !Array.isArray(source));
|
|
228
|
+
var entries;
|
|
229
|
+
if (isObj) {
|
|
230
|
+
entries = Object.entries(source);
|
|
231
|
+
} else {
|
|
232
|
+
if (!Array.isArray(source)) source = [];
|
|
233
|
+
entries = source.map(function(item, index) { return [index, item]; });
|
|
234
|
+
}
|
|
235
|
+
var anchor = t.parentNode ? t.parentNode.querySelector('[data-sinth-for-anchor="' + t.dataset.sinthFor + '"]') : null;
|
|
236
|
+
if (anchor) {
|
|
237
|
+
var cur2 = anchor.nextSibling;
|
|
238
|
+
while (cur2 && cur2 !== t) { var nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
|
|
239
|
+
anchor.remove();
|
|
240
|
+
}
|
|
241
|
+
var fa = document.createElement('span');
|
|
242
|
+
fa.style.display = 'none';
|
|
243
|
+
fa.dataset.sinthForAnchor = t.dataset.sinthFor;
|
|
244
|
+
t.parentNode && t.parentNode.insertBefore(fa, t);
|
|
245
|
+
var _loopIdx = 0;
|
|
246
|
+
entries.forEach(function(entry) {
|
|
247
|
+
var _k = entry[0];
|
|
248
|
+
var _v = entry[1];
|
|
249
|
+
var _item = (t.dataset && t.dataset.sinthItem) ? t.dataset.sinthItem : '__item__';
|
|
250
|
+
var _key = t.dataset && t.dataset.sinthKey ? t.dataset.sinthKey : null;
|
|
251
|
+
var _idx = t.dataset && t.dataset.sinthIndex ? t.dataset.sinthIndex : null;
|
|
252
|
+
_loopIdx++;
|
|
253
|
+
if (t.dataset.sinthIfReplace) {
|
|
254
|
+
var _rp = document.getElementById(t.dataset.sinthIfReplace);
|
|
255
|
+
if (_rp) _rp.parentNode.removeChild(_rp);
|
|
256
|
+
}
|
|
257
|
+
var frag = document.createRange().createContextualFragment(t.innerHTML);
|
|
258
|
+
frag.querySelectorAll('.sinth-expr').forEach(function(el) {
|
|
259
|
+
try {
|
|
260
|
+
var exprFn = __X[el.dataset.exprId];
|
|
261
|
+
if (!exprFn) return;
|
|
262
|
+
var _ctx = {};
|
|
263
|
+
if (_item) _ctx[_item] = _v;
|
|
264
|
+
if (_key) _ctx[_key] = _k;
|
|
265
|
+
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
266
|
+
el.textContent = exprFn(_ctx);
|
|
267
|
+
el.classList.remove('sinth-expr');
|
|
268
|
+
} catch(e) {}
|
|
269
|
+
});
|
|
270
|
+
frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
|
|
271
|
+
var condFn = __X[ifT.dataset.sinthIfExpr];
|
|
272
|
+
var _ctx = {};
|
|
273
|
+
if (_item) _ctx[_item] = _v;
|
|
274
|
+
if (_key) _ctx[_key] = _k;
|
|
275
|
+
if (_idx) _ctx[_idx] = _loopIdx - 1;
|
|
276
|
+
var cond = false;
|
|
277
|
+
try { if (condFn) cond = condFn(_ctx); } catch(e) {}
|
|
278
|
+
if (cond) {
|
|
279
|
+
var ifContent = document.createRange().createContextualFragment(ifT.innerHTML);
|
|
280
|
+
ifContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
281
|
+
try {
|
|
282
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
283
|
+
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
284
|
+
} catch(e) {}
|
|
285
|
+
});
|
|
286
|
+
ifT.parentNode.insertBefore(ifContent, ifT);
|
|
287
|
+
} else {
|
|
288
|
+
var elseT = ifT.nextElementSibling;
|
|
289
|
+
if (elseT && elseT.hasAttribute('data-sinth-else')) {
|
|
290
|
+
var elseContent = document.createRange().createContextualFragment(elseT.innerHTML);
|
|
291
|
+
elseContent.querySelectorAll('.sinth-expr').forEach(function(el2) {
|
|
292
|
+
try {
|
|
293
|
+
var exprFn2 = __X[el2.dataset.exprId];
|
|
294
|
+
if (exprFn2) el2.textContent = exprFn2(_ctx);
|
|
295
|
+
} catch(e) {}
|
|
296
|
+
});
|
|
297
|
+
ifT.parentNode.insertBefore(elseContent, ifT);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
if (${needsDelay}) {
|
|
302
|
+
frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
303
|
+
frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
304
|
+
}
|
|
305
|
+
t.parentNode && t.parentNode.insertBefore(frag, t);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
`;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (needsMixed) {
|
|
312
|
+
helpers += `
|
|
313
|
+
function sinthMixedBlock(el, condId, ifJS, ifHTML, elseJS, elseHTML) {
|
|
314
|
+
var condFn = __X[condId];
|
|
315
|
+
var cond = condFn ? condFn() : false;
|
|
316
|
+
if (cond) {
|
|
317
|
+
if (ifJS) eval(ifJS);
|
|
318
|
+
el.innerHTML = ifHTML;
|
|
319
|
+
} else {
|
|
320
|
+
if (elseJS) eval(elseJS);
|
|
321
|
+
el.innerHTML = elseHTML;
|
|
322
|
+
}
|
|
323
|
+
el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
|
|
324
|
+
el.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);
|
|
325
|
+
if (${needsDelay}) {
|
|
326
|
+
el.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
327
|
+
el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
`;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const exprArrayJS = exprRegistry.length > 0
|
|
334
|
+
? `var __X = [${exprRegistry.map((js) => `function(_ctx){ return ${js}; }`).join(",")}];\n`
|
|
335
|
+
: "";
|
|
336
|
+
|
|
337
|
+
let renderBody = "";
|
|
338
|
+
renderBody += ` var _sx = window.scrollX, _sy = window.scrollY;\n`;
|
|
339
|
+
|
|
340
|
+
if (needsLogic) {
|
|
341
|
+
renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (needsMixed) {
|
|
345
|
+
for (const mb of mixedBlocks) {
|
|
346
|
+
renderBody += ` (function() {
|
|
347
|
+
var __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
|
|
348
|
+
if (__el) sinthMixedBlock(__el, ${mb.conditionJS}, ${JSON.stringify(mb.ifJS)}, ${JSON.stringify(mb.ifHTML)}, ${JSON.stringify(mb.elseJS)}, ${JSON.stringify(mb.elseHTML)});
|
|
349
|
+
})();\n`;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
|
|
354
|
+
var target = document.getElementById(el.dataset.sinthRemove);
|
|
355
|
+
if (target) target.remove();
|
|
356
|
+
});\n`;
|
|
357
|
+
|
|
358
|
+
if (needsIf) {
|
|
359
|
+
renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);\n`;
|
|
360
|
+
}
|
|
361
|
+
if (needsFor) {
|
|
362
|
+
renderBody += ` document.querySelectorAll('template[data-sinth-for]').forEach(sinthForBlock);\n`;
|
|
363
|
+
}
|
|
364
|
+
renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
365
|
+
try { el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
|
|
366
|
+
});\n`;
|
|
367
|
+
renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
368
|
+
try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
|
|
369
|
+
});\n`;
|
|
370
|
+
if (needsExpr) {
|
|
371
|
+
renderBody += ` document.querySelectorAll('.sinth-expr').forEach(sinthExpr);\n`;
|
|
372
|
+
}
|
|
373
|
+
if (needsDelay) {
|
|
374
|
+
renderBody += ` setTimeout(function() {
|
|
375
|
+
document.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
|
|
376
|
+
document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(sinthDelayExpr);
|
|
377
|
+
}, 0);\n`;
|
|
378
|
+
}
|
|
379
|
+
renderBody += ` window.scrollTo(_sx, _sy);\n`;
|
|
380
|
+
|
|
381
|
+
const renderFunc = needsRender ? `function sinthRender() {\n${renderBody}}\nsinthRender();` : "";
|
|
382
|
+
|
|
383
|
+
const pageCode = `// Sinth page runtime
|
|
384
|
+
${varLines}
|
|
385
|
+
${functionsJS ? functionsJS + "\n" : ""}${exprArrayJS}
|
|
386
|
+
${renderFunc}`;
|
|
387
|
+
|
|
388
|
+
if (opts.sharedRuntime && helpers.trim()) {
|
|
389
|
+
const sharedCode = `// Sinth shared runtime
|
|
390
|
+
${helpers}`;
|
|
391
|
+
return { page: pageCode, shared: sharedCode };
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return `// Sinth compiled runtime
|
|
395
|
+
${varLines}
|
|
396
|
+
${functionsJS ? functionsJS + "\n" : ""}${helpers}
|
|
397
|
+
${exprArrayJS}
|
|
398
|
+
${renderFunc}`;
|
|
399
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Sinth Compiler
|
|
3
|
+
|
|
4
|
+
import * as fs from "fs";
|
|
5
|
+
import * as path from "path";
|
|
6
|
+
import * as http from "http";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export interface Loc { file: string; line: number; col: number }
|
|
10
|
+
|
|
11
|
+
export enum TT {
|
|
12
|
+
LBRACE, RBRACE, LPAREN, RPAREN, COLON, COMMA, EQUALS, DOT,
|
|
13
|
+
LBRACKET, RBRACKET,
|
|
14
|
+
STRING, NUMBER, BOOL_TRUE, BOOL_FALSE, NULL_LIT,
|
|
15
|
+
RAW_BLOCK,
|
|
16
|
+
IDENT,
|
|
17
|
+
KW_IMPORT, KW_AS, KW_CSS,
|
|
18
|
+
KW_COMPONENT, KW_STYLE, KW_SCRIPT,
|
|
19
|
+
KW_GLOBAL, KW_PAGE, KW_LANG,
|
|
20
|
+
KW_CUSTOM_EL, KW_CUSTOM,
|
|
21
|
+
KW_IF, KW_ELSE, KW_FOR, KW_IN, KW_REMOVE,
|
|
22
|
+
KW_FUNCTION,
|
|
23
|
+
OP_PLUS, OP_MINUS, OP_STAR, OP_SLASH, OP_SEMI,
|
|
24
|
+
OP_LT, OP_GT, OP_NEQ, OP_EQEQ, OP_LTEQ, OP_GTEQ,
|
|
25
|
+
OP_ARROW,
|
|
26
|
+
EOF,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface Token { type: TT; value: string; loc: Loc }
|
|
30
|
+
|
|
31
|
+
export type LitStr = { kind: "str"; value: string };
|
|
32
|
+
export type LitNum = { kind: "num"; value: number };
|
|
33
|
+
export type LitBool = { kind: "bool"; value: boolean };
|
|
34
|
+
export type LitNull = { kind: "null" };
|
|
35
|
+
export type Literal = LitStr | LitNum | LitBool | LitNull;
|
|
36
|
+
|
|
37
|
+
export type UnaryOp = "not" | "-";
|
|
38
|
+
export type BinaryOp = "+" | "-" | "*" | "/" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "and" | "or";
|
|
39
|
+
export type AssignOp = "=" | "+=" | "-=";
|
|
40
|
+
export type PostfixOp = "++" | "--";
|
|
41
|
+
|
|
42
|
+
export interface Expression {
|
|
43
|
+
kind: "literal" | "variable" | "unary" | "binary" | "assign" | "postfix" | "index" | "call";
|
|
44
|
+
value?: Literal;
|
|
45
|
+
name?: string;
|
|
46
|
+
op?: UnaryOp | BinaryOp | AssignOp | PostfixOp;
|
|
47
|
+
operand?: Expression;
|
|
48
|
+
left?: Expression;
|
|
49
|
+
right?: Expression;
|
|
50
|
+
target?: string;
|
|
51
|
+
object?: Expression;
|
|
52
|
+
key?: Expression;
|
|
53
|
+
callee?: Expression;
|
|
54
|
+
args?: Expression[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface Attr { name: string; value: Literal | null; loc: Loc }
|
|
58
|
+
export interface TextNode { kind: "text"; value: string; loc: Loc }
|
|
59
|
+
export interface CompUse { kind: "use"; name: string; attrs: Attr[]; children: Child[]; loc: Loc }
|
|
60
|
+
export interface ExprNode { kind: "expr"; expression: Expression; loc: Loc }
|
|
61
|
+
export interface AssignStmt { kind: "assign_stmt"; expression: Expression; loc: Loc }
|
|
62
|
+
export interface IfBlock { kind: "if"; condition: Expression; body: Child[]; elseBody?: Child[]; loc: Loc }
|
|
63
|
+
export interface ForLoop { kind: "for"; keyVar?: string; itemVar: string; indexVar?: string; arrayVar: string; body: Child[]; loc: Loc }
|
|
64
|
+
export interface RemoveStmt { kind: "remove"; target: string; loc: Loc }
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
export type Child = TextNode | CompUse | ExprNode | AssignStmt | IfBlock | ForLoop | RemoveStmt;
|
|
68
|
+
|
|
69
|
+
export interface ParamDecl { name: string; defaultVal?: Literal; loc: Loc }
|
|
70
|
+
|
|
71
|
+
export interface StyleBlock {
|
|
72
|
+
global: boolean;
|
|
73
|
+
lang: "css" | "scss";
|
|
74
|
+
raw: string;
|
|
75
|
+
target?: string;
|
|
76
|
+
extraAttrs: Record<string, string>;
|
|
77
|
+
loc: Loc;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface ScriptBlock { raw: string; attrs: Record<string, string>; loc: Loc }
|
|
81
|
+
export interface CompDef { name: string; params: ParamDecl[]; body: Child[]; styles: StyleBlock[]; scripts: ScriptBlock[]; loc: Loc }
|
|
82
|
+
export interface CustomElDecl { sinthName: string; tagName: string; params: ParamDecl[]; loc: Loc }
|
|
83
|
+
export interface CustomElInfo { tagName: string; params: ParamDecl[] }
|
|
84
|
+
|
|
85
|
+
export type VarType = "int" | "str" | "bool" | "str[]" | "obj";
|
|
86
|
+
export interface VarDeclaration { kind: "var"; name: string; varType: VarType; value: Literal | null; loc: Loc }
|
|
87
|
+
|
|
88
|
+
export type ImportNode =
|
|
89
|
+
| { kind: "sinth"; path: string; loc: Loc }
|
|
90
|
+
| { kind: "css"; path: string; loc: Loc }
|
|
91
|
+
| { kind: "js"; name: string; alias?: string; loc: Loc };
|
|
92
|
+
|
|
93
|
+
export interface MetaEntry { key: string; value: Literal; loc: Loc }
|
|
94
|
+
|
|
95
|
+
export interface FunctionDef {
|
|
96
|
+
name: string;
|
|
97
|
+
params: ParamDecl[];
|
|
98
|
+
returnType?: VarType;
|
|
99
|
+
body: Child[];
|
|
100
|
+
loc: Loc;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface SinthFile {
|
|
104
|
+
filePath: string;
|
|
105
|
+
isPage: boolean;
|
|
106
|
+
imports: ImportNode[];
|
|
107
|
+
meta: MetaEntry[];
|
|
108
|
+
defs: CompDef[];
|
|
109
|
+
functions: FunctionDef[];
|
|
110
|
+
uses: CompUse[];
|
|
111
|
+
styles: StyleBlock[];
|
|
112
|
+
scripts: ScriptBlock[];
|
|
113
|
+
customEls: CustomElDecl[];
|
|
114
|
+
varDecls: VarDeclaration[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface MixedBlockEntry {
|
|
118
|
+
id: string;
|
|
119
|
+
conditionJS: string;
|
|
120
|
+
ifJS: string;
|
|
121
|
+
ifHTML: string;
|
|
122
|
+
elseJS: string;
|
|
123
|
+
elseHTML: string;
|
|
124
|
+
replaceId?: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface CompileCtx {
|
|
128
|
+
allDefs: Map<string, CompDef>;
|
|
129
|
+
customEls: Map<string, CustomElInfo>;
|
|
130
|
+
cssLinks: string[];
|
|
131
|
+
jsLinks: { src: string; attrs: Record<string, string> }[];
|
|
132
|
+
scopeHash: string;
|
|
133
|
+
pageFile: string;
|
|
134
|
+
extraCSS: string[];
|
|
135
|
+
mixedBlocks: MixedBlockEntry[];
|
|
136
|
+
mixedCounter: number;
|
|
137
|
+
logicBlocks: string[];
|
|
138
|
+
ifIdCounter: number;
|
|
139
|
+
exprRegistry: string[];
|
|
140
|
+
exprMap: Map<string, number>;
|
|
141
|
+
loopVars?: Set<string>;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export class SinthError extends Error {
|
|
145
|
+
constructor(
|
|
146
|
+
public readonly rawMsg: string,
|
|
147
|
+
public readonly loc?: Loc,
|
|
148
|
+
public readonly sourceLine?: string,
|
|
149
|
+
) {
|
|
150
|
+
super(SinthError.buildMessage(rawMsg, loc, sourceLine));
|
|
151
|
+
Object.setPrototypeOf(this, SinthError.prototype);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
static buildMessage(msg: string, loc?: Loc, sourceLine?: string): string {
|
|
155
|
+
let s = `Error: ${msg}`;
|
|
156
|
+
if (loc) {
|
|
157
|
+
s += `\n at: ${loc.file} (line ${loc.line}, col ${loc.col})`;
|
|
158
|
+
if (sourceLine !== undefined) {
|
|
159
|
+
const lineNum = String(loc.line).padStart(4);
|
|
160
|
+
const pointer = " ".repeat(Math.max(0, loc.col - 1)) + "^";
|
|
161
|
+
s += `\n${lineNum} | ${sourceLine}\n | ${pointer}`;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return s;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
withSource(lines: string[]): SinthError {
|
|
168
|
+
if (!this.loc || this.sourceLine !== undefined) return this;
|
|
169
|
+
const line = lines[this.loc.line - 1] ?? "";
|
|
170
|
+
return new SinthError(this.rawMsg, this.loc, line);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export class SinthWarning {
|
|
175
|
+
static emit(msg: string, loc?: Loc): void {
|
|
176
|
+
const where = loc ? `\n at: ${loc.file}:${loc.line}` : "";
|
|
177
|
+
process.stderr.write(`\x1b[33mWarning:\x1b[0m ${msg}${where}\n`);
|
|
178
|
+
}
|
|
179
|
+
}
|