@malloydata/motly-ts-parser 0.0.1

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.
@@ -0,0 +1,45 @@
1
+ import { MOTLYValue, MOTLYError, MOTLYSchemaError, MOTLYValidationError } from "motly-ts-interface";
2
+ /**
3
+ * A stateful MOTLY parsing session.
4
+ *
5
+ * Pure TypeScript implementation — no WASM, no native dependencies.
6
+ * API-compatible with the Rust `MotlySession`.
7
+ */
8
+ export declare class MOTLYSession {
9
+ private value;
10
+ private schema;
11
+ private disposed;
12
+ /**
13
+ * Parse MOTLY source and apply it to the session's value in place.
14
+ * Returns only parse errors.
15
+ */
16
+ parse(source: string): MOTLYError[];
17
+ /**
18
+ * Parse MOTLY source as a schema and store it in the session.
19
+ * The schema is parsed fresh (not merged).
20
+ */
21
+ parseSchema(source: string): MOTLYError[];
22
+ /**
23
+ * Reset the session's value to empty, keeping the schema.
24
+ */
25
+ reset(): void;
26
+ /**
27
+ * Return a deep clone of the session's current value.
28
+ */
29
+ getValue(): MOTLYValue;
30
+ /**
31
+ * Validate the session's value against its stored schema.
32
+ * Returns an empty array if no schema has been set.
33
+ */
34
+ validateSchema(): MOTLYSchemaError[];
35
+ /**
36
+ * Validate that all `$`-references in the session's value resolve.
37
+ */
38
+ validateReferences(): MOTLYValidationError[];
39
+ /**
40
+ * No-op for pure TS (no native resources to free).
41
+ * After calling `dispose()`, all other methods will throw.
42
+ */
43
+ dispose(): void;
44
+ private ensureAlive;
45
+ }
@@ -0,0 +1,135 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MOTLYSession = void 0;
4
+ const parser_1 = require("./parser");
5
+ const interpreter_1 = require("./interpreter");
6
+ const validate_1 = require("./validate");
7
+ /**
8
+ * A stateful MOTLY parsing session.
9
+ *
10
+ * Pure TypeScript implementation — no WASM, no native dependencies.
11
+ * API-compatible with the Rust `MotlySession`.
12
+ */
13
+ class MOTLYSession {
14
+ constructor() {
15
+ this.value = {};
16
+ this.schema = null;
17
+ this.disposed = false;
18
+ }
19
+ /**
20
+ * Parse MOTLY source and apply it to the session's value in place.
21
+ * Returns only parse errors.
22
+ */
23
+ parse(source) {
24
+ this.ensureAlive();
25
+ try {
26
+ const stmts = (0, parser_1.parse)(source);
27
+ this.value = (0, interpreter_1.execute)(stmts, this.value);
28
+ return [];
29
+ }
30
+ catch (e) {
31
+ if (isMotlyError(e))
32
+ return [e];
33
+ throw e;
34
+ }
35
+ }
36
+ /**
37
+ * Parse MOTLY source as a schema and store it in the session.
38
+ * The schema is parsed fresh (not merged).
39
+ */
40
+ parseSchema(source) {
41
+ this.ensureAlive();
42
+ try {
43
+ const stmts = (0, parser_1.parse)(source);
44
+ this.schema = (0, interpreter_1.execute)(stmts, {});
45
+ return [];
46
+ }
47
+ catch (e) {
48
+ if (isMotlyError(e))
49
+ return [e];
50
+ throw e;
51
+ }
52
+ }
53
+ /**
54
+ * Reset the session's value to empty, keeping the schema.
55
+ */
56
+ reset() {
57
+ this.ensureAlive();
58
+ this.value = {};
59
+ }
60
+ /**
61
+ * Return a deep clone of the session's current value.
62
+ */
63
+ getValue() {
64
+ this.ensureAlive();
65
+ return deepClone(this.value);
66
+ }
67
+ /**
68
+ * Validate the session's value against its stored schema.
69
+ * Returns an empty array if no schema has been set.
70
+ */
71
+ validateSchema() {
72
+ this.ensureAlive();
73
+ if (this.schema === null)
74
+ return [];
75
+ return (0, validate_1.validateSchema)(this.value, this.schema);
76
+ }
77
+ /**
78
+ * Validate that all `$`-references in the session's value resolve.
79
+ */
80
+ validateReferences() {
81
+ this.ensureAlive();
82
+ return (0, validate_1.validateReferences)(this.value);
83
+ }
84
+ /**
85
+ * No-op for pure TS (no native resources to free).
86
+ * After calling `dispose()`, all other methods will throw.
87
+ */
88
+ dispose() {
89
+ this.disposed = true;
90
+ }
91
+ ensureAlive() {
92
+ if (this.disposed) {
93
+ throw new Error("MOTLYSession has been disposed");
94
+ }
95
+ }
96
+ }
97
+ exports.MOTLYSession = MOTLYSession;
98
+ function isMotlyError(e) {
99
+ return (typeof e === "object" &&
100
+ e !== null &&
101
+ "code" in e &&
102
+ "message" in e &&
103
+ "begin" in e &&
104
+ "end" in e);
105
+ }
106
+ function deepClone(value) {
107
+ const result = {};
108
+ if (value.deleted)
109
+ result.deleted = true;
110
+ if (value.eq !== undefined) {
111
+ if (value.eq instanceof Date) {
112
+ result.eq = new Date(value.eq.getTime());
113
+ }
114
+ else if (Array.isArray(value.eq)) {
115
+ result.eq = value.eq.map(cloneNode);
116
+ }
117
+ else {
118
+ result.eq = value.eq;
119
+ }
120
+ }
121
+ if (value.properties) {
122
+ const props = {};
123
+ for (const key of Object.keys(value.properties)) {
124
+ props[key] = cloneNode(value.properties[key]);
125
+ }
126
+ result.properties = props;
127
+ }
128
+ return result;
129
+ }
130
+ function cloneNode(node) {
131
+ if ("linkTo" in node) {
132
+ return { linkTo: node.linkTo };
133
+ }
134
+ return deepClone(node);
135
+ }
@@ -0,0 +1,3 @@
1
+ import { MOTLYValue, MOTLYSchemaError, MOTLYValidationError } from "motly-ts-interface";
2
+ export declare function validateReferences(root: MOTLYValue): MOTLYValidationError[];
3
+ export declare function validateSchema(tag: MOTLYValue, schema: MOTLYValue): MOTLYSchemaError[];