@pubid/pubid 0.1.69 → 0.1.71

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.
Files changed (56) hide show
  1. package/README.md +123 -1
  2. package/dist/flavors/bsi/index.d.ts +3 -2
  3. package/dist/flavors/bsi/index.js +43 -42
  4. package/dist/flavors/bsi/worker.d.ts +3 -0
  5. package/dist/flavors/bsi/worker.js +18 -0
  6. package/dist/flavors/cen_cenelec/index.d.ts +3 -2
  7. package/dist/flavors/cen_cenelec/index.js +14 -8
  8. package/dist/flavors/cen_cenelec/worker.d.ts +3 -0
  9. package/dist/flavors/cen_cenelec/worker.js +17 -0
  10. package/dist/flavors/csa/index.d.ts +2 -1
  11. package/dist/flavors/csa/index.js +27 -28
  12. package/dist/flavors/csa/worker.d.ts +3 -0
  13. package/dist/flavors/csa/worker.js +17 -0
  14. package/dist/flavors/evs/index.d.ts +2 -1
  15. package/dist/flavors/evs/index.js +4 -4
  16. package/dist/flavors/evs/worker.d.ts +3 -0
  17. package/dist/flavors/evs/worker.js +18 -0
  18. package/dist/flavors/foreign-resolver.d.ts +6 -0
  19. package/dist/flavors/foreign-resolver.js +9 -0
  20. package/dist/flavors/gost/index.d.ts +2 -1
  21. package/dist/flavors/gost/index.js +19 -20
  22. package/dist/flavors/gost/worker.d.ts +3 -0
  23. package/dist/flavors/gost/worker.js +21 -0
  24. package/dist/flavors/ieee/artifact_tree.js +1 -1
  25. package/dist/flavors/ieee/implementation.d.ts +16 -0
  26. package/dist/flavors/ieee/implementation.js +2195 -0
  27. package/dist/flavors/ieee/index.js +18 -2181
  28. package/dist/flavors/ieee/worker.d.ts +7 -0
  29. package/dist/flavors/ieee/worker.js +31 -0
  30. package/dist/flavors/index.js +6 -6
  31. package/dist/flavors/nist/implementation.d.ts +5 -0
  32. package/dist/flavors/nist/implementation.js +1326 -0
  33. package/dist/flavors/nist/index.js +14 -1330
  34. package/dist/flavors/nist/worker.d.ts +3 -0
  35. package/dist/flavors/nist/worker.js +24 -0
  36. package/dist/flavors/oiml/implementation.d.ts +2 -1
  37. package/dist/flavors/oiml/implementation.js +2 -3
  38. package/dist/flavors/oiml/index.d.ts +2 -0
  39. package/dist/flavors/oiml/index.js +10 -0
  40. package/dist/flavors/oiml/worker.d.ts +2 -0
  41. package/dist/flavors/oiml/worker.js +14 -0
  42. package/dist/flavors/worker-registry.d.ts +13 -0
  43. package/dist/flavors/worker-registry.js +120 -0
  44. package/dist/parg/artifacts/ieee.d.ts +2 -0
  45. package/dist/parg/artifacts/ieee.js +5 -0
  46. package/dist/parg/artifacts/nist.d.ts +2 -0
  47. package/dist/parg/artifacts/nist.js +5 -0
  48. package/dist/parg/runtime.d.ts +1 -13
  49. package/dist/parg/runtime.js +4 -36
  50. package/dist/parg/shape.d.ts +13 -0
  51. package/dist/parg/shape.js +39 -0
  52. package/dist/parg/worker.d.ts +9 -0
  53. package/dist/parg/worker.js +32 -0
  54. package/dist/register-types.d.ts +7 -1
  55. package/dist/register-types.js +7 -1
  56. package/package.json +7 -2
@@ -1,1335 +1,19 @@
1
- import { PubidRuntime, toBuilderTree } from "../../parg/runtime.js";
1
+ //! The node entry for the nist flavor: the fs-backed PARG runtime
2
+ //! wired into the flavor's pure implementation. The implementation
3
+ //! (./implementation.js) is environment-agnostic; the worker entry
4
+ //! (./worker.js) composes the same code over parsanol's browser engine
5
+ //! and the bundled artifact (pubid/pubid-ts#232).
6
+ import { PubidRuntime } from "../../parg/runtime.js";
7
+ import { makeNistGrammarImplementation } from "./implementation.js";
2
8
  // The grammar runtime is the baked PARG artifact (pubid-grammar
3
- // artifacts/nist.json via the parsanol engine) — the same artifact the
4
- // gem runs, so grammar changes land once and this port cannot drift.
5
- // Only the preprocessor, builder and model are ported code.
9
+ // artifacts/nist.json via the parsanol engine), loaded lazily from disk
10
+ // on the first parse and cached for the process.
6
11
  let cachedNistRuntime;
7
- function cachedRuntime(input) {
8
- cachedNistRuntime ??= PubidRuntime.load("nist");
9
- return cachedNistRuntime.parseShape(input);
10
- }
11
- import { preprocessNist } from "./preprocessor.js";
12
- import { NIST_CLASSES, NistCode, NistEdition, NistPart, NistStage, NistSupplement, NistTranslation, NistUpdate, NistVersion, NistVolume, SP_SUBSERIES, seriesPolicy, } from "./model.js";
13
- /**
14
- * Port of lib/pubid/nist/{builder,caster,router,parser_output_normalizer,
15
- * circular_supplement_builder}.rb — the parse-tree → identifier pipeline.
16
- */
17
- const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
18
- const s = (v) => {
19
- if (v === undefined || v === null)
20
- return undefined;
21
- const str = Array.isArray(v) ? v.join("") : String(v);
22
- return str.length > 0 ? str : undefined;
23
- };
24
- function flatten(data) {
25
- return Array.isArray(data) ? Object.assign({}, ...data) : data;
26
- }
27
- const TRANSLATION_MAP = {
28
- es: "spa", sp: "spa", pt: "por", id: "ind",
29
- chi: "zho", viet: "vie", port: "por", esp: "spa",
30
- };
31
- const MONTH_NUMBERS = {
32
- Jan: 1, January: 1, Feb: 2, February: 2, Mar: 3, March: 3,
33
- Apr: 4, April: 4, May: 5, Jun: 6, June: 6, Jul: 7, July: 7,
34
- Aug: 8, August: 8, Sep: 9, Sept: 9, September: 9, Oct: 10, October: 10,
35
- Nov: 11, November: 11, Dec: 12, December: 12,
36
- };
37
- const VALID_YEAR_RANGE_MIN = 1901;
38
- const VALID_YEAR_RANGE_MAX = 2026;
39
- const DASH_YEAR_AS_EDITION_SERIES = new Set(["HB", "CS", "FIPS"]);
40
- // --- Parser output normalizer ---------------------------------------------------
41
- function normalizeParsedHash(h) {
42
- mergeEditionEIntoUpdate(h);
43
- extractEmbeddedEditionWithYear(h);
44
- extractEmbeddedEditionWithoutDashYear(h);
45
- splitSecondNumberEditionYear(h);
46
- splitFipsMonthYearAfterPart(h);
47
- disambiguateIrCompoundVsEdition(h);
48
- disambiguateDashYear(h);
49
- }
50
- function mergeEditionEIntoUpdate(h) {
51
- const updatePrefix = h["update_prefix"];
52
- const update = h["update"];
53
- const editionE = h["edition_e"];
54
- if (updatePrefix === undefined || update === undefined || !isObj(editionE))
55
- return;
56
- const editionId = editionE["edition_id"];
57
- h["update"] = { ...(isObj(update) ? update : {}), update_year: editionId };
58
- delete h["edition_e"];
59
- }
60
- function editionFromParts(type, id, additionalText) {
61
- return { __edition: true, type, id, ...(additionalText !== undefined ? { additional_text: additionalText } : {}) };
62
- }
63
- function extractEmbeddedEditionWithYear(h) {
64
- const first = s(h["first_number"]);
65
- if (first !== undefined && /^[0-9]+[a-zA-Z]\d+$/.test(first) && h["edition_dash_year"] !== undefined) {
66
- const m = /^(\d+)([a-zA-Z])(\d+)$/.exec(first);
67
- if (m !== null) {
68
- h["first_number"] = m[1];
69
- h["edition_with_year"] = editionFromParts(m[2].toLowerCase(), m[3], s(isObj(h["edition_dash_year"]) ? h["edition_dash_year"]["dash_year"] : undefined));
70
- delete h["edition_dash_year"];
71
- return;
72
- }
73
- }
74
- // Same shape embedded in the SECOND number ("53e1988" of "260-53e1988").
75
- const second = s(h["second_number"]);
76
- if (second !== undefined && /^[0-9]+[a-zA-Z]\d+$/.test(second)) {
77
- const m = /^(\d+)([a-zA-Z])(\d+)$/.exec(second);
78
- if (m !== null) {
79
- h["second_number"] = m[1];
80
- h["edition_with_year"] = editionFromParts(m[2].toLowerCase(), m[3]);
81
- }
82
- }
83
- }
84
- function extractEmbeddedEditionWithoutDashYear(h) {
85
- if (h["second_number"] !== undefined)
86
- return;
87
- const first = s(h["first_number"]);
88
- if (first === undefined || !/^[0-9]+[a-zA-Z]\d+$/.test(first))
89
- return;
90
- if (h["edition_dash_year"] !== undefined)
91
- return;
92
- const m = /^(\d+)([a-zA-Z])(\d+)$/.exec(first);
93
- if (m === null)
94
- return;
95
- h["first_number"] = m[1];
96
- h["edition_with_year"] = editionFromParts(m[2].toLowerCase(), m[3]);
97
- }
98
- function splitSecondNumberEditionYear(h) {
99
- const combined = h["second_number_edition_year"];
100
- if (!isObj(combined))
101
- return;
102
- h["second_number"] = combined["second_number"];
103
- const dashYear = s(combined["dash_year"]);
104
- const isHandbook = s(h["series"]) === "HB";
105
- if (isHandbook && dashYear !== undefined && /^\d{4}$/.test(dashYear)) {
106
- h["edition_from_year"] = editionFromParts("e", dashYear);
107
- }
108
- else {
109
- h["edition_dash_year"] = { dash_year: dashYear };
110
- }
111
- delete h["second_number_edition_year"];
112
- }
113
- function splitFipsMonthYearAfterPart(h) {
114
- const combined = h["fips_month_year_after_part"];
115
- if (!isObj(combined))
116
- return;
117
- h["second_number"] = combined["second_number"];
118
- const monthStr = s(combined["edition_month"]) ?? "";
119
- const yearStr = s(combined["edition_year"]) ?? "";
120
- const monthNum = MONTH_NUMBERS[monthStr];
121
- const editionId = monthNum !== undefined ? `${yearStr}${String(monthNum).padStart(2, "0")}` : yearStr;
122
- h["edition_from_year"] = editionFromParts("e", editionId);
123
- delete h["fips_month_year_after_part"];
124
- }
125
- function disambiguateIrCompoundVsEdition(h) {
126
- if (s(h["series"]) !== "IR")
127
- return;
128
- if (h["first_number"] === undefined || h["edition_dash_year"] === undefined)
129
- return;
130
- const firstNum = s(h["first_number"]) ?? "";
131
- const dashYear = s(isObj(h["edition_dash_year"]) ? h["edition_dash_year"]["dash_year"] : undefined) ?? "";
132
- if (!/^\d{2}$/.test(firstNum) || !/^\d{4}$/.test(dashYear))
133
- return;
134
- const isValidYear = Number(dashYear) >= VALID_YEAR_RANGE_MIN && Number(dashYear) <= VALID_YEAR_RANGE_MAX;
135
- const hasEmbeddedEdition = h["edition_e"] !== undefined;
136
- if (isValidYear && !hasEmbeddedEdition) {
137
- h["edition"] = editionFromParts("e", dashYear);
138
- }
139
- else {
140
- h["first_number"] = `${firstNum}-${dashYear}`;
141
- }
142
- delete h["edition_dash_year"];
143
- }
144
- function disambiguateDashYear(h) {
145
- if (h["first_number"] === undefined || h["edition_dash_year"] === undefined)
146
- return;
147
- const first = s(h["first_number"]) ?? "";
148
- if (/^[0-9]+[a-zA-Z]\d+$/.test(first))
149
- return;
150
- const dashYear = s(isObj(h["edition_dash_year"]) ? h["edition_dash_year"]["dash_year"] : undefined) ?? "";
151
- const series = s(h["series"]) ?? "";
152
- const dashYearNum = Number(dashYear);
153
- const isValidYear = dashYearNum >= VALID_YEAR_RANGE_MIN && dashYearNum <= VALID_YEAR_RANGE_MAX;
154
- if (series === "RPT") {
155
- h["first_number"] = `${first}-${dashYear}`;
156
- delete h["edition_dash_year"];
157
- }
158
- else if (series === "GCR") {
159
- h["edition_from_year"] = editionFromParts("e", dashYear);
160
- delete h["edition_dash_year"];
161
- }
162
- else if (series === "IR" && isValidYear) {
163
- h["edition_from_year"] = editionFromParts("e", dashYear);
164
- delete h["edition_dash_year"];
165
- }
166
- else if (DASH_YEAR_AS_EDITION_SERIES.has(series) && isValidYear) {
167
- h["edition_from_year"] = editionFromParts("e", dashYear);
168
- delete h["edition_dash_year"];
169
- }
170
- else if (dashYearNum < 1900) {
171
- h["second_number"] = dashYear;
172
- delete h["edition_dash_year"];
173
- }
174
- }
175
- function classForKind(kind) {
176
- return NIST_CLASSES.get(kind);
177
- }
178
- function locateIdentifierKlass(h) {
179
- if (h["dated_date"] !== undefined && h["dated_seq"] !== undefined) {
180
- return classForKind("dated-document");
181
- }
182
- let series = s(h["series"]);
183
- // The default-series plain-number branch (pubid#711): the grammar's
184
- // trailing alternatives accept a bare report number ("800-90B") and
185
- // its publisher-led spelling ("NIST 800-90B") with NO series token.
186
- // The SP series is the default: the docid-derived citation of
187
- // NIST.SP.800-90B is exactly the bare docnumber "800-90B", and the
188
- // Builder fills the same SP series the spelled form carries, so the
189
- // two spellings are one identity to a lookup. No other branch can
190
- // arrive here series-less (dated shapes returned above; every
191
- // series-bearing branch captures :series).
192
- if (series === undefined) {
193
- return classForKind("special-publication");
194
- }
195
- if (series?.startsWith("NBS ") === true || series?.startsWith("NBS.") === true || series?.startsWith("NIST.") === true) {
196
- const separator = series.startsWith("NBS ") ? "NBS " : series.startsWith("NBS.") ? "NBS." : "NIST.";
197
- let simple = series.slice(separator.length);
198
- if (simple === "LCIRC" || simple === "CIRC") {
199
- return hasSupplement(h) ? classForKind("circular-supplement") : simple === "CIRC" ? classForKind("circular") : classForKind("letter-circular");
200
- }
201
- series = simple;
202
- }
203
- if (series === "CS") {
204
- // The composite branch of the split first_number wrapper (the
205
- // grammar's v#n# branch) arrives under first_number_composite.
206
- const firstNum = h["first_number"] ?? h["first_number_composite"];
207
- if (isObj(firstNum) && firstNum["volume_number"] !== undefined && firstNum["issue_number"] !== undefined) {
208
- return classForKind("commercial-standards-monthly");
209
- }
210
- const firstNumStr = s(firstNum) ?? "";
211
- if (/^e\d{3,}/.test(firstNumStr)) {
212
- return classForKind("commercial-standard-emergency");
213
- }
214
- }
215
- // Series token → type via the typed-stage abbreviation table.
216
- const kind = SERIES_TO_KIND[series ?? ""];
217
- return kind !== undefined ? classForKind(kind) : classForKind("identifier");
218
- }
219
- const SERIES_TO_KIND = {
220
- SP: "special-publication",
221
- "NIST SP": "special-publication",
222
- "NBS SP": "special-publication",
223
- FIPS: "federal-information-processing-standards",
224
- "NIST FIPS": "federal-information-processing-standards",
225
- IR: "interagency-report",
226
- "NIST IR": "interagency-report",
227
- "NBS IR": "interagency-report",
228
- HB: "handbook",
229
- "NIST HB": "handbook",
230
- "NBS HB": "handbook",
231
- TN: "technical-note",
232
- "NIST TN": "technical-note",
233
- "NBS TN": "technical-note",
234
- CIRC: "circular",
235
- "NBS CIRC": "circular",
236
- CRPL: "crpl-report",
237
- "NBS CRPL": "crpl-report",
238
- "CRPL-F-B": "crpl-report",
239
- "CRPL-F-A": "crpl-report",
240
- "NBS CRPL-F-B": "crpl-report",
241
- "NBS CRPL-F-A": "crpl-report",
242
- "NBS BRPD-CRPL-D": "crpl-report",
243
- RPT: "report",
244
- "NBS RPT": "report",
245
- MONO: "monograph",
246
- "NBS MONO": "monograph",
247
- "NIST MONO": "monograph",
248
- MP: "miscellaneous-publication",
249
- "NBS MP": "miscellaneous-publication",
250
- GCR: "grant-contractor-report",
251
- "NIST GCR": "grant-contractor-report",
252
- NCSTAR: "ncstar",
253
- "NIST NCSTAR": "ncstar",
254
- OWMWP: "owmwp",
255
- "NIST OWMWP": "owmwp",
256
- NSRDS: "nsrds",
257
- "NBS NSRDS": "nsrds",
258
- "NSRDS-NBS": "nsrds",
259
- LCIRC: "letter-circular",
260
- CS: "commercial-standard",
261
- "NBS CS": "commercial-standard",
262
- "NBS CS-E": "commercial-standard-emergency",
263
- "CS-E": "commercial-standard-emergency",
264
- CSM: "commercial-standards-monthly",
265
- "NBS CSM": "commercial-standards-monthly",
266
- "NIST PS": "identifier",
267
- "NIST DCI": "identifier",
268
- "NIST Other": "identifier",
269
- "ITL Bulletin": "identifier",
270
- "CSRC Building Block": "identifier",
271
- "CSRC Use Case": "identifier",
272
- "CSRC Book": "identifier",
273
- };
274
- function hasSupplement(h) {
275
- return (h["supplement"] !== undefined ||
276
- h["supplement_date_range"] !== undefined ||
277
- h["supplement_date"] !== undefined ||
278
- h["supplement_slash_year"] !== undefined ||
279
- h["supplement_with_rev"] !== undefined);
280
- }
281
- function editionCast(type, id, additionalText, originalPrefix) {
282
- const edition = new NistEdition({ type, id, ...(additionalText !== undefined ? { additional_text: additionalText } : {}), ...(originalPrefix !== undefined ? { original_prefix: originalPrefix } : {}) });
283
- return { edition };
284
- }
285
- function cast(type, value, h, policy, series = "") {
286
- switch (type) {
287
- case "publisher":
288
- return s(value);
289
- case "dated_date": {
290
- if (!isObj(value))
291
- return undefined;
292
- return { date_year: s(value["date_year"]), date_month: s(value["date_month"]), date_day: s(value["date_day"]) };
293
- }
294
- case "dated_seq":
295
- return s(value) === undefined ? undefined : { dated_seq: s(value) };
296
- case "year":
297
- return s(value) === undefined ? undefined : Number(s(value));
298
- case "series": {
299
- const strValue = s(value);
300
- if (strValue === undefined)
301
- return undefined;
302
- const m = /^(NBS|NIST) (.+)$/.exec(strValue);
303
- if (m !== null) {
304
- return { publisher: m[1], series: new NistCode({ value: m[2] }) };
305
- }
306
- return { series: new NistCode({ value: strValue }) };
307
- }
308
- case "volume_number": {
309
- const v = s(value);
310
- return v === undefined ? undefined : { volume: new NistVolume({ value: v }) };
311
- }
312
- case "issue_number": {
313
- const v = s(value);
314
- return v === undefined ? undefined : { part: new NistPart({ type: "n", value: v }) };
315
- }
316
- case "part_number":
317
- return s(value);
318
- case "n_letter_part": {
319
- // Part type "n" with a letter value (pubid#555): the "1nA" /
320
- // "1-1nA" annex tokens capture whole, the caster strips the n.
321
- const v = s(value);
322
- if (v === undefined)
323
- return undefined;
324
- return { part: new NistPart({ type: "n", value: v.replace(/^n/, "").toUpperCase() }) };
325
- }
326
- case "letter_number": {
327
- if (!isObj(value))
328
- return undefined;
329
- const suffix = s(value["letter_suffix"]) ?? "";
330
- const extra = s(value["letter_suffix_extra"]) ?? "";
331
- const full = extra === "" ? suffix : `${suffix}${extra}`;
332
- if (full === "")
333
- return undefined;
334
- // IR keeps R/Ur for the compound handler; NCSTAR/MONO keep any
335
- // letter in the number (Ruby Series class dispatch).
336
- if (series === "IR" && (full === "R" || full === "Ur"))
337
- return { letter_number: { ...value, letter_suffix: full } };
338
- if (series === "NCSTAR" || series === "MONO") {
339
- return { letter_number: { ...value, letter_suffix: full } };
340
- }
341
- return { part: new NistPart({ type: "", value: full.toUpperCase() }) };
342
- }
343
- case "fips_part": {
344
- const v = s(value);
345
- return v === undefined ? undefined : { part: new NistPart({ type: "pt", value: v }) };
346
- }
347
- case "owmwp_date_number": {
348
- if (value === undefined)
349
- return undefined;
350
- const v = isObj(value) ? value : {};
351
- const owmwp = isObj(v["owmwp_date_number"]) ? v["owmwp_date_number"] : v;
352
- const numberPart = `${s(owmwp["owmwp_month"])}-${s(owmwp["owmwp_day"])}`;
353
- return { first_number: numberPart, edition: new NistEdition({ type: "e", id: s(owmwp["owmwp_year"]) }) };
354
- }
355
- // The polymorphic number wrappers split per branch in the grammar
356
- // (asme's designator/designator_code pattern): first_number /
357
- // second_number arrive over the bare branches, first_number_composite /
358
- // second_number_composite (raw) over the branches carrying inner
359
- // captures. The composite keys map onto the same dispatch.
360
- case "first_number":
361
- case "first_number_composite":
362
- return castNumber("first_number", value, h, policy);
363
- case "second_number":
364
- case "second_number_composite":
365
- return castNumber("second_number", value, h, policy);
366
- case "crpl_range": {
367
- const strValue = s(value);
368
- if (strValue === undefined)
369
- return undefined;
370
- let m = /^(\d+)_(\d+-\d+)([A-Z])$/.exec(strValue);
371
- if (m !== null) {
372
- return {
373
- second_number: m[1],
374
- part: new NistPart({ type: "pt", value: m[2] }),
375
- supplement: m[3],
376
- };
377
- }
378
- m = /^(\d+)_(\d+-\d+)$/.exec(strValue);
379
- if (m !== null) {
380
- return { second_number: m[1], part: new NistPart({ type: "pt", value: m[2] }) };
381
- }
382
- return { second_number: strValue };
383
- }
384
- case "stage": {
385
- if (!isObj(value))
386
- return undefined;
387
- const stageId = (s(value["stage_id"]) ?? "").toLowerCase();
388
- const stageType = (s(value["stage_type"]) ?? "").toLowerCase();
389
- if (stageId === "" || stageType === "")
390
- return undefined;
391
- return { stage: new NistStage({ id: stageId, type: stageType }) };
392
- }
393
- case "stage_id":
394
- case "stage_type":
395
- return undefined;
396
- case "parsed_format": {
397
- const v = s(value);
398
- return v === "short" || v === undefined ? undefined : v;
399
- }
400
- case "translation": {
401
- const v = s(value);
402
- if (v === undefined)
403
- return undefined;
404
- const stripped = v.trim();
405
- const code = TRANSLATION_MAP[stripped.toLowerCase()] ?? stripped.toLowerCase();
406
- // The gem's Translation#to_s (short) renders the parenthesised
407
- // suffix — the 1.x corpus shape ("(spa)", pubid#497); the wire
408
- // serializes that form.
409
- return { translation_component: new NistTranslation({ code }), translation: `(${code})` };
410
- }
411
- case "version": {
412
- const v = s(value);
413
- return v === undefined ? undefined : { version_component: new NistVersion({ value: v }) };
414
- }
415
- case "update": {
416
- if (isObj(value)) {
417
- // Ruby's prefix lookup reads the INNER hash where update_prefix
418
- // never lands — the prefix is always the slash form.
419
- const update = new NistUpdate({
420
- number: s(value["update_number"]),
421
- year: s(value["update_year"]),
422
- month: s(value["update_month"]),
423
- prefix: "slash",
424
- });
425
- return { update, update_component: update };
426
- }
427
- // Bare "-upd"/"/upd" with no details: Ruby's empty-string branch reads
428
- // the OUTER hash prefix, so the dash/slash distinction survives here.
429
- const barePrefix = (s(h["update_prefix"]) ?? "").includes("-") ? "dash" : "slash";
430
- const update = new NistUpdate({ number: "1", prefix: barePrefix });
431
- return { update, update_component: update };
432
- }
433
- case "update_prefix":
434
- case "update_number":
435
- case "update_year":
436
- case "update_month":
437
- return undefined;
438
- case "volume": {
439
- const v = s(value);
440
- return v === undefined ? undefined : { volume: new NistVolume({ value: v }) };
441
- }
442
- case "section":
443
- case "appendix":
444
- case "errata":
445
- case "index":
446
- // The component's value is the long form either way (pubid#584):
447
- // both spellings parse, the render prints the abbreviated indx.
448
- if (s(value) === "indx")
449
- return "index";
450
- return s(value);
451
- case "insert":
452
- return s(value);
453
- case "revision": {
454
- if (isObj(value) && value["revision_prefix"] !== undefined) {
455
- const prefix = s(value["revision_prefix"]) ?? "";
456
- let id = (s(value["revision_id"]) ?? "").trim();
457
- if (id === "" || id === "r" || id === "R")
458
- id = "1";
459
- else {
460
- const m = /^(\d+[a-z]?)$/.exec(id);
461
- if (m !== null)
462
- id = m[1];
463
- }
464
- return editionCast("r", id, undefined, prefix);
465
- }
466
- const strValue = (s(value) ?? "").trim();
467
- let revisionId;
468
- if (strValue === "" || strValue === "r" || strValue === "R")
469
- revisionId = "1";
470
- else {
471
- const m = /^[rR]?(\d+[a-z]?)$/.exec(strValue);
472
- revisionId = m !== null ? m[1] : strValue;
473
- }
474
- return editionCast("r", revisionId);
475
- }
476
- case "revision_year": {
477
- const yearValue = (s(value) ?? "").trim();
478
- if (h["revision_month"] !== undefined)
479
- return yearValue;
480
- return editionCast("r", yearValue);
481
- }
482
- case "revision_month":
483
- return s(value);
484
- case "supplement_year":
485
- return s(value) === undefined ? undefined : { supplement: s(value) };
486
- case "supplement": {
487
- if (value === undefined)
488
- return undefined;
489
- if (Array.isArray(value) && value.length === 0)
490
- return { supplement: "" };
491
- const strValue = (s(value) ?? "").trim();
492
- return strValue === "" ? undefined : { supplement: strValue };
493
- }
494
- case "supplement_date_range": {
495
- if (!isObj(value))
496
- return undefined;
497
- const ms = s(value["supp_month_start"]);
498
- const ys = s(value["supp_year_start"]);
499
- const me = s(value["supp_month_end"]);
500
- const ye = s(value["supp_year_end"]);
501
- return {
502
- supplement_date_range_start: ms !== undefined && ys !== undefined ? `${ms}${ys}` : undefined,
503
- supplement_date_range_end: me !== undefined && ye !== undefined ? `${me}${ye}` : undefined,
504
- };
505
- }
506
- case "supplement_date": {
507
- if (!isObj(value))
508
- return undefined;
509
- const month = s(value["supp_month"]);
510
- const year = s(value["supp_year"]);
511
- return month !== undefined && year !== undefined ? { supplement: `${month}${year}` } : undefined;
512
- }
513
- case "supplement_slash_year": {
514
- if (!isObj(value))
515
- return undefined;
516
- const number = s(value["supp_number"]);
517
- const year = s(value["supp_year"]);
518
- return number !== undefined && year !== undefined ? { supplement: `${number}/${year}` } : undefined;
519
- }
520
- case "supplement_with_rev":
521
- return { supplement: "", supplement_has_revision: true };
522
- case "supp_year":
523
- return { supplement: s(value) ?? "" };
524
- case "edition_e":
525
- return isObj(value) ? editionCast("e", s(value["edition_id"])) : undefined;
526
- case "edition_r":
527
- case "edition_r_no_space":
528
- case "edition_rev":
529
- return isObj(value) ? editionCast("r", s(value["edition_id"])) : undefined;
530
- case "edition_r_letter":
531
- if (!isObj(value))
532
- return undefined;
533
- return editionCast("r", s(value["edition_id"]), (s(value["edition_letter"]) ?? "").toLowerCase());
534
- case "edition_r_letter_only":
535
- if (!isObj(value))
536
- return undefined;
537
- return editionCast("r", (s(value["edition_letter"]) ?? "").toLowerCase());
538
- case "edition_historical":
539
- return isObj(value) ? editionCast("-", s(value["edition_id"])) : undefined;
540
- case "edition_r_with_space_letter":
541
- case "edition_r_no_space_letter": {
542
- if (!isObj(value))
543
- return undefined;
544
- const id = s(value["edition_id"]);
545
- const editionLetter = (s(value["edition_letter"]) ?? "").toUpperCase();
546
- const hasUpdate = h["update_prefix"] !== undefined || h["update"] !== undefined;
547
- if (hasUpdate)
548
- return editionCast("r", id, editionLetter);
549
- return editionCast("r", id, editionLetter, " r");
550
- }
551
- case "edition_r_with_space": {
552
- if (!isObj(value))
553
- return undefined;
554
- const id = s(value["edition_id"]);
555
- const hasUpdate = h["update_prefix"] !== undefined || h["update"] !== undefined;
556
- if (hasUpdate)
557
- return editionCast("r", id);
558
- return editionCast("r", id, undefined, " r");
559
- }
560
- case "edition_id":
561
- case "edition_date":
562
- case "legacy_edition":
563
- case "edition_month":
564
- return undefined;
565
- case "edition_day":
566
- return s(value);
567
- case "edition_dash_year":
568
- return undefined;
569
- case "edition_year": {
570
- const yearValue = s(value);
571
- if (yearValue === undefined)
572
- return undefined;
573
- const monthStr = s(h["edition_month"]);
574
- const policyHere = seriesPolicy(s(h["series"]));
575
- if (monthStr !== undefined) {
576
- const monthNum = MONTH_NUMBERS[monthStr];
577
- if (monthNum !== undefined) {
578
- if (!policyHere.modernEditionDate &&
579
- /^[A-Z][a-z]+/.test(monthStr) &&
580
- /^\d{4}$/.test(yearValue)) {
581
- return {
582
- edition: new NistEdition({ type: "-", id: "", additional_text: `${monthStr}${yearValue}` }),
583
- edition_year: yearValue,
584
- };
585
- }
586
- let editionId = `${yearValue}${String(monthNum).padStart(2, "0")}`;
587
- if (policyHere.modernEditionDate && h["edition_day"] !== undefined) {
588
- const dayNum = Number(s(h["edition_day"]));
589
- if (dayNum > 0 && dayNum <= 31)
590
- editionId += `${String(dayNum).padStart(2, "0")}`;
591
- }
592
- return { edition: new NistEdition({ type: "e", id: editionId }), edition_year: yearValue };
593
- }
594
- }
595
- return { edition: new NistEdition({ type: "e", id: yearValue }), edition_year: yearValue };
596
- }
597
- case "edition_e_date":
598
- return isObj(value) ? editionCast("e", s(value["edition_date"])) : undefined;
599
- case "part": {
600
- const strValue = (s(value) ?? "").trim();
601
- if (strValue === "")
602
- return undefined;
603
- let m = /^(\d+)add(e\d+)$/.exec(strValue);
604
- if (m !== null)
605
- return { part: new NistPart({ type: "pt", value: m[1] }), addendum: "true" };
606
- m = /^(\d+)add/.exec(strValue);
607
- if (m !== null)
608
- return { part: new NistPart({ type: "pt", value: m[1] }), addendum: "true" };
609
- return { part: new NistPart({ type: "pt", value: strValue }) };
610
- }
611
- case "edition_letter":
612
- return s(value);
613
- case "public_draft":
614
- return s(value);
615
- case "draft": {
616
- const strValue = (s(value) ?? "").trim();
617
- if (strValue === "")
618
- return undefined;
619
- let m = /^\s*-draft\s+(\d+)$/.exec(strValue);
620
- if (m !== null)
621
- return { draft_number: m[1] };
622
- m = /^\s*(\d+)pd$/.exec(strValue);
623
- if (m !== null)
624
- return { public_draft: m[1] };
625
- return strValue;
626
- }
627
- case "addendum": {
628
- if (isObj(value)) {
629
- const addendumNum = (s(value["addendum_number"]) ?? "").trim();
630
- return addendumNum !== "" ? { addendum_number: addendumNum } : { addendum: "true" };
631
- }
632
- const strValue = (s(value) ?? "").trim();
633
- return strValue === "" ? { addendum: "true" } : { addendum_number: strValue };
634
- }
635
- case "addendum_number":
636
- return s(value);
637
- case "supplement_suffix":
638
- return { supplement: s(value) ?? "" };
639
- case "date": {
640
- if (!isObj(value))
641
- return undefined;
642
- const month = s(value["date_month"]);
643
- const year = s(value["date_year"]);
644
- if (month !== undefined && year !== undefined && value["date_day"] === undefined && /^[A-Za-z]+$/.test(month)) {
645
- return editionCast("-", undefined, `${month}${year}`);
646
- }
647
- return undefined;
648
- }
649
- default:
650
- return isObj(value) ? value : undefined;
651
- }
652
- }
653
- function castNumber(type, value, h, policy) {
654
- if (value === undefined || (s(value) === undefined && !isObj(value)))
655
- return undefined;
656
- if (isObj(value)) {
657
- if (value["owmwp_date_number"] !== undefined) {
658
- const owmwp = value["owmwp_date_number"];
659
- const numberPart = `${s(owmwp["owmwp_month"])}-${s(owmwp["owmwp_day"])}`;
660
- return { [type]: numberPart, edition: new NistEdition({ type: "e", id: s(owmwp["owmwp_year"]) }) };
661
- }
662
- if (type === "second_number" && value["number_only"] !== undefined && value["edition_id"] !== undefined) {
663
- return { second_number: value };
664
- }
665
- // The dotted revision spelling ("8278.r1"): fold to the edition,
666
- // matching the glued "8278r1" canonical — the raw hash must not
667
- // stringify into the number.
668
- if (type === "second_number" && value["revision_simple"] !== undefined) {
669
- const simple = value["revision_simple"];
670
- const editionId = s(simple["edition_id"]) ?? "";
671
- return {
672
- edition: new NistEdition({ type: "r", id: editionId }),
673
- };
674
- }
675
- if (type === "second_number" && value["revision_letter"] !== undefined) {
676
- const revisionData = value["revision_letter"];
677
- const numberOnly = s(revisionData["number_only"]) ?? "";
678
- const letter = (s(revisionData["letter"]) ?? "").toUpperCase();
679
- return { second_number: `${numberOnly}r${letter}` };
680
- }
681
- if (value["volume_number"] !== undefined && value["issue_number"] !== undefined) {
682
- return {
683
- volume: new NistVolume({ value: s(value["volume_number"]) }),
684
- part: new NistPart({ type: "n", value: s(value["issue_number"]) }),
685
- };
686
- }
687
- if (type === "first_number" && value["number_with_rev_year"] !== undefined) {
688
- const data = value["number_with_rev_year"];
689
- return {
690
- first_number: s(data["number"]),
691
- edition: new NistEdition({ type: "rv", id: s(data["revision_year"]) }),
692
- };
693
- }
694
- if (type === "first_number" && value["number_with_letter_revision"] !== undefined) {
695
- const data = value["number_with_letter_revision"];
696
- return {
697
- first_number: s(data["number"]),
698
- part: new NistPart({ type: "", value: (s(data["letter_suffix"]) ?? "").toUpperCase() }),
699
- edition: new NistEdition({ type: "r", id: s(data["revision_id"]) }),
700
- };
701
- }
702
- if (type === "first_number" && value["number"] !== undefined && value["language_code"] !== undefined) {
703
- const languageCode = (s(value["language_code"]) ?? "").toLowerCase();
704
- return {
705
- first_number: s(value["number"]),
706
- translation_component: new NistTranslation({ code: TRANSLATION_MAP[languageCode] ?? languageCode }),
707
- };
708
- }
709
- if (type === "first_number" && value["number"] !== undefined && value["part_number"] !== undefined && value["edition_year"] !== undefined) {
710
- return {
711
- first_number: s(value["number"]),
712
- part: new NistPart({ type: "pt", value: s(value["part_number"]) }),
713
- edition: new NistEdition({ type: "e", id: s(value["edition_year"]) }),
714
- };
715
- }
716
- if (type === "first_number" && value["number_with_volume"] !== undefined) {
717
- const data = value["number_with_volume"];
718
- if (data["volume_suffix"] !== undefined) {
719
- return {
720
- first_number: s(data["number"]),
721
- volume: new NistVolume({ value: s(data["volume_suffix"]) }),
722
- };
723
- }
724
- }
725
- }
726
- const strValue = s(value) ?? "";
727
- if (type === "first_number") {
728
- let m;
729
- if (h["edition_year_separate"] !== undefined && /^(\d+)e(\d+)$/.test(strValue)) {
730
- m = /^(\d+)e(\d+)$/.exec(strValue);
731
- return {
732
- first_number: m[1],
733
- edition: new NistEdition({ type: "e", id: m[2], additional_text: s(h["edition_year_separate"]) }),
734
- };
735
- }
736
- if (h["historical_month"] !== undefined && h["historical_year"] !== undefined) {
737
- const monthPart = s(h["historical_month"]);
738
- const yearPart = s(h["historical_year"]);
739
- if (/^\d+$/.test(strValue)) {
740
- return { first_number: strValue, edition: new NistEdition({ type: "-", additional_text: `${monthPart}${yearPart}` }) };
741
- }
742
- return { edition: new NistEdition({ type: "-", additional_text: `${monthPart}${yearPart}` }) };
743
- }
744
- if ((m = /^(\d+)supp?$/.exec(strValue)) !== null) {
745
- if (h["supplement_year"] !== undefined) {
746
- return { first_number: m[1], supplement: s(h["supplement_year"]) };
747
- }
748
- return { first_number: m[1], supplement: "" };
749
- }
750
- if ((m = /^(\d+)supprev$/.exec(strValue)) !== null) {
751
- return { first_number: m[1], supplement: "", supplement_has_revision: true };
752
- }
753
- if ((m = /^(\d+)e(\d+)-(\d{4})$/.exec(strValue)) !== null) {
754
- return { first_number: m[1], edition: new NistEdition({ type: "e", id: m[2], additional_text: m[3] }) };
755
- }
756
- if ((m = /^-([A-Za-z]{3,9})(\d{4})$/.exec(strValue)) !== null) {
757
- return { edition: new NistEdition({ type: "-", additional_text: `${m[1]}${m[2]}` }) };
758
- }
759
- if (/^e(\d{3,})$/.test(strValue)) {
760
- return { first_number: h["second_number"] !== undefined ? strValue : strValue.slice(1) };
761
- }
762
- if ((m = /^(\d+)e(\d+)$/.exec(strValue)) !== null && h["second_number"] === undefined && h["edition_dash_year"] === undefined) {
763
- return { first_number: m[1], edition: new NistEdition({ type: "e", id: m[2] }) };
764
- }
765
- // "150-1Ae2009" / "53ar1": letter between the compound number and an
766
- // edition/revision splits off as a Part component.
767
- if ((m = /^(\d+-\d+)([A-Z])e(\d{4})$/.exec(strValue)) !== null) {
768
- return {
769
- first_number: m[1],
770
- part: new NistPart({ type: "", value: m[2] }),
771
- edition: new NistEdition({ type: "e", id: m[3] }),
772
- };
773
- }
774
- if ((m = /^e(\d{1,2})$/.exec(strValue)) !== null) {
775
- return { edition: new NistEdition({ type: "e", id: m[1] }) };
776
- }
777
- if ((m = /^(\d+)e(\d+)rev(\d{4})$/.exec(strValue)) !== null) {
778
- return { first_number: m[1], edition: new NistEdition({ type: "e", id: m[2], additional_text: m[3] }) };
779
- }
780
- if ((m = /^(\d+)e(\d+)(rev.+)$/.exec(strValue)) !== null) {
781
- return { first_number: m[1], edition: new NistEdition({ type: "e", id: m[2], additional_text: m[3].replace(/^rev/, "") }) };
782
- }
783
- if ((m = /^(\d+)supp([A-Za-z]{3,9})(\d{4})$/.exec(strValue)) !== null) {
784
- return { first_number: m[1], supplement: `${m[2]}${m[3]}` };
785
- }
786
- if ((m = /^(\d+)supp(\d{4})$/.exec(strValue)) !== null) {
787
- return { first_number: m[1], supplement: m[2] };
788
- }
789
- if ((m = /^(\d+)supp-(\d{4})$/.exec(strValue)) !== null) {
790
- return { first_number: m[1], supplement: m[2] };
791
- }
792
- if ((m = /^(\d+)e(\d+)supp$/.exec(strValue)) !== null) {
793
- return { first_number: m[1], edition: new NistEdition({ type: "e", id: m[2] }), supplement: "" };
794
- }
795
- }
796
- else if (type === "second_number" && isObj(value) && value["first_number"] !== undefined) {
797
- const v = value;
798
- return {
799
- first_number: s(v["first_number"]),
800
- part: new NistPart({ value: s(v["part_value"]) ?? "" }),
801
- edition: new NistEdition({ type: "r", id: s(v["revision_value"]) }),
802
- };
803
- }
804
- let m;
805
- if ((m = /^(.+?)pt(\d+)r(\d+[a-z]?)$/.exec(strValue)) !== null) {
806
- return { [type]: m[1], part: new NistPart({ type: "pt", value: m[2] }), edition: new NistEdition({ type: "r", id: m[3] }) };
807
- }
808
- if ((m = /^(.+?)pt(\d+)$/.exec(strValue)) !== null) {
809
- return { [type]: m[1], part: new NistPart({ type: "pt", value: m[2] }) };
810
- }
811
- if ((m = /^(\d+)v(\d+)$/.exec(strValue)) !== null) {
812
- return { [type]: m[1], volume: m[2] };
813
- }
814
- if ((m = /^(.+?)(r\d+\/\d{4})$/i.exec(strValue)) !== null) {
815
- const rm = /^r(\d+)\/(\d{4})$/.exec(m[2]);
816
- if (rm !== null) {
817
- return { [type]: m[1], edition: new NistEdition({ type: "r", id: rm[1], additional_text: rm[2] }) };
818
- }
819
- }
820
- if ((m = /^(.*\d)(r\d{4})$/i.exec(strValue)) !== null) {
821
- return { [type]: m[1], edition: new NistEdition({ type: "r", id: m[2].slice(1) }) };
822
- }
823
- if ((m = /^(.+?)(r[A-Za-z]{3,9}\d{4})$/i.exec(strValue)) !== null) {
824
- const rm = /^r([A-Za-z]{3,9})(\d{4})$/.exec(m[2]);
825
- if (rm !== null) {
826
- return { [type]: m[1], edition: new NistEdition({ type: "r", id: `${rm[1]}${rm[2]}` }) };
827
- }
828
- }
829
- if ((m = /^(.*\d)(r\d+[a-z]?)$/i.exec(strValue)) !== null) {
830
- return { [type]: m[1], edition: new NistEdition({ type: "r", id: m[2].slice(1) }) };
831
- }
832
- if ((m = /^(.+?)(?<![a-zA-Z])(r)$/i.exec(strValue)) !== null) {
833
- return { [type]: m[1], edition: new NistEdition({ type: "r", id: "1" }) };
834
- }
835
- if ((m = /^(.+?)([A-Z])(r\d+[a-z]?)$/.exec(strValue)) !== null) {
836
- return {
837
- [type]: m[1],
838
- part: new NistPart({ type: "", value: m[2] }),
839
- edition: new NistEdition({ type: "r", id: m[3].slice(1) }),
840
- };
841
- }
842
- if ((m = /^(.+?)([A-Z])$/.exec(strValue)) !== null) {
843
- if (policy.preserveLetterSuffix) {
844
- return { [type]: strValue };
845
- }
846
- return { [type]: m[1], part: new NistPart({ type: "", value: m[2] }) };
847
- }
848
- return { [type]: strValue };
849
- }
850
- class NistBuilder {
851
- build(data, parsedFormat) {
852
- const h = { ...flatten(data) };
853
- h["parsed_format"] = parsedFormat;
854
- normalizeParsedHash(h);
855
- if (h["supplement_date_range"] !== undefined ||
856
- h["supplement_slash_year"] !== undefined ||
857
- h["supplement_month_year"] !== undefined ||
858
- h["supplement_year"] !== undefined ||
859
- h["supplement"] !== undefined ||
860
- h["supplement_empty"] !== undefined ||
861
- // The circ_series route marks the bare "NBS.CIRC.sup" head: the
862
- // edition-2 grammar cannot carry the zero-width supplement_empty
863
- // marker the Parslet tree used, so the series wrapper IS the
864
- // supplement signal.
865
- h["circ_series"] !== undefined ||
866
- h["base_portion"] !== undefined) {
867
- return this.buildCircularSupplement(h);
868
- }
869
- const klass = locateIdentifierKlass(h);
870
- const policy = seriesPolicy(s(h["series"]));
871
- const identifier = new klass();
872
- // The default-series plain-number branch (pubid#711): the grammar's
873
- // trailing alternatives carry no series token ("800-90B" /
874
- // "NIST 800-90B"), so the identity gains the class's default series
875
- // and parses == to the spelled "NIST SP 800-90B" form. The bare
876
- // publisher rides the series-default-publisher fill at the tail
877
- // (the #704 shape: filled after publisher_was_parsed is recorded,
878
- // so the bare face keeps rendering bare). The policy stays the
879
- // series-less Base one — Ruby's Series.for reads parsed_hash, not
880
- // the filled attribute (SP's own policy is Base anyway).
881
- if (h["series"] === undefined) {
882
- const defaultSeries = klass.nistDefaultSeries;
883
- if (defaultSeries !== undefined) {
884
- identifier["series"] = new NistCode({ value: defaultSeries });
885
- }
886
- }
887
- const attrs = {};
888
- const toEdition = (v) => {
889
- if (isObj(v)) {
890
- return new NistEdition({
891
- type: s(v["type"]),
892
- id: s(v["id"]),
893
- additional_text: s(v["additional_text"]),
894
- });
895
- }
896
- return undefined;
897
- };
898
- for (const editionKey of ["edition_with_year", "edition_from_year", "edition"]) {
899
- if (h[editionKey] !== undefined) {
900
- const edition = toEdition(h[editionKey]);
901
- if (edition !== undefined)
902
- attrs["edition"] = edition;
903
- delete h[editionKey];
904
- }
905
- }
906
- const supp = { value: undefined, hasRevision: false, rangeStart: undefined, rangeEnd: undefined, present: false };
907
- const captureSupplement = (key, value) => {
908
- if (key === "supplement") {
909
- supp.value = value === undefined || value === null ? undefined : String(value);
910
- }
911
- else if (key === "supplement_has_revision") {
912
- supp.hasRevision = value === true;
913
- }
914
- else if (key === "supplement_date_range_start") {
915
- supp.rangeStart = s(value);
916
- }
917
- else if (key === "supplement_date_range_end") {
918
- supp.rangeEnd = s(value);
919
- }
920
- else {
921
- return false;
922
- }
923
- supp.present = true;
924
- return true;
925
- };
926
- let firstNum;
927
- let secondNum;
928
- let secondNumRaw;
929
- let decimalNum;
930
- let letterNum;
931
- let partNum;
932
- let extractedRevision;
933
- const seriesToken = s(h["series"]);
934
- for (const [key, rawValue] of Object.entries(h)) {
935
- const realized = cast(key, rawValue, h, policy, seriesToken);
936
- if (realized === undefined)
937
- continue;
938
- if (typeof realized !== "object" || realized === null) {
939
- if (captureSupplement(key, realized))
940
- continue;
941
- }
942
- if (key === "first_number" && typeof realized === "string") {
943
- firstNum = realized;
944
- continue;
945
- }
946
- if (key === "second_number" && typeof realized === "string") {
947
- secondNum = realized;
948
- continue;
949
- }
950
- if (key === "second_number" && isObj(realized) && isObj(realized["second_number"])) {
951
- secondNumRaw = realized["second_number"];
952
- continue;
953
- }
954
- if (key === "crpl_range" && typeof realized === "object" && realized !== null && !Array.isArray(realized)) {
955
- const rec = realized;
956
- const sn = rec["second_number"];
957
- if (typeof sn === "string")
958
- secondNum = sn;
959
- if (rec["part"] !== undefined)
960
- attrs["part"] = rec["part"];
961
- if (rec["supplement"] !== undefined)
962
- captureSupplement("supplement", rec["supplement"]);
963
- continue;
964
- }
965
- if (key === "part_number") {
966
- partNum = s(rawValue);
967
- continue;
968
- }
969
- if (key === "decimal_number" && isObj(rawValue)) {
970
- decimalNum = rawValue;
971
- continue;
972
- }
973
- if (key === "letter_number" && isObj(rawValue)) {
974
- letterNum = rawValue;
975
- if (isObj(realized) && "part" in realized)
976
- attrs["part"] = realized["part"];
977
- continue;
978
- }
979
- if (isObj(realized)) {
980
- for (const [subKey, subValue] of Object.entries(realized)) {
981
- if (subKey === "first_number" && typeof subValue === "string") {
982
- firstNum = subValue;
983
- continue;
984
- }
985
- if (subKey === "second_number" && typeof subValue === "string") {
986
- secondNum = subValue;
987
- continue;
988
- }
989
- if (subKey === "second_number" && isObj(subValue)) {
990
- secondNumRaw = subValue;
991
- extractedRevision = "r";
992
- continue;
993
- }
994
- if (subKey === "revision") {
995
- extractedRevision = s(subValue);
996
- continue;
997
- }
998
- if (captureSupplement(subKey, subValue))
999
- continue;
1000
- attrs[subKey] = subValue;
1001
- }
1002
- }
1003
- else {
1004
- attrs[key] = realized;
1005
- }
1006
- }
1007
- Object.assign(identifier, attrs);
1008
- // Compound number construction.
1009
- const self = identifier;
1010
- if (firstNum !== undefined && self["number"] === undefined) {
1011
- if (self["volume"] !== undefined && self["issue_number"] !== undefined) {
1012
- // v#n# handled as Part during cast.
1013
- }
1014
- else if (decimalNum !== undefined) {
1015
- self["number"] = `${firstNum}-${s(decimalNum["decimal_base"])}.${s(decimalNum["decimal_suffix"])}`;
1016
- }
1017
- else if (letterNum !== undefined) {
1018
- const letterBase = s(letterNum["letter_base"]) ?? "";
1019
- const letterSuffix = s(letterNum["letter_suffix"]) ?? "";
1020
- const extra = s(letterNum["letter_suffix_extra"]) ?? "";
1021
- const full = extra === "" ? letterSuffix : `${letterSuffix}${extra}`;
1022
- if (policy === seriesPolicy("IR") && full === "R") {
1023
- self["number"] = `${firstNum}-${letterBase}`;
1024
- self["edition"] = new NistEdition({ type: "r", id: "1" });
1025
- }
1026
- else if (self["part"] !== undefined) {
1027
- self["number"] = `${firstNum}-${letterBase}`;
1028
- }
1029
- else {
1030
- self["number"] = `${firstNum}-${letterBase}${full}`;
1031
- }
1032
- }
1033
- else if (secondNumRaw !== undefined && secondNumRaw["number_only"] !== undefined && secondNumRaw["edition_id"] !== undefined) {
1034
- self["number"] = `${firstNum}-${s(secondNumRaw["number_only"])}`;
1035
- self["edition"] = new NistEdition({ type: "r", id: s(secondNumRaw["edition_id"]) });
1036
- }
1037
- else if (secondNum !== undefined) {
1038
- let m = /^e(\d{3})$/.exec(firstNum);
1039
- if (m !== null && /^\d{2}$/.test(secondNum)) {
1040
- self["number"] = m[1];
1041
- self["edition"] = new NistEdition({ type: "e", id: `19${secondNum}` });
1042
- }
1043
- else if ((m = /^(\d+)e(\d+)$/.exec(firstNum)) !== null && /^\d{2,4}$/.test(secondNum)) {
1044
- let yearPart = secondNum;
1045
- if (yearPart.length === 2)
1046
- yearPart = `19${yearPart}`;
1047
- self["number"] = m[1];
1048
- self["edition"] = new NistEdition({ type: "e", id: m[2], additional_text: yearPart });
1049
- }
1050
- else if ((m = /^(\d+)supp?$/.exec(firstNum)) !== null && /^\d{4}$/.test(secondNum)) {
1051
- self["number"] = m[1];
1052
- supp.value = secondNum;
1053
- supp.present = true;
1054
- }
1055
- else if ((m = /^(\d+)supp?$/.exec(secondNum)) !== null) {
1056
- self["number"] = `${firstNum}-${m[1]}`;
1057
- supp.value = "";
1058
- supp.present = true;
1059
- }
1060
- else if (klass === classForKind("technical-note") && /^(19|20)\d{2}$/.test(secondNum)) {
1061
- self["number"] = firstNum;
1062
- self["edition"] = new NistEdition({ type: "e", id: secondNum });
1063
- self["edition_year"] = secondNum;
1064
- }
1065
- else if (partNum !== undefined && policy.partNumAsComponent) {
1066
- self["part"] = new NistPart({ type: "pt", value: partNum });
1067
- self["number"] = `${firstNum}-${secondNum}`;
1068
- }
1069
- else {
1070
- let compound = `${firstNum}-${secondNum}`;
1071
- if (partNum !== undefined)
1072
- compound += `-${partNum}`;
1073
- self["number"] = compound;
1074
- }
1075
- }
1076
- else {
1077
- self["number"] = firstNum;
1078
- }
1079
- }
1080
- if (extractedRevision !== undefined && extractedRevision !== "r" && self["edition"] === undefined) {
1081
- self["edition"] = new NistEdition({ type: "r", id: extractedRevision });
1082
- }
1083
- // SP subseries metadata.
1084
- if (klass === classForKind("special-publication") && firstNum !== undefined) {
1085
- if (SP_SUBSERIES.includes(firstNum)) {
1086
- self["subseries"] = new NistCode({ value: firstNum });
1087
- }
1088
- else {
1089
- const m = /^(\d+GB)\b/.exec(firstNum);
1090
- if (m !== null && SP_SUBSERIES.includes(m[1])) {
1091
- self["subseries"] = new NistCode({ value: m[1] });
1092
- }
1093
- }
1094
- }
1095
- // IR reverses the "84e2946" form back to "84-2946".
1096
- if (policy === seriesPolicy("IR") && typeof self["number"] === "string") {
1097
- const m = /^(\d+)e(\d{4})$/.exec(self["number"]);
1098
- if (m !== null) {
1099
- self["number"] = `${m[1]}-${m[2]}`;
1100
- self["edition"] = undefined;
1101
- }
1102
- }
1103
- if (self["publisher"] === undefined) {
1104
- self["publisher_was_parsed"] = false;
1105
- }
1106
- // TN bare "-upd": Ruby's TechnicalNote#update_component= replaces a
1107
- // dash-prefix update that has no year with the default Feb-2021 update.
1108
- const tnSeriesCode = identifier.seriesCode?.();
1109
- const bareUpdate = self["update_component"] instanceof NistUpdate ? self["update_component"] : undefined;
1110
- if (tnSeriesCode === "TN" && bareUpdate !== undefined && bareUpdate.prefix === "dash" && bareUpdate.year === undefined) {
1111
- const defaulted = new NistUpdate({
1112
- number: bareUpdate.number ?? "1",
1113
- year: "2021",
1114
- month: "02",
1115
- prefix: "slash",
1116
- });
1117
- self["update_component"] = defaulted;
1118
- self["update"] = defaulted;
1119
- }
1120
- // Revision with month+year → update component (V1 compatibility).
1121
- if (h["revision_month"] !== undefined && h["revision_year"] !== undefined) {
1122
- const monthNum = MONTH_NUMBERS[s(h["revision_month"]) ?? ""] ?? 1;
1123
- const update = new NistUpdate({
1124
- number: "1",
1125
- year: s(h["revision_year"]),
1126
- month: String(monthNum).padStart(2, "0"),
1127
- prefix: "slash",
1128
- });
1129
- self["update_component"] = update;
1130
- self["update"] = update;
1131
- self["revision_year"] = undefined;
1132
- self["revision_month"] = undefined;
1133
- }
1134
- if ((supp.present || supp.hasRevision) && "supplement" in identifier.constructor.attributes) {
1135
- if (supp.rangeStart !== undefined || supp.rangeEnd !== undefined) {
1136
- const component = {};
1137
- const sm = /^([A-Za-z]{3,9})?(\d{4})$/.exec(supp.rangeStart ?? "");
1138
- if (supp.rangeStart !== undefined && sm !== null) {
1139
- component["month"] = sm[1];
1140
- component["year"] = sm[2];
1141
- }
1142
- const em = /^([A-Za-z]{3,9})?(\d{4})$/.exec(supp.rangeEnd ?? "");
1143
- if (supp.rangeEnd !== undefined && em !== null) {
1144
- component["month_end"] = em[1];
1145
- component["year_end"] = em[2];
1146
- }
1147
- self["supplement"] = new NistSupplement(component);
1148
- }
1149
- else {
1150
- self["supplement"] = NistSupplement.fromRaw(supp.value, supp.hasRevision);
1151
- }
1152
- }
1153
- // apply_series_default_publisher (the #704 rule): a series that
1154
- // implies its publisher (bare "FIPS 197" is "NIST FIPS 197"; bare
1155
- // "800-90B" is "NIST SP 800-90B" — pubid#712) gets it now, after the
1156
- // publisher_was_parsed: false above, so the two spellings are ==
1157
- // while the bare face keeps rendering bare (the printed-publisher
1158
- // views read the flag, not the filled value). Runs only when the
1159
- // publisher is absent AND unspelled — at this point that is exactly
1160
- // `publisher === undefined` (the flag was recorded false above iff
1161
- // no publisher was parsed).
1162
- if (self["publisher"] === undefined) {
1163
- const defaultPublisher = identifier.constructor.nistDefaultPublisher;
1164
- if (defaultPublisher !== undefined && defaultPublisher !== "") {
1165
- self["publisher"] = defaultPublisher;
1166
- }
1167
- }
1168
- return identifier;
1169
- }
1170
- buildCircularSupplement(h) {
1171
- if (isObj(h["supplement_slash_year"]) || isObj(h["implicit_supplement"])) {
1172
- return this.buildCircularSupplementWrapper(h);
1173
- }
1174
- const seriesValue = isObj(h["circ_series"]) ? s(h["circ_series"]["series"]) : s(h["series"]);
1175
- // The dotted series literal ("NBS.CIRC") names its publisher in the
1176
- // first token; the builder default would render "NIST" instead.
1177
- const seriesPublisher = seriesValue?.startsWith("NBS") ? "NBS" : undefined;
1178
- if (isObj(h["supplement_date_range"])) {
1179
- const range = h["supplement_date_range"];
1180
- const identifier = this.build({ series: seriesValue, publisher: seriesPublisher }, "short");
1181
- const ms = s(range["supp_month_start"]);
1182
- const ys = s(range["supp_year_start"]);
1183
- const me = s(range["supp_month_end"]);
1184
- const ye = s(range["supp_year_end"]);
1185
- identifier["supplement"] = new NistSupplement({
1186
- month: ms,
1187
- year: ys,
1188
- month_end: me,
1189
- year_end: ye,
1190
- });
1191
- return identifier;
1192
- }
1193
- const base = this.buildCircularSupplementBase(h, seriesValue, seriesPublisher);
1194
- const self = base;
1195
- const raw = h["supplement_month_year"] !== undefined
1196
- ? s(h["supplement_month_year"])
1197
- : h["supplement_year"] !== undefined
1198
- ? s(h["supplement_year"])
1199
- : "";
1200
- self["supplement"] = NistSupplement.fromRaw(raw);
1201
- return base;
1202
- }
1203
- buildCircularSupplementBase(h, seriesValue, seriesPublisher) {
1204
- const basePortion = h["base_portion"];
1205
- if (!isObj(basePortion)) {
1206
- return this.build({
1207
- publisher: s(h["publisher"]) ?? seriesPublisher,
1208
- series: seriesValue ?? s(h["series"]),
1209
- first_number: s(h["first_number"]),
1210
- }, s(h["parsed_format"]) ?? "short");
1211
- }
1212
- const baseNumber = s(basePortion["simple_number"]) ?? s(basePortion["base_number"]);
1213
- let letterSuffix;
1214
- if (basePortion["letter_suffix"] !== undefined) {
1215
- letterSuffix = (s(basePortion["letter_suffix"]) ?? "").toUpperCase();
1216
- }
1217
- let publisherValue;
1218
- if (isObj(h["circ_series"]) && h["circ_series"]["series"] !== undefined) {
1219
- const seriesStr = s(h["circ_series"]["series"]) ?? "";
1220
- if (seriesStr.includes(" "))
1221
- publisherValue = seriesStr.split(" ")[0];
1222
- }
1223
- const hasEdition = basePortion["edition_number"] !== undefined;
1224
- let baseNumberWithSuffix = baseNumber ?? "";
1225
- if (letterSuffix !== undefined)
1226
- baseNumberWithSuffix += letterSuffix;
1227
- if (hasEdition)
1228
- baseNumberWithSuffix += `e${s(basePortion["edition_number"])}`;
1229
- const baseHash = {
1230
- series: seriesValue,
1231
- first_number: baseNumberWithSuffix,
1232
- };
1233
- if (publisherValue !== undefined)
1234
- baseHash["publisher"] = publisherValue;
1235
- if (hasEdition)
1236
- baseHash["edition_e"] = { edition_id: s(basePortion["edition_number"]) };
1237
- return this.build(baseHash, s(h["parsed_format"]) ?? "short");
1238
- }
1239
- buildCircularSupplementWrapper(h) {
1240
- const klass = classForKind("circular-supplement");
1241
- const supplement = new klass();
1242
- const self = supplement;
1243
- const seriesValue = isObj(h["circ_series"])
1244
- ? s(h["circ_series"]["series"])
1245
- : s(h["series"]);
1246
- if (isObj(h["supplement_date_range"])) {
1247
- const range = h["supplement_date_range"];
1248
- const ms = s(range["supp_month_start"]);
1249
- const ys = s(range["supp_year_start"]);
1250
- const me = s(range["supp_month_end"]);
1251
- const ye = s(range["supp_year_end"]);
1252
- if (ms !== undefined && ys !== undefined)
1253
- self["supplement_date_range_start"] = `${ms}${ys}`;
1254
- if (me !== undefined && ye !== undefined)
1255
- self["supplement_date_range_end"] = `${me}${ye}`;
1256
- return supplement;
1257
- }
1258
- if (isObj(h["base_portion"])) {
1259
- const basePortion = h["base_portion"];
1260
- const baseNumber = s(basePortion["simple_number"]) ?? s(basePortion["base_number"]);
1261
- let letterSuffix;
1262
- if (basePortion["letter_suffix"] !== undefined) {
1263
- letterSuffix = (s(basePortion["letter_suffix"]) ?? "").toUpperCase();
1264
- }
1265
- let publisherValue;
1266
- if (isObj(h["circ_series"]) && h["circ_series"]["series"] !== undefined) {
1267
- const seriesStr = s(h["circ_series"]["series"]) ?? "";
1268
- if (seriesStr.includes(" "))
1269
- publisherValue = seriesStr.split(" ")[0];
1270
- }
1271
- const hasRevision = basePortion["revision_letter"] !== undefined && basePortion["revision_number"] !== undefined;
1272
- const hasEdition = basePortion["edition_number"] !== undefined;
1273
- let baseNumberWithSuffix = baseNumber ?? "";
1274
- if (letterSuffix !== undefined)
1275
- baseNumberWithSuffix += letterSuffix;
1276
- if (hasEdition)
1277
- baseNumberWithSuffix += `e${s(basePortion["edition_number"])}`;
1278
- const baseHash = { series: seriesValue, first_number: baseNumberWithSuffix };
1279
- if (publisherValue !== undefined)
1280
- baseHash["publisher"] = publisherValue;
1281
- if (hasEdition)
1282
- baseHash["edition_e"] = { edition_id: s(basePortion["edition_number"]) };
1283
- self["base"] = this.build(baseHash, s(h["parsed_format"]) ?? "short");
1284
- if (hasRevision && isObj(h["implicit_supplement"])) {
1285
- const revisionNumber = s(basePortion["revision_number"]) ?? "";
1286
- const supplementYear = s(h["implicit_supplement"]["implicit_supplement_year"]) ?? "";
1287
- self["update"] = new NistUpdate({ number: "1", year: `${supplementYear}${revisionNumber}`, prefix: "slash" });
1288
- self["implicit_supplement"] = true;
1289
- }
1290
- }
1291
- else if (h["first_number"] !== undefined) {
1292
- self["base"] = this.build({
1293
- publisher: s(h["publisher"]),
1294
- series: seriesValue ?? s(h["series"]),
1295
- first_number: s(h["first_number"]),
1296
- }, s(h["parsed_format"]) ?? "short");
1297
- }
1298
- if (h["supplement_month_year"] !== undefined) {
1299
- self["edition"] = new NistEdition({ type: "s", id: s(h["supplement_month_year"]) });
1300
- }
1301
- else if (h["supplement_year"] !== undefined) {
1302
- self["edition"] = new NistEdition({ type: "s", id: s(h["supplement_year"]) });
1303
- }
1304
- else if (isObj(h["supplement_slash_year"])) {
1305
- const suppHash = h["supplement_slash_year"];
1306
- const suppNumber = s(suppHash["supp_number"]) ?? "";
1307
- const suppYear = s(suppHash["supp_year"]) ?? "";
1308
- self["update"] = new NistUpdate({ number: "1", year: `${suppYear}${suppNumber.padStart(2, "0")}`, prefix: "slash" });
1309
- }
1310
- return supplement;
1311
- }
1312
- }
1313
12
  export function nistGrammarImplementation() {
1314
- const builder = new NistBuilder();
1315
- return {
1316
- parse(input) {
1317
- const { cleaned, format } = preprocessNist(input);
1318
- const base = cleaned.replace(/\s*\(all parts\)\s*$/, "");
1319
- const tree = toBuilderTree(cachedRuntime(base));
1320
- // The reference's language_code rule captures its separator with
1321
- // the code (".uppl"); the baked nist artifact labels the code
1322
- // alone. Restore the reference's slice for a trailing dot-prefixed
1323
- // code, so the renderer prints the canonical double-dot face
1324
- // ("NIST.SP.955-S..uppl").
1325
- const dotted = /\.([a-z]{2,4})$/.exec(base);
1326
- if (dotted && tree["translation"] === dotted[1]) {
1327
- tree["translation"] = `.${dotted[1]}`;
1328
- }
1329
- const marked = base !== cleaned
1330
- ? { ...tree, all_parts: true }
1331
- : tree;
1332
- return builder.build(marked, format);
13
+ return makeNistGrammarImplementation({
14
+ parseShape(input) {
15
+ cachedNistRuntime ??= PubidRuntime.load("nist");
16
+ return cachedNistRuntime.parseShape(input);
1333
17
  },
1334
- };
18
+ });
1335
19
  }