kaleidl 0.0.0-stage → 0.1.2
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 +18 -2
- package/package.json +24 -4
- package/src/index.ts +713 -0
- package/src/lml-artifact.ts +8 -0
- package/src/lml.ts +71 -0
- package/src/node.ts +36 -0
package/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# kaleidl-ts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
MN 112 labeling engine — TypeScript port of the Ruby
|
|
4
|
+
[kaleidl](https://github.com/kaleidl/kaleidl) reference.
|
|
5
|
+
|
|
6
|
+
Conformance contract: [kaleidl/kaleidl-testsuite](https://github.com/kaleidl/kaleidl-testsuite)
|
|
7
|
+
(vendored under `vectors/`). The golden-tree parity test
|
|
8
|
+
(`test/golden.test.ts`) is the acceptance target: the engine must
|
|
9
|
+
reproduce the reference label map exactly.
|
|
10
|
+
|
|
11
|
+
## Status
|
|
12
|
+
|
|
13
|
+
Initial port. Profile loading, context chains, per-context counters,
|
|
14
|
+
conditional templates, and assignment/reference label forms are
|
|
15
|
+
implemented; the golden-parity test currently fails on the annex-scoped
|
|
16
|
+
members (counter anchoring does not restart per annex and `%parent`
|
|
17
|
+
binds empty for notes/examples) — the resolution ranking and anchoring
|
|
18
|
+
need one more pass mirroring the Ruby association-chain model
|
|
19
|
+
(`kaleidl` `Runtime::Engine#resolve_own_label`).
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kaleidl",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "MN 112 labeling engine (TypeScript port of kaleidl)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "github:kaleidl/kaleidl-ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"src"
|
|
9
|
+
],
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "src/index.ts",
|
|
12
|
+
"types": "src/index.ts",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node --experimental-strip-types --import tsx test/golden.test.ts && node --experimental-strip-types --import tsx test/lml.test.ts"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"js-yaml": "^4.1.0",
|
|
18
|
+
"parsanol": "^1.5.36"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@types/js-yaml": "^4.0.9",
|
|
22
|
+
"@types/node": "^22",
|
|
23
|
+
"tsx": "^4",
|
|
24
|
+
"typescript": "^5"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,713 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* kaleidl — MN 112 labeling engine (TypeScript port of the Ruby kaleidl
|
|
3
|
+
* reference). Conformance contract: metanorma/ladl-testsuite.
|
|
4
|
+
*
|
|
5
|
+
* Faithful port of Ladl::Runtime::Engine: the element's label resolves
|
|
6
|
+
* through the association chain (declared context first, then ancestors
|
|
7
|
+
* nearest-first; contexts that bind no labeller for the type are
|
|
8
|
+
* skipped — they scope but do not label), counters are scoped by the
|
|
9
|
+
* declaring carrier element and restart per hierarchical scope, %pn
|
|
10
|
+
* chains through the labeller parent tree with an element-type
|
|
11
|
+
* fallback, and templates render %n, %pn, %parent, %self with {key}
|
|
12
|
+
* vocabulary tokens resolved through the host labels dictionary and
|
|
13
|
+
* the English default vocabulary.
|
|
14
|
+
*/
|
|
15
|
+
import * as yaml from "js-yaml";
|
|
16
|
+
|
|
17
|
+
export interface Instance {
|
|
18
|
+
name: string;
|
|
19
|
+
type: string;
|
|
20
|
+
properties: Record<string, any>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface Profile {
|
|
24
|
+
instances: Record<string, Instance>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface Element {
|
|
28
|
+
type: string;
|
|
29
|
+
id: string;
|
|
30
|
+
context?: string | null;
|
|
31
|
+
children?: Element[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface Label {
|
|
35
|
+
value: string;
|
|
36
|
+
reference: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class ResolutionError extends Error {}
|
|
40
|
+
|
|
41
|
+
export const LABEL_VOCABULARY: Record<string, string> = {
|
|
42
|
+
clause: "Clause",
|
|
43
|
+
subclause: "Clause",
|
|
44
|
+
annex: "Annex",
|
|
45
|
+
appendix: "Appendix",
|
|
46
|
+
section: "Section",
|
|
47
|
+
figure: "Figure",
|
|
48
|
+
table: "Table",
|
|
49
|
+
formula: "Formula",
|
|
50
|
+
inequality: "Inequality",
|
|
51
|
+
note: "NOTE",
|
|
52
|
+
note_xref: "NOTE",
|
|
53
|
+
note_to_entry: "Note { $n } to entry",
|
|
54
|
+
note_to_entry_xref: "{ $parent }, Note { $n } to entry",
|
|
55
|
+
example: "EXAMPLE",
|
|
56
|
+
example_xref: "EXAMPLE",
|
|
57
|
+
termexample: "EXAMPLE",
|
|
58
|
+
list: "List",
|
|
59
|
+
introduction: "Introduction",
|
|
60
|
+
norm_annex: "normative",
|
|
61
|
+
inform_annex: "informative",
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export function ref(instance: Instance, key: string): string {
|
|
65
|
+
const value = instance.properties?.[key];
|
|
66
|
+
if (value == null) return "";
|
|
67
|
+
return typeof value === "object" ? (value.ref ?? "") : String(value);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function refs(instance: Instance, key: string): string[] {
|
|
71
|
+
const value = instance.properties?.[key];
|
|
72
|
+
if (value == null) return [];
|
|
73
|
+
const list = Array.isArray(value) ? value : [value];
|
|
74
|
+
return list.map((v: any) => (typeof v === "object" ? v.ref : String(v)));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const PLACEHOLDER = /%pn|%parent|%self|%n|\{([A-Za-z][A-Za-z0-9_]*)\}/g;
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
// Label catalog values are Fluent message patterns (metanorma/
|
|
81
|
+
// fluent-min semantics): `{ $var }` placeables bind from the labeller
|
|
82
|
+
// bindings, `{ message }` references resolve within the catalog
|
|
83
|
+
// (default vocabulary + host labels), depth is bounded, and a missing
|
|
84
|
+
// variable renders as its own literal `{ name }`.
|
|
85
|
+
export const FLUENT_MAX_DEPTH = 5;
|
|
86
|
+
|
|
87
|
+
export function messageTable(bindings: Record<string, any>): Record<string, string> {
|
|
88
|
+
return { ...LABEL_VOCABULARY, ...(bindings.labels ?? {}) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function resolveMessage(value: string, bindings: Record<string, any>, depth = 0): string {
|
|
92
|
+
if (depth > FLUENT_MAX_DEPTH) throw new ResolutionError("catalog message nesting too deep");
|
|
93
|
+
let out = "";
|
|
94
|
+
let i = 0;
|
|
95
|
+
while (i < value.length) {
|
|
96
|
+
const ch = value[i];
|
|
97
|
+
if (ch === "\\" && i + 1 < value.length && "{}\\".includes(value[i + 1])) {
|
|
98
|
+
out += value[i + 1];
|
|
99
|
+
i += 2;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (ch === "{") {
|
|
103
|
+
const end = value.indexOf("}", i);
|
|
104
|
+
if (end < 0) { out += ch; i += 1; continue; }
|
|
105
|
+
const inner = value.slice(i + 1, end).trim();
|
|
106
|
+
if (inner.startsWith("$")) {
|
|
107
|
+
const name = inner.slice(1).trim();
|
|
108
|
+
const v = bindings[name];
|
|
109
|
+
out += v == null ? `{${name}}` : String(v);
|
|
110
|
+
} else {
|
|
111
|
+
const ref = messageTable(bindings)[inner];
|
|
112
|
+
if (ref == null) {
|
|
113
|
+
throw new ResolutionError(`catalog message references unknown message ${JSON.stringify(inner)}`);
|
|
114
|
+
}
|
|
115
|
+
out += resolveMessage(ref, bindings, depth + 1);
|
|
116
|
+
}
|
|
117
|
+
i = end + 1;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
out += ch;
|
|
121
|
+
i += 1;
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function render(pattern: string, bindings: Record<string, any>): string {
|
|
127
|
+
return pattern.replace(PLACEHOLDER, (token, key) => {
|
|
128
|
+
if (
|
|
129
|
+
token === "%n" || token === "%pn" ||
|
|
130
|
+
token === "%parent" || token === "%self"
|
|
131
|
+
) {
|
|
132
|
+
const value = bindings[token.slice(1)];
|
|
133
|
+
if (value == null) {
|
|
134
|
+
if (token === "%parent" || token === "%self") return "";
|
|
135
|
+
throw new ResolutionError(
|
|
136
|
+
`template ${JSON.stringify(bindings.pattern)} uses ${token} ` +
|
|
137
|
+
"but the labeller has no parent labeller bound for this element",
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return String(value);
|
|
141
|
+
}
|
|
142
|
+
const labels = bindings.labels ?? {};
|
|
143
|
+
const value = key in labels ? String(labels[key])
|
|
144
|
+
: key in LABEL_VOCABULARY ? LABEL_VOCABULARY[key] : undefined;
|
|
145
|
+
if (value === undefined) {
|
|
146
|
+
throw new ResolutionError(
|
|
147
|
+
`template ${JSON.stringify(bindings.pattern)} uses {${key}} ` +
|
|
148
|
+
"but the key is in neither the labels dictionary nor the " +
|
|
149
|
+
"default vocabulary",
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
return resolveMessage(value, bindings);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function loadProfile(text: string): Profile {
|
|
157
|
+
const data = yaml.load(text) as any;
|
|
158
|
+
const entries = data?.instances;
|
|
159
|
+
if (!Array.isArray(entries)) {
|
|
160
|
+
throw new Error("profile must be a mapping with an instances list");
|
|
161
|
+
}
|
|
162
|
+
const instances: Record<string, Instance> = {};
|
|
163
|
+
for (const entry of entries) {
|
|
164
|
+
if (!entry.name || !entry.type) {
|
|
165
|
+
throw new Error(`instance needs name and type: ${JSON.stringify(entry)}`);
|
|
166
|
+
}
|
|
167
|
+
instances[entry.name] = {
|
|
168
|
+
name: entry.name,
|
|
169
|
+
type: entry.type,
|
|
170
|
+
properties: entry.properties ?? {},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return { instances };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export class SequenceExhaustedError extends Error {}
|
|
177
|
+
|
|
178
|
+
const CYRILLIC_UPPER = [
|
|
179
|
+
"А", "Б", "В", "Г", "Д", "Е", "Ж", "З", "И", "Й", "К", "Л", "М", "Н",
|
|
180
|
+
"О", "П", "Р", "С", "Т", "У", "Ф", "Х", "Ц", "Ч", "Ш", "Щ", "Ъ", "Ы",
|
|
181
|
+
"Ь", "Э", "Ю", "Я",
|
|
182
|
+
];
|
|
183
|
+
const CYRILLIC_LOWER = CYRILLIC_UPPER.map((c) => c.toLowerCase());
|
|
184
|
+
const GREEK_UPPER = [
|
|
185
|
+
"Α", "Β", "Γ", "Δ", "Ε", "Ζ", "Η", "Θ", "Ι", "Κ", "Λ", "Μ", "Ν", "Ξ",
|
|
186
|
+
"Ο", "Π", "Ρ", "Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω",
|
|
187
|
+
];
|
|
188
|
+
const GREEK_LOWER = GREEK_UPPER.map((c) => c.toLowerCase());
|
|
189
|
+
const HEBREW = [
|
|
190
|
+
"א", "ב", "ג", "ד", "ה", "ו", "ז", "ח", "ט", "י", "כ", "ל", "מ", "נ",
|
|
191
|
+
"ס", "ע", "פ", "צ", "ק", "ר", "ש", "ת",
|
|
192
|
+
];
|
|
193
|
+
|
|
194
|
+
function numericSpec(fmt: string) {
|
|
195
|
+
return { type: "NumericSequence", properties: { initial_value: 1, increment: 1, format: fmt } };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// The default sequences required of a LADL-compliant runtime (MN 112
|
|
199
|
+
// "NumericSequence").
|
|
200
|
+
export const BUILTIN_SEQUENCE_SPECS: Record<
|
|
201
|
+
string, { type: string; properties: Record<string, any> }
|
|
202
|
+
> = {
|
|
203
|
+
DecimalNumericSequence: numericSpec("decimal"),
|
|
204
|
+
ArabicNumericSequence: numericSpec("arabic_indic"),
|
|
205
|
+
ArabicIndicNumericSequence: {
|
|
206
|
+
type: "NumericSequence",
|
|
207
|
+
properties: { initial_value: 1, increment: 1, format: "arabic_indic", char_direction: "rtl" },
|
|
208
|
+
},
|
|
209
|
+
RomanUpperNumericSequence: numericSpec("roman_upper"),
|
|
210
|
+
RomanLowerNumericSequence: numericSpec("roman_lower"),
|
|
211
|
+
AlphaUpperNumericSequence: numericSpec("alpha_upper"),
|
|
212
|
+
AlphaLowerNumericSequence: numericSpec("alpha_lower"),
|
|
213
|
+
ChineseNumericSequence: numericSpec("chinese"),
|
|
214
|
+
JapaneseNumericSequence: numericSpec("japanese"),
|
|
215
|
+
KoreanNumericSequence: numericSpec("korean"),
|
|
216
|
+
CyrillicUpperCharacterSequence: {
|
|
217
|
+
type: "CharacterSequence",
|
|
218
|
+
properties: { characters: [...CYRILLIC_UPPER], expansion_rule: "prepend_prefix" },
|
|
219
|
+
},
|
|
220
|
+
CyrillicLowerCharacterSequence: {
|
|
221
|
+
type: "CharacterSequence",
|
|
222
|
+
properties: { characters: [...CYRILLIC_LOWER], expansion_rule: "prepend_prefix" },
|
|
223
|
+
},
|
|
224
|
+
GreekUpperCharacterSequence: {
|
|
225
|
+
type: "CharacterSequence",
|
|
226
|
+
properties: { characters: [...GREEK_UPPER], expansion_rule: "prepend_prefix" },
|
|
227
|
+
},
|
|
228
|
+
GreekLowerCharacterSequence: {
|
|
229
|
+
type: "CharacterSequence",
|
|
230
|
+
properties: { characters: [...GREEK_LOWER], expansion_rule: "prepend_prefix" },
|
|
231
|
+
},
|
|
232
|
+
HebrewNumericSequence: {
|
|
233
|
+
type: "CharacterSequence",
|
|
234
|
+
properties: { characters: [...HEBREW], expansion_rule: "prepend_prefix" },
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const ARABIC_INDIC_DIGITS = "٠١٢٣٤٥٦٧٨٩";
|
|
239
|
+
|
|
240
|
+
// Labeling formats: renders a 1-based numeric value in a particular
|
|
241
|
+
// alphanumeric system (MN 112 "labeling format").
|
|
242
|
+
export const Formatters = {
|
|
243
|
+
format(fmt: string, number: number): string {
|
|
244
|
+
if (number < 1) {
|
|
245
|
+
throw new ResolutionError(`sequence value must be positive, got ${number}`);
|
|
246
|
+
}
|
|
247
|
+
switch (fmt) {
|
|
248
|
+
case "decimal": return String(number);
|
|
249
|
+
case "roman_upper": return Formatters.roman(number);
|
|
250
|
+
case "roman_lower": return Formatters.roman(number).toLowerCase();
|
|
251
|
+
case "alpha_upper": return Formatters.alpha(number);
|
|
252
|
+
case "alpha_lower": return Formatters.alpha(number).toLowerCase();
|
|
253
|
+
case "arabic_indic": {
|
|
254
|
+
let out = "";
|
|
255
|
+
for (const ch of String(number)) {
|
|
256
|
+
const d = ch.charCodeAt(0) - 48;
|
|
257
|
+
out += d >= 0 && d <= 9 ? ARABIC_INDIC_DIGITS[d] : ch;
|
|
258
|
+
}
|
|
259
|
+
return out;
|
|
260
|
+
}
|
|
261
|
+
case "chinese":
|
|
262
|
+
case "japanese":
|
|
263
|
+
case "korean":
|
|
264
|
+
return Formatters.eastAsian(fmt, number);
|
|
265
|
+
default:
|
|
266
|
+
throw new ResolutionError(`unknown labeling format: ${JSON.stringify(fmt)}`);
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
roman(number: number): string {
|
|
271
|
+
const pairs: [number, string][] = [
|
|
272
|
+
[1000, "M"], [900, "CM"], [500, "D"], [400, "CD"], [100, "C"],
|
|
273
|
+
[90, "XC"], [50, "L"], [40, "XL"], [10, "X"], [9, "IX"],
|
|
274
|
+
[5, "V"], [4, "IV"], [1, "I"],
|
|
275
|
+
];
|
|
276
|
+
let out = "";
|
|
277
|
+
let n = number;
|
|
278
|
+
for (const [value, letters] of pairs) {
|
|
279
|
+
while (n >= value) { out += letters; n -= value; }
|
|
280
|
+
}
|
|
281
|
+
return out;
|
|
282
|
+
},
|
|
283
|
+
|
|
284
|
+
alpha(number: number): string {
|
|
285
|
+
return Formatters.digits(number, 26).map((d) => String.fromCharCode(64 + d)).join("");
|
|
286
|
+
},
|
|
287
|
+
|
|
288
|
+
eastAsian(fmt: string, number: number): string {
|
|
289
|
+
const units = Formatters.cjkUnits[fmt];
|
|
290
|
+
const smallTen = Formatters.cjkSmallTen[fmt];
|
|
291
|
+
const smallHundred = Formatters.cjkSmallHundred[fmt];
|
|
292
|
+
const smallThousand = Formatters.cjkSmallThousand[fmt];
|
|
293
|
+
const thousands = Math.floor(number / 1000);
|
|
294
|
+
const restThousands = number % 1000;
|
|
295
|
+
const hundreds = Math.floor(restThousands / 100);
|
|
296
|
+
const restHundreds = restThousands % 100;
|
|
297
|
+
const tens = Math.floor(restHundreds / 10);
|
|
298
|
+
const ones = restHundreds % 10;
|
|
299
|
+
let out = "";
|
|
300
|
+
if (thousands > 0) out += Formatters.compound(units, thousands, smallThousand, false);
|
|
301
|
+
if (hundreds > 0) out += Formatters.compound(units, hundreds, smallHundred, false);
|
|
302
|
+
if (tens > 0) out += Formatters.compound(units, tens, smallTen, true);
|
|
303
|
+
if (ones > 0) out += units[ones];
|
|
304
|
+
return out;
|
|
305
|
+
},
|
|
306
|
+
|
|
307
|
+
cjkUnits: {
|
|
308
|
+
chinese: ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"],
|
|
309
|
+
japanese: ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"],
|
|
310
|
+
korean: ["영", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"],
|
|
311
|
+
} as Record<string, string[]>,
|
|
312
|
+
cjkSmallTen: { chinese: "十", japanese: "十", korean: "십" } as Record<string, string>,
|
|
313
|
+
cjkSmallHundred: { chinese: "百", japanese: "百", korean: "백" } as Record<string, string>,
|
|
314
|
+
cjkSmallThousand: { chinese: "千", japanese: "千", korean: "천" } as Record<string, string>,
|
|
315
|
+
|
|
316
|
+
compound(units: string[], count: number, marker: string, dropSingleUnit: boolean): string {
|
|
317
|
+
const prefix = count === 1 && dropSingleUnit ? "" : units[count];
|
|
318
|
+
return `${prefix}${marker}`;
|
|
319
|
+
},
|
|
320
|
+
|
|
321
|
+
// Bijective (spreadsheet-style) decomposition of a 1-based number
|
|
322
|
+
// into digits of the given radix.
|
|
323
|
+
digits(number: number, radix: number): number[] {
|
|
324
|
+
const digits: number[] = [];
|
|
325
|
+
let n = number;
|
|
326
|
+
while (n > 0) {
|
|
327
|
+
n -= 1;
|
|
328
|
+
digits.unshift((n % radix) + 1);
|
|
329
|
+
n = Math.floor(n / radix);
|
|
330
|
+
}
|
|
331
|
+
return digits;
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
const elementIds = new WeakMap<object, number>();
|
|
336
|
+
let nextElementId = 1;
|
|
337
|
+
|
|
338
|
+
function objectId(element: Element): number {
|
|
339
|
+
let id = elementIds.get(element);
|
|
340
|
+
if (id === undefined) {
|
|
341
|
+
id = nextElementId++;
|
|
342
|
+
elementIds.set(element, id);
|
|
343
|
+
}
|
|
344
|
+
return id;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export class Resolver {
|
|
348
|
+
profile: Profile;
|
|
349
|
+
private nameOf = new Map<Instance, string>();
|
|
350
|
+
private labellersByContext = new Map<string, Map<string, Instance>>();
|
|
351
|
+
|
|
352
|
+
constructor(profile: Profile) {
|
|
353
|
+
this.profile = profile;
|
|
354
|
+
for (const inst of Object.values(profile.instances)) {
|
|
355
|
+
if (inst.type !== "Labeller") continue;
|
|
356
|
+
const context = ref(inst, "context");
|
|
357
|
+
if (!context) {
|
|
358
|
+
throw new ResolutionError(`labeller ${inst.name} declares no context`);
|
|
359
|
+
}
|
|
360
|
+
this.nameOf.set(inst, inst.name);
|
|
361
|
+
if (!this.labellersByContext.has(context)) {
|
|
362
|
+
this.labellersByContext.set(context, new Map());
|
|
363
|
+
}
|
|
364
|
+
this.labellersByContext
|
|
365
|
+
.get(context)!.set(inst.properties?.type, inst);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
labellerForContext(context: string, elementType: string): Instance | null {
|
|
370
|
+
return this.labellersByContext.get(context)?.get(elementType) ?? null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
instanceNameOf(labeller: Instance): string {
|
|
374
|
+
return this.nameOf.get(labeller)!;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
ancestorChain(labellerName: string): string[] {
|
|
378
|
+
const chain = [labellerName];
|
|
379
|
+
const seen = new Set([labellerName]);
|
|
380
|
+
let current = labellerName;
|
|
381
|
+
for (;;) {
|
|
382
|
+
const labeller = this.profile.instances[current];
|
|
383
|
+
if (!labeller) break;
|
|
384
|
+
const parent = ref(labeller, "parent");
|
|
385
|
+
if (!parent || seen.has(parent)) break;
|
|
386
|
+
chain.push(parent);
|
|
387
|
+
seen.add(parent);
|
|
388
|
+
current = parent;
|
|
389
|
+
}
|
|
390
|
+
return chain;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
interface Frame {
|
|
395
|
+
element: Element;
|
|
396
|
+
declared?: string | null;
|
|
397
|
+
entry: Entry | null;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
interface Entry {
|
|
401
|
+
element: Element;
|
|
402
|
+
etype: string;
|
|
403
|
+
labeller: Instance;
|
|
404
|
+
labellerName: string;
|
|
405
|
+
contextName: string;
|
|
406
|
+
carrierObjectId: number;
|
|
407
|
+
carrierEntry: Entry | null;
|
|
408
|
+
position: number;
|
|
409
|
+
rawValue: string | null;
|
|
410
|
+
parentHier: string | null;
|
|
411
|
+
parentEntry: Entry | null;
|
|
412
|
+
hierarchical: string | null;
|
|
413
|
+
local: string | null;
|
|
414
|
+
global: string | null;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function conditionHolds(condition: any, total: number): boolean {
|
|
418
|
+
if (!condition) return false;
|
|
419
|
+
if (condition.type === "element_count_gt") return total > condition.threshold;
|
|
420
|
+
if (condition.type === "element_count_lt") return total < condition.threshold;
|
|
421
|
+
return false;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
function selectTemplate(templates: Instance[], total: number): Instance | null {
|
|
425
|
+
const holding = templates.find(
|
|
426
|
+
(t) => t.properties?.condition && conditionHolds(t.properties.condition, total),
|
|
427
|
+
);
|
|
428
|
+
if (holding) return holding;
|
|
429
|
+
return templates.find((t) => !t.properties?.condition) ?? templates[0] ?? null;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function joinHierarchical(parentHier: string | null, raw: string | null): string | null {
|
|
433
|
+
if (parentHier && raw === null) return parentHier;
|
|
434
|
+
if (parentHier === null) return raw;
|
|
435
|
+
return `${parentHier}.${raw}`;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export class Engine {
|
|
439
|
+
labels = new Map<string, Label>();
|
|
440
|
+
private resolved: Resolver;
|
|
441
|
+
private labelsInput: Record<string, string>;
|
|
442
|
+
private plan: Entry[] = [];
|
|
443
|
+
private totals = new Map<string, number>();
|
|
444
|
+
|
|
445
|
+
constructor(profile: Profile, labels?: Record<string, string>) {
|
|
446
|
+
this.resolved = new Resolver(profile);
|
|
447
|
+
this.labelsInput = labels ?? {};
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
label(tree: Element): this {
|
|
451
|
+
this.plan = [];
|
|
452
|
+
this.totals = new Map();
|
|
453
|
+
this.labels = new Map();
|
|
454
|
+
this.walk(tree, []);
|
|
455
|
+
this.render();
|
|
456
|
+
return this;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
private walk(element: Element, ancestors: Frame[]): void {
|
|
460
|
+
const [labeller, contextName, labelCarrier] =
|
|
461
|
+
this.resolveOwnLabel(element, ancestors);
|
|
462
|
+
const association = this.nearestAssociation(element, ancestors);
|
|
463
|
+
const frame: Frame = { element, declared: element.context, entry: null };
|
|
464
|
+
if (labeller) {
|
|
465
|
+
frame.entry = this.planElement(
|
|
466
|
+
element, labeller, contextName!, labelCarrier, association, ancestors,
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
ancestors.push(frame);
|
|
470
|
+
for (const child of element.children ?? []) this.walk(child, ancestors);
|
|
471
|
+
ancestors.pop();
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
private resolveOwnLabel(
|
|
475
|
+
element: Element,
|
|
476
|
+
ancestors: Frame[],
|
|
477
|
+
): [Instance | null, string | null, Frame | null] {
|
|
478
|
+
if (element.context) {
|
|
479
|
+
const labeller =
|
|
480
|
+
this.resolved.labellerForContext(element.context, element.type);
|
|
481
|
+
if (labeller) {
|
|
482
|
+
const carrier: Frame = { element, declared: element.context, entry: null };
|
|
483
|
+
return [labeller, element.context, carrier];
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
for (const frame of [...ancestors].reverse()) {
|
|
487
|
+
if (!frame.declared) continue;
|
|
488
|
+
const labeller =
|
|
489
|
+
this.resolved.labellerForContext(frame.declared, element.type);
|
|
490
|
+
if (labeller) return [labeller, frame.declared, frame];
|
|
491
|
+
}
|
|
492
|
+
return [null, null, null];
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
private nearestAssociation(
|
|
496
|
+
element: Element,
|
|
497
|
+
ancestors: Frame[],
|
|
498
|
+
): Frame | null {
|
|
499
|
+
if (element.context) {
|
|
500
|
+
return { element, declared: element.context, entry: null };
|
|
501
|
+
}
|
|
502
|
+
for (const frame of [...ancestors].reverse()) {
|
|
503
|
+
if (frame.declared) return frame;
|
|
504
|
+
}
|
|
505
|
+
return null;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
private planElement(
|
|
509
|
+
element: Element,
|
|
510
|
+
labeller: Instance,
|
|
511
|
+
contextName: string,
|
|
512
|
+
labelCarrier: Frame | null,
|
|
513
|
+
scopeAssociation: Frame | null,
|
|
514
|
+
ancestors: Frame[],
|
|
515
|
+
): Entry {
|
|
516
|
+
const parent = this.findParentEntry(labeller, ancestors);
|
|
517
|
+
const parentHier = parent ? parent.hierarchical : null;
|
|
518
|
+
const position = this.incrementCounter(
|
|
519
|
+
labelCarrier!, labeller, parentHier,
|
|
520
|
+
);
|
|
521
|
+
const raw = this.sequenceRaw(labeller, position);
|
|
522
|
+
const entry: Entry = {
|
|
523
|
+
element,
|
|
524
|
+
etype: element.type,
|
|
525
|
+
labeller,
|
|
526
|
+
labellerName: this.resolved.instanceNameOf(labeller),
|
|
527
|
+
contextName,
|
|
528
|
+
carrierObjectId: objectId(labelCarrier!.element),
|
|
529
|
+
carrierEntry: scopeAssociation ? scopeAssociation.entry : null,
|
|
530
|
+
position,
|
|
531
|
+
rawValue: raw,
|
|
532
|
+
parentHier,
|
|
533
|
+
parentEntry: parent,
|
|
534
|
+
hierarchical: joinHierarchical(parentHier, raw),
|
|
535
|
+
local: null,
|
|
536
|
+
global: null,
|
|
537
|
+
};
|
|
538
|
+
if (scopeAssociation && scopeAssociation.element === element) {
|
|
539
|
+
scopeAssociation.entry = entry;
|
|
540
|
+
entry.carrierEntry = entry;
|
|
541
|
+
}
|
|
542
|
+
this.plan.push(entry);
|
|
543
|
+
return entry;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
private findParentEntry(labeller: Instance, ancestors: Frame[]): Entry | null {
|
|
547
|
+
const parentName = ref(labeller, "parent");
|
|
548
|
+
if (!parentName) return null;
|
|
549
|
+
const parentLabeller = this.resolved.profile.instances[parentName];
|
|
550
|
+
if (!parentLabeller) {
|
|
551
|
+
throw new ResolutionError(
|
|
552
|
+
`labeller ${JSON.stringify(this.resolved.instanceNameOf(labeller))} ` +
|
|
553
|
+
`references undefined parent labeller ${JSON.stringify(parentName)}`,
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
const chain = this.resolved.ancestorChain(
|
|
557
|
+
this.resolved.instanceNameOf(labeller),
|
|
558
|
+
);
|
|
559
|
+
for (const frame of [...ancestors].reverse()) {
|
|
560
|
+
const entry = frame.entry;
|
|
561
|
+
if (entry && chain.includes(entry.labellerName)) return entry;
|
|
562
|
+
}
|
|
563
|
+
const parentType = parentLabeller.properties?.type;
|
|
564
|
+
for (const frame of [...ancestors].reverse()) {
|
|
565
|
+
const entry = frame.entry;
|
|
566
|
+
if (entry && entry.etype === parentType) return entry;
|
|
567
|
+
}
|
|
568
|
+
return null;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
private incrementCounter(
|
|
572
|
+
carrier: Frame,
|
|
573
|
+
labeller: Instance,
|
|
574
|
+
parentHier: string | null,
|
|
575
|
+
): number {
|
|
576
|
+
const key = `${objectId(carrier.element)}\u0000` +
|
|
577
|
+
`${this.resolved.instanceNameOf(labeller)}\u0000${parentHier ?? ""}`;
|
|
578
|
+
const n = (this.totals.get(key) ?? 0) + 1;
|
|
579
|
+
this.totals.set(key, n);
|
|
580
|
+
return n;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
private sequenceRaw(labeller: Instance, position: number): string | null {
|
|
584
|
+
const sequence = ref(labeller, "sequence");
|
|
585
|
+
if (!sequence) return null;
|
|
586
|
+
return this.sequenceValue(this.lookupSequence(sequence), position);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
private lookupSequence(name: string): Instance {
|
|
590
|
+
const instance = this.resolved.profile.instances[name];
|
|
591
|
+
if (
|
|
592
|
+
instance &&
|
|
593
|
+
(instance.type === "NumericSequence" ||
|
|
594
|
+
instance.type === "CharacterSequence")
|
|
595
|
+
) {
|
|
596
|
+
return instance;
|
|
597
|
+
}
|
|
598
|
+
const spec = BUILTIN_SEQUENCE_SPECS[name];
|
|
599
|
+
if (!spec) {
|
|
600
|
+
throw new ResolutionError(
|
|
601
|
+
`undefined sequence: ${JSON.stringify(name)}`,
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
return { name, type: spec.type, properties: { ...spec.properties } };
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
private sequenceValue(sequence: Instance, position: number): string {
|
|
608
|
+
return sequence.type === "NumericSequence"
|
|
609
|
+
? this.numericValue(sequence, position)
|
|
610
|
+
: this.characterValue(sequence, position);
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
private numericValue(sequence: Instance, position: number): string {
|
|
614
|
+
const initial = sequence.properties.initial_value || 1;
|
|
615
|
+
const increment = sequence.properties.increment || 1;
|
|
616
|
+
const raw = initial + (position - 1) * increment;
|
|
617
|
+
const baseName = ref(sequence, "base_sequence");
|
|
618
|
+
if (baseName) return this.sequenceValue(this.lookupSequence(baseName), raw);
|
|
619
|
+
const fmt = sequence.properties.format || "decimal";
|
|
620
|
+
const cycleLength = sequence.properties.cycle_length;
|
|
621
|
+
if (Number.isInteger(cycleLength) && cycleLength > 0) {
|
|
622
|
+
return Formatters.digits(raw, cycleLength)
|
|
623
|
+
.map((d) => Formatters.format(fmt, d))
|
|
624
|
+
.join("");
|
|
625
|
+
}
|
|
626
|
+
return Formatters.format(fmt, raw);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
private characterValue(sequence: Instance, position: number): string {
|
|
630
|
+
const chars: string[] = [...(sequence.properties.characters ?? [])];
|
|
631
|
+
if (!chars.length) {
|
|
632
|
+
throw new SequenceExhaustedError("sequence has no characters");
|
|
633
|
+
}
|
|
634
|
+
const pos = position - 1;
|
|
635
|
+
if (pos >= 0 && pos < chars.length) return chars[pos];
|
|
636
|
+
const size = chars.length;
|
|
637
|
+
const cycle = Math.floor(pos / size);
|
|
638
|
+
const rule = sequence.properties.expansion_rule;
|
|
639
|
+
if (rule === "repeat_label") return chars[pos % size];
|
|
640
|
+
if (rule === "prepend_prefix") {
|
|
641
|
+
const prefixes = this.prefixesFor(sequence, chars);
|
|
642
|
+
return prefixes[(cycle - 1) % prefixes.length] + chars[pos % size];
|
|
643
|
+
}
|
|
644
|
+
if (rule === "double_character") {
|
|
645
|
+
return chars[pos % size].repeat(cycle + 1);
|
|
646
|
+
}
|
|
647
|
+
if (rule === "append_suffix") {
|
|
648
|
+
const suffixes = this.prefixesFor(sequence, chars);
|
|
649
|
+
return chars[pos % size] + suffixes[(cycle - 1) % suffixes.length];
|
|
650
|
+
}
|
|
651
|
+
if (rule === "none" || rule == null) {
|
|
652
|
+
throw new SequenceExhaustedError(
|
|
653
|
+
`sequence exhausted at position ${pos + 1} (expansion_rule none)`,
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
throw new ResolutionError(
|
|
657
|
+
`unknown expansion_rule: ${JSON.stringify(rule)}`,
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
private prefixesFor(sequence: Instance, chars: string[]): string[] {
|
|
662
|
+
let prefixes = sequence.properties.expansion_prefix ?? [];
|
|
663
|
+
if (typeof prefixes === "string") prefixes = [prefixes];
|
|
664
|
+
prefixes = prefixes.filter((p: string) => p);
|
|
665
|
+
return prefixes.length ? prefixes : chars;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
private render(): void {
|
|
669
|
+
for (const entry of this.plan) {
|
|
670
|
+
const labeller = entry.labeller;
|
|
671
|
+
const key = `${entry.carrierObjectId}\u0000` +
|
|
672
|
+
`${entry.labellerName}\u0000${entry.parentHier ?? ""}`;
|
|
673
|
+
const total = this.totals.get(key) ?? 1;
|
|
674
|
+
const localTemplate = selectTemplate(
|
|
675
|
+
this.templates(labeller, "template"), total,
|
|
676
|
+
);
|
|
677
|
+
if (!localTemplate) {
|
|
678
|
+
throw new ResolutionError(
|
|
679
|
+
`labeller ${JSON.stringify(entry.labellerName)} has no label template`,
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
const bindings: Record<string, any> = {
|
|
683
|
+
n: entry.rawValue,
|
|
684
|
+
pn: entry.parentHier,
|
|
685
|
+
parent: entry.carrierEntry ? entry.carrierEntry.global : null,
|
|
686
|
+
labels: this.labelsInput,
|
|
687
|
+
pattern: localTemplate.properties?.pattern,
|
|
688
|
+
};
|
|
689
|
+
entry.local = render(localTemplate.properties.pattern, bindings);
|
|
690
|
+
const globals = this.templates(labeller, "global_template");
|
|
691
|
+
if (!globals.length) {
|
|
692
|
+
entry.global = entry.local;
|
|
693
|
+
} else {
|
|
694
|
+
const globalTemplate = selectTemplate(globals, total)!;
|
|
695
|
+
bindings.pattern = globalTemplate.properties?.pattern;
|
|
696
|
+
bindings.self = entry.local;
|
|
697
|
+
entry.global = render(globalTemplate.properties.pattern, bindings);
|
|
698
|
+
}
|
|
699
|
+
if (entry.element.id) {
|
|
700
|
+
this.labels.set(entry.element.id, {
|
|
701
|
+
value: entry.local!,
|
|
702
|
+
reference: entry.global!,
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
private templates(labeller: Instance, key: string): Instance[] {
|
|
709
|
+
return refs(labeller, key)
|
|
710
|
+
.map((n) => this.resolved.profile.instances[n])
|
|
711
|
+
.filter((t): t is Instance => t != null);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sealed parsanol `lml` grammar artifact, byte-exact as baked
|
|
3
|
+
* by parsanol/grammars (grammar Lml, compiler 1.5.26, artifact
|
|
4
|
+
* sha256:2be6267c036caef844bb7abb7dd4f3d38613a9126647a1eb8c4b8cca4cfe2d4e).
|
|
5
|
+
* The seal is a raw-byte digest, so this travels as a string —
|
|
6
|
+
* re-vendor from parsanol/grammars on re-seal, never hand-edit.
|
|
7
|
+
*/
|
|
8
|
+
export const LML_ARTIFACT = "{\"bindings\":null,\"version\":\"1.0.0\",\"grammar\":\"Lml\",\"shape\":\"parsanol-tree/v2\",\"binding_version\":1,\"entries\":{\"main\":{\"root\":\"lml\",\"grammar\":{\"atoms\":[{\"Entity\":{\"atom\":13}},{\"Re\":{\"pattern\":\"[ ]\"}},{\"Re\":{\"pattern\":\"[\\\\x09]\"}},{\"Re\":{\"pattern\":\"[\\\\x0d]\"}},{\"Re\":{\"pattern\":\"[\\\\x0a]\"}},{\"Entity\":{\"atom\":11}},{\"Str\":{\"pattern\":\"//\"}},{\"Re\":{\"pattern\":\"[\\\\x20-\\\\u{10ffff}]\"}},{\"Repetition\":{\"atom\":7,\"min\":0,\"max\":null,\"tag\":\"Repetition\"}},{\"Re\":{\"pattern\":\"[\\\\x0a]\"}},{\"Repetition\":{\"atom\":9,\"min\":0,\"max\":1,\"tag\":\"Maybe\"}},{\"Sequence\":{\"atoms\":[6,8,10]}},{\"Alternative\":{\"atoms\":[1,2,3,4,5]}},{\"Repetition\":{\"atom\":12,\"min\":0,\"max\":null,\"tag\":\"Repetition\"}},{\"Entity\":{\"atom\":193}},{\"Re\":{\"pattern\":\"(?i:instance)\"}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":25}},{\"Entity\":{\"atom\":23}},{\"Re\":{\"pattern\":\"[\\\\x41-\\\\x5a]\"}},{\"Re\":{\"pattern\":\"[\\\\x61-\\\\x7a]\"}},{\"Re\":{\"pattern\":\"[\\\\x30-\\\\x39]\"}},{\"Re\":{\"pattern\":\"[_]\"}},{\"Alternative\":{\"atoms\":[19,20,21,22]}},{\"Repetition\":{\"atom\":18,\"min\":1,\"max\":null,\"tag\":\"Repetition\"}},{\"Named\":{\"name\":\"name\",\"atom\":24}},{\"Entity\":{\"atom\":13}},{\"Str\":{\"pattern\":\"<\"}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":32}},{\"Entity\":{\"atom\":23}},{\"Repetition\":{\"atom\":30,\"min\":1,\"max\":null,\"tag\":\"Repetition\"}},{\"Named\":{\"name\":\"type\",\"atom\":31}},{\"Sequence\":{\"atoms\":[15,16,17,26,27,28,29]}},{\"Entity\":{\"atom\":32}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":64}},{\"Re\":{\"pattern\":\"[\\\"]\"}},{\"Entity\":{\"atom\":61}},{\"Re\":{\"pattern\":\"[ !]\"}},{\"Re\":{\"pattern\":\"[\\\\x23-\\\\x5b]\"}},{\"Re\":{\"pattern\":\"[\\\\x5d-\\\\u{10ffff}]\"}},{\"Re\":{\"pattern\":\"[\\\\\\\\]\"}},{\"Re\":{\"pattern\":\"[\\\"]\"}},{\"Re\":{\"pattern\":\"[\\\\\\\\]\"}},{\"Re\":{\"pattern\":\"[/]\"}},{\"Re\":{\"pattern\":\"[b]\"}},{\"Re\":{\"pattern\":\"[f]\"}},{\"Re\":{\"pattern\":\"[n]\"}},{\"Re\":{\"pattern\":\"[r]\"}},{\"Re\":{\"pattern\":\"[t]\"}},{\"Re\":{\"pattern\":\"[u]\"}},{\"Re\":{\"pattern\":\"[\\\\x30-\\\\x39]\"}},{\"Re\":{\"pattern\":\"[\\\\x41-\\\\x46]\"}},{\"Re\":{\"pattern\":\"[\\\\x61-\\\\x66]\"}},{\"Alternative\":{\"atoms\":[52,53,54]}},{\"Repetition\":{\"atom\":55,\"min\":4,\"max\":null,\"tag\":\"Repetition\"}},{\"Sequence\":{\"atoms\":[51,56]}},{\"Alternative\":{\"atoms\":[43,44,45,46,47,48,49,50,57]}},{\"Sequence\":{\"atoms\":[42,58]}},{\"Alternative\":{\"atoms\":[39,40,41,59]}},{\"Repetition\":{\"atom\":60,\"min\":0,\"max\":null,\"tag\":\"Repetition\"}},{\"Re\":{\"pattern\":\"[\\\"]\"}},{\"Sequence\":{\"atoms\":[37,38,62]}},{\"Named\":{\"name\":\"name\",\"atom\":63}},{\"Sequence\":{\"atoms\":[34,35,36]}},{\"Alternative\":{\"atoms\":[33,65]}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":191}},{\"Str\":{\"pattern\":\"{\"}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":181}},{\"Entity\":{\"atom\":75}},{\"Entity\":{\"atom\":23}},{\"Repetition\":{\"atom\":73,\"min\":1,\"max\":null,\"tag\":\"Repetition\"}},{\"Named\":{\"name\":\"key\",\"atom\":74}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":178}},{\"Str\":{\"pattern\":\"=\"}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":177}},{\"Entity\":{\"atom\":86}},{\"Re\":{\"pattern\":\"[\\\"]\"}},{\"Entity\":{\"atom\":61}},{\"Re\":{\"pattern\":\"[\\\"]\"}},{\"Sequence\":{\"atoms\":[82,83,84]}},{\"Named\":{\"name\":\"string\",\"atom\":85}},{\"Entity\":{\"atom\":90}},{\"Re\":{\"pattern\":\"[\\\\x30-\\\\x39]\"}},{\"Repetition\":{\"atom\":88,\"min\":1,\"max\":null,\"tag\":\"Repetition\"}},{\"Named\":{\"name\":\"integer\",\"atom\":89}},{\"Entity\":{\"atom\":124}},{\"Str\":{\"pattern\":\"[\"}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":109}},{\"Entity\":{\"atom\":86}},{\"Entity\":{\"atom\":107}},{\"Str\":{\"pattern\":\"ref:\"}},{\"Str\":{\"pattern\":\"(\"}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":103}},{\"Entity\":{\"atom\":23}},{\"Repetition\":{\"atom\":101,\"min\":1,\"max\":null,\"tag\":\"Repetition\"}},{\"Named\":{\"name\":\"symbol\",\"atom\":102}},{\"Entity\":{\"atom\":13}},{\"Str\":{\"pattern\":\")\"}},{\"Sequence\":{\"atoms\":[97,98,99,100,104,105]}},{\"Named\":{\"name\":\"ref\",\"atom\":106}},{\"Entity\":{\"atom\":103}},{\"Alternative\":{\"atoms\":[95,96,108]}},{\"Entity\":{\"atom\":13}},{\"Str\":{\"pattern\":\",\"}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":109}},{\"Sequence\":{\"atoms\":[110,111,112,113]}},{\"Repetition\":{\"atom\":114,\"min\":0,\"max\":null,\"tag\":\"Repetition\"}},{\"Sequence\":{\"atoms\":[94,115]}},{\"Repetition\":{\"atom\":116,\"min\":0,\"max\":1,\"tag\":\"Maybe\"}},{\"Entity\":{\"atom\":13}},{\"Str\":{\"pattern\":\",\"}},{\"Repetition\":{\"atom\":119,\"min\":0,\"max\":1,\"tag\":\"Maybe\"}},{\"Entity\":{\"atom\":13}},{\"Str\":{\"pattern\":\"]\"}},{\"Sequence\":{\"atoms\":[92,93,117,118,120,121,122]}},{\"Named\":{\"name\":\"array\",\"atom\":123}},{\"Entity\":{\"atom\":174}},{\"Str\":{\"pattern\":\"{\"}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":164}},{\"Sequence\":{\"atoms\":[72,76,77]}},{\"Entity\":{\"atom\":75}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":161}},{\"Entity\":{\"atom\":159}},{\"Str\":{\"pattern\":\"{\"}},{\"Entity\":{\"atom\":155}},{\"Entity\":{\"atom\":153}},{\"Str\":{\"pattern\":\"{\"}},{\"Entity\":{\"atom\":150}},{\"Str\":{\"pattern\":\"{\"}},{\"Lookahead\":{\"atom\":139,\"positive\":false}},{\"Str\":{\"pattern\":\"}\"}},{\"Lookahead\":{\"atom\":141,\"positive\":false}},{\"Re\":{\"pattern\":\"[\\\\x20-\\\\u{10ffff}]\"}},{\"Sequence\":{\"atoms\":[140,142,143]}},{\"Re\":{\"pattern\":\"[\\\\x09]\"}},{\"Re\":{\"pattern\":\"[\\\\x0d]\"}},{\"Re\":{\"pattern\":\"[\\\\x0a]\"}},{\"Alternative\":{\"atoms\":[144,145,146,147]}},{\"Alternative\":{\"atoms\":[136,148]}},{\"Repetition\":{\"atom\":149,\"min\":0,\"max\":null,\"tag\":\"Repetition\"}},{\"Entity\":{\"atom\":13}},{\"Str\":{\"pattern\":\"}\"}},{\"Sequence\":{\"atoms\":[137,138,151,152]}},{\"Alternative\":{\"atoms\":[136,148]}},{\"Repetition\":{\"atom\":154,\"min\":0,\"max\":null,\"tag\":\"Repetition\"}},{\"Entity\":{\"atom\":13}},{\"Str\":{\"pattern\":\"}\"}},{\"Sequence\":{\"atoms\":[134,135,156,157]}},{\"Named\":{\"name\":\"prose\",\"atom\":158}},{\"Entity\":{\"atom\":86}},{\"Alternative\":{\"atoms\":[133,160]}},{\"Sequence\":{\"atoms\":[130,131,132]}},{\"Alternative\":{\"atoms\":[129,162]}},{\"Named\":{\"name\":\"attr\",\"atom\":163}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":164}},{\"Sequence\":{\"atoms\":[165,166]}},{\"Repetition\":{\"atom\":167,\"min\":0,\"max\":null,\"tag\":\"Repetition\"}},{\"Sequence\":{\"atoms\":[128,168]}},{\"Repetition\":{\"atom\":169,\"min\":0,\"max\":1,\"tag\":\"Maybe\"}},{\"Entity\":{\"atom\":13}},{\"Str\":{\"pattern\":\"}\"}},{\"Sequence\":{\"atoms\":[126,127,170,171,172]}},{\"Named\":{\"name\":\"block\",\"atom\":173}},{\"Entity\":{\"atom\":107}},{\"Entity\":{\"atom\":103}},{\"Alternative\":{\"atoms\":[81,87,91,125,175,176]}},{\"Sequence\":{\"atoms\":[78,79,80]}},{\"Sequence\":{\"atoms\":[72,76,77]}},{\"Alternative\":{\"atoms\":[179,162]}},{\"Named\":{\"name\":\"attr\",\"atom\":180}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":181}},{\"Sequence\":{\"atoms\":[182,183]}},{\"Repetition\":{\"atom\":184,\"min\":0,\"max\":null,\"tag\":\"Repetition\"}},{\"Sequence\":{\"atoms\":[71,185]}},{\"Repetition\":{\"atom\":186,\"min\":0,\"max\":1,\"tag\":\"Maybe\"}},{\"Entity\":{\"atom\":13}},{\"Str\":{\"pattern\":\"}\"}},{\"Sequence\":{\"atoms\":[69,70,187,188,189]}},{\"Named\":{\"name\":\"block\",\"atom\":190}},{\"Sequence\":{\"atoms\":[66,67,68]}},{\"Named\":{\"name\":\"instance\",\"atom\":192}},{\"Entity\":{\"atom\":13}},{\"Entity\":{\"atom\":193}},{\"Sequence\":{\"atoms\":[194,195]}},{\"Repetition\":{\"atom\":196,\"min\":0,\"max\":null,\"tag\":\"Repetition\"}},{\"Sequence\":{\"atoms\":[14,197]}},{\"Repetition\":{\"atom\":198,\"min\":0,\"max\":1,\"tag\":\"Maybe\"}},{\"Entity\":{\"atom\":13}},{\"Sequence\":{\"atoms\":[0,199,200]}}],\"root\":201},\"bindings\":[]}},\"preprocess\":{},\"tables\":{},\"lint\":{\"order_warnings\":[\"rule value: branches 2 and 6 are order-dependent (shared first bytes; ordered choice is decisive)\",\"rule value: branches 5 and 6 are order-dependent (shared first bytes; ordered choice is decisive)\",\"rule item: branches 2 and 3 are order-dependent (shared first bytes; ordered choice is decisive)\"]},\"default_entry\":\"main\",\"render\":{},\"derive\":{},\"tests\":[{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance A < LabelStyle {\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance A < LabelStyle {\\n name = \\\"A\\\"\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance A < LabelStyle {\\n pattern = \\\"{clause} %n\\\"\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance A < LabelTemplate {\\n pattern = \\\"%pn.%n\\\"\\n definition \\\"one-line string shorthand\\\"\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance A < LabelTemplate {\\n definition {\\n free prose with\\n * bullets\\n }\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance A < Labeller {\\n condition = {\\n type = \\\"element_count_gt\\\"\\n threshold = 1\\n }\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance A < LabelStyle {\\n sequences = [\\n DecimalNumericSequence,\\n AlphaUpperNumericSequence,\\n ]\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance A < LabelStyle {\\n labellers = [\\n \\\"id:clause_labeller\\\",\\n \\\"id:annex_labeller\\\"\\n ]\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"// leading comment\\ninstance A < LabelContext {\\n parent = B // trailing comment\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance A < LabelContext {\\n name = \\\"document\\\"\\n definition {\\n top context\\n }\\n}\\n\\ninstance B < LabelContext {\\n parent = A\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"instance AnnexLabeller < Labeller {\\n template = [NoteSingleLabelTemplate, NoteMultipleLabelTemplate]\\n global_template = [NoteSingleGlobalLabelTemplate, NoteMultipleGlobalLabelTemplate]\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"LabelStyle \\\"ISOIECLabelStyle\\\" {\\n name = \\\"ISO/IEC Label Style\\\"\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"LabelContext \\\"DocumentContext\\\" {\\n name = \\\"document\\\"\\n}\\n\\nLabelTemplate \\\"ClauseLabelTemplate\\\" {\\n pattern = \\\"{clause} %n\\\"\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"accept\",\"input\":\"LabelStyle \\\"S\\\" {\\n sequences = [\\n ref:(DecimalNumericSequence),\\n ref:(AlphaUpperNumericSequence)\\n ]\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"reject\",\"input\":\"instance A LabelStyle {\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"reject\",\"input\":\"instance A < LabelStyle {\\n name =\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"reject\",\"input\":\"instance A < LabelStyle {\\n = \\\"value\\\"\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"reject\",\"input\":\"stray text\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"reject\",\"input\":\"instance A < LabelStyle {\\n name = \\\"unterminated\\n}\",\"expect\":{}},{\"entry\":\"main\",\"kind\":\"reject\",\"input\":\"instance A < LabelStyle {\\n condition = {\\n threshold = 1\\n\",\"expect\":{}}],\"docs\":{\"sp\":\"sp = whitespace and comments \u2014 legal between all tokens.\",\"lml\":\"lml = sp [instance *(sp instance)] sp \u2014 the whole document.\\nRepetitions over capturing rules use the separated form\\n([ x *(sp x) ]); a bare repetition of a dict-producing rule\\ntrips the native VM's subtree merge.\",\"instance\":\"instance = a typed profile instance with a block body, in either\\nheader form: `instance Name < Type` (the LADL worked profile) or\\n`Type \\\"Name\\\"` (the spec-annex style). Both bind {name, type}.\",\"attr_block\":\"attr_block = the { ... } body of an instance: attributes and comments.\",\"attr\":\"attr = key = value, or a shorthand (prose block / one-line string).\",\"attr_eq\":\"attr_eq = the = value half of an attribute.\",\"attr_shorthand\":\"shorthand without \\\"=\\\": a prose block or a one-line string\",\"value\":\"value = string / integer / array / inline block / reference /\\nsymbol reference.\",\"reference\":\"reference = the explicit reference form ref:(Name) used by the\\nspec-annex style (bound as ref).\",\"inline_block\":\"inline_block = a nested attribute block as a value (conditional templates).\",\"array\":\"array = comma-separated strings and symbols; a trailing comma is legal.\",\"item\":\"item = one array element: a string, a reference, or a symbol\\nreference.\",\"prose_block\":\"prose = free text with balanced braces (definition blocks,\\nbullet lists); nests so {key} tokens cannot terminate the block.\",\"prose\":\"prose = free text with balanced braces (definition bodies).\",\"prose_nested\":\"prose_nested = a brace-nested span inside prose ({key} tokens cannot close a block).\",\"key\":\"key = an attribute name.\",\"instance_name\":\"instance_name = the declaring name of an instance (bound as name).\",\"instance_quoted_name\":\"instance_quoted_name = the quoted declaring name in the Type \\\"Name\\\"\\nheader form (bound as name, same capture as the keyword form).\",\"type_name\":\"type_name = the supertype after <, or the leading type of the\\nType \\\"Name\\\" header form (bound as type).\",\"symbol\":\"symbol = a bare reference to a sibling instance or a built-in sequence.\",\"integer\":\"integer = a bare non-negative integer (condition thresholds).\",\"string\":\"string = a single-line double-quoted string with backslash escapes.\",\"string_body\":\"string_body = the string character run (shared by string and the\\nquoted instance-name header form).\",\"comment\":\"comment = // to end of line, consuming the terminator.\",\"name_char\":\"name_char = the shared identifier class: A-Z a-z 0-9 _.\"},\"source\":\"# LML: the LutaML textual realization of the MN 112 (LADL) labeling\\n# profile \u2014 the kaleidl fleet's source format. A document is a stream\\n# of typed instances with two header forms \u2014 `instance Name < Type { }`\\n# (the LADL worked profile) and `Type \\\"Name\\\" { }` (the spec-annex\\n# style); both bind {name, type}. Attributes\\n# take five value shapes: quoted strings (label words carry {key}\\n# tokens and %n placeholders as plain string content), integers, bare\\n# symbol references (built-in sequences, sibling instances), arrays of\\n# strings and symbols (trailing comma allowed), and inline attribute\\n# blocks (conditional templates: condition = { type = ... threshold = 1 }).\\n# Two attribute shorthands bind without \\\"=\\\": a prose definition block\\n# (`definition { free text with * bullets }`) and a one-line string\\n# (`definition \\\"...\\\"`). \\\"//\\\" comments run to end of line and are legal\\n# wherever whitespace is. Instances may appear in any order; an empty\\n# document parses.\\n#\\n# Dialect notes: names are [A-Za-z0-9_] runs (instance names, types,\\n# keys, symbols share the class \u2014 the integer rule wins on ordered\\n# choice); strings are single-line with backslash escapes; braces\\n# balance recursively inside prose blocks; CRLF is tolerated as\\n# whitespace. Parsed but not validated: cross-references between\\n# instances (a name may reference an instance defined anywhere or a\\n# host-declared built-in sequence).\\n\\ngrammar Lml version \\\"1.0.0\\\" edition 2 {\\n ## sp = whitespace and comments \u2014 legal between all tokens.\\n sp = *( %x20 / %x09 / %x0D / %x0A / comment )\\n\\n ## lml = sp [instance *(sp instance)] sp \u2014 the whole document.\\n ## Repetitions over capturing rules use the separated form\\n ## ([ x *(sp x) ]); a bare repetition of a dict-producing rule\\n ## trips the native VM's subtree merge.\\n lml = sp [ instance *( sp instance ) ] sp\\n\\n ## instance = a typed profile instance with a block body, in either\\n ## header form: `instance Name < Type` (the LADL worked profile) or\\n ## `Type \\\"Name\\\"` (the spec-annex style). Both bind {name, type}.\\n instance = ( ( %i\\\"instance\\\" sp instance_name sp \\\"<\\\" sp type_name / type_name sp instance_quoted_name ) sp attr_block ) as instance\\n\\n ## attr_block = the { ... } body of an instance: attributes and comments.\\n attr_block = ( \\\"{\\\" sp [ attr *( sp attr ) ] sp \\\"}\\\" ) as block\\n\\n ## attr = key = value, or a shorthand (prose block / one-line string).\\n attr = ( key sp attr_eq / key sp attr_shorthand ) as attr\\n ## attr_eq = the = value half of an attribute.\\n attr_eq = \\\"=\\\" sp value\\n ## shorthand without \\\"=\\\": a prose block or a one-line string\\n attr_shorthand = prose_block / string\\n\\n ## value = string / integer / array / inline block / reference /\\n ## symbol reference.\\n value = string / integer / array / inline_block / reference / symbol\\n ## reference = the explicit reference form ref:(Name) used by the\\n ## spec-annex style (bound as ref).\\n reference = ( \\\"ref:\\\" \\\"(\\\" sp symbol sp \\\")\\\" ) as ref\\n ## inline_block = a nested attribute block as a value (conditional templates).\\n inline_block = ( \\\"{\\\" sp [ attr *( sp attr ) ] sp \\\"}\\\" ) as block\\n\\n ## array = comma-separated strings and symbols; a trailing comma is legal.\\n array = ( \\\"[\\\" sp [ item *( sp \\\",\\\" sp item ) ] sp [ \\\",\\\" ] sp \\\"]\\\" ) as array\\n ## item = one array element: a string, a reference, or a symbol\\n ## reference.\\n item = string / reference / symbol\\n\\n ## prose = free text with balanced braces (definition blocks,\\n ## bullet lists); nests so {key} tokens cannot terminate the block.\\n prose_block = ( \\\"{\\\" prose sp \\\"}\\\" ) as prose\\n ## prose = free text with balanced braces (definition bodies).\\n prose = *( prose_nested / ( !\\\"{\\\" !\\\"}\\\" %x20-10FFFF / %x09 / %x0D / %x0A ) )\\n ## prose_nested = a brace-nested span inside prose ({key} tokens cannot close a block).\\n prose_nested = \\\"{\\\" prose sp \\\"}\\\"\\n\\n ## key = an attribute name.\\n key = ( 1*name_char ) as key\\n ## instance_name = the declaring name of an instance (bound as name).\\n instance_name = ( 1*name_char ) as name\\n ## instance_quoted_name = the quoted declaring name in the Type \\\"Name\\\"\\n ## header form (bound as name, same capture as the keyword form).\\n instance_quoted_name = ( %x22 string_body %x22 ) as name\\n ## type_name = the supertype after <, or the leading type of the\\n ## Type \\\"Name\\\" header form (bound as type).\\n type_name = ( 1*name_char ) as type\\n ## symbol = a bare reference to a sibling instance or a built-in sequence.\\n symbol = ( 1*name_char ) as symbol\\n ## integer = a bare non-negative integer (condition thresholds).\\n integer = ( 1*%x30-39 ) as integer\\n\\n ## string = a single-line double-quoted string with backslash escapes.\\n string = ( %x22 string_body %x22 ) as string\\n ## string_body = the string character run (shared by string and the\\n ## quoted instance-name header form).\\n string_body = *( %x20-21 / %x23-5B / %x5D-10FFFF / %x5C ( %x22 / %x5C / %x2F / %x62 / %x66 / %x6E / %x72 / %x74 / ( %x75 4*( %x30-39 / %x41-46 / %x61-66 ) ) ) )\\n\\n ## comment = // to end of line, consuming the terminator.\\n comment = \\\"//\\\" *( %x20-10FFFF ) [ %x0A ]\\n\\n ## name_char = the shared identifier class: A-Z a-z 0-9 _.\\n name_char = %x41-5A / %x61-7A / %x30-39 / %x5F\\n\\n entry main: lml\\n\\n test {\\n accept \\\"instance A < LabelStyle {\\\\n}\\\"\\n accept \\\"instance A < LabelStyle {\\\\n name = \\\\\\\"A\\\\\\\"\\\\n}\\\"\\n accept \\\"instance A < LabelStyle {\\\\n pattern = \\\\\\\"{clause} %n\\\\\\\"\\\\n}\\\"\\n accept \\\"instance A < LabelTemplate {\\\\n pattern = \\\\\\\"%pn.%n\\\\\\\"\\\\n definition \\\\\\\"one-line string shorthand\\\\\\\"\\\\n}\\\"\\n accept \\\"instance A < LabelTemplate {\\\\n definition {\\\\n free prose with\\\\n * bullets\\\\n }\\\\n}\\\"\\n accept \\\"instance A < Labeller {\\\\n condition = {\\\\n type = \\\\\\\"element_count_gt\\\\\\\"\\\\n threshold = 1\\\\n }\\\\n}\\\"\\n accept \\\"instance A < LabelStyle {\\\\n sequences = [\\\\n DecimalNumericSequence,\\\\n AlphaUpperNumericSequence,\\\\n ]\\\\n}\\\"\\n accept \\\"instance A < LabelStyle {\\\\n labellers = [\\\\n \\\\\\\"id:clause_labeller\\\\\\\",\\\\n \\\\\\\"id:annex_labeller\\\\\\\"\\\\n ]\\\\n}\\\"\\n accept \\\"// leading comment\\\\ninstance A < LabelContext {\\\\n parent = B // trailing comment\\\\n}\\\"\\n accept \\\"instance A < LabelContext {\\\\n name = \\\\\\\"document\\\\\\\"\\\\n definition {\\\\n top context\\\\n }\\\\n}\\\\n\\\\ninstance B < LabelContext {\\\\n parent = A\\\\n}\\\"\\n accept \\\"instance AnnexLabeller < Labeller {\\\\n template = [NoteSingleLabelTemplate, NoteMultipleLabelTemplate]\\\\n global_template = [NoteSingleGlobalLabelTemplate, NoteMultipleGlobalLabelTemplate]\\\\n}\\\"\\n accept \\\"\\\"\\n accept \\\"LabelStyle \\\\\\\"ISOIECLabelStyle\\\\\\\" {\\\\n name = \\\\\\\"ISO/IEC Label Style\\\\\\\"\\\\n}\\\"\\n accept \\\"LabelContext \\\\\\\"DocumentContext\\\\\\\" {\\\\n name = \\\\\\\"document\\\\\\\"\\\\n}\\\\n\\\\nLabelTemplate \\\\\\\"ClauseLabelTemplate\\\\\\\" {\\\\n pattern = \\\\\\\"{clause} %n\\\\\\\"\\\\n}\\\"\\n accept \\\"LabelStyle \\\\\\\"S\\\\\\\" {\\\\n sequences = [\\\\n ref:(DecimalNumericSequence),\\\\n ref:(AlphaUpperNumericSequence)\\\\n ]\\\\n}\\\"\\n reject \\\"instance A LabelStyle {\\\\n}\\\"\\n reject \\\"instance A < LabelStyle {\\\\n name =\\\\n}\\\"\\n reject \\\"instance A < LabelStyle {\\\\n = \\\\\\\"value\\\\\\\"\\\\n}\\\"\\n reject \\\"stray text\\\"\\n reject \\\"instance A < LabelStyle {\\\\n name = \\\\\\\"unterminated\\\\n}\\\"\\n reject \\\"instance A < LabelStyle {\\\\n condition = {\\\\n threshold = 1\\\\n\\\"\\n }\\n}\\n\",\"source_hash\":\"3d1955259129bce3745d88fad983864853d94649c73f9626b2191592f5199e8a\",\"unicode_tables\":\"onigmo (Ruby 4.0.7)\",\"compiler_version\":\"1.5.26\",\"edition\":2,\"semantics_hash\":\"sha256:078b27121f2d50d096c68999311dd8a7058752450d4cd5d90f0439adcd754778\",\"checksum\":\"sha256:2be6267c036caef844bb7abb7dd4f3d38613a9126647a1eb8c4b8cca4cfe2d4e\",\"validated\":{\"lint_errors\":0,\"lint_warnings\":3,\"tests_total\":21,\"tests_failures\":0,\"self_test\":true},\"byte_checksum\":\"sha256:3a1f029e0e8716dc4680e29adb31ebfa0afea33755ec6ff5d6c815910d036f2b\"}\n";
|
package/src/lml.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LML (LutaML) profile loading — the textual MN 112 realization —
|
|
3
|
+
* through the sealed parsanol `lml` grammar artifact. This module is
|
|
4
|
+
* browser-pure: the artifact and the parse-tree mapper carry no
|
|
5
|
+
* platform I/O; runtimes (node.ts, the websites) own the wasm glue.
|
|
6
|
+
*
|
|
7
|
+
* Provenance: parsanol/grammars grammars/lml.parg, artifact
|
|
8
|
+
* sha256:2be6267c036caef844bb7abb7dd4f3d38613a9126647a1eb8c4b8cca4cfe2d4e,
|
|
9
|
+
* compiled with parsanol 1.5.26. The registry's corpus freezes the
|
|
10
|
+
* kaleidl golden fixtures. Re-vendor the artifact from parsanol/grammars
|
|
11
|
+
* when its seal changes — never hand-edit it here.
|
|
12
|
+
*/
|
|
13
|
+
import { LML_ARTIFACT } from "./lml-artifact.js";
|
|
14
|
+
import type { Instance, Profile } from "./index.js";
|
|
15
|
+
|
|
16
|
+
export { LML_ARTIFACT } from "./lml-artifact.js";
|
|
17
|
+
export const LML_ARTIFACT_CHECKSUM = "sha256:2be6267c036caef844bb7abb7dd4f3d38613a9126647a1eb8c4b8cca4cfe2d4e";
|
|
18
|
+
|
|
19
|
+
type Span = { value: string };
|
|
20
|
+
type ValueNode =
|
|
21
|
+
| { string: Span }
|
|
22
|
+
| { integer: Span }
|
|
23
|
+
| { symbol: Span }
|
|
24
|
+
| { ref: { symbol: Span } }
|
|
25
|
+
| { prose: Span }
|
|
26
|
+
| { array: ValueNode[] }
|
|
27
|
+
| { block: AttrNode[] };
|
|
28
|
+
type AttrNode = { attr: { key: Span } & ValueNode };
|
|
29
|
+
|
|
30
|
+
function unquote(raw: string): string {
|
|
31
|
+
return raw.slice(1, -1).replace(/\\(.)/g, "$1");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function mapValue(node: ValueNode): any {
|
|
35
|
+
if ("string" in node) return unquote(node.string.value);
|
|
36
|
+
if ("integer" in node) return Number(node.integer.value);
|
|
37
|
+
if ("symbol" in node) return { ref: node.symbol.value };
|
|
38
|
+
if ("ref" in node) return { ref: node.ref.symbol.value };
|
|
39
|
+
if ("prose" in node) return node.prose.value.replace(/^[{ \n\r\t]+/, "").replace(/[}\s]+$/, "");
|
|
40
|
+
if ("array" in node) return node.array.map(mapValue);
|
|
41
|
+
if ("block" in node) {
|
|
42
|
+
const out: Record<string, any> = {};
|
|
43
|
+
for (const entry of node.block) out[entry.attr.key.value] = mapValue(entry.attr as ValueNode);
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
throw new Error("unreachable LML value kind");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Map a parsanol `lml` parse tree to the Profile the engine consumes. */
|
|
50
|
+
export function profileFromLmlTree(tree: any): Profile {
|
|
51
|
+
const instances: Record<string, Instance> = {};
|
|
52
|
+
const nodes = Array.isArray(tree) ? tree : [];
|
|
53
|
+
for (const node of nodes) {
|
|
54
|
+
const inst = node?.instance;
|
|
55
|
+
if (!inst) throw new Error("LML parse tree: expected an instance node");
|
|
56
|
+
// The quoted-name header form (Type "Name") captures the name with
|
|
57
|
+
// its quotes; normalize to the bare identifier.
|
|
58
|
+
const rawName = inst.name?.value ?? "";
|
|
59
|
+
const name = rawName.startsWith('"') ? unquote(rawName) : rawName;
|
|
60
|
+
const type = inst.type?.value;
|
|
61
|
+
if (!name || !type) throw new Error("LML parse tree: instance needs name and type");
|
|
62
|
+
const properties: Record<string, any> = {};
|
|
63
|
+
for (const entry of inst.block ?? []) {
|
|
64
|
+
const attr = entry?.attr;
|
|
65
|
+
if (!attr) continue;
|
|
66
|
+
properties[attr.key.value] = mapValue(attr as ValueNode);
|
|
67
|
+
}
|
|
68
|
+
instances[name] = { name, type, properties };
|
|
69
|
+
}
|
|
70
|
+
return { instances };
|
|
71
|
+
}
|
package/src/node.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
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.
|
|
5
|
+
*/
|
|
6
|
+
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";
|
|
10
|
+
|
|
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
|
+
}
|