@intlify/message-compiler 9.2.2 → 9.3.0-beta.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.
@@ -1,1436 +1 @@
1
- /*!
2
- * message-compiler v9.2.2
3
- * (c) 2022 kazuya kawaguchi
4
- * Released under the MIT License.
5
- */
6
- 'use strict';
7
-
8
- Object.defineProperty(exports, '__esModule', { value: true });
9
-
10
- var shared = require('@intlify/shared');
11
- var sourceMap = require('source-map');
12
-
13
- const CompileErrorCodes = {
14
- // tokenizer error codes
15
- EXPECTED_TOKEN: 1,
16
- INVALID_TOKEN_IN_PLACEHOLDER: 2,
17
- UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER: 3,
18
- UNKNOWN_ESCAPE_SEQUENCE: 4,
19
- INVALID_UNICODE_ESCAPE_SEQUENCE: 5,
20
- UNBALANCED_CLOSING_BRACE: 6,
21
- UNTERMINATED_CLOSING_BRACE: 7,
22
- EMPTY_PLACEHOLDER: 8,
23
- NOT_ALLOW_NEST_PLACEHOLDER: 9,
24
- INVALID_LINKED_FORMAT: 10,
25
- // parser error codes
26
- MUST_HAVE_MESSAGES_IN_PLURAL: 11,
27
- UNEXPECTED_EMPTY_LINKED_MODIFIER: 12,
28
- UNEXPECTED_EMPTY_LINKED_KEY: 13,
29
- UNEXPECTED_LEXICAL_ANALYSIS: 14,
30
- // Special value for higher-order compilers to pick up the last code
31
- // to avoid collision of error codes. This should always be kept as the last
32
- // item.
33
- __EXTEND_POINT__: 15
34
- };
35
- /** @internal */
36
- const errorMessages = {
37
- // tokenizer error messages
38
- [CompileErrorCodes.EXPECTED_TOKEN]: `Expected token: '{0}'`,
39
- [CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER]: `Invalid token in placeholder: '{0}'`,
40
- [CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER]: `Unterminated single quote in placeholder`,
41
- [CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE]: `Unknown escape sequence: \\{0}`,
42
- [CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE]: `Invalid unicode escape sequence: {0}`,
43
- [CompileErrorCodes.UNBALANCED_CLOSING_BRACE]: `Unbalanced closing brace`,
44
- [CompileErrorCodes.UNTERMINATED_CLOSING_BRACE]: `Unterminated closing brace`,
45
- [CompileErrorCodes.EMPTY_PLACEHOLDER]: `Empty placeholder`,
46
- [CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER]: `Not allowed nest placeholder`,
47
- [CompileErrorCodes.INVALID_LINKED_FORMAT]: `Invalid linked format`,
48
- // parser error messages
49
- [CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]: `Plural must have messages`,
50
- [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]: `Unexpected empty linked modifier`,
51
- [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]: `Unexpected empty linked key`,
52
- [CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`
53
- };
54
- function createCompileError(code, loc, options = {}) {
55
- const { domain, messages, args } = options;
56
- const msg = code;
57
- const error = new SyntaxError(String(msg));
58
- error.code = code;
59
- if (loc) {
60
- error.location = loc;
61
- }
62
- error.domain = domain;
63
- return error;
64
- }
65
- /** @internal */
66
- function defaultOnError(error) {
67
- throw error;
68
- }
69
-
70
- const LocationStub = {
71
- start: { line: 1, column: 1, offset: 0 },
72
- end: { line: 1, column: 1, offset: 0 }
73
- };
74
- function createPosition(line, column, offset) {
75
- return { line, column, offset };
76
- }
77
- function createLocation(start, end, source) {
78
- const loc = { start, end };
79
- if (source != null) {
80
- loc.source = source;
81
- }
82
- return loc;
83
- }
84
-
85
- const CHAR_SP = ' ';
86
- const CHAR_CR = '\r';
87
- const CHAR_LF = '\n';
88
- const CHAR_LS = String.fromCharCode(0x2028);
89
- const CHAR_PS = String.fromCharCode(0x2029);
90
- function createScanner(str) {
91
- const _buf = str;
92
- let _index = 0;
93
- let _line = 1;
94
- let _column = 1;
95
- let _peekOffset = 0;
96
- const isCRLF = (index) => _buf[index] === CHAR_CR && _buf[index + 1] === CHAR_LF;
97
- const isLF = (index) => _buf[index] === CHAR_LF;
98
- const isPS = (index) => _buf[index] === CHAR_PS;
99
- const isLS = (index) => _buf[index] === CHAR_LS;
100
- const isLineEnd = (index) => isCRLF(index) || isLF(index) || isPS(index) || isLS(index);
101
- const index = () => _index;
102
- const line = () => _line;
103
- const column = () => _column;
104
- const peekOffset = () => _peekOffset;
105
- const charAt = (offset) => isCRLF(offset) || isPS(offset) || isLS(offset) ? CHAR_LF : _buf[offset];
106
- const currentChar = () => charAt(_index);
107
- const currentPeek = () => charAt(_index + _peekOffset);
108
- function next() {
109
- _peekOffset = 0;
110
- if (isLineEnd(_index)) {
111
- _line++;
112
- _column = 0;
113
- }
114
- if (isCRLF(_index)) {
115
- _index++;
116
- }
117
- _index++;
118
- _column++;
119
- return _buf[_index];
120
- }
121
- function peek() {
122
- if (isCRLF(_index + _peekOffset)) {
123
- _peekOffset++;
124
- }
125
- _peekOffset++;
126
- return _buf[_index + _peekOffset];
127
- }
128
- function reset() {
129
- _index = 0;
130
- _line = 1;
131
- _column = 1;
132
- _peekOffset = 0;
133
- }
134
- function resetPeek(offset = 0) {
135
- _peekOffset = offset;
136
- }
137
- function skipToPeek() {
138
- const target = _index + _peekOffset;
139
- // eslint-disable-next-line no-unmodified-loop-condition
140
- while (target !== _index) {
141
- next();
142
- }
143
- _peekOffset = 0;
144
- }
145
- return {
146
- index,
147
- line,
148
- column,
149
- peekOffset,
150
- charAt,
151
- currentChar,
152
- currentPeek,
153
- next,
154
- peek,
155
- reset,
156
- resetPeek,
157
- skipToPeek
158
- };
159
- }
160
-
161
- const EOF = undefined;
162
- const LITERAL_DELIMITER = "'";
163
- const ERROR_DOMAIN$1 = 'tokenizer';
164
- function createTokenizer(source, options = {}) {
165
- const location = options.location !== false;
166
- const _scnr = createScanner(source);
167
- const currentOffset = () => _scnr.index();
168
- const currentPosition = () => createPosition(_scnr.line(), _scnr.column(), _scnr.index());
169
- const _initLoc = currentPosition();
170
- const _initOffset = currentOffset();
171
- const _context = {
172
- currentType: 14 /* EOF */,
173
- offset: _initOffset,
174
- startLoc: _initLoc,
175
- endLoc: _initLoc,
176
- lastType: 14 /* EOF */,
177
- lastOffset: _initOffset,
178
- lastStartLoc: _initLoc,
179
- lastEndLoc: _initLoc,
180
- braceNest: 0,
181
- inLinked: false,
182
- text: ''
183
- };
184
- const context = () => _context;
185
- const { onError } = options;
186
- function emitError(code, pos, offset, ...args) {
187
- const ctx = context();
188
- pos.column += offset;
189
- pos.offset += offset;
190
- if (onError) {
191
- const loc = createLocation(ctx.startLoc, pos);
192
- const err = createCompileError(code, loc, {
193
- domain: ERROR_DOMAIN$1,
194
- args
195
- });
196
- onError(err);
197
- }
198
- }
199
- function getToken(context, type, value) {
200
- context.endLoc = currentPosition();
201
- context.currentType = type;
202
- const token = { type };
203
- if (location) {
204
- token.loc = createLocation(context.startLoc, context.endLoc);
205
- }
206
- if (value != null) {
207
- token.value = value;
208
- }
209
- return token;
210
- }
211
- const getEndToken = (context) => getToken(context, 14 /* EOF */);
212
- function eat(scnr, ch) {
213
- if (scnr.currentChar() === ch) {
214
- scnr.next();
215
- return ch;
216
- }
217
- else {
218
- emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
219
- return '';
220
- }
221
- }
222
- function peekSpaces(scnr) {
223
- let buf = '';
224
- while (scnr.currentPeek() === CHAR_SP || scnr.currentPeek() === CHAR_LF) {
225
- buf += scnr.currentPeek();
226
- scnr.peek();
227
- }
228
- return buf;
229
- }
230
- function skipSpaces(scnr) {
231
- const buf = peekSpaces(scnr);
232
- scnr.skipToPeek();
233
- return buf;
234
- }
235
- function isIdentifierStart(ch) {
236
- if (ch === EOF) {
237
- return false;
238
- }
239
- const cc = ch.charCodeAt(0);
240
- return ((cc >= 97 && cc <= 122) || // a-z
241
- (cc >= 65 && cc <= 90) || // A-Z
242
- cc === 95 // _
243
- );
244
- }
245
- function isNumberStart(ch) {
246
- if (ch === EOF) {
247
- return false;
248
- }
249
- const cc = ch.charCodeAt(0);
250
- return cc >= 48 && cc <= 57; // 0-9
251
- }
252
- function isNamedIdentifierStart(scnr, context) {
253
- const { currentType } = context;
254
- if (currentType !== 2 /* BraceLeft */) {
255
- return false;
256
- }
257
- peekSpaces(scnr);
258
- const ret = isIdentifierStart(scnr.currentPeek());
259
- scnr.resetPeek();
260
- return ret;
261
- }
262
- function isListIdentifierStart(scnr, context) {
263
- const { currentType } = context;
264
- if (currentType !== 2 /* BraceLeft */) {
265
- return false;
266
- }
267
- peekSpaces(scnr);
268
- const ch = scnr.currentPeek() === '-' ? scnr.peek() : scnr.currentPeek();
269
- const ret = isNumberStart(ch);
270
- scnr.resetPeek();
271
- return ret;
272
- }
273
- function isLiteralStart(scnr, context) {
274
- const { currentType } = context;
275
- if (currentType !== 2 /* BraceLeft */) {
276
- return false;
277
- }
278
- peekSpaces(scnr);
279
- const ret = scnr.currentPeek() === LITERAL_DELIMITER;
280
- scnr.resetPeek();
281
- return ret;
282
- }
283
- function isLinkedDotStart(scnr, context) {
284
- const { currentType } = context;
285
- if (currentType !== 8 /* LinkedAlias */) {
286
- return false;
287
- }
288
- peekSpaces(scnr);
289
- const ret = scnr.currentPeek() === "." /* LinkedDot */;
290
- scnr.resetPeek();
291
- return ret;
292
- }
293
- function isLinkedModifierStart(scnr, context) {
294
- const { currentType } = context;
295
- if (currentType !== 9 /* LinkedDot */) {
296
- return false;
297
- }
298
- peekSpaces(scnr);
299
- const ret = isIdentifierStart(scnr.currentPeek());
300
- scnr.resetPeek();
301
- return ret;
302
- }
303
- function isLinkedDelimiterStart(scnr, context) {
304
- const { currentType } = context;
305
- if (!(currentType === 8 /* LinkedAlias */ ||
306
- currentType === 12 /* LinkedModifier */)) {
307
- return false;
308
- }
309
- peekSpaces(scnr);
310
- const ret = scnr.currentPeek() === ":" /* LinkedDelimiter */;
311
- scnr.resetPeek();
312
- return ret;
313
- }
314
- function isLinkedReferStart(scnr, context) {
315
- const { currentType } = context;
316
- if (currentType !== 10 /* LinkedDelimiter */) {
317
- return false;
318
- }
319
- const fn = () => {
320
- const ch = scnr.currentPeek();
321
- if (ch === "{" /* BraceLeft */) {
322
- return isIdentifierStart(scnr.peek());
323
- }
324
- else if (ch === "@" /* LinkedAlias */ ||
325
- ch === "%" /* Modulo */ ||
326
- ch === "|" /* Pipe */ ||
327
- ch === ":" /* LinkedDelimiter */ ||
328
- ch === "." /* LinkedDot */ ||
329
- ch === CHAR_SP ||
330
- !ch) {
331
- return false;
332
- }
333
- else if (ch === CHAR_LF) {
334
- scnr.peek();
335
- return fn();
336
- }
337
- else {
338
- // other characters
339
- return isIdentifierStart(ch);
340
- }
341
- };
342
- const ret = fn();
343
- scnr.resetPeek();
344
- return ret;
345
- }
346
- function isPluralStart(scnr) {
347
- peekSpaces(scnr);
348
- const ret = scnr.currentPeek() === "|" /* Pipe */;
349
- scnr.resetPeek();
350
- return ret;
351
- }
352
- function detectModuloStart(scnr) {
353
- const spaces = peekSpaces(scnr);
354
- const ret = scnr.currentPeek() === "%" /* Modulo */ &&
355
- scnr.peek() === "{" /* BraceLeft */;
356
- scnr.resetPeek();
357
- return {
358
- isModulo: ret,
359
- hasSpace: spaces.length > 0
360
- };
361
- }
362
- function isTextStart(scnr, reset = true) {
363
- const fn = (hasSpace = false, prev = '', detectModulo = false) => {
364
- const ch = scnr.currentPeek();
365
- if (ch === "{" /* BraceLeft */) {
366
- return prev === "%" /* Modulo */ ? false : hasSpace;
367
- }
368
- else if (ch === "@" /* LinkedAlias */ || !ch) {
369
- return prev === "%" /* Modulo */ ? true : hasSpace;
370
- }
371
- else if (ch === "%" /* Modulo */) {
372
- scnr.peek();
373
- return fn(hasSpace, "%" /* Modulo */, true);
374
- }
375
- else if (ch === "|" /* Pipe */) {
376
- return prev === "%" /* Modulo */ || detectModulo
377
- ? true
378
- : !(prev === CHAR_SP || prev === CHAR_LF);
379
- }
380
- else if (ch === CHAR_SP) {
381
- scnr.peek();
382
- return fn(true, CHAR_SP, detectModulo);
383
- }
384
- else if (ch === CHAR_LF) {
385
- scnr.peek();
386
- return fn(true, CHAR_LF, detectModulo);
387
- }
388
- else {
389
- return true;
390
- }
391
- };
392
- const ret = fn();
393
- reset && scnr.resetPeek();
394
- return ret;
395
- }
396
- function takeChar(scnr, fn) {
397
- const ch = scnr.currentChar();
398
- if (ch === EOF) {
399
- return EOF;
400
- }
401
- if (fn(ch)) {
402
- scnr.next();
403
- return ch;
404
- }
405
- return null;
406
- }
407
- function takeIdentifierChar(scnr) {
408
- const closure = (ch) => {
409
- const cc = ch.charCodeAt(0);
410
- return ((cc >= 97 && cc <= 122) || // a-z
411
- (cc >= 65 && cc <= 90) || // A-Z
412
- (cc >= 48 && cc <= 57) || // 0-9
413
- cc === 95 || // _
414
- cc === 36 // $
415
- );
416
- };
417
- return takeChar(scnr, closure);
418
- }
419
- function takeDigit(scnr) {
420
- const closure = (ch) => {
421
- const cc = ch.charCodeAt(0);
422
- return cc >= 48 && cc <= 57; // 0-9
423
- };
424
- return takeChar(scnr, closure);
425
- }
426
- function takeHexDigit(scnr) {
427
- const closure = (ch) => {
428
- const cc = ch.charCodeAt(0);
429
- return ((cc >= 48 && cc <= 57) || // 0-9
430
- (cc >= 65 && cc <= 70) || // A-F
431
- (cc >= 97 && cc <= 102)); // a-f
432
- };
433
- return takeChar(scnr, closure);
434
- }
435
- function getDigits(scnr) {
436
- let ch = '';
437
- let num = '';
438
- while ((ch = takeDigit(scnr))) {
439
- num += ch;
440
- }
441
- return num;
442
- }
443
- function readModulo(scnr) {
444
- skipSpaces(scnr);
445
- const ch = scnr.currentChar();
446
- if (ch !== "%" /* Modulo */) {
447
- emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
448
- }
449
- scnr.next();
450
- return "%" /* Modulo */;
451
- }
452
- function readText(scnr) {
453
- let buf = '';
454
- while (true) {
455
- const ch = scnr.currentChar();
456
- if (ch === "{" /* BraceLeft */ ||
457
- ch === "}" /* BraceRight */ ||
458
- ch === "@" /* LinkedAlias */ ||
459
- ch === "|" /* Pipe */ ||
460
- !ch) {
461
- break;
462
- }
463
- else if (ch === "%" /* Modulo */) {
464
- if (isTextStart(scnr)) {
465
- buf += ch;
466
- scnr.next();
467
- }
468
- else {
469
- break;
470
- }
471
- }
472
- else if (ch === CHAR_SP || ch === CHAR_LF) {
473
- if (isTextStart(scnr)) {
474
- buf += ch;
475
- scnr.next();
476
- }
477
- else if (isPluralStart(scnr)) {
478
- break;
479
- }
480
- else {
481
- buf += ch;
482
- scnr.next();
483
- }
484
- }
485
- else {
486
- buf += ch;
487
- scnr.next();
488
- }
489
- }
490
- return buf;
491
- }
492
- function readNamedIdentifier(scnr) {
493
- skipSpaces(scnr);
494
- let ch = '';
495
- let name = '';
496
- while ((ch = takeIdentifierChar(scnr))) {
497
- name += ch;
498
- }
499
- if (scnr.currentChar() === EOF) {
500
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
501
- }
502
- return name;
503
- }
504
- function readListIdentifier(scnr) {
505
- skipSpaces(scnr);
506
- let value = '';
507
- if (scnr.currentChar() === '-') {
508
- scnr.next();
509
- value += `-${getDigits(scnr)}`;
510
- }
511
- else {
512
- value += getDigits(scnr);
513
- }
514
- if (scnr.currentChar() === EOF) {
515
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
516
- }
517
- return value;
518
- }
519
- function readLiteral(scnr) {
520
- skipSpaces(scnr);
521
- eat(scnr, `\'`);
522
- let ch = '';
523
- let literal = '';
524
- const fn = (x) => x !== LITERAL_DELIMITER && x !== CHAR_LF;
525
- while ((ch = takeChar(scnr, fn))) {
526
- if (ch === '\\') {
527
- literal += readEscapeSequence(scnr);
528
- }
529
- else {
530
- literal += ch;
531
- }
532
- }
533
- const current = scnr.currentChar();
534
- if (current === CHAR_LF || current === EOF) {
535
- emitError(CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER, currentPosition(), 0);
536
- // TODO: Is it correct really?
537
- if (current === CHAR_LF) {
538
- scnr.next();
539
- eat(scnr, `\'`);
540
- }
541
- return literal;
542
- }
543
- eat(scnr, `\'`);
544
- return literal;
545
- }
546
- function readEscapeSequence(scnr) {
547
- const ch = scnr.currentChar();
548
- switch (ch) {
549
- case '\\':
550
- case `\'`:
551
- scnr.next();
552
- return `\\${ch}`;
553
- case 'u':
554
- return readUnicodeEscapeSequence(scnr, ch, 4);
555
- case 'U':
556
- return readUnicodeEscapeSequence(scnr, ch, 6);
557
- default:
558
- emitError(CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE, currentPosition(), 0, ch);
559
- return '';
560
- }
561
- }
562
- function readUnicodeEscapeSequence(scnr, unicode, digits) {
563
- eat(scnr, unicode);
564
- let sequence = '';
565
- for (let i = 0; i < digits; i++) {
566
- const ch = takeHexDigit(scnr);
567
- if (!ch) {
568
- emitError(CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE, currentPosition(), 0, `\\${unicode}${sequence}${scnr.currentChar()}`);
569
- break;
570
- }
571
- sequence += ch;
572
- }
573
- return `\\${unicode}${sequence}`;
574
- }
575
- function readInvalidIdentifier(scnr) {
576
- skipSpaces(scnr);
577
- let ch = '';
578
- let identifiers = '';
579
- const closure = (ch) => ch !== "{" /* BraceLeft */ &&
580
- ch !== "}" /* BraceRight */ &&
581
- ch !== CHAR_SP &&
582
- ch !== CHAR_LF;
583
- while ((ch = takeChar(scnr, closure))) {
584
- identifiers += ch;
585
- }
586
- return identifiers;
587
- }
588
- function readLinkedModifier(scnr) {
589
- let ch = '';
590
- let name = '';
591
- while ((ch = takeIdentifierChar(scnr))) {
592
- name += ch;
593
- }
594
- return name;
595
- }
596
- function readLinkedRefer(scnr) {
597
- const fn = (detect = false, buf) => {
598
- const ch = scnr.currentChar();
599
- if (ch === "{" /* BraceLeft */ ||
600
- ch === "%" /* Modulo */ ||
601
- ch === "@" /* LinkedAlias */ ||
602
- ch === "|" /* Pipe */ ||
603
- !ch) {
604
- return buf;
605
- }
606
- else if (ch === CHAR_SP) {
607
- return buf;
608
- }
609
- else if (ch === CHAR_LF) {
610
- buf += ch;
611
- scnr.next();
612
- return fn(detect, buf);
613
- }
614
- else {
615
- buf += ch;
616
- scnr.next();
617
- return fn(true, buf);
618
- }
619
- };
620
- return fn(false, '');
621
- }
622
- function readPlural(scnr) {
623
- skipSpaces(scnr);
624
- const plural = eat(scnr, "|" /* Pipe */);
625
- skipSpaces(scnr);
626
- return plural;
627
- }
628
- // TODO: We need refactoring of token parsing ...
629
- function readTokenInPlaceholder(scnr, context) {
630
- let token = null;
631
- const ch = scnr.currentChar();
632
- switch (ch) {
633
- case "{" /* BraceLeft */:
634
- if (context.braceNest >= 1) {
635
- emitError(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER, currentPosition(), 0);
636
- }
637
- scnr.next();
638
- token = getToken(context, 2 /* BraceLeft */, "{" /* BraceLeft */);
639
- skipSpaces(scnr);
640
- context.braceNest++;
641
- return token;
642
- case "}" /* BraceRight */:
643
- if (context.braceNest > 0 &&
644
- context.currentType === 2 /* BraceLeft */) {
645
- emitError(CompileErrorCodes.EMPTY_PLACEHOLDER, currentPosition(), 0);
646
- }
647
- scnr.next();
648
- token = getToken(context, 3 /* BraceRight */, "}" /* BraceRight */);
649
- context.braceNest--;
650
- context.braceNest > 0 && skipSpaces(scnr);
651
- if (context.inLinked && context.braceNest === 0) {
652
- context.inLinked = false;
653
- }
654
- return token;
655
- case "@" /* LinkedAlias */:
656
- if (context.braceNest > 0) {
657
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
658
- }
659
- token = readTokenInLinked(scnr, context) || getEndToken(context);
660
- context.braceNest = 0;
661
- return token;
662
- default:
663
- let validNamedIdentifier = true;
664
- let validListIdentifier = true;
665
- let validLiteral = true;
666
- if (isPluralStart(scnr)) {
667
- if (context.braceNest > 0) {
668
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
669
- }
670
- token = getToken(context, 1 /* Pipe */, readPlural(scnr));
671
- // reset
672
- context.braceNest = 0;
673
- context.inLinked = false;
674
- return token;
675
- }
676
- if (context.braceNest > 0 &&
677
- (context.currentType === 5 /* Named */ ||
678
- context.currentType === 6 /* List */ ||
679
- context.currentType === 7 /* Literal */)) {
680
- emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
681
- context.braceNest = 0;
682
- return readToken(scnr, context);
683
- }
684
- if ((validNamedIdentifier = isNamedIdentifierStart(scnr, context))) {
685
- token = getToken(context, 5 /* Named */, readNamedIdentifier(scnr));
686
- skipSpaces(scnr);
687
- return token;
688
- }
689
- if ((validListIdentifier = isListIdentifierStart(scnr, context))) {
690
- token = getToken(context, 6 /* List */, readListIdentifier(scnr));
691
- skipSpaces(scnr);
692
- return token;
693
- }
694
- if ((validLiteral = isLiteralStart(scnr, context))) {
695
- token = getToken(context, 7 /* Literal */, readLiteral(scnr));
696
- skipSpaces(scnr);
697
- return token;
698
- }
699
- if (!validNamedIdentifier && !validListIdentifier && !validLiteral) {
700
- // TODO: we should be re-designed invalid cases, when we will extend message syntax near the future ...
701
- token = getToken(context, 13 /* InvalidPlace */, readInvalidIdentifier(scnr));
702
- emitError(CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER, currentPosition(), 0, token.value);
703
- skipSpaces(scnr);
704
- return token;
705
- }
706
- break;
707
- }
708
- return token;
709
- }
710
- // TODO: We need refactoring of token parsing ...
711
- function readTokenInLinked(scnr, context) {
712
- const { currentType } = context;
713
- let token = null;
714
- const ch = scnr.currentChar();
715
- if ((currentType === 8 /* LinkedAlias */ ||
716
- currentType === 9 /* LinkedDot */ ||
717
- currentType === 12 /* LinkedModifier */ ||
718
- currentType === 10 /* LinkedDelimiter */) &&
719
- (ch === CHAR_LF || ch === CHAR_SP)) {
720
- emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
721
- }
722
- switch (ch) {
723
- case "@" /* LinkedAlias */:
724
- scnr.next();
725
- token = getToken(context, 8 /* LinkedAlias */, "@" /* LinkedAlias */);
726
- context.inLinked = true;
727
- return token;
728
- case "." /* LinkedDot */:
729
- skipSpaces(scnr);
730
- scnr.next();
731
- return getToken(context, 9 /* LinkedDot */, "." /* LinkedDot */);
732
- case ":" /* LinkedDelimiter */:
733
- skipSpaces(scnr);
734
- scnr.next();
735
- return getToken(context, 10 /* LinkedDelimiter */, ":" /* LinkedDelimiter */);
736
- default:
737
- if (isPluralStart(scnr)) {
738
- token = getToken(context, 1 /* Pipe */, readPlural(scnr));
739
- // reset
740
- context.braceNest = 0;
741
- context.inLinked = false;
742
- return token;
743
- }
744
- if (isLinkedDotStart(scnr, context) ||
745
- isLinkedDelimiterStart(scnr, context)) {
746
- skipSpaces(scnr);
747
- return readTokenInLinked(scnr, context);
748
- }
749
- if (isLinkedModifierStart(scnr, context)) {
750
- skipSpaces(scnr);
751
- return getToken(context, 12 /* LinkedModifier */, readLinkedModifier(scnr));
752
- }
753
- if (isLinkedReferStart(scnr, context)) {
754
- skipSpaces(scnr);
755
- if (ch === "{" /* BraceLeft */) {
756
- // scan the placeholder
757
- return readTokenInPlaceholder(scnr, context) || token;
758
- }
759
- else {
760
- return getToken(context, 11 /* LinkedKey */, readLinkedRefer(scnr));
761
- }
762
- }
763
- if (currentType === 8 /* LinkedAlias */) {
764
- emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
765
- }
766
- context.braceNest = 0;
767
- context.inLinked = false;
768
- return readToken(scnr, context);
769
- }
770
- }
771
- // TODO: We need refactoring of token parsing ...
772
- function readToken(scnr, context) {
773
- let token = { type: 14 /* EOF */ };
774
- if (context.braceNest > 0) {
775
- return readTokenInPlaceholder(scnr, context) || getEndToken(context);
776
- }
777
- if (context.inLinked) {
778
- return readTokenInLinked(scnr, context) || getEndToken(context);
779
- }
780
- const ch = scnr.currentChar();
781
- switch (ch) {
782
- case "{" /* BraceLeft */:
783
- return readTokenInPlaceholder(scnr, context) || getEndToken(context);
784
- case "}" /* BraceRight */:
785
- emitError(CompileErrorCodes.UNBALANCED_CLOSING_BRACE, currentPosition(), 0);
786
- scnr.next();
787
- return getToken(context, 3 /* BraceRight */, "}" /* BraceRight */);
788
- case "@" /* LinkedAlias */:
789
- return readTokenInLinked(scnr, context) || getEndToken(context);
790
- default:
791
- if (isPluralStart(scnr)) {
792
- token = getToken(context, 1 /* Pipe */, readPlural(scnr));
793
- // reset
794
- context.braceNest = 0;
795
- context.inLinked = false;
796
- return token;
797
- }
798
- const { isModulo, hasSpace } = detectModuloStart(scnr);
799
- if (isModulo) {
800
- return hasSpace
801
- ? getToken(context, 0 /* Text */, readText(scnr))
802
- : getToken(context, 4 /* Modulo */, readModulo(scnr));
803
- }
804
- if (isTextStart(scnr)) {
805
- return getToken(context, 0 /* Text */, readText(scnr));
806
- }
807
- break;
808
- }
809
- return token;
810
- }
811
- function nextToken() {
812
- const { currentType, offset, startLoc, endLoc } = _context;
813
- _context.lastType = currentType;
814
- _context.lastOffset = offset;
815
- _context.lastStartLoc = startLoc;
816
- _context.lastEndLoc = endLoc;
817
- _context.offset = currentOffset();
818
- _context.startLoc = currentPosition();
819
- if (_scnr.currentChar() === EOF) {
820
- return getToken(_context, 14 /* EOF */);
821
- }
822
- return readToken(_scnr, _context);
823
- }
824
- return {
825
- nextToken,
826
- currentOffset,
827
- currentPosition,
828
- context
829
- };
830
- }
831
-
832
- const ERROR_DOMAIN = 'parser';
833
- // Backslash backslash, backslash quote, uHHHH, UHHHHHH.
834
- const KNOWN_ESCAPES = /(?:\\\\|\\'|\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{6}))/g;
835
- function fromEscapeSequence(match, codePoint4, codePoint6) {
836
- switch (match) {
837
- case `\\\\`:
838
- return `\\`;
839
- case `\\\'`:
840
- return `\'`;
841
- default: {
842
- const codePoint = parseInt(codePoint4 || codePoint6, 16);
843
- if (codePoint <= 0xd7ff || codePoint >= 0xe000) {
844
- return String.fromCodePoint(codePoint);
845
- }
846
- // invalid ...
847
- // Replace them with U+FFFD REPLACEMENT CHARACTER.
848
- return '�';
849
- }
850
- }
851
- }
852
- function createParser(options = {}) {
853
- const location = options.location !== false;
854
- const { onError } = options;
855
- function emitError(tokenzer, code, start, offset, ...args) {
856
- const end = tokenzer.currentPosition();
857
- end.offset += offset;
858
- end.column += offset;
859
- if (onError) {
860
- const loc = createLocation(start, end);
861
- const err = createCompileError(code, loc, {
862
- domain: ERROR_DOMAIN,
863
- args
864
- });
865
- onError(err);
866
- }
867
- }
868
- function startNode(type, offset, loc) {
869
- const node = {
870
- type,
871
- start: offset,
872
- end: offset
873
- };
874
- if (location) {
875
- node.loc = { start: loc, end: loc };
876
- }
877
- return node;
878
- }
879
- function endNode(node, offset, pos, type) {
880
- node.end = offset;
881
- if (type) {
882
- node.type = type;
883
- }
884
- if (location && node.loc) {
885
- node.loc.end = pos;
886
- }
887
- }
888
- function parseText(tokenizer, value) {
889
- const context = tokenizer.context();
890
- const node = startNode(3 /* Text */, context.offset, context.startLoc);
891
- node.value = value;
892
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
893
- return node;
894
- }
895
- function parseList(tokenizer, index) {
896
- const context = tokenizer.context();
897
- const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
898
- const node = startNode(5 /* List */, offset, loc);
899
- node.index = parseInt(index, 10);
900
- tokenizer.nextToken(); // skip brach right
901
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
902
- return node;
903
- }
904
- function parseNamed(tokenizer, key) {
905
- const context = tokenizer.context();
906
- const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
907
- const node = startNode(4 /* Named */, offset, loc);
908
- node.key = key;
909
- tokenizer.nextToken(); // skip brach right
910
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
911
- return node;
912
- }
913
- function parseLiteral(tokenizer, value) {
914
- const context = tokenizer.context();
915
- const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
916
- const node = startNode(9 /* Literal */, offset, loc);
917
- node.value = value.replace(KNOWN_ESCAPES, fromEscapeSequence);
918
- tokenizer.nextToken(); // skip brach right
919
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
920
- return node;
921
- }
922
- function parseLinkedModifier(tokenizer) {
923
- const token = tokenizer.nextToken();
924
- const context = tokenizer.context();
925
- const { lastOffset: offset, lastStartLoc: loc } = context; // get linked dot loc
926
- const node = startNode(8 /* LinkedModifier */, offset, loc);
927
- if (token.type !== 12 /* LinkedModifier */) {
928
- // empty modifier
929
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER, context.lastStartLoc, 0);
930
- node.value = '';
931
- endNode(node, offset, loc);
932
- return {
933
- nextConsumeToken: token,
934
- node
935
- };
936
- }
937
- // check token
938
- if (token.value == null) {
939
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
940
- }
941
- node.value = token.value || '';
942
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
943
- return {
944
- node
945
- };
946
- }
947
- function parseLinkedKey(tokenizer, value) {
948
- const context = tokenizer.context();
949
- const node = startNode(7 /* LinkedKey */, context.offset, context.startLoc);
950
- node.value = value;
951
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
952
- return node;
953
- }
954
- function parseLinked(tokenizer) {
955
- const context = tokenizer.context();
956
- const linkedNode = startNode(6 /* Linked */, context.offset, context.startLoc);
957
- let token = tokenizer.nextToken();
958
- if (token.type === 9 /* LinkedDot */) {
959
- const parsed = parseLinkedModifier(tokenizer);
960
- linkedNode.modifier = parsed.node;
961
- token = parsed.nextConsumeToken || tokenizer.nextToken();
962
- }
963
- // asset check token
964
- if (token.type !== 10 /* LinkedDelimiter */) {
965
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
966
- }
967
- token = tokenizer.nextToken();
968
- // skip brace left
969
- if (token.type === 2 /* BraceLeft */) {
970
- token = tokenizer.nextToken();
971
- }
972
- switch (token.type) {
973
- case 11 /* LinkedKey */:
974
- if (token.value == null) {
975
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
976
- }
977
- linkedNode.key = parseLinkedKey(tokenizer, token.value || '');
978
- break;
979
- case 5 /* Named */:
980
- if (token.value == null) {
981
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
982
- }
983
- linkedNode.key = parseNamed(tokenizer, token.value || '');
984
- break;
985
- case 6 /* List */:
986
- if (token.value == null) {
987
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
988
- }
989
- linkedNode.key = parseList(tokenizer, token.value || '');
990
- break;
991
- case 7 /* Literal */:
992
- if (token.value == null) {
993
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
994
- }
995
- linkedNode.key = parseLiteral(tokenizer, token.value || '');
996
- break;
997
- default:
998
- // empty key
999
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY, context.lastStartLoc, 0);
1000
- const nextContext = tokenizer.context();
1001
- const emptyLinkedKeyNode = startNode(7 /* LinkedKey */, nextContext.offset, nextContext.startLoc);
1002
- emptyLinkedKeyNode.value = '';
1003
- endNode(emptyLinkedKeyNode, nextContext.offset, nextContext.startLoc);
1004
- linkedNode.key = emptyLinkedKeyNode;
1005
- endNode(linkedNode, nextContext.offset, nextContext.startLoc);
1006
- return {
1007
- nextConsumeToken: token,
1008
- node: linkedNode
1009
- };
1010
- }
1011
- endNode(linkedNode, tokenizer.currentOffset(), tokenizer.currentPosition());
1012
- return {
1013
- node: linkedNode
1014
- };
1015
- }
1016
- function parseMessage(tokenizer) {
1017
- const context = tokenizer.context();
1018
- const startOffset = context.currentType === 1 /* Pipe */
1019
- ? tokenizer.currentOffset()
1020
- : context.offset;
1021
- const startLoc = context.currentType === 1 /* Pipe */
1022
- ? context.endLoc
1023
- : context.startLoc;
1024
- const node = startNode(2 /* Message */, startOffset, startLoc);
1025
- node.items = [];
1026
- let nextToken = null;
1027
- do {
1028
- const token = nextToken || tokenizer.nextToken();
1029
- nextToken = null;
1030
- switch (token.type) {
1031
- case 0 /* Text */:
1032
- if (token.value == null) {
1033
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1034
- }
1035
- node.items.push(parseText(tokenizer, token.value || ''));
1036
- break;
1037
- case 6 /* List */:
1038
- if (token.value == null) {
1039
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1040
- }
1041
- node.items.push(parseList(tokenizer, token.value || ''));
1042
- break;
1043
- case 5 /* Named */:
1044
- if (token.value == null) {
1045
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1046
- }
1047
- node.items.push(parseNamed(tokenizer, token.value || ''));
1048
- break;
1049
- case 7 /* Literal */:
1050
- if (token.value == null) {
1051
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
1052
- }
1053
- node.items.push(parseLiteral(tokenizer, token.value || ''));
1054
- break;
1055
- case 8 /* LinkedAlias */:
1056
- const parsed = parseLinked(tokenizer);
1057
- node.items.push(parsed.node);
1058
- nextToken = parsed.nextConsumeToken || null;
1059
- break;
1060
- }
1061
- } while (context.currentType !== 14 /* EOF */ &&
1062
- context.currentType !== 1 /* Pipe */);
1063
- // adjust message node loc
1064
- const endOffset = context.currentType === 1 /* Pipe */
1065
- ? context.lastOffset
1066
- : tokenizer.currentOffset();
1067
- const endLoc = context.currentType === 1 /* Pipe */
1068
- ? context.lastEndLoc
1069
- : tokenizer.currentPosition();
1070
- endNode(node, endOffset, endLoc);
1071
- return node;
1072
- }
1073
- function parsePlural(tokenizer, offset, loc, msgNode) {
1074
- const context = tokenizer.context();
1075
- let hasEmptyMessage = msgNode.items.length === 0;
1076
- const node = startNode(1 /* Plural */, offset, loc);
1077
- node.cases = [];
1078
- node.cases.push(msgNode);
1079
- do {
1080
- const msg = parseMessage(tokenizer);
1081
- if (!hasEmptyMessage) {
1082
- hasEmptyMessage = msg.items.length === 0;
1083
- }
1084
- node.cases.push(msg);
1085
- } while (context.currentType !== 14 /* EOF */);
1086
- if (hasEmptyMessage) {
1087
- emitError(tokenizer, CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL, loc, 0);
1088
- }
1089
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1090
- return node;
1091
- }
1092
- function parseResource(tokenizer) {
1093
- const context = tokenizer.context();
1094
- const { offset, startLoc } = context;
1095
- const msgNode = parseMessage(tokenizer);
1096
- if (context.currentType === 14 /* EOF */) {
1097
- return msgNode;
1098
- }
1099
- else {
1100
- return parsePlural(tokenizer, offset, startLoc, msgNode);
1101
- }
1102
- }
1103
- function parse(source) {
1104
- const tokenizer = createTokenizer(source, shared.assign({}, options));
1105
- const context = tokenizer.context();
1106
- const node = startNode(0 /* Resource */, context.offset, context.startLoc);
1107
- if (location && node.loc) {
1108
- node.loc.source = source;
1109
- }
1110
- node.body = parseResource(tokenizer);
1111
- // assert whether achieved to EOF
1112
- if (context.currentType !== 14 /* EOF */) {
1113
- emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, source[context.offset] || '');
1114
- }
1115
- endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
1116
- return node;
1117
- }
1118
- return { parse };
1119
- }
1120
- function getTokenCaption(token) {
1121
- if (token.type === 14 /* EOF */) {
1122
- return 'EOF';
1123
- }
1124
- const name = (token.value || '').replace(/\r?\n/gu, '\\n');
1125
- return name.length > 10 ? name.slice(0, 9) + '…' : name;
1126
- }
1127
-
1128
- function createTransformer(ast, options = {} // eslint-disable-line
1129
- ) {
1130
- const _context = {
1131
- ast,
1132
- helpers: new Set()
1133
- };
1134
- const context = () => _context;
1135
- const helper = (name) => {
1136
- _context.helpers.add(name);
1137
- return name;
1138
- };
1139
- return { context, helper };
1140
- }
1141
- function traverseNodes(nodes, transformer) {
1142
- for (let i = 0; i < nodes.length; i++) {
1143
- traverseNode(nodes[i], transformer);
1144
- }
1145
- }
1146
- function traverseNode(node, transformer) {
1147
- // TODO: if we need pre-hook of transform, should be implemented to here
1148
- switch (node.type) {
1149
- case 1 /* Plural */:
1150
- traverseNodes(node.cases, transformer);
1151
- transformer.helper("plural" /* PLURAL */);
1152
- break;
1153
- case 2 /* Message */:
1154
- traverseNodes(node.items, transformer);
1155
- break;
1156
- case 6 /* Linked */:
1157
- const linked = node;
1158
- traverseNode(linked.key, transformer);
1159
- transformer.helper("linked" /* LINKED */);
1160
- transformer.helper("type" /* TYPE */);
1161
- break;
1162
- case 5 /* List */:
1163
- transformer.helper("interpolate" /* INTERPOLATE */);
1164
- transformer.helper("list" /* LIST */);
1165
- break;
1166
- case 4 /* Named */:
1167
- transformer.helper("interpolate" /* INTERPOLATE */);
1168
- transformer.helper("named" /* NAMED */);
1169
- break;
1170
- }
1171
- // TODO: if we need post-hook of transform, should be implemented to here
1172
- }
1173
- // transform AST
1174
- function transform(ast, options = {} // eslint-disable-line
1175
- ) {
1176
- const transformer = createTransformer(ast);
1177
- transformer.helper("normalize" /* NORMALIZE */);
1178
- // traverse
1179
- ast.body && traverseNode(ast.body, transformer);
1180
- // set meta information
1181
- const context = transformer.context();
1182
- ast.helpers = Array.from(context.helpers);
1183
- }
1184
-
1185
- function createCodeGenerator(ast, options) {
1186
- const { sourceMap: sourceMap$1, filename, breakLineCode, needIndent: _needIndent } = options;
1187
- const _context = {
1188
- source: ast.loc.source,
1189
- filename,
1190
- code: '',
1191
- column: 1,
1192
- line: 1,
1193
- offset: 0,
1194
- map: undefined,
1195
- breakLineCode,
1196
- needIndent: _needIndent,
1197
- indentLevel: 0
1198
- };
1199
- const context = () => _context;
1200
- function push(code, node) {
1201
- _context.code += code;
1202
- if (_context.map) {
1203
- if (node && node.loc && node.loc !== LocationStub) {
1204
- addMapping(node.loc.start, getMappingName(node));
1205
- }
1206
- advancePositionWithSource(_context, code);
1207
- }
1208
- }
1209
- function _newline(n, withBreakLine = true) {
1210
- const _breakLineCode = withBreakLine ? breakLineCode : '';
1211
- push(_needIndent ? _breakLineCode + ` `.repeat(n) : _breakLineCode);
1212
- }
1213
- function indent(withNewLine = true) {
1214
- const level = ++_context.indentLevel;
1215
- withNewLine && _newline(level);
1216
- }
1217
- function deindent(withNewLine = true) {
1218
- const level = --_context.indentLevel;
1219
- withNewLine && _newline(level);
1220
- }
1221
- function newline() {
1222
- _newline(_context.indentLevel);
1223
- }
1224
- const helper = (key) => `_${key}`;
1225
- const needIndent = () => _context.needIndent;
1226
- function addMapping(loc, name) {
1227
- _context.map.addMapping({
1228
- name,
1229
- source: _context.filename,
1230
- original: {
1231
- line: loc.line,
1232
- column: loc.column - 1
1233
- },
1234
- generated: {
1235
- line: _context.line,
1236
- column: _context.column - 1
1237
- }
1238
- });
1239
- }
1240
- if (sourceMap$1) {
1241
- _context.map = new sourceMap.SourceMapGenerator();
1242
- _context.map.setSourceContent(filename, _context.source);
1243
- }
1244
- return {
1245
- context,
1246
- push,
1247
- indent,
1248
- deindent,
1249
- newline,
1250
- helper,
1251
- needIndent
1252
- };
1253
- }
1254
- function generateLinkedNode(generator, node) {
1255
- const { helper } = generator;
1256
- generator.push(`${helper("linked" /* LINKED */)}(`);
1257
- generateNode(generator, node.key);
1258
- if (node.modifier) {
1259
- generator.push(`, `);
1260
- generateNode(generator, node.modifier);
1261
- generator.push(`, _type`);
1262
- }
1263
- else {
1264
- generator.push(`, undefined, _type`);
1265
- }
1266
- generator.push(`)`);
1267
- }
1268
- function generateMessageNode(generator, node) {
1269
- const { helper, needIndent } = generator;
1270
- generator.push(`${helper("normalize" /* NORMALIZE */)}([`);
1271
- generator.indent(needIndent());
1272
- const length = node.items.length;
1273
- for (let i = 0; i < length; i++) {
1274
- generateNode(generator, node.items[i]);
1275
- if (i === length - 1) {
1276
- break;
1277
- }
1278
- generator.push(', ');
1279
- }
1280
- generator.deindent(needIndent());
1281
- generator.push('])');
1282
- }
1283
- function generatePluralNode(generator, node) {
1284
- const { helper, needIndent } = generator;
1285
- if (node.cases.length > 1) {
1286
- generator.push(`${helper("plural" /* PLURAL */)}([`);
1287
- generator.indent(needIndent());
1288
- const length = node.cases.length;
1289
- for (let i = 0; i < length; i++) {
1290
- generateNode(generator, node.cases[i]);
1291
- if (i === length - 1) {
1292
- break;
1293
- }
1294
- generator.push(', ');
1295
- }
1296
- generator.deindent(needIndent());
1297
- generator.push(`])`);
1298
- }
1299
- }
1300
- function generateResource(generator, node) {
1301
- if (node.body) {
1302
- generateNode(generator, node.body);
1303
- }
1304
- else {
1305
- generator.push('null');
1306
- }
1307
- }
1308
- function generateNode(generator, node) {
1309
- const { helper } = generator;
1310
- switch (node.type) {
1311
- case 0 /* Resource */:
1312
- generateResource(generator, node);
1313
- break;
1314
- case 1 /* Plural */:
1315
- generatePluralNode(generator, node);
1316
- break;
1317
- case 2 /* Message */:
1318
- generateMessageNode(generator, node);
1319
- break;
1320
- case 6 /* Linked */:
1321
- generateLinkedNode(generator, node);
1322
- break;
1323
- case 8 /* LinkedModifier */:
1324
- generator.push(JSON.stringify(node.value), node);
1325
- break;
1326
- case 7 /* LinkedKey */:
1327
- generator.push(JSON.stringify(node.value), node);
1328
- break;
1329
- case 5 /* List */:
1330
- generator.push(`${helper("interpolate" /* INTERPOLATE */)}(${helper("list" /* LIST */)}(${node.index}))`, node);
1331
- break;
1332
- case 4 /* Named */:
1333
- generator.push(`${helper("interpolate" /* INTERPOLATE */)}(${helper("named" /* NAMED */)}(${JSON.stringify(node.key)}))`, node);
1334
- break;
1335
- case 9 /* Literal */:
1336
- generator.push(JSON.stringify(node.value), node);
1337
- break;
1338
- case 3 /* Text */:
1339
- generator.push(JSON.stringify(node.value), node);
1340
- break;
1341
- }
1342
- }
1343
- // generate code from AST
1344
- const generate = (ast, options = {} // eslint-disable-line
1345
- ) => {
1346
- const mode = shared.isString(options.mode) ? options.mode : 'normal';
1347
- const filename = shared.isString(options.filename)
1348
- ? options.filename
1349
- : 'message.intl';
1350
- const sourceMap = !!options.sourceMap;
1351
- // prettier-ignore
1352
- const breakLineCode = options.breakLineCode != null
1353
- ? options.breakLineCode
1354
- : mode === 'arrow'
1355
- ? ';'
1356
- : '\n';
1357
- const needIndent = options.needIndent ? options.needIndent : mode !== 'arrow';
1358
- const helpers = ast.helpers || [];
1359
- const generator = createCodeGenerator(ast, {
1360
- mode,
1361
- filename,
1362
- sourceMap,
1363
- breakLineCode,
1364
- needIndent
1365
- });
1366
- generator.push(mode === 'normal' ? `function __msg__ (ctx) {` : `(ctx) => {`);
1367
- generator.indent(needIndent);
1368
- if (helpers.length > 0) {
1369
- generator.push(`const { ${helpers.map(s => `${s}: _${s}`).join(', ')} } = ctx`);
1370
- generator.newline();
1371
- }
1372
- generator.push(`return `);
1373
- generateNode(generator, ast);
1374
- generator.deindent(needIndent);
1375
- generator.push(`}`);
1376
- const { code, map } = generator.context();
1377
- return {
1378
- ast,
1379
- code,
1380
- map: map ? map.toJSON() : undefined // eslint-disable-line @typescript-eslint/no-explicit-any
1381
- };
1382
- };
1383
- function getMappingName(node) {
1384
- switch (node.type) {
1385
- case 3 /* Text */:
1386
- case 9 /* Literal */:
1387
- case 8 /* LinkedModifier */:
1388
- case 7 /* LinkedKey */:
1389
- return node.value;
1390
- case 5 /* List */:
1391
- return node.index.toString();
1392
- case 4 /* Named */:
1393
- return node.key;
1394
- default:
1395
- return undefined;
1396
- }
1397
- }
1398
- function advancePositionWithSource(pos, source, numberOfCharacters = source.length) {
1399
- let linesCount = 0;
1400
- let lastNewLinePos = -1;
1401
- for (let i = 0; i < numberOfCharacters; i++) {
1402
- if (source.charCodeAt(i) === 10 /* newline char code */) {
1403
- linesCount++;
1404
- lastNewLinePos = i;
1405
- }
1406
- }
1407
- pos.offset += numberOfCharacters;
1408
- pos.line += linesCount;
1409
- pos.column =
1410
- lastNewLinePos === -1
1411
- ? pos.column + numberOfCharacters
1412
- : numberOfCharacters - lastNewLinePos;
1413
- return pos;
1414
- }
1415
-
1416
- function baseCompile(source, options = {}) {
1417
- const assignedOptions = shared.assign({}, options);
1418
- // parse source codes
1419
- const parser = createParser(assignedOptions);
1420
- const ast = parser.parse(source);
1421
- // transform ASTs
1422
- transform(ast, assignedOptions);
1423
- // generate javascript codes
1424
- return generate(ast, assignedOptions);
1425
- }
1426
-
1427
- exports.CompileErrorCodes = CompileErrorCodes;
1428
- exports.ERROR_DOMAIN = ERROR_DOMAIN;
1429
- exports.LocationStub = LocationStub;
1430
- exports.baseCompile = baseCompile;
1431
- exports.createCompileError = createCompileError;
1432
- exports.createLocation = createLocation;
1433
- exports.createParser = createParser;
1434
- exports.createPosition = createPosition;
1435
- exports.defaultOnError = defaultOnError;
1436
- exports.errorMessages = errorMessages;
1
+ module.exports = require('../dist/message-compiler.prod.cjs')