@yannosay/sinth 4.1.0 → 5.1.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.
Files changed (78) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/DOCS.md +1359 -0
  3. package/README.md +83 -74
  4. package/bin/sinth.js +2 -1
  5. package/dist/cli/args.d.ts +11 -0
  6. package/dist/cli/args.js +74 -0
  7. package/dist/cli/index.d.ts +1 -0
  8. package/dist/cli/index.js +437 -0
  9. package/dist/cli/templates.d.ts +10 -0
  10. package/dist/cli/templates.js +193 -0
  11. package/dist/cli/ui.d.ts +28 -0
  12. package/dist/cli/ui.js +61 -0
  13. package/dist/cli/update-check.d.ts +2 -0
  14. package/dist/cli/update-check.js +124 -0
  15. package/dist/compiler/ast.d.ts +315 -0
  16. package/dist/compiler/ast.js +4 -0
  17. package/dist/compiler/codegen.d.ts +143 -0
  18. package/dist/compiler/codegen.js +1326 -0
  19. package/dist/compiler/diagnostics.d.ts +36 -0
  20. package/dist/compiler/diagnostics.js +142 -0
  21. package/dist/compiler/elements.d.ts +14 -0
  22. package/dist/compiler/elements.js +78 -0
  23. package/dist/compiler/globals.d.ts +2 -0
  24. package/dist/compiler/globals.js +27 -0
  25. package/dist/compiler/html.d.ts +37 -0
  26. package/dist/compiler/html.js +152 -0
  27. package/dist/compiler/lexer.d.ts +42 -0
  28. package/dist/compiler/lexer.js +404 -0
  29. package/dist/compiler/parser.d.ts +95 -0
  30. package/dist/compiler/parser.js +1374 -0
  31. package/dist/compiler/prerender.d.ts +72 -0
  32. package/dist/compiler/prerender.js +910 -0
  33. package/dist/compiler/script-analysis.d.ts +6 -0
  34. package/dist/compiler/script-analysis.js +106 -0
  35. package/dist/compiler/style.d.ts +39 -0
  36. package/dist/compiler/style.js +463 -0
  37. package/dist/deploy/cloudflare.d.ts +9 -0
  38. package/dist/deploy/cloudflare.js +111 -0
  39. package/dist/index.d.ts +13 -0
  40. package/dist/index.js +23 -0
  41. package/dist/project/build.d.ts +31 -0
  42. package/dist/project/build.js +425 -0
  43. package/dist/project/config.d.ts +33 -0
  44. package/dist/project/config.js +142 -0
  45. package/dist/project/project.d.ts +74 -0
  46. package/dist/project/project.js +535 -0
  47. package/dist/runtime/index.d.ts +68 -0
  48. package/dist/runtime/index.js +912 -0
  49. package/dist/server/client.d.ts +1 -0
  50. package/dist/server/client.js +112 -0
  51. package/dist/server/dev.d.ts +8 -0
  52. package/dist/server/dev.js +244 -0
  53. package/dist/server/preview.d.ts +6 -0
  54. package/dist/server/preview.js +106 -0
  55. package/dist/server/static.d.ts +10 -0
  56. package/dist/server/static.js +136 -0
  57. package/lib/ui/index.sinth +436 -0
  58. package/package.json +33 -15
  59. package/schema/sinth.config.schema.json +54 -0
  60. package/dist/browser/compile.js +0 -63
  61. package/dist/cli.js +0 -509
  62. package/dist/core/builtins.js +0 -117
  63. package/dist/core/cli-compiler.js +0 -705
  64. package/dist/core/compiler.js +0 -1489
  65. package/dist/core/expr.js +0 -129
  66. package/dist/core/head-builder.js +0 -130
  67. package/dist/core/lexer.js +0 -410
  68. package/dist/core/parser.js +0 -1646
  69. package/dist/core/runtime/custom-element.js +0 -81
  70. package/dist/core/runtime/functions.js +0 -99
  71. package/dist/core/runtime/helpers.js +0 -347
  72. package/dist/core/runtime/render.js +0 -136
  73. package/dist/core/style-processor.js +0 -317
  74. package/dist/core/types.js +0 -93
  75. package/dist/resolver.js +0 -175
  76. package/dist/server.js +0 -174
  77. package/dist/update-check.js +0 -109
  78. package/dist/utils.js +0 -80
package/dist/core/expr.js DELETED
@@ -1,129 +0,0 @@
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
- const operand = expr.operand;
50
- if (operand.kind === "binary" && (operand.op === "+" || operand.op === "-" || operand.op === "*" || operand.op === "/" || operand.op === "%")) {
51
- return `${o}(${compileExprToJS(operand, loopVars, namespace, declaredVars)})`;
52
- }
53
- return `${o}${compileExprToJS(operand, loopVars, namespace, declaredVars)}`;
54
- }
55
- case "assign": {
56
- const v = expr.right ? compileExprToJS(expr.right, loopVars, namespace, declaredVars) : "null";
57
- let t = expr.target ?? "undefined";
58
- if (namespace && declaredVars && expr.target) {
59
- const dotIdx = expr.target.indexOf('.');
60
- const root = dotIdx !== -1 ? expr.target.substring(0, dotIdx) : expr.target;
61
- if (declaredVars.has(root)) {
62
- t = namespace + "_" + expr.target;
63
- }
64
- }
65
- return `${t} ${expr.op} ${v}`;
66
- }
67
- case "index":
68
- return `${compileExprToJS(expr.object, loopVars, namespace, declaredVars)}[${compileExprToJS(expr.key, loopVars, namespace, declaredVars)}]`;
69
- case "postfix": {
70
- let t2 = expr.target ?? "undefined";
71
- if (namespace && declaredVars && expr.target) {
72
- const dotIdx = expr.target.indexOf('.');
73
- const root = dotIdx !== -1 ? expr.target.substring(0, dotIdx) : expr.target;
74
- if (declaredVars.has(root)) {
75
- t2 = namespace + "_" + expr.target;
76
- }
77
- }
78
- return `${t2}${expr.op}`;
79
- }
80
- case "call": {
81
- let callee = compileExprToJS(expr.callee, loopVars, namespace, declaredVars);
82
- if (expr.memo && callee.startsWith('$')) {
83
- callee = callee.substring(1);
84
- }
85
- if (callee === "remove") {
86
- const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars, namespace, declaredVars) : "''";
87
- return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
88
- }
89
- const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars, namespace, declaredVars)).join(", ");
90
- if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
91
- return callee;
92
- }
93
- return `${callee}(${args})`;
94
- }
95
- default:
96
- return "";
97
- }
98
- }
99
- function compileIfToJS(ifBlock, loopVars, namespace, declaredVars) {
100
- const cond = compileExprToJS(ifBlock.condition, loopVars, namespace, declaredVars);
101
- const ifJS = bodyToJS(ifBlock.body, loopVars, namespace, declaredVars);
102
- const elseJS = ifBlock.elseBody ? bodyToJS(ifBlock.elseBody, loopVars, namespace, declaredVars) : "";
103
- let js = `if (${cond}) {\n${ifJS}}\n`;
104
- if (elseJS)
105
- js += `else {\n${elseJS}}\n`;
106
- return js;
107
- }
108
- function bodyToJS(children, loopVars, namespace, declaredVars) {
109
- return children
110
- .filter(c => c.kind === "assign_stmt" || c.kind === "if" || c.kind === "return" || c.kind === "expr")
111
- .map(c => {
112
- if (c.kind === "assign_stmt")
113
- return ` ${compileExprToJS(c.expression, loopVars, namespace, declaredVars)};\n`;
114
- if (c.kind === "return")
115
- return ` return ${c.expression ? compileExprToJS(c.expression, loopVars, namespace, declaredVars) : ""};\n`;
116
- if (c.kind === "if")
117
- return compileIfToJS(c, loopVars, namespace, declaredVars).replace(/^/gm, " ") + "\n";
118
- if (c.kind === "expr")
119
- return ` ${compileExprToJS(c.expression, loopVars, namespace, declaredVars)};\n`;
120
- return "";
121
- })
122
- .join("");
123
- }
124
- function scopeVariableName(name, ctx) {
125
- if (ctx?.scopePrefix && ctx?.scopeVar && ctx?.varDecls?.some(v => v.name === name)) {
126
- return `${ctx.scopePrefix}${name}`;
127
- }
128
- return name;
129
- }
@@ -1,130 +0,0 @@
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
- }
@@ -1,410 +0,0 @@
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 === "-") {
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)) {
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
- while (this.isDigit(this.src[this.pos] ?? "")) {
369
- s += this.src[this.pos];
370
- this.adv();
371
- }
372
- if (this.src[this.pos] === "." && this.isDigit(this.src[this.pos + 1] ?? "")) {
373
- s += ".";
374
- this.adv();
375
- while (this.isDigit(this.src[this.pos] ?? "")) {
376
- s += this.src[this.pos];
377
- this.adv();
378
- }
379
- }
380
- return { type: types_1.TT.NUMBER, value: s, loc };
381
- }
382
- readIdent(loc) {
383
- let s = "";
384
- while (this.pos < this.src.length && this.isIdentCont(this.src[this.pos])) {
385
- s += this.src[this.pos];
386
- this.adv();
387
- }
388
- const kw = KEYWORDS[s];
389
- return { type: kw !== undefined ? kw : types_1.TT.IDENT, value: s, loc };
390
- }
391
- adv() {
392
- const ch = this.src[this.pos++];
393
- if (ch === "\n") {
394
- this.line++;
395
- this.col = 1;
396
- }
397
- else {
398
- this.col++;
399
- }
400
- }
401
- single(type, loc) {
402
- this.adv();
403
- return { type, value: this.src[this.pos - 1], loc };
404
- }
405
- loc() { return { file: this.file, line: this.line, col: this.col }; }
406
- isDigit(c) { return c >= "0" && c <= "9"; }
407
- isIdentStart(c) { return /[a-zA-Z_$\u00C0-\uFFFF]/.test(c); }
408
- isIdentCont(c) { return /[a-zA-Z0-9_$\u00C0-\uFFFF]/.test(c); }
409
- }
410
- exports.Lexer = Lexer;