@formatjs/icu-messageformat-parser 3.2.1 → 3.4.0

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/parser.js CHANGED
@@ -1,918 +1,857 @@
1
- import { __assign } from "tslib";
2
- import { ErrorKind } from './error.js';
3
- import { SKELETON_TYPE, TYPE, } from './types.js';
4
- import { SPACE_SEPARATOR_REGEX } from './regex.generated.js';
5
- import { parseNumberSkeleton, parseNumberSkeletonFromString, parseDateTimeSkeleton, } from '@formatjs/icu-skeleton-parser';
6
- import { getBestPattern } from './date-time-pattern-generator.js';
7
- var SPACE_SEPARATOR_START_REGEX = new RegExp("^".concat(SPACE_SEPARATOR_REGEX.source, "*"));
8
- var SPACE_SEPARATOR_END_REGEX = new RegExp("".concat(SPACE_SEPARATOR_REGEX.source, "*$"));
1
+ import { ErrorKind } from "./error.js";
2
+ import { SKELETON_TYPE, TYPE } from "./types.js";
3
+ import { SPACE_SEPARATOR_REGEX } from "./regex.generated.js";
4
+ import { parseNumberSkeleton, parseNumberSkeletonFromString, parseDateTimeSkeleton } from "@formatjs/icu-skeleton-parser";
5
+ import { getBestPattern } from "./date-time-pattern-generator.js";
6
+ const SPACE_SEPARATOR_START_REGEX = new RegExp(`^${SPACE_SEPARATOR_REGEX.source}*`);
7
+ const SPACE_SEPARATOR_END_REGEX = new RegExp(`${SPACE_SEPARATOR_REGEX.source}*$`);
9
8
  function createLocation(start, end) {
10
- return { start: start, end: end };
9
+ return {
10
+ start,
11
+ end
12
+ };
11
13
  }
12
14
  // #region Ponyfills
13
15
  // Consolidate these variables up top for easier toggling during debugging
14
- var hasNativeFromEntries = !!Object.fromEntries;
15
- var hasTrimStart = !!String.prototype.trimStart;
16
- var hasTrimEnd = !!String.prototype.trimEnd;
17
- var fromEntries =
18
- // native
19
- hasNativeFromEntries
20
- ? Object.fromEntries
21
- : // Ponyfill
22
- function fromEntries(entries) {
23
- var obj = {};
24
- for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
25
- var _a = entries_1[_i], k = _a[0], v = _a[1];
26
- obj[k] = v;
27
- }
28
- return obj;
29
- };
30
- var trimStart = hasTrimStart
31
- ? // Native
32
- function trimStart(s) {
33
- return s.trimStart();
34
- }
35
- : // Ponyfill
36
- function trimStart(s) {
37
- return s.replace(SPACE_SEPARATOR_START_REGEX, '');
38
- };
39
- var trimEnd = hasTrimEnd
40
- ? // Native
41
- function trimEnd(s) {
42
- return s.trimEnd();
43
- }
44
- : // Ponyfill
45
- function trimEnd(s) {
46
- return s.replace(SPACE_SEPARATOR_END_REGEX, '');
47
- };
16
+ const hasNativeFromEntries = !!Object.fromEntries;
17
+ const hasTrimStart = !!String.prototype.trimStart;
18
+ const hasTrimEnd = !!String.prototype.trimEnd;
19
+ const fromEntries = hasNativeFromEntries ? Object.fromEntries : function fromEntries(entries) {
20
+ const obj = {};
21
+ for (const [k, v] of entries) {
22
+ obj[k] = v;
23
+ }
24
+ return obj;
25
+ };
26
+ const trimStart = hasTrimStart ? function trimStart(s) {
27
+ return s.trimStart();
28
+ } : function trimStart(s) {
29
+ return s.replace(SPACE_SEPARATOR_START_REGEX, "");
30
+ };
31
+ const trimEnd = hasTrimEnd ? function trimEnd(s) {
32
+ return s.trimEnd();
33
+ } : function trimEnd(s) {
34
+ return s.replace(SPACE_SEPARATOR_END_REGEX, "");
35
+ };
48
36
  // #endregion
49
- var IDENTIFIER_PREFIX_RE = new RegExp('([^\\p{White_Space}\\p{Pattern_Syntax}]*)', 'yu');
37
+ const IDENTIFIER_PREFIX_RE = new RegExp("([^\\p{White_Space}\\p{Pattern_Syntax}]*)", "yu");
50
38
  function matchIdentifierAtIndex(s, index) {
51
- var _a;
52
- IDENTIFIER_PREFIX_RE.lastIndex = index;
53
- var match = IDENTIFIER_PREFIX_RE.exec(s);
54
- return (_a = match[1]) !== null && _a !== void 0 ? _a : '';
39
+ IDENTIFIER_PREFIX_RE.lastIndex = index;
40
+ const match = IDENTIFIER_PREFIX_RE.exec(s);
41
+ return match[1] ?? "";
42
+ }
43
+ export class Parser {
44
+ message;
45
+ position;
46
+ locale;
47
+ ignoreTag;
48
+ requiresOtherClause;
49
+ shouldParseSkeletons;
50
+ constructor(message, options = {}) {
51
+ this.message = message;
52
+ this.position = {
53
+ offset: 0,
54
+ line: 1,
55
+ column: 1
56
+ };
57
+ this.ignoreTag = !!options.ignoreTag;
58
+ this.locale = options.locale;
59
+ this.requiresOtherClause = !!options.requiresOtherClause;
60
+ this.shouldParseSkeletons = !!options.shouldParseSkeletons;
61
+ }
62
+ parse() {
63
+ if (this.offset() !== 0) {
64
+ throw Error("parser can only be used once");
65
+ }
66
+ return this.parseMessage(0, "", false);
67
+ }
68
+ parseMessage(nestingLevel, parentArgType, expectingCloseTag) {
69
+ let elements = [];
70
+ while (!this.isEOF()) {
71
+ const char = this.char();
72
+ if (char === 123) {
73
+ const result = this.parseArgument(nestingLevel, expectingCloseTag);
74
+ if (result.err) {
75
+ return result;
76
+ }
77
+ elements.push(result.val);
78
+ } else if (char === 125 && nestingLevel > 0) {
79
+ break;
80
+ } else if (char === 35 && (parentArgType === "plural" || parentArgType === "selectordinal")) {
81
+ const position = this.clonePosition();
82
+ this.bump();
83
+ elements.push({
84
+ type: TYPE.pound,
85
+ location: createLocation(position, this.clonePosition())
86
+ });
87
+ } else if (char === 60 && !this.ignoreTag && this.peek() === 47) {
88
+ if (expectingCloseTag) {
89
+ break;
90
+ } else {
91
+ return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(this.clonePosition(), this.clonePosition()));
92
+ }
93
+ } else if (char === 60 && !this.ignoreTag && _isAlpha(this.peek() || 0)) {
94
+ const result = this.parseTag(nestingLevel, parentArgType);
95
+ if (result.err) {
96
+ return result;
97
+ }
98
+ elements.push(result.val);
99
+ } else {
100
+ const result = this.parseLiteral(nestingLevel, parentArgType);
101
+ if (result.err) {
102
+ return result;
103
+ }
104
+ elements.push(result.val);
105
+ }
106
+ }
107
+ return {
108
+ val: elements,
109
+ err: null
110
+ };
111
+ }
112
+ /**
113
+ * A tag name must start with an ASCII lower/upper case letter. The grammar is based on the
114
+ * [custom element name][] except that a dash is NOT always mandatory and uppercase letters
115
+ * are accepted:
116
+ *
117
+ * ```
118
+ * tag ::= "<" tagName (whitespace)* "/>" | "<" tagName (whitespace)* ">" message "</" tagName (whitespace)* ">"
119
+ * tagName ::= [a-z] (PENChar)*
120
+ * PENChar ::=
121
+ * "-" | "." | [0-9] | "_" | [a-z] | [A-Z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
122
+ * [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
123
+ * [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
124
+ * ```
125
+ *
126
+ * [custom element name]: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
127
+ * NOTE: We're a bit more lax here since HTML technically does not allow uppercase HTML element but we do
128
+ * since other tag-based engines like React allow it
129
+ */
130
+ parseTag(nestingLevel, parentArgType) {
131
+ const startPosition = this.clonePosition();
132
+ this.bump();
133
+ const tagName = this.parseTagName();
134
+ this.bumpSpace();
135
+ if (this.bumpIf("/>")) {
136
+ // Self closing tag
137
+ return {
138
+ val: {
139
+ type: TYPE.literal,
140
+ value: `<${tagName}/>`,
141
+ location: createLocation(startPosition, this.clonePosition())
142
+ },
143
+ err: null
144
+ };
145
+ } else if (this.bumpIf(">")) {
146
+ const childrenResult = this.parseMessage(nestingLevel + 1, parentArgType, true);
147
+ if (childrenResult.err) {
148
+ return childrenResult;
149
+ }
150
+ const children = childrenResult.val;
151
+ // Expecting a close tag
152
+ const endTagStartPosition = this.clonePosition();
153
+ if (this.bumpIf("</")) {
154
+ if (this.isEOF() || !_isAlpha(this.char())) {
155
+ return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
156
+ }
157
+ const closingTagNameStartPosition = this.clonePosition();
158
+ const closingTagName = this.parseTagName();
159
+ if (tagName !== closingTagName) {
160
+ return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(closingTagNameStartPosition, this.clonePosition()));
161
+ }
162
+ this.bumpSpace();
163
+ if (!this.bumpIf(">")) {
164
+ return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
165
+ }
166
+ return {
167
+ val: {
168
+ type: TYPE.tag,
169
+ value: tagName,
170
+ children,
171
+ location: createLocation(startPosition, this.clonePosition())
172
+ },
173
+ err: null
174
+ };
175
+ } else {
176
+ return this.error(ErrorKind.UNCLOSED_TAG, createLocation(startPosition, this.clonePosition()));
177
+ }
178
+ } else {
179
+ return this.error(ErrorKind.INVALID_TAG, createLocation(startPosition, this.clonePosition()));
180
+ }
181
+ }
182
+ /**
183
+ * This method assumes that the caller has peeked ahead for the first tag character.
184
+ */
185
+ parseTagName() {
186
+ const startOffset = this.offset();
187
+ this.bump();
188
+ while (!this.isEOF() && _isPotentialElementNameChar(this.char())) {
189
+ this.bump();
190
+ }
191
+ return this.message.slice(startOffset, this.offset());
192
+ }
193
+ parseLiteral(nestingLevel, parentArgType) {
194
+ const start = this.clonePosition();
195
+ let value = "";
196
+ while (true) {
197
+ const parseQuoteResult = this.tryParseQuote(parentArgType);
198
+ if (parseQuoteResult) {
199
+ value += parseQuoteResult;
200
+ continue;
201
+ }
202
+ const parseUnquotedResult = this.tryParseUnquoted(nestingLevel, parentArgType);
203
+ if (parseUnquotedResult) {
204
+ value += parseUnquotedResult;
205
+ continue;
206
+ }
207
+ const parseLeftAngleResult = this.tryParseLeftAngleBracket();
208
+ if (parseLeftAngleResult) {
209
+ value += parseLeftAngleResult;
210
+ continue;
211
+ }
212
+ break;
213
+ }
214
+ const location = createLocation(start, this.clonePosition());
215
+ return {
216
+ val: {
217
+ type: TYPE.literal,
218
+ value,
219
+ location
220
+ },
221
+ err: null
222
+ };
223
+ }
224
+ tryParseLeftAngleBracket() {
225
+ if (!this.isEOF() && this.char() === 60 && (this.ignoreTag || !_isAlphaOrSlash(this.peek() || 0))) {
226
+ this.bump();
227
+ return "<";
228
+ }
229
+ return null;
230
+ }
231
+ /**
232
+ * Starting with ICU 4.8, an ASCII apostrophe only starts quoted text if it immediately precedes
233
+ * a character that requires quoting (that is, "only where needed"), and works the same in
234
+ * nested messages as on the top level of the pattern. The new behavior is otherwise compatible.
235
+ */
236
+ tryParseQuote(parentArgType) {
237
+ if (this.isEOF() || this.char() !== 39) {
238
+ return null;
239
+ }
240
+ // Parse escaped char following the apostrophe, or early return if there is no escaped char.
241
+ // Check if is valid escaped character
242
+ switch (this.peek()) {
243
+ case 39:
244
+ // double quote, should return as a single quote.
245
+ this.bump();
246
+ this.bump();
247
+ return "'";
248
+ case 123:
249
+ case 60:
250
+ case 62:
251
+ case 125: break;
252
+ case 35:
253
+ if (parentArgType === "plural" || parentArgType === "selectordinal") {
254
+ break;
255
+ }
256
+ return null;
257
+ default: return null;
258
+ }
259
+ this.bump();
260
+ const codePoints = [this.char()];
261
+ this.bump();
262
+ // read chars until the optional closing apostrophe is found
263
+ while (!this.isEOF()) {
264
+ const ch = this.char();
265
+ if (ch === 39) {
266
+ if (this.peek() === 39) {
267
+ codePoints.push(39);
268
+ // Bump one more time because we need to skip 2 characters.
269
+ this.bump();
270
+ } else {
271
+ // Optional closing apostrophe.
272
+ this.bump();
273
+ break;
274
+ }
275
+ } else {
276
+ codePoints.push(ch);
277
+ }
278
+ this.bump();
279
+ }
280
+ return String.fromCodePoint(...codePoints);
281
+ }
282
+ tryParseUnquoted(nestingLevel, parentArgType) {
283
+ if (this.isEOF()) {
284
+ return null;
285
+ }
286
+ const ch = this.char();
287
+ if (ch === 60 || ch === 123 || ch === 35 && (parentArgType === "plural" || parentArgType === "selectordinal") || ch === 125 && nestingLevel > 0) {
288
+ return null;
289
+ } else {
290
+ this.bump();
291
+ return String.fromCodePoint(ch);
292
+ }
293
+ }
294
+ parseArgument(nestingLevel, expectingCloseTag) {
295
+ const openingBracePosition = this.clonePosition();
296
+ this.bump();
297
+ this.bumpSpace();
298
+ if (this.isEOF()) {
299
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
300
+ }
301
+ if (this.char() === 125) {
302
+ this.bump();
303
+ return this.error(ErrorKind.EMPTY_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
304
+ }
305
+ // argument name
306
+ let value = this.parseIdentifierIfPossible().value;
307
+ if (!value) {
308
+ return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
309
+ }
310
+ this.bumpSpace();
311
+ if (this.isEOF()) {
312
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
313
+ }
314
+ switch (this.char()) {
315
+ case 125: {
316
+ this.bump();
317
+ return {
318
+ val: {
319
+ type: TYPE.argument,
320
+ value,
321
+ location: createLocation(openingBracePosition, this.clonePosition())
322
+ },
323
+ err: null
324
+ };
325
+ }
326
+ case 44: {
327
+ this.bump();
328
+ this.bumpSpace();
329
+ if (this.isEOF()) {
330
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
331
+ }
332
+ return this.parseArgumentOptions(nestingLevel, expectingCloseTag, value, openingBracePosition);
333
+ }
334
+ default: return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
335
+ }
336
+ }
337
+ /**
338
+ * Advance the parser until the end of the identifier, if it is currently on
339
+ * an identifier character. Return an empty string otherwise.
340
+ */
341
+ parseIdentifierIfPossible() {
342
+ const startingPosition = this.clonePosition();
343
+ const startOffset = this.offset();
344
+ const value = matchIdentifierAtIndex(this.message, startOffset);
345
+ const endOffset = startOffset + value.length;
346
+ this.bumpTo(endOffset);
347
+ const endPosition = this.clonePosition();
348
+ const location = createLocation(startingPosition, endPosition);
349
+ return {
350
+ value,
351
+ location
352
+ };
353
+ }
354
+ parseArgumentOptions(nestingLevel, expectingCloseTag, value, openingBracePosition) {
355
+ // Parse this range:
356
+ // {name, type, style}
357
+ // ^---^
358
+ let typeStartPosition = this.clonePosition();
359
+ let argType = this.parseIdentifierIfPossible().value;
360
+ let typeEndPosition = this.clonePosition();
361
+ switch (argType) {
362
+ case "":
363
+ // Expecting a style string number, date, time, plural, selectordinal, or select.
364
+ return this.error(ErrorKind.EXPECT_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
365
+ case "number":
366
+ case "date":
367
+ case "time": {
368
+ // Parse this range:
369
+ // {name, number, style}
370
+ // ^-------^
371
+ this.bumpSpace();
372
+ let styleAndLocation = null;
373
+ if (this.bumpIf(",")) {
374
+ this.bumpSpace();
375
+ const styleStartPosition = this.clonePosition();
376
+ const result = this.parseSimpleArgStyleIfPossible();
377
+ if (result.err) {
378
+ return result;
379
+ }
380
+ const style = trimEnd(result.val);
381
+ if (style.length === 0) {
382
+ return this.error(ErrorKind.EXPECT_ARGUMENT_STYLE, createLocation(this.clonePosition(), this.clonePosition()));
383
+ }
384
+ const styleLocation = createLocation(styleStartPosition, this.clonePosition());
385
+ styleAndLocation = {
386
+ style,
387
+ styleLocation
388
+ };
389
+ }
390
+ const argCloseResult = this.tryParseArgumentClose(openingBracePosition);
391
+ if (argCloseResult.err) {
392
+ return argCloseResult;
393
+ }
394
+ const location = createLocation(openingBracePosition, this.clonePosition());
395
+ // Extract style or skeleton
396
+ if (styleAndLocation && styleAndLocation.style.startsWith("::")) {
397
+ // Skeleton starts with `::`.
398
+ let skeleton = trimStart(styleAndLocation.style.slice(2));
399
+ if (argType === "number") {
400
+ const result = this.parseNumberSkeletonFromString(skeleton, styleAndLocation.styleLocation);
401
+ if (result.err) {
402
+ return result;
403
+ }
404
+ return {
405
+ val: {
406
+ type: TYPE.number,
407
+ value,
408
+ location,
409
+ style: result.val
410
+ },
411
+ err: null
412
+ };
413
+ } else {
414
+ if (skeleton.length === 0) {
415
+ return this.error(ErrorKind.EXPECT_DATE_TIME_SKELETON, location);
416
+ }
417
+ let dateTimePattern = skeleton;
418
+ // Get "best match" pattern only if locale is passed, if not, let it
419
+ // pass as-is where `parseDateTimeSkeleton()` will throw an error
420
+ // for unsupported patterns.
421
+ if (this.locale) {
422
+ dateTimePattern = getBestPattern(skeleton, this.locale);
423
+ }
424
+ const style = {
425
+ type: SKELETON_TYPE.dateTime,
426
+ pattern: dateTimePattern,
427
+ location: styleAndLocation.styleLocation,
428
+ parsedOptions: this.shouldParseSkeletons ? parseDateTimeSkeleton(dateTimePattern) : {}
429
+ };
430
+ const type = argType === "date" ? TYPE.date : TYPE.time;
431
+ return {
432
+ val: {
433
+ type,
434
+ value,
435
+ location,
436
+ style
437
+ },
438
+ err: null
439
+ };
440
+ }
441
+ }
442
+ // Regular style or no style.
443
+ return {
444
+ val: {
445
+ type: argType === "number" ? TYPE.number : argType === "date" ? TYPE.date : TYPE.time,
446
+ value,
447
+ location,
448
+ style: styleAndLocation?.style ?? null
449
+ },
450
+ err: null
451
+ };
452
+ }
453
+ case "plural":
454
+ case "selectordinal":
455
+ case "select": {
456
+ // Parse this range:
457
+ // {name, plural, options}
458
+ // ^---------^
459
+ const typeEndPosition = this.clonePosition();
460
+ this.bumpSpace();
461
+ if (!this.bumpIf(",")) {
462
+ return this.error(ErrorKind.EXPECT_SELECT_ARGUMENT_OPTIONS, createLocation(typeEndPosition, { ...typeEndPosition }));
463
+ }
464
+ this.bumpSpace();
465
+ // Parse offset:
466
+ // {name, plural, offset:1, options}
467
+ // ^-----^
468
+ //
469
+ // or the first option:
470
+ //
471
+ // {name, plural, one {...} other {...}}
472
+ // ^--^
473
+ let identifierAndLocation = this.parseIdentifierIfPossible();
474
+ let pluralOffset = 0;
475
+ if (argType !== "select" && identifierAndLocation.value === "offset") {
476
+ if (!this.bumpIf(":")) {
477
+ return this.error(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, createLocation(this.clonePosition(), this.clonePosition()));
478
+ }
479
+ this.bumpSpace();
480
+ const result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, ErrorKind.INVALID_PLURAL_ARGUMENT_OFFSET_VALUE);
481
+ if (result.err) {
482
+ return result;
483
+ }
484
+ // Parse another identifier for option parsing
485
+ this.bumpSpace();
486
+ identifierAndLocation = this.parseIdentifierIfPossible();
487
+ pluralOffset = result.val;
488
+ }
489
+ const optionsResult = this.tryParsePluralOrSelectOptions(nestingLevel, argType, expectingCloseTag, identifierAndLocation);
490
+ if (optionsResult.err) {
491
+ return optionsResult;
492
+ }
493
+ const argCloseResult = this.tryParseArgumentClose(openingBracePosition);
494
+ if (argCloseResult.err) {
495
+ return argCloseResult;
496
+ }
497
+ const location = createLocation(openingBracePosition, this.clonePosition());
498
+ if (argType === "select") {
499
+ return {
500
+ val: {
501
+ type: TYPE.select,
502
+ value,
503
+ options: fromEntries(optionsResult.val),
504
+ location
505
+ },
506
+ err: null
507
+ };
508
+ } else {
509
+ return {
510
+ val: {
511
+ type: TYPE.plural,
512
+ value,
513
+ options: fromEntries(optionsResult.val),
514
+ offset: pluralOffset,
515
+ pluralType: argType === "plural" ? "cardinal" : "ordinal",
516
+ location
517
+ },
518
+ err: null
519
+ };
520
+ }
521
+ }
522
+ default: return this.error(ErrorKind.INVALID_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
523
+ }
524
+ }
525
+ tryParseArgumentClose(openingBracePosition) {
526
+ // Parse: {value, number, ::currency/GBP }
527
+ //
528
+ if (this.isEOF() || this.char() !== 125) {
529
+ return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
530
+ }
531
+ this.bump();
532
+ return {
533
+ val: true,
534
+ err: null
535
+ };
536
+ }
537
+ /**
538
+ * See: https://github.com/unicode-org/icu/blob/af7ed1f6d2298013dc303628438ec4abe1f16479/icu4c/source/common/messagepattern.cpp#L659
539
+ */
540
+ parseSimpleArgStyleIfPossible() {
541
+ let nestedBraces = 0;
542
+ const startPosition = this.clonePosition();
543
+ while (!this.isEOF()) {
544
+ const ch = this.char();
545
+ switch (ch) {
546
+ case 39: {
547
+ // Treat apostrophe as quoting but include it in the style part.
548
+ // Find the end of the quoted literal text.
549
+ this.bump();
550
+ let apostrophePosition = this.clonePosition();
551
+ if (!this.bumpUntil("'")) {
552
+ return this.error(ErrorKind.UNCLOSED_QUOTE_IN_ARGUMENT_STYLE, createLocation(apostrophePosition, this.clonePosition()));
553
+ }
554
+ this.bump();
555
+ break;
556
+ }
557
+ case 123: {
558
+ nestedBraces += 1;
559
+ this.bump();
560
+ break;
561
+ }
562
+ case 125: {
563
+ if (nestedBraces > 0) {
564
+ nestedBraces -= 1;
565
+ } else {
566
+ return {
567
+ val: this.message.slice(startPosition.offset, this.offset()),
568
+ err: null
569
+ };
570
+ }
571
+ break;
572
+ }
573
+ default:
574
+ this.bump();
575
+ break;
576
+ }
577
+ }
578
+ return {
579
+ val: this.message.slice(startPosition.offset, this.offset()),
580
+ err: null
581
+ };
582
+ }
583
+ parseNumberSkeletonFromString(skeleton, location) {
584
+ let tokens = [];
585
+ try {
586
+ tokens = parseNumberSkeletonFromString(skeleton);
587
+ } catch {
588
+ return this.error(ErrorKind.INVALID_NUMBER_SKELETON, location);
589
+ }
590
+ return {
591
+ val: {
592
+ type: SKELETON_TYPE.number,
593
+ tokens,
594
+ location,
595
+ parsedOptions: this.shouldParseSkeletons ? parseNumberSkeleton(tokens) : {}
596
+ },
597
+ err: null
598
+ };
599
+ }
600
+ /**
601
+ * @param nesting_level The current nesting level of messages.
602
+ * This can be positive when parsing message fragment in select or plural argument options.
603
+ * @param parent_arg_type The parent argument's type.
604
+ * @param parsed_first_identifier If provided, this is the first identifier-like selector of
605
+ * the argument. It is a by-product of a previous parsing attempt.
606
+ * @param expecting_close_tag If true, this message is directly or indirectly nested inside
607
+ * between a pair of opening and closing tags. The nested message will not parse beyond
608
+ * the closing tag boundary.
609
+ */
610
+ tryParsePluralOrSelectOptions(nestingLevel, parentArgType, expectCloseTag, parsedFirstIdentifier) {
611
+ let hasOtherClause = false;
612
+ const options = [];
613
+ const parsedSelectors = new Set();
614
+ let { value: selector, location: selectorLocation } = parsedFirstIdentifier;
615
+ // Parse:
616
+ // one {one apple}
617
+ // ^--^
618
+ while (true) {
619
+ if (selector.length === 0) {
620
+ const startPosition = this.clonePosition();
621
+ if (parentArgType !== "select" && this.bumpIf("=")) {
622
+ // Try parse `={number}` selector
623
+ const result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, ErrorKind.INVALID_PLURAL_ARGUMENT_SELECTOR);
624
+ if (result.err) {
625
+ return result;
626
+ }
627
+ selectorLocation = createLocation(startPosition, this.clonePosition());
628
+ selector = this.message.slice(startPosition.offset, this.offset());
629
+ } else {
630
+ break;
631
+ }
632
+ }
633
+ // Duplicate selector clauses
634
+ if (parsedSelectors.has(selector)) {
635
+ return this.error(parentArgType === "select" ? ErrorKind.DUPLICATE_SELECT_ARGUMENT_SELECTOR : ErrorKind.DUPLICATE_PLURAL_ARGUMENT_SELECTOR, selectorLocation);
636
+ }
637
+ if (selector === "other") {
638
+ hasOtherClause = true;
639
+ }
640
+ // Parse:
641
+ // one {one apple}
642
+ // ^----------^
643
+ this.bumpSpace();
644
+ const openingBracePosition = this.clonePosition();
645
+ if (!this.bumpIf("{")) {
646
+ return this.error(parentArgType === "select" ? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT : ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT, createLocation(this.clonePosition(), this.clonePosition()));
647
+ }
648
+ const fragmentResult = this.parseMessage(nestingLevel + 1, parentArgType, expectCloseTag);
649
+ if (fragmentResult.err) {
650
+ return fragmentResult;
651
+ }
652
+ const argCloseResult = this.tryParseArgumentClose(openingBracePosition);
653
+ if (argCloseResult.err) {
654
+ return argCloseResult;
655
+ }
656
+ options.push([selector, {
657
+ value: fragmentResult.val,
658
+ location: createLocation(openingBracePosition, this.clonePosition())
659
+ }]);
660
+ // Keep track of the existing selectors
661
+ parsedSelectors.add(selector);
662
+ // Prep next selector clause.
663
+ this.bumpSpace();
664
+ ({value: selector, location: selectorLocation} = this.parseIdentifierIfPossible());
665
+ }
666
+ if (options.length === 0) {
667
+ return this.error(parentArgType === "select" ? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR : ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, createLocation(this.clonePosition(), this.clonePosition()));
668
+ }
669
+ if (this.requiresOtherClause && !hasOtherClause) {
670
+ return this.error(ErrorKind.MISSING_OTHER_CLAUSE, createLocation(this.clonePosition(), this.clonePosition()));
671
+ }
672
+ return {
673
+ val: options,
674
+ err: null
675
+ };
676
+ }
677
+ tryParseDecimalInteger(expectNumberError, invalidNumberError) {
678
+ let sign = 1;
679
+ const startingPosition = this.clonePosition();
680
+ if (this.bumpIf("+")) {} else if (this.bumpIf("-")) {
681
+ sign = -1;
682
+ }
683
+ let hasDigits = false;
684
+ let decimal = 0;
685
+ while (!this.isEOF()) {
686
+ const ch = this.char();
687
+ if (ch >= 48 && ch <= 57) {
688
+ hasDigits = true;
689
+ decimal = decimal * 10 + (ch - 48);
690
+ this.bump();
691
+ } else {
692
+ break;
693
+ }
694
+ }
695
+ const location = createLocation(startingPosition, this.clonePosition());
696
+ if (!hasDigits) {
697
+ return this.error(expectNumberError, location);
698
+ }
699
+ decimal *= sign;
700
+ if (!Number.isSafeInteger(decimal)) {
701
+ return this.error(invalidNumberError, location);
702
+ }
703
+ return {
704
+ val: decimal,
705
+ err: null
706
+ };
707
+ }
708
+ offset() {
709
+ return this.position.offset;
710
+ }
711
+ isEOF() {
712
+ return this.offset() === this.message.length;
713
+ }
714
+ clonePosition() {
715
+ // This is much faster than `Object.assign` or spread.
716
+ return {
717
+ offset: this.position.offset,
718
+ line: this.position.line,
719
+ column: this.position.column
720
+ };
721
+ }
722
+ /**
723
+ * Return the code point at the current position of the parser.
724
+ * Throws if the index is out of bound.
725
+ */
726
+ char() {
727
+ const offset = this.position.offset;
728
+ if (offset >= this.message.length) {
729
+ throw Error("out of bound");
730
+ }
731
+ const code = this.message.codePointAt(offset);
732
+ if (code === undefined) {
733
+ throw Error(`Offset ${offset} is at invalid UTF-16 code unit boundary`);
734
+ }
735
+ return code;
736
+ }
737
+ error(kind, location) {
738
+ return {
739
+ val: null,
740
+ err: {
741
+ kind,
742
+ message: this.message,
743
+ location
744
+ }
745
+ };
746
+ }
747
+ /** Bump the parser to the next UTF-16 code unit. */
748
+ bump() {
749
+ if (this.isEOF()) {
750
+ return;
751
+ }
752
+ const code = this.char();
753
+ if (code === 10) {
754
+ this.position.line += 1;
755
+ this.position.column = 1;
756
+ this.position.offset += 1;
757
+ } else {
758
+ this.position.column += 1;
759
+ // 0 ~ 0x10000 -> unicode BMP, otherwise skip the surrogate pair.
760
+ this.position.offset += code < 65536 ? 1 : 2;
761
+ }
762
+ }
763
+ /**
764
+ * If the substring starting at the current position of the parser has
765
+ * the given prefix, then bump the parser to the character immediately
766
+ * following the prefix and return true. Otherwise, don't bump the parser
767
+ * and return false.
768
+ */
769
+ bumpIf(prefix) {
770
+ if (this.message.startsWith(prefix, this.offset())) {
771
+ for (let i = 0; i < prefix.length; i++) {
772
+ this.bump();
773
+ }
774
+ return true;
775
+ }
776
+ return false;
777
+ }
778
+ /**
779
+ * Bump the parser until the pattern character is found and return `true`.
780
+ * Otherwise bump to the end of the file and return `false`.
781
+ */
782
+ bumpUntil(pattern) {
783
+ const currentOffset = this.offset();
784
+ const index = this.message.indexOf(pattern, currentOffset);
785
+ if (index >= 0) {
786
+ this.bumpTo(index);
787
+ return true;
788
+ } else {
789
+ this.bumpTo(this.message.length);
790
+ return false;
791
+ }
792
+ }
793
+ /**
794
+ * Bump the parser to the target offset.
795
+ * If target offset is beyond the end of the input, bump the parser to the end of the input.
796
+ */
797
+ bumpTo(targetOffset) {
798
+ if (this.offset() > targetOffset) {
799
+ throw Error(`targetOffset ${targetOffset} must be greater than or equal to the current offset ${this.offset()}`);
800
+ }
801
+ targetOffset = Math.min(targetOffset, this.message.length);
802
+ while (true) {
803
+ const offset = this.offset();
804
+ if (offset === targetOffset) {
805
+ break;
806
+ }
807
+ if (offset > targetOffset) {
808
+ throw Error(`targetOffset ${targetOffset} is at invalid UTF-16 code unit boundary`);
809
+ }
810
+ this.bump();
811
+ if (this.isEOF()) {
812
+ break;
813
+ }
814
+ }
815
+ }
816
+ /** advance the parser through all whitespace to the next non-whitespace code unit. */
817
+ bumpSpace() {
818
+ while (!this.isEOF() && _isWhiteSpace(this.char())) {
819
+ this.bump();
820
+ }
821
+ }
822
+ /**
823
+ * Peek at the *next* Unicode codepoint in the input without advancing the parser.
824
+ * If the input has been exhausted, then this returns null.
825
+ */
826
+ peek() {
827
+ if (this.isEOF()) {
828
+ return null;
829
+ }
830
+ const code = this.char();
831
+ const offset = this.offset();
832
+ const nextCode = this.message.charCodeAt(offset + (code >= 65536 ? 2 : 1));
833
+ return nextCode ?? null;
834
+ }
55
835
  }
56
- var Parser = /** @class */ (function () {
57
- function Parser(message, options) {
58
- if (options === void 0) { options = {}; }
59
- this.message = message;
60
- this.position = { offset: 0, line: 1, column: 1 };
61
- this.ignoreTag = !!options.ignoreTag;
62
- this.locale = options.locale;
63
- this.requiresOtherClause = !!options.requiresOtherClause;
64
- this.shouldParseSkeletons = !!options.shouldParseSkeletons;
65
- }
66
- Parser.prototype.parse = function () {
67
- if (this.offset() !== 0) {
68
- throw Error('parser can only be used once');
69
- }
70
- return this.parseMessage(0, '', false);
71
- };
72
- Parser.prototype.parseMessage = function (nestingLevel, parentArgType, expectingCloseTag) {
73
- var elements = [];
74
- while (!this.isEOF()) {
75
- var char = this.char();
76
- if (char === 123 /* `{` */) {
77
- var result = this.parseArgument(nestingLevel, expectingCloseTag);
78
- if (result.err) {
79
- return result;
80
- }
81
- elements.push(result.val);
82
- }
83
- else if (char === 125 /* `}` */ && nestingLevel > 0) {
84
- break;
85
- }
86
- else if (char === 35 /* `#` */ &&
87
- (parentArgType === 'plural' || parentArgType === 'selectordinal')) {
88
- var position = this.clonePosition();
89
- this.bump();
90
- elements.push({
91
- type: TYPE.pound,
92
- location: createLocation(position, this.clonePosition()),
93
- });
94
- }
95
- else if (char === 60 /* `<` */ &&
96
- !this.ignoreTag &&
97
- this.peek() === 47 // char code for '/'
98
- ) {
99
- if (expectingCloseTag) {
100
- break;
101
- }
102
- else {
103
- return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(this.clonePosition(), this.clonePosition()));
104
- }
105
- }
106
- else if (char === 60 /* `<` */ &&
107
- !this.ignoreTag &&
108
- _isAlpha(this.peek() || 0)) {
109
- var result = this.parseTag(nestingLevel, parentArgType);
110
- if (result.err) {
111
- return result;
112
- }
113
- elements.push(result.val);
114
- }
115
- else {
116
- var result = this.parseLiteral(nestingLevel, parentArgType);
117
- if (result.err) {
118
- return result;
119
- }
120
- elements.push(result.val);
121
- }
122
- }
123
- return { val: elements, err: null };
124
- };
125
- /**
126
- * A tag name must start with an ASCII lower/upper case letter. The grammar is based on the
127
- * [custom element name][] except that a dash is NOT always mandatory and uppercase letters
128
- * are accepted:
129
- *
130
- * ```
131
- * tag ::= "<" tagName (whitespace)* "/>" | "<" tagName (whitespace)* ">" message "</" tagName (whitespace)* ">"
132
- * tagName ::= [a-z] (PENChar)*
133
- * PENChar ::=
134
- * "-" | "." | [0-9] | "_" | [a-z] | [A-Z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
135
- * [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
136
- * [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
137
- * ```
138
- *
139
- * [custom element name]: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
140
- * NOTE: We're a bit more lax here since HTML technically does not allow uppercase HTML element but we do
141
- * since other tag-based engines like React allow it
142
- */
143
- Parser.prototype.parseTag = function (nestingLevel, parentArgType) {
144
- var startPosition = this.clonePosition();
145
- this.bump(); // `<`
146
- var tagName = this.parseTagName();
147
- this.bumpSpace();
148
- if (this.bumpIf('/>')) {
149
- // Self closing tag
150
- return {
151
- val: {
152
- type: TYPE.literal,
153
- value: "<".concat(tagName, "/>"),
154
- location: createLocation(startPosition, this.clonePosition()),
155
- },
156
- err: null,
157
- };
158
- }
159
- else if (this.bumpIf('>')) {
160
- var childrenResult = this.parseMessage(nestingLevel + 1, parentArgType, true);
161
- if (childrenResult.err) {
162
- return childrenResult;
163
- }
164
- var children = childrenResult.val;
165
- // Expecting a close tag
166
- var endTagStartPosition = this.clonePosition();
167
- if (this.bumpIf('</')) {
168
- if (this.isEOF() || !_isAlpha(this.char())) {
169
- return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
170
- }
171
- var closingTagNameStartPosition = this.clonePosition();
172
- var closingTagName = this.parseTagName();
173
- if (tagName !== closingTagName) {
174
- return this.error(ErrorKind.UNMATCHED_CLOSING_TAG, createLocation(closingTagNameStartPosition, this.clonePosition()));
175
- }
176
- this.bumpSpace();
177
- if (!this.bumpIf('>')) {
178
- return this.error(ErrorKind.INVALID_TAG, createLocation(endTagStartPosition, this.clonePosition()));
179
- }
180
- return {
181
- val: {
182
- type: TYPE.tag,
183
- value: tagName,
184
- children: children,
185
- location: createLocation(startPosition, this.clonePosition()),
186
- },
187
- err: null,
188
- };
189
- }
190
- else {
191
- return this.error(ErrorKind.UNCLOSED_TAG, createLocation(startPosition, this.clonePosition()));
192
- }
193
- }
194
- else {
195
- return this.error(ErrorKind.INVALID_TAG, createLocation(startPosition, this.clonePosition()));
196
- }
197
- };
198
- /**
199
- * This method assumes that the caller has peeked ahead for the first tag character.
200
- */
201
- Parser.prototype.parseTagName = function () {
202
- var startOffset = this.offset();
203
- this.bump(); // the first tag name character
204
- while (!this.isEOF() && _isPotentialElementNameChar(this.char())) {
205
- this.bump();
206
- }
207
- return this.message.slice(startOffset, this.offset());
208
- };
209
- Parser.prototype.parseLiteral = function (nestingLevel, parentArgType) {
210
- var start = this.clonePosition();
211
- var value = '';
212
- while (true) {
213
- var parseQuoteResult = this.tryParseQuote(parentArgType);
214
- if (parseQuoteResult) {
215
- value += parseQuoteResult;
216
- continue;
217
- }
218
- var parseUnquotedResult = this.tryParseUnquoted(nestingLevel, parentArgType);
219
- if (parseUnquotedResult) {
220
- value += parseUnquotedResult;
221
- continue;
222
- }
223
- var parseLeftAngleResult = this.tryParseLeftAngleBracket();
224
- if (parseLeftAngleResult) {
225
- value += parseLeftAngleResult;
226
- continue;
227
- }
228
- break;
229
- }
230
- var location = createLocation(start, this.clonePosition());
231
- return {
232
- val: { type: TYPE.literal, value: value, location: location },
233
- err: null,
234
- };
235
- };
236
- Parser.prototype.tryParseLeftAngleBracket = function () {
237
- if (!this.isEOF() &&
238
- this.char() === 60 /* `<` */ &&
239
- (this.ignoreTag ||
240
- // If at the opening tag or closing tag position, bail.
241
- !_isAlphaOrSlash(this.peek() || 0))) {
242
- this.bump(); // `<`
243
- return '<';
244
- }
245
- return null;
246
- };
247
- /**
248
- * Starting with ICU 4.8, an ASCII apostrophe only starts quoted text if it immediately precedes
249
- * a character that requires quoting (that is, "only where needed"), and works the same in
250
- * nested messages as on the top level of the pattern. The new behavior is otherwise compatible.
251
- */
252
- Parser.prototype.tryParseQuote = function (parentArgType) {
253
- if (this.isEOF() || this.char() !== 39 /* `'` */) {
254
- return null;
255
- }
256
- // Parse escaped char following the apostrophe, or early return if there is no escaped char.
257
- // Check if is valid escaped character
258
- switch (this.peek()) {
259
- case 39 /* `'` */:
260
- // double quote, should return as a single quote.
261
- this.bump();
262
- this.bump();
263
- return "'";
264
- // '{', '<', '>', '}'
265
- case 123:
266
- case 60:
267
- case 62:
268
- case 125:
269
- break;
270
- case 35: // '#'
271
- if (parentArgType === 'plural' || parentArgType === 'selectordinal') {
272
- break;
273
- }
274
- return null;
275
- default:
276
- return null;
277
- }
278
- this.bump(); // apostrophe
279
- var codePoints = [this.char()]; // escaped char
280
- this.bump();
281
- // read chars until the optional closing apostrophe is found
282
- while (!this.isEOF()) {
283
- var ch = this.char();
284
- if (ch === 39 /* `'` */) {
285
- if (this.peek() === 39 /* `'` */) {
286
- codePoints.push(39);
287
- // Bump one more time because we need to skip 2 characters.
288
- this.bump();
289
- }
290
- else {
291
- // Optional closing apostrophe.
292
- this.bump();
293
- break;
294
- }
295
- }
296
- else {
297
- codePoints.push(ch);
298
- }
299
- this.bump();
300
- }
301
- return String.fromCodePoint.apply(String, codePoints);
302
- };
303
- Parser.prototype.tryParseUnquoted = function (nestingLevel, parentArgType) {
304
- if (this.isEOF()) {
305
- return null;
306
- }
307
- var ch = this.char();
308
- if (ch === 60 /* `<` */ ||
309
- ch === 123 /* `{` */ ||
310
- (ch === 35 /* `#` */ &&
311
- (parentArgType === 'plural' || parentArgType === 'selectordinal')) ||
312
- (ch === 125 /* `}` */ && nestingLevel > 0)) {
313
- return null;
314
- }
315
- else {
316
- this.bump();
317
- return String.fromCodePoint(ch);
318
- }
319
- };
320
- Parser.prototype.parseArgument = function (nestingLevel, expectingCloseTag) {
321
- var openingBracePosition = this.clonePosition();
322
- this.bump(); // `{`
323
- this.bumpSpace();
324
- if (this.isEOF()) {
325
- return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
326
- }
327
- if (this.char() === 125 /* `}` */) {
328
- this.bump();
329
- return this.error(ErrorKind.EMPTY_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
330
- }
331
- // argument name
332
- var value = this.parseIdentifierIfPossible().value;
333
- if (!value) {
334
- return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
335
- }
336
- this.bumpSpace();
337
- if (this.isEOF()) {
338
- return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
339
- }
340
- switch (this.char()) {
341
- // Simple argument: `{name}`
342
- case 125 /* `}` */: {
343
- this.bump(); // `}`
344
- return {
345
- val: {
346
- type: TYPE.argument,
347
- // value does not include the opening and closing braces.
348
- value: value,
349
- location: createLocation(openingBracePosition, this.clonePosition()),
350
- },
351
- err: null,
352
- };
353
- }
354
- // Argument with options: `{name, format, ...}`
355
- case 44 /* `,` */: {
356
- this.bump(); // `,`
357
- this.bumpSpace();
358
- if (this.isEOF()) {
359
- return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
360
- }
361
- return this.parseArgumentOptions(nestingLevel, expectingCloseTag, value, openingBracePosition);
362
- }
363
- default:
364
- return this.error(ErrorKind.MALFORMED_ARGUMENT, createLocation(openingBracePosition, this.clonePosition()));
365
- }
366
- };
367
- /**
368
- * Advance the parser until the end of the identifier, if it is currently on
369
- * an identifier character. Return an empty string otherwise.
370
- */
371
- Parser.prototype.parseIdentifierIfPossible = function () {
372
- var startingPosition = this.clonePosition();
373
- var startOffset = this.offset();
374
- var value = matchIdentifierAtIndex(this.message, startOffset);
375
- var endOffset = startOffset + value.length;
376
- this.bumpTo(endOffset);
377
- var endPosition = this.clonePosition();
378
- var location = createLocation(startingPosition, endPosition);
379
- return { value: value, location: location };
380
- };
381
- Parser.prototype.parseArgumentOptions = function (nestingLevel, expectingCloseTag, value, openingBracePosition) {
382
- var _a;
383
- // Parse this range:
384
- // {name, type, style}
385
- // ^---^
386
- var typeStartPosition = this.clonePosition();
387
- var argType = this.parseIdentifierIfPossible().value;
388
- var typeEndPosition = this.clonePosition();
389
- switch (argType) {
390
- case '':
391
- // Expecting a style string number, date, time, plural, selectordinal, or select.
392
- return this.error(ErrorKind.EXPECT_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
393
- case 'number':
394
- case 'date':
395
- case 'time': {
396
- // Parse this range:
397
- // {name, number, style}
398
- // ^-------^
399
- this.bumpSpace();
400
- var styleAndLocation = null;
401
- if (this.bumpIf(',')) {
402
- this.bumpSpace();
403
- var styleStartPosition = this.clonePosition();
404
- var result = this.parseSimpleArgStyleIfPossible();
405
- if (result.err) {
406
- return result;
407
- }
408
- var style = trimEnd(result.val);
409
- if (style.length === 0) {
410
- return this.error(ErrorKind.EXPECT_ARGUMENT_STYLE, createLocation(this.clonePosition(), this.clonePosition()));
411
- }
412
- var styleLocation = createLocation(styleStartPosition, this.clonePosition());
413
- styleAndLocation = { style: style, styleLocation: styleLocation };
414
- }
415
- var argCloseResult = this.tryParseArgumentClose(openingBracePosition);
416
- if (argCloseResult.err) {
417
- return argCloseResult;
418
- }
419
- var location_1 = createLocation(openingBracePosition, this.clonePosition());
420
- // Extract style or skeleton
421
- if (styleAndLocation && styleAndLocation.style.startsWith('::')) {
422
- // Skeleton starts with `::`.
423
- var skeleton = trimStart(styleAndLocation.style.slice(2));
424
- if (argType === 'number') {
425
- var result = this.parseNumberSkeletonFromString(skeleton, styleAndLocation.styleLocation);
426
- if (result.err) {
427
- return result;
428
- }
429
- return {
430
- val: { type: TYPE.number, value: value, location: location_1, style: result.val },
431
- err: null,
432
- };
433
- }
434
- else {
435
- if (skeleton.length === 0) {
436
- return this.error(ErrorKind.EXPECT_DATE_TIME_SKELETON, location_1);
437
- }
438
- var dateTimePattern = skeleton;
439
- // Get "best match" pattern only if locale is passed, if not, let it
440
- // pass as-is where `parseDateTimeSkeleton()` will throw an error
441
- // for unsupported patterns.
442
- if (this.locale) {
443
- dateTimePattern = getBestPattern(skeleton, this.locale);
444
- }
445
- var style = {
446
- type: SKELETON_TYPE.dateTime,
447
- pattern: dateTimePattern,
448
- location: styleAndLocation.styleLocation,
449
- parsedOptions: this.shouldParseSkeletons
450
- ? parseDateTimeSkeleton(dateTimePattern)
451
- : {},
452
- };
453
- var type = argType === 'date' ? TYPE.date : TYPE.time;
454
- return {
455
- val: { type: type, value: value, location: location_1, style: style },
456
- err: null,
457
- };
458
- }
459
- }
460
- // Regular style or no style.
461
- return {
462
- val: {
463
- type: argType === 'number'
464
- ? TYPE.number
465
- : argType === 'date'
466
- ? TYPE.date
467
- : TYPE.time,
468
- value: value,
469
- location: location_1,
470
- style: (_a = styleAndLocation === null || styleAndLocation === void 0 ? void 0 : styleAndLocation.style) !== null && _a !== void 0 ? _a : null,
471
- },
472
- err: null,
473
- };
474
- }
475
- case 'plural':
476
- case 'selectordinal':
477
- case 'select': {
478
- // Parse this range:
479
- // {name, plural, options}
480
- // ^---------^
481
- var typeEndPosition_1 = this.clonePosition();
482
- this.bumpSpace();
483
- if (!this.bumpIf(',')) {
484
- return this.error(ErrorKind.EXPECT_SELECT_ARGUMENT_OPTIONS, createLocation(typeEndPosition_1, __assign({}, typeEndPosition_1)));
485
- }
486
- this.bumpSpace();
487
- // Parse offset:
488
- // {name, plural, offset:1, options}
489
- // ^-----^
490
- //
491
- // or the first option:
492
- //
493
- // {name, plural, one {...} other {...}}
494
- // ^--^
495
- var identifierAndLocation = this.parseIdentifierIfPossible();
496
- var pluralOffset = 0;
497
- if (argType !== 'select' && identifierAndLocation.value === 'offset') {
498
- if (!this.bumpIf(':')) {
499
- return this.error(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, createLocation(this.clonePosition(), this.clonePosition()));
500
- }
501
- this.bumpSpace();
502
- var result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE, ErrorKind.INVALID_PLURAL_ARGUMENT_OFFSET_VALUE);
503
- if (result.err) {
504
- return result;
505
- }
506
- // Parse another identifier for option parsing
507
- this.bumpSpace();
508
- identifierAndLocation = this.parseIdentifierIfPossible();
509
- pluralOffset = result.val;
510
- }
511
- var optionsResult = this.tryParsePluralOrSelectOptions(nestingLevel, argType, expectingCloseTag, identifierAndLocation);
512
- if (optionsResult.err) {
513
- return optionsResult;
514
- }
515
- var argCloseResult = this.tryParseArgumentClose(openingBracePosition);
516
- if (argCloseResult.err) {
517
- return argCloseResult;
518
- }
519
- var location_2 = createLocation(openingBracePosition, this.clonePosition());
520
- if (argType === 'select') {
521
- return {
522
- val: {
523
- type: TYPE.select,
524
- value: value,
525
- options: fromEntries(optionsResult.val),
526
- location: location_2,
527
- },
528
- err: null,
529
- };
530
- }
531
- else {
532
- return {
533
- val: {
534
- type: TYPE.plural,
535
- value: value,
536
- options: fromEntries(optionsResult.val),
537
- offset: pluralOffset,
538
- pluralType: argType === 'plural' ? 'cardinal' : 'ordinal',
539
- location: location_2,
540
- },
541
- err: null,
542
- };
543
- }
544
- }
545
- default:
546
- return this.error(ErrorKind.INVALID_ARGUMENT_TYPE, createLocation(typeStartPosition, typeEndPosition));
547
- }
548
- };
549
- Parser.prototype.tryParseArgumentClose = function (openingBracePosition) {
550
- // Parse: {value, number, ::currency/GBP }
551
- //
552
- if (this.isEOF() || this.char() !== 125 /* `}` */) {
553
- return this.error(ErrorKind.EXPECT_ARGUMENT_CLOSING_BRACE, createLocation(openingBracePosition, this.clonePosition()));
554
- }
555
- this.bump(); // `}`
556
- return { val: true, err: null };
557
- };
558
- /**
559
- * See: https://github.com/unicode-org/icu/blob/af7ed1f6d2298013dc303628438ec4abe1f16479/icu4c/source/common/messagepattern.cpp#L659
560
- */
561
- Parser.prototype.parseSimpleArgStyleIfPossible = function () {
562
- var nestedBraces = 0;
563
- var startPosition = this.clonePosition();
564
- while (!this.isEOF()) {
565
- var ch = this.char();
566
- switch (ch) {
567
- case 39 /* `'` */: {
568
- // Treat apostrophe as quoting but include it in the style part.
569
- // Find the end of the quoted literal text.
570
- this.bump();
571
- var apostrophePosition = this.clonePosition();
572
- if (!this.bumpUntil("'")) {
573
- return this.error(ErrorKind.UNCLOSED_QUOTE_IN_ARGUMENT_STYLE, createLocation(apostrophePosition, this.clonePosition()));
574
- }
575
- this.bump();
576
- break;
577
- }
578
- case 123 /* `{` */: {
579
- nestedBraces += 1;
580
- this.bump();
581
- break;
582
- }
583
- case 125 /* `}` */: {
584
- if (nestedBraces > 0) {
585
- nestedBraces -= 1;
586
- }
587
- else {
588
- return {
589
- val: this.message.slice(startPosition.offset, this.offset()),
590
- err: null,
591
- };
592
- }
593
- break;
594
- }
595
- default:
596
- this.bump();
597
- break;
598
- }
599
- }
600
- return {
601
- val: this.message.slice(startPosition.offset, this.offset()),
602
- err: null,
603
- };
604
- };
605
- Parser.prototype.parseNumberSkeletonFromString = function (skeleton, location) {
606
- var tokens = [];
607
- try {
608
- tokens = parseNumberSkeletonFromString(skeleton);
609
- }
610
- catch (_a) {
611
- return this.error(ErrorKind.INVALID_NUMBER_SKELETON, location);
612
- }
613
- return {
614
- val: {
615
- type: SKELETON_TYPE.number,
616
- tokens: tokens,
617
- location: location,
618
- parsedOptions: this.shouldParseSkeletons
619
- ? parseNumberSkeleton(tokens)
620
- : {},
621
- },
622
- err: null,
623
- };
624
- };
625
- /**
626
- * @param nesting_level The current nesting level of messages.
627
- * This can be positive when parsing message fragment in select or plural argument options.
628
- * @param parent_arg_type The parent argument's type.
629
- * @param parsed_first_identifier If provided, this is the first identifier-like selector of
630
- * the argument. It is a by-product of a previous parsing attempt.
631
- * @param expecting_close_tag If true, this message is directly or indirectly nested inside
632
- * between a pair of opening and closing tags. The nested message will not parse beyond
633
- * the closing tag boundary.
634
- */
635
- Parser.prototype.tryParsePluralOrSelectOptions = function (nestingLevel, parentArgType, expectCloseTag, parsedFirstIdentifier) {
636
- var _a;
637
- var hasOtherClause = false;
638
- var options = [];
639
- var parsedSelectors = new Set();
640
- var selector = parsedFirstIdentifier.value, selectorLocation = parsedFirstIdentifier.location;
641
- // Parse:
642
- // one {one apple}
643
- // ^--^
644
- while (true) {
645
- if (selector.length === 0) {
646
- var startPosition = this.clonePosition();
647
- if (parentArgType !== 'select' && this.bumpIf('=')) {
648
- // Try parse `={number}` selector
649
- var result = this.tryParseDecimalInteger(ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, ErrorKind.INVALID_PLURAL_ARGUMENT_SELECTOR);
650
- if (result.err) {
651
- return result;
652
- }
653
- selectorLocation = createLocation(startPosition, this.clonePosition());
654
- selector = this.message.slice(startPosition.offset, this.offset());
655
- }
656
- else {
657
- break;
658
- }
659
- }
660
- // Duplicate selector clauses
661
- if (parsedSelectors.has(selector)) {
662
- return this.error(parentArgType === 'select'
663
- ? ErrorKind.DUPLICATE_SELECT_ARGUMENT_SELECTOR
664
- : ErrorKind.DUPLICATE_PLURAL_ARGUMENT_SELECTOR, selectorLocation);
665
- }
666
- if (selector === 'other') {
667
- hasOtherClause = true;
668
- }
669
- // Parse:
670
- // one {one apple}
671
- // ^----------^
672
- this.bumpSpace();
673
- var openingBracePosition = this.clonePosition();
674
- if (!this.bumpIf('{')) {
675
- return this.error(parentArgType === 'select'
676
- ? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT
677
- : ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT, createLocation(this.clonePosition(), this.clonePosition()));
678
- }
679
- var fragmentResult = this.parseMessage(nestingLevel + 1, parentArgType, expectCloseTag);
680
- if (fragmentResult.err) {
681
- return fragmentResult;
682
- }
683
- var argCloseResult = this.tryParseArgumentClose(openingBracePosition);
684
- if (argCloseResult.err) {
685
- return argCloseResult;
686
- }
687
- options.push([
688
- selector,
689
- {
690
- value: fragmentResult.val,
691
- location: createLocation(openingBracePosition, this.clonePosition()),
692
- },
693
- ]);
694
- // Keep track of the existing selectors
695
- parsedSelectors.add(selector);
696
- // Prep next selector clause.
697
- this.bumpSpace();
698
- (_a = this.parseIdentifierIfPossible(), selector = _a.value, selectorLocation = _a.location);
699
- }
700
- if (options.length === 0) {
701
- return this.error(parentArgType === 'select'
702
- ? ErrorKind.EXPECT_SELECT_ARGUMENT_SELECTOR
703
- : ErrorKind.EXPECT_PLURAL_ARGUMENT_SELECTOR, createLocation(this.clonePosition(), this.clonePosition()));
704
- }
705
- if (this.requiresOtherClause && !hasOtherClause) {
706
- return this.error(ErrorKind.MISSING_OTHER_CLAUSE, createLocation(this.clonePosition(), this.clonePosition()));
707
- }
708
- return { val: options, err: null };
709
- };
710
- Parser.prototype.tryParseDecimalInteger = function (expectNumberError, invalidNumberError) {
711
- var sign = 1;
712
- var startingPosition = this.clonePosition();
713
- if (this.bumpIf('+')) {
714
- }
715
- else if (this.bumpIf('-')) {
716
- sign = -1;
717
- }
718
- var hasDigits = false;
719
- var decimal = 0;
720
- while (!this.isEOF()) {
721
- var ch = this.char();
722
- if (ch >= 48 /* `0` */ && ch <= 57 /* `9` */) {
723
- hasDigits = true;
724
- decimal = decimal * 10 + (ch - 48);
725
- this.bump();
726
- }
727
- else {
728
- break;
729
- }
730
- }
731
- var location = createLocation(startingPosition, this.clonePosition());
732
- if (!hasDigits) {
733
- return this.error(expectNumberError, location);
734
- }
735
- decimal *= sign;
736
- if (!Number.isSafeInteger(decimal)) {
737
- return this.error(invalidNumberError, location);
738
- }
739
- return { val: decimal, err: null };
740
- };
741
- Parser.prototype.offset = function () {
742
- return this.position.offset;
743
- };
744
- Parser.prototype.isEOF = function () {
745
- return this.offset() === this.message.length;
746
- };
747
- Parser.prototype.clonePosition = function () {
748
- // This is much faster than `Object.assign` or spread.
749
- return {
750
- offset: this.position.offset,
751
- line: this.position.line,
752
- column: this.position.column,
753
- };
754
- };
755
- /**
756
- * Return the code point at the current position of the parser.
757
- * Throws if the index is out of bound.
758
- */
759
- Parser.prototype.char = function () {
760
- var offset = this.position.offset;
761
- if (offset >= this.message.length) {
762
- throw Error('out of bound');
763
- }
764
- var code = this.message.codePointAt(offset);
765
- if (code === undefined) {
766
- throw Error("Offset ".concat(offset, " is at invalid UTF-16 code unit boundary"));
767
- }
768
- return code;
769
- };
770
- Parser.prototype.error = function (kind, location) {
771
- return {
772
- val: null,
773
- err: {
774
- kind: kind,
775
- message: this.message,
776
- location: location,
777
- },
778
- };
779
- };
780
- /** Bump the parser to the next UTF-16 code unit. */
781
- Parser.prototype.bump = function () {
782
- if (this.isEOF()) {
783
- return;
784
- }
785
- var code = this.char();
786
- if (code === 10 /* '\n' */) {
787
- this.position.line += 1;
788
- this.position.column = 1;
789
- this.position.offset += 1;
790
- }
791
- else {
792
- this.position.column += 1;
793
- // 0 ~ 0x10000 -> unicode BMP, otherwise skip the surrogate pair.
794
- this.position.offset += code < 0x10000 ? 1 : 2;
795
- }
796
- };
797
- /**
798
- * If the substring starting at the current position of the parser has
799
- * the given prefix, then bump the parser to the character immediately
800
- * following the prefix and return true. Otherwise, don't bump the parser
801
- * and return false.
802
- */
803
- Parser.prototype.bumpIf = function (prefix) {
804
- if (this.message.startsWith(prefix, this.offset())) {
805
- for (var i = 0; i < prefix.length; i++) {
806
- this.bump();
807
- }
808
- return true;
809
- }
810
- return false;
811
- };
812
- /**
813
- * Bump the parser until the pattern character is found and return `true`.
814
- * Otherwise bump to the end of the file and return `false`.
815
- */
816
- Parser.prototype.bumpUntil = function (pattern) {
817
- var currentOffset = this.offset();
818
- var index = this.message.indexOf(pattern, currentOffset);
819
- if (index >= 0) {
820
- this.bumpTo(index);
821
- return true;
822
- }
823
- else {
824
- this.bumpTo(this.message.length);
825
- return false;
826
- }
827
- };
828
- /**
829
- * Bump the parser to the target offset.
830
- * If target offset is beyond the end of the input, bump the parser to the end of the input.
831
- */
832
- Parser.prototype.bumpTo = function (targetOffset) {
833
- if (this.offset() > targetOffset) {
834
- throw Error("targetOffset ".concat(targetOffset, " must be greater than or equal to the current offset ").concat(this.offset()));
835
- }
836
- targetOffset = Math.min(targetOffset, this.message.length);
837
- while (true) {
838
- var offset = this.offset();
839
- if (offset === targetOffset) {
840
- break;
841
- }
842
- if (offset > targetOffset) {
843
- throw Error("targetOffset ".concat(targetOffset, " is at invalid UTF-16 code unit boundary"));
844
- }
845
- this.bump();
846
- if (this.isEOF()) {
847
- break;
848
- }
849
- }
850
- };
851
- /** advance the parser through all whitespace to the next non-whitespace code unit. */
852
- Parser.prototype.bumpSpace = function () {
853
- while (!this.isEOF() && _isWhiteSpace(this.char())) {
854
- this.bump();
855
- }
856
- };
857
- /**
858
- * Peek at the *next* Unicode codepoint in the input without advancing the parser.
859
- * If the input has been exhausted, then this returns null.
860
- */
861
- Parser.prototype.peek = function () {
862
- if (this.isEOF()) {
863
- return null;
864
- }
865
- var code = this.char();
866
- var offset = this.offset();
867
- var nextCode = this.message.charCodeAt(offset + (code >= 0x10000 ? 2 : 1));
868
- return nextCode !== null && nextCode !== void 0 ? nextCode : null;
869
- };
870
- return Parser;
871
- }());
872
- export { Parser };
873
836
  /**
874
- * This check if codepoint is alphabet (lower & uppercase)
875
- * @param codepoint
876
- * @returns
877
- */
837
+ * This check if codepoint is alphabet (lower & uppercase)
838
+ * @param codepoint
839
+ * @returns
840
+ */
878
841
  function _isAlpha(codepoint) {
879
- return ((codepoint >= 97 && codepoint <= 122) ||
880
- (codepoint >= 65 && codepoint <= 90));
842
+ return codepoint >= 97 && codepoint <= 122 || codepoint >= 65 && codepoint <= 90;
881
843
  }
882
844
  function _isAlphaOrSlash(codepoint) {
883
- return _isAlpha(codepoint) || codepoint === 47; /* '/' */
845
+ return _isAlpha(codepoint) || codepoint === 47;
884
846
  }
885
847
  /** See `parseTag` function docs. */
886
848
  function _isPotentialElementNameChar(c) {
887
- return (c === 45 /* '-' */ ||
888
- c === 46 /* '.' */ ||
889
- (c >= 48 && c <= 57) /* 0..9 */ ||
890
- c === 95 /* '_' */ ||
891
- (c >= 97 && c <= 122) /** a..z */ ||
892
- (c >= 65 && c <= 90) /* A..Z */ ||
893
- c == 0xb7 ||
894
- (c >= 0xc0 && c <= 0xd6) ||
895
- (c >= 0xd8 && c <= 0xf6) ||
896
- (c >= 0xf8 && c <= 0x37d) ||
897
- (c >= 0x37f && c <= 0x1fff) ||
898
- (c >= 0x200c && c <= 0x200d) ||
899
- (c >= 0x203f && c <= 0x2040) ||
900
- (c >= 0x2070 && c <= 0x218f) ||
901
- (c >= 0x2c00 && c <= 0x2fef) ||
902
- (c >= 0x3001 && c <= 0xd7ff) ||
903
- (c >= 0xf900 && c <= 0xfdcf) ||
904
- (c >= 0xfdf0 && c <= 0xfffd) ||
905
- (c >= 0x10000 && c <= 0xeffff));
849
+ return c === 45 || c === 46 || c >= 48 && c <= 57 || c === 95 || c >= 97 && c <= 122 || c >= 65 && c <= 90 || c == 183 || c >= 192 && c <= 214 || c >= 216 && c <= 246 || c >= 248 && c <= 893 || c >= 895 && c <= 8191 || c >= 8204 && c <= 8205 || c >= 8255 && c <= 8256 || c >= 8304 && c <= 8591 || c >= 11264 && c <= 12271 || c >= 12289 && c <= 55295 || c >= 63744 && c <= 64975 || c >= 65008 && c <= 65533 || c >= 65536 && c <= 983039;
906
850
  }
907
851
  /**
908
- * Code point equivalent of regex `\p{White_Space}`.
909
- * From: https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
910
- */
852
+ * Code point equivalent of regex `\p{White_Space}`.
853
+ * From: https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt
854
+ */
911
855
  function _isWhiteSpace(c) {
912
- return ((c >= 0x0009 && c <= 0x000d) ||
913
- c === 0x0020 ||
914
- c === 0x0085 ||
915
- (c >= 0x200e && c <= 0x200f) ||
916
- c === 0x2028 ||
917
- c === 0x2029);
856
+ return c >= 9 && c <= 13 || c === 32 || c === 133 || c >= 8206 && c <= 8207 || c === 8232 || c === 8233;
918
857
  }