@yannosay/sinth 1.11.6 → 1.12.1
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
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compileExprToJS = compileExprToJS;
|
|
4
|
+
exports.compileIfToJS = compileIfToJS;
|
|
5
|
+
exports.bodyToJS = bodyToJS;
|
|
6
|
+
exports.scopeVariableName = scopeVariableName;
|
|
7
|
+
function compileExprToJS(expr, loopVars, namespace, declaredVars) {
|
|
8
|
+
switch (expr.kind) {
|
|
9
|
+
case "literal":
|
|
10
|
+
if (!expr.value)
|
|
11
|
+
return "null";
|
|
12
|
+
if (expr.value.kind === "str") {
|
|
13
|
+
const sv = expr.value.value;
|
|
14
|
+
if (sv.startsWith("__ARR__"))
|
|
15
|
+
return sv.slice(7);
|
|
16
|
+
if (sv.startsWith("__VAR__"))
|
|
17
|
+
return sv.slice(7);
|
|
18
|
+
return JSON.stringify(sv);
|
|
19
|
+
}
|
|
20
|
+
if (expr.value.kind === "num")
|
|
21
|
+
return String(expr.value.value);
|
|
22
|
+
if (expr.value.kind === "bool")
|
|
23
|
+
return String(expr.value.value);
|
|
24
|
+
return "null";
|
|
25
|
+
case "variable":
|
|
26
|
+
if (loopVars && expr.name) {
|
|
27
|
+
const dotIdx = expr.name.indexOf('.');
|
|
28
|
+
const root = dotIdx !== -1 ? expr.name.substring(0, dotIdx) : expr.name;
|
|
29
|
+
if (loopVars.has(root)) {
|
|
30
|
+
return `_ctx.${expr.name}`;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (namespace && declaredVars && expr.name) {
|
|
34
|
+
const dotIdx2 = expr.name.indexOf('.');
|
|
35
|
+
const root2 = dotIdx2 !== -1 ? expr.name.substring(0, dotIdx2) : expr.name;
|
|
36
|
+
if (declaredVars.has(root2)) {
|
|
37
|
+
return namespace + "_" + expr.name;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return expr.name ?? "undefined";
|
|
41
|
+
case "binary": {
|
|
42
|
+
const l = compileExprToJS(expr.left, loopVars, namespace, declaredVars);
|
|
43
|
+
const r = compileExprToJS(expr.right, loopVars, namespace, declaredVars);
|
|
44
|
+
const o = expr.op === "and" ? "&&" : expr.op === "or" ? "||" : expr.op;
|
|
45
|
+
return `(${l} ${o} ${r})`;
|
|
46
|
+
}
|
|
47
|
+
case "unary": {
|
|
48
|
+
const o = expr.op === "not" ? "!" : expr.op;
|
|
49
|
+
return `${o}(${compileExprToJS(expr.operand, loopVars, namespace, declaredVars)})`;
|
|
50
|
+
}
|
|
51
|
+
case "assign": {
|
|
52
|
+
const v = expr.right ? compileExprToJS(expr.right, loopVars, namespace, declaredVars) : "null";
|
|
53
|
+
let t = expr.target ?? "undefined";
|
|
54
|
+
if (namespace && declaredVars && expr.target) {
|
|
55
|
+
const dotIdx = expr.target.indexOf('.');
|
|
56
|
+
const root = dotIdx !== -1 ? expr.target.substring(0, dotIdx) : expr.target;
|
|
57
|
+
if (declaredVars.has(root)) {
|
|
58
|
+
t = namespace + "_" + expr.target;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return `${t} ${expr.op} ${v}`;
|
|
62
|
+
}
|
|
63
|
+
case "index":
|
|
64
|
+
return `${compileExprToJS(expr.object, loopVars, namespace, declaredVars)}[${compileExprToJS(expr.key, loopVars, namespace, declaredVars)}]`;
|
|
65
|
+
case "postfix": {
|
|
66
|
+
let t2 = expr.target ?? "undefined";
|
|
67
|
+
if (namespace && declaredVars && expr.target) {
|
|
68
|
+
const dotIdx = expr.target.indexOf('.');
|
|
69
|
+
const root = dotIdx !== -1 ? expr.target.substring(0, dotIdx) : expr.target;
|
|
70
|
+
if (declaredVars.has(root)) {
|
|
71
|
+
t2 = namespace + "_" + expr.target;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return `${t2}${expr.op}`;
|
|
75
|
+
}
|
|
76
|
+
case "call": {
|
|
77
|
+
const callee = compileExprToJS(expr.callee, loopVars, namespace, declaredVars);
|
|
78
|
+
if (callee === "remove") {
|
|
79
|
+
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars, namespace, declaredVars) : "''";
|
|
80
|
+
return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
|
|
81
|
+
}
|
|
82
|
+
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars, namespace, declaredVars)).join(", ");
|
|
83
|
+
if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
|
|
84
|
+
return callee;
|
|
85
|
+
}
|
|
86
|
+
return `${callee}(${args})`;
|
|
87
|
+
}
|
|
88
|
+
default:
|
|
89
|
+
return "";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function compileIfToJS(ifBlock, loopVars, namespace, declaredVars) {
|
|
93
|
+
const cond = compileExprToJS(ifBlock.condition, loopVars, namespace, declaredVars);
|
|
94
|
+
const ifJS = bodyToJS(ifBlock.body, loopVars, namespace, declaredVars);
|
|
95
|
+
const elseJS = ifBlock.elseBody ? bodyToJS(ifBlock.elseBody, loopVars, namespace, declaredVars) : "";
|
|
96
|
+
let js = `if (${cond}) {\n${ifJS}}\n`;
|
|
97
|
+
if (elseJS)
|
|
98
|
+
js += `else {\n${elseJS}}\n`;
|
|
99
|
+
return js;
|
|
100
|
+
}
|
|
101
|
+
function bodyToJS(children, loopVars, namespace, declaredVars) {
|
|
102
|
+
return children
|
|
103
|
+
.filter(c => c.kind === "assign_stmt" || c.kind === "if" || c.kind === "return" || c.kind === "expr")
|
|
104
|
+
.map(c => {
|
|
105
|
+
if (c.kind === "assign_stmt")
|
|
106
|
+
return ` ${compileExprToJS(c.expression, loopVars, namespace, declaredVars)};\n`;
|
|
107
|
+
if (c.kind === "return")
|
|
108
|
+
return ` return ${c.expression ? compileExprToJS(c.expression, loopVars, namespace, declaredVars) : ""};\n`;
|
|
109
|
+
if (c.kind === "if")
|
|
110
|
+
return compileIfToJS(c, loopVars, namespace, declaredVars).replace(/^/gm, " ") + "\n";
|
|
111
|
+
if (c.kind === "expr")
|
|
112
|
+
return ` ${compileExprToJS(c.expression, loopVars, namespace, declaredVars)};\n`;
|
|
113
|
+
return "";
|
|
114
|
+
})
|
|
115
|
+
.join("");
|
|
116
|
+
}
|
|
117
|
+
function scopeVariableName(name, ctx) {
|
|
118
|
+
if (ctx?.scopePrefix && ctx?.scopeVar && ctx?.varDecls?.some(v => v.name === name)) {
|
|
119
|
+
return `${ctx.scopePrefix}${name}`;
|
|
120
|
+
}
|
|
121
|
+
return name;
|
|
122
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.buildHeadData = buildHeadData;
|
|
37
|
+
exports.faviconType = faviconType;
|
|
38
|
+
exports.renderHead = renderHead;
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const types_1 = require("./types");
|
|
41
|
+
const utils_1 = require("../utils");
|
|
42
|
+
function buildHeadData(meta) {
|
|
43
|
+
let title, fav;
|
|
44
|
+
let lang = "en", charset = "UTF-8", viewport = "width=device-width, initial-scale=1.0";
|
|
45
|
+
const metaTags = [];
|
|
46
|
+
const metaProps = [];
|
|
47
|
+
for (const m of meta) {
|
|
48
|
+
const val = (0, utils_1.litToString)(m.value);
|
|
49
|
+
switch (m.key) {
|
|
50
|
+
case "title":
|
|
51
|
+
title = val;
|
|
52
|
+
break;
|
|
53
|
+
case "fav":
|
|
54
|
+
fav = val;
|
|
55
|
+
break;
|
|
56
|
+
case "lang":
|
|
57
|
+
lang = val;
|
|
58
|
+
break;
|
|
59
|
+
case "charset":
|
|
60
|
+
charset = val;
|
|
61
|
+
break;
|
|
62
|
+
case "viewport":
|
|
63
|
+
viewport = val;
|
|
64
|
+
break;
|
|
65
|
+
case "descr":
|
|
66
|
+
metaTags.push({ name: "description", content: val });
|
|
67
|
+
break;
|
|
68
|
+
case "author":
|
|
69
|
+
metaTags.push({ name: "author", content: val });
|
|
70
|
+
break;
|
|
71
|
+
case "keywords":
|
|
72
|
+
metaTags.push({ name: "keywords", content: val });
|
|
73
|
+
break;
|
|
74
|
+
case "robots":
|
|
75
|
+
metaTags.push({ name: "robots", content: val });
|
|
76
|
+
break;
|
|
77
|
+
default:
|
|
78
|
+
if (m.key.startsWith("og") || m.key.startsWith("twitter")) {
|
|
79
|
+
const prop = m.key
|
|
80
|
+
.replace(/^og([A-Z])/, (_, c) => `og:${c.toLowerCase()}`)
|
|
81
|
+
.replace(/^twitter([A-Z])/, (_, c) => `twitter:${c.toLowerCase()}`);
|
|
82
|
+
metaProps.push({ property: prop, content: val });
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
metaTags.push({ name: (0, utils_1.camelToKebab)(m.key), content: val });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return { title, fav, lang, charset, viewport, metaTags, metaProps };
|
|
90
|
+
}
|
|
91
|
+
function faviconType(p) {
|
|
92
|
+
switch (path.extname(p).toLowerCase()) {
|
|
93
|
+
case ".ico": return "image/x-icon";
|
|
94
|
+
case ".png": return "image/png";
|
|
95
|
+
case ".svg": return "image/svg+xml";
|
|
96
|
+
case ".gif": return "image/gif";
|
|
97
|
+
case ".webp": return "image/webp";
|
|
98
|
+
case ".jpg":
|
|
99
|
+
case ".jpeg": return "image/jpeg";
|
|
100
|
+
default:
|
|
101
|
+
types_1.SinthWarning.emit(`Unknown favicon extension '${path.extname(p)}', defaulting to image/x-icon`);
|
|
102
|
+
return "image/x-icon";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function renderHead(hd, cssLinks, jsLinks, scopedCSS, companionJS) {
|
|
106
|
+
const lines = [];
|
|
107
|
+
lines.push(` <meta charset="${(0, utils_1.escAttr)(hd.charset)}">`);
|
|
108
|
+
lines.push(` <meta name="viewport" content="${(0, utils_1.escAttr)(hd.viewport)}">`);
|
|
109
|
+
if (hd.title)
|
|
110
|
+
lines.push(` <title>${(0, utils_1.esc)(hd.title)}</title>`);
|
|
111
|
+
if (hd.fav)
|
|
112
|
+
lines.push(` <link rel="icon" href="${(0, utils_1.escAttr)(hd.fav)}" type="${faviconType(hd.fav)}">`);
|
|
113
|
+
for (const m of hd.metaTags)
|
|
114
|
+
lines.push(` <meta name="${(0, utils_1.escAttr)(m.name)}" content="${(0, utils_1.escAttr)(m.content)}">`);
|
|
115
|
+
for (const m of hd.metaProps)
|
|
116
|
+
lines.push(` <meta property="${(0, utils_1.escAttr)(m.property)}" content="${(0, utils_1.escAttr)(m.content)}">`);
|
|
117
|
+
for (const css of cssLinks)
|
|
118
|
+
lines.push(` <link rel="stylesheet" href="${(0, utils_1.escAttr)(css)}">`);
|
|
119
|
+
if (scopedCSS.trim())
|
|
120
|
+
lines.push(` <style>\n${scopedCSS}\n </style>`);
|
|
121
|
+
for (const js of jsLinks) {
|
|
122
|
+
const extra = Object.entries(js.attrs)
|
|
123
|
+
.map(([k, v]) => v === "true" ? k : `${k}="${(0, utils_1.escAttr)(v)}"`)
|
|
124
|
+
.join(" ");
|
|
125
|
+
lines.push(` <script src="${(0, utils_1.escAttr)(js.src)}"${extra ? " " + extra : ""}></script>`);
|
|
126
|
+
}
|
|
127
|
+
if (companionJS)
|
|
128
|
+
lines.push(` <script src="${(0, utils_1.escAttr)(companionJS)}"></script>`);
|
|
129
|
+
return `<head>\n${lines.join("\n")}\n</head>`;
|
|
130
|
+
}
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Lexer = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
// lexer
|
|
6
|
+
const KEYWORDS = {
|
|
7
|
+
"import": types_1.TT.KW_IMPORT,
|
|
8
|
+
"as": types_1.TT.KW_AS,
|
|
9
|
+
"css": types_1.TT.KW_CSS,
|
|
10
|
+
"component": types_1.TT.KW_COMPONENT,
|
|
11
|
+
"style": types_1.TT.KW_STYLE,
|
|
12
|
+
"script": types_1.TT.KW_SCRIPT,
|
|
13
|
+
"global": types_1.TT.KW_GLOBAL,
|
|
14
|
+
"page": types_1.TT.KW_PAGE,
|
|
15
|
+
"lang": types_1.TT.KW_LANG,
|
|
16
|
+
"true": types_1.TT.BOOL_TRUE,
|
|
17
|
+
"false": types_1.TT.BOOL_FALSE,
|
|
18
|
+
"null": types_1.TT.NULL_LIT,
|
|
19
|
+
"custom-element": types_1.TT.KW_CUSTOM_EL,
|
|
20
|
+
"custom": types_1.TT.KW_CUSTOM,
|
|
21
|
+
"if": types_1.TT.KW_IF,
|
|
22
|
+
"else": types_1.TT.KW_ELSE,
|
|
23
|
+
"elif": types_1.TT.KW_ELSE,
|
|
24
|
+
"for": types_1.TT.KW_FOR,
|
|
25
|
+
"in": types_1.TT.KW_IN,
|
|
26
|
+
"remove": types_1.TT.KW_REMOVE,
|
|
27
|
+
"function": types_1.TT.KW_FUNCTION,
|
|
28
|
+
"return": types_1.TT.KW_RETURN,
|
|
29
|
+
};
|
|
30
|
+
class Lexer {
|
|
31
|
+
constructor(src, file) {
|
|
32
|
+
this.src = src;
|
|
33
|
+
this.file = file;
|
|
34
|
+
this.pos = 0;
|
|
35
|
+
this.line = 1;
|
|
36
|
+
this.col = 1;
|
|
37
|
+
this.rawBlockPending = false;
|
|
38
|
+
}
|
|
39
|
+
tokenize() {
|
|
40
|
+
const tokens = [];
|
|
41
|
+
while (true) {
|
|
42
|
+
this.skipWS();
|
|
43
|
+
if (this.pos >= this.src.length)
|
|
44
|
+
break;
|
|
45
|
+
const loc = this.loc();
|
|
46
|
+
const ch = this.src[this.pos];
|
|
47
|
+
if (ch === "(") {
|
|
48
|
+
tokens.push(this.single(types_1.TT.LPAREN, loc));
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (ch === ")") {
|
|
52
|
+
tokens.push(this.single(types_1.TT.RPAREN, loc));
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (ch === "}") {
|
|
56
|
+
tokens.push(this.single(types_1.TT.RBRACE, loc));
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (ch === ",") {
|
|
60
|
+
tokens.push(this.single(types_1.TT.COMMA, loc));
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (ch === ".") {
|
|
64
|
+
tokens.push(this.single(types_1.TT.DOT, loc));
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (ch === ":") {
|
|
68
|
+
tokens.push(this.single(types_1.TT.COLON, loc));
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (ch === "[") {
|
|
72
|
+
tokens.push(this.single(types_1.TT.LBRACKET, loc));
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (ch === "]") {
|
|
76
|
+
tokens.push(this.single(types_1.TT.RBRACKET, loc));
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
// two-character operators ->
|
|
80
|
+
if (ch === "=" && this.src[this.pos + 1] === "=") {
|
|
81
|
+
this.adv();
|
|
82
|
+
this.adv();
|
|
83
|
+
tokens.push({ type: types_1.TT.OP_EQEQ, value: "==", loc });
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (ch === "!" && this.src[this.pos + 1] === "=") {
|
|
87
|
+
this.adv();
|
|
88
|
+
this.adv();
|
|
89
|
+
tokens.push({ type: types_1.TT.OP_NEQ, value: "!=", loc });
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (ch === "!") {
|
|
93
|
+
tokens.push(this.single(types_1.TT.OP_NOT, loc));
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (ch === ">" && this.src[this.pos + 1] === "=") {
|
|
97
|
+
this.adv();
|
|
98
|
+
this.adv();
|
|
99
|
+
tokens.push({ type: types_1.TT.OP_GTEQ, value: ">=", loc });
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (ch === "<" && this.src[this.pos + 1] === "=") {
|
|
103
|
+
this.adv();
|
|
104
|
+
this.adv();
|
|
105
|
+
tokens.push({ type: types_1.TT.OP_LTEQ, value: "<=", loc });
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (ch === "=") {
|
|
109
|
+
tokens.push(this.single(types_1.TT.EQUALS, loc));
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
if (ch === "+") {
|
|
113
|
+
tokens.push(this.single(types_1.TT.OP_PLUS, loc));
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (ch === "*") {
|
|
117
|
+
tokens.push(this.single(types_1.TT.OP_STAR, loc));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (ch === "/") {
|
|
121
|
+
tokens.push(this.single(types_1.TT.OP_SLASH, loc));
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (ch === "%") {
|
|
125
|
+
tokens.push(this.single(types_1.TT.OP_MOD, loc));
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (ch === ";") {
|
|
129
|
+
tokens.push(this.single(types_1.TT.OP_SEMI, loc));
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (ch === "<") {
|
|
133
|
+
tokens.push(this.single(types_1.TT.OP_LT, loc));
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (ch === ">") {
|
|
137
|
+
tokens.push(this.single(types_1.TT.OP_GT, loc));
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (ch === "-" && this.src[this.pos + 1] === ">") {
|
|
141
|
+
this.adv();
|
|
142
|
+
this.adv();
|
|
143
|
+
tokens.push({ type: types_1.TT.OP_ARROW, value: "->", loc });
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (ch === "-" && !this.isDigit(this.src[this.pos + 1] ?? "")) {
|
|
147
|
+
tokens.push(this.single(types_1.TT.OP_MINUS, loc));
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (ch === "{") {
|
|
151
|
+
if (this.rawBlockPending) {
|
|
152
|
+
this.rawBlockPending = false;
|
|
153
|
+
tokens.push(this.readRawBlock(loc));
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
tokens.push(this.single(types_1.TT.LBRACE, loc));
|
|
157
|
+
}
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (ch === '"' || ch === "'") {
|
|
161
|
+
tokens.push(this.readString(loc));
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
if (this.isDigit(ch) || (ch === "-" && this.isDigit(this.src[this.pos + 1] ?? ""))) {
|
|
165
|
+
tokens.push(this.readNumber(loc));
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (this.isIdentStart(ch)) {
|
|
169
|
+
const tok = this.readIdent(loc);
|
|
170
|
+
if (tok.type === types_1.TT.KW_STYLE ||
|
|
171
|
+
tok.type === types_1.TT.KW_SCRIPT ||
|
|
172
|
+
tok.type === types_1.TT.KW_COMPONENT ||
|
|
173
|
+
tok.type === types_1.TT.KW_CUSTOM_EL ||
|
|
174
|
+
tok.type === types_1.TT.KW_CUSTOM) {
|
|
175
|
+
this.rawBlockPending = true;
|
|
176
|
+
}
|
|
177
|
+
tokens.push(tok);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
throw new types_1.SinthError(`Unexpected character '${ch}'`, loc);
|
|
181
|
+
}
|
|
182
|
+
tokens.push({ type: types_1.TT.EOF, value: "", loc: this.loc() });
|
|
183
|
+
return tokens;
|
|
184
|
+
}
|
|
185
|
+
skipWS() {
|
|
186
|
+
while (this.pos < this.src.length) {
|
|
187
|
+
const ch = this.src[this.pos];
|
|
188
|
+
if (ch === " " || ch === "\t" || ch === "\r" || ch === "\n") {
|
|
189
|
+
this.adv();
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
// sinth comments: -- line or --[ nestable block for when doing longgerrr comments]--
|
|
193
|
+
if (ch === "-" && this.src[this.pos + 1] === "-") {
|
|
194
|
+
if (this.src[this.pos + 2] === "[") {
|
|
195
|
+
this.adv();
|
|
196
|
+
this.adv();
|
|
197
|
+
this.adv();
|
|
198
|
+
let depth = 1;
|
|
199
|
+
while (this.pos < this.src.length && depth > 0) {
|
|
200
|
+
if (this.src[this.pos] === "-" && this.src[this.pos + 1] === "-" && this.src[this.pos + 2] === "[") {
|
|
201
|
+
depth++;
|
|
202
|
+
this.adv();
|
|
203
|
+
this.adv();
|
|
204
|
+
this.adv();
|
|
205
|
+
}
|
|
206
|
+
else if (this.src[this.pos] === "]" && this.src[this.pos + 1] === "-" && this.src[this.pos + 2] === "-") {
|
|
207
|
+
depth--;
|
|
208
|
+
this.adv();
|
|
209
|
+
this.adv();
|
|
210
|
+
this.adv();
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
this.adv();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (depth > 0)
|
|
217
|
+
throw new types_1.SinthError("Unterminated block comment --[ ... ]--", this.loc());
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
while (this.pos < this.src.length && this.src[this.pos] !== "\n")
|
|
221
|
+
this.adv();
|
|
222
|
+
}
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
readRawBlock(loc) {
|
|
229
|
+
this.adv(); // consume '{'
|
|
230
|
+
const start = this.pos;
|
|
231
|
+
let depth = 1;
|
|
232
|
+
while (this.pos < this.src.length && depth > 0) {
|
|
233
|
+
const ch = this.src[this.pos];
|
|
234
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
235
|
+
this.skipRawString(ch);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
if (ch === "/" && this.src[this.pos + 1] === "/") {
|
|
239
|
+
while (this.pos < this.src.length && this.src[this.pos] !== "\n")
|
|
240
|
+
this.adv();
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (ch === "/" && this.src[this.pos + 1] === "*") {
|
|
244
|
+
this.adv();
|
|
245
|
+
this.adv();
|
|
246
|
+
while (this.pos < this.src.length - 1 &&
|
|
247
|
+
!(this.src[this.pos] === "*" && this.src[this.pos + 1] === "/")) {
|
|
248
|
+
this.adv();
|
|
249
|
+
}
|
|
250
|
+
this.adv();
|
|
251
|
+
this.adv();
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (ch === "{") {
|
|
255
|
+
depth++;
|
|
256
|
+
this.adv();
|
|
257
|
+
}
|
|
258
|
+
else if (ch === "}") {
|
|
259
|
+
depth--;
|
|
260
|
+
if (depth > 0)
|
|
261
|
+
this.adv();
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
this.adv();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const content = this.src.substring(start, this.pos);
|
|
268
|
+
this.adv(); // consume closing '}'
|
|
269
|
+
return { type: types_1.TT.RAW_BLOCK, value: content, loc };
|
|
270
|
+
}
|
|
271
|
+
skipRawString(quote) {
|
|
272
|
+
this.adv();
|
|
273
|
+
while (this.pos < this.src.length) {
|
|
274
|
+
const c = this.src[this.pos];
|
|
275
|
+
if (c === "\\") {
|
|
276
|
+
this.adv();
|
|
277
|
+
this.adv();
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
if (c === quote) {
|
|
281
|
+
this.adv();
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
if (quote === "`" && c === "$" && this.src[this.pos + 1] === "{") {
|
|
285
|
+
this.adv();
|
|
286
|
+
this.adv();
|
|
287
|
+
let d = 1;
|
|
288
|
+
while (this.pos < this.src.length && d > 0) {
|
|
289
|
+
if (this.src[this.pos] === "{")
|
|
290
|
+
d++;
|
|
291
|
+
else if (this.src[this.pos] === "}")
|
|
292
|
+
d--;
|
|
293
|
+
this.adv();
|
|
294
|
+
}
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
this.adv();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
readString(loc) {
|
|
301
|
+
const quote = this.src[this.pos];
|
|
302
|
+
this.adv();
|
|
303
|
+
let result = "";
|
|
304
|
+
while (this.pos < this.src.length && this.src[this.pos] !== quote) {
|
|
305
|
+
if (this.src[this.pos] === "\\") {
|
|
306
|
+
this.adv();
|
|
307
|
+
const e = this.src[this.pos];
|
|
308
|
+
switch (e) {
|
|
309
|
+
case "\\":
|
|
310
|
+
result += "\\";
|
|
311
|
+
break;
|
|
312
|
+
case '"':
|
|
313
|
+
result += '"';
|
|
314
|
+
break;
|
|
315
|
+
case "'":
|
|
316
|
+
result += "'";
|
|
317
|
+
break;
|
|
318
|
+
case "n":
|
|
319
|
+
result += "\n";
|
|
320
|
+
break;
|
|
321
|
+
case "r":
|
|
322
|
+
result += "\r";
|
|
323
|
+
break;
|
|
324
|
+
case "t":
|
|
325
|
+
result += "\t";
|
|
326
|
+
break;
|
|
327
|
+
case "$":
|
|
328
|
+
result += "$";
|
|
329
|
+
break;
|
|
330
|
+
case "{":
|
|
331
|
+
result += "{";
|
|
332
|
+
break;
|
|
333
|
+
case "u": {
|
|
334
|
+
this.adv();
|
|
335
|
+
if (this.src[this.pos] === "{") {
|
|
336
|
+
this.adv();
|
|
337
|
+
let hex = "";
|
|
338
|
+
while (this.src[this.pos] !== "}") {
|
|
339
|
+
hex += this.src[this.pos];
|
|
340
|
+
this.adv();
|
|
341
|
+
}
|
|
342
|
+
result += String.fromCodePoint(parseInt(hex, 16));
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
const hex = this.src.substring(this.pos, this.pos + 4);
|
|
346
|
+
result += String.fromCharCode(parseInt(hex, 16));
|
|
347
|
+
this.pos += 3;
|
|
348
|
+
this.col += 3;
|
|
349
|
+
}
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
default: throw new types_1.SinthError(`Unknown escape sequence \\${e}`, loc);
|
|
353
|
+
}
|
|
354
|
+
this.adv();
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
result += this.src[this.pos];
|
|
358
|
+
this.adv();
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (this.pos >= this.src.length)
|
|
362
|
+
throw new types_1.SinthError("Unterminated string literal", loc);
|
|
363
|
+
this.adv();
|
|
364
|
+
return { type: types_1.TT.STRING, value: result, loc };
|
|
365
|
+
}
|
|
366
|
+
readNumber(loc) {
|
|
367
|
+
let s = "";
|
|
368
|
+
if (this.src[this.pos] === "-") {
|
|
369
|
+
s += "-";
|
|
370
|
+
this.adv();
|
|
371
|
+
}
|
|
372
|
+
while (this.isDigit(this.src[this.pos] ?? "")) {
|
|
373
|
+
s += this.src[this.pos];
|
|
374
|
+
this.adv();
|
|
375
|
+
}
|
|
376
|
+
if (this.src[this.pos] === "." && this.isDigit(this.src[this.pos + 1] ?? "")) {
|
|
377
|
+
s += ".";
|
|
378
|
+
this.adv();
|
|
379
|
+
while (this.isDigit(this.src[this.pos] ?? "")) {
|
|
380
|
+
s += this.src[this.pos];
|
|
381
|
+
this.adv();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return { type: types_1.TT.NUMBER, value: s, loc };
|
|
385
|
+
}
|
|
386
|
+
readIdent(loc) {
|
|
387
|
+
let s = "";
|
|
388
|
+
while (this.pos < this.src.length && this.isIdentCont(this.src[this.pos])) {
|
|
389
|
+
s += this.src[this.pos];
|
|
390
|
+
this.adv();
|
|
391
|
+
}
|
|
392
|
+
const kw = KEYWORDS[s];
|
|
393
|
+
return { type: kw !== undefined ? kw : types_1.TT.IDENT, value: s, loc };
|
|
394
|
+
}
|
|
395
|
+
adv() {
|
|
396
|
+
const ch = this.src[this.pos++];
|
|
397
|
+
if (ch === "\n") {
|
|
398
|
+
this.line++;
|
|
399
|
+
this.col = 1;
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
this.col++;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
single(type, loc) {
|
|
406
|
+
this.adv();
|
|
407
|
+
return { type, value: this.src[this.pos - 1], loc };
|
|
408
|
+
}
|
|
409
|
+
loc() { return { file: this.file, line: this.line, col: this.col }; }
|
|
410
|
+
isDigit(c) { return c >= "0" && c <= "9"; }
|
|
411
|
+
isIdentStart(c) { return /[a-zA-Z_$\u00C0-\uFFFF]/.test(c); }
|
|
412
|
+
isIdentCont(c) { return /[a-zA-Z0-9_$\-\u00C0-\uFFFF]/.test(c); }
|
|
413
|
+
}
|
|
414
|
+
exports.Lexer = Lexer;
|