parsanol 0.10.44 → 0.10.45
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/README.md +42 -0
- package/dist/browser.d.ts +16 -0
- package/dist/browser.js +43 -0
- package/dist/runtime-core.d.ts +162 -0
- package/dist/runtime-core.js +268 -0
- package/dist/runtime.d.ts +5 -121
- package/dist/runtime.js +5 -176
- package/package.json +21 -5
- package/vendor/parsanol-wasm/BUILT_FROM +1 -1
- package/vendor/parsanol-wasm/BUILT_FROM.json +29 -0
- package/vendor/parsanol-wasm/LICENSE +21 -0
- package/vendor/parsanol-wasm/parsanol.cjs +37 -0
- package/vendor/parsanol-wasm/parsanol.d.ts +6 -0
- package/vendor/parsanol-wasm/parsanol_bg.wasm +0 -0
- package/vendor/parsanol-wasm/parsanol_bg.wasm.d.ts +17 -15
- package/vendor/parsanol-web/BUILT_FROM.json +24 -0
- package/vendor/parsanol-web/LICENSE +21 -0
- package/vendor/parsanol-web/README.md +36 -0
- package/vendor/parsanol-web/parsanol.d.ts +251 -0
- package/vendor/parsanol-web/parsanol.js +803 -0
- package/vendor/parsanol-web/parsanol_bg.wasm +0 -0
- package/vendor/parsanol-web/parsanol_bg.wasm.d.ts +58 -0
package/README.md
CHANGED
|
@@ -86,3 +86,45 @@ any baked artifact that passes its embedded tests on the other engines
|
|
|
86
86
|
passes here identically. Run the runtime tests with
|
|
87
87
|
`PARG_ARTIFACT_DIR=<artifacts> npm test` (the artifact-gated case
|
|
88
88
|
skips when no artifacts dir is set).
|
|
89
|
+
|
|
90
|
+
## Browser runtime
|
|
91
|
+
|
|
92
|
+
The browser entry uses the official `@parsanol/wasm` web build shipped with
|
|
93
|
+
this package. It fetches WASM relative to the generated web module.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import initialize, { PargRuntime } from "parsanol/browser";
|
|
97
|
+
await initialize();
|
|
98
|
+
const parser = await PargRuntime.fromURL("/artifacts/example.json");
|
|
99
|
+
const shape = parser.parseShape("input");
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
To provide your own WASM location or bytes, pass them to `initialize`. A
|
|
103
|
+
synchronous constructor requires initialization first. Pass original artifact
|
|
104
|
+
and binding JSON text unchanged; parsing and reserializing checksum-bound
|
|
105
|
+
resources can change their numeric representations.
|
|
106
|
+
|
|
107
|
+
The browser entry has HTTP `fromURL` instead of Node filesystem helpers. Its
|
|
108
|
+
`compiledFrom(source)` uses asynchronous WebCrypto: `await` its result. The
|
|
109
|
+
Node entry retains its synchronous API. Browser-aware package resolution also
|
|
110
|
+
selects this entry; the explicit `/browser` import supplies its browser types.
|
|
111
|
+
|
|
112
|
+
`npm run test:browser` checks the packed package in real Chrome/Chromium,
|
|
113
|
+
including native parsing, compiler agreement, recovery, artifact verification,
|
|
114
|
+
and HTTP failures. Set `CHROME_BIN` to an existing browser if needed.
|
|
115
|
+
`npm run refresh-web-wasm -- 0.20.12` reproduces the vendored browser assets
|
|
116
|
+
from the pinned official npm release; keep its version aligned with the Node
|
|
117
|
+
engine. No Rust build or Node polyfill runs in the browser.
|
|
118
|
+
|
|
119
|
+
Parser resource guards are distinct from syntax refusals. The WASM engine keeps
|
|
120
|
+
its 1000 rule-reference recursion budget and additionally limits simultaneous VM
|
|
121
|
+
atom calls to 1024, before the host call stack can throw and strand a borrow.
|
|
122
|
+
`RecursionLimitExceeded` and `CallDepthLimitExceeded` expose `category`, `depth`
|
|
123
|
+
and `maxDepth`. These budgets count engine calls, not parentheses in user input.
|
|
124
|
+
Binding shape JSON has a separate 512-container budget. `applyBindings` and
|
|
125
|
+
`applyBindingsArtifact` throw `ShapeDepthLimitExceeded` (`category: "shape_depth_limit"`,
|
|
126
|
+
`depth`, `maxDepth`) above that budget. The bounded bridge accepts parser-produced
|
|
127
|
+
depth-64 Amath shapes, which exceed serde JSON's implicit 127-container guard.
|
|
128
|
+
|
|
129
|
+
A resource abort leaves the artifact reusable and freeable. Generic VM/host errors
|
|
130
|
+
retain their original type; they are never relabeled as a depth error.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { InitInput } from "../vendor/parsanol-web/parsanol.js";
|
|
2
|
+
import { PargRuntimeCore } from "./runtime-core.js";
|
|
3
|
+
export { deriveTemplate, renderSegments, materializeFromSchema, RecursionLimitExceeded, CallDepthLimitExceeded, ShapeDepthLimitExceeded } from "./runtime-core.js";
|
|
4
|
+
export type { FieldRequirement, EntrySchema, Schema, RenderSegment, RecoveryError, RecoveryEnvelope, ParsingCompatibility, CompatibilityEnvelope, Materialized } from "./runtime-core.js";
|
|
5
|
+
/** Fetch/instantiate the web-target engine; custom bytes, module or URL are supported. */
|
|
6
|
+
export declare function initialize(input?: InitInput | Promise<InitInput>): Promise<void>;
|
|
7
|
+
export default initialize;
|
|
8
|
+
/** Compile PARG using the same initialized web engine used for parsing. */
|
|
9
|
+
export declare function compileSource(source: string): Record<string, unknown>;
|
|
10
|
+
export declare class PargRuntime extends PargRuntimeCore {
|
|
11
|
+
constructor(artifactJson: string, entry?: string);
|
|
12
|
+
/** Source verification uses asynchronous browser WebCrypto, unlike the Node entry. */
|
|
13
|
+
compiledFrom(source: string): Promise<boolean>;
|
|
14
|
+
/** Load a checksummed artifact over HTTP; rejects non-success HTTP status. */
|
|
15
|
+
static fromURL(url: string | URL, entry?: string): Promise<PargRuntime>;
|
|
16
|
+
}
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** Browser entry backed by the target=web Parsanol engine. */
|
|
2
|
+
import initWasm, * as webWasm from "../vendor/parsanol-web/parsanol.js";
|
|
3
|
+
import { PargRuntimeCore } from "./runtime-core.js";
|
|
4
|
+
export { deriveTemplate, renderSegments, materializeFromSchema, RecursionLimitExceeded, CallDepthLimitExceeded, ShapeDepthLimitExceeded } from "./runtime-core.js";
|
|
5
|
+
let initialized = false;
|
|
6
|
+
/** Fetch/instantiate the web-target engine; custom bytes, module or URL are supported. */
|
|
7
|
+
export async function initialize(input) {
|
|
8
|
+
await initWasm(input === undefined ? undefined : { module_or_path: input });
|
|
9
|
+
initialized = true;
|
|
10
|
+
}
|
|
11
|
+
export default initialize;
|
|
12
|
+
function wasm() {
|
|
13
|
+
if (!initialized)
|
|
14
|
+
throw new Error("Parsanol browser engine is not initialized; await initialize() first");
|
|
15
|
+
return webWasm;
|
|
16
|
+
}
|
|
17
|
+
/** Compile PARG using the same initialized web engine used for parsing. */
|
|
18
|
+
export function compileSource(source) {
|
|
19
|
+
return JSON.parse(wasm().compileSource(source));
|
|
20
|
+
}
|
|
21
|
+
export class PargRuntime extends PargRuntimeCore {
|
|
22
|
+
constructor(artifactJson, entry) {
|
|
23
|
+
super(artifactJson, entry, wasm);
|
|
24
|
+
}
|
|
25
|
+
/** Source verification uses asynchronous browser WebCrypto, unlike the Node entry. */
|
|
26
|
+
async compiledFrom(source) {
|
|
27
|
+
if (this.sourceHash === null)
|
|
28
|
+
return false;
|
|
29
|
+
if (!globalThis.crypto?.subtle)
|
|
30
|
+
throw new Error("source verification requires WebCrypto in a secure context");
|
|
31
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(source));
|
|
32
|
+
const hex = Array.from(new Uint8Array(digest), value => value.toString(16).padStart(2, "0")).join("");
|
|
33
|
+
return this.sourceHash === hex;
|
|
34
|
+
}
|
|
35
|
+
/** Load a checksummed artifact over HTTP; rejects non-success HTTP status. */
|
|
36
|
+
static async fromURL(url, entry) {
|
|
37
|
+
await initialize();
|
|
38
|
+
const response = await fetch(url);
|
|
39
|
+
if (!response.ok)
|
|
40
|
+
throw new Error(`artifact fetch failed: HTTP ${response.status}`);
|
|
41
|
+
return new PargRuntime(await response.text(), entry);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
export interface WasmModule {
|
|
2
|
+
PargArtifactJs: new (artifactJson: string) => any;
|
|
3
|
+
compileSource: (source: string) => string;
|
|
4
|
+
}
|
|
5
|
+
type PargArtifactJs = any;
|
|
6
|
+
/** A native rule-recursion resource guard, distinct from syntax refusal. */
|
|
7
|
+
export declare class RecursionLimitExceeded extends Error {
|
|
8
|
+
readonly depth: number;
|
|
9
|
+
readonly maxDepth: number;
|
|
10
|
+
readonly category = "recursion_limit";
|
|
11
|
+
constructor(depth: number, maxDepth: number, message?: string);
|
|
12
|
+
}
|
|
13
|
+
/** WASM VM-call budget, separate from the rule-reference recursion guard. */
|
|
14
|
+
export declare class CallDepthLimitExceeded extends Error {
|
|
15
|
+
readonly depth: number;
|
|
16
|
+
readonly maxDepth: number;
|
|
17
|
+
readonly category = "call_depth_limit";
|
|
18
|
+
constructor(depth: number, maxDepth: number, message?: string);
|
|
19
|
+
}
|
|
20
|
+
/** Container nesting budget at the WASM binding shape-JSON bridge. */
|
|
21
|
+
export declare class ShapeDepthLimitExceeded extends Error {
|
|
22
|
+
readonly depth: number;
|
|
23
|
+
readonly maxDepth: number;
|
|
24
|
+
readonly category = "shape_depth_limit";
|
|
25
|
+
constructor(depth: number, maxDepth: number, message?: string);
|
|
26
|
+
}
|
|
27
|
+
/** A field requirement from the binding-requirements schema. */
|
|
28
|
+
export interface FieldRequirement {
|
|
29
|
+
type: string;
|
|
30
|
+
card: string;
|
|
31
|
+
preprocess?: string;
|
|
32
|
+
}
|
|
33
|
+
/** The binding-requirements schema record for one entry. */
|
|
34
|
+
export interface EntrySchema {
|
|
35
|
+
root: string;
|
|
36
|
+
fields: Record<string, FieldRequirement>;
|
|
37
|
+
examples: {
|
|
38
|
+
input: string;
|
|
39
|
+
captures: Record<string, unknown>;
|
|
40
|
+
}[];
|
|
41
|
+
}
|
|
42
|
+
export type Schema = Record<string, EntrySchema>;
|
|
43
|
+
/**
|
|
44
|
+
* A loaded, checksum-verified PG artifact with its schema.
|
|
45
|
+
* Construction throws when the checksum does not verify.
|
|
46
|
+
*/
|
|
47
|
+
export declare class PargRuntimeCore {
|
|
48
|
+
readonly artifact: PargArtifactJs;
|
|
49
|
+
readonly schema: Schema;
|
|
50
|
+
readonly renderSpec: Record<string, RenderSegment[]>;
|
|
51
|
+
readonly entry: string;
|
|
52
|
+
readonly defaultEntry: string | null;
|
|
53
|
+
/** parsanol-ts#5 provenance: SHA-256 of the .parg source text and the
|
|
54
|
+
* compiler version. Null for artifacts compiled before these fields
|
|
55
|
+
* existed (unverifiable, not stale). */
|
|
56
|
+
readonly sourceHash: string | null;
|
|
57
|
+
readonly compilerVersion: string | null;
|
|
58
|
+
readonly source: string | null;
|
|
59
|
+
/** ruby#377 semantics fingerprint: stable across producer metadata
|
|
60
|
+
* (compiler_version, unicode_tables), so gates can tell "re-baked by
|
|
61
|
+
* a newer compiler" from "the grammar changed". Null for artifacts
|
|
62
|
+
* baked before the field existed (parsanol >= 1.3.117 / rs 0.19.14). */
|
|
63
|
+
readonly semanticsHash: string | null;
|
|
64
|
+
readonly envelope: Record<string, unknown>;
|
|
65
|
+
constructor(artifactJson: string, entry: string | undefined, engine: () => WasmModule);
|
|
66
|
+
/** The wire-format family ("parsanol-tree/v2"): a shape bump requires a
|
|
67
|
+
* new engine family; same-shape artifacts parse identically forever. */
|
|
68
|
+
get shape(): string | null;
|
|
69
|
+
/** Parse with the native wasm engine; returns the parsanol-shape tree. */
|
|
70
|
+
parseShape(input: string): unknown;
|
|
71
|
+
/** Explicit historical memo/causal-diagnostic policy; old APIs remain strict.
|
|
72
|
+
* This is a parse result, not a best-effort recovery capture envelope. */
|
|
73
|
+
parseShapeWithCompatibility(input: string, compatibility: ParsingCompatibility): CompatibilityEnvelope;
|
|
74
|
+
/**
|
|
75
|
+
* Best-effort syntax recovery. Resource aborts throw typed errors. Success returns
|
|
76
|
+
* `{ ok: true, shape }`; failure returns
|
|
77
|
+
* `{ ok: false, captures, error, consumed_upto }` — the captures
|
|
78
|
+
* observed before the failure plus the failure wire.
|
|
79
|
+
*/
|
|
80
|
+
parseShapeRecovering(input: string): RecoveryEnvelope;
|
|
81
|
+
/** Apply the entry's bindings to a parsanol-shape tree. */
|
|
82
|
+
applyBindings(shape: unknown): Record<string, unknown>;
|
|
83
|
+
/** Apply an entry's DECLARED bindings artifact (parsanol-ruby#432) to
|
|
84
|
+
* a parsanol-shape tree. The engine verifies the artifact (kind,
|
|
85
|
+
* canonical-JSON checksum, `ts` target pin, binding version) and
|
|
86
|
+
* pair-checks it against this artifact by grammar source hash before
|
|
87
|
+
* the shared apply core runs — the JSON materialized is the JSON the
|
|
88
|
+
* checksum pinned. */
|
|
89
|
+
applyBindingsArtifact(bindingsJson: string, shape: unknown): Record<string, unknown>;
|
|
90
|
+
/** Parse and bind in one step. */
|
|
91
|
+
parseAndBind(input: string): Record<string, unknown> | null;
|
|
92
|
+
/**
|
|
93
|
+
* Schema-driven materialization: the schema (from the artifact, not
|
|
94
|
+
* hand-written model code) declares the fields a data model must
|
|
95
|
+
* implement; the bound captures populate a structurally-typed object.
|
|
96
|
+
* Card `1*`/`*` fields become arrays; `0..1` fields become nullable.
|
|
97
|
+
*/
|
|
98
|
+
materialize(input: string): Materialized | null;
|
|
99
|
+
/** Render the identifier string from a bound map (F6). */
|
|
100
|
+
render(bound: Record<string, unknown>, variant?: string): string;
|
|
101
|
+
/** Parse, bind, and render in one step. */
|
|
102
|
+
renderString(input: string, variant?: string): string | null;
|
|
103
|
+
/** Evaluate a named derive spec against a bound map (F6). */
|
|
104
|
+
derive(name: string, bound: Record<string, unknown>): string;
|
|
105
|
+
/** Parse, bind, and evaluate a derive spec in one step. */
|
|
106
|
+
deriveString(input: string, name: string): string | null;
|
|
107
|
+
/** Run the artifact's embedded tests; empty list means green. */
|
|
108
|
+
runTests(): string[];
|
|
109
|
+
}
|
|
110
|
+
/** F6 v1 derive evaluator: {field} interpolates from the bound map. */
|
|
111
|
+
export declare function deriveTemplate(derive: Record<string, string>, name: string, bound: Record<string, unknown>): string;
|
|
112
|
+
export type RenderSegment = {
|
|
113
|
+
type: "field";
|
|
114
|
+
field: string;
|
|
115
|
+
} | {
|
|
116
|
+
type: "literal";
|
|
117
|
+
text: string;
|
|
118
|
+
} | {
|
|
119
|
+
type: "cond";
|
|
120
|
+
field: string;
|
|
121
|
+
then: RenderSegment[];
|
|
122
|
+
};
|
|
123
|
+
/** F6 v1 generic renderer: field / literal / cond-presence segments. */
|
|
124
|
+
export declare function renderSegments(segments: RenderSegment[] | null, bound: Record<string, unknown>): string;
|
|
125
|
+
/** A schema-materialized identifier: typed fields plus provenance. */
|
|
126
|
+
export type ParsingCompatibility = "strict" | "parslet";
|
|
127
|
+
export type CompatibilityEnvelope = {
|
|
128
|
+
ok: true;
|
|
129
|
+
shape: unknown;
|
|
130
|
+
} | {
|
|
131
|
+
ok: false;
|
|
132
|
+
error: {
|
|
133
|
+
offset: number;
|
|
134
|
+
ranked: [number, string[]][];
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
export interface RecoveryError {
|
|
138
|
+
offset: number;
|
|
139
|
+
message?: string;
|
|
140
|
+
expected?: string[];
|
|
141
|
+
line?: number;
|
|
142
|
+
column?: number;
|
|
143
|
+
}
|
|
144
|
+
/** The parseShapeRecovering envelope (TODO.sota/1). */
|
|
145
|
+
export interface RecoveryEnvelope {
|
|
146
|
+
ok: boolean;
|
|
147
|
+
shape?: unknown;
|
|
148
|
+
captures?: Record<string, unknown[]>;
|
|
149
|
+
error?: RecoveryError;
|
|
150
|
+
consumed_upto?: number;
|
|
151
|
+
}
|
|
152
|
+
export interface Materialized {
|
|
153
|
+
entry: string;
|
|
154
|
+
attributes: Record<string, unknown>;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Schema-driven materialization independent of any wasm handle —
|
|
158
|
+
* the same function the model layer will build on (T2 codegen emits the
|
|
159
|
+
* static view of exactly these fields).
|
|
160
|
+
*/
|
|
161
|
+
export declare function materializeFromSchema(schema: EntrySchema, bound: Record<string, unknown>): Materialized;
|
|
162
|
+
export {};
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/** A native rule-recursion resource guard, distinct from syntax refusal. */
|
|
2
|
+
export class RecursionLimitExceeded extends Error {
|
|
3
|
+
depth;
|
|
4
|
+
maxDepth;
|
|
5
|
+
category = "recursion_limit";
|
|
6
|
+
constructor(depth, maxDepth, message) {
|
|
7
|
+
super(message ?? `Recursion limit exceeded: depth ${depth}, limit ${maxDepth}`);
|
|
8
|
+
this.depth = depth;
|
|
9
|
+
this.maxDepth = maxDepth;
|
|
10
|
+
this.name = "RecursionLimitExceeded";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** WASM VM-call budget, separate from the rule-reference recursion guard. */
|
|
14
|
+
export class CallDepthLimitExceeded extends Error {
|
|
15
|
+
depth;
|
|
16
|
+
maxDepth;
|
|
17
|
+
category = "call_depth_limit";
|
|
18
|
+
constructor(depth, maxDepth, message) {
|
|
19
|
+
super(message ?? `VM call depth exceeded: depth ${depth}, limit ${maxDepth}`);
|
|
20
|
+
this.depth = depth;
|
|
21
|
+
this.maxDepth = maxDepth;
|
|
22
|
+
this.name = "CallDepthLimitExceeded";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Container nesting budget at the WASM binding shape-JSON bridge. */
|
|
26
|
+
export class ShapeDepthLimitExceeded extends Error {
|
|
27
|
+
depth;
|
|
28
|
+
maxDepth;
|
|
29
|
+
category = "shape_depth_limit";
|
|
30
|
+
constructor(depth, maxDepth, message) {
|
|
31
|
+
super(message ?? `Binding shape depth exceeded: depth ${depth}, limit ${maxDepth}`);
|
|
32
|
+
this.depth = depth;
|
|
33
|
+
this.maxDepth = maxDepth;
|
|
34
|
+
this.name = "ShapeDepthLimitExceeded";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function nativeResource(value) {
|
|
38
|
+
if (value === null || typeof value !== "object")
|
|
39
|
+
return null;
|
|
40
|
+
const wire = value;
|
|
41
|
+
if (typeof wire["depth"] !== "number" || typeof wire["max_depth"] !== "number")
|
|
42
|
+
return null;
|
|
43
|
+
const message = typeof wire["message"] === "string" ? wire["message"] : undefined;
|
|
44
|
+
if (wire["category"] === "recursion_limit")
|
|
45
|
+
return new RecursionLimitExceeded(wire["depth"], wire["max_depth"], message);
|
|
46
|
+
if (wire["category"] === "call_depth_limit")
|
|
47
|
+
return new CallDepthLimitExceeded(wire["depth"], wire["max_depth"], message);
|
|
48
|
+
if (wire["category"] === "shape_depth_limit")
|
|
49
|
+
return new ShapeDepthLimitExceeded(wire["depth"], wire["max_depth"], message);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
function rethrowNative(error) { throw nativeResource(error) ?? error; }
|
|
53
|
+
/**
|
|
54
|
+
* A loaded, checksum-verified PG artifact with its schema.
|
|
55
|
+
* Construction throws when the checksum does not verify.
|
|
56
|
+
*/
|
|
57
|
+
export class PargRuntimeCore {
|
|
58
|
+
artifact;
|
|
59
|
+
schema;
|
|
60
|
+
renderSpec;
|
|
61
|
+
entry;
|
|
62
|
+
defaultEntry;
|
|
63
|
+
/** parsanol-ts#5 provenance: SHA-256 of the .parg source text and the
|
|
64
|
+
* compiler version. Null for artifacts compiled before these fields
|
|
65
|
+
* existed (unverifiable, not stale). */
|
|
66
|
+
sourceHash;
|
|
67
|
+
compilerVersion;
|
|
68
|
+
source;
|
|
69
|
+
/** ruby#377 semantics fingerprint: stable across producer metadata
|
|
70
|
+
* (compiler_version, unicode_tables), so gates can tell "re-baked by
|
|
71
|
+
* a newer compiler" from "the grammar changed". Null for artifacts
|
|
72
|
+
* baked before the field existed (parsanol >= 1.3.117 / rs 0.19.14). */
|
|
73
|
+
semanticsHash;
|
|
74
|
+
envelope;
|
|
75
|
+
constructor(artifactJson, entry, engine) {
|
|
76
|
+
const parsed = JSON.parse(artifactJson);
|
|
77
|
+
this.envelope = parsed;
|
|
78
|
+
// Dynamic artifacts (parsanol-ruby#129 runtime state / custom atoms)
|
|
79
|
+
// carry Ruby closures; their grammars deserialize portably but parse
|
|
80
|
+
// only on a Ruby runtime. Fail here, at the boundary, with the
|
|
81
|
+
// routing answer instead of a wasm deserialization error deep in the
|
|
82
|
+
// atom tree.
|
|
83
|
+
if (parsed["dynamic"] === true) {
|
|
84
|
+
throw new Error("this artifact uses PARG runtime state or custom atoms and parses " +
|
|
85
|
+
"only on a Ruby runtime (parsanol-ruby#129)");
|
|
86
|
+
}
|
|
87
|
+
this.artifact = new (engine().PargArtifactJs)(artifactJson);
|
|
88
|
+
this.schema = JSON.parse(this.artifact.schema());
|
|
89
|
+
this.renderSpec =
|
|
90
|
+
parsed["render"] ?? {};
|
|
91
|
+
// The compiler bakes default_entry: JSON key order is not preserved
|
|
92
|
+
// across engines, so the sorted entry list cannot rederive it.
|
|
93
|
+
this.defaultEntry = parsed["default_entry"] ?? null;
|
|
94
|
+
this.sourceHash = parsed["source_hash"] ?? null;
|
|
95
|
+
this.compilerVersion = parsed["compiler_version"] ?? null;
|
|
96
|
+
this.source = parsed["source"] ?? null;
|
|
97
|
+
this.semanticsHash = parsed["semantics_hash"] ?? null;
|
|
98
|
+
this.entry = entry ?? this.defaultEntry ?? this.artifact.entryNames()[0];
|
|
99
|
+
}
|
|
100
|
+
/** The wire-format family ("parsanol-tree/v2"): a shape bump requires a
|
|
101
|
+
* new engine family; same-shape artifacts parse identically forever. */
|
|
102
|
+
get shape() {
|
|
103
|
+
return this.envelope["shape"] ?? null;
|
|
104
|
+
}
|
|
105
|
+
/** Parse with the native wasm engine; returns the parsanol-shape tree. */
|
|
106
|
+
parseShape(input) {
|
|
107
|
+
try {
|
|
108
|
+
return JSON.parse(this.artifact.parseShape(this.entry, input));
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
return rethrowNative(error);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** Explicit historical memo/causal-diagnostic policy; old APIs remain strict.
|
|
115
|
+
* This is a parse result, not a best-effort recovery capture envelope. */
|
|
116
|
+
parseShapeWithCompatibility(input, compatibility) {
|
|
117
|
+
if (typeof this.artifact.parseShapeWithCompatibility !== "function") {
|
|
118
|
+
throw new Error("native engine does not expose explicit parsing compatibility");
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
const result = JSON.parse(this.artifact.parseShapeWithCompatibility(this.entry, input, compatibility));
|
|
122
|
+
const resource = nativeResource(result?.error);
|
|
123
|
+
if (resource)
|
|
124
|
+
throw resource;
|
|
125
|
+
return result;
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
return rethrowNative(error);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Best-effort syntax recovery. Resource aborts throw typed errors. Success returns
|
|
133
|
+
* `{ ok: true, shape }`; failure returns
|
|
134
|
+
* `{ ok: false, captures, error, consumed_upto }` — the captures
|
|
135
|
+
* observed before the failure plus the failure wire.
|
|
136
|
+
*/
|
|
137
|
+
parseShapeRecovering(input) {
|
|
138
|
+
try {
|
|
139
|
+
return JSON.parse(this.artifact.parseShapeRecovering(this.entry, input));
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
return rethrowNative(error);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/** Apply the entry's bindings to a parsanol-shape tree. */
|
|
146
|
+
applyBindings(shape) {
|
|
147
|
+
try {
|
|
148
|
+
return JSON.parse(this.artifact.applyBindings(this.entry, JSON.stringify(shape)));
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
return rethrowNative(error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** Apply an entry's DECLARED bindings artifact (parsanol-ruby#432) to
|
|
155
|
+
* a parsanol-shape tree. The engine verifies the artifact (kind,
|
|
156
|
+
* canonical-JSON checksum, `ts` target pin, binding version) and
|
|
157
|
+
* pair-checks it against this artifact by grammar source hash before
|
|
158
|
+
* the shared apply core runs — the JSON materialized is the JSON the
|
|
159
|
+
* checksum pinned. */
|
|
160
|
+
applyBindingsArtifact(bindingsJson, shape) {
|
|
161
|
+
try {
|
|
162
|
+
return JSON.parse(this.artifact.applyBindingsArtifact(bindingsJson, this.entry, JSON.stringify(shape)));
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
return rethrowNative(error);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** Parse and bind in one step. */
|
|
169
|
+
parseAndBind(input) {
|
|
170
|
+
try {
|
|
171
|
+
return JSON.parse(this.artifact.parseAndBind(this.entry, input));
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Schema-driven materialization: the schema (from the artifact, not
|
|
179
|
+
* hand-written model code) declares the fields a data model must
|
|
180
|
+
* implement; the bound captures populate a structurally-typed object.
|
|
181
|
+
* Card `1*`/`*` fields become arrays; `0..1` fields become nullable.
|
|
182
|
+
*/
|
|
183
|
+
materialize(input) {
|
|
184
|
+
const bound = this.parseAndBind(input);
|
|
185
|
+
if (bound === null)
|
|
186
|
+
return null;
|
|
187
|
+
const schema = this.schema[this.entry];
|
|
188
|
+
if (!schema)
|
|
189
|
+
throw new Error(`artifact has no schema for entry ${this.entry}`);
|
|
190
|
+
return materializeFromSchema(schema, bound);
|
|
191
|
+
}
|
|
192
|
+
/** Render the identifier string from a bound map (F6). */
|
|
193
|
+
render(bound, variant = "default") {
|
|
194
|
+
return renderSegments((this.renderSpec[variant] ?? null), bound);
|
|
195
|
+
}
|
|
196
|
+
/** Parse, bind, and render in one step. */
|
|
197
|
+
renderString(input, variant = "default") {
|
|
198
|
+
const bound = this.parseAndBind(input);
|
|
199
|
+
return bound === null ? null : this.render(bound, variant);
|
|
200
|
+
}
|
|
201
|
+
/** Evaluate a named derive spec against a bound map (F6). */
|
|
202
|
+
derive(name, bound) {
|
|
203
|
+
return deriveTemplate((this.envelope["derive"] ?? {}), name, bound);
|
|
204
|
+
}
|
|
205
|
+
/** Parse, bind, and evaluate a derive spec in one step. */
|
|
206
|
+
deriveString(input, name) {
|
|
207
|
+
const bound = this.parseAndBind(input);
|
|
208
|
+
return bound === null ? null : this.derive(name, bound);
|
|
209
|
+
}
|
|
210
|
+
/** Run the artifact's embedded tests; empty list means green. */
|
|
211
|
+
runTests() {
|
|
212
|
+
return JSON.parse(this.artifact.runTests());
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** F6 v1 derive evaluator: {field} interpolates from the bound map. */
|
|
216
|
+
export function deriveTemplate(derive, name, bound) {
|
|
217
|
+
const template = derive[name];
|
|
218
|
+
if (template === undefined)
|
|
219
|
+
throw new Error(`derive spec ${name} not declared`);
|
|
220
|
+
return template.replace(/\{(\w+)\}/g, (_, field) => {
|
|
221
|
+
const value = bound[field];
|
|
222
|
+
return value === undefined || value === null ? "" : String(value);
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/** F6 v1 generic renderer: field / literal / cond-presence segments. */
|
|
226
|
+
export function renderSegments(segments, bound) {
|
|
227
|
+
let out = "";
|
|
228
|
+
for (const segment of segments ?? []) {
|
|
229
|
+
if (segment.type === "field") {
|
|
230
|
+
const value = bound[segment.field];
|
|
231
|
+
out += value === undefined || value === null ? "" : String(value);
|
|
232
|
+
}
|
|
233
|
+
else if (segment.type === "literal") {
|
|
234
|
+
out += segment.text;
|
|
235
|
+
}
|
|
236
|
+
else if (segment.type === "cond") {
|
|
237
|
+
const value = bound[segment.field];
|
|
238
|
+
if (value !== undefined && value !== null)
|
|
239
|
+
out += renderSegments(segment.then, bound);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return out;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Schema-driven materialization independent of any wasm handle —
|
|
246
|
+
* the same function the model layer will build on (T2 codegen emits the
|
|
247
|
+
* static view of exactly these fields).
|
|
248
|
+
*/
|
|
249
|
+
export function materializeFromSchema(schema, bound) {
|
|
250
|
+
const attributes = {};
|
|
251
|
+
for (const [path, requirements] of Object.entries(schema.fields)) {
|
|
252
|
+
const leaf = path.includes("[]")
|
|
253
|
+
? (path.split("].").pop() ?? path)
|
|
254
|
+
: path;
|
|
255
|
+
const value = bound[leaf];
|
|
256
|
+
if (requirements.card.endsWith("*")) {
|
|
257
|
+
const list = Array.isArray(value) ? value : value === undefined ? [] : [value];
|
|
258
|
+
attributes[leaf] = list;
|
|
259
|
+
}
|
|
260
|
+
else if (requirements.card === "0..1") {
|
|
261
|
+
attributes[leaf] = value === undefined ? null : value;
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
attributes[leaf] = value;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
return { entry: schema.root, attributes };
|
|
268
|
+
}
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
type PargArtifactJs = any;
|
|
2
1
|
/**
|
|
3
2
|
* Compile PARG source to a checksummed artifact in-process — the
|
|
4
3
|
* playground's compiler-in-the-browser seam (site#19). Single-file
|
|
@@ -8,48 +7,12 @@ type PargArtifactJs = any;
|
|
|
8
7
|
* @throws when the source does not parse or the grammar is invalid.
|
|
9
8
|
*/
|
|
10
9
|
export declare function compileSource(source: string): Record<string, unknown>;
|
|
11
|
-
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
/** The binding-requirements schema record for one entry. */
|
|
18
|
-
export interface EntrySchema {
|
|
19
|
-
root: string;
|
|
20
|
-
fields: Record<string, FieldRequirement>;
|
|
21
|
-
examples: {
|
|
22
|
-
input: string;
|
|
23
|
-
captures: Record<string, unknown>;
|
|
24
|
-
}[];
|
|
25
|
-
}
|
|
26
|
-
export type Schema = Record<string, EntrySchema>;
|
|
27
|
-
/**
|
|
28
|
-
* A loaded, checksum-verified PG artifact with its schema.
|
|
29
|
-
* Construction throws when the checksum does not verify.
|
|
30
|
-
*/
|
|
31
|
-
export declare class PargRuntime {
|
|
32
|
-
readonly artifact: PargArtifactJs;
|
|
33
|
-
readonly schema: Schema;
|
|
34
|
-
readonly renderSpec: Record<string, RenderSegment[]>;
|
|
35
|
-
readonly entry: string;
|
|
36
|
-
readonly defaultEntry: string | null;
|
|
37
|
-
/** parsanol-ts#5 provenance: SHA-256 of the .parg source text and the
|
|
38
|
-
* compiler version. Null for artifacts compiled before these fields
|
|
39
|
-
* existed (unverifiable, not stale). */
|
|
40
|
-
readonly sourceHash: string | null;
|
|
41
|
-
readonly compilerVersion: string | null;
|
|
42
|
-
readonly source: string | null;
|
|
43
|
-
/** ruby#377 semantics fingerprint: stable across producer metadata
|
|
44
|
-
* (compiler_version, unicode_tables), so gates can tell "re-baked by
|
|
45
|
-
* a newer compiler" from "the grammar changed". Null for artifacts
|
|
46
|
-
* baked before the field existed (parsanol >= 1.3.117 / rs 0.19.14). */
|
|
47
|
-
readonly semanticsHash: string | null;
|
|
48
|
-
readonly envelope: Record<string, unknown>;
|
|
10
|
+
import { PargRuntimeCore } from "./runtime-core.js";
|
|
11
|
+
export { deriveTemplate, renderSegments, materializeFromSchema, RecursionLimitExceeded, CallDepthLimitExceeded, ShapeDepthLimitExceeded } from "./runtime-core.js";
|
|
12
|
+
export type { FieldRequirement, EntrySchema, Schema, RenderSegment, RecoveryError, RecoveryEnvelope, ParsingCompatibility, CompatibilityEnvelope, Materialized } from "./runtime-core.js";
|
|
13
|
+
/** Node entry: preserves synchronous WASM initialization and filesystem helpers. */
|
|
14
|
+
export declare class PargRuntime extends PargRuntimeCore {
|
|
49
15
|
constructor(artifactJson: string, entry?: string);
|
|
50
|
-
/** The wire-format family ("parsanol-tree/v2"): a shape bump requires a
|
|
51
|
-
* new engine family; same-shape artifacts parse identically forever. */
|
|
52
|
-
get shape(): string | null;
|
|
53
16
|
/** True when this artifact was compiled from `source` (the current .parg
|
|
54
17
|
* revision). The compiler bakes a bare hex digest; artifacts without the
|
|
55
18
|
* field answer false — unverifiable rather than fresh. */
|
|
@@ -60,83 +23,4 @@ export declare class PargRuntime {
|
|
|
60
23
|
static artifactsDir(): string | null;
|
|
61
24
|
/** Load `<dir>/<grammar>.json`; throws when no artifacts dir is set. */
|
|
62
25
|
static load(grammar: string, entry?: string, dir?: string): PargRuntime;
|
|
63
|
-
/** Parse with the native wasm engine; returns the parsanol-shape tree. */
|
|
64
|
-
parseShape(input: string): unknown;
|
|
65
|
-
/**
|
|
66
|
-
* Best-effort recovery (TODO.sota/1): never throws. Success returns
|
|
67
|
-
* `{ ok: true, shape }`; failure returns
|
|
68
|
-
* `{ ok: false, captures, error, consumed_upto }` — the captures
|
|
69
|
-
* observed before the failure plus the failure wire.
|
|
70
|
-
*/
|
|
71
|
-
parseShapeRecovering(input: string): RecoveryEnvelope;
|
|
72
|
-
/** Apply the entry's bindings to a parsanol-shape tree. */
|
|
73
|
-
applyBindings(shape: unknown): Record<string, unknown>;
|
|
74
|
-
/** Apply an entry's DECLARED bindings artifact (parsanol-ruby#432) to
|
|
75
|
-
* a parsanol-shape tree. The engine verifies the artifact (kind,
|
|
76
|
-
* canonical-JSON checksum, `ts` target pin, binding version) and
|
|
77
|
-
* pair-checks it against this artifact by grammar source hash before
|
|
78
|
-
* the shared apply core runs — the JSON materialized is the JSON the
|
|
79
|
-
* checksum pinned. */
|
|
80
|
-
applyBindingsArtifact(bindingsJson: string, shape: unknown): Record<string, unknown>;
|
|
81
|
-
/** Parse and bind in one step. */
|
|
82
|
-
parseAndBind(input: string): Record<string, unknown> | null;
|
|
83
|
-
/**
|
|
84
|
-
* Schema-driven materialization: the schema (from the artifact, not
|
|
85
|
-
* hand-written model code) declares the fields a data model must
|
|
86
|
-
* implement; the bound captures populate a structurally-typed object.
|
|
87
|
-
* Card `1*`/`*` fields become arrays; `0..1` fields become nullable.
|
|
88
|
-
*/
|
|
89
|
-
materialize(input: string): Materialized | null;
|
|
90
|
-
/** Render the identifier string from a bound map (F6). */
|
|
91
|
-
render(bound: Record<string, unknown>, variant?: string): string;
|
|
92
|
-
/** Parse, bind, and render in one step. */
|
|
93
|
-
renderString(input: string, variant?: string): string | null;
|
|
94
|
-
/** Evaluate a named derive spec against a bound map (F6). */
|
|
95
|
-
derive(name: string, bound: Record<string, unknown>): string;
|
|
96
|
-
/** Parse, bind, and evaluate a derive spec in one step. */
|
|
97
|
-
deriveString(input: string, name: string): string | null;
|
|
98
|
-
/** Run the artifact's embedded tests; empty list means green. */
|
|
99
|
-
runTests(): string[];
|
|
100
|
-
}
|
|
101
|
-
/** F6 v1 derive evaluator: {field} interpolates from the bound map. */
|
|
102
|
-
export declare function deriveTemplate(derive: Record<string, string>, name: string, bound: Record<string, unknown>): string;
|
|
103
|
-
export type RenderSegment = {
|
|
104
|
-
type: "field";
|
|
105
|
-
field: string;
|
|
106
|
-
} | {
|
|
107
|
-
type: "literal";
|
|
108
|
-
text: string;
|
|
109
|
-
} | {
|
|
110
|
-
type: "cond";
|
|
111
|
-
field: string;
|
|
112
|
-
then: RenderSegment[];
|
|
113
|
-
};
|
|
114
|
-
/** F6 v1 generic renderer: field / literal / cond-presence segments. */
|
|
115
|
-
export declare function renderSegments(segments: RenderSegment[] | null, bound: Record<string, unknown>): string;
|
|
116
|
-
/** A schema-materialized identifier: typed fields plus provenance. */
|
|
117
|
-
export interface RecoveryError {
|
|
118
|
-
offset: number;
|
|
119
|
-
message?: string;
|
|
120
|
-
expected?: string[];
|
|
121
|
-
line?: number;
|
|
122
|
-
column?: number;
|
|
123
26
|
}
|
|
124
|
-
/** The parseShapeRecovering envelope (TODO.sota/1). */
|
|
125
|
-
export interface RecoveryEnvelope {
|
|
126
|
-
ok: boolean;
|
|
127
|
-
shape?: unknown;
|
|
128
|
-
captures?: Record<string, unknown[]>;
|
|
129
|
-
error?: RecoveryError;
|
|
130
|
-
consumed_upto?: number;
|
|
131
|
-
}
|
|
132
|
-
export interface Materialized {
|
|
133
|
-
entry: string;
|
|
134
|
-
attributes: Record<string, unknown>;
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Schema-driven materialization independent of any wasm handle —
|
|
138
|
-
* the same function the model layer will build on (T2 codegen emits the
|
|
139
|
-
* static view of exactly these fields).
|
|
140
|
-
*/
|
|
141
|
-
export declare function materializeFromSchema(schema: EntrySchema, bound: Record<string, unknown>): Materialized;
|
|
142
|
-
export {};
|