@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
package/src/core/expr.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { Expression, Child, AssignStmt, IfBlock, ReturnStmt, LitStr, LitNum, LitBool, CompileCtx } from "./types.ts";
|
|
2
|
-
|
|
3
|
-
export function compileExprToJS(expr: Expression, loopVars?: Set<string>, namespace?: string, declaredVars?: Set<string>): string {
|
|
4
|
-
switch (expr.kind) {
|
|
5
|
-
case "literal":
|
|
6
|
-
if (!expr.value) return "null";
|
|
7
|
-
if (expr.value.kind === "str") {
|
|
8
|
-
const sv = (expr.value as LitStr).value;
|
|
9
|
-
if (sv.startsWith("__ARR__")) return sv.slice(7);
|
|
10
|
-
if (sv.startsWith("__VAR__")) return sv.slice(7);
|
|
11
|
-
return JSON.stringify(sv);
|
|
12
|
-
}
|
|
13
|
-
if (expr.value.kind === "num") return String((expr.value as LitNum).value);
|
|
14
|
-
if (expr.value.kind === "bool") return String((expr.value as LitBool).value);
|
|
15
|
-
return "null";
|
|
16
|
-
case "variable":
|
|
17
|
-
if (loopVars && expr.name) {
|
|
18
|
-
const dotIdx = expr.name.indexOf('.');
|
|
19
|
-
const root = dotIdx !== -1 ? expr.name.substring(0, dotIdx) : expr.name;
|
|
20
|
-
if (loopVars.has(root)) {
|
|
21
|
-
return `_ctx.${expr.name}`;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
if (namespace && declaredVars && expr.name) {
|
|
25
|
-
const dotIdx2 = expr.name.indexOf('.');
|
|
26
|
-
const root2 = dotIdx2 !== -1 ? expr.name.substring(0, dotIdx2) : expr.name;
|
|
27
|
-
if (declaredVars.has(root2)) {
|
|
28
|
-
return namespace + "_" + expr.name;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return expr.name ?? "undefined";
|
|
32
|
-
case "binary": {
|
|
33
|
-
const l = compileExprToJS(expr.left!, loopVars, namespace, declaredVars);
|
|
34
|
-
const r = compileExprToJS(expr.right!, loopVars, namespace, declaredVars);
|
|
35
|
-
const o = expr.op === "and" ? "&&" : expr.op === "or" ? "||" : expr.op!;
|
|
36
|
-
return `(${l} ${o} ${r})`;
|
|
37
|
-
}
|
|
38
|
-
case "unary": {
|
|
39
|
-
const o = expr.op === "not" ? "!" : expr.op!;
|
|
40
|
-
return `${o}(${compileExprToJS(expr.operand!, loopVars, namespace, declaredVars)})`;
|
|
41
|
-
}
|
|
42
|
-
case "assign": {
|
|
43
|
-
const v = expr.right ? compileExprToJS(expr.right, loopVars, namespace, declaredVars) : "null";
|
|
44
|
-
let t = expr.target ?? "undefined";
|
|
45
|
-
if (namespace && declaredVars && expr.target) {
|
|
46
|
-
const dotIdx = expr.target.indexOf('.');
|
|
47
|
-
const root = dotIdx !== -1 ? expr.target.substring(0, dotIdx) : expr.target;
|
|
48
|
-
if (declaredVars.has(root)) {
|
|
49
|
-
t = namespace + "_" + expr.target;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return `${t} ${expr.op} ${v}`;
|
|
53
|
-
}
|
|
54
|
-
case "index":
|
|
55
|
-
return `${compileExprToJS(expr.object!, loopVars, namespace, declaredVars)}[${compileExprToJS(expr.key!, loopVars, namespace, declaredVars)}]`;
|
|
56
|
-
case "postfix": {
|
|
57
|
-
let t2 = 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
|
-
t2 = namespace + "_" + expr.target;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return `${t2}${expr.op}`;
|
|
66
|
-
}
|
|
67
|
-
case "call": {
|
|
68
|
-
const callee = compileExprToJS(expr.callee!, loopVars, namespace, declaredVars);
|
|
69
|
-
if (callee === "remove") {
|
|
70
|
-
const arg = expr.args && expr.args[0] ? compileExprToJS(expr.args[0], loopVars, namespace, declaredVars) : "''";
|
|
71
|
-
return `(function(){ var _el=document.getElementById(${arg}); if(_el)_el.remove(); })()`;
|
|
72
|
-
}
|
|
73
|
-
const args = (expr.args ?? []).map(a => compileExprToJS(a, loopVars, namespace, declaredVars)).join(", ");
|
|
74
|
-
if (expr.args && expr.args.length === 0 && /\.length$/.test(callee)) {
|
|
75
|
-
return callee;
|
|
76
|
-
}
|
|
77
|
-
return `${callee}(${args})`;
|
|
78
|
-
}
|
|
79
|
-
default:
|
|
80
|
-
return "";
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
export function compileIfToJS(ifBlock: IfBlock, loopVars?: Set<string>, namespace?: string, declaredVars?: Set<string>): string {
|
|
86
|
-
const cond = compileExprToJS(ifBlock.condition, loopVars, namespace, declaredVars);
|
|
87
|
-
const ifJS = bodyToJS(ifBlock.body, loopVars, namespace, declaredVars);
|
|
88
|
-
const elseJS = ifBlock.elseBody ? bodyToJS(ifBlock.elseBody, loopVars, namespace, declaredVars) : "";
|
|
89
|
-
let js = `if (${cond}) {\n${ifJS}}\n`;
|
|
90
|
-
if (elseJS) js += `else {\n${elseJS}}\n`;
|
|
91
|
-
return js;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
export function bodyToJS(children: Child[], loopVars?: Set<string>, namespace?: string, declaredVars?: Set<string>): string {
|
|
97
|
-
return children
|
|
98
|
-
.filter(c => c.kind === "assign_stmt" || c.kind === "if" || c.kind === "return" || c.kind === "expr")
|
|
99
|
-
.map(c => {
|
|
100
|
-
if (c.kind === "assign_stmt") return ` ${compileExprToJS((c as AssignStmt).expression, loopVars, namespace, declaredVars)};\n`;
|
|
101
|
-
if (c.kind === "return") return ` return ${(c as ReturnStmt).expression ? compileExprToJS((c as ReturnStmt).expression!, loopVars, namespace, declaredVars) : ""};\n`;
|
|
102
|
-
if (c.kind === "if") return compileIfToJS(c as IfBlock, loopVars, namespace, declaredVars).replace(/^/gm, " ") + "\n";
|
|
103
|
-
if (c.kind === "expr") return ` ${compileExprToJS((c as any).expression, loopVars, namespace, declaredVars)};\n`;
|
|
104
|
-
return "";
|
|
105
|
-
})
|
|
106
|
-
.join("");
|
|
107
|
-
}
|
|
108
|
-
export function scopeVariableName(name: string, ctx?: CompileCtx): string {
|
|
109
|
-
if (ctx?.scopePrefix && ctx?.scopeVar && ctx?.varDecls?.some(v => v.name === name)) {
|
|
110
|
-
return `${ctx.scopePrefix}${name}`;
|
|
111
|
-
}
|
|
112
|
-
return name;
|
|
113
|
-
}
|
package/src/core/head-builder.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import * as path from "path";
|
|
2
|
-
import { MetaEntry, SinthWarning } from "./types.ts";
|
|
3
|
-
import { esc, escAttr, litToString, camelToKebab } from "../utils.ts";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export interface HeadData {
|
|
8
|
-
title?: string; fav?: string;
|
|
9
|
-
lang: string; charset: string; viewport: string;
|
|
10
|
-
metaTags: { name: string; content: string }[];
|
|
11
|
-
metaProps: { property: string; content: string }[];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function buildHeadData(meta: MetaEntry[]): HeadData {
|
|
15
|
-
let title: string | undefined, fav: string | undefined;
|
|
16
|
-
let lang = "en", charset = "UTF-8", viewport = "width=device-width, initial-scale=1.0";
|
|
17
|
-
const metaTags: { name: string; content: string }[] = [];
|
|
18
|
-
const metaProps: { property: string; content: string }[] = [];
|
|
19
|
-
|
|
20
|
-
for (const m of meta) {
|
|
21
|
-
const val = litToString(m.value);
|
|
22
|
-
switch (m.key) {
|
|
23
|
-
case "title": title = val; break;
|
|
24
|
-
case "fav": fav = val; break;
|
|
25
|
-
case "lang": lang = val; break;
|
|
26
|
-
case "charset": charset = val; break;
|
|
27
|
-
case "viewport": viewport = val; break;
|
|
28
|
-
case "descr": metaTags.push({ name: "description", content: val }); break;
|
|
29
|
-
case "author": metaTags.push({ name: "author", content: val }); break;
|
|
30
|
-
case "keywords": metaTags.push({ name: "keywords", content: val }); break;
|
|
31
|
-
case "robots": metaTags.push({ name: "robots", content: val }); break;
|
|
32
|
-
default:
|
|
33
|
-
if (m.key.startsWith("og") || m.key.startsWith("twitter")) {
|
|
34
|
-
const prop = m.key
|
|
35
|
-
.replace(/^og([A-Z])/, (_: string, c: string) => `og:${c.toLowerCase()}`)
|
|
36
|
-
.replace(/^twitter([A-Z])/, (_: string, c: string) => `twitter:${c.toLowerCase()}`);
|
|
37
|
-
metaProps.push({ property: prop, content: val });
|
|
38
|
-
} else {
|
|
39
|
-
metaTags.push({ name: camelToKebab(m.key), content: val });
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return { title, fav, lang, charset, viewport, metaTags, metaProps };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function faviconType(p: string): string {
|
|
48
|
-
switch (path.extname(p).toLowerCase()) {
|
|
49
|
-
case ".ico": return "image/x-icon";
|
|
50
|
-
case ".png": return "image/png";
|
|
51
|
-
case ".svg": return "image/svg+xml";
|
|
52
|
-
case ".gif": return "image/gif";
|
|
53
|
-
case ".webp": return "image/webp";
|
|
54
|
-
case ".jpg":
|
|
55
|
-
case ".jpeg": return "image/jpeg";
|
|
56
|
-
default:
|
|
57
|
-
SinthWarning.emit(`Unknown favicon extension '${path.extname(p)}', defaulting to image/x-icon`);
|
|
58
|
-
return "image/x-icon";
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function renderHead(
|
|
63
|
-
hd: HeadData,
|
|
64
|
-
cssLinks: string[],
|
|
65
|
-
jsLinks: { src: string; attrs: Record<string, string> }[],
|
|
66
|
-
scopedCSS: string,
|
|
67
|
-
companionJS: string | undefined,
|
|
68
|
-
): string {
|
|
69
|
-
const lines: string[] = [];
|
|
70
|
-
lines.push(` <meta charset="${escAttr(hd.charset)}">`);
|
|
71
|
-
lines.push(` <meta name="viewport" content="${escAttr(hd.viewport)}">`);
|
|
72
|
-
if (hd.title) lines.push(` <title>${esc(hd.title)}</title>`);
|
|
73
|
-
if (hd.fav) lines.push(` <link rel="icon" href="${escAttr(hd.fav)}" type="${faviconType(hd.fav)}">`);
|
|
74
|
-
for (const m of hd.metaTags) lines.push(` <meta name="${escAttr(m.name)}" content="${escAttr(m.content)}">`);
|
|
75
|
-
for (const m of hd.metaProps) lines.push(` <meta property="${escAttr(m.property)}" content="${escAttr(m.content)}">`);
|
|
76
|
-
for (const css of cssLinks) lines.push(` <link rel="stylesheet" href="${escAttr(css)}">`);
|
|
77
|
-
if (scopedCSS.trim()) lines.push(` <style>\n${scopedCSS}\n </style>`);
|
|
78
|
-
for (const js of jsLinks) {
|
|
79
|
-
const extra = Object.entries(js.attrs)
|
|
80
|
-
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
81
|
-
.join(" ");
|
|
82
|
-
lines.push(` <script src="${escAttr(js.src)}"${extra ? " " + extra : ""}></script>`);
|
|
83
|
-
}
|
|
84
|
-
if (companionJS) lines.push(` <script src="${escAttr(companionJS)}"></script>`);
|
|
85
|
-
return `<head>\n${lines.join("\n")}\n</head>`;
|
|
86
|
-
}
|
package/src/core/lexer.ts
DELETED
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
import { TT, Token, Loc, SinthError } from "./types.ts";
|
|
2
|
-
|
|
3
|
-
// lexer
|
|
4
|
-
const KEYWORDS: Record<string, TT> = {
|
|
5
|
-
"import": TT.KW_IMPORT,
|
|
6
|
-
"as": TT.KW_AS,
|
|
7
|
-
"css": TT.KW_CSS,
|
|
8
|
-
"component": TT.KW_COMPONENT,
|
|
9
|
-
"style": TT.KW_STYLE,
|
|
10
|
-
"script": TT.KW_SCRIPT,
|
|
11
|
-
"global": TT.KW_GLOBAL,
|
|
12
|
-
"page": TT.KW_PAGE,
|
|
13
|
-
"lang": TT.KW_LANG,
|
|
14
|
-
"true": TT.BOOL_TRUE,
|
|
15
|
-
"false": TT.BOOL_FALSE,
|
|
16
|
-
"null": TT.NULL_LIT,
|
|
17
|
-
"custom-element": TT.KW_CUSTOM_EL,
|
|
18
|
-
"custom": TT.KW_CUSTOM,
|
|
19
|
-
"if": TT.KW_IF,
|
|
20
|
-
"else": TT.KW_ELSE,
|
|
21
|
-
"elif": TT.KW_ELSE,
|
|
22
|
-
"for": TT.KW_FOR,
|
|
23
|
-
"in": TT.KW_IN,
|
|
24
|
-
"remove": TT.KW_REMOVE,
|
|
25
|
-
"function": TT.KW_FUNCTION,
|
|
26
|
-
"return": TT.KW_RETURN,
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export class Lexer {
|
|
30
|
-
private pos = 0;
|
|
31
|
-
private line = 1;
|
|
32
|
-
private col = 1;
|
|
33
|
-
private rawBlockPending = false;
|
|
34
|
-
|
|
35
|
-
constructor(public readonly src: string, private readonly file: string) {}
|
|
36
|
-
|
|
37
|
-
tokenize(): Token[] {
|
|
38
|
-
const tokens: Token[] = [];
|
|
39
|
-
while (true) {
|
|
40
|
-
this.skipWS();
|
|
41
|
-
if (this.pos >= this.src.length) break;
|
|
42
|
-
|
|
43
|
-
const loc = this.loc();
|
|
44
|
-
const ch = this.src[this.pos];
|
|
45
|
-
|
|
46
|
-
if (ch === "(") { tokens.push(this.single(TT.LPAREN, loc)); continue; }
|
|
47
|
-
if (ch === ")") { tokens.push(this.single(TT.RPAREN, loc)); continue; }
|
|
48
|
-
if (ch === "}") { tokens.push(this.single(TT.RBRACE, loc)); continue; }
|
|
49
|
-
if (ch === ",") { tokens.push(this.single(TT.COMMA, loc)); continue; }
|
|
50
|
-
if (ch === ".") { tokens.push(this.single(TT.DOT, loc)); continue; }
|
|
51
|
-
if (ch === ":") { tokens.push(this.single(TT.COLON, loc)); continue; }
|
|
52
|
-
if (ch === "[") { tokens.push(this.single(TT.LBRACKET, loc)); continue; }
|
|
53
|
-
if (ch === "]") { tokens.push(this.single(TT.RBRACKET, loc)); continue; }
|
|
54
|
-
|
|
55
|
-
// two-character operators ->
|
|
56
|
-
if (ch === "=" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_EQEQ, value: "==", loc }); continue; }
|
|
57
|
-
if (ch === "!" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_NEQ, value: "!=", loc }); continue; }
|
|
58
|
-
if (ch === "!") { tokens.push(this.single(TT.OP_NOT, loc)); continue; }
|
|
59
|
-
if (ch === ">" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_GTEQ, value: ">=", loc }); continue; }
|
|
60
|
-
if (ch === "<" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_LTEQ, value: "<=", loc }); continue; }
|
|
61
|
-
|
|
62
|
-
if (ch === "=") { tokens.push(this.single(TT.EQUALS, loc)); continue; }
|
|
63
|
-
if (ch === "+") { tokens.push(this.single(TT.OP_PLUS, loc)); continue; }
|
|
64
|
-
if (ch === "*") { tokens.push(this.single(TT.OP_STAR, loc)); continue; }
|
|
65
|
-
if (ch === "/") { tokens.push(this.single(TT.OP_SLASH, loc)); continue; }
|
|
66
|
-
if (ch === "%") { tokens.push(this.single(TT.OP_MOD, loc)); continue; }
|
|
67
|
-
if (ch === ";") { tokens.push(this.single(TT.OP_SEMI, loc)); continue; }
|
|
68
|
-
if (ch === "<") { tokens.push(this.single(TT.OP_LT, loc)); continue; }
|
|
69
|
-
if (ch === ">") { tokens.push(this.single(TT.OP_GT, loc)); continue; }
|
|
70
|
-
if (ch === "-" && this.src[this.pos + 1] === ">") {
|
|
71
|
-
this.adv(); this.adv();
|
|
72
|
-
tokens.push({ type: TT.OP_ARROW, value: "->", loc });
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
if (ch === "-" && !this.isDigit(this.src[this.pos + 1] ?? "")) {
|
|
76
|
-
tokens.push(this.single(TT.OP_MINUS, loc)); continue;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (ch === "{") {
|
|
80
|
-
if (this.rawBlockPending) {
|
|
81
|
-
this.rawBlockPending = false;
|
|
82
|
-
tokens.push(this.readRawBlock(loc));
|
|
83
|
-
} else {
|
|
84
|
-
tokens.push(this.single(TT.LBRACE, loc));
|
|
85
|
-
}
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (ch === '"' || ch === "'") { tokens.push(this.readString(loc)); continue; }
|
|
90
|
-
|
|
91
|
-
if (this.isDigit(ch) || (ch === "-" && this.isDigit(this.src[this.pos + 1] ?? ""))) {
|
|
92
|
-
tokens.push(this.readNumber(loc)); continue;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (this.isIdentStart(ch)) {
|
|
96
|
-
const tok = this.readIdent(loc);
|
|
97
|
-
if (
|
|
98
|
-
tok.type === TT.KW_STYLE ||
|
|
99
|
-
tok.type === TT.KW_SCRIPT ||
|
|
100
|
-
tok.type === TT.KW_COMPONENT ||
|
|
101
|
-
tok.type === TT.KW_CUSTOM_EL ||
|
|
102
|
-
tok.type === TT.KW_CUSTOM
|
|
103
|
-
) {
|
|
104
|
-
this.rawBlockPending = true;
|
|
105
|
-
}
|
|
106
|
-
tokens.push(tok);
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
throw new SinthError(`Unexpected character '${ch}'`, loc);
|
|
111
|
-
}
|
|
112
|
-
tokens.push({ type: TT.EOF, value: "", loc: this.loc() });
|
|
113
|
-
return tokens;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
private skipWS(): void {
|
|
117
|
-
while (this.pos < this.src.length) {
|
|
118
|
-
const ch = this.src[this.pos];
|
|
119
|
-
if (ch === " " || ch === "\t" || ch === "\r" || ch === "\n") { this.adv(); continue; }
|
|
120
|
-
|
|
121
|
-
// sinth comments: -- line or --[ nestable block for when doing longgerrr comments]--
|
|
122
|
-
if (ch === "-" && this.src[this.pos + 1] === "-") {
|
|
123
|
-
if (this.src[this.pos + 2] === "[") {
|
|
124
|
-
this.adv(); this.adv(); this.adv();
|
|
125
|
-
let depth = 1;
|
|
126
|
-
while (this.pos < this.src.length && depth > 0) {
|
|
127
|
-
if (this.src[this.pos] === "-" && this.src[this.pos + 1] === "-" && this.src[this.pos + 2] === "[") {
|
|
128
|
-
depth++; this.adv(); this.adv(); this.adv();
|
|
129
|
-
} else if (this.src[this.pos] === "]" && this.src[this.pos + 1] === "-" && this.src[this.pos + 2] === "-") {
|
|
130
|
-
depth--; this.adv(); this.adv(); this.adv();
|
|
131
|
-
} else {
|
|
132
|
-
this.adv();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (depth > 0) throw new SinthError("Unterminated block comment --[ ... ]--", this.loc());
|
|
136
|
-
} else {
|
|
137
|
-
while (this.pos < this.src.length && this.src[this.pos] !== "\n") this.adv();
|
|
138
|
-
}
|
|
139
|
-
continue;
|
|
140
|
-
}
|
|
141
|
-
break;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private readRawBlock(loc: Loc): Token {
|
|
146
|
-
this.adv(); // consume '{'
|
|
147
|
-
const start = this.pos;
|
|
148
|
-
let depth = 1;
|
|
149
|
-
|
|
150
|
-
while (this.pos < this.src.length && depth > 0) {
|
|
151
|
-
const ch = this.src[this.pos];
|
|
152
|
-
if (ch === '"' || ch === "'" || ch === "`") { this.skipRawString(ch); continue; }
|
|
153
|
-
if (ch === "/" && this.src[this.pos + 1] === "/") {
|
|
154
|
-
while (this.pos < this.src.length && this.src[this.pos] !== "\n") this.adv();
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
if (ch === "/" && this.src[this.pos + 1] === "*") {
|
|
158
|
-
this.adv(); this.adv();
|
|
159
|
-
while (this.pos < this.src.length - 1 &&
|
|
160
|
-
!(this.src[this.pos] === "*" && this.src[this.pos + 1] === "/")) { this.adv(); }
|
|
161
|
-
this.adv(); this.adv();
|
|
162
|
-
continue;
|
|
163
|
-
}
|
|
164
|
-
if (ch === "{") { depth++; this.adv(); }
|
|
165
|
-
else if (ch === "}") {
|
|
166
|
-
depth--;
|
|
167
|
-
if (depth > 0) this.adv();
|
|
168
|
-
} else {
|
|
169
|
-
this.adv();
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const content = this.src.substring(start, this.pos);
|
|
174
|
-
this.adv(); // consume closing '}'
|
|
175
|
-
return { type: TT.RAW_BLOCK, value: content, loc };
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
private skipRawString(quote: string): void {
|
|
179
|
-
this.adv();
|
|
180
|
-
while (this.pos < this.src.length) {
|
|
181
|
-
const c = this.src[this.pos];
|
|
182
|
-
if (c === "\\") { this.adv(); this.adv(); continue; }
|
|
183
|
-
if (c === quote) { this.adv(); return; }
|
|
184
|
-
if (quote === "`" && c === "$" && this.src[this.pos + 1] === "{") {
|
|
185
|
-
this.adv(); this.adv();
|
|
186
|
-
let d = 1;
|
|
187
|
-
while (this.pos < this.src.length && d > 0) {
|
|
188
|
-
if (this.src[this.pos] === "{") d++;
|
|
189
|
-
else if (this.src[this.pos] === "}") d--;
|
|
190
|
-
this.adv();
|
|
191
|
-
}
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
this.adv();
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
private readString(loc: Loc): Token {
|
|
199
|
-
const quote = this.src[this.pos];
|
|
200
|
-
this.adv();
|
|
201
|
-
let result = "";
|
|
202
|
-
while (this.pos < this.src.length && this.src[this.pos] !== quote) {
|
|
203
|
-
if (this.src[this.pos] === "\\") {
|
|
204
|
-
this.adv();
|
|
205
|
-
const e = this.src[this.pos];
|
|
206
|
-
switch (e) {
|
|
207
|
-
case "\\": result += "\\"; break;
|
|
208
|
-
case '"': result += '"'; break;
|
|
209
|
-
case "'": result += "'"; break;
|
|
210
|
-
case "n": result += "\n"; break;
|
|
211
|
-
case "r": result += "\r"; break;
|
|
212
|
-
case "t": result += "\t"; break;
|
|
213
|
-
case "$": result += "$"; break;
|
|
214
|
-
case "{": result += "{"; break;
|
|
215
|
-
case "u": {
|
|
216
|
-
this.adv();
|
|
217
|
-
if (this.src[this.pos] === "{") {
|
|
218
|
-
this.adv();
|
|
219
|
-
let hex = "";
|
|
220
|
-
while (this.src[this.pos] !== "}") { hex += this.src[this.pos]; this.adv(); }
|
|
221
|
-
result += String.fromCodePoint(parseInt(hex, 16));
|
|
222
|
-
} else {
|
|
223
|
-
const hex = this.src.substring(this.pos, this.pos + 4);
|
|
224
|
-
result += String.fromCharCode(parseInt(hex, 16));
|
|
225
|
-
this.pos += 3; this.col += 3;
|
|
226
|
-
}
|
|
227
|
-
break;
|
|
228
|
-
}
|
|
229
|
-
default: throw new SinthError(`Unknown escape sequence \\${e}`, loc);
|
|
230
|
-
}
|
|
231
|
-
this.adv();
|
|
232
|
-
} else {
|
|
233
|
-
result += this.src[this.pos];
|
|
234
|
-
this.adv();
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
if (this.pos >= this.src.length) throw new SinthError("Unterminated string literal", loc);
|
|
238
|
-
this.adv();
|
|
239
|
-
return { type: TT.STRING, value: result, loc };
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
private readNumber(loc: Loc): Token {
|
|
243
|
-
let s = "";
|
|
244
|
-
if (this.src[this.pos] === "-") { s += "-"; this.adv(); }
|
|
245
|
-
while (this.isDigit(this.src[this.pos] ?? "")) { s += this.src[this.pos]; this.adv(); }
|
|
246
|
-
if (this.src[this.pos] === "." && this.isDigit(this.src[this.pos + 1] ?? "")) {
|
|
247
|
-
s += "."; this.adv();
|
|
248
|
-
while (this.isDigit(this.src[this.pos] ?? "")) { s += this.src[this.pos]; this.adv(); }
|
|
249
|
-
}
|
|
250
|
-
return { type: TT.NUMBER, value: s, loc };
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
private readIdent(loc: Loc): Token {
|
|
254
|
-
let s = "";
|
|
255
|
-
while (this.pos < this.src.length && this.isIdentCont(this.src[this.pos])) {
|
|
256
|
-
s += this.src[this.pos]; this.adv();
|
|
257
|
-
}
|
|
258
|
-
const kw = KEYWORDS[s];
|
|
259
|
-
return { type: kw !== undefined ? kw : TT.IDENT, value: s, loc };
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
private adv(): void {
|
|
263
|
-
const ch = this.src[this.pos++];
|
|
264
|
-
if (ch === "\n") { this.line++; this.col = 1; } else { this.col++; }
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
private single(type: TT, loc: Loc): Token {
|
|
268
|
-
this.adv();
|
|
269
|
-
return { type, value: this.src[this.pos - 1], loc };
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
private loc(): Loc { return { file: this.file, line: this.line, col: this.col }; }
|
|
273
|
-
private isDigit(c: string) { return c >= "0" && c <= "9"; }
|
|
274
|
-
private isIdentStart(c: string) { return /[a-zA-Z_$\u00C0-\uFFFF]/.test(c); }
|
|
275
|
-
private isIdentCont(c: string) { return /[a-zA-Z0-9_$\-\u00C0-\uFFFF]/.test(c); }
|
|
276
|
-
}
|