@intlify/message-compiler 9.3.0-beta.2 → 9.3.0-beta.21

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