@yannosay/sinth 1.11.6 → 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/bin/sinth.js +1 -4
- package/dist/browser/compile.js +62 -0
- package/dist/cli.js +469 -0
- package/dist/core/builtins.js +117 -0
- package/dist/core/cli-compiler.js +553 -0
- package/dist/core/compiler.js +1344 -0
- package/dist/core/expr.js +122 -0
- package/dist/core/head-builder.js +130 -0
- package/dist/core/lexer.js +414 -0
- package/dist/core/parser.js +1672 -0
- package/dist/core/runtime/custom-element.js +81 -0
- package/dist/core/runtime/functions.js +99 -0
- package/{src/core/runtime/helpers.ts → dist/core/runtime/helpers.js} +24 -34
- package/{src/core/runtime/render.ts → dist/core/runtime/render.js} +48 -63
- package/dist/core/style-processor.js +318 -0
- package/dist/core/types.js +93 -0
- package/dist/resolver.js +175 -0
- package/dist/server.js +168 -0
- package/dist/update-check.js +101 -0
- package/dist/utils.js +73 -0
- package/package.json +12 -8
- package/src/browser/compile.ts +0 -72
- package/src/cli.ts +0 -339
- package/src/core/builtins.ts +0 -117
- package/src/core/cli-compiler.ts +0 -514
- package/src/core/compiler.ts +0 -1351
- package/src/core/expr.ts +0 -113
- package/src/core/head-builder.ts +0 -86
- package/src/core/lexer.ts +0 -276
- package/src/core/parser.ts +0 -1533
- package/src/core/runtime/custom-element.ts +0 -84
- package/src/core/runtime/functions.ts +0 -87
- package/src/core/style-processor.ts +0 -299
- package/src/core/types.ts +0 -197
- package/src/resolver.ts +0 -150
- package/src/server.ts +0 -129
- package/src/utils.ts +0 -77
- package/tsconfig.json +0 -27
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { CustomElDecl, CompileCtx } from "../types.ts";
|
|
2
|
-
import { renderChild } from "../compiler.ts";
|
|
3
|
-
import { litToString } from "../../utils.ts";
|
|
4
|
-
|
|
5
|
-
export function compileCustomElement(cel: CustomElDecl, opts: { sharedRuntime: boolean }, hash: string, ctx: CompileCtx): string {
|
|
6
|
-
const componentId = `${cel.sinthName}_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
7
|
-
const exportTag = cel.exportTag ?? `sinth-el-${componentId.slice(-8)}`;
|
|
8
|
-
const varDecls = cel.varDecls;
|
|
9
|
-
|
|
10
|
-
const varLines = varDecls.map(v => {
|
|
11
|
-
if (v.value) {
|
|
12
|
-
const val = litToString(v.value);
|
|
13
|
-
if (v.varType === "int") return `this._${v.name} = ${parseFloat(val) || 0};`;
|
|
14
|
-
if (v.varType === "bool") return `this._${v.name} = ${val === "true" ? "true" : "false"};`;
|
|
15
|
-
return `this._${v.name} = ${JSON.stringify(val)};`;
|
|
16
|
-
}
|
|
17
|
-
const defaults: Record<string, string> = { str: '""', int: "0", bool: "false", "str[]": "[]" };
|
|
18
|
-
return `this._${v.name} = ${defaults[v.varType ?? "str"] ?? "undefined"};`;
|
|
19
|
-
}).join("\n ");
|
|
20
|
-
|
|
21
|
-
const childCtx: CompileCtx = {
|
|
22
|
-
...ctx,
|
|
23
|
-
exprRegistry: [],
|
|
24
|
-
exprMap: new Map(),
|
|
25
|
-
logicBlocks: [],
|
|
26
|
-
mixedBlocks: [],
|
|
27
|
-
customEls: new Map(),
|
|
28
|
-
scopePrefix: 'this._',
|
|
29
|
-
scopeVar: 'this',
|
|
30
|
-
ceActionHandlers: [],
|
|
31
|
-
varDecls: varDecls,
|
|
32
|
-
scopeHash: componentId,
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const customVarMap = new Map<string, string>();
|
|
36
|
-
for (const v of varDecls) {
|
|
37
|
-
customVarMap.set(v.name, v.name);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const bodyHTML = cel.body.map(c => renderChild(c, childCtx, customVarMap, 0)).join("\n");
|
|
41
|
-
|
|
42
|
-
const className = `CE_${componentId.replace(/[^a-zA-Z0-9]/g, '_')}`;
|
|
43
|
-
const escapedHTML = bodyHTML.replace(/`/g, '\\`').replace(/\$/g, '\\$');
|
|
44
|
-
|
|
45
|
-
return `(function(){
|
|
46
|
-
const tagName='${exportTag}';
|
|
47
|
-
if(customElements.get(tagName))return;
|
|
48
|
-
class ${className} extends HTMLElement{
|
|
49
|
-
constructor(){
|
|
50
|
-
super();
|
|
51
|
-
this.attachShadow({mode:'open'});
|
|
52
|
-
${varLines}
|
|
53
|
-
}
|
|
54
|
-
connectedCallback(){
|
|
55
|
-
this.shadowRoot.innerHTML=\`${escapedHTML}\`;
|
|
56
|
-
const btns=this.shadowRoot.querySelectorAll('button');
|
|
57
|
-
for(let i=0;i<btns.length;i++){
|
|
58
|
-
btns[i].onclick=()=>{
|
|
59
|
-
if(this._count!==undefined){
|
|
60
|
-
this._count++;
|
|
61
|
-
const spans=this.shadowRoot.querySelectorAll('[data-expr-id]');
|
|
62
|
-
for(let j=0;j<spans.length;j++){
|
|
63
|
-
const s=spans[j];
|
|
64
|
-
const id=s.getAttribute('data-expr-id');
|
|
65
|
-
if(id==='0'){
|
|
66
|
-
s.textContent=this._count;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
const spans=this.shadowRoot.querySelectorAll('[data-expr-id]');
|
|
73
|
-
for(let i=0;i<spans.length;i++){
|
|
74
|
-
const s=spans[i];
|
|
75
|
-
const id=s.getAttribute('data-expr-id');
|
|
76
|
-
if(id==='0'){
|
|
77
|
-
s.textContent=this._count;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
customElements.define(tagName,${className});
|
|
83
|
-
})();`;
|
|
84
|
-
}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { CompileCtx, FunctionDef, IfBlock, VarDeclaration, Expression } from "../types.ts";
|
|
2
|
-
import { compileExprToJS, compileIfToJS } from "../expr.ts";
|
|
3
|
-
|
|
4
|
-
export function compileFunctionDef(fn: FunctionDef, ctx: CompileCtx): string {
|
|
5
|
-
const paramsJS = fn.params.map(p => p.name).join(", ");
|
|
6
|
-
|
|
7
|
-
if (fn.returnType === "ui") {
|
|
8
|
-
return "";
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const bodyStatements: string[] = [];
|
|
12
|
-
|
|
13
|
-
for (const child of fn.body) {
|
|
14
|
-
switch (child.kind) {
|
|
15
|
-
case "text":
|
|
16
|
-
break;
|
|
17
|
-
case "expr":
|
|
18
|
-
bodyStatements.push(`${compileExprToJS(child.expression, undefined, ctx.namespace, ctx.declaredVars)};`);
|
|
19
|
-
break;
|
|
20
|
-
case "assign_stmt":
|
|
21
|
-
bodyStatements.push(`${compileExprToJS(child.expression, undefined, ctx.namespace, ctx.declaredVars)};`);
|
|
22
|
-
break;
|
|
23
|
-
case "if":
|
|
24
|
-
bodyStatements.push(compileIfToJS(child as IfBlock, undefined, ctx.namespace, ctx.declaredVars));
|
|
25
|
-
break;
|
|
26
|
-
case "for": {
|
|
27
|
-
const fl = child as any;
|
|
28
|
-
const itemVar = fl.itemVar;
|
|
29
|
-
const arrayVar = (ctx.namespace && ctx.declaredVars && ctx.declaredVars.has(fl.arrayVar)) ? ctx.namespace + "_" + fl.arrayVar : fl.arrayVar;
|
|
30
|
-
const bodyJS = fl.body.map((c: any) => {
|
|
31
|
-
if (c.kind === "assign_stmt") return `${compileExprToJS(c.expression, undefined, ctx.namespace, ctx.declaredVars)};`;
|
|
32
|
-
if (c.kind === "if") return compileIfToJS(c, undefined, ctx.namespace, ctx.declaredVars);
|
|
33
|
-
if (c.kind === "expr") return `${compileExprToJS(c.expression, undefined, ctx.namespace, ctx.declaredVars)};`;
|
|
34
|
-
return "";
|
|
35
|
-
}).filter(Boolean).join("\n");
|
|
36
|
-
const bodyLines = bodyJS.split("\n");
|
|
37
|
-
const indentedBody = bodyLines.map((line: string) => ` ${line}`).join("\n");
|
|
38
|
-
if (fl.indexVar) {
|
|
39
|
-
const alreadyDeclared = fn.body.some((bc: any) => bc.kind === "var" && bc.name === fl.indexVar);
|
|
40
|
-
if (!alreadyDeclared) {
|
|
41
|
-
bodyStatements.push(`let ${fl.indexVar} = 0;`);
|
|
42
|
-
}
|
|
43
|
-
bodyStatements.push(`for (let ${itemVar} of ${arrayVar}) {\n${indentedBody}\n ${fl.indexVar} = ${fl.indexVar} + 1;\n }`);
|
|
44
|
-
} else {
|
|
45
|
-
bodyStatements.push(`for (let ${itemVar} of ${arrayVar}) {\n${indentedBody}\n }`);
|
|
46
|
-
}
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
case "var": {
|
|
50
|
-
const vd = child as VarDeclaration;
|
|
51
|
-
let initJS: string;
|
|
52
|
-
if (vd.value) {
|
|
53
|
-
const lit = vd.value;
|
|
54
|
-
if (lit.kind === "str") {
|
|
55
|
-
if (lit.value.startsWith("__ARR__")) {
|
|
56
|
-
initJS = lit.value.slice(7);
|
|
57
|
-
} else if (lit.value.startsWith("__EXPR__")) {
|
|
58
|
-
try {
|
|
59
|
-
const innerExpr: Expression = JSON.parse(lit.value.substring(8));
|
|
60
|
-
initJS = compileExprToJS(innerExpr, undefined, ctx.namespace, ctx.declaredVars);
|
|
61
|
-
} catch { initJS = JSON.stringify(lit.value); }
|
|
62
|
-
} else {
|
|
63
|
-
initJS = JSON.stringify(lit.value);
|
|
64
|
-
}
|
|
65
|
-
} else if (lit.kind === "num") {
|
|
66
|
-
initJS = String(lit.value);
|
|
67
|
-
} else if (lit.kind === "bool") {
|
|
68
|
-
initJS = String(lit.value);
|
|
69
|
-
} else if (lit.kind === "null") {
|
|
70
|
-
initJS = "null";
|
|
71
|
-
} else {
|
|
72
|
-
initJS = "null";
|
|
73
|
-
}
|
|
74
|
-
} else {
|
|
75
|
-
const defaults: Record<string, string> = { str: '""', int: "0", bool: "false", "str[]": "[]", obj: "{}" };
|
|
76
|
-
initJS = defaults[vd.varType] ?? "undefined";
|
|
77
|
-
}
|
|
78
|
-
bodyStatements.push(`let ${vd.name} = ${initJS};`);
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
case "return":
|
|
82
|
-
bodyStatements.push(`return ${child.expression ? compileExprToJS(child.expression, undefined, ctx.namespace, ctx.declaredVars) : ""};`);
|
|
83
|
-
break;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return `function ${fn.name}(${paramsJS}) {\n${bodyStatements.map(s => ` ${s}`).join("\n")}\n}`;
|
|
87
|
-
}
|
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
import { BUILTIN_MAP } from "./builtins.ts";
|
|
2
|
-
import { SinthError, SinthWarning, StyleBlock } from "./types.ts";
|
|
3
|
-
import { camelToKebab, interpolateAttr } from "../utils.ts";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* for who's reading this: this maps Sinth Style pseudo-class shorthand keywords to real CSS pseudo-classes.
|
|
9
|
-
* these are used inside component style blocks, for example: onHover { color: "blue" }
|
|
10
|
-
*/
|
|
11
|
-
export const SINTH_PSEUDO_CLASS: Record<string, string> = {
|
|
12
|
-
onHover: ":hover",
|
|
13
|
-
onFocus: ":focus",
|
|
14
|
-
onActive: ":active",
|
|
15
|
-
onVisited: ":visited",
|
|
16
|
-
onChecked: ":checked",
|
|
17
|
-
onDisabled: ":disabled",
|
|
18
|
-
onEnabled: ":enabled",
|
|
19
|
-
onRequired: ":required",
|
|
20
|
-
onOptional: ":optional",
|
|
21
|
-
onValid: ":valid",
|
|
22
|
-
onInvalid: ":invalid",
|
|
23
|
-
onPlaceholderShown:":placeholder-shown",
|
|
24
|
-
onFocusWithin: ":focus-within",
|
|
25
|
-
onFocusVisible: ":focus-visible",
|
|
26
|
-
onFirst: ":first-child",
|
|
27
|
-
onLast: ":last-child",
|
|
28
|
-
onFirstOfType: ":first-of-type",
|
|
29
|
-
onLastOfType: ":last-of-type",
|
|
30
|
-
onEmpty: ":empty",
|
|
31
|
-
onTarget: ":target",
|
|
32
|
-
onLink: ":link",
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export const SINTH_PSEUDO_ELEMENT: Record<string, string> = {
|
|
36
|
-
before: "::before",
|
|
37
|
-
after: "::after",
|
|
38
|
-
placeholder: "::placeholder",
|
|
39
|
-
selection: "::selection",
|
|
40
|
-
firstLine: "::first-line",
|
|
41
|
-
firstLetter: "::first-letter",
|
|
42
|
-
marker: "::marker",
|
|
43
|
-
backdrop: "::backdrop",
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export function sinthCompToSelector(name: string): string {
|
|
47
|
-
const info = BUILTIN_MAP[name];
|
|
48
|
-
if (!info) return name.toLowerCase();
|
|
49
|
-
if (info.defaultClass) {
|
|
50
|
-
const classes = info.defaultClass.split(" ").map(c => `.${c}`).join("");
|
|
51
|
-
return `${info.tag}${classes}`;
|
|
52
|
-
}
|
|
53
|
-
return info.tag;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* supported conversions:
|
|
58
|
-
* - component names as selectors: `Paragraph { ... }` → `p { ... }`
|
|
59
|
-
* - pseudo-class shorthands: `onHover { ... }` → `&:hover { ... }`
|
|
60
|
-
* - pseudo-element shorthands: `before { ... }` → `&::before { ... }`
|
|
61
|
-
* - media queries: `media(maxWidth: "600px") { ... }` → `@media (max-width: 600px) { ... }`
|
|
62
|
-
* - CSS custom properties: `var --primary: "#3b82f6"` → `--primary: #3b82f6;`
|
|
63
|
-
*/
|
|
64
|
-
export function preprocessSinthStyle(raw: string): string {
|
|
65
|
-
const lines = raw.split("\n");
|
|
66
|
-
const result: string[] = [];
|
|
67
|
-
|
|
68
|
-
for (const line of lines) {
|
|
69
|
-
const trimmed = line.trim();
|
|
70
|
-
const indent = line.match(/^(\s*)/)?.[1] ?? "";
|
|
71
|
-
|
|
72
|
-
if (!trimmed) { result.push(line); continue; }
|
|
73
|
-
|
|
74
|
-
if (trimmed.startsWith("--") && !trimmed.startsWith("--[")) { continue; }
|
|
75
|
-
// Standard comments pass through
|
|
76
|
-
if (trimmed.startsWith("//") || trimmed.startsWith("/*") || trimmed.startsWith("*")) {
|
|
77
|
-
result.push(line); continue;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const mediaMatch = trimmed.match(/^media\s*\(([^)]+)\)\s*(\{?\s*)$/);
|
|
81
|
-
if (mediaMatch) {
|
|
82
|
-
const params = parseSinthMediaParams(mediaMatch[1]);
|
|
83
|
-
result.push(`${indent}@media (${params}) {`);
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const blockMatch = trimmed.match(/^([a-zA-Z][a-zA-Z0-9]*)(\s*\{\s*)$/);
|
|
88
|
-
if (blockMatch) {
|
|
89
|
-
const keyword = blockMatch[1];
|
|
90
|
-
|
|
91
|
-
const pseudoCls = SINTH_PSEUDO_CLASS[keyword];
|
|
92
|
-
if (pseudoCls) {
|
|
93
|
-
result.push(`${indent}&${pseudoCls} {`);
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const pseudoEl = SINTH_PSEUDO_ELEMENT[keyword];
|
|
98
|
-
if (pseudoEl) {
|
|
99
|
-
result.push(`${indent}&${pseudoEl} {`);
|
|
100
|
-
continue;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (/^[A-Z]/.test(keyword) && BUILTIN_MAP[keyword]) {
|
|
104
|
-
result.push(`${indent}${sinthCompToSelector(keyword)} {`);
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const varPropMatch = trimmed.match(/^var\s+(--[a-zA-Z][a-zA-Z0-9-]*)\s*:\s*["']?([^"';]+)["']?\s*;?\s*$/);
|
|
110
|
-
if (varPropMatch) {
|
|
111
|
-
result.push(`${indent}${varPropMatch[1]}: ${varPropMatch[2].trim()};`);
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
result.push(line);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return result.join("\n");
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function parseSinthMediaParams(params: string): string {
|
|
122
|
-
const parts = params.split(",").map(p => {
|
|
123
|
-
const m = p.trim().match(/^([a-zA-Z]+)\s*:\s*["']?([^"',]+)["']?\s*$/);
|
|
124
|
-
if (!m) return p.trim();
|
|
125
|
-
return `${camelToKebab(m[1])}: ${m[2].trim()}`;
|
|
126
|
-
});
|
|
127
|
-
return parts.join(") and (");
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// style processor
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
export function convertCSSProps(css: string): string {
|
|
134
|
-
return css.split("\n").map(line => {
|
|
135
|
-
const trimmed = line.trim();
|
|
136
|
-
if (!trimmed) return line;
|
|
137
|
-
if (trimmed === "}" || trimmed.startsWith("}")) return line;
|
|
138
|
-
if (trimmed.startsWith("//") || trimmed.startsWith("/*") || trimmed.startsWith("*")) return line;
|
|
139
|
-
if (trimmed.startsWith("@")) return line;
|
|
140
|
-
if (trimmed.includes("{")) return line;
|
|
141
|
-
|
|
142
|
-
const m = line.match(/^(\s*)([a-zA-Z][a-zA-Z0-9-]*)(\s*:\s*)(.+?)(\s*;?\s*)$/);
|
|
143
|
-
if (!m) return line;
|
|
144
|
-
|
|
145
|
-
const [, indent, rawProp, , rawVal] = m;
|
|
146
|
-
|
|
147
|
-
const hasCamelCase = /[a-z][A-Z]/.test(rawProp) || /^[A-Z]/.test(rawProp);
|
|
148
|
-
const hasQuotedVal = /^["']/.test(rawVal.trim());
|
|
149
|
-
if (!hasCamelCase && !hasQuotedVal) return line;
|
|
150
|
-
|
|
151
|
-
let val = rawVal.trim();
|
|
152
|
-
if (val.endsWith(";")) val = val.slice(0, -1).trimEnd();
|
|
153
|
-
if ((val.startsWith('"') && val.endsWith('"')) ||
|
|
154
|
-
(val.startsWith("'") && val.endsWith("'"))) {
|
|
155
|
-
val = val.slice(1, -1);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return `${indent}${camelToKebab(rawProp)}: ${val};`;
|
|
159
|
-
}).join("\n");
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export function processStyleBlock(
|
|
163
|
-
block: StyleBlock,
|
|
164
|
-
hash: string,
|
|
165
|
-
params: Map<string, string> = new Map(),
|
|
166
|
-
): string {
|
|
167
|
-
let raw = block.raw;
|
|
168
|
-
|
|
169
|
-
// 1. interpolate {param} placeholders
|
|
170
|
-
if (params.size > 0) raw = interpolateAttr(raw, params);
|
|
171
|
-
|
|
172
|
-
// 2. sinth style preprocessing
|
|
173
|
-
raw = preprocessSinthStyle(raw);
|
|
174
|
-
|
|
175
|
-
// 3. warn if & in plain CSS
|
|
176
|
-
if (block.lang === "css" && raw.includes("&")) {
|
|
177
|
-
SinthWarning.emit(
|
|
178
|
-
`'&' (CSS nesting) found in style block. Nested selectors require a modern browser or lang="scss".`,
|
|
179
|
-
block.loc,
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// 4. camelCase → kebab-case
|
|
184
|
-
let css = convertCSSProps(raw);
|
|
185
|
-
|
|
186
|
-
// 5. SCSS compilation
|
|
187
|
-
if (block.lang === "scss") {
|
|
188
|
-
try {
|
|
189
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
190
|
-
const sass: { compileString: (src: string) => { css: string } } = require("sass");
|
|
191
|
-
css = sass.compileString(css).css;
|
|
192
|
-
} catch (e: unknown) {
|
|
193
|
-
const err = e as { code?: string; message?: string };
|
|
194
|
-
if (err.code === "MODULE_NOT_FOUND" || err.message?.includes("Cannot find module")) {
|
|
195
|
-
throw new SinthError("SCSS requires the 'sass' package. Install with: npm install sass");
|
|
196
|
-
}
|
|
197
|
-
throw new SinthError(`SCSS compilation failed: ${err.message ?? String(e)}`);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// 6. if target component given, wrap content in its CSS selector
|
|
202
|
-
if (block.target) {
|
|
203
|
-
const selector = sinthCompToSelector(block.target);
|
|
204
|
-
css = `${selector} {\n${css}\n}`;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// 7. scope (except it's global)
|
|
208
|
-
if (!block.global) css = scopeCSS(css, hash);
|
|
209
|
-
|
|
210
|
-
return css;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export function scopeCSS(css: string, hash: string): string {
|
|
214
|
-
return processRules(css, `[data-s="${hash}"]`);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
export function processRules(css: string, attr: string): string {
|
|
218
|
-
let out = "", i = 0;
|
|
219
|
-
|
|
220
|
-
while (i < css.length) {
|
|
221
|
-
if (/\s/.test(css[i])) { out += css[i++]; continue; }
|
|
222
|
-
|
|
223
|
-
if (css[i] === "/" && css[i + 1] === "*") {
|
|
224
|
-
const end = css.indexOf("*/", i + 2);
|
|
225
|
-
if (end === -1) { out += css.substring(i); break; }
|
|
226
|
-
out += css.substring(i, end + 2);
|
|
227
|
-
i = end + 2;
|
|
228
|
-
continue;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const braceIdx = findCSSBrace(css, i);
|
|
232
|
-
if (braceIdx === -1) { out += css.substring(i); break; }
|
|
233
|
-
|
|
234
|
-
const selector = css.substring(i, braceIdx).trim();
|
|
235
|
-
const { content, end } = extractCSSBlock(css, braceIdx);
|
|
236
|
-
|
|
237
|
-
if (/^@(-webkit-|-moz-)?keyframes/.test(selector)) {
|
|
238
|
-
out += `${selector} {${content}}\n`;
|
|
239
|
-
} else if (/^@(media|supports|layer|container)/.test(selector)) {
|
|
240
|
-
out += `${selector} {\n${processRules(content, attr)}\n}\n`;
|
|
241
|
-
} else if (selector.startsWith("@")) {
|
|
242
|
-
out += `${selector} {${content}}\n`;
|
|
243
|
-
} else if (selector.length > 0) {
|
|
244
|
-
out += `${scopeSelectors(selector, attr)} {${content}}\n`;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
i = end;
|
|
248
|
-
}
|
|
249
|
-
return out;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export function scopeSelectors(selList: string, attr: string): string {
|
|
253
|
-
return selList
|
|
254
|
-
.split(",")
|
|
255
|
-
.map(s => {
|
|
256
|
-
s = s.trim();
|
|
257
|
-
if (!s) return "";
|
|
258
|
-
if (/^(html|body|:root)(\s|$|{|,)/.test(s)) return s;
|
|
259
|
-
const m = s.match(/^(.*?)((?::{1,2}[a-zA-Z-]+(?:\([^)]*\))?)+)$/);
|
|
260
|
-
if (m && m[1].trim()) {
|
|
261
|
-
return `${attr} ${m[1].trimEnd()}${m[2]}, ${m[1].trimEnd()}${attr}${m[2]}`;
|
|
262
|
-
}
|
|
263
|
-
return `${attr} ${s}, ${s}${attr}`;
|
|
264
|
-
})
|
|
265
|
-
.filter(Boolean)
|
|
266
|
-
.join(",\n");
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export function findCSSBrace(css: string, from: number): number {
|
|
270
|
-
let i = from;
|
|
271
|
-
while (i < css.length) {
|
|
272
|
-
if (css[i] === '"' || css[i] === "'") {
|
|
273
|
-
const q = css[i++];
|
|
274
|
-
while (i < css.length && css[i] !== q) { if (css[i] === "\\") i++; i++; }
|
|
275
|
-
i++;
|
|
276
|
-
} else if (css[i] === "{") {
|
|
277
|
-
return i;
|
|
278
|
-
} else { i++; }
|
|
279
|
-
}
|
|
280
|
-
return -1;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
export function extractCSSBlock(css: string, openBrace: number): { content: string; end: number } {
|
|
284
|
-
let i = openBrace + 1, depth = 1;
|
|
285
|
-
while (i < css.length && depth > 0) {
|
|
286
|
-
if (css[i] === '"' || css[i] === "'") {
|
|
287
|
-
const q = css[i++];
|
|
288
|
-
while (i < css.length && css[i] !== q) { if (css[i] === "\\") i++; i++; }
|
|
289
|
-
i++;
|
|
290
|
-
} else if (css[i] === "/" && css[i + 1] === "*") {
|
|
291
|
-
i += 2;
|
|
292
|
-
while (i < css.length - 1 && !(css[i] === "*" && css[i + 1] === "/")) i++;
|
|
293
|
-
i += 2;
|
|
294
|
-
} else if (css[i] === "{") { depth++; i++; }
|
|
295
|
-
else if (css[i] === "}") { depth--; if (depth > 0) i++; else break; }
|
|
296
|
-
else { i++; }
|
|
297
|
-
}
|
|
298
|
-
return { content: css.substring(openBrace + 1, i), end: i + 1 };
|
|
299
|
-
}
|
package/src/core/types.ts
DELETED
|
@@ -1,197 +0,0 @@
|
|
|
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, KW_RETURN,
|
|
23
|
-
OP_PLUS, OP_MINUS, OP_STAR, OP_SLASH, OP_MOD, OP_SEMI,
|
|
24
|
-
OP_LT, OP_GT, OP_NEQ, OP_EQEQ, OP_LTEQ, OP_GTEQ,
|
|
25
|
-
OP_ARROW,
|
|
26
|
-
OP_NOT,
|
|
27
|
-
EOF,
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface Token { type: TT; value: string; loc: Loc }
|
|
31
|
-
|
|
32
|
-
export type LitStr = { kind: "str"; value: string };
|
|
33
|
-
export type LitNum = { kind: "num"; value: number };
|
|
34
|
-
export type LitBool = { kind: "bool"; value: boolean };
|
|
35
|
-
export type LitNull = { kind: "null" };
|
|
36
|
-
export type Literal = LitStr | LitNum | LitBool | LitNull;
|
|
37
|
-
|
|
38
|
-
export type UnaryOp = "not" | "-";
|
|
39
|
-
export type BinaryOp = "+" | "-" | "*" | "/" | "%" | "==" | "!=" | "<" | ">" | "<=" | ">=" | "and" | "or";
|
|
40
|
-
export type AssignOp = "=" | "+=" | "-=";
|
|
41
|
-
export type PostfixOp = "++" | "--";
|
|
42
|
-
|
|
43
|
-
export interface Expression {
|
|
44
|
-
kind: "literal" | "variable" | "unary" | "binary" | "assign" | "postfix" | "index" | "call" | "expr_ref";
|
|
45
|
-
memo?: boolean;
|
|
46
|
-
value?: Literal;
|
|
47
|
-
name?: string;
|
|
48
|
-
op?: UnaryOp | BinaryOp | AssignOp | PostfixOp;
|
|
49
|
-
operand?: Expression;
|
|
50
|
-
left?: Expression;
|
|
51
|
-
right?: Expression;
|
|
52
|
-
target?: string;
|
|
53
|
-
object?: Expression;
|
|
54
|
-
key?: Expression;
|
|
55
|
-
callee?: Expression;
|
|
56
|
-
args?: Expression[];
|
|
57
|
-
exprId?: number; // for "expr_ref" kind
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface Attr { name: string; value: Literal | null; loc: Loc }
|
|
61
|
-
export interface TextNode { kind: "text"; value: string; loc: Loc }
|
|
62
|
-
export interface CompUse { kind: "use"; name: string; attrs: Attr[]; children: Child[]; loc: Loc }
|
|
63
|
-
export interface ExprNode { kind: "expr"; expression: Expression; loc: Loc }
|
|
64
|
-
export interface AssignStmt { kind: "assign_stmt"; expression: Expression; loc: Loc }
|
|
65
|
-
export interface IfBlock { kind: "if"; condition: Expression; body: Child[]; elseBody?: Child[]; persist?: boolean; loc: Loc }
|
|
66
|
-
export interface ForLoop { kind: "for"; keyVar?: string; itemVar: string; indexVar?: string; arrayVar: string; body: Child[]; loc: Loc }
|
|
67
|
-
export interface RemoveStmt { kind: "remove"; target: string; loc: Loc }
|
|
68
|
-
export interface ReturnStmt { kind: "return"; expression?: Expression; loc: Loc }
|
|
69
|
-
export interface ComponentExpr { kind: "component_expr"; children: Child[]; loc: Loc }
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
export type Child = TextNode | CompUse | ExprNode | AssignStmt | IfBlock | ForLoop | RemoveStmt | ReturnStmt | ComponentExpr | VarDeclaration;
|
|
73
|
-
|
|
74
|
-
export interface ParamDecl { name: string; paramType?: VarType; defaultVal?: Literal; loc: Loc }
|
|
75
|
-
|
|
76
|
-
export interface StyleBlock {
|
|
77
|
-
global: boolean;
|
|
78
|
-
lang: "css" | "scss";
|
|
79
|
-
raw: string;
|
|
80
|
-
target?: string;
|
|
81
|
-
extraAttrs: Record<string, string>;
|
|
82
|
-
loc: Loc;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export interface ScriptBlock { raw: string; attrs: Record<string, string>; loc: Loc }
|
|
86
|
-
export interface CompDef { name: string; params: ParamDecl[]; body: Child[]; styles: StyleBlock[]; scripts: ScriptBlock[]; loc: Loc }
|
|
87
|
-
export interface CustomElDecl { sinthName: string; tagName: string; exportTag?: string; params: ParamDecl[]; body: Child[]; styles: StyleBlock[]; scripts: ScriptBlock[]; varDecls: VarDeclaration[]; loc: Loc
|
|
88
|
-
}export interface CustomElInfo { tagName: string; params: ParamDecl[] }
|
|
89
|
-
|
|
90
|
-
export type VarType = "int" | "str" | "bool" | "str[]" | "obj" | "ui";
|
|
91
|
-
export interface VarDeclaration { kind: "var"; name: string; varType: VarType; value: Literal | null; loc: Loc }
|
|
92
|
-
|
|
93
|
-
export type ImportNode =
|
|
94
|
-
| { kind: "sinth"; path: string; loc: Loc }
|
|
95
|
-
| { kind: "css"; path: string; loc: Loc }
|
|
96
|
-
| { kind: "js"; name: string; alias?: string; loc: Loc };
|
|
97
|
-
|
|
98
|
-
export interface MetaEntry { key: string; value: Literal; loc: Loc }
|
|
99
|
-
|
|
100
|
-
export interface FunctionDef {
|
|
101
|
-
name: string;
|
|
102
|
-
params: ParamDecl[];
|
|
103
|
-
returnType?: VarType;
|
|
104
|
-
body: Child[];
|
|
105
|
-
loc: Loc;
|
|
106
|
-
}
|
|
107
|
-
export interface SinthFile {
|
|
108
|
-
filePath: string;
|
|
109
|
-
isPage: boolean;
|
|
110
|
-
imports: ImportNode[];
|
|
111
|
-
meta: MetaEntry[];
|
|
112
|
-
defs: CompDef[];
|
|
113
|
-
functions: FunctionDef[];
|
|
114
|
-
uses: CompUse[];
|
|
115
|
-
styles: StyleBlock[];
|
|
116
|
-
scripts: ScriptBlock[];
|
|
117
|
-
customEls: CustomElDecl[];
|
|
118
|
-
varDecls: VarDeclaration[];
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export interface MixedBlockEntry {
|
|
122
|
-
id: string;
|
|
123
|
-
conditionJS: string;
|
|
124
|
-
ifJS: string;
|
|
125
|
-
ifHTML: string;
|
|
126
|
-
elseJS: string;
|
|
127
|
-
elseHTML: string;
|
|
128
|
-
replaceId?: string;
|
|
129
|
-
persist?: boolean;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export interface CompileCtx {
|
|
133
|
-
allDefs: Map<string, CompDef>;
|
|
134
|
-
functionDefs: FunctionDef[];
|
|
135
|
-
customEls: Map<string, CustomElInfo>;
|
|
136
|
-
cssLinks: string[];
|
|
137
|
-
jsLinks: { src: string; attrs: Record<string, string> }[];
|
|
138
|
-
scopeHash: string;
|
|
139
|
-
pageFile: string;
|
|
140
|
-
extraCSS: string[];
|
|
141
|
-
mixedBlocks: MixedBlockEntry[];
|
|
142
|
-
mixedCounter: number;
|
|
143
|
-
logicBlocks: string[];
|
|
144
|
-
ifIdCounter: number;
|
|
145
|
-
exprRegistry: string[];
|
|
146
|
-
exprMap: Map<string, number>;
|
|
147
|
-
varDecls?: VarDeclaration[];
|
|
148
|
-
actionButtons: { qid: string; delayMs: number; hide: boolean }[];
|
|
149
|
-
loopVars?: Set<string>;
|
|
150
|
-
diffingEnabled?: boolean;
|
|
151
|
-
declaredVars?: Set<string>;
|
|
152
|
-
|
|
153
|
-
// custom elements
|
|
154
|
-
scopePrefix?: string;
|
|
155
|
-
scopeVar?: string;
|
|
156
|
-
ceActionHandlers?: string[];
|
|
157
|
-
|
|
158
|
-
// web
|
|
159
|
-
namespace?: string;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export class SinthError extends Error {
|
|
163
|
-
constructor(
|
|
164
|
-
public readonly rawMsg: string,
|
|
165
|
-
public readonly loc?: Loc,
|
|
166
|
-
public readonly sourceLine?: string,
|
|
167
|
-
) {
|
|
168
|
-
super(SinthError.buildMessage(rawMsg, loc, sourceLine));
|
|
169
|
-
Object.setPrototypeOf(this, SinthError.prototype);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
static buildMessage(msg: string, loc?: Loc, sourceLine?: string): string {
|
|
173
|
-
let s = `Error: ${msg}`;
|
|
174
|
-
if (loc) {
|
|
175
|
-
s += `\n at: ${loc.file} (line ${loc.line}, col ${loc.col})`;
|
|
176
|
-
if (sourceLine !== undefined) {
|
|
177
|
-
const lineNum = String(loc.line).padStart(4);
|
|
178
|
-
const pointer = " ".repeat(Math.max(0, loc.col - 1)) + "^";
|
|
179
|
-
s += `\n${lineNum} | ${sourceLine}\n | ${pointer}`;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
return s;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
withSource(lines: string[]): SinthError {
|
|
186
|
-
if (!this.loc || this.sourceLine !== undefined) return this;
|
|
187
|
-
const line = lines[this.loc.line - 1] ?? "";
|
|
188
|
-
return new SinthError(this.rawMsg, this.loc, line);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
export class SinthWarning {
|
|
193
|
-
static emit(msg: string, loc?: Loc): void {
|
|
194
|
-
const where = loc ? `\n at: ${loc.file}:${loc.line}` : "";
|
|
195
|
-
process.stderr.write(`\x1b[33mWarning:\x1b[0m ${msg}${where}\n`);
|
|
196
|
-
}
|
|
197
|
-
}
|