@menapo/vi18n 0.1.0 → 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 +14 -14
- package/dist/{apply-Bre-lY9J.js → apply-FAHl1R6T.js} +69 -89
- package/dist/{captured-Bj8bO6vd.js → captured-CTjjDF5o.js} +196 -217
- package/dist/{check-DyrAbv73.js → check-DwFwOB1d.js} +66 -84
- package/dist/cli.js +2 -3
- package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
- package/dist/collisions-uMTuq36M.js +215 -0
- package/dist/compile-CEvH-6sY.js +240 -0
- package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
- package/dist/concurrency-BCY_4zcr.js +1 -0
- package/dist/concurrency-Dy2ny50V.js +107 -0
- package/dist/config.d.ts +1 -5
- package/dist/config.js +4 -5
- package/dist/{types-D3QGM8XO.js → diagnostics-1onDF6Ua.js} +26 -26
- package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
- package/dist/{duplicates-yuRljQ6h.js → duplicates-CsUXRX2J.js} +10 -27
- package/dist/engine-Bf7nVigL.js +556 -0
- package/dist/engine.d.ts +1 -5
- package/dist/engine.js +297 -168
- package/dist/exchange-DYG9RZoT.js +41 -0
- package/dist/{exclude-Du-Zd_qI.js → exclude-vMnm3lqU.js} +2 -3
- package/dist/{export-DGsMbcTZ.js → export-BYq0gfLd.js} +17 -38
- package/dist/{extract-Dujlsgnk.js → extract-CYtuEo8u.js} +114 -82
- package/dist/{formats-KPIg-08V.js → formats-ChV1LVH_.js} +352 -276
- package/dist/fs-ezCMZeDV.js +52 -0
- package/dist/i18next-1waHziMy.js +1 -0
- package/dist/i18next-D66Icz3L.js +1907 -0
- package/dist/{import-Cwdhcywt.js → import-BApbQlmm.js} +81 -75
- package/dist/index.d.ts +1 -13
- package/dist/index.js +14 -16
- package/dist/{init-BecCTvKi.js → init-J7AwwA5e.js} +466 -501
- package/dist/{library-D5CWvTQs.js → library-BfSOBeBd.js} +975 -451
- package/dist/lingui-Ee2farVo.js +902 -0
- package/dist/lint.d.ts +0 -4
- package/dist/lint.js +69 -82
- package/dist/{load-gW-oJt1Q.js → load-BFLLqY8x.js} +272 -147
- package/dist/{load-CQRz4PpM.js → load-CMwhukZ4.js} +1211 -1115
- package/dist/{locales-DX4MOBFE.js → locales-DxLDrx-h.js} +271 -237
- package/dist/{lsp-Cq1Nuu-Q.js → lsp-BC7xDGON.js} +4 -5
- package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
- package/dist/{mcp-Cf0T7qPW.js → mcp-Cka8ywDF.js} +4 -5
- package/dist/{merge-driver-CpiiSVIc.js → merge-driver-B04Apm29.js} +138 -122
- package/dist/migrate-D7TNl6SD.js +2169 -0
- package/dist/migrate-khCY8TE0.js +1 -0
- package/dist/{openai-CTaMDcl-.js → openai-RWw0YlAx.js} +128 -87
- package/dist/{output-DsaHKHEt.js → output-CF9uvk_-.js} +15 -9
- package/dist/parse.d.ts +1 -3
- package/dist/parse.js +7 -8
- package/dist/{pipeline-wxS0SApG.js → pipeline-B30irGMD.js} +1 -1
- package/dist/{pipeline-C7g11THL.js → pipeline-sgl_q_op.js} +387 -359
- package/dist/project-DSzWKU3C.js +544 -0
- package/dist/{pseudo-DRv30iN5.js → pseudo-DNbNNVGP.js} +125 -3
- package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
- package/dist/react-intl-tzGIk6G3.js +634 -0
- package/dist/restore-BG_zMpSj.js +199 -0
- package/dist/rewrite-hAcRJwiZ.js +415 -0
- package/dist/{rich-DCUKl5HM.js → rich-ApY1Dm4S.js} +35 -28
- package/dist/rolldown-runtime-C1I3wEg2.js +43 -0
- package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
- package/dist/run-CFzR3V93.js +647 -0
- package/dist/scan-worker.d.ts +2 -4
- package/dist/scan-worker.js +1 -1
- package/dist/{scoped-BMYpSthj.js → scoped-h3KE8XWW.js} +3 -4
- package/dist/{server-BDkykMjb.js → server-WlxZH0oX.js} +275 -330
- package/dist/{server-qv3mz70d.js → server-ceuzzkal.js} +292 -386
- package/dist/server.d.ts +1 -17
- package/dist/server.js +415 -381
- package/dist/{sitemap-DV3o8QD7.js → sitemap-796jJKUT.js} +21 -33
- package/dist/{sources-D19czDSA.js → sources-DuOdM02f.js} +712 -373
- package/dist/{status-DZArW5aE.js → status-CTH3qI-0.js} +30 -30
- package/dist/{stdio-FPOKQAgu.js → stdio-D9kTzyqB.js} +2 -4
- package/dist/{stdio-CVaJfxA7.js → stdio-FWIVU3P4.js} +1 -2
- package/dist/sync-CpL0Qsgw.js +1352 -0
- package/dist/{t-DC-c9zE5.js → t-gvEXx25s.js} +76 -53
- package/dist/test.d.ts +5 -16
- package/dist/test.js +77 -48
- package/dist/translate-BrfVRkjl.js +336 -0
- package/dist/{ts-BaU5yOf0.js → ts-BX6kwig5.js} +25 -14
- package/dist/{types-DtvQ7UdD.js → types-3kwyaCAm.js} +1 -2
- package/dist/{types-DsdWBG0b.d.ts → types-BaG1LboH.d.ts} +2 -6
- package/dist/{types-CgUCkFU1.d.ts → types-Cy2bPdSg.d.ts} +14 -20
- package/dist/types-Ds_vebja.js +41 -0
- package/dist/{units-CQ5Q99RC.js → units-VVLgQ7x7.js} +7 -16
- package/dist/{url-CRfCOhUI.js → url-Bpc6wLea.js} +24 -11
- package/dist/{url-CsBkRaJy.js → url-D2_VlEeH.js} +25 -12
- package/dist/{validate-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
- package/dist/{validate-BxpupdAf.js → validate-zv4-IZyZ.js} +690 -377
- package/dist/{version-BlyLGlOa.js → version-BFs370Ce.js} +3 -5
- package/dist/{vite-scan-B_YW4X_a.js → vite-scan-DFKSGVE9.js} +87 -92
- package/dist/vite.d.ts +2 -6
- package/dist/vite.js +6482 -6018
- package/dist/vue-i18n-CBmZqHbG.js +806 -0
- package/dist/{which-C6PFI1Dm.js → which-BpyJyBkX.js} +44 -37
- package/dist/{worker-84U6uYB3.js → worker-BjzVqJVO.js} +454 -608
- package/package.json +8 -8
- package/dist/check-D34P-S--.js +0 -1
- package/dist/codec-DuvU2VQy.js +0 -207
- package/dist/collisions-CfpZU8e4.js +0 -217
- package/dist/compile-B5Mz6WJm.js +0 -245
- package/dist/concurrency-C9UUWuDC.js +0 -96
- package/dist/concurrency-DONL7t4V.js +0 -1
- package/dist/engine-B7p40M__.js +0 -531
- package/dist/i18next-CrUnKFfh.js +0 -1596
- package/dist/i18next-Rgo-v9l0.js +0 -1
- package/dist/lingui--PcOOYry.js +0 -833
- package/dist/messages-Cxa-5tR0.js +0 -166
- package/dist/migrate-CntqvrW4.js +0 -1
- package/dist/migrate-dPgq9uzy.js +0 -1484
- package/dist/project-Dwx7iPm6.js +0 -381
- package/dist/project-pXipT8iG.js +0 -1046
- package/dist/react-intl-CaBVwJ9u.js +0 -519
- package/dist/restore-Btq66Tid.js +0 -175
- package/dist/rewrite-CiTRAICl.js +0 -803
- package/dist/run-DYD_VXKy.js +0 -694
- package/dist/runtime-DTMmliT1.js +0 -64
- package/dist/sync-CcVzger_.js +0 -658
- package/dist/translate-DZR3MkSA.js +0 -299
- package/dist/types-wSh9fCBY.js +0 -11
- package/dist/vue-i18n-CEMnpv7k.js +0 -651
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as isDiagnosticError, o as ID_LENGTH, t as diagnosticError } from "./diagnostics-1onDF6Ua.js";
|
|
2
|
+
import { isAbsolute, relative, sep } from "node:path";
|
|
2
3
|
import { hash } from "node:crypto";
|
|
3
|
-
|
|
4
|
+
// src/core/icu/skeleton.ts
|
|
4
5
|
const fail = (code, message) => ({
|
|
5
6
|
ok: false,
|
|
6
7
|
code,
|
|
@@ -211,22 +212,13 @@ function applyNumberStem(stem, parts, token, options) {
|
|
|
211
212
|
case "currency": {
|
|
212
213
|
const code = parts[1];
|
|
213
214
|
if (optionCount !== 1 || code === void 0) return fail("icu-unknown-currency", `${token} needs one currency code: write ::currency/EUR (with the code you need)`);
|
|
214
|
-
if (!
|
|
215
|
+
if (!isSupportedCurrency(code)) return fail("icu-unknown-currency", `Unknown currency code "${code}": use an ISO 4217 code such as EUR`);
|
|
215
216
|
options.style = "currency";
|
|
216
217
|
options.currency = code;
|
|
217
218
|
return null;
|
|
218
219
|
}
|
|
219
220
|
case "measure-unit":
|
|
220
|
-
case "unit":
|
|
221
|
-
const written = parts[1];
|
|
222
|
-
if (optionCount !== 1 || written === void 0) return fail("icu-unknown-unit", `${token} needs one unit: write ::unit/kilometer or ::measure-unit/length-meter`);
|
|
223
|
-
const unit = written.replace(/^(.*?)-/, "");
|
|
224
|
-
if (stem === "unit" && unit !== written) return fail("icu-unknown-unit", isSupportedUnit(unit) && !isSupportedUnit(written) ? `${token} has a unit type: write ::unit/${unit} or ::measure-unit/${written}` : `${token} names a hyphenated unit, which ::unit/ cannot express: write ::measure-unit/<type>-${written}, as in ::measure-unit/volume-fluid-ounce or ::measure-unit/speed-kilometer-per-hour`);
|
|
225
|
-
if (!isSupportedUnit(unit)) return fail("icu-unknown-unit", `Unknown unit "${written}": use a unit Intl.NumberFormat supports, such as kilometer`);
|
|
226
|
-
options.style = "unit";
|
|
227
|
-
options.unit = unit;
|
|
228
|
-
return null;
|
|
229
|
-
}
|
|
221
|
+
case "unit": return unitStem(stem, parts, token, options);
|
|
230
222
|
case "scientific":
|
|
231
223
|
case "engineering":
|
|
232
224
|
options.notation = stem;
|
|
@@ -245,6 +237,22 @@ function applyNumberStem(stem, parts, token, options) {
|
|
|
245
237
|
case "scale":
|
|
246
238
|
case "%x100": return unknownStem(token, "scaling is not supported: scale the value before formatting it");
|
|
247
239
|
}
|
|
240
|
+
return applyShapedStem(stem, parts, token, options);
|
|
241
|
+
}
|
|
242
|
+
/** `unit/…` and `measure-unit/…`. */
|
|
243
|
+
function unitStem(stem, parts, token, options) {
|
|
244
|
+
const written = parts[1];
|
|
245
|
+
if (parts.length !== 2 || written === void 0) return fail("icu-unknown-unit", `${token} needs one unit: write ::unit/kilometer or ::measure-unit/length-meter`);
|
|
246
|
+
const unit = written.replace(/^(.*?)-/, "");
|
|
247
|
+
if (stem === "unit" && unit !== written) return fail("icu-unknown-unit", isSupportedUnit(unit) && !isSupportedUnit(written) ? `${token} has a unit type: write ::unit/${unit} or ::measure-unit/${written}` : `${token} names a hyphenated unit, which ::unit/ cannot express: write ::measure-unit/<type>-${written}, as in ::measure-unit/volume-fluid-ounce or ::measure-unit/speed-kilometer-per-hour`);
|
|
248
|
+
if (!isSupportedUnit(unit)) return fail("icu-unknown-unit", `Unknown unit "${written}": use a unit Intl.NumberFormat supports, such as kilometer`);
|
|
249
|
+
options.style = "unit";
|
|
250
|
+
options.unit = unit;
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
/** Stems recognised by their shape: concise integer width, precision, sign, concise notation. */
|
|
254
|
+
function applyShapedStem(stem, parts, token, options) {
|
|
255
|
+
const optionCount = parts.length - 1;
|
|
248
256
|
if (CONCISE_INTEGER_WIDTH.test(stem)) {
|
|
249
257
|
if (optionCount > 0) return unknownStem(token, `${stem} takes no option`);
|
|
250
258
|
options.minimumIntegerDigits = stem.length;
|
|
@@ -253,16 +261,7 @@ function applyNumberStem(stem, parts, token, options) {
|
|
|
253
261
|
const fraction = FRACTION_PRECISION.exec(stem);
|
|
254
262
|
if (fraction !== null) {
|
|
255
263
|
if (optionCount > 1) return unknownStem(token, "fraction precision takes at most one option");
|
|
256
|
-
|
|
257
|
-
if (star === "*") options.minimumFractionDigits = zeros.length;
|
|
258
|
-
else if (hashes !== void 0) options.maximumFractionDigits = hashes.length;
|
|
259
|
-
else if (minZeros !== void 0 && maxHashes !== void 0) {
|
|
260
|
-
options.minimumFractionDigits = minZeros.length;
|
|
261
|
-
options.maximumFractionDigits = minZeros.length + maxHashes.length;
|
|
262
|
-
} else {
|
|
263
|
-
options.minimumFractionDigits = zeros.length;
|
|
264
|
-
options.maximumFractionDigits = zeros.length;
|
|
265
|
-
}
|
|
264
|
+
applyFraction(fraction, options);
|
|
266
265
|
const option = parts[1];
|
|
267
266
|
if (option === "w") options.trailingZeroDisplay = "stripIfInteger";
|
|
268
267
|
else if (option !== void 0 && !applySignificant(option, options)) return unknownStem(token, `"${option}" is neither w nor a significant-digits option such as @@#`);
|
|
@@ -289,11 +288,24 @@ function applyNumberStem(stem, parts, token, options) {
|
|
|
289
288
|
}
|
|
290
289
|
return unknownStem(token, "not a number skeleton stem vi18n supports");
|
|
291
290
|
}
|
|
291
|
+
/** A fraction precision stem's digit counts (`.00`, `.0*`, `.##`, `.0#`). */
|
|
292
|
+
function applyFraction(fraction, options) {
|
|
293
|
+
const [, zeros, star, hashes, minZeros, maxHashes] = fraction;
|
|
294
|
+
if (star === "*") options.minimumFractionDigits = zeros.length;
|
|
295
|
+
else if (hashes !== void 0) options.maximumFractionDigits = hashes.length;
|
|
296
|
+
else if (minZeros !== void 0 && maxHashes !== void 0) {
|
|
297
|
+
options.minimumFractionDigits = minZeros.length;
|
|
298
|
+
options.maximumFractionDigits = minZeros.length + maxHashes.length;
|
|
299
|
+
} else {
|
|
300
|
+
options.minimumFractionDigits = zeros.length;
|
|
301
|
+
options.maximumFractionDigits = zeros.length;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
292
304
|
/** FormatJS `parseSignificantPrecision`, restricted to inputs it does not crash on. */
|
|
293
305
|
function applySignificant(text, options) {
|
|
294
306
|
const match = SIGNIFICANT_PRECISION.exec(text);
|
|
295
307
|
if (match === null) return false;
|
|
296
|
-
const [, ats, extra, priority] = match;
|
|
308
|
+
const [, ats = "", extra, priority] = match;
|
|
297
309
|
if (priority === "r") options.roundingPriority = "morePrecision";
|
|
298
310
|
else if (priority === "s") options.roundingPriority = "lessPrecision";
|
|
299
311
|
if (extra === void 0) {
|
|
@@ -403,6 +415,15 @@ function dateSkeleton(skeleton) {
|
|
|
403
415
|
options: nonEmpty(options)
|
|
404
416
|
};
|
|
405
417
|
}
|
|
418
|
+
/** The width of a text field (era, weekday) by its length: 4 long, 5 narrow, else short. */
|
|
419
|
+
const TEXT_WIDTHS = [
|
|
420
|
+
"short",
|
|
421
|
+
"short",
|
|
422
|
+
"short",
|
|
423
|
+
"long",
|
|
424
|
+
"narrow",
|
|
425
|
+
"short"
|
|
426
|
+
];
|
|
406
427
|
function applyDateField(run, options) {
|
|
407
428
|
const symbol = run[0];
|
|
408
429
|
const length = run.length;
|
|
@@ -413,12 +434,12 @@ function applyDateField(run, options) {
|
|
|
413
434
|
return null;
|
|
414
435
|
};
|
|
415
436
|
switch (symbol) {
|
|
416
|
-
case "G": return set("era", length
|
|
437
|
+
case "G": return set("era", TEXT_WIDTHS[length - 1]);
|
|
417
438
|
case "y": return set("year", length === 2 ? "2-digit" : "numeric");
|
|
418
439
|
case "M":
|
|
419
440
|
case "L": return set("month", MONTH_WIDTHS[length - 1]);
|
|
420
441
|
case "d": return set("day", NUMERIC_2DIGIT[length - 1]);
|
|
421
|
-
case "E": return set("weekday", length
|
|
442
|
+
case "E": return set("weekday", TEXT_WIDTHS[length - 1]);
|
|
422
443
|
case "e":
|
|
423
444
|
case "c":
|
|
424
445
|
if (length < 4) return unknownDate(run, "numeric weekdays are not supported; use EEE or EEEE");
|
|
@@ -462,9 +483,6 @@ function nonEmpty(options) {
|
|
|
462
483
|
}
|
|
463
484
|
let currencies;
|
|
464
485
|
let units;
|
|
465
|
-
function supportedCurrencies() {
|
|
466
|
-
return currencies ??= new Set(Intl.supportedValuesOf("currency"));
|
|
467
|
-
}
|
|
468
486
|
/** A sanctioned unit, or `<unit>-per-<unit>` of two (what Intl.NumberFormat accepts). */
|
|
469
487
|
function isSupportedUnit(unit) {
|
|
470
488
|
units ??= new Set(Intl.supportedValuesOf("unit"));
|
|
@@ -472,8 +490,11 @@ function isSupportedUnit(unit) {
|
|
|
472
490
|
const per = unit.indexOf("-per-");
|
|
473
491
|
return per > 0 && units.has(unit.slice(0, per)) && units.has(unit.slice(per + 5));
|
|
474
492
|
}
|
|
475
|
-
|
|
476
|
-
|
|
493
|
+
function isSupportedCurrency(code) {
|
|
494
|
+
currencies ??= new Set(Intl.supportedValuesOf("currency"));
|
|
495
|
+
return currencies.has(code);
|
|
496
|
+
}
|
|
497
|
+
// src/core/icu/parse.ts
|
|
477
498
|
/**
|
|
478
499
|
* ICU MessageFormat 1 in FormatJS's dialect (INTERNALS §2.1), written as one
|
|
479
500
|
* recursive-descent pass over UTF-16 code units: every syntax character is
|
|
@@ -488,18 +509,18 @@ function isSupportedUnit(unit) {
|
|
|
488
509
|
const TAB$1 = 9;
|
|
489
510
|
const CR$2 = 13;
|
|
490
511
|
const SPACE$1 = 32;
|
|
491
|
-
const HASH$
|
|
492
|
-
const APOS = 39;
|
|
512
|
+
const HASH$2 = 35;
|
|
513
|
+
const APOS$1 = 39;
|
|
493
514
|
const PLUS$1 = 43;
|
|
494
515
|
const COMMA = 44;
|
|
495
516
|
const MINUS$1 = 45;
|
|
496
|
-
const SLASH$
|
|
517
|
+
const SLASH$2 = 47;
|
|
497
518
|
const COLON$1 = 58;
|
|
498
|
-
const LT = 60;
|
|
519
|
+
const LT$1 = 60;
|
|
499
520
|
const EQUALS$1 = 61;
|
|
500
|
-
const GT = 62;
|
|
501
|
-
const LBRACE$
|
|
502
|
-
const RBRACE$
|
|
521
|
+
const GT$1 = 62;
|
|
522
|
+
const LBRACE$2 = 123;
|
|
523
|
+
const RBRACE$2 = 125;
|
|
503
524
|
/** `[^\p{White_Space}\p{Pattern_Syntax}]*`: FormatJS's argument and selector names. */
|
|
504
525
|
const IDENTIFIER = /[^\p{White_Space}\p{Pattern_Syntax}]*/uy;
|
|
505
526
|
const UNSUPPORTED_TYPES = /* @__PURE__ */ new Set([
|
|
@@ -550,6 +571,18 @@ function positionAt(source, offset) {
|
|
|
550
571
|
column: end - lineStart + 1
|
|
551
572
|
};
|
|
552
573
|
}
|
|
574
|
+
/** An apostrophe before `next` opens a quoted section (ICU's ApostropheMode DOUBLE_OPTIONAL). */
|
|
575
|
+
function opensQuote(next, poundSensitive) {
|
|
576
|
+
return next === LBRACE$2 || next === RBRACE$2 || next === LT$1 || next === GT$1 || next === HASH$2 && poundSensitive;
|
|
577
|
+
}
|
|
578
|
+
/** The apostrophe closing a quoted section whose text starts before `from` (`''` is one apostrophe in it), or the end. */
|
|
579
|
+
function closingQuote(src, from) {
|
|
580
|
+
let pos = from;
|
|
581
|
+
while (pos < src.length) if (src.charCodeAt(pos) !== APOS$1) pos++;
|
|
582
|
+
else if (src.charCodeAt(pos + 1) === APOS$1) pos += 2;
|
|
583
|
+
else return pos;
|
|
584
|
+
return src.length;
|
|
585
|
+
}
|
|
553
586
|
var Parser$1 = class {
|
|
554
587
|
src;
|
|
555
588
|
locations;
|
|
@@ -575,36 +608,23 @@ var Parser$1 = class {
|
|
|
575
608
|
const src = this.src;
|
|
576
609
|
while (this.pos < src.length) {
|
|
577
610
|
const c = src.charCodeAt(this.pos);
|
|
578
|
-
if (c === LBRACE$
|
|
579
|
-
else if (c === RBRACE$
|
|
580
|
-
else if (c === HASH$
|
|
611
|
+
if (c === LBRACE$2) nodes.push(this.parseArgument(depth, poundSensitive, inTag));
|
|
612
|
+
else if (c === RBRACE$2 && depth > 0) break;
|
|
613
|
+
else if (c === HASH$2 && poundSensitive) {
|
|
581
614
|
if (!pluralParent) throw this.error("icu-pound-nested", "# is ambiguous inside a select nested in a plural: write the plural's argument (as in {count}) for the number, or '#' for a literal #", this.pos);
|
|
582
615
|
const start = this.pos++;
|
|
583
|
-
nodes.push(this.
|
|
584
|
-
|
|
585
|
-
loc: {
|
|
586
|
-
start,
|
|
587
|
-
end: this.pos
|
|
588
|
-
}
|
|
589
|
-
} : { type: "pound" });
|
|
590
|
-
} else if (c === LT && src.charCodeAt(this.pos + 1) === SLASH$1) {
|
|
616
|
+
nodes.push(this.located({ type: "pound" }, start));
|
|
617
|
+
} else if (c === LT$1 && src.charCodeAt(this.pos + 1) === SLASH$2) {
|
|
591
618
|
if (inTag) break;
|
|
592
619
|
throw this.error("icu-syntax", "Closing tag without a matching opening tag", this.pos);
|
|
593
|
-
} else if (c === LT && isAsciiLetter(src.charCodeAt(this.pos + 1))) nodes.push(this.parseTag(depth, pluralParent, poundSensitive));
|
|
620
|
+
} else if (c === LT$1 && isAsciiLetter(src.charCodeAt(this.pos + 1))) nodes.push(this.parseTag(depth, pluralParent, poundSensitive));
|
|
594
621
|
else {
|
|
595
622
|
const start = this.pos;
|
|
596
623
|
const value = this.parseLiteral(depth, poundSensitive);
|
|
597
|
-
nodes.push(this.
|
|
598
|
-
type: "literal",
|
|
599
|
-
value,
|
|
600
|
-
loc: {
|
|
601
|
-
start,
|
|
602
|
-
end: this.pos
|
|
603
|
-
}
|
|
604
|
-
} : {
|
|
624
|
+
nodes.push(this.located({
|
|
605
625
|
type: "literal",
|
|
606
626
|
value
|
|
607
|
-
});
|
|
627
|
+
}, start));
|
|
608
628
|
}
|
|
609
629
|
}
|
|
610
630
|
return nodes;
|
|
@@ -618,48 +638,26 @@ var Parser$1 = class {
|
|
|
618
638
|
let run = pos;
|
|
619
639
|
while (pos < length) {
|
|
620
640
|
const c = src.charCodeAt(pos);
|
|
621
|
-
if (c === APOS) {
|
|
641
|
+
if (c === APOS$1) {
|
|
622
642
|
const next = src.charCodeAt(pos + 1);
|
|
623
|
-
if (next === APOS) {
|
|
643
|
+
if (next === APOS$1) {
|
|
624
644
|
value += src.slice(run, pos + 1);
|
|
625
645
|
pos += 2;
|
|
626
646
|
run = pos;
|
|
627
|
-
|
|
628
|
-
}
|
|
629
|
-
if (next === LBRACE$1 || next === RBRACE$1 || next === LT || next === GT || next === HASH$1 && poundSensitive) {
|
|
647
|
+
} else if (opensQuote(next, poundSensitive)) {
|
|
630
648
|
this.quotes?.push(pos);
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
pos
|
|
634
|
-
for (;;) {
|
|
635
|
-
if (pos >= length) {
|
|
636
|
-
value += src.slice(quoted, pos);
|
|
637
|
-
break;
|
|
638
|
-
}
|
|
639
|
-
if (src.charCodeAt(pos) === APOS) {
|
|
640
|
-
if (src.charCodeAt(pos + 1) === APOS) {
|
|
641
|
-
value += src.slice(quoted, pos + 1);
|
|
642
|
-
pos += 2;
|
|
643
|
-
quoted = pos;
|
|
644
|
-
continue;
|
|
645
|
-
}
|
|
646
|
-
value += src.slice(quoted, pos);
|
|
647
|
-
pos++;
|
|
648
|
-
break;
|
|
649
|
-
}
|
|
650
|
-
pos++;
|
|
651
|
-
}
|
|
649
|
+
const close = closingQuote(src, pos + 2);
|
|
650
|
+
value += src.slice(run, pos) + src.slice(pos + 1, close).replaceAll("''", "'");
|
|
651
|
+
pos = close < length ? close + 1 : close;
|
|
652
652
|
run = pos;
|
|
653
|
-
|
|
654
|
-
}
|
|
655
|
-
pos++;
|
|
653
|
+
} else pos++;
|
|
656
654
|
continue;
|
|
657
655
|
}
|
|
658
|
-
if (c === LBRACE$
|
|
659
|
-
if (c === LT) {
|
|
656
|
+
if (c === LBRACE$2) break;
|
|
657
|
+
if (c === LT$1) {
|
|
660
658
|
const next = src.charCodeAt(pos + 1);
|
|
661
|
-
if (next === SLASH$
|
|
662
|
-
} else if (c === HASH$
|
|
659
|
+
if (next === SLASH$2 || isAsciiLetter(next)) break;
|
|
660
|
+
} else if (c === HASH$2 && poundSensitive || c === RBRACE$2 && depth > 0) break;
|
|
663
661
|
pos++;
|
|
664
662
|
}
|
|
665
663
|
this.pos = pos;
|
|
@@ -672,21 +670,13 @@ var Parser$1 = class {
|
|
|
672
670
|
this.skipSpace();
|
|
673
671
|
if (this.src.startsWith("/>", this.pos)) {
|
|
674
672
|
this.pos += 2;
|
|
675
|
-
return this.
|
|
676
|
-
type: "tag",
|
|
677
|
-
name,
|
|
678
|
-
children: null,
|
|
679
|
-
loc: {
|
|
680
|
-
start,
|
|
681
|
-
end: this.pos
|
|
682
|
-
}
|
|
683
|
-
} : {
|
|
673
|
+
return this.located({
|
|
684
674
|
type: "tag",
|
|
685
675
|
name,
|
|
686
676
|
children: null
|
|
687
|
-
};
|
|
677
|
+
}, start);
|
|
688
678
|
}
|
|
689
|
-
if (this.src.charCodeAt(this.pos) !== GT) throw this.error("icu-syntax", `Malformed tag <${name}: tags have no attributes`, start);
|
|
679
|
+
if (this.src.charCodeAt(this.pos) !== GT$1) throw this.error("icu-syntax", `Malformed tag <${name}: tags have no attributes`, start);
|
|
690
680
|
this.pos++;
|
|
691
681
|
const children = this.parseMessage(depth + 1, pluralParent, poundSensitive, true);
|
|
692
682
|
const closeStart = this.pos;
|
|
@@ -696,21 +686,13 @@ var Parser$1 = class {
|
|
|
696
686
|
const closeName = this.parseTagName();
|
|
697
687
|
if (closeName !== name) throw this.error("icu-syntax", `Closing tag </${closeName}> does not match <${name}>`, closeStart);
|
|
698
688
|
this.skipSpace();
|
|
699
|
-
if (this.src.charCodeAt(this.pos) !== GT) throw this.error("icu-syntax", `Malformed closing tag for <${name}>`, closeStart);
|
|
689
|
+
if (this.src.charCodeAt(this.pos) !== GT$1) throw this.error("icu-syntax", `Malformed closing tag for <${name}>`, closeStart);
|
|
700
690
|
this.pos++;
|
|
701
|
-
return this.
|
|
702
|
-
type: "tag",
|
|
703
|
-
name,
|
|
704
|
-
children,
|
|
705
|
-
loc: {
|
|
706
|
-
start,
|
|
707
|
-
end: this.pos
|
|
708
|
-
}
|
|
709
|
-
} : {
|
|
691
|
+
return this.located({
|
|
710
692
|
type: "tag",
|
|
711
693
|
name,
|
|
712
694
|
children
|
|
713
|
-
};
|
|
695
|
+
}, start);
|
|
714
696
|
}
|
|
715
697
|
/** Assumes the current character is an ASCII letter (checked by the caller). */
|
|
716
698
|
parseTagName() {
|
|
@@ -728,25 +710,18 @@ var Parser$1 = class {
|
|
|
728
710
|
const open = this.pos++;
|
|
729
711
|
this.skipSpace();
|
|
730
712
|
if (this.pos >= src.length) throw this.unclosed(open);
|
|
731
|
-
if (src.charCodeAt(this.pos) === RBRACE$
|
|
713
|
+
if (src.charCodeAt(this.pos) === RBRACE$2) throw this.error("icu-syntax", "Empty argument {}: name it, as in {name}", open);
|
|
732
714
|
const name = this.identifier();
|
|
733
715
|
if (name === "") throw this.malformed(open);
|
|
734
716
|
this.skipSpace();
|
|
735
717
|
if (this.pos >= src.length) throw this.unclosed(open);
|
|
736
718
|
const c = src.charCodeAt(this.pos);
|
|
737
|
-
if (c === RBRACE$
|
|
719
|
+
if (c === RBRACE$2) {
|
|
738
720
|
this.pos++;
|
|
739
|
-
return this.
|
|
740
|
-
type: "argument",
|
|
741
|
-
name,
|
|
742
|
-
loc: {
|
|
743
|
-
start: open,
|
|
744
|
-
end: this.pos
|
|
745
|
-
}
|
|
746
|
-
} : {
|
|
721
|
+
return this.located({
|
|
747
722
|
type: "argument",
|
|
748
723
|
name
|
|
749
|
-
};
|
|
724
|
+
}, open);
|
|
750
725
|
}
|
|
751
726
|
if (c !== COMMA) throw this.malformed(open);
|
|
752
727
|
this.pos++;
|
|
@@ -760,7 +735,11 @@ var Parser$1 = class {
|
|
|
760
735
|
case "time": return this.parseFormatted(type, name, open);
|
|
761
736
|
case "select":
|
|
762
737
|
case "plural":
|
|
763
|
-
case "selectordinal": return this.parseChoice(
|
|
738
|
+
case "selectordinal": return this.parseChoice({
|
|
739
|
+
type,
|
|
740
|
+
name,
|
|
741
|
+
open
|
|
742
|
+
}, depth, poundSensitive, inTag);
|
|
764
743
|
case "": throw this.error("icu-syntax", `Expected an argument type after {${name},`, typeStart);
|
|
765
744
|
default:
|
|
766
745
|
if (UNSUPPORTED_TYPES.has(type)) throw this.error("icu-unsupported-type", type === "choice" ? "{…, choice} is not supported: use {…, plural} or {…, select}" : `{…, ${type}} is not supported because Intl cannot render it: use {…, number}, {…, plural} or {…, selectordinal}`, typeStart);
|
|
@@ -779,7 +758,7 @@ var Parser$1 = class {
|
|
|
779
758
|
style = this.simpleStyle().trimEnd();
|
|
780
759
|
if (style.length === 0) throw this.error("icu-syntax", `Expected a style after {${name}, ${type},`, this.pos);
|
|
781
760
|
}
|
|
782
|
-
if (src.charCodeAt(this.pos) !== RBRACE$
|
|
761
|
+
if (src.charCodeAt(this.pos) !== RBRACE$2) throw this.unclosed(open);
|
|
783
762
|
this.pos++;
|
|
784
763
|
const resolved = resolveStyle(type, style);
|
|
785
764
|
if (!resolved.ok) throw this.error(resolved.code, resolved.message, styleStart);
|
|
@@ -789,11 +768,7 @@ var Parser$1 = class {
|
|
|
789
768
|
style: resolved.style,
|
|
790
769
|
options: resolved.options
|
|
791
770
|
};
|
|
792
|
-
|
|
793
|
-
start: open,
|
|
794
|
-
end: this.pos
|
|
795
|
-
};
|
|
796
|
-
return node;
|
|
771
|
+
return this.located(node, open);
|
|
797
772
|
}
|
|
798
773
|
/** ICU `MessagePattern` simple style: up to the unbalanced `}`, quotes kept. */
|
|
799
774
|
simpleStyle() {
|
|
@@ -802,14 +777,14 @@ var Parser$1 = class {
|
|
|
802
777
|
let nested = 0;
|
|
803
778
|
while (this.pos < src.length) {
|
|
804
779
|
const c = src.charCodeAt(this.pos);
|
|
805
|
-
if (c === APOS) {
|
|
780
|
+
if (c === APOS$1) {
|
|
806
781
|
const close = src.indexOf("'", this.pos + 1);
|
|
807
782
|
if (close === -1) throw this.error("icu-syntax", "Unclosed quote in the argument style", this.pos);
|
|
808
783
|
this.pos = close + 1;
|
|
809
|
-
} else if (c === LBRACE$
|
|
784
|
+
} else if (c === LBRACE$2) {
|
|
810
785
|
nested++;
|
|
811
786
|
this.pos++;
|
|
812
|
-
} else if (c === RBRACE$
|
|
787
|
+
} else if (c === RBRACE$2) {
|
|
813
788
|
if (nested === 0) break;
|
|
814
789
|
nested--;
|
|
815
790
|
this.pos++;
|
|
@@ -817,7 +792,7 @@ var Parser$1 = class {
|
|
|
817
792
|
}
|
|
818
793
|
return src.slice(start, this.pos);
|
|
819
794
|
}
|
|
820
|
-
parseChoice(type, name, open, depth, poundSensitive, inTag) {
|
|
795
|
+
parseChoice({ type, name, open }, depth, poundSensitive, inTag) {
|
|
821
796
|
const src = this.src;
|
|
822
797
|
this.skipSpace();
|
|
823
798
|
if (src.charCodeAt(this.pos) !== COMMA) throw this.error("icu-syntax", `Expected options after {${name}, ${type}`, this.pos);
|
|
@@ -849,52 +824,41 @@ var Parser$1 = class {
|
|
|
849
824
|
if (keys.has(key)) throw this.error("icu-syntax", `Duplicate option "${key}" in {${name}, ${type}}`, keyStart);
|
|
850
825
|
keys.add(key);
|
|
851
826
|
this.skipSpace();
|
|
852
|
-
if (src.charCodeAt(this.pos) !== LBRACE$
|
|
827
|
+
if (src.charCodeAt(this.pos) !== LBRACE$2) throw this.error("icu-syntax", `Expected {…} after option "${key}" in {${name}, ${type}}`, this.pos);
|
|
853
828
|
const valueOpen = this.pos++;
|
|
854
829
|
const value = this.parseMessage(depth + 1, plural, childPoundSensitive, inTag);
|
|
855
|
-
if (src.charCodeAt(this.pos) !== RBRACE$
|
|
830
|
+
if (src.charCodeAt(this.pos) !== RBRACE$2) throw this.unclosed(valueOpen);
|
|
856
831
|
this.pos++;
|
|
857
|
-
options.push(this.
|
|
858
|
-
key,
|
|
859
|
-
value,
|
|
860
|
-
loc: {
|
|
861
|
-
start: keyStart,
|
|
862
|
-
end: this.pos
|
|
863
|
-
}
|
|
864
|
-
} : {
|
|
832
|
+
options.push(this.located({
|
|
865
833
|
key,
|
|
866
834
|
value
|
|
867
|
-
});
|
|
835
|
+
}, keyStart));
|
|
868
836
|
this.skipSpace();
|
|
869
837
|
keyStart = this.pos;
|
|
870
838
|
selector = this.identifier();
|
|
871
839
|
}
|
|
872
840
|
if (options.length === 0) throw this.error("icu-syntax", `Expected at least one option in {${name}, ${type}}`, this.pos);
|
|
873
841
|
if (!keys.has("other")) throw this.error("icu-missing-other", `{${name}, ${type}} needs an other {…} option`, open);
|
|
874
|
-
if (src.charCodeAt(this.pos) !== RBRACE$
|
|
842
|
+
if (src.charCodeAt(this.pos) !== RBRACE$2) throw this.unclosed(open);
|
|
875
843
|
this.pos++;
|
|
876
|
-
if (type === "select") return this.
|
|
877
|
-
type: "select",
|
|
878
|
-
name,
|
|
879
|
-
options,
|
|
880
|
-
loc: {
|
|
881
|
-
start: open,
|
|
882
|
-
end: this.pos
|
|
883
|
-
}
|
|
884
|
-
} : {
|
|
844
|
+
if (type === "select") return this.located({
|
|
885
845
|
type: "select",
|
|
886
846
|
name,
|
|
887
847
|
options
|
|
888
|
-
};
|
|
889
|
-
const
|
|
848
|
+
}, open);
|
|
849
|
+
const pluralType = type === "plural" ? "cardinal" : "ordinal";
|
|
850
|
+
return this.located({
|
|
890
851
|
type: "plural",
|
|
891
|
-
pluralType
|
|
852
|
+
pluralType,
|
|
892
853
|
name,
|
|
893
854
|
offset,
|
|
894
855
|
options
|
|
895
|
-
};
|
|
856
|
+
}, open);
|
|
857
|
+
}
|
|
858
|
+
/** `node` with its source range, from `start` to here, when locations are on. */
|
|
859
|
+
located(node, start) {
|
|
896
860
|
if (this.locations) node.loc = {
|
|
897
|
-
start
|
|
861
|
+
start,
|
|
898
862
|
end: this.pos
|
|
899
863
|
};
|
|
900
864
|
return node;
|
|
@@ -958,12 +922,46 @@ var Parser$1 = class {
|
|
|
958
922
|
function isAsciiLetter(c) {
|
|
959
923
|
return c >= 65 && c <= 90 || c >= 97 && c <= 122;
|
|
960
924
|
}
|
|
925
|
+
/** Whether code point `c` falls in one of `ranges`, a flat list of inclusive `[low, high]` pairs. */
|
|
926
|
+
function inRanges(c, ranges) {
|
|
927
|
+
for (let i = 0; i < ranges.length; i += 2) if (c >= ranges[i] && c <= ranges[i + 1]) return true;
|
|
928
|
+
return false;
|
|
929
|
+
}
|
|
930
|
+
/** XML's NameChar beyond ASCII, as inclusive code point pairs. */
|
|
931
|
+
const TAG_NAME_RANGES = [
|
|
932
|
+
183,
|
|
933
|
+
183,
|
|
934
|
+
192,
|
|
935
|
+
214,
|
|
936
|
+
216,
|
|
937
|
+
246,
|
|
938
|
+
248,
|
|
939
|
+
893,
|
|
940
|
+
895,
|
|
941
|
+
8191,
|
|
942
|
+
8204,
|
|
943
|
+
8205,
|
|
944
|
+
8255,
|
|
945
|
+
8256,
|
|
946
|
+
8304,
|
|
947
|
+
8591,
|
|
948
|
+
11264,
|
|
949
|
+
12271,
|
|
950
|
+
12289,
|
|
951
|
+
55295,
|
|
952
|
+
63744,
|
|
953
|
+
64975,
|
|
954
|
+
65008,
|
|
955
|
+
65533,
|
|
956
|
+
65536,
|
|
957
|
+
983039
|
|
958
|
+
];
|
|
961
959
|
/** FormatJS `_isPotentialElementNameChar` (custom-element name characters); MF2 markup names are checked with it too. */
|
|
962
960
|
function isTagNameChar(c) {
|
|
963
|
-
|
|
961
|
+
if (c < 128) return c >= 97 && c <= 122 || c >= 65 && c <= 90 || c >= 48 && c <= 57 || c === 45 || c === 46 || c === 95;
|
|
962
|
+
return inRanges(c, TAG_NAME_RANGES);
|
|
964
963
|
}
|
|
965
|
-
|
|
966
|
-
//#region src/core/mf2/syntax.ts
|
|
964
|
+
// src/core/mf2/syntax.ts
|
|
967
965
|
/**
|
|
968
966
|
* MessageFormat 2 syntax (Unicode LDML part 9, `message.abnf`) read into its
|
|
969
967
|
* data model, with the spec's data-model errors (PLAN §4.2, INTERNALS §2.1.4).
|
|
@@ -979,29 +977,56 @@ const TAB = 9;
|
|
|
979
977
|
const LF = 10;
|
|
980
978
|
const CR$1 = 13;
|
|
981
979
|
const SPACE = 32;
|
|
982
|
-
const HASH = 35;
|
|
980
|
+
const HASH$1 = 35;
|
|
983
981
|
const DOLLAR = 36;
|
|
984
982
|
const PLUS = 43;
|
|
985
983
|
const MINUS = 45;
|
|
986
984
|
const DOT = 46;
|
|
987
|
-
const SLASH = 47;
|
|
985
|
+
const SLASH$1 = 47;
|
|
988
986
|
const COLON = 58;
|
|
989
987
|
const EQUALS = 61;
|
|
990
988
|
const AT = 64;
|
|
991
989
|
const BACKSLASH = 92;
|
|
992
990
|
const UNDERSCORE = 95;
|
|
993
|
-
const LBRACE = 123;
|
|
991
|
+
const LBRACE$1 = 123;
|
|
994
992
|
const PIPE = 124;
|
|
995
|
-
const RBRACE = 125;
|
|
993
|
+
const RBRACE$1 = 125;
|
|
996
994
|
const IDEOGRAPHIC_SPACE = 12288;
|
|
997
995
|
/** `ws`: SP, HTAB, CR, LF, U+3000. */
|
|
998
996
|
const isWs = (c) => c === SPACE || c === TAB || c === LF || c === CR$1 || c === IDEOGRAPHIC_SPACE;
|
|
999
997
|
/** `bidi`: ALM, LRM, RLM and the four isolates. */
|
|
1000
998
|
const isBidi = (c) => c === 1564 || c === 8206 || c === 8207 || c >= 8294 && c <= 8297;
|
|
999
|
+
/** `name-start` beyond U+00A0, as inclusive code point pairs. */
|
|
1000
|
+
const NAME_START_RANGES = [
|
|
1001
|
+
161,
|
|
1002
|
+
1563,
|
|
1003
|
+
1565,
|
|
1004
|
+
5759,
|
|
1005
|
+
5761,
|
|
1006
|
+
8191,
|
|
1007
|
+
8203,
|
|
1008
|
+
8205,
|
|
1009
|
+
8208,
|
|
1010
|
+
8231,
|
|
1011
|
+
8240,
|
|
1012
|
+
8286,
|
|
1013
|
+
8288,
|
|
1014
|
+
8293,
|
|
1015
|
+
8298,
|
|
1016
|
+
12287,
|
|
1017
|
+
12289,
|
|
1018
|
+
55295,
|
|
1019
|
+
57344,
|
|
1020
|
+
64975,
|
|
1021
|
+
65008,
|
|
1022
|
+
65533,
|
|
1023
|
+
65536,
|
|
1024
|
+
1114109
|
|
1025
|
+
];
|
|
1001
1026
|
/** `name-start`, by code point. */
|
|
1002
1027
|
function isNameStart(c) {
|
|
1003
1028
|
if (c < 161) return c >= 65 && c <= 90 || c >= 97 && c <= 122 || c === PLUS || c === UNDERSCORE;
|
|
1004
|
-
return (c
|
|
1029
|
+
return inRanges(c, NAME_START_RANGES) && (c & 65534) !== 65534;
|
|
1005
1030
|
}
|
|
1006
1031
|
const isNameChar = (c) => isNameStart(c) || c >= 48 && c <= 57 || c === MINUS || c === DOT;
|
|
1007
1032
|
/**
|
|
@@ -1054,7 +1079,7 @@ var Parser = class {
|
|
|
1054
1079
|
const at = this.pos;
|
|
1055
1080
|
if (this.keyword(".input")) {
|
|
1056
1081
|
this.optional();
|
|
1057
|
-
if (!this.at(LBRACE)) throw this.fail("expected {$variable …} after .input");
|
|
1082
|
+
if (!this.at(LBRACE$1)) throw this.fail("expected {$variable …} after .input");
|
|
1058
1083
|
const value = this.expression();
|
|
1059
1084
|
if (value.operand?.type !== "variable") throw this.fail(".input declares a variable: write .input {$name …}", value.at);
|
|
1060
1085
|
declarations.push({
|
|
@@ -1071,7 +1096,7 @@ var Parser = class {
|
|
|
1071
1096
|
if (!this.at(EQUALS)) throw this.fail(`expected = after .local $${name}`);
|
|
1072
1097
|
this.pos++;
|
|
1073
1098
|
this.optional();
|
|
1074
|
-
if (!this.at(LBRACE)) throw this.fail(`expected {…} after .local $${name} =`);
|
|
1099
|
+
if (!this.at(LBRACE$1)) throw this.fail(`expected {…} after .local $${name} =`);
|
|
1075
1100
|
declarations.push({
|
|
1076
1101
|
kind: "local",
|
|
1077
1102
|
name,
|
|
@@ -1161,14 +1186,14 @@ var Parser = class {
|
|
|
1161
1186
|
if (c === BACKSLASH) {
|
|
1162
1187
|
text += src.slice(run, this.pos) + this.escape();
|
|
1163
1188
|
run = this.pos;
|
|
1164
|
-
} else if (c === LBRACE) {
|
|
1189
|
+
} else if (c === LBRACE$1) {
|
|
1165
1190
|
text += src.slice(run, this.pos);
|
|
1166
1191
|
if (text !== "") parts.push(text);
|
|
1167
1192
|
text = "";
|
|
1168
1193
|
parts.push(this.placeholder());
|
|
1169
1194
|
run = this.pos;
|
|
1170
|
-
} else if (c === RBRACE) {
|
|
1171
|
-
if (!quoted || src.charCodeAt(this.pos + 1) !== RBRACE) throw this.fail("} must be escaped as \\} in text");
|
|
1195
|
+
} else if (c === RBRACE$1) {
|
|
1196
|
+
if (!quoted || src.charCodeAt(this.pos + 1) !== RBRACE$1) throw this.fail("} must be escaped as \\} in text");
|
|
1172
1197
|
text += src.slice(run, this.pos);
|
|
1173
1198
|
this.pos += 2;
|
|
1174
1199
|
break;
|
|
@@ -1190,7 +1215,7 @@ var Parser = class {
|
|
|
1190
1215
|
}
|
|
1191
1216
|
escape() {
|
|
1192
1217
|
const next = this.src.charCodeAt(this.pos + 1);
|
|
1193
|
-
if (next === BACKSLASH || next === LBRACE || next === PIPE || next === RBRACE) {
|
|
1218
|
+
if (next === BACKSLASH || next === LBRACE$1 || next === PIPE || next === RBRACE$1) {
|
|
1194
1219
|
this.pos += 2;
|
|
1195
1220
|
return String.fromCharCode(next);
|
|
1196
1221
|
}
|
|
@@ -1200,7 +1225,7 @@ var Parser = class {
|
|
|
1200
1225
|
const at = this.pos++;
|
|
1201
1226
|
this.optional();
|
|
1202
1227
|
const c = this.src.charCodeAt(this.pos);
|
|
1203
|
-
if (c === HASH || c === SLASH) return this.markup(at, c === HASH);
|
|
1228
|
+
if (c === HASH$1 || c === SLASH$1) return this.markup(at, c === HASH$1);
|
|
1204
1229
|
this.pos = at;
|
|
1205
1230
|
return this.expression();
|
|
1206
1231
|
}
|
|
@@ -1217,12 +1242,12 @@ var Parser = class {
|
|
|
1217
1242
|
};
|
|
1218
1243
|
else if (c === COLON) fn = this.fn();
|
|
1219
1244
|
else if (c === PIPE || isNameChar(this.codePoint())) operand = this.literal();
|
|
1220
|
-
else if (c === HASH || c === SLASH) throw this.fail("markup cannot be used here");
|
|
1245
|
+
else if (c === HASH$1 || c === SLASH$1) throw this.fail("markup cannot be used here");
|
|
1221
1246
|
else throw this.fail("expected a value, $variable or :function in {…}");
|
|
1222
1247
|
let attributes = false;
|
|
1223
1248
|
for (;;) {
|
|
1224
1249
|
const spaced = this.optional();
|
|
1225
|
-
if (this.at(RBRACE)) {
|
|
1250
|
+
if (this.at(RBRACE$1)) {
|
|
1226
1251
|
this.pos++;
|
|
1227
1252
|
return {
|
|
1228
1253
|
type: "expression",
|
|
@@ -1246,10 +1271,10 @@ var Parser = class {
|
|
|
1246
1271
|
let kind = open ? "open" : "close";
|
|
1247
1272
|
for (;;) {
|
|
1248
1273
|
const spaced = this.optional();
|
|
1249
|
-
if (this.at(RBRACE)) break;
|
|
1250
|
-
if (open && this.at(SLASH)) {
|
|
1274
|
+
if (this.at(RBRACE$1)) break;
|
|
1275
|
+
if (open && this.at(SLASH$1)) {
|
|
1251
1276
|
this.pos++;
|
|
1252
|
-
if (!this.at(RBRACE)) throw this.fail("expected } after /");
|
|
1277
|
+
if (!this.at(RBRACE$1)) throw this.fail("expected } after /");
|
|
1253
1278
|
kind = "standalone";
|
|
1254
1279
|
break;
|
|
1255
1280
|
}
|
|
@@ -1452,8 +1477,7 @@ function annotated(name, declared, visiting) {
|
|
|
1452
1477
|
visiting.add(name);
|
|
1453
1478
|
return operand?.type === "variable" && declaration.kind === "local" ? annotated(operand.name, declared, visiting) : false;
|
|
1454
1479
|
}
|
|
1455
|
-
|
|
1456
|
-
//#region src/core/mf2/lower.ts
|
|
1480
|
+
// src/core/mf2/lower.ts
|
|
1457
1481
|
/**
|
|
1458
1482
|
* MF2 data model → the ICU AST (INTERNALS §2.1.4): the IR, ids, catalogs,
|
|
1459
1483
|
* validation and pseudo-locales read MF2 messages exactly as they read ICU
|
|
@@ -1489,6 +1513,22 @@ const NUMERIC = /* @__PURE__ */ new Set([
|
|
|
1489
1513
|
"percent",
|
|
1490
1514
|
"currency"
|
|
1491
1515
|
]);
|
|
1516
|
+
function isKnownFn(name) {
|
|
1517
|
+
return KNOWN.has(name);
|
|
1518
|
+
}
|
|
1519
|
+
function isNumeric(name) {
|
|
1520
|
+
return NUMERIC.has(name);
|
|
1521
|
+
}
|
|
1522
|
+
/** The `select` modes of `:number` and `:integer`. */
|
|
1523
|
+
const SELECT_MODES = /* @__PURE__ */ new Set([
|
|
1524
|
+
"plural",
|
|
1525
|
+
"ordinal",
|
|
1526
|
+
"exact"
|
|
1527
|
+
]);
|
|
1528
|
+
function isSelectMode(select) {
|
|
1529
|
+
return SELECT_MODES.has(select);
|
|
1530
|
+
}
|
|
1531
|
+
const selectError = (select) => `select=${select}: use plural, ordinal or exact`;
|
|
1492
1532
|
/** `[^\p{White_Space}\p{Pattern_Syntax}]+`: ICU argument names and select keys. */
|
|
1493
1533
|
const ICU_IDENTIFIER = /^[^\p{White_Space}\p{Pattern_Syntax}]+$/u;
|
|
1494
1534
|
/** MF2's `number-literal`. */
|
|
@@ -1553,9 +1593,13 @@ var Lowering = class {
|
|
|
1553
1593
|
}
|
|
1554
1594
|
resolve(expression) {
|
|
1555
1595
|
const { operand, fn, at } = expression;
|
|
1556
|
-
|
|
1596
|
+
const name = fn === null ? null : fn.name;
|
|
1597
|
+
if (name !== null && !isKnownFn(name)) {
|
|
1598
|
+
const removed = name === "offset" || name === "math";
|
|
1599
|
+
throw this.fail(removed ? `:${name} is not supported: pass the adjusted number as a value` : `unknown function :${name}: use :string, :number, :integer, :percent, :currency, :date, :time or :datetime`, at, removed ? "icu-unsupported-type" : "icu-syntax");
|
|
1600
|
+
}
|
|
1557
1601
|
let base;
|
|
1558
|
-
if (operand === null) throw this.fail(`{:${
|
|
1602
|
+
if (operand === null) throw this.fail(`{:${name}} has no operand: write {$name :${name}}`, at);
|
|
1559
1603
|
else if (operand.type === "literal") base = {
|
|
1560
1604
|
kind: "text",
|
|
1561
1605
|
text: operand.value
|
|
@@ -1565,8 +1609,7 @@ var Lowering = class {
|
|
|
1565
1609
|
name: operand.name,
|
|
1566
1610
|
fn: null
|
|
1567
1611
|
};
|
|
1568
|
-
if (fn === null) return base;
|
|
1569
|
-
const name = fn.name;
|
|
1612
|
+
if (fn === null || name === null) return base;
|
|
1570
1613
|
const own = this.options(name, fn.options, at);
|
|
1571
1614
|
if (name === "string") {
|
|
1572
1615
|
if (base.kind === "arg") return {
|
|
@@ -1579,29 +1622,7 @@ var Lowering = class {
|
|
|
1579
1622
|
text: base.text
|
|
1580
1623
|
};
|
|
1581
1624
|
}
|
|
1582
|
-
if (
|
|
1583
|
-
const merged = inherit(name, base.kind === "text" ? null : base.fn, own);
|
|
1584
|
-
if (name === "currency" && !merged.options.has("currency")) throw this.fail(":currency needs currency=EUR (the ISO 4217 code)", at);
|
|
1585
|
-
this.style(merged, at);
|
|
1586
|
-
if (base.kind === "text") {
|
|
1587
|
-
if (!NUMBER_LITERAL.test(base.text)) throw this.fail(`${JSON.stringify(base.text)} is not a number for :${name}`, at);
|
|
1588
|
-
return {
|
|
1589
|
-
kind: "number",
|
|
1590
|
-
text: base.text,
|
|
1591
|
-
fn: merged
|
|
1592
|
-
};
|
|
1593
|
-
}
|
|
1594
|
-
if (base.kind === "number") return {
|
|
1595
|
-
kind: "number",
|
|
1596
|
-
text: base.text,
|
|
1597
|
-
fn: merged
|
|
1598
|
-
};
|
|
1599
|
-
return {
|
|
1600
|
-
kind: "arg",
|
|
1601
|
-
name: base.name,
|
|
1602
|
-
fn: merged
|
|
1603
|
-
};
|
|
1604
|
-
}
|
|
1625
|
+
if (isNumeric(name)) return this.numeric(name, base, own, at);
|
|
1605
1626
|
if (base.kind !== "arg") throw this.fail(`formatting a literal date depends on the time zone: pass the date as a value`, at, "icu-unsupported-type");
|
|
1606
1627
|
const fnDate = annotation(name, own);
|
|
1607
1628
|
this.style(fnDate, at);
|
|
@@ -1611,6 +1632,23 @@ var Lowering = class {
|
|
|
1611
1632
|
fn: fnDate
|
|
1612
1633
|
};
|
|
1613
1634
|
}
|
|
1635
|
+
/** A numeric annotation on `base`, over the options `base` already has. */
|
|
1636
|
+
numeric(name, base, own, at) {
|
|
1637
|
+
const merged = inherit(name, base.kind === "text" ? null : base.fn, own);
|
|
1638
|
+
if (name === "currency" && !merged.options.has("currency")) throw this.fail(":currency needs currency=EUR (the ISO 4217 code)", at);
|
|
1639
|
+
this.style(merged, at);
|
|
1640
|
+
if (base.kind === "arg") return {
|
|
1641
|
+
kind: "arg",
|
|
1642
|
+
name: base.name,
|
|
1643
|
+
fn: merged
|
|
1644
|
+
};
|
|
1645
|
+
if (base.kind === "text" && !NUMBER_LITERAL.test(base.text)) throw this.fail(`${JSON.stringify(base.text)} is not a number for :${name}`, at);
|
|
1646
|
+
return {
|
|
1647
|
+
kind: "number",
|
|
1648
|
+
text: base.text,
|
|
1649
|
+
fn: merged
|
|
1650
|
+
};
|
|
1651
|
+
}
|
|
1614
1652
|
/** Literal option values; a variable must name a constant declared earlier. */
|
|
1615
1653
|
options(fn, options, at) {
|
|
1616
1654
|
const out = /* @__PURE__ */ new Map();
|
|
@@ -1690,7 +1728,7 @@ var Lowering = class {
|
|
|
1690
1728
|
let type = "number";
|
|
1691
1729
|
let style;
|
|
1692
1730
|
try {
|
|
1693
|
-
if (
|
|
1731
|
+
if (isNumeric(fn.name)) style = numberStyle(fn.name, fn.options);
|
|
1694
1732
|
else [type, style] = dateStyle(fn);
|
|
1695
1733
|
} catch (error) {
|
|
1696
1734
|
if (error instanceof Unsupported) throw this.fail(error.message, at, error.code);
|
|
@@ -1726,7 +1764,7 @@ var Lowering = class {
|
|
|
1726
1764
|
if (fn.name === "number" || fn.name === "integer") {
|
|
1727
1765
|
if (fn.inheritedSelect) throw this.fail(`$${name} takes select from the value it is declared from: set select on its own annotation`, at);
|
|
1728
1766
|
const select = fn.options.get("select") ?? "plural";
|
|
1729
|
-
if (select
|
|
1767
|
+
if (!isSelectMode(select)) throw this.fail(selectError(select), at);
|
|
1730
1768
|
if (value.kind === "number") return {
|
|
1731
1769
|
kind: "constant-number",
|
|
1732
1770
|
text: value.text,
|
|
@@ -1756,32 +1794,14 @@ var Lowering = class {
|
|
|
1756
1794
|
}
|
|
1757
1795
|
const selector = selectors[i];
|
|
1758
1796
|
const keys = [];
|
|
1759
|
-
for (const
|
|
1797
|
+
for (const variant of live.flat()) {
|
|
1760
1798
|
const key = variant.keys[i];
|
|
1761
1799
|
if (key !== null && !keys.includes(key)) keys.push(key);
|
|
1762
1800
|
}
|
|
1763
1801
|
const only = (key) => (group) => group.filter((variant) => variant.keys[i] === key);
|
|
1764
1802
|
const next = (...picks) => this.expand(selectors, i + 1, live.flatMap((group) => picks.map((pick) => pick(group))));
|
|
1765
1803
|
if (selector.kind === "constant") return next(only(selector.text), only(null));
|
|
1766
|
-
if (selector.kind === "string")
|
|
1767
|
-
for (const key of keys) if (!ICU_IDENTIFIER.test(key)) throw this.fail(`the key ${JSON.stringify(key)} cannot be an ICU select key (no spaces or punctuation)`, this.message.matchAt, "icu-unsupported-type");
|
|
1768
|
-
const options = keys.map((key) => ({
|
|
1769
|
-
key,
|
|
1770
|
-
value: next(only(key), only(null))
|
|
1771
|
-
}));
|
|
1772
|
-
const fallback = next(only(null));
|
|
1773
|
-
const other = options.find((option) => option.key === "other");
|
|
1774
|
-
if (other === void 0) options.push({
|
|
1775
|
-
key: "other",
|
|
1776
|
-
value: fallback
|
|
1777
|
-
});
|
|
1778
|
-
else if (!same(other.value, fallback)) throw this.fail(`a :string key "other" cannot be told apart from * in vi18n's IR: rename the value or merge the two variants`, this.message.matchAt, "icu-unsupported-type");
|
|
1779
|
-
return this.choice({
|
|
1780
|
-
type: "select",
|
|
1781
|
-
name: selector.name,
|
|
1782
|
-
options
|
|
1783
|
-
});
|
|
1784
|
-
}
|
|
1804
|
+
if (selector.kind === "string") return this.stringSelect(selector.name, keys, only, next);
|
|
1785
1805
|
const exact = [];
|
|
1786
1806
|
const categories = [];
|
|
1787
1807
|
for (const key of keys) if (NUMBER_LITERAL.test(key)) {
|
|
@@ -1795,26 +1815,6 @@ var Lowering = class {
|
|
|
1795
1815
|
if (categories.length > 0 && !selector.exact) throw this.fail(`the plural category of the literal ${selector.text} depends on the locale: select on a value`, this.message.matchAt, "icu-unsupported-type");
|
|
1796
1816
|
return next(only(String(Number(selector.text))), only(null));
|
|
1797
1817
|
}
|
|
1798
|
-
const byCategory = (prefix) => {
|
|
1799
|
-
const first = prefix === null ? [] : [only(prefix)];
|
|
1800
|
-
const options = categories.map((category) => ({
|
|
1801
|
-
key: category,
|
|
1802
|
-
value: next(...first, only(category), only(null))
|
|
1803
|
-
}));
|
|
1804
|
-
const fallback = next(...first, only(null));
|
|
1805
|
-
const other = options.find((option) => option.key === "other");
|
|
1806
|
-
if (other === void 0) options.push({
|
|
1807
|
-
key: "other",
|
|
1808
|
-
value: fallback
|
|
1809
|
-
});
|
|
1810
|
-
else if (!same(other.value, fallback)) {
|
|
1811
|
-
for (const category of CATEGORIES) if (!categories.includes(category)) options.push({
|
|
1812
|
-
key: category,
|
|
1813
|
-
value: fallback
|
|
1814
|
-
});
|
|
1815
|
-
}
|
|
1816
|
-
return options;
|
|
1817
|
-
};
|
|
1818
1818
|
const plural = (options) => this.choice({
|
|
1819
1819
|
type: "plural",
|
|
1820
1820
|
pluralType: selector.kind === "ordinal" ? "ordinal" : "cardinal",
|
|
@@ -1824,11 +1824,31 @@ var Lowering = class {
|
|
|
1824
1824
|
});
|
|
1825
1825
|
const options = exact.map((key) => ({
|
|
1826
1826
|
key: "=" + String(Number(key)),
|
|
1827
|
-
value: plural(byCategory(key))
|
|
1827
|
+
value: plural(byCategory(only, next, categories, key))
|
|
1828
1828
|
}));
|
|
1829
|
-
options.push(...byCategory(null));
|
|
1829
|
+
options.push(...byCategory(only, next, categories, null));
|
|
1830
1830
|
return plural(options);
|
|
1831
1831
|
}
|
|
1832
|
+
/** A `:string` selector's select, one case per key and `other` for `*`. */
|
|
1833
|
+
stringSelect(name, keys, only, next) {
|
|
1834
|
+
for (const key of keys) if (!ICU_IDENTIFIER.test(key)) throw this.fail(`the key ${JSON.stringify(key)} cannot be an ICU select key (no spaces or punctuation)`, this.message.matchAt, "icu-unsupported-type");
|
|
1835
|
+
const options = keys.map((key) => ({
|
|
1836
|
+
key,
|
|
1837
|
+
value: next(only(key), only(null))
|
|
1838
|
+
}));
|
|
1839
|
+
const fallback = next(only(null));
|
|
1840
|
+
const other = options.find((option) => option.key === "other");
|
|
1841
|
+
if (other === void 0) options.push({
|
|
1842
|
+
key: "other",
|
|
1843
|
+
value: fallback
|
|
1844
|
+
});
|
|
1845
|
+
else if (!same(other.value, fallback)) throw this.fail(`a :string key "other" cannot be told apart from * in vi18n's IR: rename the value or merge the two variants`, this.message.matchAt, "icu-unsupported-type");
|
|
1846
|
+
return this.choice({
|
|
1847
|
+
type: "select",
|
|
1848
|
+
name,
|
|
1849
|
+
options
|
|
1850
|
+
});
|
|
1851
|
+
}
|
|
1832
1852
|
/** A select or plural, or its one value when every case renders the same. */
|
|
1833
1853
|
choice(node) {
|
|
1834
1854
|
const [first, ...rest] = node.options;
|
|
@@ -1839,6 +1859,27 @@ var Lowering = class {
|
|
|
1839
1859
|
return mf2Error(this.source, message, offset, code);
|
|
1840
1860
|
}
|
|
1841
1861
|
};
|
|
1862
|
+
/** The cases after an exact key `prefix` (or none): categories, then `*`. */
|
|
1863
|
+
function byCategory(only, next, categories, prefix) {
|
|
1864
|
+
const first = prefix === null ? [] : [only(prefix)];
|
|
1865
|
+
const options = categories.map((category) => ({
|
|
1866
|
+
key: category,
|
|
1867
|
+
value: next(...first, only(category), only(null))
|
|
1868
|
+
}));
|
|
1869
|
+
const fallback = next(...first, only(null));
|
|
1870
|
+
const other = options.find((option) => option.key === "other");
|
|
1871
|
+
if (other === void 0) options.push({
|
|
1872
|
+
key: "other",
|
|
1873
|
+
value: fallback
|
|
1874
|
+
});
|
|
1875
|
+
else if (!same(other.value, fallback)) {
|
|
1876
|
+
for (const category of CATEGORIES) if (!categories.includes(category)) options.push({
|
|
1877
|
+
key: category,
|
|
1878
|
+
value: fallback
|
|
1879
|
+
});
|
|
1880
|
+
}
|
|
1881
|
+
return options;
|
|
1882
|
+
}
|
|
1842
1883
|
function annotation(name, options) {
|
|
1843
1884
|
return {
|
|
1844
1885
|
name,
|
|
@@ -1846,6 +1887,19 @@ function annotation(name, options) {
|
|
|
1846
1887
|
inheritedSelect: false
|
|
1847
1888
|
};
|
|
1848
1889
|
}
|
|
1890
|
+
/** The options of the value an annotation is declared from that it does not take over. */
|
|
1891
|
+
const DROPPED = {
|
|
1892
|
+
integer: [
|
|
1893
|
+
"minimumFractionDigits",
|
|
1894
|
+
"maximumFractionDigits",
|
|
1895
|
+
"minimumSignificantDigits"
|
|
1896
|
+
],
|
|
1897
|
+
percent: [
|
|
1898
|
+
"minimumIntegerDigits",
|
|
1899
|
+
"roundingIncrement",
|
|
1900
|
+
"select"
|
|
1901
|
+
]
|
|
1902
|
+
};
|
|
1849
1903
|
/**
|
|
1850
1904
|
* MF2 option inheritance: a numeric annotation on a numeric value keeps the
|
|
1851
1905
|
* value's options it has itself, its own winning; `:integer` drops fraction
|
|
@@ -1853,17 +1907,9 @@ function annotation(name, options) {
|
|
|
1853
1907
|
* `select`.
|
|
1854
1908
|
*/
|
|
1855
1909
|
function inherit(name, prev, own) {
|
|
1856
|
-
if (prev === null || !
|
|
1910
|
+
if (prev === null || !isNumeric(prev.name)) return annotation(name, own);
|
|
1857
1911
|
const known = OPTIONS[name];
|
|
1858
|
-
const dropped = name
|
|
1859
|
-
"minimumFractionDigits",
|
|
1860
|
-
"maximumFractionDigits",
|
|
1861
|
-
"minimumSignificantDigits"
|
|
1862
|
-
] : name === "percent" ? [
|
|
1863
|
-
"minimumIntegerDigits",
|
|
1864
|
-
"roundingIncrement",
|
|
1865
|
-
"select"
|
|
1866
|
-
] : [];
|
|
1912
|
+
const dropped = DROPPED[name] ?? [];
|
|
1867
1913
|
const options = /* @__PURE__ */ new Map();
|
|
1868
1914
|
for (const [key, value] of prev.options) if (known.includes(key) && !dropped.includes(key)) options.set(key, value);
|
|
1869
1915
|
const inheritedSelect = options.has("select") && !own.has("select");
|
|
@@ -1969,109 +2015,119 @@ const OPTIONS = {
|
|
|
1969
2015
|
* options match what MF2's `Intl.NumberFormat`-named options mean). Keyword
|
|
1970
2016
|
* styles where one says the same: `integer`, `percent`.
|
|
1971
2017
|
*/
|
|
1972
|
-
function numberStyle(
|
|
1973
|
-
const o
|
|
1974
|
-
for (const key of o.keys()) if (!OPTIONS[fn.name].includes(key)) throw invalid(`:${fn.name} has no option ${key}`);
|
|
1975
|
-
const digits = (key) => {
|
|
1976
|
-
const value = o.get(key);
|
|
1977
|
-
if (value === void 0) return null;
|
|
1978
|
-
if (!DIGIT_SIZE.test(value)) throw invalid(`${key}=${value} is not a digit count`);
|
|
1979
|
-
return Number(value);
|
|
1980
|
-
};
|
|
2018
|
+
function numberStyle(name, o) {
|
|
2019
|
+
for (const key of o.keys()) if (!OPTIONS[name].includes(key)) throw invalid(`:${name} has no option ${key}`);
|
|
1981
2020
|
const tokens = [];
|
|
1982
|
-
if (
|
|
1983
|
-
if (
|
|
1984
|
-
if (
|
|
1985
|
-
const minInt = digits("minimumIntegerDigits");
|
|
1986
|
-
const minFrac = digits("minimumFractionDigits");
|
|
1987
|
-
const maxFrac = digits("maximumFractionDigits");
|
|
1988
|
-
const minSig = digits("minimumSignificantDigits");
|
|
1989
|
-
const maxSig = digits("maximumSignificantDigits");
|
|
2021
|
+
if (name === "integer") tokens.push(".");
|
|
2022
|
+
if (name === "percent") tokens.push("percent");
|
|
2023
|
+
if (name === "currency") tokens.push("currency/" + o.get("currency").toUpperCase());
|
|
2024
|
+
const minInt = digits(o, "minimumIntegerDigits");
|
|
2025
|
+
const minFrac = digits(o, "minimumFractionDigits");
|
|
2026
|
+
const maxFrac = digits(o, "maximumFractionDigits");
|
|
2027
|
+
const minSig = digits(o, "minimumSignificantDigits");
|
|
2028
|
+
const maxSig = digits(o, "maximumSignificantDigits");
|
|
1990
2029
|
if (minInt !== null) tokens.push("integer-width/*" + "0".repeat(minInt));
|
|
1991
|
-
|
|
1992
|
-
if (fn.name === "currency") {
|
|
1993
|
-
const value = o.get("fractionDigits");
|
|
1994
|
-
if (value !== void 0 && value !== "auto") {
|
|
1995
|
-
if (!DIGIT_SIZE.test(value)) throw invalid(`fractionDigits=${value} is not a digit count`);
|
|
1996
|
-
fraction = "." + "0".repeat(Number(value));
|
|
1997
|
-
}
|
|
1998
|
-
} else if (minFrac !== null || maxFrac !== null) {
|
|
1999
|
-
const min = minFrac ?? 0;
|
|
2000
|
-
if (maxFrac === null) fraction = min === 0 ? null : "." + "0".repeat(min) + "*";
|
|
2001
|
-
else if (maxFrac < min) throw invalid("maximumFractionDigits is below minimumFractionDigits");
|
|
2002
|
-
else fraction = "." + "0".repeat(min) + "#".repeat(maxFrac - min);
|
|
2003
|
-
}
|
|
2004
|
-
const strip = o.get("trailingZeroDisplay");
|
|
2005
|
-
if (strip === "stripIfInteger") {
|
|
2006
|
-
if (fraction === null || fraction.endsWith("*")) throw new Unsupported("trailingZeroDisplay=stripIfInteger needs both fraction digit counts");
|
|
2007
|
-
if (fraction !== ".") fraction += "/w";
|
|
2008
|
-
} else if (strip !== void 0 && strip !== "auto") throw new Unsupported(`trailingZeroDisplay=${strip} is not supported`);
|
|
2030
|
+
const fraction = stripped(name === "currency" ? currencyFraction(o) : fractionDigits(minFrac, maxFrac), o.get("trailingZeroDisplay"));
|
|
2009
2031
|
if (fraction !== null) tokens.push(fraction);
|
|
2010
|
-
|
|
2011
|
-
if (minSig !== null || maxSig !== null) {
|
|
2012
|
-
const min = minSig ?? 1;
|
|
2013
|
-
if (min < 1) throw invalid("minimumSignificantDigits must be at least 1");
|
|
2014
|
-
if (maxSig === null) significant = "@".repeat(min) + "+";
|
|
2015
|
-
else if (maxSig < min) throw invalid("maximumSignificantDigits is below minimumSignificantDigits");
|
|
2016
|
-
else significant = "@".repeat(min) + "#".repeat(maxSig - min);
|
|
2017
|
-
}
|
|
2018
|
-
const priority = o.get("roundingPriority");
|
|
2019
|
-
if (priority !== void 0 && priority !== "auto") {
|
|
2020
|
-
if (significant === null || priority !== "morePrecision" && priority !== "lessPrecision") throw new Unsupported(`roundingPriority=${priority} needs significant digits set here`);
|
|
2021
|
-
significant += priority === "morePrecision" ? "r" : "s";
|
|
2022
|
-
}
|
|
2032
|
+
const significant = prioritised(significantDigits(minSig, maxSig), o.get("roundingPriority"));
|
|
2023
2033
|
if (significant !== null) tokens.push(significant);
|
|
2034
|
+
tokens.push(...optionStems(o));
|
|
2035
|
+
const select = o.get("select");
|
|
2036
|
+
if (select !== void 0 && !isSelectMode(select)) throw invalid(selectError(select));
|
|
2037
|
+
if (tokens.length === 0) return null;
|
|
2038
|
+
if (tokens.length === 1 && tokens[0] === ".") return "integer";
|
|
2039
|
+
if (tokens.length === 1 && tokens[0] === "percent") return "percent";
|
|
2040
|
+
return "::" + tokens.join(" ");
|
|
2041
|
+
}
|
|
2042
|
+
/** A digit-count option, checked; null when not set. */
|
|
2043
|
+
function digits(o, key) {
|
|
2044
|
+
const value = o.get(key);
|
|
2045
|
+
if (value === void 0) return null;
|
|
2046
|
+
if (!DIGIT_SIZE.test(value)) throw invalid(`${key}=${value} is not a digit count`);
|
|
2047
|
+
return Number(value);
|
|
2048
|
+
}
|
|
2049
|
+
/** `:currency`'s `fractionDigits` as a precision stem. */
|
|
2050
|
+
function currencyFraction(o) {
|
|
2051
|
+
const value = o.get("fractionDigits");
|
|
2052
|
+
if (value === void 0 || value === "auto") return null;
|
|
2053
|
+
if (!DIGIT_SIZE.test(value)) throw invalid(`fractionDigits=${value} is not a digit count`);
|
|
2054
|
+
return "." + "0".repeat(Number(value));
|
|
2055
|
+
}
|
|
2056
|
+
/** Minimum and maximum fraction digits as a precision stem (`.00#`, `.0*`). */
|
|
2057
|
+
function fractionDigits(minFrac, maxFrac) {
|
|
2058
|
+
if (minFrac === null && maxFrac === null) return null;
|
|
2059
|
+
const min = minFrac ?? 0;
|
|
2060
|
+
if (maxFrac === null) return min === 0 ? null : "." + "0".repeat(min) + "*";
|
|
2061
|
+
if (maxFrac < min) throw invalid("maximumFractionDigits is below minimumFractionDigits");
|
|
2062
|
+
return "." + "0".repeat(min) + "#".repeat(maxFrac - min);
|
|
2063
|
+
}
|
|
2064
|
+
/** The precision stem with `trailingZeroDisplay` applied (`/w`). */
|
|
2065
|
+
function stripped(fraction, strip) {
|
|
2066
|
+
if (strip === void 0 || strip === "auto") return fraction;
|
|
2067
|
+
if (strip !== "stripIfInteger") throw new Unsupported(`trailingZeroDisplay=${strip} is not supported`);
|
|
2068
|
+
if (fraction === null || fraction.endsWith("*")) throw new Unsupported("trailingZeroDisplay=stripIfInteger needs both fraction digit counts");
|
|
2069
|
+
return fraction === "." ? fraction : fraction + "/w";
|
|
2070
|
+
}
|
|
2071
|
+
/** Minimum and maximum significant digits as a stem (`@@#`, `@+`). */
|
|
2072
|
+
function significantDigits(minSig, maxSig) {
|
|
2073
|
+
if (minSig === null && maxSig === null) return null;
|
|
2074
|
+
const min = minSig ?? 1;
|
|
2075
|
+
if (min < 1) throw invalid("minimumSignificantDigits must be at least 1");
|
|
2076
|
+
if (maxSig === null) return "@".repeat(min) + "+";
|
|
2077
|
+
if (maxSig < min) throw invalid("maximumSignificantDigits is below minimumSignificantDigits");
|
|
2078
|
+
return "@".repeat(min) + "#".repeat(maxSig - min);
|
|
2079
|
+
}
|
|
2080
|
+
/** The significant-digits stem with `roundingPriority` applied (`r`, `s`). */
|
|
2081
|
+
function prioritised(significant, priority) {
|
|
2082
|
+
if (priority === void 0 || priority === "auto") return significant;
|
|
2083
|
+
if (significant === null || priority !== "morePrecision" && priority !== "lessPrecision") throw new Unsupported(`roundingPriority=${priority} needs significant digits set here`);
|
|
2084
|
+
return significant + (priority === "morePrecision" ? "r" : "s");
|
|
2085
|
+
}
|
|
2086
|
+
/** Stems for the sign, grouping, currency display and rounding options. */
|
|
2087
|
+
function optionStems(o) {
|
|
2088
|
+
const stems = [];
|
|
2024
2089
|
const currencySign = o.get("currencySign");
|
|
2025
|
-
if (currencySign === "accounting")
|
|
2090
|
+
if (currencySign === "accounting") stems.push("sign-accounting");
|
|
2026
2091
|
else if (currencySign !== void 0 && currencySign !== "standard") throw new Unsupported(`currencySign=${currencySign} is not supported`);
|
|
2027
|
-
const sign = o
|
|
2028
|
-
if (sign !==
|
|
2029
|
-
const stem = Object.hasOwn(SIGN, sign) ? SIGN[sign] : void 0;
|
|
2030
|
-
if (stem === void 0) throw new Unsupported(`signDisplay=${sign} is not supported`);
|
|
2031
|
-
tokens.push(stem);
|
|
2032
|
-
}
|
|
2092
|
+
const sign = tableStem(SIGN, o, "signDisplay", "auto");
|
|
2093
|
+
if (sign !== null) stems.push(sign);
|
|
2033
2094
|
const grouping = o.get("useGrouping");
|
|
2034
|
-
if (grouping === "never" || grouping === "false")
|
|
2095
|
+
if (grouping === "never" || grouping === "false") stems.push("group-off");
|
|
2035
2096
|
else if (grouping !== void 0 && grouping !== "auto") throw new Unsupported(`useGrouping=${grouping} is not supported`);
|
|
2036
|
-
const display = o
|
|
2037
|
-
if (display !==
|
|
2038
|
-
const stem = Object.hasOwn(CURRENCY_DISPLAY, display) ? CURRENCY_DISPLAY[display] : void 0;
|
|
2039
|
-
if (stem === void 0) throw new Unsupported(`currencyDisplay=${display} is not supported`);
|
|
2040
|
-
tokens.push(stem);
|
|
2041
|
-
}
|
|
2097
|
+
const display = tableStem(CURRENCY_DISPLAY, o, "currencyDisplay", "symbol");
|
|
2098
|
+
if (display !== null) stems.push(display);
|
|
2042
2099
|
const increment = o.get("roundingIncrement");
|
|
2043
2100
|
if (increment !== void 0 && increment !== "1") throw new Unsupported(`roundingIncrement=${increment} is not supported`);
|
|
2044
|
-
const mode = o
|
|
2045
|
-
if (mode !==
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
const
|
|
2051
|
-
if (
|
|
2052
|
-
|
|
2053
|
-
if (
|
|
2054
|
-
return
|
|
2055
|
-
}
|
|
2101
|
+
const mode = tableStem(ROUNDING, o, "roundingMode", "halfExpand");
|
|
2102
|
+
if (mode !== null) stems.push(mode);
|
|
2103
|
+
return stems;
|
|
2104
|
+
}
|
|
2105
|
+
/** The stem `table` gives option `key`'s value; null for its default. */
|
|
2106
|
+
function tableStem(table, o, key, fallback) {
|
|
2107
|
+
const value = o.get(key);
|
|
2108
|
+
if (value === void 0 || value === fallback) return null;
|
|
2109
|
+
const stem = Object.hasOwn(table, value) ? table[value] : void 0;
|
|
2110
|
+
if (stem === void 0) throw new Unsupported(`${key}=${value} is not supported`);
|
|
2111
|
+
return stem;
|
|
2112
|
+
}
|
|
2113
|
+
/** The skeleton fields of each `fields` value, one symbol each: `M` and `E` take their width from `length`. */
|
|
2056
2114
|
const DATE_FIELDS = {
|
|
2057
|
-
weekday:
|
|
2058
|
-
"day-weekday":
|
|
2059
|
-
"month-day":
|
|
2060
|
-
"month-day-weekday":
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
"
|
|
2072
|
-
"
|
|
2073
|
-
"d",
|
|
2074
|
-
"E"
|
|
2115
|
+
weekday: "E",
|
|
2116
|
+
"day-weekday": "dE",
|
|
2117
|
+
"month-day": "Md",
|
|
2118
|
+
"month-day-weekday": "MdE",
|
|
2119
|
+
"year-month-day": "yMd",
|
|
2120
|
+
"year-month-day-weekday": "yMdE"
|
|
2121
|
+
};
|
|
2122
|
+
/** The options of `:date`, `:time` and `:datetime` vi18n supports. */
|
|
2123
|
+
const DATE_OPTIONS = {
|
|
2124
|
+
date: ["fields", "length"],
|
|
2125
|
+
time: ["precision", "timeZoneStyle"],
|
|
2126
|
+
datetime: [
|
|
2127
|
+
"dateFields",
|
|
2128
|
+
"dateLength",
|
|
2129
|
+
"timePrecision",
|
|
2130
|
+
"timeZoneStyle"
|
|
2075
2131
|
]
|
|
2076
2132
|
};
|
|
2077
2133
|
const MONTH = {
|
|
@@ -2095,12 +2151,7 @@ const ZONE = {
|
|
|
2095
2151
|
*/
|
|
2096
2152
|
function dateStyle(fn) {
|
|
2097
2153
|
const o = fn.options;
|
|
2098
|
-
const known = fn.name
|
|
2099
|
-
"dateFields",
|
|
2100
|
-
"dateLength",
|
|
2101
|
-
"timePrecision",
|
|
2102
|
-
"timeZoneStyle"
|
|
2103
|
-
];
|
|
2154
|
+
const known = DATE_OPTIONS[fn.name] ?? [];
|
|
2104
2155
|
for (const key of o.keys()) if (!known.includes(key)) throw new Unsupported(`:${fn.name} has no option ${key} vi18n supports (Draft options and overrides are not)`);
|
|
2105
2156
|
const pick = (table, key, value) => {
|
|
2106
2157
|
if (!Object.hasOwn(table, value)) throw invalid(`${key}=${value} is not a value of ${key}`);
|
|
@@ -2111,7 +2162,8 @@ function dateStyle(fn) {
|
|
|
2111
2162
|
const length = o.get(lengthKey) ?? "medium";
|
|
2112
2163
|
const month = pick(MONTH, lengthKey, length);
|
|
2113
2164
|
if (!Object.hasOwn(DATE_FIELDS, fields)) throw invalid(`${fieldsKey}=${fields} is not a value of ${fieldsKey}`);
|
|
2114
|
-
|
|
2165
|
+
const weekday = length === "long" ? "EEEE" : "EEE";
|
|
2166
|
+
return DATE_FIELDS[fields].replace("M", month).replace("E", weekday);
|
|
2115
2167
|
};
|
|
2116
2168
|
const time = (precisionKey) => {
|
|
2117
2169
|
const zone = o.get("timeZoneStyle");
|
|
@@ -2121,8 +2173,7 @@ function dateStyle(fn) {
|
|
|
2121
2173
|
if (fn.name === "time") return ["time", "::" + time("precision")];
|
|
2122
2174
|
return ["date", "::" + date("dateFields", "dateLength") + time("timePrecision")];
|
|
2123
2175
|
}
|
|
2124
|
-
|
|
2125
|
-
//#region src/core/mf2/index.ts
|
|
2176
|
+
// src/core/mf2/index.ts
|
|
2126
2177
|
/**
|
|
2127
2178
|
* The message front ends (PLAN §4.2, INTERNALS §2.1.4): ICU MessageFormat 1,
|
|
2128
2179
|
* and MessageFormat 2 lowered into the same ICU AST. `parseSource` is what
|
|
@@ -2152,7 +2203,7 @@ function bodyStart(source) {
|
|
|
2152
2203
|
let i = 0;
|
|
2153
2204
|
for (; i < source.length; i++) {
|
|
2154
2205
|
const c = source.charCodeAt(i);
|
|
2155
|
-
if (!(c
|
|
2206
|
+
if (!isWs(c) && !isBidi(c)) break;
|
|
2156
2207
|
}
|
|
2157
2208
|
return i;
|
|
2158
2209
|
}
|
|
@@ -2185,8 +2236,133 @@ function hinted(error, hint) {
|
|
|
2185
2236
|
message: `${diagnostic.message} (${hint})`
|
|
2186
2237
|
});
|
|
2187
2238
|
}
|
|
2188
|
-
|
|
2189
|
-
|
|
2239
|
+
// src/core/icu/print.ts
|
|
2240
|
+
const HASH = 35;
|
|
2241
|
+
const APOS = 39;
|
|
2242
|
+
const SLASH = 47;
|
|
2243
|
+
const LT = 60;
|
|
2244
|
+
const GT = 62;
|
|
2245
|
+
const LBRACE = 123;
|
|
2246
|
+
const RBRACE = 125;
|
|
2247
|
+
/** "Nothing follows": the end of the whole message. */
|
|
2248
|
+
const END = -1;
|
|
2249
|
+
/** Prints a message in canonical form. Literal text keeps every character. */
|
|
2250
|
+
function printICU(message) {
|
|
2251
|
+
return printNodes(message, false, END);
|
|
2252
|
+
}
|
|
2253
|
+
/**
|
|
2254
|
+
* @param poundSensitive inside any plural/selectordinal option, where `#` must be quoted
|
|
2255
|
+
* @param after first character printed after these nodes: `}` (option), `<` (closing tag) or END
|
|
2256
|
+
*/
|
|
2257
|
+
function printNodes(nodes, poundSensitive, after) {
|
|
2258
|
+
let out = "";
|
|
2259
|
+
const count = nodes.length;
|
|
2260
|
+
for (let i = 0; i < count; i++) {
|
|
2261
|
+
const node = nodes[i];
|
|
2262
|
+
if (node.type === "literal") {
|
|
2263
|
+
let value = node.value;
|
|
2264
|
+
while (i + 1 < count && nodes[i + 1].type === "literal") value += nodes[++i].value;
|
|
2265
|
+
const next = i + 1 < count ? firstChar(nodes[i + 1]) : after;
|
|
2266
|
+
out += printLiteral(value, poundSensitive, next);
|
|
2267
|
+
continue;
|
|
2268
|
+
}
|
|
2269
|
+
out += printNode(node, poundSensitive);
|
|
2270
|
+
}
|
|
2271
|
+
return out;
|
|
2272
|
+
}
|
|
2273
|
+
function printNode(node, poundSensitive) {
|
|
2274
|
+
switch (node.type) {
|
|
2275
|
+
case "literal": return printLiteral(node.value, poundSensitive, END);
|
|
2276
|
+
case "argument": return `{${node.name}}`;
|
|
2277
|
+
case "number":
|
|
2278
|
+
case "date":
|
|
2279
|
+
case "time": {
|
|
2280
|
+
const style = node.style;
|
|
2281
|
+
if (style === null) return `{${node.name}, ${node.type}}`;
|
|
2282
|
+
return `{${node.name}, ${node.type}, ${style.kind === "skeleton" ? "::" + style.value : style.value}}`;
|
|
2283
|
+
}
|
|
2284
|
+
case "select": return `{${node.name}, select, ${printOptions(node.options, poundSensitive)}}`;
|
|
2285
|
+
case "plural": {
|
|
2286
|
+
const type = node.pluralType === "cardinal" ? "plural" : "selectordinal";
|
|
2287
|
+
const offset = node.offset === 0 ? "" : `offset:${node.offset} `;
|
|
2288
|
+
return `{${node.name}, ${type}, ${offset}${printOptions(node.options, true)}}`;
|
|
2289
|
+
}
|
|
2290
|
+
case "pound": return "#";
|
|
2291
|
+
case "tag": return node.children === null ? `<${node.name}/>` : `<${node.name}>${printNodes(node.children, poundSensitive, LT)}</${node.name}>`;
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
function printOptions(options, poundSensitive) {
|
|
2295
|
+
let out = "";
|
|
2296
|
+
for (let i = 0; i < options.length; i++) {
|
|
2297
|
+
const option = options[i];
|
|
2298
|
+
if (i > 0) out += " ";
|
|
2299
|
+
out += `${option.key} {${printNodes(option.value, poundSensitive, RBRACE)}}`;
|
|
2300
|
+
}
|
|
2301
|
+
return out;
|
|
2302
|
+
}
|
|
2303
|
+
function firstChar(node) {
|
|
2304
|
+
switch (node.type) {
|
|
2305
|
+
case "tag": return LT;
|
|
2306
|
+
case "pound": return HASH;
|
|
2307
|
+
case "literal": return node.value.charCodeAt(0);
|
|
2308
|
+
default: return LBRACE;
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
/**
|
|
2312
|
+
* Minimal, deterministic quoting: a run starting at a special character
|
|
2313
|
+
* (`{`, `}`, `#` where pound-sensitive, `<` before a letter or `/`) is wrapped
|
|
2314
|
+
* in apostrophes together with any special characters and apostrophes that
|
|
2315
|
+
* follow it; any other apostrophe is doubled only where a single one would
|
|
2316
|
+
* open a quote.
|
|
2317
|
+
*/
|
|
2318
|
+
function printLiteral(value, poundSensitive, after) {
|
|
2319
|
+
if (!needsQuoting(value, poundSensitive)) return value;
|
|
2320
|
+
const length = value.length;
|
|
2321
|
+
let out = "";
|
|
2322
|
+
let run = 0;
|
|
2323
|
+
let i = 0;
|
|
2324
|
+
while (i < length) {
|
|
2325
|
+
const c = value.charCodeAt(i);
|
|
2326
|
+
if (isSpecial(value, i, c, poundSensitive)) {
|
|
2327
|
+
let j = i + 1;
|
|
2328
|
+
while (j < length) {
|
|
2329
|
+
const d = value.charCodeAt(j);
|
|
2330
|
+
if (d !== APOS && !isSpecial(value, j, d, poundSensitive)) break;
|
|
2331
|
+
j++;
|
|
2332
|
+
}
|
|
2333
|
+
out += value.slice(run, i) + "'" + value.slice(i, j).replaceAll("'", "''") + "'";
|
|
2334
|
+
i = j;
|
|
2335
|
+
run = j;
|
|
2336
|
+
continue;
|
|
2337
|
+
}
|
|
2338
|
+
if (c === APOS) {
|
|
2339
|
+
const next = i + 1 < length ? value.charCodeAt(i + 1) : after;
|
|
2340
|
+
if (next === APOS || next === LBRACE || next === RBRACE || next === LT || next === GT || next === HASH && poundSensitive) {
|
|
2341
|
+
out += value.slice(run, i + 1) + "'";
|
|
2342
|
+
run = i + 1;
|
|
2343
|
+
}
|
|
2344
|
+
}
|
|
2345
|
+
i++;
|
|
2346
|
+
}
|
|
2347
|
+
return out + value.slice(run);
|
|
2348
|
+
}
|
|
2349
|
+
function needsQuoting(value, poundSensitive) {
|
|
2350
|
+
for (let i = 0; i < value.length; i++) {
|
|
2351
|
+
const c = value.charCodeAt(i);
|
|
2352
|
+
if (c === APOS || c === LBRACE || c === RBRACE || c === LT || c === HASH && poundSensitive) return true;
|
|
2353
|
+
}
|
|
2354
|
+
return false;
|
|
2355
|
+
}
|
|
2356
|
+
function isSpecial(value, i, c, poundSensitive) {
|
|
2357
|
+
if (c === LBRACE || c === RBRACE) return true;
|
|
2358
|
+
if (c === HASH) return poundSensitive;
|
|
2359
|
+
if (c === LT) {
|
|
2360
|
+
const next = value.charCodeAt(i + 1);
|
|
2361
|
+
return next === SLASH || next >= 65 && next <= 90 || next >= 97 && next <= 122;
|
|
2362
|
+
}
|
|
2363
|
+
return false;
|
|
2364
|
+
}
|
|
2365
|
+
// src/core/icu/canonical.ts
|
|
2190
2366
|
const CR = /\r\n?/g;
|
|
2191
2367
|
/** NFC and LF line ends: what every parser and hash sees. */
|
|
2192
2368
|
function normalizeSource(source) {
|
|
@@ -2204,8 +2380,7 @@ function isPlainText(text) {
|
|
|
2204
2380
|
}
|
|
2205
2381
|
return true;
|
|
2206
2382
|
}
|
|
2207
|
-
|
|
2208
|
-
//#region src/core/id/hash.ts
|
|
2383
|
+
// src/core/id/hash.ts
|
|
2209
2384
|
/**
|
|
2210
2385
|
* Ids (INTERNALS §2.2.1, §2.2.3, PLAN §5.5). An id depends only on its own
|
|
2211
2386
|
* canonical text and `ids.length`, so every process computes it alone and
|
|
@@ -2240,8 +2415,148 @@ const MANUAL_ID = /^[A-Za-z0-9_:-]+(\.[A-Za-z0-9_:-]+)+$/;
|
|
|
2240
2415
|
function isValidManualId(id) {
|
|
2241
2416
|
return MANUAL_ID.test(id);
|
|
2242
2417
|
}
|
|
2243
|
-
|
|
2244
|
-
|
|
2418
|
+
// src/core/codec.ts
|
|
2419
|
+
/**
|
|
2420
|
+
* The one place that turns source text into ids (INTERNALS §2.2). Extract,
|
|
2421
|
+
* catalogs and compilation see the same message many times per run, so parses,
|
|
2422
|
+
* canonical forms and hashes are memoised per string.
|
|
2423
|
+
*/
|
|
2424
|
+
/**
|
|
2425
|
+
* Memo entries past this count are dropped all at once: a long dev session
|
|
2426
|
+
* sees every edit of every message, while one scan of a large project needs
|
|
2427
|
+
* the whole set warm.
|
|
2428
|
+
*/
|
|
2429
|
+
const MEMO_LIMIT = 2e5;
|
|
2430
|
+
/**
|
|
2431
|
+
* What `parseICU` returns for text without syntax characters: no node for
|
|
2432
|
+
* "", else one literal (most sources and translations). An MF2 complex
|
|
2433
|
+
* message is lowered to the same AST (INTERNALS §2.1.4), so its canonical
|
|
2434
|
+
* form, id and catalog entry are ICU.
|
|
2435
|
+
*/
|
|
2436
|
+
function parseNormalized(normalized) {
|
|
2437
|
+
if (!isPlainText(normalized)) return parseSource(normalized);
|
|
2438
|
+
return normalized === "" ? [] : [{
|
|
2439
|
+
type: "literal",
|
|
2440
|
+
value: normalized
|
|
2441
|
+
}];
|
|
2442
|
+
}
|
|
2443
|
+
function createCodec(idLength) {
|
|
2444
|
+
if (!Number.isInteger(idLength) || idLength < ID_LENGTH.min || idLength > ID_LENGTH.max) throw diagnosticError({
|
|
2445
|
+
code: "config-invalid",
|
|
2446
|
+
severity: "error",
|
|
2447
|
+
message: `ids.length must be an integer from ${ID_LENGTH.min} to ${ID_LENGTH.max}, not ${String(idLength)}`
|
|
2448
|
+
});
|
|
2449
|
+
const parsed = /* @__PURE__ */ new Map();
|
|
2450
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
2451
|
+
/** `normalized` is passed when the caller already normalised `source`. */
|
|
2452
|
+
const entry = (source, normalized) => {
|
|
2453
|
+
let found = parsed.get(source);
|
|
2454
|
+
if (found === void 0) {
|
|
2455
|
+
try {
|
|
2456
|
+
found = {
|
|
2457
|
+
ast: parseNormalized(normalized ?? normalizeSource(source)),
|
|
2458
|
+
canonical: null
|
|
2459
|
+
};
|
|
2460
|
+
} catch (error) {
|
|
2461
|
+
if (!isDiagnosticError(error)) throw error;
|
|
2462
|
+
found = { error };
|
|
2463
|
+
}
|
|
2464
|
+
if (parsed.size >= MEMO_LIMIT) parsed.clear();
|
|
2465
|
+
parsed.set(source, found);
|
|
2466
|
+
}
|
|
2467
|
+
return found;
|
|
2468
|
+
};
|
|
2469
|
+
const hashOf = (canonical) => {
|
|
2470
|
+
let found = hashes.get(canonical);
|
|
2471
|
+
if (found === void 0) {
|
|
2472
|
+
found = sourceHash(canonical);
|
|
2473
|
+
if (hashes.size >= MEMO_LIMIT) hashes.clear();
|
|
2474
|
+
hashes.set(canonical, found);
|
|
2475
|
+
}
|
|
2476
|
+
return found;
|
|
2477
|
+
};
|
|
2478
|
+
return {
|
|
2479
|
+
idLength,
|
|
2480
|
+
idScheme: idScheme(idLength),
|
|
2481
|
+
canonicalize(source) {
|
|
2482
|
+
let memo = parsed.get(source);
|
|
2483
|
+
if (memo === void 0) {
|
|
2484
|
+
const normalized = normalizeSource(source);
|
|
2485
|
+
if (isPlainText(normalized)) return normalized;
|
|
2486
|
+
memo = entry(source, normalized);
|
|
2487
|
+
}
|
|
2488
|
+
if ("error" in memo) throw memo.error;
|
|
2489
|
+
return memo.canonical ??= printICU(memo.ast);
|
|
2490
|
+
},
|
|
2491
|
+
parse(source) {
|
|
2492
|
+
const memo = entry(source);
|
|
2493
|
+
if ("error" in memo) throw memo.error;
|
|
2494
|
+
return memo.ast;
|
|
2495
|
+
},
|
|
2496
|
+
sourceHash: hashOf,
|
|
2497
|
+
label(canonical) {
|
|
2498
|
+
return hashOf(canonical).slice(0, idLength);
|
|
2499
|
+
}
|
|
2500
|
+
};
|
|
2501
|
+
}
|
|
2502
|
+
/** `codec.canonicalize(text)`, or null when `text` is not valid ICU; any other error is a bug and is rethrown. */
|
|
2503
|
+
function tryCanonicalize(codec, text) {
|
|
2504
|
+
try {
|
|
2505
|
+
return codec.canonicalize(text);
|
|
2506
|
+
} catch (error) {
|
|
2507
|
+
if (isDiagnosticError(error)) return null;
|
|
2508
|
+
throw error;
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
// src/core/extract/paths.ts
|
|
2512
|
+
/**
|
|
2513
|
+
* Path and file-system helpers the file walk, the opted-in packages, the
|
|
2514
|
+
* project scan and wrapper resolution share, so they agree on what a module
|
|
2515
|
+
* id names and which files are scanned.
|
|
2516
|
+
*/
|
|
2517
|
+
/** Posix form of a native path; `\` never occurs in posix names worth keeping. */
|
|
2518
|
+
function toPosix(path) {
|
|
2519
|
+
return sep === "/" ? path : path.replaceAll("\\", "/");
|
|
2520
|
+
}
|
|
2521
|
+
/** `file` as diagnostics show it: posix, relative to `root` when under it, else absolute. */
|
|
2522
|
+
function displayPath(root, file) {
|
|
2523
|
+
const rel = relative(root, file);
|
|
2524
|
+
if (rel === "" || rel.startsWith("..") || isAbsolute(rel)) return file.split(sep).join("/");
|
|
2525
|
+
return rel.split(sep).join("/");
|
|
2526
|
+
}
|
|
2527
|
+
/** `specifier` without its `?query`. */
|
|
2528
|
+
function withoutQuery(specifier) {
|
|
2529
|
+
const query = specifier.indexOf("?");
|
|
2530
|
+
return query === -1 ? specifier : specifier.slice(0, query);
|
|
2531
|
+
}
|
|
2532
|
+
/** The file a module id names, posix: without a virtual module's `\0` prefix or a `?query`. */
|
|
2533
|
+
function modulePath(id) {
|
|
2534
|
+
return toPosix(withoutQuery(id.startsWith("\0") ? id.slice(1) : id));
|
|
2535
|
+
}
|
|
2536
|
+
/** Declaration files: a `.ts`-family extension, never a call. */
|
|
2537
|
+
const DECLARATION = /\.d\.[cm]?ts$/;
|
|
2538
|
+
/** Whether a file name carries a scanned extension (config extensions are single, like `.tsx`) and is no declaration file. */
|
|
2539
|
+
function hasExtension(name, extensions) {
|
|
2540
|
+
for (const ext of extensions) if (name.endsWith(ext) && name.length > ext.length) return !DECLARATION.test(name);
|
|
2541
|
+
return false;
|
|
2542
|
+
}
|
|
2543
|
+
/** A file or directory that is not there. */
|
|
2544
|
+
const MISSING = /* @__PURE__ */ new Set(["ENOENT", "ENOTDIR"]);
|
|
2545
|
+
/** Whether `error` is a file-system error with one of `codes`. */
|
|
2546
|
+
function isMissing(error, codes = MISSING) {
|
|
2547
|
+
const code = error?.code;
|
|
2548
|
+
return code !== void 0 && codes.has(code);
|
|
2549
|
+
}
|
|
2550
|
+
/** Runs `run` over `items`, at most `limit` at a time. */
|
|
2551
|
+
async function mapLimit(items, limit, run) {
|
|
2552
|
+
let next = 0;
|
|
2553
|
+
const lanes = [];
|
|
2554
|
+
for (let lane = 0; lane < Math.min(limit, items.length); lane++) lanes.push((async () => {
|
|
2555
|
+
while (next < items.length) await run(items[next++]);
|
|
2556
|
+
})());
|
|
2557
|
+
await Promise.all(lanes);
|
|
2558
|
+
}
|
|
2559
|
+
// src/core/icu/structure.ts
|
|
2245
2560
|
/**
|
|
2246
2561
|
* What a translation must keep from its source (INTERNALS §2.1.2).
|
|
2247
2562
|
*
|
|
@@ -2321,8 +2636,7 @@ function use(name, kind, inPlural, structure, required) {
|
|
|
2321
2636
|
function describe(kind) {
|
|
2322
2637
|
return kind === "argument" ? "a plain argument" : `{…, ${kind}}`;
|
|
2323
2638
|
}
|
|
2324
|
-
|
|
2325
|
-
//#region src/core/icu/validate.ts
|
|
2639
|
+
// src/core/icu/validate.ts
|
|
2326
2640
|
/**
|
|
2327
2641
|
* Message validation (INTERNALS §2.1.3, PLAN §5.4), run at extract, compile,
|
|
2328
2642
|
* AI post-check and CI. Parse errors are thrown by the parser; everything
|
|
@@ -2611,5 +2925,4 @@ function positionOf(text, char) {
|
|
|
2611
2925
|
function hex(code) {
|
|
2612
2926
|
return code.toString(16).toUpperCase().padStart(4, "0");
|
|
2613
2927
|
}
|
|
2614
|
-
|
|
2615
|
-
export { isManualId as a, sourceHash as c, parseSource as d, parseICU as f, idScheme as i, isPlainText as l, validateSource as n, isValidManualId as o, quoteStarts as p, validateTranslation as r, parseIdScheme as s, pluralCategories as t, normalizeSource as u };
|
|
2928
|
+
export { parseIdScheme as _, displayPath as a, parseICU as b, mapLimit as c, withoutQuery as d, createCodec as f, isValidManualId as g, isManualId as h, MISSING as i, modulePath as l, idScheme as m, validateSource as n, hasExtension as o, tryCanonicalize as p, validateTranslation as r, isMissing as s, pluralCategories as t, toPosix as u, normalizeSource as v, quoteStarts as x, printICU as y };
|