@intlify/core 9.3.0 → 9.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/dist/core.cjs +2 -2
- package/dist/core.esm-browser.js +36 -16
- package/dist/core.esm-browser.prod.js +2 -2
- package/dist/core.global.js +36 -16
- package/dist/core.global.prod.js +2 -2
- package/dist/core.mjs +5 -5
- package/dist/core.node.mjs +3 -0
- package/dist/core.prod.cjs +2 -2
- package/dist/core.runtime.esm-browser.js +1504 -12
- package/dist/core.runtime.esm-browser.prod.js +2 -2
- package/dist/core.runtime.global.js +1504 -12
- package/dist/core.runtime.global.prod.js +2 -2
- package/dist/core.runtime.mjs +1 -1
- package/package.json +9 -9
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* core v9.
|
|
2
|
+
* core v9.4.1
|
|
3
3
|
* (c) 2023 kazuya kawaguchi
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -142,6 +142,17 @@ var IntlifyCore = (function (exports) {
|
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
+
function createPosition(line, column, offset) {
|
|
146
|
+
return { line, column, offset };
|
|
147
|
+
}
|
|
148
|
+
function createLocation(start, end, source) {
|
|
149
|
+
const loc = { start, end };
|
|
150
|
+
if (source != null) {
|
|
151
|
+
loc.source = source;
|
|
152
|
+
}
|
|
153
|
+
return loc;
|
|
154
|
+
}
|
|
155
|
+
|
|
145
156
|
const CompileErrorCodes = {
|
|
146
157
|
// tokenizer error codes
|
|
147
158
|
EXPECTED_TOKEN: 1,
|
|
@@ -203,6 +214,1448 @@ var IntlifyCore = (function (exports) {
|
|
|
203
214
|
error.domain = domain;
|
|
204
215
|
return error;
|
|
205
216
|
}
|
|
217
|
+
/** @internal */
|
|
218
|
+
function defaultOnError(error) {
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const RE_HTML_TAG = /<\/?[\w\s="/.':;#-\/]+>/;
|
|
223
|
+
const detectHtmlTag = (source) => RE_HTML_TAG.test(source);
|
|
224
|
+
|
|
225
|
+
const CHAR_SP = ' ';
|
|
226
|
+
const CHAR_CR = '\r';
|
|
227
|
+
const CHAR_LF = '\n';
|
|
228
|
+
const CHAR_LS = String.fromCharCode(0x2028);
|
|
229
|
+
const CHAR_PS = String.fromCharCode(0x2029);
|
|
230
|
+
function createScanner(str) {
|
|
231
|
+
const _buf = str;
|
|
232
|
+
let _index = 0;
|
|
233
|
+
let _line = 1;
|
|
234
|
+
let _column = 1;
|
|
235
|
+
let _peekOffset = 0;
|
|
236
|
+
const isCRLF = (index) => _buf[index] === CHAR_CR && _buf[index + 1] === CHAR_LF;
|
|
237
|
+
const isLF = (index) => _buf[index] === CHAR_LF;
|
|
238
|
+
const isPS = (index) => _buf[index] === CHAR_PS;
|
|
239
|
+
const isLS = (index) => _buf[index] === CHAR_LS;
|
|
240
|
+
const isLineEnd = (index) => isCRLF(index) || isLF(index) || isPS(index) || isLS(index);
|
|
241
|
+
const index = () => _index;
|
|
242
|
+
const line = () => _line;
|
|
243
|
+
const column = () => _column;
|
|
244
|
+
const peekOffset = () => _peekOffset;
|
|
245
|
+
const charAt = (offset) => isCRLF(offset) || isPS(offset) || isLS(offset) ? CHAR_LF : _buf[offset];
|
|
246
|
+
const currentChar = () => charAt(_index);
|
|
247
|
+
const currentPeek = () => charAt(_index + _peekOffset);
|
|
248
|
+
function next() {
|
|
249
|
+
_peekOffset = 0;
|
|
250
|
+
if (isLineEnd(_index)) {
|
|
251
|
+
_line++;
|
|
252
|
+
_column = 0;
|
|
253
|
+
}
|
|
254
|
+
if (isCRLF(_index)) {
|
|
255
|
+
_index++;
|
|
256
|
+
}
|
|
257
|
+
_index++;
|
|
258
|
+
_column++;
|
|
259
|
+
return _buf[_index];
|
|
260
|
+
}
|
|
261
|
+
function peek() {
|
|
262
|
+
if (isCRLF(_index + _peekOffset)) {
|
|
263
|
+
_peekOffset++;
|
|
264
|
+
}
|
|
265
|
+
_peekOffset++;
|
|
266
|
+
return _buf[_index + _peekOffset];
|
|
267
|
+
}
|
|
268
|
+
function reset() {
|
|
269
|
+
_index = 0;
|
|
270
|
+
_line = 1;
|
|
271
|
+
_column = 1;
|
|
272
|
+
_peekOffset = 0;
|
|
273
|
+
}
|
|
274
|
+
function resetPeek(offset = 0) {
|
|
275
|
+
_peekOffset = offset;
|
|
276
|
+
}
|
|
277
|
+
function skipToPeek() {
|
|
278
|
+
const target = _index + _peekOffset;
|
|
279
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
280
|
+
while (target !== _index) {
|
|
281
|
+
next();
|
|
282
|
+
}
|
|
283
|
+
_peekOffset = 0;
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
index,
|
|
287
|
+
line,
|
|
288
|
+
column,
|
|
289
|
+
peekOffset,
|
|
290
|
+
charAt,
|
|
291
|
+
currentChar,
|
|
292
|
+
currentPeek,
|
|
293
|
+
next,
|
|
294
|
+
peek,
|
|
295
|
+
reset,
|
|
296
|
+
resetPeek,
|
|
297
|
+
skipToPeek
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const EOF = undefined;
|
|
302
|
+
const DOT = '.';
|
|
303
|
+
const LITERAL_DELIMITER = "'";
|
|
304
|
+
const ERROR_DOMAIN$3 = 'tokenizer';
|
|
305
|
+
function createTokenizer(source, options = {}) {
|
|
306
|
+
const location = options.location !== false;
|
|
307
|
+
const _scnr = createScanner(source);
|
|
308
|
+
const currentOffset = () => _scnr.index();
|
|
309
|
+
const currentPosition = () => createPosition(_scnr.line(), _scnr.column(), _scnr.index());
|
|
310
|
+
const _initLoc = currentPosition();
|
|
311
|
+
const _initOffset = currentOffset();
|
|
312
|
+
const _context = {
|
|
313
|
+
currentType: 14 /* TokenTypes.EOF */,
|
|
314
|
+
offset: _initOffset,
|
|
315
|
+
startLoc: _initLoc,
|
|
316
|
+
endLoc: _initLoc,
|
|
317
|
+
lastType: 14 /* TokenTypes.EOF */,
|
|
318
|
+
lastOffset: _initOffset,
|
|
319
|
+
lastStartLoc: _initLoc,
|
|
320
|
+
lastEndLoc: _initLoc,
|
|
321
|
+
braceNest: 0,
|
|
322
|
+
inLinked: false,
|
|
323
|
+
text: ''
|
|
324
|
+
};
|
|
325
|
+
const context = () => _context;
|
|
326
|
+
const { onError } = options;
|
|
327
|
+
function emitError(code, pos, offset, ...args) {
|
|
328
|
+
const ctx = context();
|
|
329
|
+
pos.column += offset;
|
|
330
|
+
pos.offset += offset;
|
|
331
|
+
if (onError) {
|
|
332
|
+
const loc = location ? createLocation(ctx.startLoc, pos) : null;
|
|
333
|
+
const err = createCompileError(code, loc, {
|
|
334
|
+
domain: ERROR_DOMAIN$3,
|
|
335
|
+
args
|
|
336
|
+
});
|
|
337
|
+
onError(err);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function getToken(context, type, value) {
|
|
341
|
+
context.endLoc = currentPosition();
|
|
342
|
+
context.currentType = type;
|
|
343
|
+
const token = { type };
|
|
344
|
+
if (location) {
|
|
345
|
+
token.loc = createLocation(context.startLoc, context.endLoc);
|
|
346
|
+
}
|
|
347
|
+
if (value != null) {
|
|
348
|
+
token.value = value;
|
|
349
|
+
}
|
|
350
|
+
return token;
|
|
351
|
+
}
|
|
352
|
+
const getEndToken = (context) => getToken(context, 14 /* TokenTypes.EOF */);
|
|
353
|
+
function eat(scnr, ch) {
|
|
354
|
+
if (scnr.currentChar() === ch) {
|
|
355
|
+
scnr.next();
|
|
356
|
+
return ch;
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
|
|
360
|
+
return '';
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function peekSpaces(scnr) {
|
|
364
|
+
let buf = '';
|
|
365
|
+
while (scnr.currentPeek() === CHAR_SP || scnr.currentPeek() === CHAR_LF) {
|
|
366
|
+
buf += scnr.currentPeek();
|
|
367
|
+
scnr.peek();
|
|
368
|
+
}
|
|
369
|
+
return buf;
|
|
370
|
+
}
|
|
371
|
+
function skipSpaces(scnr) {
|
|
372
|
+
const buf = peekSpaces(scnr);
|
|
373
|
+
scnr.skipToPeek();
|
|
374
|
+
return buf;
|
|
375
|
+
}
|
|
376
|
+
function isIdentifierStart(ch) {
|
|
377
|
+
if (ch === EOF) {
|
|
378
|
+
return false;
|
|
379
|
+
}
|
|
380
|
+
const cc = ch.charCodeAt(0);
|
|
381
|
+
return ((cc >= 97 && cc <= 122) || // a-z
|
|
382
|
+
(cc >= 65 && cc <= 90) || // A-Z
|
|
383
|
+
cc === 95 // _
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
function isNumberStart(ch) {
|
|
387
|
+
if (ch === EOF) {
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
const cc = ch.charCodeAt(0);
|
|
391
|
+
return cc >= 48 && cc <= 57; // 0-9
|
|
392
|
+
}
|
|
393
|
+
function isNamedIdentifierStart(scnr, context) {
|
|
394
|
+
const { currentType } = context;
|
|
395
|
+
if (currentType !== 2 /* TokenTypes.BraceLeft */) {
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
peekSpaces(scnr);
|
|
399
|
+
const ret = isIdentifierStart(scnr.currentPeek());
|
|
400
|
+
scnr.resetPeek();
|
|
401
|
+
return ret;
|
|
402
|
+
}
|
|
403
|
+
function isListIdentifierStart(scnr, context) {
|
|
404
|
+
const { currentType } = context;
|
|
405
|
+
if (currentType !== 2 /* TokenTypes.BraceLeft */) {
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
peekSpaces(scnr);
|
|
409
|
+
const ch = scnr.currentPeek() === '-' ? scnr.peek() : scnr.currentPeek();
|
|
410
|
+
const ret = isNumberStart(ch);
|
|
411
|
+
scnr.resetPeek();
|
|
412
|
+
return ret;
|
|
413
|
+
}
|
|
414
|
+
function isLiteralStart(scnr, context) {
|
|
415
|
+
const { currentType } = context;
|
|
416
|
+
if (currentType !== 2 /* TokenTypes.BraceLeft */) {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
peekSpaces(scnr);
|
|
420
|
+
const ret = scnr.currentPeek() === LITERAL_DELIMITER;
|
|
421
|
+
scnr.resetPeek();
|
|
422
|
+
return ret;
|
|
423
|
+
}
|
|
424
|
+
function isLinkedDotStart(scnr, context) {
|
|
425
|
+
const { currentType } = context;
|
|
426
|
+
if (currentType !== 8 /* TokenTypes.LinkedAlias */) {
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
peekSpaces(scnr);
|
|
430
|
+
const ret = scnr.currentPeek() === "." /* TokenChars.LinkedDot */;
|
|
431
|
+
scnr.resetPeek();
|
|
432
|
+
return ret;
|
|
433
|
+
}
|
|
434
|
+
function isLinkedModifierStart(scnr, context) {
|
|
435
|
+
const { currentType } = context;
|
|
436
|
+
if (currentType !== 9 /* TokenTypes.LinkedDot */) {
|
|
437
|
+
return false;
|
|
438
|
+
}
|
|
439
|
+
peekSpaces(scnr);
|
|
440
|
+
const ret = isIdentifierStart(scnr.currentPeek());
|
|
441
|
+
scnr.resetPeek();
|
|
442
|
+
return ret;
|
|
443
|
+
}
|
|
444
|
+
function isLinkedDelimiterStart(scnr, context) {
|
|
445
|
+
const { currentType } = context;
|
|
446
|
+
if (!(currentType === 8 /* TokenTypes.LinkedAlias */ ||
|
|
447
|
+
currentType === 12 /* TokenTypes.LinkedModifier */)) {
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
peekSpaces(scnr);
|
|
451
|
+
const ret = scnr.currentPeek() === ":" /* TokenChars.LinkedDelimiter */;
|
|
452
|
+
scnr.resetPeek();
|
|
453
|
+
return ret;
|
|
454
|
+
}
|
|
455
|
+
function isLinkedReferStart(scnr, context) {
|
|
456
|
+
const { currentType } = context;
|
|
457
|
+
if (currentType !== 10 /* TokenTypes.LinkedDelimiter */) {
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
const fn = () => {
|
|
461
|
+
const ch = scnr.currentPeek();
|
|
462
|
+
if (ch === "{" /* TokenChars.BraceLeft */) {
|
|
463
|
+
return isIdentifierStart(scnr.peek());
|
|
464
|
+
}
|
|
465
|
+
else if (ch === "@" /* TokenChars.LinkedAlias */ ||
|
|
466
|
+
ch === "%" /* TokenChars.Modulo */ ||
|
|
467
|
+
ch === "|" /* TokenChars.Pipe */ ||
|
|
468
|
+
ch === ":" /* TokenChars.LinkedDelimiter */ ||
|
|
469
|
+
ch === "." /* TokenChars.LinkedDot */ ||
|
|
470
|
+
ch === CHAR_SP ||
|
|
471
|
+
!ch) {
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
else if (ch === CHAR_LF) {
|
|
475
|
+
scnr.peek();
|
|
476
|
+
return fn();
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
// other characters
|
|
480
|
+
return isIdentifierStart(ch);
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
const ret = fn();
|
|
484
|
+
scnr.resetPeek();
|
|
485
|
+
return ret;
|
|
486
|
+
}
|
|
487
|
+
function isPluralStart(scnr) {
|
|
488
|
+
peekSpaces(scnr);
|
|
489
|
+
const ret = scnr.currentPeek() === "|" /* TokenChars.Pipe */;
|
|
490
|
+
scnr.resetPeek();
|
|
491
|
+
return ret;
|
|
492
|
+
}
|
|
493
|
+
function detectModuloStart(scnr) {
|
|
494
|
+
const spaces = peekSpaces(scnr);
|
|
495
|
+
const ret = scnr.currentPeek() === "%" /* TokenChars.Modulo */ &&
|
|
496
|
+
scnr.peek() === "{" /* TokenChars.BraceLeft */;
|
|
497
|
+
scnr.resetPeek();
|
|
498
|
+
return {
|
|
499
|
+
isModulo: ret,
|
|
500
|
+
hasSpace: spaces.length > 0
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
function isTextStart(scnr, reset = true) {
|
|
504
|
+
const fn = (hasSpace = false, prev = '', detectModulo = false) => {
|
|
505
|
+
const ch = scnr.currentPeek();
|
|
506
|
+
if (ch === "{" /* TokenChars.BraceLeft */) {
|
|
507
|
+
return prev === "%" /* TokenChars.Modulo */ ? false : hasSpace;
|
|
508
|
+
}
|
|
509
|
+
else if (ch === "@" /* TokenChars.LinkedAlias */ || !ch) {
|
|
510
|
+
return prev === "%" /* TokenChars.Modulo */ ? true : hasSpace;
|
|
511
|
+
}
|
|
512
|
+
else if (ch === "%" /* TokenChars.Modulo */) {
|
|
513
|
+
scnr.peek();
|
|
514
|
+
return fn(hasSpace, "%" /* TokenChars.Modulo */, true);
|
|
515
|
+
}
|
|
516
|
+
else if (ch === "|" /* TokenChars.Pipe */) {
|
|
517
|
+
return prev === "%" /* TokenChars.Modulo */ || detectModulo
|
|
518
|
+
? true
|
|
519
|
+
: !(prev === CHAR_SP || prev === CHAR_LF);
|
|
520
|
+
}
|
|
521
|
+
else if (ch === CHAR_SP) {
|
|
522
|
+
scnr.peek();
|
|
523
|
+
return fn(true, CHAR_SP, detectModulo);
|
|
524
|
+
}
|
|
525
|
+
else if (ch === CHAR_LF) {
|
|
526
|
+
scnr.peek();
|
|
527
|
+
return fn(true, CHAR_LF, detectModulo);
|
|
528
|
+
}
|
|
529
|
+
else {
|
|
530
|
+
return true;
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const ret = fn();
|
|
534
|
+
reset && scnr.resetPeek();
|
|
535
|
+
return ret;
|
|
536
|
+
}
|
|
537
|
+
function takeChar(scnr, fn) {
|
|
538
|
+
const ch = scnr.currentChar();
|
|
539
|
+
if (ch === EOF) {
|
|
540
|
+
return EOF;
|
|
541
|
+
}
|
|
542
|
+
if (fn(ch)) {
|
|
543
|
+
scnr.next();
|
|
544
|
+
return ch;
|
|
545
|
+
}
|
|
546
|
+
return null;
|
|
547
|
+
}
|
|
548
|
+
function takeIdentifierChar(scnr) {
|
|
549
|
+
const closure = (ch) => {
|
|
550
|
+
const cc = ch.charCodeAt(0);
|
|
551
|
+
return ((cc >= 97 && cc <= 122) || // a-z
|
|
552
|
+
(cc >= 65 && cc <= 90) || // A-Z
|
|
553
|
+
(cc >= 48 && cc <= 57) || // 0-9
|
|
554
|
+
cc === 95 || // _
|
|
555
|
+
cc === 36 // $
|
|
556
|
+
);
|
|
557
|
+
};
|
|
558
|
+
return takeChar(scnr, closure);
|
|
559
|
+
}
|
|
560
|
+
function takeDigit(scnr) {
|
|
561
|
+
const closure = (ch) => {
|
|
562
|
+
const cc = ch.charCodeAt(0);
|
|
563
|
+
return cc >= 48 && cc <= 57; // 0-9
|
|
564
|
+
};
|
|
565
|
+
return takeChar(scnr, closure);
|
|
566
|
+
}
|
|
567
|
+
function takeHexDigit(scnr) {
|
|
568
|
+
const closure = (ch) => {
|
|
569
|
+
const cc = ch.charCodeAt(0);
|
|
570
|
+
return ((cc >= 48 && cc <= 57) || // 0-9
|
|
571
|
+
(cc >= 65 && cc <= 70) || // A-F
|
|
572
|
+
(cc >= 97 && cc <= 102)); // a-f
|
|
573
|
+
};
|
|
574
|
+
return takeChar(scnr, closure);
|
|
575
|
+
}
|
|
576
|
+
function getDigits(scnr) {
|
|
577
|
+
let ch = '';
|
|
578
|
+
let num = '';
|
|
579
|
+
while ((ch = takeDigit(scnr))) {
|
|
580
|
+
num += ch;
|
|
581
|
+
}
|
|
582
|
+
return num;
|
|
583
|
+
}
|
|
584
|
+
function readModulo(scnr) {
|
|
585
|
+
skipSpaces(scnr);
|
|
586
|
+
const ch = scnr.currentChar();
|
|
587
|
+
if (ch !== "%" /* TokenChars.Modulo */) {
|
|
588
|
+
emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
|
|
589
|
+
}
|
|
590
|
+
scnr.next();
|
|
591
|
+
return "%" /* TokenChars.Modulo */;
|
|
592
|
+
}
|
|
593
|
+
function readText(scnr) {
|
|
594
|
+
let buf = '';
|
|
595
|
+
while (true) {
|
|
596
|
+
const ch = scnr.currentChar();
|
|
597
|
+
if (ch === "{" /* TokenChars.BraceLeft */ ||
|
|
598
|
+
ch === "}" /* TokenChars.BraceRight */ ||
|
|
599
|
+
ch === "@" /* TokenChars.LinkedAlias */ ||
|
|
600
|
+
ch === "|" /* TokenChars.Pipe */ ||
|
|
601
|
+
!ch) {
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
else if (ch === "%" /* TokenChars.Modulo */) {
|
|
605
|
+
if (isTextStart(scnr)) {
|
|
606
|
+
buf += ch;
|
|
607
|
+
scnr.next();
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
break;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
else if (ch === CHAR_SP || ch === CHAR_LF) {
|
|
614
|
+
if (isTextStart(scnr)) {
|
|
615
|
+
buf += ch;
|
|
616
|
+
scnr.next();
|
|
617
|
+
}
|
|
618
|
+
else if (isPluralStart(scnr)) {
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
else {
|
|
622
|
+
buf += ch;
|
|
623
|
+
scnr.next();
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
buf += ch;
|
|
628
|
+
scnr.next();
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return buf;
|
|
632
|
+
}
|
|
633
|
+
function readNamedIdentifier(scnr) {
|
|
634
|
+
skipSpaces(scnr);
|
|
635
|
+
let ch = '';
|
|
636
|
+
let name = '';
|
|
637
|
+
while ((ch = takeIdentifierChar(scnr))) {
|
|
638
|
+
name += ch;
|
|
639
|
+
}
|
|
640
|
+
if (scnr.currentChar() === EOF) {
|
|
641
|
+
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
642
|
+
}
|
|
643
|
+
return name;
|
|
644
|
+
}
|
|
645
|
+
function readListIdentifier(scnr) {
|
|
646
|
+
skipSpaces(scnr);
|
|
647
|
+
let value = '';
|
|
648
|
+
if (scnr.currentChar() === '-') {
|
|
649
|
+
scnr.next();
|
|
650
|
+
value += `-${getDigits(scnr)}`;
|
|
651
|
+
}
|
|
652
|
+
else {
|
|
653
|
+
value += getDigits(scnr);
|
|
654
|
+
}
|
|
655
|
+
if (scnr.currentChar() === EOF) {
|
|
656
|
+
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
657
|
+
}
|
|
658
|
+
return value;
|
|
659
|
+
}
|
|
660
|
+
function readLiteral(scnr) {
|
|
661
|
+
skipSpaces(scnr);
|
|
662
|
+
eat(scnr, `\'`);
|
|
663
|
+
let ch = '';
|
|
664
|
+
let literal = '';
|
|
665
|
+
const fn = (x) => x !== LITERAL_DELIMITER && x !== CHAR_LF;
|
|
666
|
+
while ((ch = takeChar(scnr, fn))) {
|
|
667
|
+
if (ch === '\\') {
|
|
668
|
+
literal += readEscapeSequence(scnr);
|
|
669
|
+
}
|
|
670
|
+
else {
|
|
671
|
+
literal += ch;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
const current = scnr.currentChar();
|
|
675
|
+
if (current === CHAR_LF || current === EOF) {
|
|
676
|
+
emitError(CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER, currentPosition(), 0);
|
|
677
|
+
// TODO: Is it correct really?
|
|
678
|
+
if (current === CHAR_LF) {
|
|
679
|
+
scnr.next();
|
|
680
|
+
eat(scnr, `\'`);
|
|
681
|
+
}
|
|
682
|
+
return literal;
|
|
683
|
+
}
|
|
684
|
+
eat(scnr, `\'`);
|
|
685
|
+
return literal;
|
|
686
|
+
}
|
|
687
|
+
function readEscapeSequence(scnr) {
|
|
688
|
+
const ch = scnr.currentChar();
|
|
689
|
+
switch (ch) {
|
|
690
|
+
case '\\':
|
|
691
|
+
case `\'`:
|
|
692
|
+
scnr.next();
|
|
693
|
+
return `\\${ch}`;
|
|
694
|
+
case 'u':
|
|
695
|
+
return readUnicodeEscapeSequence(scnr, ch, 4);
|
|
696
|
+
case 'U':
|
|
697
|
+
return readUnicodeEscapeSequence(scnr, ch, 6);
|
|
698
|
+
default:
|
|
699
|
+
emitError(CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE, currentPosition(), 0, ch);
|
|
700
|
+
return '';
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
function readUnicodeEscapeSequence(scnr, unicode, digits) {
|
|
704
|
+
eat(scnr, unicode);
|
|
705
|
+
let sequence = '';
|
|
706
|
+
for (let i = 0; i < digits; i++) {
|
|
707
|
+
const ch = takeHexDigit(scnr);
|
|
708
|
+
if (!ch) {
|
|
709
|
+
emitError(CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE, currentPosition(), 0, `\\${unicode}${sequence}${scnr.currentChar()}`);
|
|
710
|
+
break;
|
|
711
|
+
}
|
|
712
|
+
sequence += ch;
|
|
713
|
+
}
|
|
714
|
+
return `\\${unicode}${sequence}`;
|
|
715
|
+
}
|
|
716
|
+
function readInvalidIdentifier(scnr) {
|
|
717
|
+
skipSpaces(scnr);
|
|
718
|
+
let ch = '';
|
|
719
|
+
let identifiers = '';
|
|
720
|
+
const closure = (ch) => ch !== "{" /* TokenChars.BraceLeft */ &&
|
|
721
|
+
ch !== "}" /* TokenChars.BraceRight */ &&
|
|
722
|
+
ch !== CHAR_SP &&
|
|
723
|
+
ch !== CHAR_LF;
|
|
724
|
+
while ((ch = takeChar(scnr, closure))) {
|
|
725
|
+
identifiers += ch;
|
|
726
|
+
}
|
|
727
|
+
return identifiers;
|
|
728
|
+
}
|
|
729
|
+
function readLinkedModifier(scnr) {
|
|
730
|
+
let ch = '';
|
|
731
|
+
let name = '';
|
|
732
|
+
while ((ch = takeIdentifierChar(scnr))) {
|
|
733
|
+
name += ch;
|
|
734
|
+
}
|
|
735
|
+
return name;
|
|
736
|
+
}
|
|
737
|
+
function readLinkedRefer(scnr) {
|
|
738
|
+
const fn = (detect = false, buf) => {
|
|
739
|
+
const ch = scnr.currentChar();
|
|
740
|
+
if (ch === "{" /* TokenChars.BraceLeft */ ||
|
|
741
|
+
ch === "%" /* TokenChars.Modulo */ ||
|
|
742
|
+
ch === "@" /* TokenChars.LinkedAlias */ ||
|
|
743
|
+
ch === "|" /* TokenChars.Pipe */ ||
|
|
744
|
+
ch === "(" /* TokenChars.ParenLeft */ ||
|
|
745
|
+
ch === ")" /* TokenChars.ParenRight */ ||
|
|
746
|
+
!ch) {
|
|
747
|
+
return buf;
|
|
748
|
+
}
|
|
749
|
+
else if (ch === CHAR_SP) {
|
|
750
|
+
return buf;
|
|
751
|
+
}
|
|
752
|
+
else if (ch === CHAR_LF || ch === DOT) {
|
|
753
|
+
buf += ch;
|
|
754
|
+
scnr.next();
|
|
755
|
+
return fn(detect, buf);
|
|
756
|
+
}
|
|
757
|
+
else {
|
|
758
|
+
buf += ch;
|
|
759
|
+
scnr.next();
|
|
760
|
+
return fn(true, buf);
|
|
761
|
+
}
|
|
762
|
+
};
|
|
763
|
+
return fn(false, '');
|
|
764
|
+
}
|
|
765
|
+
function readPlural(scnr) {
|
|
766
|
+
skipSpaces(scnr);
|
|
767
|
+
const plural = eat(scnr, "|" /* TokenChars.Pipe */);
|
|
768
|
+
skipSpaces(scnr);
|
|
769
|
+
return plural;
|
|
770
|
+
}
|
|
771
|
+
// TODO: We need refactoring of token parsing ...
|
|
772
|
+
function readTokenInPlaceholder(scnr, context) {
|
|
773
|
+
let token = null;
|
|
774
|
+
const ch = scnr.currentChar();
|
|
775
|
+
switch (ch) {
|
|
776
|
+
case "{" /* TokenChars.BraceLeft */:
|
|
777
|
+
if (context.braceNest >= 1) {
|
|
778
|
+
emitError(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER, currentPosition(), 0);
|
|
779
|
+
}
|
|
780
|
+
scnr.next();
|
|
781
|
+
token = getToken(context, 2 /* TokenTypes.BraceLeft */, "{" /* TokenChars.BraceLeft */);
|
|
782
|
+
skipSpaces(scnr);
|
|
783
|
+
context.braceNest++;
|
|
784
|
+
return token;
|
|
785
|
+
case "}" /* TokenChars.BraceRight */:
|
|
786
|
+
if (context.braceNest > 0 &&
|
|
787
|
+
context.currentType === 2 /* TokenTypes.BraceLeft */) {
|
|
788
|
+
emitError(CompileErrorCodes.EMPTY_PLACEHOLDER, currentPosition(), 0);
|
|
789
|
+
}
|
|
790
|
+
scnr.next();
|
|
791
|
+
token = getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
|
|
792
|
+
context.braceNest--;
|
|
793
|
+
context.braceNest > 0 && skipSpaces(scnr);
|
|
794
|
+
if (context.inLinked && context.braceNest === 0) {
|
|
795
|
+
context.inLinked = false;
|
|
796
|
+
}
|
|
797
|
+
return token;
|
|
798
|
+
case "@" /* TokenChars.LinkedAlias */:
|
|
799
|
+
if (context.braceNest > 0) {
|
|
800
|
+
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
801
|
+
}
|
|
802
|
+
token = readTokenInLinked(scnr, context) || getEndToken(context);
|
|
803
|
+
context.braceNest = 0;
|
|
804
|
+
return token;
|
|
805
|
+
default:
|
|
806
|
+
let validNamedIdentifier = true;
|
|
807
|
+
let validListIdentifier = true;
|
|
808
|
+
let validLiteral = true;
|
|
809
|
+
if (isPluralStart(scnr)) {
|
|
810
|
+
if (context.braceNest > 0) {
|
|
811
|
+
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
812
|
+
}
|
|
813
|
+
token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
|
|
814
|
+
// reset
|
|
815
|
+
context.braceNest = 0;
|
|
816
|
+
context.inLinked = false;
|
|
817
|
+
return token;
|
|
818
|
+
}
|
|
819
|
+
if (context.braceNest > 0 &&
|
|
820
|
+
(context.currentType === 5 /* TokenTypes.Named */ ||
|
|
821
|
+
context.currentType === 6 /* TokenTypes.List */ ||
|
|
822
|
+
context.currentType === 7 /* TokenTypes.Literal */)) {
|
|
823
|
+
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
824
|
+
context.braceNest = 0;
|
|
825
|
+
return readToken(scnr, context);
|
|
826
|
+
}
|
|
827
|
+
if ((validNamedIdentifier = isNamedIdentifierStart(scnr, context))) {
|
|
828
|
+
token = getToken(context, 5 /* TokenTypes.Named */, readNamedIdentifier(scnr));
|
|
829
|
+
skipSpaces(scnr);
|
|
830
|
+
return token;
|
|
831
|
+
}
|
|
832
|
+
if ((validListIdentifier = isListIdentifierStart(scnr, context))) {
|
|
833
|
+
token = getToken(context, 6 /* TokenTypes.List */, readListIdentifier(scnr));
|
|
834
|
+
skipSpaces(scnr);
|
|
835
|
+
return token;
|
|
836
|
+
}
|
|
837
|
+
if ((validLiteral = isLiteralStart(scnr, context))) {
|
|
838
|
+
token = getToken(context, 7 /* TokenTypes.Literal */, readLiteral(scnr));
|
|
839
|
+
skipSpaces(scnr);
|
|
840
|
+
return token;
|
|
841
|
+
}
|
|
842
|
+
if (!validNamedIdentifier && !validListIdentifier && !validLiteral) {
|
|
843
|
+
// TODO: we should be re-designed invalid cases, when we will extend message syntax near the future ...
|
|
844
|
+
token = getToken(context, 13 /* TokenTypes.InvalidPlace */, readInvalidIdentifier(scnr));
|
|
845
|
+
emitError(CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER, currentPosition(), 0, token.value);
|
|
846
|
+
skipSpaces(scnr);
|
|
847
|
+
return token;
|
|
848
|
+
}
|
|
849
|
+
break;
|
|
850
|
+
}
|
|
851
|
+
return token;
|
|
852
|
+
}
|
|
853
|
+
// TODO: We need refactoring of token parsing ...
|
|
854
|
+
function readTokenInLinked(scnr, context) {
|
|
855
|
+
const { currentType } = context;
|
|
856
|
+
let token = null;
|
|
857
|
+
const ch = scnr.currentChar();
|
|
858
|
+
if ((currentType === 8 /* TokenTypes.LinkedAlias */ ||
|
|
859
|
+
currentType === 9 /* TokenTypes.LinkedDot */ ||
|
|
860
|
+
currentType === 12 /* TokenTypes.LinkedModifier */ ||
|
|
861
|
+
currentType === 10 /* TokenTypes.LinkedDelimiter */) &&
|
|
862
|
+
(ch === CHAR_LF || ch === CHAR_SP)) {
|
|
863
|
+
emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
|
|
864
|
+
}
|
|
865
|
+
switch (ch) {
|
|
866
|
+
case "@" /* TokenChars.LinkedAlias */:
|
|
867
|
+
scnr.next();
|
|
868
|
+
token = getToken(context, 8 /* TokenTypes.LinkedAlias */, "@" /* TokenChars.LinkedAlias */);
|
|
869
|
+
context.inLinked = true;
|
|
870
|
+
return token;
|
|
871
|
+
case "." /* TokenChars.LinkedDot */:
|
|
872
|
+
skipSpaces(scnr);
|
|
873
|
+
scnr.next();
|
|
874
|
+
return getToken(context, 9 /* TokenTypes.LinkedDot */, "." /* TokenChars.LinkedDot */);
|
|
875
|
+
case ":" /* TokenChars.LinkedDelimiter */:
|
|
876
|
+
skipSpaces(scnr);
|
|
877
|
+
scnr.next();
|
|
878
|
+
return getToken(context, 10 /* TokenTypes.LinkedDelimiter */, ":" /* TokenChars.LinkedDelimiter */);
|
|
879
|
+
default:
|
|
880
|
+
if (isPluralStart(scnr)) {
|
|
881
|
+
token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
|
|
882
|
+
// reset
|
|
883
|
+
context.braceNest = 0;
|
|
884
|
+
context.inLinked = false;
|
|
885
|
+
return token;
|
|
886
|
+
}
|
|
887
|
+
if (isLinkedDotStart(scnr, context) ||
|
|
888
|
+
isLinkedDelimiterStart(scnr, context)) {
|
|
889
|
+
skipSpaces(scnr);
|
|
890
|
+
return readTokenInLinked(scnr, context);
|
|
891
|
+
}
|
|
892
|
+
if (isLinkedModifierStart(scnr, context)) {
|
|
893
|
+
skipSpaces(scnr);
|
|
894
|
+
return getToken(context, 12 /* TokenTypes.LinkedModifier */, readLinkedModifier(scnr));
|
|
895
|
+
}
|
|
896
|
+
if (isLinkedReferStart(scnr, context)) {
|
|
897
|
+
skipSpaces(scnr);
|
|
898
|
+
if (ch === "{" /* TokenChars.BraceLeft */) {
|
|
899
|
+
// scan the placeholder
|
|
900
|
+
return readTokenInPlaceholder(scnr, context) || token;
|
|
901
|
+
}
|
|
902
|
+
else {
|
|
903
|
+
return getToken(context, 11 /* TokenTypes.LinkedKey */, readLinkedRefer(scnr));
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
if (currentType === 8 /* TokenTypes.LinkedAlias */) {
|
|
907
|
+
emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
|
|
908
|
+
}
|
|
909
|
+
context.braceNest = 0;
|
|
910
|
+
context.inLinked = false;
|
|
911
|
+
return readToken(scnr, context);
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
// TODO: We need refactoring of token parsing ...
|
|
915
|
+
function readToken(scnr, context) {
|
|
916
|
+
let token = { type: 14 /* TokenTypes.EOF */ };
|
|
917
|
+
if (context.braceNest > 0) {
|
|
918
|
+
return readTokenInPlaceholder(scnr, context) || getEndToken(context);
|
|
919
|
+
}
|
|
920
|
+
if (context.inLinked) {
|
|
921
|
+
return readTokenInLinked(scnr, context) || getEndToken(context);
|
|
922
|
+
}
|
|
923
|
+
const ch = scnr.currentChar();
|
|
924
|
+
switch (ch) {
|
|
925
|
+
case "{" /* TokenChars.BraceLeft */:
|
|
926
|
+
return readTokenInPlaceholder(scnr, context) || getEndToken(context);
|
|
927
|
+
case "}" /* TokenChars.BraceRight */:
|
|
928
|
+
emitError(CompileErrorCodes.UNBALANCED_CLOSING_BRACE, currentPosition(), 0);
|
|
929
|
+
scnr.next();
|
|
930
|
+
return getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
|
|
931
|
+
case "@" /* TokenChars.LinkedAlias */:
|
|
932
|
+
return readTokenInLinked(scnr, context) || getEndToken(context);
|
|
933
|
+
default:
|
|
934
|
+
if (isPluralStart(scnr)) {
|
|
935
|
+
token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
|
|
936
|
+
// reset
|
|
937
|
+
context.braceNest = 0;
|
|
938
|
+
context.inLinked = false;
|
|
939
|
+
return token;
|
|
940
|
+
}
|
|
941
|
+
const { isModulo, hasSpace } = detectModuloStart(scnr);
|
|
942
|
+
if (isModulo) {
|
|
943
|
+
return hasSpace
|
|
944
|
+
? getToken(context, 0 /* TokenTypes.Text */, readText(scnr))
|
|
945
|
+
: getToken(context, 4 /* TokenTypes.Modulo */, readModulo(scnr));
|
|
946
|
+
}
|
|
947
|
+
if (isTextStart(scnr)) {
|
|
948
|
+
return getToken(context, 0 /* TokenTypes.Text */, readText(scnr));
|
|
949
|
+
}
|
|
950
|
+
break;
|
|
951
|
+
}
|
|
952
|
+
return token;
|
|
953
|
+
}
|
|
954
|
+
function nextToken() {
|
|
955
|
+
const { currentType, offset, startLoc, endLoc } = _context;
|
|
956
|
+
_context.lastType = currentType;
|
|
957
|
+
_context.lastOffset = offset;
|
|
958
|
+
_context.lastStartLoc = startLoc;
|
|
959
|
+
_context.lastEndLoc = endLoc;
|
|
960
|
+
_context.offset = currentOffset();
|
|
961
|
+
_context.startLoc = currentPosition();
|
|
962
|
+
if (_scnr.currentChar() === EOF) {
|
|
963
|
+
return getToken(_context, 14 /* TokenTypes.EOF */);
|
|
964
|
+
}
|
|
965
|
+
return readToken(_scnr, _context);
|
|
966
|
+
}
|
|
967
|
+
return {
|
|
968
|
+
nextToken,
|
|
969
|
+
currentOffset,
|
|
970
|
+
currentPosition,
|
|
971
|
+
context
|
|
972
|
+
};
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
const ERROR_DOMAIN$2 = 'parser';
|
|
976
|
+
// Backslash backslash, backslash quote, uHHHH, UHHHHHH.
|
|
977
|
+
const KNOWN_ESCAPES = /(?:\\\\|\\'|\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{6}))/g;
|
|
978
|
+
function fromEscapeSequence(match, codePoint4, codePoint6) {
|
|
979
|
+
switch (match) {
|
|
980
|
+
case `\\\\`:
|
|
981
|
+
return `\\`;
|
|
982
|
+
case `\\\'`:
|
|
983
|
+
return `\'`;
|
|
984
|
+
default: {
|
|
985
|
+
const codePoint = parseInt(codePoint4 || codePoint6, 16);
|
|
986
|
+
if (codePoint <= 0xd7ff || codePoint >= 0xe000) {
|
|
987
|
+
return String.fromCodePoint(codePoint);
|
|
988
|
+
}
|
|
989
|
+
// invalid ...
|
|
990
|
+
// Replace them with U+FFFD REPLACEMENT CHARACTER.
|
|
991
|
+
return '�';
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
function createParser(options = {}) {
|
|
996
|
+
const location = options.location !== false;
|
|
997
|
+
const { onError } = options;
|
|
998
|
+
function emitError(tokenzer, code, start, offset, ...args) {
|
|
999
|
+
const end = tokenzer.currentPosition();
|
|
1000
|
+
end.offset += offset;
|
|
1001
|
+
end.column += offset;
|
|
1002
|
+
if (onError) {
|
|
1003
|
+
const loc = location ? createLocation(start, end) : null;
|
|
1004
|
+
const err = createCompileError(code, loc, {
|
|
1005
|
+
domain: ERROR_DOMAIN$2,
|
|
1006
|
+
args
|
|
1007
|
+
});
|
|
1008
|
+
onError(err);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
function startNode(type, offset, loc) {
|
|
1012
|
+
const node = { type };
|
|
1013
|
+
if (location) {
|
|
1014
|
+
node.start = offset;
|
|
1015
|
+
node.end = offset;
|
|
1016
|
+
node.loc = { start: loc, end: loc };
|
|
1017
|
+
}
|
|
1018
|
+
return node;
|
|
1019
|
+
}
|
|
1020
|
+
function endNode(node, offset, pos, type) {
|
|
1021
|
+
if (type) {
|
|
1022
|
+
node.type = type;
|
|
1023
|
+
}
|
|
1024
|
+
if (location) {
|
|
1025
|
+
node.end = offset;
|
|
1026
|
+
if (node.loc) {
|
|
1027
|
+
node.loc.end = pos;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
function parseText(tokenizer, value) {
|
|
1032
|
+
const context = tokenizer.context();
|
|
1033
|
+
const node = startNode(3 /* NodeTypes.Text */, context.offset, context.startLoc);
|
|
1034
|
+
node.value = value;
|
|
1035
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1036
|
+
return node;
|
|
1037
|
+
}
|
|
1038
|
+
function parseList(tokenizer, index) {
|
|
1039
|
+
const context = tokenizer.context();
|
|
1040
|
+
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
1041
|
+
const node = startNode(5 /* NodeTypes.List */, offset, loc);
|
|
1042
|
+
node.index = parseInt(index, 10);
|
|
1043
|
+
tokenizer.nextToken(); // skip brach right
|
|
1044
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1045
|
+
return node;
|
|
1046
|
+
}
|
|
1047
|
+
function parseNamed(tokenizer, key) {
|
|
1048
|
+
const context = tokenizer.context();
|
|
1049
|
+
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
1050
|
+
const node = startNode(4 /* NodeTypes.Named */, offset, loc);
|
|
1051
|
+
node.key = key;
|
|
1052
|
+
tokenizer.nextToken(); // skip brach right
|
|
1053
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1054
|
+
return node;
|
|
1055
|
+
}
|
|
1056
|
+
function parseLiteral(tokenizer, value) {
|
|
1057
|
+
const context = tokenizer.context();
|
|
1058
|
+
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
1059
|
+
const node = startNode(9 /* NodeTypes.Literal */, offset, loc);
|
|
1060
|
+
node.value = value.replace(KNOWN_ESCAPES, fromEscapeSequence);
|
|
1061
|
+
tokenizer.nextToken(); // skip brach right
|
|
1062
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1063
|
+
return node;
|
|
1064
|
+
}
|
|
1065
|
+
function parseLinkedModifier(tokenizer) {
|
|
1066
|
+
const token = tokenizer.nextToken();
|
|
1067
|
+
const context = tokenizer.context();
|
|
1068
|
+
const { lastOffset: offset, lastStartLoc: loc } = context; // get linked dot loc
|
|
1069
|
+
const node = startNode(8 /* NodeTypes.LinkedModifier */, offset, loc);
|
|
1070
|
+
if (token.type !== 12 /* TokenTypes.LinkedModifier */) {
|
|
1071
|
+
// empty modifier
|
|
1072
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER, context.lastStartLoc, 0);
|
|
1073
|
+
node.value = '';
|
|
1074
|
+
endNode(node, offset, loc);
|
|
1075
|
+
return {
|
|
1076
|
+
nextConsumeToken: token,
|
|
1077
|
+
node
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
// check token
|
|
1081
|
+
if (token.value == null) {
|
|
1082
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1083
|
+
}
|
|
1084
|
+
node.value = token.value || '';
|
|
1085
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1086
|
+
return {
|
|
1087
|
+
node
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
function parseLinkedKey(tokenizer, value) {
|
|
1091
|
+
const context = tokenizer.context();
|
|
1092
|
+
const node = startNode(7 /* NodeTypes.LinkedKey */, context.offset, context.startLoc);
|
|
1093
|
+
node.value = value;
|
|
1094
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1095
|
+
return node;
|
|
1096
|
+
}
|
|
1097
|
+
function parseLinked(tokenizer) {
|
|
1098
|
+
const context = tokenizer.context();
|
|
1099
|
+
const linkedNode = startNode(6 /* NodeTypes.Linked */, context.offset, context.startLoc);
|
|
1100
|
+
let token = tokenizer.nextToken();
|
|
1101
|
+
if (token.type === 9 /* TokenTypes.LinkedDot */) {
|
|
1102
|
+
const parsed = parseLinkedModifier(tokenizer);
|
|
1103
|
+
linkedNode.modifier = parsed.node;
|
|
1104
|
+
token = parsed.nextConsumeToken || tokenizer.nextToken();
|
|
1105
|
+
}
|
|
1106
|
+
// asset check token
|
|
1107
|
+
if (token.type !== 10 /* TokenTypes.LinkedDelimiter */) {
|
|
1108
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1109
|
+
}
|
|
1110
|
+
token = tokenizer.nextToken();
|
|
1111
|
+
// skip brace left
|
|
1112
|
+
if (token.type === 2 /* TokenTypes.BraceLeft */) {
|
|
1113
|
+
token = tokenizer.nextToken();
|
|
1114
|
+
}
|
|
1115
|
+
switch (token.type) {
|
|
1116
|
+
case 11 /* TokenTypes.LinkedKey */:
|
|
1117
|
+
if (token.value == null) {
|
|
1118
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1119
|
+
}
|
|
1120
|
+
linkedNode.key = parseLinkedKey(tokenizer, token.value || '');
|
|
1121
|
+
break;
|
|
1122
|
+
case 5 /* TokenTypes.Named */:
|
|
1123
|
+
if (token.value == null) {
|
|
1124
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1125
|
+
}
|
|
1126
|
+
linkedNode.key = parseNamed(tokenizer, token.value || '');
|
|
1127
|
+
break;
|
|
1128
|
+
case 6 /* TokenTypes.List */:
|
|
1129
|
+
if (token.value == null) {
|
|
1130
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1131
|
+
}
|
|
1132
|
+
linkedNode.key = parseList(tokenizer, token.value || '');
|
|
1133
|
+
break;
|
|
1134
|
+
case 7 /* TokenTypes.Literal */:
|
|
1135
|
+
if (token.value == null) {
|
|
1136
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1137
|
+
}
|
|
1138
|
+
linkedNode.key = parseLiteral(tokenizer, token.value || '');
|
|
1139
|
+
break;
|
|
1140
|
+
default:
|
|
1141
|
+
// empty key
|
|
1142
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY, context.lastStartLoc, 0);
|
|
1143
|
+
const nextContext = tokenizer.context();
|
|
1144
|
+
const emptyLinkedKeyNode = startNode(7 /* NodeTypes.LinkedKey */, nextContext.offset, nextContext.startLoc);
|
|
1145
|
+
emptyLinkedKeyNode.value = '';
|
|
1146
|
+
endNode(emptyLinkedKeyNode, nextContext.offset, nextContext.startLoc);
|
|
1147
|
+
linkedNode.key = emptyLinkedKeyNode;
|
|
1148
|
+
endNode(linkedNode, nextContext.offset, nextContext.startLoc);
|
|
1149
|
+
return {
|
|
1150
|
+
nextConsumeToken: token,
|
|
1151
|
+
node: linkedNode
|
|
1152
|
+
};
|
|
1153
|
+
}
|
|
1154
|
+
endNode(linkedNode, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1155
|
+
return {
|
|
1156
|
+
node: linkedNode
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
function parseMessage(tokenizer) {
|
|
1160
|
+
const context = tokenizer.context();
|
|
1161
|
+
const startOffset = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1162
|
+
? tokenizer.currentOffset()
|
|
1163
|
+
: context.offset;
|
|
1164
|
+
const startLoc = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1165
|
+
? context.endLoc
|
|
1166
|
+
: context.startLoc;
|
|
1167
|
+
const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
|
|
1168
|
+
node.items = [];
|
|
1169
|
+
let nextToken = null;
|
|
1170
|
+
do {
|
|
1171
|
+
const token = nextToken || tokenizer.nextToken();
|
|
1172
|
+
nextToken = null;
|
|
1173
|
+
switch (token.type) {
|
|
1174
|
+
case 0 /* TokenTypes.Text */:
|
|
1175
|
+
if (token.value == null) {
|
|
1176
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1177
|
+
}
|
|
1178
|
+
node.items.push(parseText(tokenizer, token.value || ''));
|
|
1179
|
+
break;
|
|
1180
|
+
case 6 /* TokenTypes.List */:
|
|
1181
|
+
if (token.value == null) {
|
|
1182
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1183
|
+
}
|
|
1184
|
+
node.items.push(parseList(tokenizer, token.value || ''));
|
|
1185
|
+
break;
|
|
1186
|
+
case 5 /* TokenTypes.Named */:
|
|
1187
|
+
if (token.value == null) {
|
|
1188
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1189
|
+
}
|
|
1190
|
+
node.items.push(parseNamed(tokenizer, token.value || ''));
|
|
1191
|
+
break;
|
|
1192
|
+
case 7 /* TokenTypes.Literal */:
|
|
1193
|
+
if (token.value == null) {
|
|
1194
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1195
|
+
}
|
|
1196
|
+
node.items.push(parseLiteral(tokenizer, token.value || ''));
|
|
1197
|
+
break;
|
|
1198
|
+
case 8 /* TokenTypes.LinkedAlias */:
|
|
1199
|
+
const parsed = parseLinked(tokenizer);
|
|
1200
|
+
node.items.push(parsed.node);
|
|
1201
|
+
nextToken = parsed.nextConsumeToken || null;
|
|
1202
|
+
break;
|
|
1203
|
+
}
|
|
1204
|
+
} while (context.currentType !== 14 /* TokenTypes.EOF */ &&
|
|
1205
|
+
context.currentType !== 1 /* TokenTypes.Pipe */);
|
|
1206
|
+
// adjust message node loc
|
|
1207
|
+
const endOffset = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1208
|
+
? context.lastOffset
|
|
1209
|
+
: tokenizer.currentOffset();
|
|
1210
|
+
const endLoc = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1211
|
+
? context.lastEndLoc
|
|
1212
|
+
: tokenizer.currentPosition();
|
|
1213
|
+
endNode(node, endOffset, endLoc);
|
|
1214
|
+
return node;
|
|
1215
|
+
}
|
|
1216
|
+
function parsePlural(tokenizer, offset, loc, msgNode) {
|
|
1217
|
+
const context = tokenizer.context();
|
|
1218
|
+
let hasEmptyMessage = msgNode.items.length === 0;
|
|
1219
|
+
const node = startNode(1 /* NodeTypes.Plural */, offset, loc);
|
|
1220
|
+
node.cases = [];
|
|
1221
|
+
node.cases.push(msgNode);
|
|
1222
|
+
do {
|
|
1223
|
+
const msg = parseMessage(tokenizer);
|
|
1224
|
+
if (!hasEmptyMessage) {
|
|
1225
|
+
hasEmptyMessage = msg.items.length === 0;
|
|
1226
|
+
}
|
|
1227
|
+
node.cases.push(msg);
|
|
1228
|
+
} while (context.currentType !== 14 /* TokenTypes.EOF */);
|
|
1229
|
+
if (hasEmptyMessage) {
|
|
1230
|
+
emitError(tokenizer, CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL, loc, 0);
|
|
1231
|
+
}
|
|
1232
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1233
|
+
return node;
|
|
1234
|
+
}
|
|
1235
|
+
function parseResource(tokenizer) {
|
|
1236
|
+
const context = tokenizer.context();
|
|
1237
|
+
const { offset, startLoc } = context;
|
|
1238
|
+
const msgNode = parseMessage(tokenizer);
|
|
1239
|
+
if (context.currentType === 14 /* TokenTypes.EOF */) {
|
|
1240
|
+
return msgNode;
|
|
1241
|
+
}
|
|
1242
|
+
else {
|
|
1243
|
+
return parsePlural(tokenizer, offset, startLoc, msgNode);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
function parse(source) {
|
|
1247
|
+
const tokenizer = createTokenizer(source, assign({}, options));
|
|
1248
|
+
const context = tokenizer.context();
|
|
1249
|
+
const node = startNode(0 /* NodeTypes.Resource */, context.offset, context.startLoc);
|
|
1250
|
+
if (location && node.loc) {
|
|
1251
|
+
node.loc.source = source;
|
|
1252
|
+
}
|
|
1253
|
+
node.body = parseResource(tokenizer);
|
|
1254
|
+
if (options.onCacheKey) {
|
|
1255
|
+
node.cacheKey = options.onCacheKey(source);
|
|
1256
|
+
}
|
|
1257
|
+
// assert whether achieved to EOF
|
|
1258
|
+
if (context.currentType !== 14 /* TokenTypes.EOF */) {
|
|
1259
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, source[context.offset] || '');
|
|
1260
|
+
}
|
|
1261
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1262
|
+
return node;
|
|
1263
|
+
}
|
|
1264
|
+
return { parse };
|
|
1265
|
+
}
|
|
1266
|
+
function getTokenCaption(token) {
|
|
1267
|
+
if (token.type === 14 /* TokenTypes.EOF */) {
|
|
1268
|
+
return 'EOF';
|
|
1269
|
+
}
|
|
1270
|
+
const name = (token.value || '').replace(/\r?\n/gu, '\\n');
|
|
1271
|
+
return name.length > 10 ? name.slice(0, 9) + '…' : name;
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
function createTransformer(ast, options = {} // eslint-disable-line
|
|
1275
|
+
) {
|
|
1276
|
+
const _context = {
|
|
1277
|
+
ast,
|
|
1278
|
+
helpers: new Set()
|
|
1279
|
+
};
|
|
1280
|
+
const context = () => _context;
|
|
1281
|
+
const helper = (name) => {
|
|
1282
|
+
_context.helpers.add(name);
|
|
1283
|
+
return name;
|
|
1284
|
+
};
|
|
1285
|
+
return { context, helper };
|
|
1286
|
+
}
|
|
1287
|
+
function traverseNodes(nodes, transformer) {
|
|
1288
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1289
|
+
traverseNode(nodes[i], transformer);
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
function traverseNode(node, transformer) {
|
|
1293
|
+
// TODO: if we need pre-hook of transform, should be implemented to here
|
|
1294
|
+
switch (node.type) {
|
|
1295
|
+
case 1 /* NodeTypes.Plural */:
|
|
1296
|
+
traverseNodes(node.cases, transformer);
|
|
1297
|
+
transformer.helper("plural" /* HelperNameMap.PLURAL */);
|
|
1298
|
+
break;
|
|
1299
|
+
case 2 /* NodeTypes.Message */:
|
|
1300
|
+
traverseNodes(node.items, transformer);
|
|
1301
|
+
break;
|
|
1302
|
+
case 6 /* NodeTypes.Linked */:
|
|
1303
|
+
const linked = node;
|
|
1304
|
+
traverseNode(linked.key, transformer);
|
|
1305
|
+
transformer.helper("linked" /* HelperNameMap.LINKED */);
|
|
1306
|
+
transformer.helper("type" /* HelperNameMap.TYPE */);
|
|
1307
|
+
break;
|
|
1308
|
+
case 5 /* NodeTypes.List */:
|
|
1309
|
+
transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
|
|
1310
|
+
transformer.helper("list" /* HelperNameMap.LIST */);
|
|
1311
|
+
break;
|
|
1312
|
+
case 4 /* NodeTypes.Named */:
|
|
1313
|
+
transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
|
|
1314
|
+
transformer.helper("named" /* HelperNameMap.NAMED */);
|
|
1315
|
+
break;
|
|
1316
|
+
}
|
|
1317
|
+
// TODO: if we need post-hook of transform, should be implemented to here
|
|
1318
|
+
}
|
|
1319
|
+
// transform AST
|
|
1320
|
+
function transform(ast, options = {} // eslint-disable-line
|
|
1321
|
+
) {
|
|
1322
|
+
const transformer = createTransformer(ast);
|
|
1323
|
+
transformer.helper("normalize" /* HelperNameMap.NORMALIZE */);
|
|
1324
|
+
// traverse
|
|
1325
|
+
ast.body && traverseNode(ast.body, transformer);
|
|
1326
|
+
// set meta information
|
|
1327
|
+
const context = transformer.context();
|
|
1328
|
+
ast.helpers = Array.from(context.helpers);
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
function optimize(ast) {
|
|
1332
|
+
const body = ast.body;
|
|
1333
|
+
if (body.type === 2 /* NodeTypes.Message */) {
|
|
1334
|
+
optimizeMessageNode(body);
|
|
1335
|
+
}
|
|
1336
|
+
else {
|
|
1337
|
+
body.cases.forEach(c => optimizeMessageNode(c));
|
|
1338
|
+
}
|
|
1339
|
+
return ast;
|
|
1340
|
+
}
|
|
1341
|
+
function optimizeMessageNode(message) {
|
|
1342
|
+
if (message.items.length === 1) {
|
|
1343
|
+
const item = message.items[0];
|
|
1344
|
+
if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
|
|
1345
|
+
message.static = item.value;
|
|
1346
|
+
delete item.value; // optimization for size
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
else {
|
|
1350
|
+
const values = [];
|
|
1351
|
+
for (let i = 0; i < message.items.length; i++) {
|
|
1352
|
+
const item = message.items[i];
|
|
1353
|
+
if (!(item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */)) {
|
|
1354
|
+
break;
|
|
1355
|
+
}
|
|
1356
|
+
if (item.value == null) {
|
|
1357
|
+
break;
|
|
1358
|
+
}
|
|
1359
|
+
values.push(item.value);
|
|
1360
|
+
}
|
|
1361
|
+
if (values.length === message.items.length) {
|
|
1362
|
+
message.static = join(values);
|
|
1363
|
+
for (let i = 0; i < message.items.length; i++) {
|
|
1364
|
+
const item = message.items[i];
|
|
1365
|
+
if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
|
|
1366
|
+
delete item.value; // optimization for size
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
const ERROR_DOMAIN$1 = 'minifier';
|
|
1374
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1375
|
+
function minify(node) {
|
|
1376
|
+
node.t = node.type;
|
|
1377
|
+
switch (node.type) {
|
|
1378
|
+
case 0 /* NodeTypes.Resource */:
|
|
1379
|
+
const resource = node;
|
|
1380
|
+
minify(resource.body);
|
|
1381
|
+
resource.b = resource.body;
|
|
1382
|
+
delete resource.body;
|
|
1383
|
+
break;
|
|
1384
|
+
case 1 /* NodeTypes.Plural */:
|
|
1385
|
+
const plural = node;
|
|
1386
|
+
const cases = plural.cases;
|
|
1387
|
+
for (let i = 0; i < cases.length; i++) {
|
|
1388
|
+
minify(cases[i]);
|
|
1389
|
+
}
|
|
1390
|
+
plural.c = cases;
|
|
1391
|
+
delete plural.cases;
|
|
1392
|
+
break;
|
|
1393
|
+
case 2 /* NodeTypes.Message */:
|
|
1394
|
+
const message = node;
|
|
1395
|
+
const items = message.items;
|
|
1396
|
+
for (let i = 0; i < items.length; i++) {
|
|
1397
|
+
minify(items[i]);
|
|
1398
|
+
}
|
|
1399
|
+
message.i = items;
|
|
1400
|
+
delete message.items;
|
|
1401
|
+
if (message.static) {
|
|
1402
|
+
message.s = message.static;
|
|
1403
|
+
delete message.static;
|
|
1404
|
+
}
|
|
1405
|
+
break;
|
|
1406
|
+
case 3 /* NodeTypes.Text */:
|
|
1407
|
+
case 9 /* NodeTypes.Literal */:
|
|
1408
|
+
case 8 /* NodeTypes.LinkedModifier */:
|
|
1409
|
+
case 7 /* NodeTypes.LinkedKey */:
|
|
1410
|
+
const valueNode = node;
|
|
1411
|
+
if (valueNode.value) {
|
|
1412
|
+
valueNode.v = valueNode.value;
|
|
1413
|
+
delete valueNode.value;
|
|
1414
|
+
}
|
|
1415
|
+
break;
|
|
1416
|
+
case 6 /* NodeTypes.Linked */:
|
|
1417
|
+
const linked = node;
|
|
1418
|
+
minify(linked.key);
|
|
1419
|
+
linked.k = linked.key;
|
|
1420
|
+
delete linked.key;
|
|
1421
|
+
if (linked.modifier) {
|
|
1422
|
+
minify(linked.modifier);
|
|
1423
|
+
linked.m = linked.modifier;
|
|
1424
|
+
delete linked.modifier;
|
|
1425
|
+
}
|
|
1426
|
+
break;
|
|
1427
|
+
case 5 /* NodeTypes.List */:
|
|
1428
|
+
const list = node;
|
|
1429
|
+
list.i = list.index;
|
|
1430
|
+
delete list.index;
|
|
1431
|
+
break;
|
|
1432
|
+
case 4 /* NodeTypes.Named */:
|
|
1433
|
+
const named = node;
|
|
1434
|
+
named.k = named.key;
|
|
1435
|
+
delete named.key;
|
|
1436
|
+
break;
|
|
1437
|
+
default:
|
|
1438
|
+
{
|
|
1439
|
+
throw createCompileError(CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE, null, {
|
|
1440
|
+
domain: ERROR_DOMAIN$1,
|
|
1441
|
+
args: [node.type]
|
|
1442
|
+
});
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
delete node.type;
|
|
1446
|
+
}
|
|
1447
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
1448
|
+
|
|
1449
|
+
const ERROR_DOMAIN = 'parser';
|
|
1450
|
+
function createCodeGenerator(ast, options) {
|
|
1451
|
+
const { sourceMap, filename, breakLineCode, needIndent: _needIndent } = options;
|
|
1452
|
+
const location = options.location !== false;
|
|
1453
|
+
const _context = {
|
|
1454
|
+
filename,
|
|
1455
|
+
code: '',
|
|
1456
|
+
column: 1,
|
|
1457
|
+
line: 1,
|
|
1458
|
+
offset: 0,
|
|
1459
|
+
map: undefined,
|
|
1460
|
+
breakLineCode,
|
|
1461
|
+
needIndent: _needIndent,
|
|
1462
|
+
indentLevel: 0
|
|
1463
|
+
};
|
|
1464
|
+
if (location && ast.loc) {
|
|
1465
|
+
_context.source = ast.loc.source;
|
|
1466
|
+
}
|
|
1467
|
+
const context = () => _context;
|
|
1468
|
+
function push(code, node) {
|
|
1469
|
+
_context.code += code;
|
|
1470
|
+
}
|
|
1471
|
+
function _newline(n, withBreakLine = true) {
|
|
1472
|
+
const _breakLineCode = withBreakLine ? breakLineCode : '';
|
|
1473
|
+
push(_needIndent ? _breakLineCode + ` `.repeat(n) : _breakLineCode);
|
|
1474
|
+
}
|
|
1475
|
+
function indent(withNewLine = true) {
|
|
1476
|
+
const level = ++_context.indentLevel;
|
|
1477
|
+
withNewLine && _newline(level);
|
|
1478
|
+
}
|
|
1479
|
+
function deindent(withNewLine = true) {
|
|
1480
|
+
const level = --_context.indentLevel;
|
|
1481
|
+
withNewLine && _newline(level);
|
|
1482
|
+
}
|
|
1483
|
+
function newline() {
|
|
1484
|
+
_newline(_context.indentLevel);
|
|
1485
|
+
}
|
|
1486
|
+
const helper = (key) => `_${key}`;
|
|
1487
|
+
const needIndent = () => _context.needIndent;
|
|
1488
|
+
return {
|
|
1489
|
+
context,
|
|
1490
|
+
push,
|
|
1491
|
+
indent,
|
|
1492
|
+
deindent,
|
|
1493
|
+
newline,
|
|
1494
|
+
helper,
|
|
1495
|
+
needIndent
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
function generateLinkedNode(generator, node) {
|
|
1499
|
+
const { helper } = generator;
|
|
1500
|
+
generator.push(`${helper("linked" /* HelperNameMap.LINKED */)}(`);
|
|
1501
|
+
generateNode(generator, node.key);
|
|
1502
|
+
if (node.modifier) {
|
|
1503
|
+
generator.push(`, `);
|
|
1504
|
+
generateNode(generator, node.modifier);
|
|
1505
|
+
generator.push(`, _type`);
|
|
1506
|
+
}
|
|
1507
|
+
else {
|
|
1508
|
+
generator.push(`, undefined, _type`);
|
|
1509
|
+
}
|
|
1510
|
+
generator.push(`)`);
|
|
1511
|
+
}
|
|
1512
|
+
function generateMessageNode(generator, node) {
|
|
1513
|
+
const { helper, needIndent } = generator;
|
|
1514
|
+
generator.push(`${helper("normalize" /* HelperNameMap.NORMALIZE */)}([`);
|
|
1515
|
+
generator.indent(needIndent());
|
|
1516
|
+
const length = node.items.length;
|
|
1517
|
+
for (let i = 0; i < length; i++) {
|
|
1518
|
+
generateNode(generator, node.items[i]);
|
|
1519
|
+
if (i === length - 1) {
|
|
1520
|
+
break;
|
|
1521
|
+
}
|
|
1522
|
+
generator.push(', ');
|
|
1523
|
+
}
|
|
1524
|
+
generator.deindent(needIndent());
|
|
1525
|
+
generator.push('])');
|
|
1526
|
+
}
|
|
1527
|
+
function generatePluralNode(generator, node) {
|
|
1528
|
+
const { helper, needIndent } = generator;
|
|
1529
|
+
if (node.cases.length > 1) {
|
|
1530
|
+
generator.push(`${helper("plural" /* HelperNameMap.PLURAL */)}([`);
|
|
1531
|
+
generator.indent(needIndent());
|
|
1532
|
+
const length = node.cases.length;
|
|
1533
|
+
for (let i = 0; i < length; i++) {
|
|
1534
|
+
generateNode(generator, node.cases[i]);
|
|
1535
|
+
if (i === length - 1) {
|
|
1536
|
+
break;
|
|
1537
|
+
}
|
|
1538
|
+
generator.push(', ');
|
|
1539
|
+
}
|
|
1540
|
+
generator.deindent(needIndent());
|
|
1541
|
+
generator.push(`])`);
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
function generateResource(generator, node) {
|
|
1545
|
+
if (node.body) {
|
|
1546
|
+
generateNode(generator, node.body);
|
|
1547
|
+
}
|
|
1548
|
+
else {
|
|
1549
|
+
generator.push('null');
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
function generateNode(generator, node) {
|
|
1553
|
+
const { helper } = generator;
|
|
1554
|
+
switch (node.type) {
|
|
1555
|
+
case 0 /* NodeTypes.Resource */:
|
|
1556
|
+
generateResource(generator, node);
|
|
1557
|
+
break;
|
|
1558
|
+
case 1 /* NodeTypes.Plural */:
|
|
1559
|
+
generatePluralNode(generator, node);
|
|
1560
|
+
break;
|
|
1561
|
+
case 2 /* NodeTypes.Message */:
|
|
1562
|
+
generateMessageNode(generator, node);
|
|
1563
|
+
break;
|
|
1564
|
+
case 6 /* NodeTypes.Linked */:
|
|
1565
|
+
generateLinkedNode(generator, node);
|
|
1566
|
+
break;
|
|
1567
|
+
case 8 /* NodeTypes.LinkedModifier */:
|
|
1568
|
+
generator.push(JSON.stringify(node.value), node);
|
|
1569
|
+
break;
|
|
1570
|
+
case 7 /* NodeTypes.LinkedKey */:
|
|
1571
|
+
generator.push(JSON.stringify(node.value), node);
|
|
1572
|
+
break;
|
|
1573
|
+
case 5 /* NodeTypes.List */:
|
|
1574
|
+
generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("list" /* HelperNameMap.LIST */)}(${node.index}))`, node);
|
|
1575
|
+
break;
|
|
1576
|
+
case 4 /* NodeTypes.Named */:
|
|
1577
|
+
generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("named" /* HelperNameMap.NAMED */)}(${JSON.stringify(node.key)}))`, node);
|
|
1578
|
+
break;
|
|
1579
|
+
case 9 /* NodeTypes.Literal */:
|
|
1580
|
+
generator.push(JSON.stringify(node.value), node);
|
|
1581
|
+
break;
|
|
1582
|
+
case 3 /* NodeTypes.Text */:
|
|
1583
|
+
generator.push(JSON.stringify(node.value), node);
|
|
1584
|
+
break;
|
|
1585
|
+
default:
|
|
1586
|
+
{
|
|
1587
|
+
throw createCompileError(CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE, null, {
|
|
1588
|
+
domain: ERROR_DOMAIN,
|
|
1589
|
+
args: [node.type]
|
|
1590
|
+
});
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
// generate code from AST
|
|
1595
|
+
const generate = (ast, options = {} // eslint-disable-line
|
|
1596
|
+
) => {
|
|
1597
|
+
const mode = isString(options.mode) ? options.mode : 'normal';
|
|
1598
|
+
const filename = isString(options.filename)
|
|
1599
|
+
? options.filename
|
|
1600
|
+
: 'message.intl';
|
|
1601
|
+
const sourceMap = !!options.sourceMap;
|
|
1602
|
+
// prettier-ignore
|
|
1603
|
+
const breakLineCode = options.breakLineCode != null
|
|
1604
|
+
? options.breakLineCode
|
|
1605
|
+
: mode === 'arrow'
|
|
1606
|
+
? ';'
|
|
1607
|
+
: '\n';
|
|
1608
|
+
const needIndent = options.needIndent ? options.needIndent : mode !== 'arrow';
|
|
1609
|
+
const helpers = ast.helpers || [];
|
|
1610
|
+
const generator = createCodeGenerator(ast, {
|
|
1611
|
+
mode,
|
|
1612
|
+
filename,
|
|
1613
|
+
sourceMap,
|
|
1614
|
+
breakLineCode,
|
|
1615
|
+
needIndent
|
|
1616
|
+
});
|
|
1617
|
+
generator.push(mode === 'normal' ? `function __msg__ (ctx) {` : `(ctx) => {`);
|
|
1618
|
+
generator.indent(needIndent);
|
|
1619
|
+
if (helpers.length > 0) {
|
|
1620
|
+
generator.push(`const { ${join(helpers.map(s => `${s}: _${s}`), ', ')} } = ctx`);
|
|
1621
|
+
generator.newline();
|
|
1622
|
+
}
|
|
1623
|
+
generator.push(`return `);
|
|
1624
|
+
generateNode(generator, ast);
|
|
1625
|
+
generator.deindent(needIndent);
|
|
1626
|
+
generator.push(`}`);
|
|
1627
|
+
delete ast.helpers;
|
|
1628
|
+
const { code, map } = generator.context();
|
|
1629
|
+
return {
|
|
1630
|
+
ast,
|
|
1631
|
+
code,
|
|
1632
|
+
map: map ? map.toJSON() : undefined // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1633
|
+
};
|
|
1634
|
+
};
|
|
1635
|
+
|
|
1636
|
+
function baseCompile$1(source, options = {}) {
|
|
1637
|
+
const assignedOptions = assign({}, options);
|
|
1638
|
+
const jit = !!assignedOptions.jit;
|
|
1639
|
+
const enalbeMinify = !!assignedOptions.minify;
|
|
1640
|
+
const enambeOptimize = assignedOptions.optimize == null ? true : assignedOptions.optimize;
|
|
1641
|
+
// parse source codes
|
|
1642
|
+
const parser = createParser(assignedOptions);
|
|
1643
|
+
const ast = parser.parse(source);
|
|
1644
|
+
if (!jit) {
|
|
1645
|
+
// transform ASTs
|
|
1646
|
+
transform(ast, assignedOptions);
|
|
1647
|
+
// generate javascript codes
|
|
1648
|
+
return generate(ast, assignedOptions);
|
|
1649
|
+
}
|
|
1650
|
+
else {
|
|
1651
|
+
// optimize ASTs
|
|
1652
|
+
enambeOptimize && optimize(ast);
|
|
1653
|
+
// minimize ASTs
|
|
1654
|
+
enalbeMinify && minify(ast);
|
|
1655
|
+
// In JIT mode, no ast transform, no code generation.
|
|
1656
|
+
return { ast, code: '' };
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
206
1659
|
|
|
207
1660
|
const pathStateMachine = [];
|
|
208
1661
|
pathStateMachine[0 /* States.BEFORE_PATH */] = {
|
|
@@ -599,11 +2052,6 @@ var IntlifyCore = (function (exports) {
|
|
|
599
2052
|
return ctx;
|
|
600
2053
|
}
|
|
601
2054
|
|
|
602
|
-
const IntlifyDevToolsHooks = {
|
|
603
|
-
I18nInit: 'i18n:init',
|
|
604
|
-
FunctionTranslate: 'function:translate'
|
|
605
|
-
};
|
|
606
|
-
|
|
607
2055
|
let devtools = null;
|
|
608
2056
|
function setDevToolsHook(hook) {
|
|
609
2057
|
devtools = hook;
|
|
@@ -614,14 +2062,14 @@ var IntlifyCore = (function (exports) {
|
|
|
614
2062
|
function initI18nDevTools(i18n, version, meta) {
|
|
615
2063
|
// TODO: queue if devtools is undefined
|
|
616
2064
|
devtools &&
|
|
617
|
-
devtools.emit(IntlifyDevToolsHooks.I18nInit
|
|
2065
|
+
devtools.emit("i18n:init" /* IntlifyDevToolsHooks.I18nInit */, {
|
|
618
2066
|
timestamp: Date.now(),
|
|
619
2067
|
i18n,
|
|
620
2068
|
version,
|
|
621
2069
|
meta
|
|
622
2070
|
});
|
|
623
2071
|
}
|
|
624
|
-
const translateDevTools = /* #__PURE__*/ createDevToolsHook(IntlifyDevToolsHooks.FunctionTranslate);
|
|
2072
|
+
const translateDevTools = /* #__PURE__*/ createDevToolsHook("function:translate" /* IntlifyDevToolsHooks.FunctionTranslate */);
|
|
625
2073
|
function createDevToolsHook(hook) {
|
|
626
2074
|
return (payloads) => devtools && devtools.emit(hook, payloads);
|
|
627
2075
|
}
|
|
@@ -771,7 +2219,7 @@ var IntlifyCore = (function (exports) {
|
|
|
771
2219
|
* Intlify core-base version
|
|
772
2220
|
* @internal
|
|
773
2221
|
*/
|
|
774
|
-
const VERSION = '9.
|
|
2222
|
+
const VERSION = '9.4.1';
|
|
775
2223
|
const NOT_REOSLVED = -1;
|
|
776
2224
|
const DEFAULT_LOCALE = 'en-US';
|
|
777
2225
|
const MISSING_RESOLVE_VALUE = '';
|
|
@@ -1074,6 +2522,13 @@ var IntlifyCore = (function (exports) {
|
|
|
1074
2522
|
[CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: 'Not support non-string message'
|
|
1075
2523
|
};
|
|
1076
2524
|
|
|
2525
|
+
const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
|
|
2526
|
+
function checkHtmlMessage(source, warnHtmlMessage) {
|
|
2527
|
+
if (warnHtmlMessage && detectHtmlTag(source)) {
|
|
2528
|
+
warn(format$1(WARN_MESSAGE, { source }));
|
|
2529
|
+
}
|
|
2530
|
+
}
|
|
2531
|
+
const defaultOnCacheKey = (message) => message;
|
|
1077
2532
|
let compileCache = Object.create(null);
|
|
1078
2533
|
function clearCompileCache() {
|
|
1079
2534
|
compileCache = Object.create(null);
|
|
@@ -1081,7 +2536,18 @@ var IntlifyCore = (function (exports) {
|
|
|
1081
2536
|
const isMessageAST = (val) => isObject(val) &&
|
|
1082
2537
|
(val.t === 0 || val.type === 0) &&
|
|
1083
2538
|
('b' in val || 'body' in val);
|
|
1084
|
-
function
|
|
2539
|
+
function baseCompile(message, options = {}) {
|
|
2540
|
+
// error detecting on compile
|
|
2541
|
+
let detectError = false;
|
|
2542
|
+
const onError = options.onError || defaultOnError;
|
|
2543
|
+
options.onError = (err) => {
|
|
2544
|
+
detectError = true;
|
|
2545
|
+
onError(err);
|
|
2546
|
+
};
|
|
2547
|
+
// compile with mesasge-compiler
|
|
2548
|
+
return { ...baseCompile$1(message, options), detectError };
|
|
2549
|
+
}
|
|
2550
|
+
const compileToFunction = /* #__PURE__*/ (message, context) => {
|
|
1085
2551
|
if (!isString(message)) {
|
|
1086
2552
|
throw createCoreError(CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE);
|
|
1087
2553
|
}
|
|
@@ -1089,9 +2555,35 @@ var IntlifyCore = (function (exports) {
|
|
|
1089
2555
|
warn(`Runtime compilation is not supported in ${'core.runtime.global.js' }.`);
|
|
1090
2556
|
return (() => message);
|
|
1091
2557
|
}
|
|
1092
|
-
}
|
|
2558
|
+
};
|
|
1093
2559
|
function compile(message, context) {
|
|
1094
|
-
{
|
|
2560
|
+
if (isString(message)) {
|
|
2561
|
+
// check HTML message
|
|
2562
|
+
const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
|
|
2563
|
+
? context.warnHtmlMessage
|
|
2564
|
+
: true;
|
|
2565
|
+
checkHtmlMessage(message, warnHtmlMessage);
|
|
2566
|
+
// check caches
|
|
2567
|
+
const onCacheKey = context.onCacheKey || defaultOnCacheKey;
|
|
2568
|
+
const cacheKey = onCacheKey(message);
|
|
2569
|
+
const cached = compileCache[cacheKey];
|
|
2570
|
+
if (cached) {
|
|
2571
|
+
return cached;
|
|
2572
|
+
}
|
|
2573
|
+
// compile with JIT mode
|
|
2574
|
+
const { ast, detectError } = baseCompile(message, {
|
|
2575
|
+
...context,
|
|
2576
|
+
location: true,
|
|
2577
|
+
jit: true
|
|
2578
|
+
});
|
|
2579
|
+
// compose message function from AST
|
|
2580
|
+
const msg = format(ast);
|
|
2581
|
+
// if occurred compile error, don't cache
|
|
2582
|
+
return !detectError
|
|
2583
|
+
? (compileCache[cacheKey] = msg)
|
|
2584
|
+
: msg;
|
|
2585
|
+
}
|
|
2586
|
+
else {
|
|
1095
2587
|
if (!isMessageAST(message)) {
|
|
1096
2588
|
warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
|
|
1097
2589
|
return (() => message);
|