kaleidl 0.1.2 → 0.1.3

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/package.json CHANGED
@@ -1,17 +1,35 @@
1
1
  {
2
2
  "name": "kaleidl",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "MN 112 labeling engine (TypeScript port of kaleidl)",
5
5
  "license": "MIT",
6
6
  "repository": "github:kaleidl/kaleidl-ts",
7
7
  "files": [
8
+ "dist",
8
9
  "src"
9
10
  ],
10
11
  "type": "module",
11
- "main": "src/index.ts",
12
- "types": "src/index.ts",
12
+ "main": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js",
18
+ "require": "./dist/index.cjs"
19
+ },
20
+ "./node": {
21
+ "types": "./dist/node.d.ts",
22
+ "import": "./dist/node.js",
23
+ "require": "./dist/node.cjs"
24
+ },
25
+ "./src/*": "./src/*",
26
+ "./package.json": "./package.json"
27
+ },
13
28
  "scripts": {
14
- "test": "node --experimental-strip-types --import tsx test/golden.test.ts && node --experimental-strip-types --import tsx test/lml.test.ts"
29
+ "build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && node scripts/finalize-cjs.mjs && node scripts/check-artifact.mjs",
30
+ "prepack": "npm run build",
31
+ "test": "node --experimental-strip-types --import tsx test/golden.test.ts && node --experimental-strip-types --import tsx test/lml.test.ts",
32
+ "pack-smoke": "bash scripts/pack-smoke.sh"
15
33
  },
16
34
  "dependencies": {
17
35
  "js-yaml": "^4.1.0",
package/src/lml.ts CHANGED
@@ -31,16 +31,27 @@ function unquote(raw: string): string {
31
31
  return raw.slice(1, -1).replace(/\\(.)/g, "$1");
32
32
  }
33
33
 
34
+ /**
35
+ * The items of a repetition site (the instance spine, `block`,
36
+ * `array`). The parslet shape law flattens a single iteration to the
37
+ * bare node and an empty repetition to null — normalize to a list.
38
+ */
39
+ function items(node: any): any[] {
40
+ if (Array.isArray(node)) return node;
41
+ if (node && typeof node === "object") return [node];
42
+ return [];
43
+ }
44
+
34
45
  function mapValue(node: ValueNode): any {
35
46
  if ("string" in node) return unquote(node.string.value);
36
47
  if ("integer" in node) return Number(node.integer.value);
37
48
  if ("symbol" in node) return { ref: node.symbol.value };
38
49
  if ("ref" in node) return { ref: node.ref.symbol.value };
39
50
  if ("prose" in node) return node.prose.value.replace(/^[{ \n\r\t]+/, "").replace(/[}\s]+$/, "");
40
- if ("array" in node) return node.array.map(mapValue);
51
+ if ("array" in node) return items(node.array).map(mapValue);
41
52
  if ("block" in node) {
42
53
  const out: Record<string, any> = {};
43
- for (const entry of node.block) out[entry.attr.key.value] = mapValue(entry.attr as ValueNode);
54
+ for (const entry of items(node.block)) out[entry.attr.key.value] = mapValue(entry.attr as ValueNode);
44
55
  return out;
45
56
  }
46
57
  throw new Error("unreachable LML value kind");
@@ -49,8 +60,7 @@ function mapValue(node: ValueNode): any {
49
60
  /** Map a parsanol `lml` parse tree to the Profile the engine consumes. */
50
61
  export function profileFromLmlTree(tree: any): Profile {
51
62
  const instances: Record<string, Instance> = {};
52
- const nodes = Array.isArray(tree) ? tree : [];
53
- for (const node of nodes) {
63
+ for (const node of items(tree)) {
54
64
  const inst = node?.instance;
55
65
  if (!inst) throw new Error("LML parse tree: expected an instance node");
56
66
  // The quoted-name header form (Type "Name") captures the name with
@@ -60,7 +70,7 @@ export function profileFromLmlTree(tree: any): Profile {
60
70
  const type = inst.type?.value;
61
71
  if (!name || !type) throw new Error("LML parse tree: instance needs name and type");
62
72
  const properties: Record<string, any> = {};
63
- for (const entry of inst.block ?? []) {
73
+ for (const entry of items(inst.block)) {
64
74
  const attr = entry?.attr;
65
75
  if (!attr) continue;
66
76
  properties[attr.key.value] = mapValue(attr as ValueNode);
@@ -0,0 +1,11 @@
1
+ /**
2
+ * kaleidl node entry (CJS build): the twin of node.ts. The emitted
3
+ * file is CommonJS, so the bare-specifier resolver is the real
4
+ * `require` — import.meta cannot appear here, and the loaders live
5
+ * in node-loaders.ts. The ESM build excludes this file.
6
+ */
7
+ import { nodeLoaders } from "./node-loaders.js";
8
+
9
+ export const { loadProfileFile, loadLml, loadLmlFile } = nodeLoaders(
10
+ (spec) => require.resolve(spec),
11
+ );
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Shared node loaders over the pure core, parameterized by the host
3
+ * module system's bare-specifier resolver. The ESM entry (node.ts)
4
+ * injects createRequire(import.meta.url) and the CJS entry (node-cjs.ts)
5
+ * injects the real CommonJS require: import.meta cannot appear in the
6
+ * CJS build, so the resolver cannot live in this shared file.
7
+ *
8
+ * The browser-facing entry is src/index.ts — it must stay platform-free,
9
+ * the same split rhoella-ts uses.
10
+ */
11
+ import * as fs from "node:fs";
12
+ import { loadProfile, type Profile } from "./index.js";
13
+ import { LML_ARTIFACT, profileFromLmlTree } from "./lml.js";
14
+
15
+ export interface NodeLoaders {
16
+ loadProfileFile(path: string): Profile;
17
+ loadLml(source: string): Promise<Profile>;
18
+ loadLmlFile(path: string): Promise<Profile>;
19
+ }
20
+
21
+ export function nodeLoaders(resolve: (spec: string) => string): NodeLoaders {
22
+ let lmlRuntime: Promise<any> | null = null;
23
+
24
+ async function lmlParse(source: string): Promise<any> {
25
+ if (!lmlRuntime) {
26
+ lmlRuntime = (async () => {
27
+ const { initialize, PargRuntime } = await import("parsanol/browser");
28
+ await initialize(fs.readFileSync(resolve("parsanol/wasm")));
29
+ return new PargRuntime(LML_ARTIFACT);
30
+ })();
31
+ }
32
+ const runtime = await lmlRuntime;
33
+ return runtime.parseShape(source, "main");
34
+ }
35
+
36
+ async function loadLml(source: string): Promise<Profile> {
37
+ return profileFromLmlTree(await lmlParse(source));
38
+ }
39
+
40
+ return {
41
+ loadProfileFile(path: string): Profile {
42
+ return loadProfile(fs.readFileSync(path, "utf-8"));
43
+ },
44
+ loadLml,
45
+ loadLmlFile(path: string): Promise<Profile> {
46
+ return loadLml(fs.readFileSync(path, "utf-8"));
47
+ },
48
+ };
49
+ }
package/src/node.ts CHANGED
@@ -1,36 +1,15 @@
1
1
  /**
2
- * kaleidl node entry: file-system loaders over the pure core. The
3
- * browser-facing entry is src/index.ts — it must stay platform-free,
4
- * the same split rhoella-ts uses.
2
+ * kaleidl node entry (ESM build): file-system loaders over the pure
3
+ * core. The browser-facing entry is src/index.ts — it must stay
4
+ * platform-free.
5
+ *
6
+ * The CJS twin lives in node-cjs.ts: import.meta cannot appear in the
7
+ * CJS build, so each module system provides its own bare-specifier
8
+ * resolver and the loaders themselves live in node-loaders.ts.
5
9
  */
6
10
  import { createRequire } from "node:module";
7
- import * as fs from "node:fs";
8
- import { loadProfile, type Profile } from "./index.js";
9
- import { LML_ARTIFACT, profileFromLmlTree } from "./lml.js";
11
+ import { nodeLoaders } from "./node-loaders.js";
10
12
 
11
- export function loadProfileFile(path: string): Profile {
12
- return loadProfile(fs.readFileSync(path, "utf-8"));
13
- }
14
-
15
- let lmlRuntime: Promise<any> | null = null;
16
-
17
- async function lmlParse(source: string): Promise<any> {
18
- if (!lmlRuntime) {
19
- lmlRuntime = (async () => {
20
- const require = createRequire(import.meta.url);
21
- const { initialize, PargRuntime } = await import("parsanol/browser");
22
- await initialize(fs.readFileSync(require.resolve("parsanol/wasm")));
23
- return new PargRuntime(LML_ARTIFACT);
24
- })();
25
- }
26
- const runtime = await lmlRuntime;
27
- return runtime.parseShape(source, "main");
28
- }
29
-
30
- export async function loadLml(source: string): Promise<Profile> {
31
- return profileFromLmlTree(await lmlParse(source));
32
- }
33
-
34
- export async function loadLmlFile(path: string): Promise<Profile> {
35
- return loadLml(fs.readFileSync(path, "utf-8"));
36
- }
13
+ export const { loadProfileFile, loadLml, loadLmlFile } = nodeLoaders(
14
+ (spec) => createRequire(import.meta.url).resolve(spec),
15
+ );