@intlify/core 9.3.0 → 9.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -0
- package/dist/core.cjs +2 -2
- package/dist/core.esm-browser.js +34 -13
- package/dist/core.esm-browser.prod.js +2 -2
- package/dist/core.global.js +34 -13
- 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 +1505 -12
- package/dist/core.runtime.esm-browser.prod.js +2 -2
- package/dist/core.runtime.global.js +1505 -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.0
|
|
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,1449 @@ 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) {
|
|
745
|
+
return buf;
|
|
746
|
+
}
|
|
747
|
+
else if (ch === CHAR_SP) {
|
|
748
|
+
return buf;
|
|
749
|
+
}
|
|
750
|
+
else if (ch === CHAR_LF || ch === DOT) {
|
|
751
|
+
buf += ch;
|
|
752
|
+
scnr.next();
|
|
753
|
+
return fn(detect, buf);
|
|
754
|
+
}
|
|
755
|
+
else if (!isIdentifierStart(ch)) {
|
|
756
|
+
return buf;
|
|
757
|
+
}
|
|
758
|
+
else {
|
|
759
|
+
buf += ch;
|
|
760
|
+
scnr.next();
|
|
761
|
+
return fn(true, buf);
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
return fn(false, '');
|
|
765
|
+
}
|
|
766
|
+
function readPlural(scnr) {
|
|
767
|
+
skipSpaces(scnr);
|
|
768
|
+
const plural = eat(scnr, "|" /* TokenChars.Pipe */);
|
|
769
|
+
skipSpaces(scnr);
|
|
770
|
+
return plural;
|
|
771
|
+
}
|
|
772
|
+
// TODO: We need refactoring of token parsing ...
|
|
773
|
+
function readTokenInPlaceholder(scnr, context) {
|
|
774
|
+
let token = null;
|
|
775
|
+
const ch = scnr.currentChar();
|
|
776
|
+
switch (ch) {
|
|
777
|
+
case "{" /* TokenChars.BraceLeft */:
|
|
778
|
+
if (context.braceNest >= 1) {
|
|
779
|
+
emitError(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER, currentPosition(), 0);
|
|
780
|
+
}
|
|
781
|
+
scnr.next();
|
|
782
|
+
token = getToken(context, 2 /* TokenTypes.BraceLeft */, "{" /* TokenChars.BraceLeft */);
|
|
783
|
+
skipSpaces(scnr);
|
|
784
|
+
context.braceNest++;
|
|
785
|
+
return token;
|
|
786
|
+
case "}" /* TokenChars.BraceRight */:
|
|
787
|
+
if (context.braceNest > 0 &&
|
|
788
|
+
context.currentType === 2 /* TokenTypes.BraceLeft */) {
|
|
789
|
+
emitError(CompileErrorCodes.EMPTY_PLACEHOLDER, currentPosition(), 0);
|
|
790
|
+
}
|
|
791
|
+
scnr.next();
|
|
792
|
+
token = getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
|
|
793
|
+
context.braceNest--;
|
|
794
|
+
context.braceNest > 0 && skipSpaces(scnr);
|
|
795
|
+
if (context.inLinked && context.braceNest === 0) {
|
|
796
|
+
context.inLinked = false;
|
|
797
|
+
}
|
|
798
|
+
return token;
|
|
799
|
+
case "@" /* TokenChars.LinkedAlias */:
|
|
800
|
+
if (context.braceNest > 0) {
|
|
801
|
+
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
802
|
+
}
|
|
803
|
+
token = readTokenInLinked(scnr, context) || getEndToken(context);
|
|
804
|
+
context.braceNest = 0;
|
|
805
|
+
return token;
|
|
806
|
+
default:
|
|
807
|
+
let validNamedIdentifier = true;
|
|
808
|
+
let validListIdentifier = true;
|
|
809
|
+
let validLiteral = true;
|
|
810
|
+
if (isPluralStart(scnr)) {
|
|
811
|
+
if (context.braceNest > 0) {
|
|
812
|
+
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
813
|
+
}
|
|
814
|
+
token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
|
|
815
|
+
// reset
|
|
816
|
+
context.braceNest = 0;
|
|
817
|
+
context.inLinked = false;
|
|
818
|
+
return token;
|
|
819
|
+
}
|
|
820
|
+
if (context.braceNest > 0 &&
|
|
821
|
+
(context.currentType === 5 /* TokenTypes.Named */ ||
|
|
822
|
+
context.currentType === 6 /* TokenTypes.List */ ||
|
|
823
|
+
context.currentType === 7 /* TokenTypes.Literal */)) {
|
|
824
|
+
emitError(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE, currentPosition(), 0);
|
|
825
|
+
context.braceNest = 0;
|
|
826
|
+
return readToken(scnr, context);
|
|
827
|
+
}
|
|
828
|
+
if ((validNamedIdentifier = isNamedIdentifierStart(scnr, context))) {
|
|
829
|
+
token = getToken(context, 5 /* TokenTypes.Named */, readNamedIdentifier(scnr));
|
|
830
|
+
skipSpaces(scnr);
|
|
831
|
+
return token;
|
|
832
|
+
}
|
|
833
|
+
if ((validListIdentifier = isListIdentifierStart(scnr, context))) {
|
|
834
|
+
token = getToken(context, 6 /* TokenTypes.List */, readListIdentifier(scnr));
|
|
835
|
+
skipSpaces(scnr);
|
|
836
|
+
return token;
|
|
837
|
+
}
|
|
838
|
+
if ((validLiteral = isLiteralStart(scnr, context))) {
|
|
839
|
+
token = getToken(context, 7 /* TokenTypes.Literal */, readLiteral(scnr));
|
|
840
|
+
skipSpaces(scnr);
|
|
841
|
+
return token;
|
|
842
|
+
}
|
|
843
|
+
if (!validNamedIdentifier && !validListIdentifier && !validLiteral) {
|
|
844
|
+
// TODO: we should be re-designed invalid cases, when we will extend message syntax near the future ...
|
|
845
|
+
token = getToken(context, 13 /* TokenTypes.InvalidPlace */, readInvalidIdentifier(scnr));
|
|
846
|
+
emitError(CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER, currentPosition(), 0, token.value);
|
|
847
|
+
skipSpaces(scnr);
|
|
848
|
+
return token;
|
|
849
|
+
}
|
|
850
|
+
break;
|
|
851
|
+
}
|
|
852
|
+
return token;
|
|
853
|
+
}
|
|
854
|
+
// TODO: We need refactoring of token parsing ...
|
|
855
|
+
function readTokenInLinked(scnr, context) {
|
|
856
|
+
const { currentType } = context;
|
|
857
|
+
let token = null;
|
|
858
|
+
const ch = scnr.currentChar();
|
|
859
|
+
if ((currentType === 8 /* TokenTypes.LinkedAlias */ ||
|
|
860
|
+
currentType === 9 /* TokenTypes.LinkedDot */ ||
|
|
861
|
+
currentType === 12 /* TokenTypes.LinkedModifier */ ||
|
|
862
|
+
currentType === 10 /* TokenTypes.LinkedDelimiter */) &&
|
|
863
|
+
(ch === CHAR_LF || ch === CHAR_SP)) {
|
|
864
|
+
emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
|
|
865
|
+
}
|
|
866
|
+
switch (ch) {
|
|
867
|
+
case "@" /* TokenChars.LinkedAlias */:
|
|
868
|
+
scnr.next();
|
|
869
|
+
token = getToken(context, 8 /* TokenTypes.LinkedAlias */, "@" /* TokenChars.LinkedAlias */);
|
|
870
|
+
context.inLinked = true;
|
|
871
|
+
return token;
|
|
872
|
+
case "." /* TokenChars.LinkedDot */:
|
|
873
|
+
skipSpaces(scnr);
|
|
874
|
+
scnr.next();
|
|
875
|
+
return getToken(context, 9 /* TokenTypes.LinkedDot */, "." /* TokenChars.LinkedDot */);
|
|
876
|
+
case ":" /* TokenChars.LinkedDelimiter */:
|
|
877
|
+
skipSpaces(scnr);
|
|
878
|
+
scnr.next();
|
|
879
|
+
return getToken(context, 10 /* TokenTypes.LinkedDelimiter */, ":" /* TokenChars.LinkedDelimiter */);
|
|
880
|
+
default:
|
|
881
|
+
if (isPluralStart(scnr)) {
|
|
882
|
+
token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
|
|
883
|
+
// reset
|
|
884
|
+
context.braceNest = 0;
|
|
885
|
+
context.inLinked = false;
|
|
886
|
+
return token;
|
|
887
|
+
}
|
|
888
|
+
if (isLinkedDotStart(scnr, context) ||
|
|
889
|
+
isLinkedDelimiterStart(scnr, context)) {
|
|
890
|
+
skipSpaces(scnr);
|
|
891
|
+
return readTokenInLinked(scnr, context);
|
|
892
|
+
}
|
|
893
|
+
if (isLinkedModifierStart(scnr, context)) {
|
|
894
|
+
skipSpaces(scnr);
|
|
895
|
+
return getToken(context, 12 /* TokenTypes.LinkedModifier */, readLinkedModifier(scnr));
|
|
896
|
+
}
|
|
897
|
+
if (isLinkedReferStart(scnr, context)) {
|
|
898
|
+
skipSpaces(scnr);
|
|
899
|
+
if (ch === "{" /* TokenChars.BraceLeft */) {
|
|
900
|
+
// scan the placeholder
|
|
901
|
+
return readTokenInPlaceholder(scnr, context) || token;
|
|
902
|
+
}
|
|
903
|
+
else {
|
|
904
|
+
return getToken(context, 11 /* TokenTypes.LinkedKey */, readLinkedRefer(scnr));
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
if (currentType === 8 /* TokenTypes.LinkedAlias */) {
|
|
908
|
+
emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
|
|
909
|
+
}
|
|
910
|
+
context.braceNest = 0;
|
|
911
|
+
context.inLinked = false;
|
|
912
|
+
return readToken(scnr, context);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
// TODO: We need refactoring of token parsing ...
|
|
916
|
+
function readToken(scnr, context) {
|
|
917
|
+
let token = { type: 14 /* TokenTypes.EOF */ };
|
|
918
|
+
if (context.braceNest > 0) {
|
|
919
|
+
return readTokenInPlaceholder(scnr, context) || getEndToken(context);
|
|
920
|
+
}
|
|
921
|
+
if (context.inLinked) {
|
|
922
|
+
return readTokenInLinked(scnr, context) || getEndToken(context);
|
|
923
|
+
}
|
|
924
|
+
const ch = scnr.currentChar();
|
|
925
|
+
switch (ch) {
|
|
926
|
+
case "{" /* TokenChars.BraceLeft */:
|
|
927
|
+
return readTokenInPlaceholder(scnr, context) || getEndToken(context);
|
|
928
|
+
case "}" /* TokenChars.BraceRight */:
|
|
929
|
+
emitError(CompileErrorCodes.UNBALANCED_CLOSING_BRACE, currentPosition(), 0);
|
|
930
|
+
scnr.next();
|
|
931
|
+
return getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
|
|
932
|
+
case "@" /* TokenChars.LinkedAlias */:
|
|
933
|
+
return readTokenInLinked(scnr, context) || getEndToken(context);
|
|
934
|
+
default:
|
|
935
|
+
if (isPluralStart(scnr)) {
|
|
936
|
+
token = getToken(context, 1 /* TokenTypes.Pipe */, readPlural(scnr));
|
|
937
|
+
// reset
|
|
938
|
+
context.braceNest = 0;
|
|
939
|
+
context.inLinked = false;
|
|
940
|
+
return token;
|
|
941
|
+
}
|
|
942
|
+
const { isModulo, hasSpace } = detectModuloStart(scnr);
|
|
943
|
+
if (isModulo) {
|
|
944
|
+
return hasSpace
|
|
945
|
+
? getToken(context, 0 /* TokenTypes.Text */, readText(scnr))
|
|
946
|
+
: getToken(context, 4 /* TokenTypes.Modulo */, readModulo(scnr));
|
|
947
|
+
}
|
|
948
|
+
if (isTextStart(scnr)) {
|
|
949
|
+
return getToken(context, 0 /* TokenTypes.Text */, readText(scnr));
|
|
950
|
+
}
|
|
951
|
+
break;
|
|
952
|
+
}
|
|
953
|
+
return token;
|
|
954
|
+
}
|
|
955
|
+
function nextToken() {
|
|
956
|
+
const { currentType, offset, startLoc, endLoc } = _context;
|
|
957
|
+
_context.lastType = currentType;
|
|
958
|
+
_context.lastOffset = offset;
|
|
959
|
+
_context.lastStartLoc = startLoc;
|
|
960
|
+
_context.lastEndLoc = endLoc;
|
|
961
|
+
_context.offset = currentOffset();
|
|
962
|
+
_context.startLoc = currentPosition();
|
|
963
|
+
if (_scnr.currentChar() === EOF) {
|
|
964
|
+
return getToken(_context, 14 /* TokenTypes.EOF */);
|
|
965
|
+
}
|
|
966
|
+
return readToken(_scnr, _context);
|
|
967
|
+
}
|
|
968
|
+
return {
|
|
969
|
+
nextToken,
|
|
970
|
+
currentOffset,
|
|
971
|
+
currentPosition,
|
|
972
|
+
context
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
const ERROR_DOMAIN$2 = 'parser';
|
|
977
|
+
// Backslash backslash, backslash quote, uHHHH, UHHHHHH.
|
|
978
|
+
const KNOWN_ESCAPES = /(?:\\\\|\\'|\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{6}))/g;
|
|
979
|
+
function fromEscapeSequence(match, codePoint4, codePoint6) {
|
|
980
|
+
switch (match) {
|
|
981
|
+
case `\\\\`:
|
|
982
|
+
return `\\`;
|
|
983
|
+
case `\\\'`:
|
|
984
|
+
return `\'`;
|
|
985
|
+
default: {
|
|
986
|
+
const codePoint = parseInt(codePoint4 || codePoint6, 16);
|
|
987
|
+
if (codePoint <= 0xd7ff || codePoint >= 0xe000) {
|
|
988
|
+
return String.fromCodePoint(codePoint);
|
|
989
|
+
}
|
|
990
|
+
// invalid ...
|
|
991
|
+
// Replace them with U+FFFD REPLACEMENT CHARACTER.
|
|
992
|
+
return '�';
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
function createParser(options = {}) {
|
|
997
|
+
const location = options.location !== false;
|
|
998
|
+
const { onError } = options;
|
|
999
|
+
function emitError(tokenzer, code, start, offset, ...args) {
|
|
1000
|
+
const end = tokenzer.currentPosition();
|
|
1001
|
+
end.offset += offset;
|
|
1002
|
+
end.column += offset;
|
|
1003
|
+
if (onError) {
|
|
1004
|
+
const loc = location ? createLocation(start, end) : null;
|
|
1005
|
+
const err = createCompileError(code, loc, {
|
|
1006
|
+
domain: ERROR_DOMAIN$2,
|
|
1007
|
+
args
|
|
1008
|
+
});
|
|
1009
|
+
onError(err);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
function startNode(type, offset, loc) {
|
|
1013
|
+
const node = { type };
|
|
1014
|
+
if (location) {
|
|
1015
|
+
node.start = offset;
|
|
1016
|
+
node.end = offset;
|
|
1017
|
+
node.loc = { start: loc, end: loc };
|
|
1018
|
+
}
|
|
1019
|
+
return node;
|
|
1020
|
+
}
|
|
1021
|
+
function endNode(node, offset, pos, type) {
|
|
1022
|
+
if (type) {
|
|
1023
|
+
node.type = type;
|
|
1024
|
+
}
|
|
1025
|
+
if (location) {
|
|
1026
|
+
node.end = offset;
|
|
1027
|
+
if (node.loc) {
|
|
1028
|
+
node.loc.end = pos;
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
function parseText(tokenizer, value) {
|
|
1033
|
+
const context = tokenizer.context();
|
|
1034
|
+
const node = startNode(3 /* NodeTypes.Text */, context.offset, context.startLoc);
|
|
1035
|
+
node.value = value;
|
|
1036
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1037
|
+
return node;
|
|
1038
|
+
}
|
|
1039
|
+
function parseList(tokenizer, index) {
|
|
1040
|
+
const context = tokenizer.context();
|
|
1041
|
+
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
1042
|
+
const node = startNode(5 /* NodeTypes.List */, offset, loc);
|
|
1043
|
+
node.index = parseInt(index, 10);
|
|
1044
|
+
tokenizer.nextToken(); // skip brach right
|
|
1045
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1046
|
+
return node;
|
|
1047
|
+
}
|
|
1048
|
+
function parseNamed(tokenizer, key) {
|
|
1049
|
+
const context = tokenizer.context();
|
|
1050
|
+
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
1051
|
+
const node = startNode(4 /* NodeTypes.Named */, offset, loc);
|
|
1052
|
+
node.key = key;
|
|
1053
|
+
tokenizer.nextToken(); // skip brach right
|
|
1054
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1055
|
+
return node;
|
|
1056
|
+
}
|
|
1057
|
+
function parseLiteral(tokenizer, value) {
|
|
1058
|
+
const context = tokenizer.context();
|
|
1059
|
+
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
1060
|
+
const node = startNode(9 /* NodeTypes.Literal */, offset, loc);
|
|
1061
|
+
node.value = value.replace(KNOWN_ESCAPES, fromEscapeSequence);
|
|
1062
|
+
tokenizer.nextToken(); // skip brach right
|
|
1063
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1064
|
+
return node;
|
|
1065
|
+
}
|
|
1066
|
+
function parseLinkedModifier(tokenizer) {
|
|
1067
|
+
const token = tokenizer.nextToken();
|
|
1068
|
+
const context = tokenizer.context();
|
|
1069
|
+
const { lastOffset: offset, lastStartLoc: loc } = context; // get linked dot loc
|
|
1070
|
+
const node = startNode(8 /* NodeTypes.LinkedModifier */, offset, loc);
|
|
1071
|
+
if (token.type !== 12 /* TokenTypes.LinkedModifier */) {
|
|
1072
|
+
// empty modifier
|
|
1073
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER, context.lastStartLoc, 0);
|
|
1074
|
+
node.value = '';
|
|
1075
|
+
endNode(node, offset, loc);
|
|
1076
|
+
return {
|
|
1077
|
+
nextConsumeToken: token,
|
|
1078
|
+
node
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
// check token
|
|
1082
|
+
if (token.value == null) {
|
|
1083
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1084
|
+
}
|
|
1085
|
+
node.value = token.value || '';
|
|
1086
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1087
|
+
return {
|
|
1088
|
+
node
|
|
1089
|
+
};
|
|
1090
|
+
}
|
|
1091
|
+
function parseLinkedKey(tokenizer, value) {
|
|
1092
|
+
const context = tokenizer.context();
|
|
1093
|
+
const node = startNode(7 /* NodeTypes.LinkedKey */, context.offset, context.startLoc);
|
|
1094
|
+
node.value = value;
|
|
1095
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1096
|
+
return node;
|
|
1097
|
+
}
|
|
1098
|
+
function parseLinked(tokenizer) {
|
|
1099
|
+
const context = tokenizer.context();
|
|
1100
|
+
const linkedNode = startNode(6 /* NodeTypes.Linked */, context.offset, context.startLoc);
|
|
1101
|
+
let token = tokenizer.nextToken();
|
|
1102
|
+
if (token.type === 9 /* TokenTypes.LinkedDot */) {
|
|
1103
|
+
const parsed = parseLinkedModifier(tokenizer);
|
|
1104
|
+
linkedNode.modifier = parsed.node;
|
|
1105
|
+
token = parsed.nextConsumeToken || tokenizer.nextToken();
|
|
1106
|
+
}
|
|
1107
|
+
// asset check token
|
|
1108
|
+
if (token.type !== 10 /* TokenTypes.LinkedDelimiter */) {
|
|
1109
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1110
|
+
}
|
|
1111
|
+
token = tokenizer.nextToken();
|
|
1112
|
+
// skip brace left
|
|
1113
|
+
if (token.type === 2 /* TokenTypes.BraceLeft */) {
|
|
1114
|
+
token = tokenizer.nextToken();
|
|
1115
|
+
}
|
|
1116
|
+
switch (token.type) {
|
|
1117
|
+
case 11 /* TokenTypes.LinkedKey */:
|
|
1118
|
+
if (token.value == null) {
|
|
1119
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1120
|
+
}
|
|
1121
|
+
linkedNode.key = parseLinkedKey(tokenizer, token.value || '');
|
|
1122
|
+
break;
|
|
1123
|
+
case 5 /* TokenTypes.Named */:
|
|
1124
|
+
if (token.value == null) {
|
|
1125
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1126
|
+
}
|
|
1127
|
+
linkedNode.key = parseNamed(tokenizer, token.value || '');
|
|
1128
|
+
break;
|
|
1129
|
+
case 6 /* TokenTypes.List */:
|
|
1130
|
+
if (token.value == null) {
|
|
1131
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1132
|
+
}
|
|
1133
|
+
linkedNode.key = parseList(tokenizer, token.value || '');
|
|
1134
|
+
break;
|
|
1135
|
+
case 7 /* TokenTypes.Literal */:
|
|
1136
|
+
if (token.value == null) {
|
|
1137
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1138
|
+
}
|
|
1139
|
+
linkedNode.key = parseLiteral(tokenizer, token.value || '');
|
|
1140
|
+
break;
|
|
1141
|
+
default:
|
|
1142
|
+
// empty key
|
|
1143
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY, context.lastStartLoc, 0);
|
|
1144
|
+
const nextContext = tokenizer.context();
|
|
1145
|
+
const emptyLinkedKeyNode = startNode(7 /* NodeTypes.LinkedKey */, nextContext.offset, nextContext.startLoc);
|
|
1146
|
+
emptyLinkedKeyNode.value = '';
|
|
1147
|
+
endNode(emptyLinkedKeyNode, nextContext.offset, nextContext.startLoc);
|
|
1148
|
+
linkedNode.key = emptyLinkedKeyNode;
|
|
1149
|
+
endNode(linkedNode, nextContext.offset, nextContext.startLoc);
|
|
1150
|
+
return {
|
|
1151
|
+
nextConsumeToken: token,
|
|
1152
|
+
node: linkedNode
|
|
1153
|
+
};
|
|
1154
|
+
}
|
|
1155
|
+
endNode(linkedNode, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1156
|
+
return {
|
|
1157
|
+
node: linkedNode
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
function parseMessage(tokenizer) {
|
|
1161
|
+
const context = tokenizer.context();
|
|
1162
|
+
const startOffset = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1163
|
+
? tokenizer.currentOffset()
|
|
1164
|
+
: context.offset;
|
|
1165
|
+
const startLoc = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1166
|
+
? context.endLoc
|
|
1167
|
+
: context.startLoc;
|
|
1168
|
+
const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
|
|
1169
|
+
node.items = [];
|
|
1170
|
+
let nextToken = null;
|
|
1171
|
+
do {
|
|
1172
|
+
const token = nextToken || tokenizer.nextToken();
|
|
1173
|
+
nextToken = null;
|
|
1174
|
+
switch (token.type) {
|
|
1175
|
+
case 0 /* TokenTypes.Text */:
|
|
1176
|
+
if (token.value == null) {
|
|
1177
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1178
|
+
}
|
|
1179
|
+
node.items.push(parseText(tokenizer, token.value || ''));
|
|
1180
|
+
break;
|
|
1181
|
+
case 6 /* TokenTypes.List */:
|
|
1182
|
+
if (token.value == null) {
|
|
1183
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1184
|
+
}
|
|
1185
|
+
node.items.push(parseList(tokenizer, token.value || ''));
|
|
1186
|
+
break;
|
|
1187
|
+
case 5 /* TokenTypes.Named */:
|
|
1188
|
+
if (token.value == null) {
|
|
1189
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1190
|
+
}
|
|
1191
|
+
node.items.push(parseNamed(tokenizer, token.value || ''));
|
|
1192
|
+
break;
|
|
1193
|
+
case 7 /* TokenTypes.Literal */:
|
|
1194
|
+
if (token.value == null) {
|
|
1195
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1196
|
+
}
|
|
1197
|
+
node.items.push(parseLiteral(tokenizer, token.value || ''));
|
|
1198
|
+
break;
|
|
1199
|
+
case 8 /* TokenTypes.LinkedAlias */:
|
|
1200
|
+
const parsed = parseLinked(tokenizer);
|
|
1201
|
+
node.items.push(parsed.node);
|
|
1202
|
+
nextToken = parsed.nextConsumeToken || null;
|
|
1203
|
+
break;
|
|
1204
|
+
}
|
|
1205
|
+
} while (context.currentType !== 14 /* TokenTypes.EOF */ &&
|
|
1206
|
+
context.currentType !== 1 /* TokenTypes.Pipe */);
|
|
1207
|
+
// adjust message node loc
|
|
1208
|
+
const endOffset = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1209
|
+
? context.lastOffset
|
|
1210
|
+
: tokenizer.currentOffset();
|
|
1211
|
+
const endLoc = context.currentType === 1 /* TokenTypes.Pipe */
|
|
1212
|
+
? context.lastEndLoc
|
|
1213
|
+
: tokenizer.currentPosition();
|
|
1214
|
+
endNode(node, endOffset, endLoc);
|
|
1215
|
+
return node;
|
|
1216
|
+
}
|
|
1217
|
+
function parsePlural(tokenizer, offset, loc, msgNode) {
|
|
1218
|
+
const context = tokenizer.context();
|
|
1219
|
+
let hasEmptyMessage = msgNode.items.length === 0;
|
|
1220
|
+
const node = startNode(1 /* NodeTypes.Plural */, offset, loc);
|
|
1221
|
+
node.cases = [];
|
|
1222
|
+
node.cases.push(msgNode);
|
|
1223
|
+
do {
|
|
1224
|
+
const msg = parseMessage(tokenizer);
|
|
1225
|
+
if (!hasEmptyMessage) {
|
|
1226
|
+
hasEmptyMessage = msg.items.length === 0;
|
|
1227
|
+
}
|
|
1228
|
+
node.cases.push(msg);
|
|
1229
|
+
} while (context.currentType !== 14 /* TokenTypes.EOF */);
|
|
1230
|
+
if (hasEmptyMessage) {
|
|
1231
|
+
emitError(tokenizer, CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL, loc, 0);
|
|
1232
|
+
}
|
|
1233
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1234
|
+
return node;
|
|
1235
|
+
}
|
|
1236
|
+
function parseResource(tokenizer) {
|
|
1237
|
+
const context = tokenizer.context();
|
|
1238
|
+
const { offset, startLoc } = context;
|
|
1239
|
+
const msgNode = parseMessage(tokenizer);
|
|
1240
|
+
if (context.currentType === 14 /* TokenTypes.EOF */) {
|
|
1241
|
+
return msgNode;
|
|
1242
|
+
}
|
|
1243
|
+
else {
|
|
1244
|
+
return parsePlural(tokenizer, offset, startLoc, msgNode);
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
function parse(source) {
|
|
1248
|
+
const tokenizer = createTokenizer(source, assign({}, options));
|
|
1249
|
+
const context = tokenizer.context();
|
|
1250
|
+
const node = startNode(0 /* NodeTypes.Resource */, context.offset, context.startLoc);
|
|
1251
|
+
if (location && node.loc) {
|
|
1252
|
+
node.loc.source = source;
|
|
1253
|
+
}
|
|
1254
|
+
node.body = parseResource(tokenizer);
|
|
1255
|
+
if (options.onCacheKey) {
|
|
1256
|
+
node.cacheKey = options.onCacheKey(source);
|
|
1257
|
+
}
|
|
1258
|
+
// assert whether achieved to EOF
|
|
1259
|
+
if (context.currentType !== 14 /* TokenTypes.EOF */) {
|
|
1260
|
+
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, source[context.offset] || '');
|
|
1261
|
+
}
|
|
1262
|
+
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
1263
|
+
return node;
|
|
1264
|
+
}
|
|
1265
|
+
return { parse };
|
|
1266
|
+
}
|
|
1267
|
+
function getTokenCaption(token) {
|
|
1268
|
+
if (token.type === 14 /* TokenTypes.EOF */) {
|
|
1269
|
+
return 'EOF';
|
|
1270
|
+
}
|
|
1271
|
+
const name = (token.value || '').replace(/\r?\n/gu, '\\n');
|
|
1272
|
+
return name.length > 10 ? name.slice(0, 9) + '…' : name;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
function createTransformer(ast, options = {} // eslint-disable-line
|
|
1276
|
+
) {
|
|
1277
|
+
const _context = {
|
|
1278
|
+
ast,
|
|
1279
|
+
helpers: new Set()
|
|
1280
|
+
};
|
|
1281
|
+
const context = () => _context;
|
|
1282
|
+
const helper = (name) => {
|
|
1283
|
+
_context.helpers.add(name);
|
|
1284
|
+
return name;
|
|
1285
|
+
};
|
|
1286
|
+
return { context, helper };
|
|
1287
|
+
}
|
|
1288
|
+
function traverseNodes(nodes, transformer) {
|
|
1289
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
1290
|
+
traverseNode(nodes[i], transformer);
|
|
1291
|
+
}
|
|
1292
|
+
}
|
|
1293
|
+
function traverseNode(node, transformer) {
|
|
1294
|
+
// TODO: if we need pre-hook of transform, should be implemented to here
|
|
1295
|
+
switch (node.type) {
|
|
1296
|
+
case 1 /* NodeTypes.Plural */:
|
|
1297
|
+
traverseNodes(node.cases, transformer);
|
|
1298
|
+
transformer.helper("plural" /* HelperNameMap.PLURAL */);
|
|
1299
|
+
break;
|
|
1300
|
+
case 2 /* NodeTypes.Message */:
|
|
1301
|
+
traverseNodes(node.items, transformer);
|
|
1302
|
+
break;
|
|
1303
|
+
case 6 /* NodeTypes.Linked */:
|
|
1304
|
+
const linked = node;
|
|
1305
|
+
traverseNode(linked.key, transformer);
|
|
1306
|
+
transformer.helper("linked" /* HelperNameMap.LINKED */);
|
|
1307
|
+
transformer.helper("type" /* HelperNameMap.TYPE */);
|
|
1308
|
+
break;
|
|
1309
|
+
case 5 /* NodeTypes.List */:
|
|
1310
|
+
transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
|
|
1311
|
+
transformer.helper("list" /* HelperNameMap.LIST */);
|
|
1312
|
+
break;
|
|
1313
|
+
case 4 /* NodeTypes.Named */:
|
|
1314
|
+
transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
|
|
1315
|
+
transformer.helper("named" /* HelperNameMap.NAMED */);
|
|
1316
|
+
break;
|
|
1317
|
+
}
|
|
1318
|
+
// TODO: if we need post-hook of transform, should be implemented to here
|
|
1319
|
+
}
|
|
1320
|
+
// transform AST
|
|
1321
|
+
function transform(ast, options = {} // eslint-disable-line
|
|
1322
|
+
) {
|
|
1323
|
+
const transformer = createTransformer(ast);
|
|
1324
|
+
transformer.helper("normalize" /* HelperNameMap.NORMALIZE */);
|
|
1325
|
+
// traverse
|
|
1326
|
+
ast.body && traverseNode(ast.body, transformer);
|
|
1327
|
+
// set meta information
|
|
1328
|
+
const context = transformer.context();
|
|
1329
|
+
ast.helpers = Array.from(context.helpers);
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
function optimize(ast) {
|
|
1333
|
+
const body = ast.body;
|
|
1334
|
+
if (body.type === 2 /* NodeTypes.Message */) {
|
|
1335
|
+
optimizeMessageNode(body);
|
|
1336
|
+
}
|
|
1337
|
+
else {
|
|
1338
|
+
body.cases.forEach(c => optimizeMessageNode(c));
|
|
1339
|
+
}
|
|
1340
|
+
return ast;
|
|
1341
|
+
}
|
|
1342
|
+
function optimizeMessageNode(message) {
|
|
1343
|
+
if (message.items.length === 1) {
|
|
1344
|
+
const item = message.items[0];
|
|
1345
|
+
if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
|
|
1346
|
+
message.static = item.value;
|
|
1347
|
+
delete item.value; // optimization for size
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
else {
|
|
1351
|
+
const values = [];
|
|
1352
|
+
for (let i = 0; i < message.items.length; i++) {
|
|
1353
|
+
const item = message.items[i];
|
|
1354
|
+
if (!(item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */)) {
|
|
1355
|
+
break;
|
|
1356
|
+
}
|
|
1357
|
+
if (item.value == null) {
|
|
1358
|
+
break;
|
|
1359
|
+
}
|
|
1360
|
+
values.push(item.value);
|
|
1361
|
+
}
|
|
1362
|
+
if (values.length === message.items.length) {
|
|
1363
|
+
message.static = join(values);
|
|
1364
|
+
for (let i = 0; i < message.items.length; i++) {
|
|
1365
|
+
const item = message.items[i];
|
|
1366
|
+
if (item.type === 3 /* NodeTypes.Text */ || item.type === 9 /* NodeTypes.Literal */) {
|
|
1367
|
+
delete item.value; // optimization for size
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
const ERROR_DOMAIN$1 = 'minifier';
|
|
1375
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1376
|
+
function minify(node) {
|
|
1377
|
+
node.t = node.type;
|
|
1378
|
+
switch (node.type) {
|
|
1379
|
+
case 0 /* NodeTypes.Resource */:
|
|
1380
|
+
const resource = node;
|
|
1381
|
+
minify(resource.body);
|
|
1382
|
+
resource.b = resource.body;
|
|
1383
|
+
delete resource.body;
|
|
1384
|
+
break;
|
|
1385
|
+
case 1 /* NodeTypes.Plural */:
|
|
1386
|
+
const plural = node;
|
|
1387
|
+
const cases = plural.cases;
|
|
1388
|
+
for (let i = 0; i < cases.length; i++) {
|
|
1389
|
+
minify(cases[i]);
|
|
1390
|
+
}
|
|
1391
|
+
plural.c = cases;
|
|
1392
|
+
delete plural.cases;
|
|
1393
|
+
break;
|
|
1394
|
+
case 2 /* NodeTypes.Message */:
|
|
1395
|
+
const message = node;
|
|
1396
|
+
const items = message.items;
|
|
1397
|
+
for (let i = 0; i < items.length; i++) {
|
|
1398
|
+
minify(items[i]);
|
|
1399
|
+
}
|
|
1400
|
+
message.i = items;
|
|
1401
|
+
delete message.items;
|
|
1402
|
+
if (message.static) {
|
|
1403
|
+
message.s = message.static;
|
|
1404
|
+
delete message.static;
|
|
1405
|
+
}
|
|
1406
|
+
break;
|
|
1407
|
+
case 3 /* NodeTypes.Text */:
|
|
1408
|
+
case 9 /* NodeTypes.Literal */:
|
|
1409
|
+
case 8 /* NodeTypes.LinkedModifier */:
|
|
1410
|
+
case 7 /* NodeTypes.LinkedKey */:
|
|
1411
|
+
const valueNode = node;
|
|
1412
|
+
if (valueNode.value) {
|
|
1413
|
+
valueNode.v = valueNode.value;
|
|
1414
|
+
delete valueNode.value;
|
|
1415
|
+
}
|
|
1416
|
+
break;
|
|
1417
|
+
case 6 /* NodeTypes.Linked */:
|
|
1418
|
+
const linked = node;
|
|
1419
|
+
minify(linked.key);
|
|
1420
|
+
linked.k = linked.key;
|
|
1421
|
+
delete linked.key;
|
|
1422
|
+
if (linked.modifier) {
|
|
1423
|
+
minify(linked.modifier);
|
|
1424
|
+
linked.m = linked.modifier;
|
|
1425
|
+
delete linked.modifier;
|
|
1426
|
+
}
|
|
1427
|
+
break;
|
|
1428
|
+
case 5 /* NodeTypes.List */:
|
|
1429
|
+
const list = node;
|
|
1430
|
+
list.i = list.index;
|
|
1431
|
+
delete list.index;
|
|
1432
|
+
break;
|
|
1433
|
+
case 4 /* NodeTypes.Named */:
|
|
1434
|
+
const named = node;
|
|
1435
|
+
named.k = named.key;
|
|
1436
|
+
delete named.key;
|
|
1437
|
+
break;
|
|
1438
|
+
default:
|
|
1439
|
+
{
|
|
1440
|
+
throw createCompileError(CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE, null, {
|
|
1441
|
+
domain: ERROR_DOMAIN$1,
|
|
1442
|
+
args: [node.type]
|
|
1443
|
+
});
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
delete node.type;
|
|
1447
|
+
}
|
|
1448
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
1449
|
+
|
|
1450
|
+
const ERROR_DOMAIN = 'parser';
|
|
1451
|
+
function createCodeGenerator(ast, options) {
|
|
1452
|
+
const { sourceMap, filename, breakLineCode, needIndent: _needIndent } = options;
|
|
1453
|
+
const location = options.location !== false;
|
|
1454
|
+
const _context = {
|
|
1455
|
+
filename,
|
|
1456
|
+
code: '',
|
|
1457
|
+
column: 1,
|
|
1458
|
+
line: 1,
|
|
1459
|
+
offset: 0,
|
|
1460
|
+
map: undefined,
|
|
1461
|
+
breakLineCode,
|
|
1462
|
+
needIndent: _needIndent,
|
|
1463
|
+
indentLevel: 0
|
|
1464
|
+
};
|
|
1465
|
+
if (location && ast.loc) {
|
|
1466
|
+
_context.source = ast.loc.source;
|
|
1467
|
+
}
|
|
1468
|
+
const context = () => _context;
|
|
1469
|
+
function push(code, node) {
|
|
1470
|
+
_context.code += code;
|
|
1471
|
+
}
|
|
1472
|
+
function _newline(n, withBreakLine = true) {
|
|
1473
|
+
const _breakLineCode = withBreakLine ? breakLineCode : '';
|
|
1474
|
+
push(_needIndent ? _breakLineCode + ` `.repeat(n) : _breakLineCode);
|
|
1475
|
+
}
|
|
1476
|
+
function indent(withNewLine = true) {
|
|
1477
|
+
const level = ++_context.indentLevel;
|
|
1478
|
+
withNewLine && _newline(level);
|
|
1479
|
+
}
|
|
1480
|
+
function deindent(withNewLine = true) {
|
|
1481
|
+
const level = --_context.indentLevel;
|
|
1482
|
+
withNewLine && _newline(level);
|
|
1483
|
+
}
|
|
1484
|
+
function newline() {
|
|
1485
|
+
_newline(_context.indentLevel);
|
|
1486
|
+
}
|
|
1487
|
+
const helper = (key) => `_${key}`;
|
|
1488
|
+
const needIndent = () => _context.needIndent;
|
|
1489
|
+
return {
|
|
1490
|
+
context,
|
|
1491
|
+
push,
|
|
1492
|
+
indent,
|
|
1493
|
+
deindent,
|
|
1494
|
+
newline,
|
|
1495
|
+
helper,
|
|
1496
|
+
needIndent
|
|
1497
|
+
};
|
|
1498
|
+
}
|
|
1499
|
+
function generateLinkedNode(generator, node) {
|
|
1500
|
+
const { helper } = generator;
|
|
1501
|
+
generator.push(`${helper("linked" /* HelperNameMap.LINKED */)}(`);
|
|
1502
|
+
generateNode(generator, node.key);
|
|
1503
|
+
if (node.modifier) {
|
|
1504
|
+
generator.push(`, `);
|
|
1505
|
+
generateNode(generator, node.modifier);
|
|
1506
|
+
generator.push(`, _type`);
|
|
1507
|
+
}
|
|
1508
|
+
else {
|
|
1509
|
+
generator.push(`, undefined, _type`);
|
|
1510
|
+
}
|
|
1511
|
+
generator.push(`)`);
|
|
1512
|
+
}
|
|
1513
|
+
function generateMessageNode(generator, node) {
|
|
1514
|
+
const { helper, needIndent } = generator;
|
|
1515
|
+
generator.push(`${helper("normalize" /* HelperNameMap.NORMALIZE */)}([`);
|
|
1516
|
+
generator.indent(needIndent());
|
|
1517
|
+
const length = node.items.length;
|
|
1518
|
+
for (let i = 0; i < length; i++) {
|
|
1519
|
+
generateNode(generator, node.items[i]);
|
|
1520
|
+
if (i === length - 1) {
|
|
1521
|
+
break;
|
|
1522
|
+
}
|
|
1523
|
+
generator.push(', ');
|
|
1524
|
+
}
|
|
1525
|
+
generator.deindent(needIndent());
|
|
1526
|
+
generator.push('])');
|
|
1527
|
+
}
|
|
1528
|
+
function generatePluralNode(generator, node) {
|
|
1529
|
+
const { helper, needIndent } = generator;
|
|
1530
|
+
if (node.cases.length > 1) {
|
|
1531
|
+
generator.push(`${helper("plural" /* HelperNameMap.PLURAL */)}([`);
|
|
1532
|
+
generator.indent(needIndent());
|
|
1533
|
+
const length = node.cases.length;
|
|
1534
|
+
for (let i = 0; i < length; i++) {
|
|
1535
|
+
generateNode(generator, node.cases[i]);
|
|
1536
|
+
if (i === length - 1) {
|
|
1537
|
+
break;
|
|
1538
|
+
}
|
|
1539
|
+
generator.push(', ');
|
|
1540
|
+
}
|
|
1541
|
+
generator.deindent(needIndent());
|
|
1542
|
+
generator.push(`])`);
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
function generateResource(generator, node) {
|
|
1546
|
+
if (node.body) {
|
|
1547
|
+
generateNode(generator, node.body);
|
|
1548
|
+
}
|
|
1549
|
+
else {
|
|
1550
|
+
generator.push('null');
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
function generateNode(generator, node) {
|
|
1554
|
+
const { helper } = generator;
|
|
1555
|
+
switch (node.type) {
|
|
1556
|
+
case 0 /* NodeTypes.Resource */:
|
|
1557
|
+
generateResource(generator, node);
|
|
1558
|
+
break;
|
|
1559
|
+
case 1 /* NodeTypes.Plural */:
|
|
1560
|
+
generatePluralNode(generator, node);
|
|
1561
|
+
break;
|
|
1562
|
+
case 2 /* NodeTypes.Message */:
|
|
1563
|
+
generateMessageNode(generator, node);
|
|
1564
|
+
break;
|
|
1565
|
+
case 6 /* NodeTypes.Linked */:
|
|
1566
|
+
generateLinkedNode(generator, node);
|
|
1567
|
+
break;
|
|
1568
|
+
case 8 /* NodeTypes.LinkedModifier */:
|
|
1569
|
+
generator.push(JSON.stringify(node.value), node);
|
|
1570
|
+
break;
|
|
1571
|
+
case 7 /* NodeTypes.LinkedKey */:
|
|
1572
|
+
generator.push(JSON.stringify(node.value), node);
|
|
1573
|
+
break;
|
|
1574
|
+
case 5 /* NodeTypes.List */:
|
|
1575
|
+
generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("list" /* HelperNameMap.LIST */)}(${node.index}))`, node);
|
|
1576
|
+
break;
|
|
1577
|
+
case 4 /* NodeTypes.Named */:
|
|
1578
|
+
generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("named" /* HelperNameMap.NAMED */)}(${JSON.stringify(node.key)}))`, node);
|
|
1579
|
+
break;
|
|
1580
|
+
case 9 /* NodeTypes.Literal */:
|
|
1581
|
+
generator.push(JSON.stringify(node.value), node);
|
|
1582
|
+
break;
|
|
1583
|
+
case 3 /* NodeTypes.Text */:
|
|
1584
|
+
generator.push(JSON.stringify(node.value), node);
|
|
1585
|
+
break;
|
|
1586
|
+
default:
|
|
1587
|
+
{
|
|
1588
|
+
throw createCompileError(CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE, null, {
|
|
1589
|
+
domain: ERROR_DOMAIN,
|
|
1590
|
+
args: [node.type]
|
|
1591
|
+
});
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
// generate code from AST
|
|
1596
|
+
const generate = (ast, options = {} // eslint-disable-line
|
|
1597
|
+
) => {
|
|
1598
|
+
const mode = isString(options.mode) ? options.mode : 'normal';
|
|
1599
|
+
const filename = isString(options.filename)
|
|
1600
|
+
? options.filename
|
|
1601
|
+
: 'message.intl';
|
|
1602
|
+
const sourceMap = !!options.sourceMap;
|
|
1603
|
+
// prettier-ignore
|
|
1604
|
+
const breakLineCode = options.breakLineCode != null
|
|
1605
|
+
? options.breakLineCode
|
|
1606
|
+
: mode === 'arrow'
|
|
1607
|
+
? ';'
|
|
1608
|
+
: '\n';
|
|
1609
|
+
const needIndent = options.needIndent ? options.needIndent : mode !== 'arrow';
|
|
1610
|
+
const helpers = ast.helpers || [];
|
|
1611
|
+
const generator = createCodeGenerator(ast, {
|
|
1612
|
+
mode,
|
|
1613
|
+
filename,
|
|
1614
|
+
sourceMap,
|
|
1615
|
+
breakLineCode,
|
|
1616
|
+
needIndent
|
|
1617
|
+
});
|
|
1618
|
+
generator.push(mode === 'normal' ? `function __msg__ (ctx) {` : `(ctx) => {`);
|
|
1619
|
+
generator.indent(needIndent);
|
|
1620
|
+
if (helpers.length > 0) {
|
|
1621
|
+
generator.push(`const { ${join(helpers.map(s => `${s}: _${s}`), ', ')} } = ctx`);
|
|
1622
|
+
generator.newline();
|
|
1623
|
+
}
|
|
1624
|
+
generator.push(`return `);
|
|
1625
|
+
generateNode(generator, ast);
|
|
1626
|
+
generator.deindent(needIndent);
|
|
1627
|
+
generator.push(`}`);
|
|
1628
|
+
delete ast.helpers;
|
|
1629
|
+
const { code, map } = generator.context();
|
|
1630
|
+
return {
|
|
1631
|
+
ast,
|
|
1632
|
+
code,
|
|
1633
|
+
map: map ? map.toJSON() : undefined // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1634
|
+
};
|
|
1635
|
+
};
|
|
1636
|
+
|
|
1637
|
+
function baseCompile$1(source, options = {}) {
|
|
1638
|
+
const assignedOptions = assign({}, options);
|
|
1639
|
+
const jit = !!assignedOptions.jit;
|
|
1640
|
+
const enalbeMinify = !!assignedOptions.minify;
|
|
1641
|
+
const enambeOptimize = assignedOptions.optimize == null ? true : assignedOptions.optimize;
|
|
1642
|
+
// parse source codes
|
|
1643
|
+
const parser = createParser(assignedOptions);
|
|
1644
|
+
const ast = parser.parse(source);
|
|
1645
|
+
if (!jit) {
|
|
1646
|
+
// transform ASTs
|
|
1647
|
+
transform(ast, assignedOptions);
|
|
1648
|
+
// generate javascript codes
|
|
1649
|
+
return generate(ast, assignedOptions);
|
|
1650
|
+
}
|
|
1651
|
+
else {
|
|
1652
|
+
// optimize ASTs
|
|
1653
|
+
enambeOptimize && optimize(ast);
|
|
1654
|
+
// minimize ASTs
|
|
1655
|
+
enalbeMinify && minify(ast);
|
|
1656
|
+
// In JIT mode, no ast transform, no code generation.
|
|
1657
|
+
return { ast, code: '' };
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
206
1660
|
|
|
207
1661
|
const pathStateMachine = [];
|
|
208
1662
|
pathStateMachine[0 /* States.BEFORE_PATH */] = {
|
|
@@ -599,11 +2053,6 @@ var IntlifyCore = (function (exports) {
|
|
|
599
2053
|
return ctx;
|
|
600
2054
|
}
|
|
601
2055
|
|
|
602
|
-
const IntlifyDevToolsHooks = {
|
|
603
|
-
I18nInit: 'i18n:init',
|
|
604
|
-
FunctionTranslate: 'function:translate'
|
|
605
|
-
};
|
|
606
|
-
|
|
607
2056
|
let devtools = null;
|
|
608
2057
|
function setDevToolsHook(hook) {
|
|
609
2058
|
devtools = hook;
|
|
@@ -614,14 +2063,14 @@ var IntlifyCore = (function (exports) {
|
|
|
614
2063
|
function initI18nDevTools(i18n, version, meta) {
|
|
615
2064
|
// TODO: queue if devtools is undefined
|
|
616
2065
|
devtools &&
|
|
617
|
-
devtools.emit(IntlifyDevToolsHooks.I18nInit
|
|
2066
|
+
devtools.emit("i18n:init" /* IntlifyDevToolsHooks.I18nInit */, {
|
|
618
2067
|
timestamp: Date.now(),
|
|
619
2068
|
i18n,
|
|
620
2069
|
version,
|
|
621
2070
|
meta
|
|
622
2071
|
});
|
|
623
2072
|
}
|
|
624
|
-
const translateDevTools = /* #__PURE__*/ createDevToolsHook(IntlifyDevToolsHooks.FunctionTranslate);
|
|
2073
|
+
const translateDevTools = /* #__PURE__*/ createDevToolsHook("function:translate" /* IntlifyDevToolsHooks.FunctionTranslate */);
|
|
625
2074
|
function createDevToolsHook(hook) {
|
|
626
2075
|
return (payloads) => devtools && devtools.emit(hook, payloads);
|
|
627
2076
|
}
|
|
@@ -771,7 +2220,7 @@ var IntlifyCore = (function (exports) {
|
|
|
771
2220
|
* Intlify core-base version
|
|
772
2221
|
* @internal
|
|
773
2222
|
*/
|
|
774
|
-
const VERSION = '9.
|
|
2223
|
+
const VERSION = '9.4.0';
|
|
775
2224
|
const NOT_REOSLVED = -1;
|
|
776
2225
|
const DEFAULT_LOCALE = 'en-US';
|
|
777
2226
|
const MISSING_RESOLVE_VALUE = '';
|
|
@@ -1074,6 +2523,13 @@ var IntlifyCore = (function (exports) {
|
|
|
1074
2523
|
[CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: 'Not support non-string message'
|
|
1075
2524
|
};
|
|
1076
2525
|
|
|
2526
|
+
const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
|
|
2527
|
+
function checkHtmlMessage(source, warnHtmlMessage) {
|
|
2528
|
+
if (warnHtmlMessage && detectHtmlTag(source)) {
|
|
2529
|
+
warn(format$1(WARN_MESSAGE, { source }));
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
const defaultOnCacheKey = (message) => message;
|
|
1077
2533
|
let compileCache = Object.create(null);
|
|
1078
2534
|
function clearCompileCache() {
|
|
1079
2535
|
compileCache = Object.create(null);
|
|
@@ -1081,7 +2537,18 @@ var IntlifyCore = (function (exports) {
|
|
|
1081
2537
|
const isMessageAST = (val) => isObject(val) &&
|
|
1082
2538
|
(val.t === 0 || val.type === 0) &&
|
|
1083
2539
|
('b' in val || 'body' in val);
|
|
1084
|
-
function
|
|
2540
|
+
function baseCompile(message, options = {}) {
|
|
2541
|
+
// error detecting on compile
|
|
2542
|
+
let detectError = false;
|
|
2543
|
+
const onError = options.onError || defaultOnError;
|
|
2544
|
+
options.onError = (err) => {
|
|
2545
|
+
detectError = true;
|
|
2546
|
+
onError(err);
|
|
2547
|
+
};
|
|
2548
|
+
// compile with mesasge-compiler
|
|
2549
|
+
return { ...baseCompile$1(message, options), detectError };
|
|
2550
|
+
}
|
|
2551
|
+
const compileToFunction = /* #__PURE__*/ (message, context) => {
|
|
1085
2552
|
if (!isString(message)) {
|
|
1086
2553
|
throw createCoreError(CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE);
|
|
1087
2554
|
}
|
|
@@ -1089,9 +2556,35 @@ var IntlifyCore = (function (exports) {
|
|
|
1089
2556
|
warn(`Runtime compilation is not supported in ${'core.runtime.global.js' }.`);
|
|
1090
2557
|
return (() => message);
|
|
1091
2558
|
}
|
|
1092
|
-
}
|
|
2559
|
+
};
|
|
1093
2560
|
function compile(message, context) {
|
|
1094
|
-
{
|
|
2561
|
+
if (isString(message)) {
|
|
2562
|
+
// check HTML message
|
|
2563
|
+
const warnHtmlMessage = isBoolean(context.warnHtmlMessage)
|
|
2564
|
+
? context.warnHtmlMessage
|
|
2565
|
+
: true;
|
|
2566
|
+
checkHtmlMessage(message, warnHtmlMessage);
|
|
2567
|
+
// check caches
|
|
2568
|
+
const onCacheKey = context.onCacheKey || defaultOnCacheKey;
|
|
2569
|
+
const cacheKey = onCacheKey(message);
|
|
2570
|
+
const cached = compileCache[cacheKey];
|
|
2571
|
+
if (cached) {
|
|
2572
|
+
return cached;
|
|
2573
|
+
}
|
|
2574
|
+
// compile with JIT mode
|
|
2575
|
+
const { ast, detectError } = baseCompile(message, {
|
|
2576
|
+
...context,
|
|
2577
|
+
location: true,
|
|
2578
|
+
jit: true
|
|
2579
|
+
});
|
|
2580
|
+
// compose message function from AST
|
|
2581
|
+
const msg = format(ast);
|
|
2582
|
+
// if occurred compile error, don't cache
|
|
2583
|
+
return !detectError
|
|
2584
|
+
? (compileCache[cacheKey] = msg)
|
|
2585
|
+
: msg;
|
|
2586
|
+
}
|
|
2587
|
+
else {
|
|
1095
2588
|
if (!isMessageAST(message)) {
|
|
1096
2589
|
warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
|
|
1097
2590
|
return (() => message);
|