@intlify/message-compiler 9.11.1 → 9.12.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/dist/message-compiler.cjs +91 -34
- package/dist/message-compiler.d.ts +23 -0
- package/dist/message-compiler.esm-browser.js +89 -35
- package/dist/message-compiler.esm-browser.prod.js +2 -2
- package/dist/message-compiler.global.js +91 -34
- package/dist/message-compiler.global.prod.js +2 -2
- package/dist/message-compiler.mjs +89 -35
- package/dist/message-compiler.node.mjs +89 -35
- package/dist/message-compiler.prod.cjs +91 -34
- package/package.json +2 -2
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* message-compiler v9.
|
|
2
|
+
* message-compiler v9.12.1
|
|
3
3
|
* (c) 2024 kazuya kawaguchi
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -23,6 +23,23 @@ function createLocation(start, end, source) {
|
|
|
23
23
|
return loc;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
const CompileWarnCodes = {
|
|
27
|
+
USE_MODULO_SYNTAX: 1,
|
|
28
|
+
__EXTEND_POINT__: 2
|
|
29
|
+
};
|
|
30
|
+
/** @internal */
|
|
31
|
+
const warnMessages = {
|
|
32
|
+
[CompileWarnCodes.USE_MODULO_SYNTAX]: `Use modulo before '{{0}}'.`
|
|
33
|
+
};
|
|
34
|
+
function createCompileWarn(code, loc, ...args) {
|
|
35
|
+
const msg = shared.format(warnMessages[code] || '', ...(args || [])) ;
|
|
36
|
+
const message = { message: String(msg), code };
|
|
37
|
+
if (loc) {
|
|
38
|
+
message.location = loc;
|
|
39
|
+
}
|
|
40
|
+
return message;
|
|
41
|
+
}
|
|
42
|
+
|
|
26
43
|
const CompileErrorCodes = {
|
|
27
44
|
// tokenizer error codes
|
|
28
45
|
EXPECTED_TOKEN: 1,
|
|
@@ -416,33 +433,46 @@ function createTokenizer(source, options = {}) {
|
|
|
416
433
|
}
|
|
417
434
|
return null;
|
|
418
435
|
}
|
|
436
|
+
function isIdentifier(ch) {
|
|
437
|
+
const cc = ch.charCodeAt(0);
|
|
438
|
+
return ((cc >= 97 && cc <= 122) || // a-z
|
|
439
|
+
(cc >= 65 && cc <= 90) || // A-Z
|
|
440
|
+
(cc >= 48 && cc <= 57) || // 0-9
|
|
441
|
+
cc === 95 || // _
|
|
442
|
+
cc === 36 // $
|
|
443
|
+
);
|
|
444
|
+
}
|
|
419
445
|
function takeIdentifierChar(scnr) {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
446
|
+
return takeChar(scnr, isIdentifier);
|
|
447
|
+
}
|
|
448
|
+
function isNamedIdentifier(ch) {
|
|
449
|
+
const cc = ch.charCodeAt(0);
|
|
450
|
+
return ((cc >= 97 && cc <= 122) || // a-z
|
|
451
|
+
(cc >= 65 && cc <= 90) || // A-Z
|
|
452
|
+
(cc >= 48 && cc <= 57) || // 0-9
|
|
453
|
+
cc === 95 || // _
|
|
454
|
+
cc === 36 || // $
|
|
455
|
+
cc === 45 // -
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
function takeNamedIdentifierChar(scnr) {
|
|
459
|
+
return takeChar(scnr, isNamedIdentifier);
|
|
460
|
+
}
|
|
461
|
+
function isDigit(ch) {
|
|
462
|
+
const cc = ch.charCodeAt(0);
|
|
463
|
+
return cc >= 48 && cc <= 57; // 0-9
|
|
430
464
|
}
|
|
431
465
|
function takeDigit(scnr) {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
return
|
|
466
|
+
return takeChar(scnr, isDigit);
|
|
467
|
+
}
|
|
468
|
+
function isHexDigit(ch) {
|
|
469
|
+
const cc = ch.charCodeAt(0);
|
|
470
|
+
return ((cc >= 48 && cc <= 57) || // 0-9
|
|
471
|
+
(cc >= 65 && cc <= 70) || // A-F
|
|
472
|
+
(cc >= 97 && cc <= 102)); // a-f
|
|
437
473
|
}
|
|
438
474
|
function takeHexDigit(scnr) {
|
|
439
|
-
|
|
440
|
-
const cc = ch.charCodeAt(0);
|
|
441
|
-
return ((cc >= 48 && cc <= 57) || // 0-9
|
|
442
|
-
(cc >= 65 && cc <= 70) || // A-F
|
|
443
|
-
(cc >= 97 && cc <= 102)); // a-f
|
|
444
|
-
};
|
|
445
|
-
return takeChar(scnr, closure);
|
|
475
|
+
return takeChar(scnr, isHexDigit);
|
|
446
476
|
}
|
|
447
477
|
function getDigits(scnr) {
|
|
448
478
|
let ch = '';
|
|
@@ -506,7 +536,7 @@ function createTokenizer(source, options = {}) {
|
|
|
506
536
|
skipSpaces(scnr);
|
|
507
537
|
let ch = '';
|
|
508
538
|
let name = '';
|
|
509
|
-
while ((ch =
|
|
539
|
+
while ((ch = takeNamedIdentifierChar(scnr))) {
|
|
510
540
|
name += ch;
|
|
511
541
|
}
|
|
512
542
|
if (scnr.currentChar() === EOF) {
|
|
@@ -529,14 +559,16 @@ function createTokenizer(source, options = {}) {
|
|
|
529
559
|
}
|
|
530
560
|
return value;
|
|
531
561
|
}
|
|
562
|
+
function isLiteral(ch) {
|
|
563
|
+
return ch !== LITERAL_DELIMITER && ch !== CHAR_LF;
|
|
564
|
+
}
|
|
532
565
|
function readLiteral(scnr) {
|
|
533
566
|
skipSpaces(scnr);
|
|
534
567
|
// eslint-disable-next-line no-useless-escape
|
|
535
568
|
eat(scnr, `\'`);
|
|
536
569
|
let ch = '';
|
|
537
570
|
let literal = '';
|
|
538
|
-
|
|
539
|
-
while ((ch = takeChar(scnr, fn))) {
|
|
571
|
+
while ((ch = takeChar(scnr, isLiteral))) {
|
|
540
572
|
if (ch === '\\') {
|
|
541
573
|
literal += readEscapeSequence(scnr);
|
|
542
574
|
}
|
|
@@ -588,15 +620,17 @@ function createTokenizer(source, options = {}) {
|
|
|
588
620
|
}
|
|
589
621
|
return `\\${unicode}${sequence}`;
|
|
590
622
|
}
|
|
623
|
+
function isInvalidIdentifier(ch) {
|
|
624
|
+
return (ch !== "{" /* TokenChars.BraceLeft */ &&
|
|
625
|
+
ch !== "}" /* TokenChars.BraceRight */ &&
|
|
626
|
+
ch !== CHAR_SP &&
|
|
627
|
+
ch !== CHAR_LF);
|
|
628
|
+
}
|
|
591
629
|
function readInvalidIdentifier(scnr) {
|
|
592
630
|
skipSpaces(scnr);
|
|
593
631
|
let ch = '';
|
|
594
632
|
let identifiers = '';
|
|
595
|
-
|
|
596
|
-
ch !== "}" /* TokenChars.BraceRight */ &&
|
|
597
|
-
ch !== CHAR_SP &&
|
|
598
|
-
ch !== CHAR_LF;
|
|
599
|
-
while ((ch = takeChar(scnr, closure))) {
|
|
633
|
+
while ((ch = takeChar(scnr, isInvalidIdentifier))) {
|
|
600
634
|
identifiers += ch;
|
|
601
635
|
}
|
|
602
636
|
return identifiers;
|
|
@@ -873,7 +907,7 @@ function fromEscapeSequence(match, codePoint4, codePoint6) {
|
|
|
873
907
|
}
|
|
874
908
|
function createParser(options = {}) {
|
|
875
909
|
const location = options.location !== false;
|
|
876
|
-
const { onError } = options;
|
|
910
|
+
const { onError, onWarn } = options;
|
|
877
911
|
function emitError(tokenzer, code, start, offset, ...args) {
|
|
878
912
|
const end = tokenzer.currentPosition();
|
|
879
913
|
end.offset += offset;
|
|
@@ -887,6 +921,15 @@ function createParser(options = {}) {
|
|
|
887
921
|
onError(err);
|
|
888
922
|
}
|
|
889
923
|
}
|
|
924
|
+
function emitWarn(tokenzer, code, start, offset, ...args) {
|
|
925
|
+
const end = tokenzer.currentPosition();
|
|
926
|
+
end.offset += offset;
|
|
927
|
+
end.column += offset;
|
|
928
|
+
if (onWarn) {
|
|
929
|
+
const loc = location ? createLocation(start, end) : null;
|
|
930
|
+
onWarn(createCompileWarn(code, loc, args));
|
|
931
|
+
}
|
|
932
|
+
}
|
|
890
933
|
function startNode(type, offset, loc) {
|
|
891
934
|
const node = { type };
|
|
892
935
|
if (location) {
|
|
@@ -923,11 +966,14 @@ function createParser(options = {}) {
|
|
|
923
966
|
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
924
967
|
return node;
|
|
925
968
|
}
|
|
926
|
-
function parseNamed(tokenizer, key) {
|
|
969
|
+
function parseNamed(tokenizer, key, modulo) {
|
|
927
970
|
const context = tokenizer.context();
|
|
928
971
|
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
929
972
|
const node = startNode(4 /* NodeTypes.Named */, offset, loc);
|
|
930
973
|
node.key = key;
|
|
974
|
+
if (modulo === true) {
|
|
975
|
+
node.modulo = true;
|
|
976
|
+
}
|
|
931
977
|
tokenizer.nextToken(); // skip brach right
|
|
932
978
|
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
933
979
|
return node;
|
|
@@ -1047,6 +1093,7 @@ function createParser(options = {}) {
|
|
|
1047
1093
|
const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
|
|
1048
1094
|
node.items = [];
|
|
1049
1095
|
let nextToken = null;
|
|
1096
|
+
let modulo = null;
|
|
1050
1097
|
do {
|
|
1051
1098
|
const token = nextToken || tokenizer.nextToken();
|
|
1052
1099
|
nextToken = null;
|
|
@@ -1063,11 +1110,18 @@ function createParser(options = {}) {
|
|
|
1063
1110
|
}
|
|
1064
1111
|
node.items.push(parseList(tokenizer, token.value || ''));
|
|
1065
1112
|
break;
|
|
1113
|
+
case 4 /* TokenTypes.Modulo */:
|
|
1114
|
+
modulo = true;
|
|
1115
|
+
break;
|
|
1066
1116
|
case 5 /* TokenTypes.Named */:
|
|
1067
1117
|
if (token.value == null) {
|
|
1068
1118
|
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1069
1119
|
}
|
|
1070
|
-
node.items.push(parseNamed(tokenizer, token.value || ''));
|
|
1120
|
+
node.items.push(parseNamed(tokenizer, token.value || '', !!modulo));
|
|
1121
|
+
if (modulo) {
|
|
1122
|
+
emitWarn(tokenizer, CompileWarnCodes.USE_MODULO_SYNTAX, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1123
|
+
modulo = null;
|
|
1124
|
+
}
|
|
1071
1125
|
break;
|
|
1072
1126
|
case 7 /* TokenTypes.Literal */:
|
|
1073
1127
|
if (token.value == null) {
|
|
@@ -1605,13 +1659,16 @@ function baseCompile(source, options = {}) {
|
|
|
1605
1659
|
}
|
|
1606
1660
|
|
|
1607
1661
|
exports.CompileErrorCodes = CompileErrorCodes;
|
|
1662
|
+
exports.CompileWarnCodes = CompileWarnCodes;
|
|
1608
1663
|
exports.ERROR_DOMAIN = ERROR_DOMAIN$2;
|
|
1609
1664
|
exports.LOCATION_STUB = LOCATION_STUB;
|
|
1610
1665
|
exports.baseCompile = baseCompile;
|
|
1611
1666
|
exports.createCompileError = createCompileError;
|
|
1667
|
+
exports.createCompileWarn = createCompileWarn;
|
|
1612
1668
|
exports.createLocation = createLocation;
|
|
1613
1669
|
exports.createParser = createParser;
|
|
1614
1670
|
exports.createPosition = createPosition;
|
|
1615
1671
|
exports.defaultOnError = defaultOnError;
|
|
1616
1672
|
exports.detectHtmlTag = detectHtmlTag;
|
|
1617
1673
|
exports.errorMessages = errorMessages;
|
|
1674
|
+
exports.warnMessages = warnMessages;
|
|
@@ -12,6 +12,7 @@ export declare interface CodeGenOptions {
|
|
|
12
12
|
mode?: 'normal' | 'arrow';
|
|
13
13
|
breakLineCode?: '\n' | ';';
|
|
14
14
|
needIndent?: boolean;
|
|
15
|
+
onWarn?: CompileWarnHandler;
|
|
15
16
|
onError?: CompileErrorHandler;
|
|
16
17
|
sourceMap?: boolean;
|
|
17
18
|
filename?: string;
|
|
@@ -70,8 +71,25 @@ export declare type CompileOptions = {
|
|
|
70
71
|
|
|
71
72
|
export declare type CompilerResult = CodeGenResult;
|
|
72
73
|
|
|
74
|
+
export declare interface CompileWarn {
|
|
75
|
+
message: string;
|
|
76
|
+
code: number;
|
|
77
|
+
location?: SourceLocation;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export declare const CompileWarnCodes: {
|
|
81
|
+
readonly USE_MODULO_SYNTAX: 1;
|
|
82
|
+
readonly __EXTEND_POINT__: 2;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export declare type CompileWarnCodes = (typeof CompileWarnCodes)[keyof typeof CompileWarnCodes];
|
|
86
|
+
|
|
87
|
+
export declare type CompileWarnHandler = (warn: CompileWarn) => void;
|
|
88
|
+
|
|
73
89
|
export declare function createCompileError<T extends number>(code: T, loc: SourceLocation | null, options?: CompileErrorOptions): CompileError;
|
|
74
90
|
|
|
91
|
+
export declare function createCompileWarn<T extends number>(code: T, loc: SourceLocation | null, ...args: unknown[]): CompileWarn;
|
|
92
|
+
|
|
75
93
|
export declare function createLocation(start: Position, end: Position, source?: string): SourceLocation;
|
|
76
94
|
|
|
77
95
|
export declare function createParser(options?: ParserOptions): Parser;
|
|
@@ -147,6 +165,7 @@ export declare interface MessageNode extends Node_2 {
|
|
|
147
165
|
export declare interface NamedNode extends Node_2 {
|
|
148
166
|
type: NodeTypes.Named;
|
|
149
167
|
key: Identifier;
|
|
168
|
+
modulo?: boolean;
|
|
150
169
|
/* Excluded from this release type: k */
|
|
151
170
|
}
|
|
152
171
|
|
|
@@ -179,6 +198,7 @@ export declare interface Parser {
|
|
|
179
198
|
export declare interface ParserOptions {
|
|
180
199
|
location?: boolean;
|
|
181
200
|
onCacheKey?: (source: string) => string;
|
|
201
|
+
onWarn?: CompileWarnHandler;
|
|
182
202
|
onError?: CompileErrorHandler;
|
|
183
203
|
}
|
|
184
204
|
|
|
@@ -220,7 +240,10 @@ export declare interface TokenizeOptions {
|
|
|
220
240
|
}
|
|
221
241
|
|
|
222
242
|
export declare interface TransformOptions {
|
|
243
|
+
onWarn?: CompileWarnHandler;
|
|
223
244
|
onError?: CompileErrorHandler;
|
|
224
245
|
}
|
|
225
246
|
|
|
247
|
+
/* Excluded from this release type: warnMessages */
|
|
248
|
+
|
|
226
249
|
export { }
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* message-compiler v9.
|
|
2
|
+
* message-compiler v9.12.1
|
|
3
3
|
* (c) 2024 kazuya kawaguchi
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -43,6 +43,23 @@ function join(items, separator = '') {
|
|
|
43
43
|
return items.reduce((str, item, index) => (index === 0 ? str + item : str + separator + item), '');
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
const CompileWarnCodes = {
|
|
47
|
+
USE_MODULO_SYNTAX: 1,
|
|
48
|
+
__EXTEND_POINT__: 2
|
|
49
|
+
};
|
|
50
|
+
/** @internal */
|
|
51
|
+
const warnMessages = {
|
|
52
|
+
[CompileWarnCodes.USE_MODULO_SYNTAX]: `Use modulo before '{{0}}'.`
|
|
53
|
+
};
|
|
54
|
+
function createCompileWarn(code, loc, ...args) {
|
|
55
|
+
const msg = format(warnMessages[code] || '', ...(args || [])) ;
|
|
56
|
+
const message = { message: String(msg), code };
|
|
57
|
+
if (loc) {
|
|
58
|
+
message.location = loc;
|
|
59
|
+
}
|
|
60
|
+
return message;
|
|
61
|
+
}
|
|
62
|
+
|
|
46
63
|
const CompileErrorCodes = {
|
|
47
64
|
// tokenizer error codes
|
|
48
65
|
EXPECTED_TOKEN: 1,
|
|
@@ -436,33 +453,46 @@ function createTokenizer(source, options = {}) {
|
|
|
436
453
|
}
|
|
437
454
|
return null;
|
|
438
455
|
}
|
|
456
|
+
function isIdentifier(ch) {
|
|
457
|
+
const cc = ch.charCodeAt(0);
|
|
458
|
+
return ((cc >= 97 && cc <= 122) || // a-z
|
|
459
|
+
(cc >= 65 && cc <= 90) || // A-Z
|
|
460
|
+
(cc >= 48 && cc <= 57) || // 0-9
|
|
461
|
+
cc === 95 || // _
|
|
462
|
+
cc === 36 // $
|
|
463
|
+
);
|
|
464
|
+
}
|
|
439
465
|
function takeIdentifierChar(scnr) {
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
466
|
+
return takeChar(scnr, isIdentifier);
|
|
467
|
+
}
|
|
468
|
+
function isNamedIdentifier(ch) {
|
|
469
|
+
const cc = ch.charCodeAt(0);
|
|
470
|
+
return ((cc >= 97 && cc <= 122) || // a-z
|
|
471
|
+
(cc >= 65 && cc <= 90) || // A-Z
|
|
472
|
+
(cc >= 48 && cc <= 57) || // 0-9
|
|
473
|
+
cc === 95 || // _
|
|
474
|
+
cc === 36 || // $
|
|
475
|
+
cc === 45 // -
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
function takeNamedIdentifierChar(scnr) {
|
|
479
|
+
return takeChar(scnr, isNamedIdentifier);
|
|
480
|
+
}
|
|
481
|
+
function isDigit(ch) {
|
|
482
|
+
const cc = ch.charCodeAt(0);
|
|
483
|
+
return cc >= 48 && cc <= 57; // 0-9
|
|
450
484
|
}
|
|
451
485
|
function takeDigit(scnr) {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
return
|
|
486
|
+
return takeChar(scnr, isDigit);
|
|
487
|
+
}
|
|
488
|
+
function isHexDigit(ch) {
|
|
489
|
+
const cc = ch.charCodeAt(0);
|
|
490
|
+
return ((cc >= 48 && cc <= 57) || // 0-9
|
|
491
|
+
(cc >= 65 && cc <= 70) || // A-F
|
|
492
|
+
(cc >= 97 && cc <= 102)); // a-f
|
|
457
493
|
}
|
|
458
494
|
function takeHexDigit(scnr) {
|
|
459
|
-
|
|
460
|
-
const cc = ch.charCodeAt(0);
|
|
461
|
-
return ((cc >= 48 && cc <= 57) || // 0-9
|
|
462
|
-
(cc >= 65 && cc <= 70) || // A-F
|
|
463
|
-
(cc >= 97 && cc <= 102)); // a-f
|
|
464
|
-
};
|
|
465
|
-
return takeChar(scnr, closure);
|
|
495
|
+
return takeChar(scnr, isHexDigit);
|
|
466
496
|
}
|
|
467
497
|
function getDigits(scnr) {
|
|
468
498
|
let ch = '';
|
|
@@ -526,7 +556,7 @@ function createTokenizer(source, options = {}) {
|
|
|
526
556
|
skipSpaces(scnr);
|
|
527
557
|
let ch = '';
|
|
528
558
|
let name = '';
|
|
529
|
-
while ((ch =
|
|
559
|
+
while ((ch = takeNamedIdentifierChar(scnr))) {
|
|
530
560
|
name += ch;
|
|
531
561
|
}
|
|
532
562
|
if (scnr.currentChar() === EOF) {
|
|
@@ -549,14 +579,16 @@ function createTokenizer(source, options = {}) {
|
|
|
549
579
|
}
|
|
550
580
|
return value;
|
|
551
581
|
}
|
|
582
|
+
function isLiteral(ch) {
|
|
583
|
+
return ch !== LITERAL_DELIMITER && ch !== CHAR_LF;
|
|
584
|
+
}
|
|
552
585
|
function readLiteral(scnr) {
|
|
553
586
|
skipSpaces(scnr);
|
|
554
587
|
// eslint-disable-next-line no-useless-escape
|
|
555
588
|
eat(scnr, `\'`);
|
|
556
589
|
let ch = '';
|
|
557
590
|
let literal = '';
|
|
558
|
-
|
|
559
|
-
while ((ch = takeChar(scnr, fn))) {
|
|
591
|
+
while ((ch = takeChar(scnr, isLiteral))) {
|
|
560
592
|
if (ch === '\\') {
|
|
561
593
|
literal += readEscapeSequence(scnr);
|
|
562
594
|
}
|
|
@@ -608,15 +640,17 @@ function createTokenizer(source, options = {}) {
|
|
|
608
640
|
}
|
|
609
641
|
return `\\${unicode}${sequence}`;
|
|
610
642
|
}
|
|
643
|
+
function isInvalidIdentifier(ch) {
|
|
644
|
+
return (ch !== "{" /* TokenChars.BraceLeft */ &&
|
|
645
|
+
ch !== "}" /* TokenChars.BraceRight */ &&
|
|
646
|
+
ch !== CHAR_SP &&
|
|
647
|
+
ch !== CHAR_LF);
|
|
648
|
+
}
|
|
611
649
|
function readInvalidIdentifier(scnr) {
|
|
612
650
|
skipSpaces(scnr);
|
|
613
651
|
let ch = '';
|
|
614
652
|
let identifiers = '';
|
|
615
|
-
|
|
616
|
-
ch !== "}" /* TokenChars.BraceRight */ &&
|
|
617
|
-
ch !== CHAR_SP &&
|
|
618
|
-
ch !== CHAR_LF;
|
|
619
|
-
while ((ch = takeChar(scnr, closure))) {
|
|
653
|
+
while ((ch = takeChar(scnr, isInvalidIdentifier))) {
|
|
620
654
|
identifiers += ch;
|
|
621
655
|
}
|
|
622
656
|
return identifiers;
|
|
@@ -893,7 +927,7 @@ function fromEscapeSequence(match, codePoint4, codePoint6) {
|
|
|
893
927
|
}
|
|
894
928
|
function createParser(options = {}) {
|
|
895
929
|
const location = options.location !== false;
|
|
896
|
-
const { onError } = options;
|
|
930
|
+
const { onError, onWarn } = options;
|
|
897
931
|
function emitError(tokenzer, code, start, offset, ...args) {
|
|
898
932
|
const end = tokenzer.currentPosition();
|
|
899
933
|
end.offset += offset;
|
|
@@ -907,6 +941,15 @@ function createParser(options = {}) {
|
|
|
907
941
|
onError(err);
|
|
908
942
|
}
|
|
909
943
|
}
|
|
944
|
+
function emitWarn(tokenzer, code, start, offset, ...args) {
|
|
945
|
+
const end = tokenzer.currentPosition();
|
|
946
|
+
end.offset += offset;
|
|
947
|
+
end.column += offset;
|
|
948
|
+
if (onWarn) {
|
|
949
|
+
const loc = location ? createLocation(start, end) : null;
|
|
950
|
+
onWarn(createCompileWarn(code, loc, args));
|
|
951
|
+
}
|
|
952
|
+
}
|
|
910
953
|
function startNode(type, offset, loc) {
|
|
911
954
|
const node = { type };
|
|
912
955
|
if (location) {
|
|
@@ -943,11 +986,14 @@ function createParser(options = {}) {
|
|
|
943
986
|
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
944
987
|
return node;
|
|
945
988
|
}
|
|
946
|
-
function parseNamed(tokenizer, key) {
|
|
989
|
+
function parseNamed(tokenizer, key, modulo) {
|
|
947
990
|
const context = tokenizer.context();
|
|
948
991
|
const { lastOffset: offset, lastStartLoc: loc } = context; // get brace left loc
|
|
949
992
|
const node = startNode(4 /* NodeTypes.Named */, offset, loc);
|
|
950
993
|
node.key = key;
|
|
994
|
+
if (modulo === true) {
|
|
995
|
+
node.modulo = true;
|
|
996
|
+
}
|
|
951
997
|
tokenizer.nextToken(); // skip brach right
|
|
952
998
|
endNode(node, tokenizer.currentOffset(), tokenizer.currentPosition());
|
|
953
999
|
return node;
|
|
@@ -1067,6 +1113,7 @@ function createParser(options = {}) {
|
|
|
1067
1113
|
const node = startNode(2 /* NodeTypes.Message */, startOffset, startLoc);
|
|
1068
1114
|
node.items = [];
|
|
1069
1115
|
let nextToken = null;
|
|
1116
|
+
let modulo = null;
|
|
1070
1117
|
do {
|
|
1071
1118
|
const token = nextToken || tokenizer.nextToken();
|
|
1072
1119
|
nextToken = null;
|
|
@@ -1083,11 +1130,18 @@ function createParser(options = {}) {
|
|
|
1083
1130
|
}
|
|
1084
1131
|
node.items.push(parseList(tokenizer, token.value || ''));
|
|
1085
1132
|
break;
|
|
1133
|
+
case 4 /* TokenTypes.Modulo */:
|
|
1134
|
+
modulo = true;
|
|
1135
|
+
break;
|
|
1086
1136
|
case 5 /* TokenTypes.Named */:
|
|
1087
1137
|
if (token.value == null) {
|
|
1088
1138
|
emitError(tokenizer, CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1089
1139
|
}
|
|
1090
|
-
node.items.push(parseNamed(tokenizer, token.value || ''));
|
|
1140
|
+
node.items.push(parseNamed(tokenizer, token.value || '', !!modulo));
|
|
1141
|
+
if (modulo) {
|
|
1142
|
+
emitWarn(tokenizer, CompileWarnCodes.USE_MODULO_SYNTAX, context.lastStartLoc, 0, getTokenCaption(token));
|
|
1143
|
+
modulo = null;
|
|
1144
|
+
}
|
|
1091
1145
|
break;
|
|
1092
1146
|
case 7 /* TokenTypes.Literal */:
|
|
1093
1147
|
if (token.value == null) {
|
|
@@ -1568,4 +1622,4 @@ function baseCompile(source, options = {}) {
|
|
|
1568
1622
|
}
|
|
1569
1623
|
}
|
|
1570
1624
|
|
|
1571
|
-
export { CompileErrorCodes, ERROR_DOMAIN$2 as ERROR_DOMAIN, LOCATION_STUB, baseCompile, createCompileError, createLocation, createParser, createPosition, defaultOnError, detectHtmlTag, errorMessages };
|
|
1625
|
+
export { CompileErrorCodes, CompileWarnCodes, ERROR_DOMAIN$2 as ERROR_DOMAIN, LOCATION_STUB, baseCompile, createCompileError, createCompileWarn, createLocation, createParser, createPosition, defaultOnError, detectHtmlTag, errorMessages, warnMessages };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* message-compiler v9.
|
|
2
|
+
* message-compiler v9.12.1
|
|
3
3
|
* (c) 2024 kazuya kawaguchi
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
6
|
-
const LOCATION_STUB={start:{line:1,column:1,offset:0},end:{line:1,column:1,offset:0}};function createPosition(e,t,n){return{line:e,column:t,offset:n}}function createLocation(e,t,n){const r={start:e,end:t};return null!=n&&(r.source=n),r}const assign=Object.assign,isString=e=>"string"==typeof e;function join(e,t=""){return e.reduce(((e,n,r)=>0===r?e+n:e+t+n),"")}const CompileErrorCodes={EXPECTED_TOKEN:1,INVALID_TOKEN_IN_PLACEHOLDER:2,UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER:3,UNKNOWN_ESCAPE_SEQUENCE:4,INVALID_UNICODE_ESCAPE_SEQUENCE:5,UNBALANCED_CLOSING_BRACE:6,UNTERMINATED_CLOSING_BRACE:7,EMPTY_PLACEHOLDER:8,NOT_ALLOW_NEST_PLACEHOLDER:9,INVALID_LINKED_FORMAT:10,MUST_HAVE_MESSAGES_IN_PLURAL:11,UNEXPECTED_EMPTY_LINKED_MODIFIER:12,UNEXPECTED_EMPTY_LINKED_KEY:13,UNEXPECTED_LEXICAL_ANALYSIS:14,UNHANDLED_CODEGEN_NODE_TYPE:15,UNHANDLED_MINIFIER_NODE_TYPE:16,__EXTEND_POINT__:17},errorMessages={[CompileErrorCodes.EXPECTED_TOKEN]:"Expected token: '{0}'",[CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER]:"Invalid token in placeholder: '{0}'",[CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER]:"Unterminated single quote in placeholder",[CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE]:"Unknown escape sequence: \\{0}",[CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE]:"Invalid unicode escape sequence: {0}",[CompileErrorCodes.UNBALANCED_CLOSING_BRACE]:"Unbalanced closing brace",[CompileErrorCodes.UNTERMINATED_CLOSING_BRACE]:"Unterminated closing brace",[CompileErrorCodes.EMPTY_PLACEHOLDER]:"Empty placeholder",[CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER]:"Not allowed nest placeholder",[CompileErrorCodes.INVALID_LINKED_FORMAT]:"Invalid linked format",[CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]:"Plural must have messages",[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]:"Unexpected empty linked modifier",[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]:"Unexpected empty linked key",[CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]:"Unexpected lexical analysis in token: '{0}'",[CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE]:"unhandled codegen node type: '{0}'",[CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE]:"unhandled mimifier node type: '{0}'"};function createCompileError(e,t,n={}){const{domain:r,messages:o,args:s}=n,c=new SyntaxError(String(e));return c.code=e,t&&(c.location=t),c.domain=r,c}function defaultOnError(e){throw e}const RE_HTML_TAG=/<\/?[\w\s="/.':;#-\/]+>/,detectHtmlTag=e=>RE_HTML_TAG.test(e),CHAR_SP=" ",CHAR_CR="\r",CHAR_LF="\n",CHAR_LS=String.fromCharCode(8232),CHAR_PS=String.fromCharCode(8233);function createScanner(e){const t=e;let n=0,r=1,o=1,s=0;const c=e=>t[e]===CHAR_CR&&t[e+1]===CHAR_LF,i=e=>t[e]===CHAR_PS,a=e=>t[e]===CHAR_LS,u=e=>c(e)||(e=>t[e]===CHAR_LF)(e)||i(e)||a(e),l=e=>c(e)||i(e)||a(e)?CHAR_LF:t[e];function E(){return s=0,u(n)&&(r++,o=0),c(n)&&n++,n++,o++,t[n]}return{index:()=>n,line:()=>r,column:()=>o,peekOffset:()=>s,charAt:l,currentChar:()=>l(n),currentPeek:()=>l(n+s),next:E,peek:function(){return c(n+s)&&s++,s++,t[n+s]},reset:function(){n=0,r=1,o=1,s=0},resetPeek:function(e=0){s=e},skipToPeek:function(){const e=n+s;for(;e!==n;)E();s=0}}}const EOF=void 0,DOT=".",LITERAL_DELIMITER="'",ERROR_DOMAIN$1="tokenizer";function createTokenizer(e,t={}){const n=!1!==t.location,r=createScanner(e),o=()=>r.index(),s=()=>createPosition(r.line(),r.column(),r.index()),c=s(),i=o(),a={currentType:14,offset:i,startLoc:c,endLoc:c,lastType:14,lastOffset:i,lastStartLoc:c,lastEndLoc:c,braceNest:0,inLinked:!1,text:""},u=()=>a,{onError:l}=t;function E(e,t,r){e.endLoc=s(),e.currentType=t;const o={type:t};return n&&(o.loc=createLocation(e.startLoc,e.endLoc)),null!=r&&(o.value=r),o}const C=e=>E(e,14);function f(e,t){return e.currentChar()===t?(e.next(),t):(CompileErrorCodes.EXPECTED_TOKEN,s(),"")}function d(e){let t="";for(;e.currentPeek()===CHAR_SP||e.currentPeek()===CHAR_LF;)t+=e.currentPeek(),e.peek();return t}function p(e){const t=d(e);return e.skipToPeek(),t}function L(e){if(e===EOF)return!1;const t=e.charCodeAt(0);return t>=97&&t<=122||t>=65&&t<=90||95===t}function _(e,t){const{currentType:n}=t;if(2!==n)return!1;d(e);const r=function(e){if(e===EOF)return!1;const t=e.charCodeAt(0);return t>=48&&t<=57}("-"===e.currentPeek()?e.peek():e.currentPeek());return e.resetPeek(),r}function N(e){d(e);const t="|"===e.currentPeek();return e.resetPeek(),t}function A(e,t=!0){const n=(t=!1,r="",o=!1)=>{const s=e.currentPeek();return"{"===s?"%"!==r&&t:"@"!==s&&s?"%"===s?(e.peek(),n(t,"%",!0)):"|"===s?!("%"!==r&&!o)||!(r===CHAR_SP||r===CHAR_LF):s===CHAR_SP?(e.peek(),n(!0,CHAR_SP,o)):s!==CHAR_LF||(e.peek(),n(!0,CHAR_LF,o)):"%"===r||t},r=n();return t&&e.resetPeek(),r}function T(e,t){const n=e.currentChar();return n===EOF?EOF:t(n)?(e.next(),n):null}function m(e){return T(e,(e=>{const t=e.charCodeAt(0);return t>=97&&t<=122||t>=65&&t<=90||t>=48&&t<=57||95===t||36===t}))}function k(e){return T(e,(e=>{const t=e.charCodeAt(0);return t>=48&&t<=57}))}function I(e){return T(e,(e=>{const t=e.charCodeAt(0);return t>=48&&t<=57||t>=65&&t<=70||t>=97&&t<=102}))}function S(e){let t="",n="";for(;t=k(e);)n+=t;return n}function P(e){let t="";for(;;){const n=e.currentChar();if("{"===n||"}"===n||"@"===n||"|"===n||!n)break;if("%"===n){if(!A(e))break;t+=n,e.next()}else if(n===CHAR_SP||n===CHAR_LF)if(A(e))t+=n,e.next();else{if(N(e))break;t+=n,e.next()}else t+=n,e.next()}return t}function h(e){const t=e.currentChar();switch(t){case"\\":case"'":return e.next(),`\\${t}`;case"u":return O(e,t,4);case"U":return O(e,t,6);default:return CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE,s(),""}}function O(e,t,n){f(e,t);let r="";for(let o=0;o<n;o++){const t=I(e);if(!t){CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE,s(),e.currentChar();break}r+=t}return`\\${t}${r}`}function y(e){p(e);const t=f(e,"|");return p(e),t}function D(e,t){let n=null;switch(e.currentChar()){case"{":return t.braceNest>=1&&(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER,s()),e.next(),n=E(t,2,"{"),p(e),t.braceNest++,n;case"}":return t.braceNest>0&&2===t.currentType&&(CompileErrorCodes.EMPTY_PLACEHOLDER,s()),e.next(),n=E(t,3,"}"),t.braceNest--,t.braceNest>0&&p(e),t.inLinked&&0===t.braceNest&&(t.inLinked=!1),n;case"@":return t.braceNest>0&&(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s()),n=R(e,t)||C(t),t.braceNest=0,n;default:{let r=!0,o=!0,c=!0;if(N(e))return t.braceNest>0&&(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s()),n=E(t,1,y(e)),t.braceNest=0,t.inLinked=!1,n;if(t.braceNest>0&&(5===t.currentType||6===t.currentType||7===t.currentType))return CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s(),t.braceNest=0,g(e,t);if(r=function(e,t){const{currentType:n}=t;if(2!==n)return!1;d(e);const r=L(e.currentPeek());return e.resetPeek(),r}(e,t))return n=E(t,5,function(e){p(e);let t="",n="";for(;t=m(e);)n+=t;return e.currentChar()===EOF&&(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s()),n}(e)),p(e),n;if(o=_(e,t))return n=E(t,6,function(e){p(e);let t="";return"-"===e.currentChar()?(e.next(),t+=`-${S(e)}`):t+=S(e),e.currentChar()===EOF&&(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s()),t}(e)),p(e),n;if(c=function(e,t){const{currentType:n}=t;if(2!==n)return!1;d(e);const r=e.currentPeek()===LITERAL_DELIMITER;return e.resetPeek(),r}(e,t))return n=E(t,7,function(e){p(e),f(e,"'");let t="",n="";const r=e=>e!==LITERAL_DELIMITER&&e!==CHAR_LF;for(;t=T(e,r);)n+="\\"===t?h(e):t;const o=e.currentChar();return o===CHAR_LF||o===EOF?(CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER,s(),o===CHAR_LF&&(e.next(),f(e,"'")),n):(f(e,"'"),n)}(e)),p(e),n;if(!r&&!o&&!c)return n=E(t,13,function(e){p(e);let t="",n="";const r=e=>"{"!==e&&"}"!==e&&e!==CHAR_SP&&e!==CHAR_LF;for(;t=T(e,r);)n+=t;return n}(e)),CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER,s(),n.value,p(e),n;break}}return n}function R(e,t){const{currentType:n}=t;let r=null;const o=e.currentChar();switch(8!==n&&9!==n&&12!==n&&10!==n||o!==CHAR_LF&&o!==CHAR_SP||(CompileErrorCodes.INVALID_LINKED_FORMAT,s()),o){case"@":return e.next(),r=E(t,8,"@"),t.inLinked=!0,r;case".":return p(e),e.next(),E(t,9,".");case":":return p(e),e.next(),E(t,10,":");default:return N(e)?(r=E(t,1,y(e)),t.braceNest=0,t.inLinked=!1,r):function(e,t){const{currentType:n}=t;if(8!==n)return!1;d(e);const r="."===e.currentPeek();return e.resetPeek(),r}(e,t)||function(e,t){const{currentType:n}=t;if(8!==n&&12!==n)return!1;d(e);const r=":"===e.currentPeek();return e.resetPeek(),r}(e,t)?(p(e),R(e,t)):function(e,t){const{currentType:n}=t;if(9!==n)return!1;d(e);const r=L(e.currentPeek());return e.resetPeek(),r}(e,t)?(p(e),E(t,12,function(e){let t="",n="";for(;t=m(e);)n+=t;return n}(e))):function(e,t){const{currentType:n}=t;if(10!==n)return!1;const r=()=>{const t=e.currentPeek();return"{"===t?L(e.peek()):!("@"===t||"%"===t||"|"===t||":"===t||"."===t||t===CHAR_SP||!t)&&(t===CHAR_LF?(e.peek(),r()):L(t))},o=r();return e.resetPeek(),o}(e,t)?(p(e),"{"===o?D(e,t)||r:E(t,11,function(e){const t=(n=!1,r)=>{const o=e.currentChar();return"{"!==o&&"%"!==o&&"@"!==o&&"|"!==o&&"("!==o&&")"!==o&&o?o===CHAR_SP?r:o===CHAR_LF||o===DOT?(r+=o,e.next(),t(n,r)):(r+=o,e.next(),t(!0,r)):r};return t(!1,"")}(e))):(8===n&&(CompileErrorCodes.INVALID_LINKED_FORMAT,s()),t.braceNest=0,t.inLinked=!1,g(e,t))}}function g(e,t){let n={type:14};if(t.braceNest>0)return D(e,t)||C(t);if(t.inLinked)return R(e,t)||C(t);switch(e.currentChar()){case"{":return D(e,t)||C(t);case"}":return CompileErrorCodes.UNBALANCED_CLOSING_BRACE,s(),e.next(),E(t,3,"}");case"@":return R(e,t)||C(t);default:{if(N(e))return n=E(t,1,y(e)),t.braceNest=0,t.inLinked=!1,n;const{isModulo:r,hasSpace:o}=function(e){const t=d(e),n="%"===e.currentPeek()&&"{"===e.peek();return e.resetPeek(),{isModulo:n,hasSpace:t.length>0}}(e);if(r)return o?E(t,0,P(e)):E(t,4,function(e){p(e);const t=e.currentChar();return"%"!==t&&(CompileErrorCodes.EXPECTED_TOKEN,s()),e.next(),"%"}(e));if(A(e))return E(t,0,P(e));break}}return n}return{nextToken:function(){const{currentType:e,offset:t,startLoc:n,endLoc:c}=a;return a.lastType=e,a.lastOffset=t,a.lastStartLoc=n,a.lastEndLoc=c,a.offset=o(),a.startLoc=s(),r.currentChar()===EOF?E(a,14):g(r,a)},currentOffset:o,currentPosition:s,context:u}}const ERROR_DOMAIN="parser",KNOWN_ESCAPES=/(?:\\\\|\\'|\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{6}))/g;function fromEscapeSequence(e,t,n){switch(e){case"\\\\":return"\\";case"\\'":return"'";default:{const e=parseInt(t||n,16);return e<=55295||e>=57344?String.fromCodePoint(e):"�"}}}function createParser(e={}){const t=!1!==e.location,{onError:n}=e;function r(e,n,r){const o={type:e};return t&&(o.start=n,o.end=n,o.loc={start:r,end:r}),o}function o(e,n,r,o){o&&(e.type=o),t&&(e.end=n,e.loc&&(e.loc.end=r))}function s(e,t){const n=e.context(),s=r(3,n.offset,n.startLoc);return s.value=t,o(s,e.currentOffset(),e.currentPosition()),s}function c(e,t){const n=e.context(),{lastOffset:s,lastStartLoc:c}=n,i=r(5,s,c);return i.index=parseInt(t,10),e.nextToken(),o(i,e.currentOffset(),e.currentPosition()),i}function i(e,t){const n=e.context(),{lastOffset:s,lastStartLoc:c}=n,i=r(4,s,c);return i.key=t,e.nextToken(),o(i,e.currentOffset(),e.currentPosition()),i}function a(e,t){const n=e.context(),{lastOffset:s,lastStartLoc:c}=n,i=r(9,s,c);return i.value=t.replace(KNOWN_ESCAPES,fromEscapeSequence),e.nextToken(),o(i,e.currentOffset(),e.currentPosition()),i}function u(e){const t=e.context(),n=r(6,t.offset,t.startLoc);let s=e.nextToken();if(9===s.type){const t=function(e){const t=e.nextToken(),n=e.context(),{lastOffset:s,lastStartLoc:c}=n,i=r(8,s,c);return 12!==t.type?(CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER,n.lastStartLoc,i.value="",o(i,s,c),{nextConsumeToken:t,node:i}):(null==t.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,n.lastStartLoc,getTokenCaption(t)),i.value=t.value||"",o(i,e.currentOffset(),e.currentPosition()),{node:i})}(e);n.modifier=t.node,s=t.nextConsumeToken||e.nextToken()}switch(10!==s.type&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(s)),s=e.nextToken(),2===s.type&&(s=e.nextToken()),s.type){case 11:null==s.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(s)),n.key=function(e,t){const n=e.context(),s=r(7,n.offset,n.startLoc);return s.value=t,o(s,e.currentOffset(),e.currentPosition()),s}(e,s.value||"");break;case 5:null==s.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(s)),n.key=i(e,s.value||"");break;case 6:null==s.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(s)),n.key=c(e,s.value||"");break;case 7:null==s.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(s)),n.key=a(e,s.value||"");break;default:{CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY,t.lastStartLoc;const c=e.context(),i=r(7,c.offset,c.startLoc);return i.value="",o(i,c.offset,c.startLoc),n.key=i,o(n,c.offset,c.startLoc),{nextConsumeToken:s,node:n}}}return o(n,e.currentOffset(),e.currentPosition()),{node:n}}function l(e){const t=e.context(),n=r(2,1===t.currentType?e.currentOffset():t.offset,1===t.currentType?t.endLoc:t.startLoc);n.items=[];let l=null;do{const r=l||e.nextToken();switch(l=null,r.type){case 0:null==r.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(r)),n.items.push(s(e,r.value||""));break;case 6:null==r.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(r)),n.items.push(c(e,r.value||""));break;case 5:null==r.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(r)),n.items.push(i(e,r.value||""));break;case 7:null==r.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(r)),n.items.push(a(e,r.value||""));break;case 8:{const t=u(e);n.items.push(t.node),l=t.nextConsumeToken||null;break}}}while(14!==t.currentType&&1!==t.currentType);return o(n,1===t.currentType?t.lastOffset:e.currentOffset(),1===t.currentType?t.lastEndLoc:e.currentPosition()),n}function E(e){const t=e.context(),{offset:n,startLoc:s}=t,c=l(e);return 14===t.currentType?c:function(e,t,n,s){const c=e.context();let i=0===s.items.length;const a=r(1,t,n);a.cases=[],a.cases.push(s);do{const t=l(e);i||(i=0===t.items.length),a.cases.push(t)}while(14!==c.currentType);return i&&CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL,o(a,e.currentOffset(),e.currentPosition()),a}(e,n,s,c)}return{parse:function(n){const s=createTokenizer(n,assign({},e)),c=s.context(),i=r(0,c.offset,c.startLoc);return t&&i.loc&&(i.loc.source=n),i.body=E(s),e.onCacheKey&&(i.cacheKey=e.onCacheKey(n)),14!==c.currentType&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,c.lastStartLoc,n[c.offset]),o(i,s.currentOffset(),s.currentPosition()),i}}}function getTokenCaption(e){if(14===e.type)return"EOF";const t=(e.value||"").replace(/\r?\n/gu,"\\n");return t.length>10?t.slice(0,9)+"…":t}function createTransformer(e,t={}){const n={ast:e,helpers:new Set};return{context:()=>n,helper:e=>(n.helpers.add(e),e)}}function traverseNodes(e,t){for(let n=0;n<e.length;n++)traverseNode(e[n],t)}function traverseNode(e,t){switch(e.type){case 1:traverseNodes(e.cases,t),t.helper("plural");break;case 2:traverseNodes(e.items,t);break;case 6:traverseNode(e.key,t),t.helper("linked"),t.helper("type");break;case 5:t.helper("interpolate"),t.helper("list");break;case 4:t.helper("interpolate"),t.helper("named")}}function transform(e,t={}){const n=createTransformer(e);n.helper("normalize"),e.body&&traverseNode(e.body,n);const r=n.context();e.helpers=Array.from(r.helpers)}function optimize(e){const t=e.body;return 2===t.type?optimizeMessageNode(t):t.cases.forEach((e=>optimizeMessageNode(e))),e}function optimizeMessageNode(e){if(1===e.items.length){const t=e.items[0];3!==t.type&&9!==t.type||(e.static=t.value,delete t.value)}else{const t=[];for(let n=0;n<e.items.length;n++){const r=e.items[n];if(3!==r.type&&9!==r.type)break;if(null==r.value)break;t.push(r.value)}if(t.length===e.items.length){e.static=join(t);for(let t=0;t<e.items.length;t++){const n=e.items[t];3!==n.type&&9!==n.type||delete n.value}}}}function minify(e){switch(e.t=e.type,e.type){case 0:{const t=e;minify(t.body),t.b=t.body,delete t.body;break}case 1:{const t=e,n=t.cases;for(let e=0;e<n.length;e++)minify(n[e]);t.c=n,delete t.cases;break}case 2:{const t=e,n=t.items;for(let e=0;e<n.length;e++)minify(n[e]);t.i=n,delete t.items,t.static&&(t.s=t.static,delete t.static);break}case 3:case 9:case 8:case 7:{const t=e;t.value&&(t.v=t.value,delete t.value);break}case 6:{const t=e;minify(t.key),t.k=t.key,delete t.key,t.modifier&&(minify(t.modifier),t.m=t.modifier,delete t.modifier);break}case 5:{const t=e;t.i=t.index,delete t.index;break}case 4:{const t=e;t.k=t.key,delete t.key;break}}delete e.type}function createCodeGenerator(e,t){const{sourceMap:n,filename:r,breakLineCode:o,needIndent:s}=t,c=!1!==t.location,i={filename:r,code:"",column:1,line:1,offset:0,map:void 0,breakLineCode:o,needIndent:s,indentLevel:0};c&&e.loc&&(i.source=e.loc.source);function a(e,t){i.code+=e}function u(e,t=!0){const n=t?o:"";a(s?n+" ".repeat(e):n)}return{context:()=>i,push:a,indent:function(e=!0){const t=++i.indentLevel;e&&u(t)},deindent:function(e=!0){const t=--i.indentLevel;e&&u(t)},newline:function(){u(i.indentLevel)},helper:e=>`_${e}`,needIndent:()=>i.needIndent}}function generateLinkedNode(e,t){const{helper:n}=e;e.push(`${n("linked")}(`),generateNode(e,t.key),t.modifier?(e.push(", "),generateNode(e,t.modifier),e.push(", _type")):e.push(", undefined, _type"),e.push(")")}function generateMessageNode(e,t){const{helper:n,needIndent:r}=e;e.push(`${n("normalize")}([`),e.indent(r());const o=t.items.length;for(let s=0;s<o&&(generateNode(e,t.items[s]),s!==o-1);s++)e.push(", ");e.deindent(r()),e.push("])")}function generatePluralNode(e,t){const{helper:n,needIndent:r}=e;if(t.cases.length>1){e.push(`${n("plural")}([`),e.indent(r());const o=t.cases.length;for(let n=0;n<o&&(generateNode(e,t.cases[n]),n!==o-1);n++)e.push(", ");e.deindent(r()),e.push("])")}}function generateResource(e,t){t.body?generateNode(e,t.body):e.push("null")}function generateNode(e,t){const{helper:n}=e;switch(t.type){case 0:generateResource(e,t);break;case 1:generatePluralNode(e,t);break;case 2:generateMessageNode(e,t);break;case 6:generateLinkedNode(e,t);break;case 8:case 7:case 9:case 3:e.push(JSON.stringify(t.value),t);break;case 5:e.push(`${n("interpolate")}(${n("list")}(${t.index}))`,t);break;case 4:e.push(`${n("interpolate")}(${n("named")}(${JSON.stringify(t.key)}))`,t)}}const generate=(e,t={})=>{const n=isString(t.mode)?t.mode:"normal",r=isString(t.filename)?t.filename:"message.intl",o=!!t.sourceMap,s=null!=t.breakLineCode?t.breakLineCode:"arrow"===n?";":"\n",c=t.needIndent?t.needIndent:"arrow"!==n,i=e.helpers||[],a=createCodeGenerator(e,{mode:n,filename:r,sourceMap:o,breakLineCode:s,needIndent:c});a.push("normal"===n?"function __msg__ (ctx) {":"(ctx) => {"),a.indent(c),i.length>0&&(a.push(`const { ${join(i.map((e=>`${e}: _${e}`)),", ")} } = ctx`),a.newline()),a.push("return "),generateNode(a,e),a.deindent(c),a.push("}"),delete e.helpers;const{code:u,map:l}=a.context();return{ast:e,code:u,map:l?l.toJSON():void 0}};function baseCompile(e,t={}){const n=assign({},t),r=!!n.jit,o=!!n.minify,s=null==n.optimize||n.optimize,c=createParser(n).parse(e);return r?(s&&optimize(c),o&&minify(c),{ast:c,code:""}):(transform(c,n),generate(c,n))}export{CompileErrorCodes,ERROR_DOMAIN,LOCATION_STUB,baseCompile,createCompileError,createLocation,createParser,createPosition,defaultOnError,detectHtmlTag,errorMessages};
|
|
6
|
+
const LOCATION_STUB={start:{line:1,column:1,offset:0},end:{line:1,column:1,offset:0}};function createPosition(e,t,n){return{line:e,column:t,offset:n}}function createLocation(e,t,n){const r={start:e,end:t};return null!=n&&(r.source=n),r}const assign=Object.assign,isString=e=>"string"==typeof e;function join(e,t=""){return e.reduce(((e,n,r)=>0===r?e+n:e+t+n),"")}const CompileWarnCodes={USE_MODULO_SYNTAX:1,__EXTEND_POINT__:2},warnMessages={[CompileWarnCodes.USE_MODULO_SYNTAX]:"Use modulo before '{{0}}'."};function createCompileWarn(e,t,...n){const r={message:String(e),code:e};return t&&(r.location=t),r}const CompileErrorCodes={EXPECTED_TOKEN:1,INVALID_TOKEN_IN_PLACEHOLDER:2,UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER:3,UNKNOWN_ESCAPE_SEQUENCE:4,INVALID_UNICODE_ESCAPE_SEQUENCE:5,UNBALANCED_CLOSING_BRACE:6,UNTERMINATED_CLOSING_BRACE:7,EMPTY_PLACEHOLDER:8,NOT_ALLOW_NEST_PLACEHOLDER:9,INVALID_LINKED_FORMAT:10,MUST_HAVE_MESSAGES_IN_PLURAL:11,UNEXPECTED_EMPTY_LINKED_MODIFIER:12,UNEXPECTED_EMPTY_LINKED_KEY:13,UNEXPECTED_LEXICAL_ANALYSIS:14,UNHANDLED_CODEGEN_NODE_TYPE:15,UNHANDLED_MINIFIER_NODE_TYPE:16,__EXTEND_POINT__:17},errorMessages={[CompileErrorCodes.EXPECTED_TOKEN]:"Expected token: '{0}'",[CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER]:"Invalid token in placeholder: '{0}'",[CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER]:"Unterminated single quote in placeholder",[CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE]:"Unknown escape sequence: \\{0}",[CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE]:"Invalid unicode escape sequence: {0}",[CompileErrorCodes.UNBALANCED_CLOSING_BRACE]:"Unbalanced closing brace",[CompileErrorCodes.UNTERMINATED_CLOSING_BRACE]:"Unterminated closing brace",[CompileErrorCodes.EMPTY_PLACEHOLDER]:"Empty placeholder",[CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER]:"Not allowed nest placeholder",[CompileErrorCodes.INVALID_LINKED_FORMAT]:"Invalid linked format",[CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]:"Plural must have messages",[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]:"Unexpected empty linked modifier",[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]:"Unexpected empty linked key",[CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]:"Unexpected lexical analysis in token: '{0}'",[CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE]:"unhandled codegen node type: '{0}'",[CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE]:"unhandled mimifier node type: '{0}'"};function createCompileError(e,t,n={}){const{domain:r,messages:o,args:s}=n,c=new SyntaxError(String(e));return c.code=e,t&&(c.location=t),c.domain=r,c}function defaultOnError(e){throw e}const RE_HTML_TAG=/<\/?[\w\s="/.':;#-\/]+>/,detectHtmlTag=e=>RE_HTML_TAG.test(e),CHAR_SP=" ",CHAR_CR="\r",CHAR_LF="\n",CHAR_LS=String.fromCharCode(8232),CHAR_PS=String.fromCharCode(8233);function createScanner(e){const t=e;let n=0,r=1,o=1,s=0;const c=e=>t[e]===CHAR_CR&&t[e+1]===CHAR_LF,a=e=>t[e]===CHAR_PS,i=e=>t[e]===CHAR_LS,u=e=>c(e)||(e=>t[e]===CHAR_LF)(e)||a(e)||i(e),l=e=>c(e)||a(e)||i(e)?CHAR_LF:t[e];function E(){return s=0,u(n)&&(r++,o=0),c(n)&&n++,n++,o++,t[n]}return{index:()=>n,line:()=>r,column:()=>o,peekOffset:()=>s,charAt:l,currentChar:()=>l(n),currentPeek:()=>l(n+s),next:E,peek:function(){return c(n+s)&&s++,s++,t[n+s]},reset:function(){n=0,r=1,o=1,s=0},resetPeek:function(e=0){s=e},skipToPeek:function(){const e=n+s;for(;e!==n;)E();s=0}}}const EOF=void 0,DOT=".",LITERAL_DELIMITER="'",ERROR_DOMAIN$1="tokenizer";function createTokenizer(e,t={}){const n=!1!==t.location,r=createScanner(e),o=()=>r.index(),s=()=>createPosition(r.line(),r.column(),r.index()),c=s(),a=o(),i={currentType:14,offset:a,startLoc:c,endLoc:c,lastType:14,lastOffset:a,lastStartLoc:c,lastEndLoc:c,braceNest:0,inLinked:!1,text:""},u=()=>i,{onError:l}=t;function E(e,t,r){e.endLoc=s(),e.currentType=t;const o={type:t};return n&&(o.loc=createLocation(e.startLoc,e.endLoc)),null!=r&&(o.value=r),o}const C=e=>E(e,14);function f(e,t){return e.currentChar()===t?(e.next(),t):(CompileErrorCodes.EXPECTED_TOKEN,s(),"")}function d(e){let t="";for(;e.currentPeek()===CHAR_SP||e.currentPeek()===CHAR_LF;)t+=e.currentPeek(),e.peek();return t}function p(e){const t=d(e);return e.skipToPeek(),t}function _(e){if(e===EOF)return!1;const t=e.charCodeAt(0);return t>=97&&t<=122||t>=65&&t<=90||95===t}function L(e,t){const{currentType:n}=t;if(2!==n)return!1;d(e);const r=function(e){if(e===EOF)return!1;const t=e.charCodeAt(0);return t>=48&&t<=57}("-"===e.currentPeek()?e.peek():e.currentPeek());return e.resetPeek(),r}function N(e){d(e);const t="|"===e.currentPeek();return e.resetPeek(),t}function A(e,t=!0){const n=(t=!1,r="",o=!1)=>{const s=e.currentPeek();return"{"===s?"%"!==r&&t:"@"!==s&&s?"%"===s?(e.peek(),n(t,"%",!0)):"|"===s?!("%"!==r&&!o)||!(r===CHAR_SP||r===CHAR_LF):s===CHAR_SP?(e.peek(),n(!0,CHAR_SP,o)):s!==CHAR_LF||(e.peek(),n(!0,CHAR_LF,o)):"%"===r||t},r=n();return t&&e.resetPeek(),r}function T(e,t){const n=e.currentChar();return n===EOF?EOF:t(n)?(e.next(),n):null}function m(e){const t=e.charCodeAt(0);return t>=97&&t<=122||t>=65&&t<=90||t>=48&&t<=57||95===t||36===t}function k(e){return T(e,m)}function I(e){const t=e.charCodeAt(0);return t>=97&&t<=122||t>=65&&t<=90||t>=48&&t<=57||95===t||36===t||45===t}function S(e){return T(e,I)}function P(e){const t=e.charCodeAt(0);return t>=48&&t<=57}function h(e){return T(e,P)}function O(e){const t=e.charCodeAt(0);return t>=48&&t<=57||t>=65&&t<=70||t>=97&&t<=102}function D(e){return T(e,O)}function y(e){let t="",n="";for(;t=h(e);)n+=t;return n}function R(e){let t="";for(;;){const n=e.currentChar();if("{"===n||"}"===n||"@"===n||"|"===n||!n)break;if("%"===n){if(!A(e))break;t+=n,e.next()}else if(n===CHAR_SP||n===CHAR_LF)if(A(e))t+=n,e.next();else{if(N(e))break;t+=n,e.next()}else t+=n,e.next()}return t}function g(e){return e!==LITERAL_DELIMITER&&e!==CHAR_LF}function U(e){const t=e.currentChar();switch(t){case"\\":case"'":return e.next(),`\\${t}`;case"u":return b(e,t,4);case"U":return b(e,t,6);default:return CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE,s(),""}}function b(e,t,n){f(e,t);let r="";for(let o=0;o<n;o++){const t=D(e);if(!t){CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE,s(),e.currentChar();break}r+=t}return`\\${t}${r}`}function x(e){return"{"!==e&&"}"!==e&&e!==CHAR_SP&&e!==CHAR_LF}function M(e){p(e);const t=f(e,"|");return p(e),t}function v(e,t){let n=null;switch(e.currentChar()){case"{":return t.braceNest>=1&&(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER,s()),e.next(),n=E(t,2,"{"),p(e),t.braceNest++,n;case"}":return t.braceNest>0&&2===t.currentType&&(CompileErrorCodes.EMPTY_PLACEHOLDER,s()),e.next(),n=E(t,3,"}"),t.braceNest--,t.braceNest>0&&p(e),t.inLinked&&0===t.braceNest&&(t.inLinked=!1),n;case"@":return t.braceNest>0&&(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s()),n=H(e,t)||C(t),t.braceNest=0,n;default:{let r=!0,o=!0,c=!0;if(N(e))return t.braceNest>0&&(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s()),n=E(t,1,M(e)),t.braceNest=0,t.inLinked=!1,n;if(t.braceNest>0&&(5===t.currentType||6===t.currentType||7===t.currentType))return CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s(),t.braceNest=0,X(e,t);if(r=function(e,t){const{currentType:n}=t;if(2!==n)return!1;d(e);const r=_(e.currentPeek());return e.resetPeek(),r}(e,t))return n=E(t,5,function(e){p(e);let t="",n="";for(;t=S(e);)n+=t;return e.currentChar()===EOF&&(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s()),n}(e)),p(e),n;if(o=L(e,t))return n=E(t,6,function(e){p(e);let t="";return"-"===e.currentChar()?(e.next(),t+=`-${y(e)}`):t+=y(e),e.currentChar()===EOF&&(CompileErrorCodes.UNTERMINATED_CLOSING_BRACE,s()),t}(e)),p(e),n;if(c=function(e,t){const{currentType:n}=t;if(2!==n)return!1;d(e);const r=e.currentPeek()===LITERAL_DELIMITER;return e.resetPeek(),r}(e,t))return n=E(t,7,function(e){p(e),f(e,"'");let t="",n="";for(;t=T(e,g);)n+="\\"===t?U(e):t;const r=e.currentChar();return r===CHAR_LF||r===EOF?(CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER,s(),r===CHAR_LF&&(e.next(),f(e,"'")),n):(f(e,"'"),n)}(e)),p(e),n;if(!r&&!o&&!c)return n=E(t,13,function(e){p(e);let t="",n="";for(;t=T(e,x);)n+=t;return n}(e)),CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER,s(),n.value,p(e),n;break}}return n}function H(e,t){const{currentType:n}=t;let r=null;const o=e.currentChar();switch(8!==n&&9!==n&&12!==n&&10!==n||o!==CHAR_LF&&o!==CHAR_SP||(CompileErrorCodes.INVALID_LINKED_FORMAT,s()),o){case"@":return e.next(),r=E(t,8,"@"),t.inLinked=!0,r;case".":return p(e),e.next(),E(t,9,".");case":":return p(e),e.next(),E(t,10,":");default:return N(e)?(r=E(t,1,M(e)),t.braceNest=0,t.inLinked=!1,r):function(e,t){const{currentType:n}=t;if(8!==n)return!1;d(e);const r="."===e.currentPeek();return e.resetPeek(),r}(e,t)||function(e,t){const{currentType:n}=t;if(8!==n&&12!==n)return!1;d(e);const r=":"===e.currentPeek();return e.resetPeek(),r}(e,t)?(p(e),H(e,t)):function(e,t){const{currentType:n}=t;if(9!==n)return!1;d(e);const r=_(e.currentPeek());return e.resetPeek(),r}(e,t)?(p(e),E(t,12,function(e){let t="",n="";for(;t=k(e);)n+=t;return n}(e))):function(e,t){const{currentType:n}=t;if(10!==n)return!1;const r=()=>{const t=e.currentPeek();return"{"===t?_(e.peek()):!("@"===t||"%"===t||"|"===t||":"===t||"."===t||t===CHAR_SP||!t)&&(t===CHAR_LF?(e.peek(),r()):_(t))},o=r();return e.resetPeek(),o}(e,t)?(p(e),"{"===o?v(e,t)||r:E(t,11,function(e){const t=(n=!1,r)=>{const o=e.currentChar();return"{"!==o&&"%"!==o&&"@"!==o&&"|"!==o&&"("!==o&&")"!==o&&o?o===CHAR_SP?r:o===CHAR_LF||o===DOT?(r+=o,e.next(),t(n,r)):(r+=o,e.next(),t(!0,r)):r};return t(!1,"")}(e))):(8===n&&(CompileErrorCodes.INVALID_LINKED_FORMAT,s()),t.braceNest=0,t.inLinked=!1,X(e,t))}}function X(e,t){let n={type:14};if(t.braceNest>0)return v(e,t)||C(t);if(t.inLinked)return H(e,t)||C(t);switch(e.currentChar()){case"{":return v(e,t)||C(t);case"}":return CompileErrorCodes.UNBALANCED_CLOSING_BRACE,s(),e.next(),E(t,3,"}");case"@":return H(e,t)||C(t);default:{if(N(e))return n=E(t,1,M(e)),t.braceNest=0,t.inLinked=!1,n;const{isModulo:r,hasSpace:o}=function(e){const t=d(e),n="%"===e.currentPeek()&&"{"===e.peek();return e.resetPeek(),{isModulo:n,hasSpace:t.length>0}}(e);if(r)return o?E(t,0,R(e)):E(t,4,function(e){p(e);const t=e.currentChar();return"%"!==t&&(CompileErrorCodes.EXPECTED_TOKEN,s()),e.next(),"%"}(e));if(A(e))return E(t,0,R(e));break}}return n}return{nextToken:function(){const{currentType:e,offset:t,startLoc:n,endLoc:c}=i;return i.lastType=e,i.lastOffset=t,i.lastStartLoc=n,i.lastEndLoc=c,i.offset=o(),i.startLoc=s(),r.currentChar()===EOF?E(i,14):X(r,i)},currentOffset:o,currentPosition:s,context:u}}const ERROR_DOMAIN="parser",KNOWN_ESCAPES=/(?:\\\\|\\'|\\u([0-9a-fA-F]{4})|\\U([0-9a-fA-F]{6}))/g;function fromEscapeSequence(e,t,n){switch(e){case"\\\\":return"\\";case"\\'":return"'";default:{const e=parseInt(t||n,16);return e<=55295||e>=57344?String.fromCodePoint(e):"�"}}}function createParser(e={}){const t=!1!==e.location,{onError:n,onWarn:r}=e;function o(e,n,r){const o={type:e};return t&&(o.start=n,o.end=n,o.loc={start:r,end:r}),o}function s(e,n,r,o){o&&(e.type=o),t&&(e.end=n,e.loc&&(e.loc.end=r))}function c(e,t){const n=e.context(),r=o(3,n.offset,n.startLoc);return r.value=t,s(r,e.currentOffset(),e.currentPosition()),r}function a(e,t){const n=e.context(),{lastOffset:r,lastStartLoc:c}=n,a=o(5,r,c);return a.index=parseInt(t,10),e.nextToken(),s(a,e.currentOffset(),e.currentPosition()),a}function i(e,t,n){const r=e.context(),{lastOffset:c,lastStartLoc:a}=r,i=o(4,c,a);return i.key=t,!0===n&&(i.modulo=!0),e.nextToken(),s(i,e.currentOffset(),e.currentPosition()),i}function u(e,t){const n=e.context(),{lastOffset:r,lastStartLoc:c}=n,a=o(9,r,c);return a.value=t.replace(KNOWN_ESCAPES,fromEscapeSequence),e.nextToken(),s(a,e.currentOffset(),e.currentPosition()),a}function l(e){const t=e.context(),n=o(6,t.offset,t.startLoc);let r=e.nextToken();if(9===r.type){const t=function(e){const t=e.nextToken(),n=e.context(),{lastOffset:r,lastStartLoc:c}=n,a=o(8,r,c);return 12!==t.type?(CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER,n.lastStartLoc,a.value="",s(a,r,c),{nextConsumeToken:t,node:a}):(null==t.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,n.lastStartLoc,getTokenCaption(t)),a.value=t.value||"",s(a,e.currentOffset(),e.currentPosition()),{node:a})}(e);n.modifier=t.node,r=t.nextConsumeToken||e.nextToken()}switch(10!==r.type&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(r)),r=e.nextToken(),2===r.type&&(r=e.nextToken()),r.type){case 11:null==r.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(r)),n.key=function(e,t){const n=e.context(),r=o(7,n.offset,n.startLoc);return r.value=t,s(r,e.currentOffset(),e.currentPosition()),r}(e,r.value||"");break;case 5:null==r.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(r)),n.key=i(e,r.value||"");break;case 6:null==r.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(r)),n.key=a(e,r.value||"");break;case 7:null==r.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(r)),n.key=u(e,r.value||"");break;default:{CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY,t.lastStartLoc;const c=e.context(),a=o(7,c.offset,c.startLoc);return a.value="",s(a,c.offset,c.startLoc),n.key=a,s(n,c.offset,c.startLoc),{nextConsumeToken:r,node:n}}}return s(n,e.currentOffset(),e.currentPosition()),{node:n}}function E(e){const t=e.context(),n=o(2,1===t.currentType?e.currentOffset():t.offset,1===t.currentType?t.endLoc:t.startLoc);n.items=[];let r=null,E=null;do{const o=r||e.nextToken();switch(r=null,o.type){case 0:null==o.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(o)),n.items.push(c(e,o.value||""));break;case 6:null==o.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(o)),n.items.push(a(e,o.value||""));break;case 4:E=!0;break;case 5:null==o.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(o)),n.items.push(i(e,o.value||"",!!E)),E&&(CompileWarnCodes.USE_MODULO_SYNTAX,t.lastStartLoc,getTokenCaption(o),E=null);break;case 7:null==o.value&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,t.lastStartLoc,getTokenCaption(o)),n.items.push(u(e,o.value||""));break;case 8:{const t=l(e);n.items.push(t.node),r=t.nextConsumeToken||null;break}}}while(14!==t.currentType&&1!==t.currentType);return s(n,1===t.currentType?t.lastOffset:e.currentOffset(),1===t.currentType?t.lastEndLoc:e.currentPosition()),n}function C(e){const t=e.context(),{offset:n,startLoc:r}=t,c=E(e);return 14===t.currentType?c:function(e,t,n,r){const c=e.context();let a=0===r.items.length;const i=o(1,t,n);i.cases=[],i.cases.push(r);do{const t=E(e);a||(a=0===t.items.length),i.cases.push(t)}while(14!==c.currentType);return a&&CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL,s(i,e.currentOffset(),e.currentPosition()),i}(e,n,r,c)}return{parse:function(n){const r=createTokenizer(n,assign({},e)),c=r.context(),a=o(0,c.offset,c.startLoc);return t&&a.loc&&(a.loc.source=n),a.body=C(r),e.onCacheKey&&(a.cacheKey=e.onCacheKey(n)),14!==c.currentType&&(CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS,c.lastStartLoc,n[c.offset]),s(a,r.currentOffset(),r.currentPosition()),a}}}function getTokenCaption(e){if(14===e.type)return"EOF";const t=(e.value||"").replace(/\r?\n/gu,"\\n");return t.length>10?t.slice(0,9)+"…":t}function createTransformer(e,t={}){const n={ast:e,helpers:new Set};return{context:()=>n,helper:e=>(n.helpers.add(e),e)}}function traverseNodes(e,t){for(let n=0;n<e.length;n++)traverseNode(e[n],t)}function traverseNode(e,t){switch(e.type){case 1:traverseNodes(e.cases,t),t.helper("plural");break;case 2:traverseNodes(e.items,t);break;case 6:traverseNode(e.key,t),t.helper("linked"),t.helper("type");break;case 5:t.helper("interpolate"),t.helper("list");break;case 4:t.helper("interpolate"),t.helper("named")}}function transform(e,t={}){const n=createTransformer(e);n.helper("normalize"),e.body&&traverseNode(e.body,n);const r=n.context();e.helpers=Array.from(r.helpers)}function optimize(e){const t=e.body;return 2===t.type?optimizeMessageNode(t):t.cases.forEach((e=>optimizeMessageNode(e))),e}function optimizeMessageNode(e){if(1===e.items.length){const t=e.items[0];3!==t.type&&9!==t.type||(e.static=t.value,delete t.value)}else{const t=[];for(let n=0;n<e.items.length;n++){const r=e.items[n];if(3!==r.type&&9!==r.type)break;if(null==r.value)break;t.push(r.value)}if(t.length===e.items.length){e.static=join(t);for(let t=0;t<e.items.length;t++){const n=e.items[t];3!==n.type&&9!==n.type||delete n.value}}}}function minify(e){switch(e.t=e.type,e.type){case 0:{const t=e;minify(t.body),t.b=t.body,delete t.body;break}case 1:{const t=e,n=t.cases;for(let e=0;e<n.length;e++)minify(n[e]);t.c=n,delete t.cases;break}case 2:{const t=e,n=t.items;for(let e=0;e<n.length;e++)minify(n[e]);t.i=n,delete t.items,t.static&&(t.s=t.static,delete t.static);break}case 3:case 9:case 8:case 7:{const t=e;t.value&&(t.v=t.value,delete t.value);break}case 6:{const t=e;minify(t.key),t.k=t.key,delete t.key,t.modifier&&(minify(t.modifier),t.m=t.modifier,delete t.modifier);break}case 5:{const t=e;t.i=t.index,delete t.index;break}case 4:{const t=e;t.k=t.key,delete t.key;break}}delete e.type}function createCodeGenerator(e,t){const{sourceMap:n,filename:r,breakLineCode:o,needIndent:s}=t,c=!1!==t.location,a={filename:r,code:"",column:1,line:1,offset:0,map:void 0,breakLineCode:o,needIndent:s,indentLevel:0};c&&e.loc&&(a.source=e.loc.source);function i(e,t){a.code+=e}function u(e,t=!0){const n=t?o:"";i(s?n+" ".repeat(e):n)}return{context:()=>a,push:i,indent:function(e=!0){const t=++a.indentLevel;e&&u(t)},deindent:function(e=!0){const t=--a.indentLevel;e&&u(t)},newline:function(){u(a.indentLevel)},helper:e=>`_${e}`,needIndent:()=>a.needIndent}}function generateLinkedNode(e,t){const{helper:n}=e;e.push(`${n("linked")}(`),generateNode(e,t.key),t.modifier?(e.push(", "),generateNode(e,t.modifier),e.push(", _type")):e.push(", undefined, _type"),e.push(")")}function generateMessageNode(e,t){const{helper:n,needIndent:r}=e;e.push(`${n("normalize")}([`),e.indent(r());const o=t.items.length;for(let s=0;s<o&&(generateNode(e,t.items[s]),s!==o-1);s++)e.push(", ");e.deindent(r()),e.push("])")}function generatePluralNode(e,t){const{helper:n,needIndent:r}=e;if(t.cases.length>1){e.push(`${n("plural")}([`),e.indent(r());const o=t.cases.length;for(let n=0;n<o&&(generateNode(e,t.cases[n]),n!==o-1);n++)e.push(", ");e.deindent(r()),e.push("])")}}function generateResource(e,t){t.body?generateNode(e,t.body):e.push("null")}function generateNode(e,t){const{helper:n}=e;switch(t.type){case 0:generateResource(e,t);break;case 1:generatePluralNode(e,t);break;case 2:generateMessageNode(e,t);break;case 6:generateLinkedNode(e,t);break;case 8:case 7:case 9:case 3:e.push(JSON.stringify(t.value),t);break;case 5:e.push(`${n("interpolate")}(${n("list")}(${t.index}))`,t);break;case 4:e.push(`${n("interpolate")}(${n("named")}(${JSON.stringify(t.key)}))`,t)}}const generate=(e,t={})=>{const n=isString(t.mode)?t.mode:"normal",r=isString(t.filename)?t.filename:"message.intl",o=!!t.sourceMap,s=null!=t.breakLineCode?t.breakLineCode:"arrow"===n?";":"\n",c=t.needIndent?t.needIndent:"arrow"!==n,a=e.helpers||[],i=createCodeGenerator(e,{mode:n,filename:r,sourceMap:o,breakLineCode:s,needIndent:c});i.push("normal"===n?"function __msg__ (ctx) {":"(ctx) => {"),i.indent(c),a.length>0&&(i.push(`const { ${join(a.map((e=>`${e}: _${e}`)),", ")} } = ctx`),i.newline()),i.push("return "),generateNode(i,e),i.deindent(c),i.push("}"),delete e.helpers;const{code:u,map:l}=i.context();return{ast:e,code:u,map:l?l.toJSON():void 0}};function baseCompile(e,t={}){const n=assign({},t),r=!!n.jit,o=!!n.minify,s=null==n.optimize||n.optimize,c=createParser(n).parse(e);return r?(s&&optimize(c),o&&minify(c),{ast:c,code:""}):(transform(c,n),generate(c,n))}export{CompileErrorCodes,CompileWarnCodes,ERROR_DOMAIN,LOCATION_STUB,baseCompile,createCompileError,createCompileWarn,createLocation,createParser,createPosition,defaultOnError,detectHtmlTag,errorMessages,warnMessages};
|