orvyn-lang 0.2.0 → 0.4.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/GUIDE.md +198 -0
- package/LANGUAGE.md +818 -30
- package/README.md +211 -250
- package/brand/orvyn-sunset-animated.svg +15 -0
- package/package.json +3 -1
- package/src/cli/orvyn.js +207 -16
- package/src/core/signal.js +104 -6
- package/src/devtools/runtime.js +25 -1
- package/src/lang/a11y.js +152 -0
- package/src/lang/auth.js +239 -0
- package/src/lang/bundle-native.js +10 -6
- package/src/lang/codegen.js +615 -58
- package/src/lang/device.js +41 -0
- package/src/lang/doctor.js +60 -0
- package/src/lang/env.js +27 -0
- package/src/lang/errors.js +513 -0
- package/src/lang/form.js +69 -8
- package/src/lang/index.js +2 -1
- package/src/lang/lexer.js +2 -0
- package/src/lang/list.js +44 -0
- package/src/lang/native/android/MainActivity.kt +65 -4
- package/src/lang/native/android/OrvynDevice.kt +163 -0
- package/src/lang/native/android/OrvynEngine.kt +176 -7
- package/src/lang/native/android/OrvynRenderer.kt +970 -50
- package/src/lang/native/android.js +74 -11
- package/src/lang/native/assets.js +39 -0
- package/src/lang/native/build.js +140 -63
- package/src/lang/native/compatibility.js +57 -0
- package/src/lang/native/config.js +108 -0
- package/src/lang/native/ios/OrvynApp.swift +6 -0
- package/src/lang/native/ios/OrvynEngine.swift +291 -4
- package/src/lang/native/ios/OrvynViews.swift +769 -41
- package/src/lang/native/ios.js +81 -2
- package/src/lang/native-runtime.js +541 -55
- package/src/lang/native-size.js +53 -0
- package/src/lang/page-files.js +71 -0
- package/src/lang/page.js +63 -0
- package/src/lang/parser.js +278 -15
- package/src/lang/prerender.js +300 -0
- package/src/lang/read-import.js +12 -0
- package/src/lang/router.js +266 -0
- package/src/lang/runtime-shared.js +796 -10
- package/src/lang/runtime.js +399 -51
- package/src/lang/select.js +13 -0
- package/src/lang/server-code.js +233 -0
- package/src/lang/server-export.js +78 -0
- package/src/lang/server-libs.js +53 -0
- package/src/lang/server.js +409 -0
- package/src/lang/style-check.js +60 -0
- package/src/lang/test-runner.js +134 -0
- package/src/lang/types.js +317 -0
- package/src/lang/vite-plugin.js +108 -6
- package/src/lang/web-overlay.js +578 -0
- package/src/lang/widgets.js +94 -0
- package/src/lang/words.js +38 -0
- package/src/runtime/animations.js +24 -0
- package/src/runtime/appear.js +73 -0
- package/src/runtime/marquee.js +57 -0
- package/src/runtime/mount.js +120 -27
- package/src/runtime/patch.js +90 -11
- package/src/runtime/style.js +50 -9
- package/src/styling/fluid.js +129 -6
- package/src/styling/spec.js +319 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
function nativeSize(value, viewport = globalThis.__orvynViewport) {
|
|
2
|
+
const width = Number(viewport?.width);
|
|
3
|
+
const height = Number(viewport?.height);
|
|
4
|
+
const atom = (part) => {
|
|
5
|
+
const match = part.trim().match(/^(-?\d+(?:\.\d+)?)(px|vw|vh|%)?$/);
|
|
6
|
+
if (!match) return void 0;
|
|
7
|
+
const amount = Number(match[1]);
|
|
8
|
+
switch (match[2]) {
|
|
9
|
+
case "vw":
|
|
10
|
+
case "%":
|
|
11
|
+
return width > 0 ? width * amount / 100 : void 0;
|
|
12
|
+
case "vh":
|
|
13
|
+
return height > 0 ? height * amount / 100 : void 0;
|
|
14
|
+
default:
|
|
15
|
+
return amount;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const pair = value.match(/^(min|max)\(\s*([^,]+)\s*,\s*([^,]+)\s*\)$/);
|
|
19
|
+
if (pair) {
|
|
20
|
+
const a = atom(pair[2]), b = atom(pair[3]);
|
|
21
|
+
return a === void 0 || b === void 0 ? void 0 : pair[1] === "min" ? Math.min(a, b) : Math.max(a, b);
|
|
22
|
+
}
|
|
23
|
+
const clamp = value.match(/^clamp\(\s*([^,]+)\s*,\s*([^,]+)\s*,\s*([^,]+)\s*\)$/);
|
|
24
|
+
if (clamp) {
|
|
25
|
+
const lo = atom(clamp[1]), mid = atom(clamp[2]), hi = atom(clamp[3]);
|
|
26
|
+
return lo === void 0 || mid === void 0 || hi === void 0 ? void 0 : Math.max(lo, Math.min(mid, hi));
|
|
27
|
+
}
|
|
28
|
+
const calc = value.match(/^calc\(\s*([^+\-]+)\s*([+\-])\s*([^+\-]+)\s*\)$/);
|
|
29
|
+
if (calc) {
|
|
30
|
+
const a = atom(calc[1]), b = atom(calc[3]);
|
|
31
|
+
return a === void 0 || b === void 0 ? void 0 : calc[2] === "+" ? a + b : a - b;
|
|
32
|
+
}
|
|
33
|
+
if (/(?:vw|vh)$/.test(value.trim())) return atom(value);
|
|
34
|
+
return /^-?\d+(?:\.\d+)?px$/.test(value.trim()) ? atom(value) : void 0;
|
|
35
|
+
}
|
|
36
|
+
function cssParts(value) {
|
|
37
|
+
const parts = [];
|
|
38
|
+
let depth = 0, current = "";
|
|
39
|
+
for (const ch of value.trim()) {
|
|
40
|
+
if (ch === "(") depth++;
|
|
41
|
+
if (ch === ")") depth--;
|
|
42
|
+
if (/\s/.test(ch) && depth === 0) {
|
|
43
|
+
if (current) parts.push(current);
|
|
44
|
+
current = "";
|
|
45
|
+
} else current += ch;
|
|
46
|
+
}
|
|
47
|
+
if (current) parts.push(current);
|
|
48
|
+
return parts;
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
cssParts,
|
|
52
|
+
nativeSize
|
|
53
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { parse } from "./parser.js";
|
|
4
|
+
function withPageFiles(source, entry) {
|
|
5
|
+
if (basename(entry) !== "app.orv") return source;
|
|
6
|
+
const root = join(dirname(entry), "pages");
|
|
7
|
+
if (!existsSync(root)) return source;
|
|
8
|
+
const declared = new Set(parse(source).body.flatMap((d) => d.kind === "Use" ? d.names : d.kind === "Screen" ? [d.name] : []));
|
|
9
|
+
const imports = [];
|
|
10
|
+
const visit = (dir) => {
|
|
11
|
+
for (const name of readdirSync(dir).sort()) {
|
|
12
|
+
const full = join(dir, name);
|
|
13
|
+
if (statSync(full).isDirectory()) {
|
|
14
|
+
visit(full);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (name !== "index.orv") continue;
|
|
18
|
+
const route = basename(dirname(full)).replace(/[-_]/g, "").toLowerCase();
|
|
19
|
+
const screens = parse(readFileSync(full, "utf-8")).body.filter((d) => d.kind === "Screen" && d.type === "screen");
|
|
20
|
+
for (const screen of screens) {
|
|
21
|
+
if (screen.kind !== "Screen" || screen.type !== "screen") continue;
|
|
22
|
+
if (screen.name.toLowerCase() !== route) continue;
|
|
23
|
+
if (declared.has(screen.name)) continue;
|
|
24
|
+
const path = "./" + relative(dirname(entry), full).split(sep).join("/").replace(/\.orv$/, "");
|
|
25
|
+
imports.push(`use ${screen.name} from ${JSON.stringify(path)}`);
|
|
26
|
+
declared.add(screen.name);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
visit(root);
|
|
31
|
+
return imports.length ? `${source}
|
|
32
|
+
${imports.join("\n")}
|
|
33
|
+
` : source;
|
|
34
|
+
}
|
|
35
|
+
function collectProjectPrograms(entry) {
|
|
36
|
+
const seen = /* @__PURE__ */ new Set();
|
|
37
|
+
const items = [];
|
|
38
|
+
const visit = (file) => {
|
|
39
|
+
file = resolve(file);
|
|
40
|
+
if (seen.has(file) || !existsSync(file)) return;
|
|
41
|
+
seen.add(file);
|
|
42
|
+
const program = parse(withPageFiles(readFileSync(file, "utf-8"), file));
|
|
43
|
+
items.push({ file, program });
|
|
44
|
+
for (const decl of program.body) {
|
|
45
|
+
if (decl.kind !== "Use" || !decl.path.startsWith(".") || /\.(m?js|ts)$/.test(decl.path)) continue;
|
|
46
|
+
const base = resolve(dirname(file), decl.path.endsWith(".orv") ? decl.path : `${decl.path}.orv`);
|
|
47
|
+
const next = existsSync(base) ? base : resolve(dirname(file), decl.path, "index.orv");
|
|
48
|
+
if (existsSync(next)) visit(next);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
visit(entry);
|
|
52
|
+
return items;
|
|
53
|
+
}
|
|
54
|
+
function projectScreens(file) {
|
|
55
|
+
let dir = dirname(resolve(file));
|
|
56
|
+
while (!existsSync(join(dir, "app.orv"))) {
|
|
57
|
+
const up = dirname(dir);
|
|
58
|
+
if (up === dir) return [];
|
|
59
|
+
dir = up;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
return collectProjectPrograms(join(dir, "app.orv")).flatMap(({ program }) => program.body.flatMap((d) => d.kind === "Screen" && d.type === "screen" ? [d.name] : []));
|
|
63
|
+
} catch {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export {
|
|
68
|
+
collectProjectPrograms,
|
|
69
|
+
projectScreens,
|
|
70
|
+
withPageFiles
|
|
71
|
+
};
|
package/src/lang/page.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
function structuredData(page, url) {
|
|
2
|
+
const article = page.type === "article";
|
|
3
|
+
const text = (value) => value ? String(value) : void 0;
|
|
4
|
+
const identity = {
|
|
5
|
+
"@type": "Person",
|
|
6
|
+
name: text(page.name ?? page.title),
|
|
7
|
+
description: text(page.description),
|
|
8
|
+
image: text(page.image),
|
|
9
|
+
jobTitle: text(page.jobTitle),
|
|
10
|
+
sameAs: Array.isArray(page.sameAs) ? page.sameAs.map(String) : void 0,
|
|
11
|
+
url: url || void 0
|
|
12
|
+
};
|
|
13
|
+
if (page.type === "person") return { "@context": "https://schema.org", ...identity };
|
|
14
|
+
if (page.type === "profile") return { "@context": "https://schema.org", "@type": "ProfilePage", name: text(page.title), description: text(page.description), mainEntity: identity, url: url || void 0 };
|
|
15
|
+
return {
|
|
16
|
+
"@context": "https://schema.org",
|
|
17
|
+
"@type": article ? "Article" : "WebPage",
|
|
18
|
+
[article ? "headline" : "name"]: text(page.title),
|
|
19
|
+
description: text(page.description),
|
|
20
|
+
image: text(page.image),
|
|
21
|
+
author: page.author ? { "@type": "Person", name: String(page.author) } : void 0,
|
|
22
|
+
datePublished: text(page.date),
|
|
23
|
+
url: url || void 0
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function applyPage(page) {
|
|
27
|
+
const doc = globalThis.document;
|
|
28
|
+
if (!doc?.head?.querySelector) return;
|
|
29
|
+
const head = (selector, tag, attributes, content, key = "content") => {
|
|
30
|
+
let el = doc.head.querySelector(selector);
|
|
31
|
+
if (!content) return el?.remove();
|
|
32
|
+
if (!el) {
|
|
33
|
+
el = doc.createElement(tag);
|
|
34
|
+
for (const [name, value] of Object.entries(attributes)) el.setAttribute(name, value);
|
|
35
|
+
doc.head.appendChild(el);
|
|
36
|
+
}
|
|
37
|
+
el.setAttribute(key, String(content));
|
|
38
|
+
};
|
|
39
|
+
const meta = (kind, name, content) => head(`meta[${kind}="${name}"]`, "meta", { [kind]: name }, content);
|
|
40
|
+
if (page.title) doc.title = String(page.title);
|
|
41
|
+
const location = globalThis.location;
|
|
42
|
+
const url = location?.origin ? `${location.origin}${location.pathname}` : "";
|
|
43
|
+
meta("name", "description", page.description);
|
|
44
|
+
meta("property", "og:title", page.title);
|
|
45
|
+
meta("property", "og:description", page.description);
|
|
46
|
+
meta("property", "og:image", page.image);
|
|
47
|
+
meta("property", "og:url", url);
|
|
48
|
+
meta("name", "twitter:card", page.image ? "summary_large_image" : "summary");
|
|
49
|
+
meta("name", "robots", page.noindex ? "noindex" : "");
|
|
50
|
+
head('link[rel="canonical"]', "link", { rel: "canonical" }, url, "href");
|
|
51
|
+
let data = doc.head.querySelector("script[data-orvyn-page]");
|
|
52
|
+
if (!data) {
|
|
53
|
+
data = doc.createElement("script");
|
|
54
|
+
data.setAttribute("type", "application/ld+json");
|
|
55
|
+
data.setAttribute("data-orvyn-page", "");
|
|
56
|
+
doc.head.appendChild(data);
|
|
57
|
+
}
|
|
58
|
+
data.textContent = JSON.stringify(structuredData(page, url));
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
applyPage,
|
|
62
|
+
structuredData
|
|
63
|
+
};
|
package/src/lang/parser.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OrvynSyntaxError, tokenize } from "./lexer.js";
|
|
2
|
+
const APPEAR_PRESET_NAMES = ["fade", "fade-up", "fade-down", "slide-left", "slide-right", "scale"];
|
|
2
3
|
const BINARY_PRECEDENCE = {
|
|
3
4
|
"??": 1,
|
|
4
5
|
"||": 1,
|
|
@@ -16,6 +17,7 @@ const BINARY_PRECEDENCE = {
|
|
|
16
17
|
"%": 6
|
|
17
18
|
};
|
|
18
19
|
const ASSIGN_OPS = /* @__PURE__ */ new Set(["=", "+=", "-=", "*=", "/="]);
|
|
20
|
+
const AUTH_KEYS = ["login", "home", "logout", "refresh", "api", "keepSaved", "onLogout", "header", "scheme"];
|
|
19
21
|
class Parser {
|
|
20
22
|
tokens;
|
|
21
23
|
pos = 0;
|
|
@@ -99,21 +101,72 @@ class Parser {
|
|
|
99
101
|
}
|
|
100
102
|
parseDecl() {
|
|
101
103
|
const token = this.peek();
|
|
102
|
-
const saved = token.type === "ident" && token.value === "saved" && this.is("state", 1);
|
|
103
|
-
|
|
104
|
-
|
|
104
|
+
const saved = token.type === "ident" && token.value === "saved" && (this.is("state", 1) || this.is("(", 1));
|
|
105
|
+
let savedVersion;
|
|
106
|
+
let savedMigrate;
|
|
107
|
+
if (saved) {
|
|
108
|
+
this.next();
|
|
109
|
+
if (this.eat("(")) {
|
|
110
|
+
this.groupDepth++;
|
|
111
|
+
const verToken = this.peek();
|
|
112
|
+
savedVersion = this.parseExpr();
|
|
113
|
+
if (savedVersion.kind !== "Number" || !Number.isInteger(savedVersion.value) || savedVersion.value < 1) {
|
|
114
|
+
this.fail("saved version must be a positive whole number, like saved(2) state ...", verToken);
|
|
115
|
+
}
|
|
116
|
+
if (this.eat(",")) savedMigrate = this.parseExpr();
|
|
117
|
+
this.expect(")", "to close saved(version)");
|
|
118
|
+
this.groupDepth--;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (token.type === "ident" && token.value === "type" && this.peek(1).type === "ident" && this.is("{", 2)) {
|
|
122
|
+
this.next();
|
|
123
|
+
const name = this.next();
|
|
124
|
+
if (!/^[A-Z]/.test(name.value)) this.fail(`Type names start with a capital letter, like "${capitalize(name.value)}"`, name);
|
|
125
|
+
this.next();
|
|
126
|
+
const fields = [];
|
|
127
|
+
this.skipNewlines();
|
|
128
|
+
while (!this.is("}")) {
|
|
129
|
+
const field = this.peek();
|
|
130
|
+
if (field.type === "eof") this.fail('Missing "}" to close the type', field);
|
|
131
|
+
if (field.type !== "ident" && field.type !== "keyword") this.fail("Expected a field, like name: text", field);
|
|
132
|
+
this.next();
|
|
133
|
+
this.expect(":", `after "${field.value}"`);
|
|
134
|
+
const ref = this.parseTypeRef();
|
|
135
|
+
const optional = this.eat("?");
|
|
136
|
+
if (fields.some((f) => f.name === field.value)) this.fail(`"${field.value}" is listed twice`, field);
|
|
137
|
+
fields.push({ name: field.value, type: ref, optional, line: field.line, col: field.col });
|
|
138
|
+
this.eat(",");
|
|
139
|
+
this.skipNewlines();
|
|
140
|
+
}
|
|
141
|
+
this.next();
|
|
142
|
+
this.endOfLine("after type");
|
|
143
|
+
return { kind: "Type", name: name.value, fields, line: token.line, col: token.col };
|
|
144
|
+
}
|
|
145
|
+
if (saved && this.expect("state", 'after "saved(...)"') || this.eat("state")) {
|
|
105
146
|
const name = this.expectIdent('after "state"');
|
|
147
|
+
const typed = this.optionalType();
|
|
106
148
|
this.expect("=", `after "state ${name.value}"`);
|
|
107
149
|
const init = this.parseExpr();
|
|
108
150
|
this.endOfLine("after state declaration");
|
|
109
|
-
return {
|
|
151
|
+
return {
|
|
152
|
+
kind: "State",
|
|
153
|
+
name: name.value,
|
|
154
|
+
...typed,
|
|
155
|
+
init,
|
|
156
|
+
saved,
|
|
157
|
+
...savedVersion ? { savedVersion } : {},
|
|
158
|
+
...savedMigrate ? { savedMigrate } : {},
|
|
159
|
+
line: token.line,
|
|
160
|
+
col: token.col
|
|
161
|
+
};
|
|
110
162
|
}
|
|
111
163
|
if (this.eat("let")) {
|
|
112
164
|
const name = this.expectIdent('after "let"');
|
|
165
|
+
const typed = this.optionalType();
|
|
113
166
|
this.expect("=", `after "let ${name.value}"`);
|
|
114
167
|
const init = this.parseExpr();
|
|
115
168
|
this.endOfLine("after let declaration");
|
|
116
|
-
return { kind: "Const", name: name.value, init, line: token.line, col: token.col };
|
|
169
|
+
return { kind: "Const", name: name.value, ...typed, init, line: token.line, col: token.col };
|
|
117
170
|
}
|
|
118
171
|
if (this.eat("use")) {
|
|
119
172
|
const names = [];
|
|
@@ -129,15 +182,69 @@ class Parser {
|
|
|
129
182
|
this.endOfLine("after use");
|
|
130
183
|
return { kind: "Use", names, path: path.value, line: token.line, col: token.col };
|
|
131
184
|
}
|
|
185
|
+
if (token.type === "ident" && token.value === "auth" && this.is("{", 1)) {
|
|
186
|
+
this.next();
|
|
187
|
+
this.next();
|
|
188
|
+
this.groupDepth++;
|
|
189
|
+
const entries = [];
|
|
190
|
+
const seen = /* @__PURE__ */ new Set();
|
|
191
|
+
while (!this.is("}")) {
|
|
192
|
+
const key = this.peek();
|
|
193
|
+
if (key.type === "eof") this.fail('Missing "}" to close auth { ... }', key);
|
|
194
|
+
if (key.type !== "ident" || !AUTH_KEYS.includes(key.value)) {
|
|
195
|
+
this.fail(`"${key.value}" is not an auth setting. Use: ${AUTH_KEYS.join(", ")}`, key);
|
|
196
|
+
}
|
|
197
|
+
if (seen.has(key.value)) this.fail(`"${key.value}" is set twice in auth { ... }`, key);
|
|
198
|
+
seen.add(key.value);
|
|
199
|
+
this.next();
|
|
200
|
+
this.expect(":", `after "${key.value}", like ${key.value}: ...`);
|
|
201
|
+
entries.push({ key: key.value, value: this.parseExpr() });
|
|
202
|
+
this.eat(",");
|
|
203
|
+
}
|
|
204
|
+
this.expect("}", "to close auth { ... }");
|
|
205
|
+
this.groupDepth--;
|
|
206
|
+
this.endOfLine("after auth { ... }");
|
|
207
|
+
const pos = { line: token.line, col: token.col };
|
|
208
|
+
const call = {
|
|
209
|
+
kind: "Call",
|
|
210
|
+
callee: { kind: "Member", object: { kind: "Ident", name: "auth", ...pos }, property: "setup", ...pos },
|
|
211
|
+
args: [{ kind: "Object", entries, ...pos }],
|
|
212
|
+
...pos
|
|
213
|
+
};
|
|
214
|
+
return { kind: "Start", body: [{ kind: "ExprStmt", expr: call, ...pos }], ...pos };
|
|
215
|
+
}
|
|
216
|
+
if (token.type === "ident" && token.value === "test" && this.peek(1).type === "string") {
|
|
217
|
+
this.next();
|
|
218
|
+
const name = this.next();
|
|
219
|
+
return { kind: "Test", name: name.value, body: this.parseBlock(), line: token.line, col: token.col };
|
|
220
|
+
}
|
|
132
221
|
if (this.eat("start")) {
|
|
133
222
|
const body = this.parseBlock();
|
|
134
223
|
return { kind: "Start", body, line: token.line, col: token.col };
|
|
135
224
|
}
|
|
225
|
+
if (token.type === "ident" && token.value === "animation" && this.peek(1).type === "ident" && this.is("{", 2)) {
|
|
226
|
+
this.next();
|
|
227
|
+
const name = this.expectIdent('after "animation"');
|
|
228
|
+
if (APPEAR_PRESET_NAMES.includes(name.value)) {
|
|
229
|
+
this.fail(`"${name.value}" is already a built-in animation. Pick another name, like "my${capitalize(name.value)}"`, name);
|
|
230
|
+
}
|
|
231
|
+
const from = this.parseExpr();
|
|
232
|
+
const pos = { line: token.line, col: token.col };
|
|
233
|
+
const init = {
|
|
234
|
+
kind: "Call",
|
|
235
|
+
callee: { kind: "Ident", name: "defineAnimation", ...pos },
|
|
236
|
+
args: [{ kind: "String", parts: [name.value], ...pos }, from],
|
|
237
|
+
...pos
|
|
238
|
+
};
|
|
239
|
+
return { kind: "Const", name: name.value, init, ...pos };
|
|
240
|
+
}
|
|
136
241
|
if (this.eat("fn")) {
|
|
137
242
|
const name = this.expectIdent('after "fn"');
|
|
243
|
+
this.paramTypes = {};
|
|
138
244
|
const params = this.parseParams();
|
|
245
|
+
const paramTypes = this.paramTypes;
|
|
139
246
|
const body = this.parseBlock();
|
|
140
|
-
return { kind: "Fn", name: name.value, params, body, line: token.line, col: token.col };
|
|
247
|
+
return { kind: "Fn", name: name.value, params, ...Object.keys(paramTypes).length ? { paramTypes } : {}, body, line: token.line, col: token.col };
|
|
141
248
|
}
|
|
142
249
|
if (this.is("screen") || this.is("component")) {
|
|
143
250
|
const type = this.next().value;
|
|
@@ -145,21 +252,53 @@ class Parser {
|
|
|
145
252
|
if (!/^[A-Z]/.test(name.value)) {
|
|
146
253
|
this.fail(`${type} names must start with a capital letter, like "${capitalize(name.value)}"`, name);
|
|
147
254
|
}
|
|
255
|
+
this.paramTypes = {};
|
|
148
256
|
const params = this.is("(") ? this.parseParams() : [];
|
|
149
|
-
const
|
|
150
|
-
|
|
257
|
+
const paramTypes = this.paramTypes;
|
|
258
|
+
let requiresAuth = false;
|
|
259
|
+
const req = this.peek();
|
|
260
|
+
if (req.type === "ident" && req.value === "requires") {
|
|
261
|
+
this.next();
|
|
262
|
+
if (type !== "screen") this.fail('Only screens can use "requires auth", not components', req);
|
|
263
|
+
const what = this.peek();
|
|
264
|
+
if (what.type !== "ident" || what.value !== "auth") this.fail('Write "requires auth", like: screen Profile requires auth { ... }', what);
|
|
265
|
+
this.next();
|
|
266
|
+
requiresAuth = true;
|
|
267
|
+
}
|
|
268
|
+
const { locals, nodes, page, prebuild } = this.parseScreenBody();
|
|
269
|
+
if (prebuild && (type !== "screen" || params.length !== 1)) {
|
|
270
|
+
this.fail('"prebuild" belongs in a screen with one value, like: screen Product(id) { prebuild: [1, 2] ... }', name);
|
|
271
|
+
}
|
|
272
|
+
if (page && type !== "screen") this.fail('Only screens can have "page", not components', name);
|
|
273
|
+
return { kind: "Screen", ...page ? { page } : {}, ...prebuild ? { prebuild } : {}, type, name: name.value, params, ...Object.keys(paramTypes).length ? { paramTypes } : {}, ...requiresAuth ? { requiresAuth } : {}, ...locals.length ? { locals } : {}, body: nodes, line: token.line, col: token.col };
|
|
151
274
|
}
|
|
152
275
|
this.fail(
|
|
153
276
|
`Unexpected ${describe(token)}. A file can only contain state, let, fn, screen, component, start or use`,
|
|
154
277
|
token
|
|
155
278
|
);
|
|
156
279
|
}
|
|
280
|
+
paramTypes = {};
|
|
281
|
+
// text, number, yesno, Product, list of Product
|
|
282
|
+
parseTypeRef() {
|
|
283
|
+
const name = this.expectIdent("for a type, like text or Product");
|
|
284
|
+
if (name.value === "list" && this.peek().type === "ident" && this.peek().value === "of") {
|
|
285
|
+
this.next();
|
|
286
|
+
return { list: this.parseTypeRef() };
|
|
287
|
+
}
|
|
288
|
+
return { name: name.value };
|
|
289
|
+
}
|
|
290
|
+
// the ": Product" part of `state cart: Product = ...`, when there is one
|
|
291
|
+
optionalType() {
|
|
292
|
+
return this.eat(":") ? { type: this.parseTypeRef() } : {};
|
|
293
|
+
}
|
|
157
294
|
parseParams() {
|
|
158
295
|
this.expect("(", "to start the parameter list");
|
|
159
296
|
this.groupDepth++;
|
|
160
297
|
const params = [];
|
|
161
298
|
while (!this.is(")")) {
|
|
162
|
-
|
|
299
|
+
const name = this.expectIdent("for a parameter").value;
|
|
300
|
+
params.push(name);
|
|
301
|
+
if (this.eat(":")) this.paramTypes[name] = this.parseTypeRef();
|
|
163
302
|
if (!this.eat(",")) break;
|
|
164
303
|
}
|
|
165
304
|
this.expect(")", "to close the parameter list");
|
|
@@ -182,10 +321,16 @@ class Parser {
|
|
|
182
321
|
}
|
|
183
322
|
parseStatement() {
|
|
184
323
|
const token = this.peek();
|
|
324
|
+
if (token.type === "ident" && token.value === "fail" && this.peek(1).type === "string") {
|
|
325
|
+
this.next();
|
|
326
|
+
const pos = { line: token.line, col: token.col };
|
|
327
|
+
return { kind: "ExprStmt", expr: { kind: "Call", callee: { kind: "Ident", name: "fail", ...pos }, args: [this.parseExpr()], ...pos }, ...pos };
|
|
328
|
+
}
|
|
185
329
|
if (this.eat("let")) {
|
|
186
330
|
const name = this.expectIdent('after "let"');
|
|
331
|
+
const typed = this.optionalType();
|
|
187
332
|
this.expect("=", `after "let ${name.value}"`);
|
|
188
|
-
return { kind: "Let", name: name.value, init: this.parseExpr(), line: token.line, col: token.col };
|
|
333
|
+
return { kind: "Let", name: name.value, ...typed, init: this.parseExpr(), line: token.line, col: token.col };
|
|
189
334
|
}
|
|
190
335
|
if (this.eat("if")) {
|
|
191
336
|
const cond = this.parseExpr();
|
|
@@ -212,6 +357,14 @@ class Parser {
|
|
|
212
357
|
const value = end.type === "newline" || this.is("}") ? null : this.parseExpr();
|
|
213
358
|
return { kind: "Return", value, line: token.line, col: token.col };
|
|
214
359
|
}
|
|
360
|
+
if (this.eat("try")) {
|
|
361
|
+
const body = this.parseBlock();
|
|
362
|
+
this.expect("catch", 'after "try { ... }"');
|
|
363
|
+
let catchName = null;
|
|
364
|
+
if (this.peek().type === "ident") catchName = this.next().value;
|
|
365
|
+
const catchBody = this.parseBlock();
|
|
366
|
+
return { kind: "Try", body, catchName, catchBody, line: token.line, col: token.col };
|
|
367
|
+
}
|
|
215
368
|
const expr = this.parseExpr();
|
|
216
369
|
const opToken = this.peek();
|
|
217
370
|
if (opToken.type === "punct" && ASSIGN_OPS.has(opToken.value)) {
|
|
@@ -225,12 +378,86 @@ class Parser {
|
|
|
225
378
|
return { kind: "ExprStmt", expr, line: token.line, col: token.col };
|
|
226
379
|
}
|
|
227
380
|
// --- view ----------------------------------------------------------------
|
|
381
|
+
parseScreenBody() {
|
|
382
|
+
let page;
|
|
383
|
+
let prebuild;
|
|
384
|
+
this.expect("{", "to start the view");
|
|
385
|
+
const locals = [];
|
|
386
|
+
const nodes = [];
|
|
387
|
+
this.skipNewlines();
|
|
388
|
+
while (!this.is("}")) {
|
|
389
|
+
if (this.peek().type === "eof") this.fail('Missing "}" to close the view', this.peek());
|
|
390
|
+
const token = this.peek();
|
|
391
|
+
if (token.type === "ident" && token.value === "prebuild" && this.is(":", 1)) {
|
|
392
|
+
if (prebuild) this.fail('A screen can have only one "prebuild" line', token);
|
|
393
|
+
this.next();
|
|
394
|
+
this.next();
|
|
395
|
+
prebuild = this.parseExpr();
|
|
396
|
+
this.endOfLine("after prebuild");
|
|
397
|
+
this.skipNewlines();
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
if (token.type === "ident" && token.value === "page" && this.peek(1).type === "ident" && this.is(":", 2)) {
|
|
401
|
+
if (page) this.fail('A screen can have only one "page" line', token);
|
|
402
|
+
this.next();
|
|
403
|
+
page = [];
|
|
404
|
+
do {
|
|
405
|
+
this.skipNewlines();
|
|
406
|
+
const key = this.expectIdent("in page");
|
|
407
|
+
if (!["title", "description", "image", "type", "author", "date", "noindex", "name", "jobTitle", "sameAs"].includes(key.value)) {
|
|
408
|
+
this.fail(`page has no "${key.value}". Use: title, description, image, type, author, date, noindex, name, jobTitle, sameAs`, key);
|
|
409
|
+
}
|
|
410
|
+
this.expect(":", `after "${key.value}"`);
|
|
411
|
+
page.push({ key: key.value, value: { kind: "Expr", expr: this.parseExpr() }, line: key.line, col: key.col });
|
|
412
|
+
} while (this.eat(","));
|
|
413
|
+
this.endOfLine("after page");
|
|
414
|
+
this.skipNewlines();
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const saved = token.type === "ident" && token.value === "saved" && (this.is("state", 1) || this.is("(", 1));
|
|
418
|
+
if (saved || this.is("state") || this.is("let")) {
|
|
419
|
+
const kind = saved ? "saved state" : token.value;
|
|
420
|
+
if (saved) {
|
|
421
|
+
this.fail("saved state belongs at the top level of a file, not inside a component or screen. Use state without saved here", token);
|
|
422
|
+
}
|
|
423
|
+
if (nodes.length > 0) {
|
|
424
|
+
this.fail(`Put ${kind} at the top of the component or screen, before any elements`, token);
|
|
425
|
+
}
|
|
426
|
+
if (this.eat("state")) {
|
|
427
|
+
const name = this.expectIdent('after "state"');
|
|
428
|
+
const typed = this.optionalType();
|
|
429
|
+
this.expect("=", `after "state ${name.value}"`);
|
|
430
|
+
locals.push({ kind: "State", name: name.value, ...typed, init: this.parseExpr(), saved: false, line: token.line, col: token.col });
|
|
431
|
+
} else {
|
|
432
|
+
this.next();
|
|
433
|
+
const name = this.expectIdent('after "let"');
|
|
434
|
+
const typed = this.optionalType();
|
|
435
|
+
this.expect("=", `after "let ${name.value}"`);
|
|
436
|
+
locals.push({ kind: "Const", name: name.value, ...typed, init: this.parseExpr(), line: token.line, col: token.col });
|
|
437
|
+
}
|
|
438
|
+
this.endOfLine(`after ${kind} declaration`);
|
|
439
|
+
this.skipNewlines();
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
nodes.push(this.parseViewNode());
|
|
443
|
+
this.endOfLine("after element");
|
|
444
|
+
this.skipNewlines();
|
|
445
|
+
}
|
|
446
|
+
this.next();
|
|
447
|
+
return { locals, nodes, page, prebuild };
|
|
448
|
+
}
|
|
228
449
|
parseViewBlock() {
|
|
229
450
|
this.expect("{", "to start the view");
|
|
230
451
|
const nodes = [];
|
|
231
452
|
this.skipNewlines();
|
|
232
453
|
while (!this.is("}")) {
|
|
233
454
|
if (this.peek().type === "eof") this.fail('Missing "}" to close the view', this.peek());
|
|
455
|
+
const token = this.peek();
|
|
456
|
+
const saved = token.type === "ident" && token.value === "saved" && (this.is("state", 1) || this.is("(", 1));
|
|
457
|
+
if (saved || this.is("state") || this.is("let")) {
|
|
458
|
+
const kind = saved ? "saved state" : token.value;
|
|
459
|
+
this.fail(`Put ${kind} at the top of the component or screen, outside elements, if, or for`, token);
|
|
460
|
+
}
|
|
234
461
|
nodes.push(this.parseViewNode());
|
|
235
462
|
this.endOfLine("after element");
|
|
236
463
|
this.skipNewlines();
|
|
@@ -253,8 +480,17 @@ class Parser {
|
|
|
253
480
|
const name = this.expectIdent('after "for"');
|
|
254
481
|
this.expect("in", `after "for ${name.value}"`);
|
|
255
482
|
const iter = this.parseExpr();
|
|
483
|
+
let key;
|
|
484
|
+
if (this.peek().type === "ident" && this.peek().value === "key") {
|
|
485
|
+
this.next();
|
|
486
|
+
key = this.parseExpr();
|
|
487
|
+
}
|
|
256
488
|
const body = this.parseViewBlock();
|
|
257
|
-
return { kind: "ViewFor", name: name.value, iter, body, line: token.line, col: token.col };
|
|
489
|
+
return { kind: "ViewFor", name: name.value, iter, ...key ? { key } : {}, body, line: token.line, col: token.col };
|
|
490
|
+
}
|
|
491
|
+
if (token.value === "slot" && token.type === "ident") {
|
|
492
|
+
this.next();
|
|
493
|
+
return { kind: "Element", name: "Slot", arg: null, props: [], children: [], line: token.line, col: token.col };
|
|
258
494
|
}
|
|
259
495
|
if (token.type !== "ident" || !/^[A-Z]/.test(token.value)) {
|
|
260
496
|
this.fail(
|
|
@@ -263,6 +499,9 @@ class Parser {
|
|
|
263
499
|
);
|
|
264
500
|
}
|
|
265
501
|
this.next();
|
|
502
|
+
if (this.is("(") && this.is(")", 1)) {
|
|
503
|
+
this.fail(`Use ${token.value} without () inside a view`, this.peek());
|
|
504
|
+
}
|
|
266
505
|
let arg = null;
|
|
267
506
|
const props = [];
|
|
268
507
|
const atArgsEnd = () => this.peek().type === "newline" || this.peek().type === "eof" || this.is("{") || this.is("}");
|
|
@@ -290,6 +529,10 @@ class Parser {
|
|
|
290
529
|
}
|
|
291
530
|
parsePropValue(key) {
|
|
292
531
|
const isEvent = /^on[A-Z]/.test(key);
|
|
532
|
+
if (["align", "justify", "textAlign"].includes(key) && this.is("start")) {
|
|
533
|
+
const token = this.next();
|
|
534
|
+
return { kind: "Expr", expr: { kind: "Ident", name: "start", line: token.line, col: token.col } };
|
|
535
|
+
}
|
|
293
536
|
if (isEvent && this.is("{")) {
|
|
294
537
|
return { kind: "Handler", body: this.parseBlock() };
|
|
295
538
|
}
|
|
@@ -324,6 +567,12 @@ class Parser {
|
|
|
324
567
|
}
|
|
325
568
|
parseUnary() {
|
|
326
569
|
const token = this.peek();
|
|
570
|
+
if (token.type === "ident" && token.value === "new" && this.peek(1).type === "ident") {
|
|
571
|
+
this.next();
|
|
572
|
+
const call = this.parsePostfix(this.parsePrimary());
|
|
573
|
+
if (call.kind !== "Call") this.fail("new needs a class call, like new Lenis()", token);
|
|
574
|
+
return { kind: "New", callee: call.callee, args: call.args, line: token.line, col: token.col };
|
|
575
|
+
}
|
|
327
576
|
if (this.eat("wait")) {
|
|
328
577
|
return { kind: "Wait", expr: this.parseUnary(), line: token.line, col: token.col };
|
|
329
578
|
}
|
|
@@ -350,10 +599,19 @@ class Parser {
|
|
|
350
599
|
this.next();
|
|
351
600
|
this.groupDepth++;
|
|
352
601
|
const args = [];
|
|
602
|
+
const named = [];
|
|
353
603
|
while (!this.is(")")) {
|
|
354
|
-
|
|
604
|
+
const key = this.peek();
|
|
605
|
+
if ((key.type === "ident" || key.type === "keyword") && this.is(":", 1)) {
|
|
606
|
+
this.next();
|
|
607
|
+
this.next();
|
|
608
|
+
named.push({ key: key.value, value: this.parseExpr() });
|
|
609
|
+
} else {
|
|
610
|
+
args.push(this.parseExpr());
|
|
611
|
+
}
|
|
355
612
|
if (!this.eat(",")) break;
|
|
356
613
|
}
|
|
614
|
+
if (named.length) args.push({ kind: "Object", entries: named, line: token.line, col: token.col });
|
|
357
615
|
this.expect(")", "to close the function call");
|
|
358
616
|
this.groupDepth--;
|
|
359
617
|
expr = { kind: "Call", callee: expr, args, line: token.line, col: token.col };
|
|
@@ -417,8 +675,12 @@ class Parser {
|
|
|
417
675
|
this.fail('Expected a field name, like { name: "Asha" }', key);
|
|
418
676
|
}
|
|
419
677
|
this.next();
|
|
420
|
-
this.
|
|
421
|
-
|
|
678
|
+
if (key.type === "ident" && (this.is(",") || this.is("}"))) {
|
|
679
|
+
entries.push({ key: key.value, value: { kind: "Ident", name: key.value, line: key.line, col: key.col } });
|
|
680
|
+
} else {
|
|
681
|
+
this.expect(":", `after "${key.value}"`);
|
|
682
|
+
entries.push({ key: key.value, value: this.parseExpr() });
|
|
683
|
+
}
|
|
422
684
|
if (!this.eat(",")) break;
|
|
423
685
|
}
|
|
424
686
|
this.expect("}", "to close the object");
|
|
@@ -484,7 +746,7 @@ class Parser {
|
|
|
484
746
|
return raw.map((part, index) => {
|
|
485
747
|
if (index % 2 === 0) return part;
|
|
486
748
|
try {
|
|
487
|
-
const inner = new Parser(tokenize(part));
|
|
749
|
+
const inner = new Parser(tokenize(part).map((t) => ({ ...t, line: token.line, col: token.col })));
|
|
488
750
|
const expr = inner.parseExpr();
|
|
489
751
|
if (inner.peek().type !== "eof") {
|
|
490
752
|
inner.fail(`Unexpected ${describe(inner.peek())}`, inner.peek());
|
|
@@ -516,5 +778,6 @@ function parse(source) {
|
|
|
516
778
|
return new Parser(tokenize(source)).parseProgram();
|
|
517
779
|
}
|
|
518
780
|
export {
|
|
781
|
+
APPEAR_PRESET_NAMES,
|
|
519
782
|
parse
|
|
520
783
|
};
|