@intlify/message-compiler 9.3.0-beta.17 → 9.3.0-beta.18

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.
Files changed (54) hide show
  1. package/dist/e2e/bridge/basic.spec.d.ts +1 -1
  2. package/dist/e2e/bridge/component.spec.d.ts +1 -1
  3. package/dist/e2e/bridge/components/datetime-format.spec.d.ts +1 -1
  4. package/dist/e2e/bridge/components/number-format.spec.d.ts +1 -1
  5. package/dist/e2e/bridge/components/translation.spec.d.ts +1 -1
  6. package/dist/e2e/bridge/plural.spec.d.ts +1 -1
  7. package/dist/e2e/bridge/scope/global.spec.d.ts +1 -1
  8. package/dist/e2e/bridge/scope/inherit-locale.spec.d.ts +1 -1
  9. package/dist/e2e/bridge/scope/local.spec.d.ts +1 -1
  10. package/dist/e2e/components/datetime-format.spec.d.ts +1 -1
  11. package/dist/e2e/components/number-format.spec.d.ts +1 -1
  12. package/dist/e2e/components/translation.spec.d.ts +1 -1
  13. package/dist/e2e/composition.spec.d.ts +1 -1
  14. package/dist/e2e/datetime.spec.d.ts +1 -1
  15. package/dist/e2e/directive/basic.spec.d.ts +1 -1
  16. package/dist/e2e/directive/object.spec.d.ts +1 -1
  17. package/dist/e2e/directive/plural.spec.d.ts +1 -1
  18. package/dist/e2e/directive/preserve.spec.d.ts +1 -1
  19. package/dist/e2e/fallback/basic.spec.d.ts +1 -1
  20. package/dist/e2e/fallback/component.spec.d.ts +1 -1
  21. package/dist/e2e/fallback/default-format.spec.d.ts +1 -1
  22. package/dist/e2e/fallback/format.spec.d.ts +1 -1
  23. package/dist/e2e/fallback/option.spec.d.ts +1 -1
  24. package/dist/e2e/fallback/suppress.spec.d.ts +1 -1
  25. package/dist/e2e/formatting/linked.spec.d.ts +1 -1
  26. package/dist/e2e/formatting/list.spec.d.ts +1 -1
  27. package/dist/e2e/formatting/literal.spec.d.ts +1 -1
  28. package/dist/e2e/formatting/named.spec.d.ts +1 -1
  29. package/dist/e2e/formatting/ruby.spec.d.ts +1 -1
  30. package/dist/e2e/functions/linked.spec.d.ts +1 -1
  31. package/dist/e2e/functions/list.spec.d.ts +1 -1
  32. package/dist/e2e/functions/named.spec.d.ts +1 -1
  33. package/dist/e2e/functions/plural.spec.d.ts +1 -1
  34. package/dist/e2e/helper.d.ts +8 -8
  35. package/dist/e2e/missing/handler.spec.d.ts +1 -1
  36. package/dist/e2e/missing/option.spec.d.ts +1 -1
  37. package/dist/e2e/missing/suppress.spec.d.ts +1 -1
  38. package/dist/e2e/number.spec.d.ts +1 -1
  39. package/dist/e2e/plural/basic.spec.d.ts +1 -1
  40. package/dist/e2e/plural/custom.spec.d.ts +1 -1
  41. package/dist/e2e/scope/global.spec.d.ts +1 -1
  42. package/dist/e2e/scope/inherit-locale.spec.d.ts +1 -1
  43. package/dist/e2e/scope/local.spec.d.ts +1 -1
  44. package/dist/e2e/started.spec.d.ts +1 -1
  45. package/dist/message-compiler.cjs +1405 -1405
  46. package/dist/message-compiler.esm-browser.js +1369 -1369
  47. package/dist/message-compiler.esm-browser.prod.js +1 -1
  48. package/dist/message-compiler.global.js +1369 -1369
  49. package/dist/message-compiler.global.prod.js +2 -2
  50. package/dist/message-compiler.mjs +1350 -1350
  51. package/dist/message-compiler.prod.cjs +1400 -1400
  52. package/dist/scripts/vitest.globalSetup.d.ts +1 -1
  53. package/dist/scripts/vitest.setup.d.ts +1 -1
  54. package/package.json +3 -3
@@ -1,1394 +1,1394 @@
1
1
  /*!
2
- * message-compiler v9.3.0-beta.17
2
+ * message-compiler v9.3.0-beta.18
3
3
  * (c) 2023 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
6
- /**
7
- * Original Utilities
8
- * written by kazuya kawaguchi
9
- */
10
- const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g;
11
- /* eslint-disable */
12
- function format(message, ...args) {
13
- if (args.length === 1 && isObject(args[0])) {
14
- args = args[0];
15
- }
16
- if (!args || !args.hasOwnProperty) {
17
- args = {};
18
- }
19
- return message.replace(RE_ARGS, (match, identifier) => {
20
- return args.hasOwnProperty(identifier) ? args[identifier] : '';
21
- });
22
- }
23
- const assign = Object.assign;
24
- const isString = (val) => typeof val === 'string';
25
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ /**
7
+ * Original Utilities
8
+ * written by kazuya kawaguchi
9
+ */
10
+ const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g;
11
+ /* eslint-disable */
12
+ function format(message, ...args) {
13
+ if (args.length === 1 && isObject(args[0])) {
14
+ args = args[0];
15
+ }
16
+ if (!args || !args.hasOwnProperty) {
17
+ args = {};
18
+ }
19
+ return message.replace(RE_ARGS, (match, identifier) => {
20
+ return args.hasOwnProperty(identifier) ? args[identifier] : '';
21
+ });
22
+ }
23
+ const assign = Object.assign;
24
+ const isString = (val) => typeof val === 'string';
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
26
  const isObject = (val) => val !== null && typeof val === 'object';
27
27
 
28
- const CompileErrorCodes = {
29
- // tokenizer error codes
30
- EXPECTED_TOKEN: 1,
31
- INVALID_TOKEN_IN_PLACEHOLDER: 2,
32
- UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER: 3,
33
- UNKNOWN_ESCAPE_SEQUENCE: 4,
34
- INVALID_UNICODE_ESCAPE_SEQUENCE: 5,
35
- UNBALANCED_CLOSING_BRACE: 6,
36
- UNTERMINATED_CLOSING_BRACE: 7,
37
- EMPTY_PLACEHOLDER: 8,
38
- NOT_ALLOW_NEST_PLACEHOLDER: 9,
39
- INVALID_LINKED_FORMAT: 10,
40
- // parser error codes
41
- MUST_HAVE_MESSAGES_IN_PLURAL: 11,
42
- UNEXPECTED_EMPTY_LINKED_MODIFIER: 12,
43
- UNEXPECTED_EMPTY_LINKED_KEY: 13,
44
- UNEXPECTED_LEXICAL_ANALYSIS: 14,
45
- // Special value for higher-order compilers to pick up the last code
46
- // to avoid collision of error codes. This should always be kept as the last
47
- // item.
48
- __EXTEND_POINT__: 15
49
- };
50
- /** @internal */
51
- const errorMessages = {
52
- // tokenizer error messages
53
- [CompileErrorCodes.EXPECTED_TOKEN]: `Expected token: '{0}'`,
54
- [CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER]: `Invalid token in placeholder: '{0}'`,
55
- [CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER]: `Unterminated single quote in placeholder`,
56
- [CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE]: `Unknown escape sequence: \\{0}`,
57
- [CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE]: `Invalid unicode escape sequence: {0}`,
58
- [CompileErrorCodes.UNBALANCED_CLOSING_BRACE]: `Unbalanced closing brace`,
59
- [CompileErrorCodes.UNTERMINATED_CLOSING_BRACE]: `Unterminated closing brace`,
60
- [CompileErrorCodes.EMPTY_PLACEHOLDER]: `Empty placeholder`,
61
- [CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER]: `Not allowed nest placeholder`,
62
- [CompileErrorCodes.INVALID_LINKED_FORMAT]: `Invalid linked format`,
63
- // parser error messages
64
- [CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]: `Plural must have messages`,
65
- [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]: `Unexpected empty linked modifier`,
66
- [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]: `Unexpected empty linked key`,
67
- [CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`
68
- };
69
- function createCompileError(code, loc, options = {}) {
70
- const { domain, messages, args } = options;
71
- const msg = format((messages || errorMessages)[code] || '', ...(args || []))
72
- ;
73
- const error = new SyntaxError(String(msg));
74
- error.code = code;
75
- if (loc) {
76
- error.location = loc;
77
- }
78
- error.domain = domain;
79
- return error;
80
- }
81
- /** @internal */
82
- function defaultOnError(error) {
83
- throw error;
28
+ const CompileErrorCodes = {
29
+ // tokenizer error codes
30
+ EXPECTED_TOKEN: 1,
31
+ INVALID_TOKEN_IN_PLACEHOLDER: 2,
32
+ UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER: 3,
33
+ UNKNOWN_ESCAPE_SEQUENCE: 4,
34
+ INVALID_UNICODE_ESCAPE_SEQUENCE: 5,
35
+ UNBALANCED_CLOSING_BRACE: 6,
36
+ UNTERMINATED_CLOSING_BRACE: 7,
37
+ EMPTY_PLACEHOLDER: 8,
38
+ NOT_ALLOW_NEST_PLACEHOLDER: 9,
39
+ INVALID_LINKED_FORMAT: 10,
40
+ // parser error codes
41
+ MUST_HAVE_MESSAGES_IN_PLURAL: 11,
42
+ UNEXPECTED_EMPTY_LINKED_MODIFIER: 12,
43
+ UNEXPECTED_EMPTY_LINKED_KEY: 13,
44
+ UNEXPECTED_LEXICAL_ANALYSIS: 14,
45
+ // Special value for higher-order compilers to pick up the last code
46
+ // to avoid collision of error codes. This should always be kept as the last
47
+ // item.
48
+ __EXTEND_POINT__: 15
49
+ };
50
+ /** @internal */
51
+ const errorMessages = {
52
+ // tokenizer error messages
53
+ [CompileErrorCodes.EXPECTED_TOKEN]: `Expected token: '{0}'`,
54
+ [CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER]: `Invalid token in placeholder: '{0}'`,
55
+ [CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER]: `Unterminated single quote in placeholder`,
56
+ [CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE]: `Unknown escape sequence: \\{0}`,
57
+ [CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE]: `Invalid unicode escape sequence: {0}`,
58
+ [CompileErrorCodes.UNBALANCED_CLOSING_BRACE]: `Unbalanced closing brace`,
59
+ [CompileErrorCodes.UNTERMINATED_CLOSING_BRACE]: `Unterminated closing brace`,
60
+ [CompileErrorCodes.EMPTY_PLACEHOLDER]: `Empty placeholder`,
61
+ [CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER]: `Not allowed nest placeholder`,
62
+ [CompileErrorCodes.INVALID_LINKED_FORMAT]: `Invalid linked format`,
63
+ // parser error messages
64
+ [CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]: `Plural must have messages`,
65
+ [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]: `Unexpected empty linked modifier`,
66
+ [CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]: `Unexpected empty linked key`,
67
+ [CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`
68
+ };
69
+ function createCompileError(code, loc, options = {}) {
70
+ const { domain, messages, args } = options;
71
+ const msg = format((messages || errorMessages)[code] || '', ...(args || []))
72
+ ;
73
+ const error = new SyntaxError(String(msg));
74
+ error.code = code;
75
+ if (loc) {
76
+ error.location = loc;
77
+ }
78
+ error.domain = domain;
79
+ return error;
80
+ }
81
+ /** @internal */
82
+ function defaultOnError(error) {
83
+ throw error;
84
84
  }
85
85
 
86
- const RE_HTML_TAG = /<\/?[\w\s="/.':;#-\/]+>/;
86
+ const RE_HTML_TAG = /<\/?[\w\s="/.':;#-\/]+>/;
87
87
  const detectHtmlTag = (source) => RE_HTML_TAG.test(source);
88
88
 
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;
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;
102
102
  }
103
103
 
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
- };
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
+ };
178
178
  }
179
179
 
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 /* TokenTypes.EOF */,
192
- offset: _initOffset,
193
- startLoc: _initLoc,
194
- endLoc: _initLoc,
195
- lastType: 14 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.LinkedAlias */) {
305
- return false;
306
- }
307
- peekSpaces(scnr);
308
- const ret = scnr.currentPeek() === "." /* TokenChars.LinkedDot */;
309
- scnr.resetPeek();
310
- return ret;
311
- }
312
- function isLinkedModifierStart(scnr, context) {
313
- const { currentType } = context;
314
- if (currentType !== 9 /* TokenTypes.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 /* TokenTypes.LinkedAlias */ ||
325
- currentType === 12 /* TokenTypes.LinkedModifier */)) {
326
- return false;
327
- }
328
- peekSpaces(scnr);
329
- const ret = scnr.currentPeek() === ":" /* TokenChars.LinkedDelimiter */;
330
- scnr.resetPeek();
331
- return ret;
332
- }
333
- function isLinkedReferStart(scnr, context) {
334
- const { currentType } = context;
335
- if (currentType !== 10 /* TokenTypes.LinkedDelimiter */) {
336
- return false;
337
- }
338
- const fn = () => {
339
- const ch = scnr.currentPeek();
340
- if (ch === "{" /* TokenChars.BraceLeft */) {
341
- return isIdentifierStart(scnr.peek());
342
- }
343
- else if (ch === "@" /* TokenChars.LinkedAlias */ ||
344
- ch === "%" /* TokenChars.Modulo */ ||
345
- ch === "|" /* TokenChars.Pipe */ ||
346
- ch === ":" /* TokenChars.LinkedDelimiter */ ||
347
- ch === "." /* TokenChars.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() === "|" /* TokenChars.Pipe */;
368
- scnr.resetPeek();
369
- return ret;
370
- }
371
- function detectModuloStart(scnr) {
372
- const spaces = peekSpaces(scnr);
373
- const ret = scnr.currentPeek() === "%" /* TokenChars.Modulo */ &&
374
- scnr.peek() === "{" /* TokenChars.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 === "{" /* TokenChars.BraceLeft */) {
385
- return prev === "%" /* TokenChars.Modulo */ ? false : hasSpace;
386
- }
387
- else if (ch === "@" /* TokenChars.LinkedAlias */ || !ch) {
388
- return prev === "%" /* TokenChars.Modulo */ ? true : hasSpace;
389
- }
390
- else if (ch === "%" /* TokenChars.Modulo */) {
391
- scnr.peek();
392
- return fn(hasSpace, "%" /* TokenChars.Modulo */, true);
393
- }
394
- else if (ch === "|" /* TokenChars.Pipe */) {
395
- return prev === "%" /* TokenChars.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 !== "%" /* TokenChars.Modulo */) {
466
- emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
467
- }
468
- scnr.next();
469
- return "%" /* TokenChars.Modulo */;
470
- }
471
- function readText(scnr) {
472
- let buf = '';
473
- while (true) {
474
- const ch = scnr.currentChar();
475
- if (ch === "{" /* TokenChars.BraceLeft */ ||
476
- ch === "}" /* TokenChars.BraceRight */ ||
477
- ch === "@" /* TokenChars.LinkedAlias */ ||
478
- ch === "|" /* TokenChars.Pipe */ ||
479
- !ch) {
480
- break;
481
- }
482
- else if (ch === "%" /* TokenChars.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 !== "{" /* TokenChars.BraceLeft */ &&
599
- ch !== "}" /* TokenChars.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 === "{" /* TokenChars.BraceLeft */ ||
619
- ch === "%" /* TokenChars.Modulo */ ||
620
- ch === "@" /* TokenChars.LinkedAlias */ ||
621
- ch === "|" /* TokenChars.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, "|" /* TokenChars.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 "{" /* TokenChars.BraceLeft */:
653
- if (context.braceNest >= 1) {
654
- emitError(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER, currentPosition(), 0);
655
- }
656
- scnr.next();
657
- token = getToken(context, 2 /* TokenTypes.BraceLeft */, "{" /* TokenChars.BraceLeft */);
658
- skipSpaces(scnr);
659
- context.braceNest++;
660
- return token;
661
- case "}" /* TokenChars.BraceRight */:
662
- if (context.braceNest > 0 &&
663
- context.currentType === 2 /* TokenTypes.BraceLeft */) {
664
- emitError(CompileErrorCodes.EMPTY_PLACEHOLDER, currentPosition(), 0);
665
- }
666
- scnr.next();
667
- token = getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.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 "@" /* TokenChars.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 /* TokenTypes.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 /* TokenTypes.Named */ ||
697
- context.currentType === 6 /* TokenTypes.List */ ||
698
- context.currentType === 7 /* TokenTypes.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 /* TokenTypes.Named */, readNamedIdentifier(scnr));
705
- skipSpaces(scnr);
706
- return token;
707
- }
708
- if ((validListIdentifier = isListIdentifierStart(scnr, context))) {
709
- token = getToken(context, 6 /* TokenTypes.List */, readListIdentifier(scnr));
710
- skipSpaces(scnr);
711
- return token;
712
- }
713
- if ((validLiteral = isLiteralStart(scnr, context))) {
714
- token = getToken(context, 7 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.LinkedAlias */ ||
735
- currentType === 9 /* TokenTypes.LinkedDot */ ||
736
- currentType === 12 /* TokenTypes.LinkedModifier */ ||
737
- currentType === 10 /* TokenTypes.LinkedDelimiter */) &&
738
- (ch === CHAR_LF || ch === CHAR_SP)) {
739
- emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
740
- }
741
- switch (ch) {
742
- case "@" /* TokenChars.LinkedAlias */:
743
- scnr.next();
744
- token = getToken(context, 8 /* TokenTypes.LinkedAlias */, "@" /* TokenChars.LinkedAlias */);
745
- context.inLinked = true;
746
- return token;
747
- case "." /* TokenChars.LinkedDot */:
748
- skipSpaces(scnr);
749
- scnr.next();
750
- return getToken(context, 9 /* TokenTypes.LinkedDot */, "." /* TokenChars.LinkedDot */);
751
- case ":" /* TokenChars.LinkedDelimiter */:
752
- skipSpaces(scnr);
753
- scnr.next();
754
- return getToken(context, 10 /* TokenTypes.LinkedDelimiter */, ":" /* TokenChars.LinkedDelimiter */);
755
- default:
756
- if (isPluralStart(scnr)) {
757
- token = getToken(context, 1 /* TokenTypes.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 /* TokenTypes.LinkedModifier */, readLinkedModifier(scnr));
771
- }
772
- if (isLinkedReferStart(scnr, context)) {
773
- skipSpaces(scnr);
774
- if (ch === "{" /* TokenChars.BraceLeft */) {
775
- // scan the placeholder
776
- return readTokenInPlaceholder(scnr, context) || token;
777
- }
778
- else {
779
- return getToken(context, 11 /* TokenTypes.LinkedKey */, readLinkedRefer(scnr));
780
- }
781
- }
782
- if (currentType === 8 /* TokenTypes.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 /* TokenTypes.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 "{" /* TokenChars.BraceLeft */:
802
- return readTokenInPlaceholder(scnr, context) || getEndToken(context);
803
- case "}" /* TokenChars.BraceRight */:
804
- emitError(CompileErrorCodes.UNBALANCED_CLOSING_BRACE, currentPosition(), 0);
805
- scnr.next();
806
- return getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
807
- case "@" /* TokenChars.LinkedAlias */:
808
- return readTokenInLinked(scnr, context) || getEndToken(context);
809
- default:
810
- if (isPluralStart(scnr)) {
811
- token = getToken(context, 1 /* TokenTypes.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 /* TokenTypes.Text */, readText(scnr))
821
- : getToken(context, 4 /* TokenTypes.Modulo */, readModulo(scnr));
822
- }
823
- if (isTextStart(scnr)) {
824
- return getToken(context, 0 /* TokenTypes.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 /* TokenTypes.EOF */);
840
- }
841
- return readToken(_scnr, _context);
842
- }
843
- return {
844
- nextToken,
845
- currentOffset,
846
- currentPosition,
847
- context
848
- };
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 /* TokenTypes.EOF */,
192
+ offset: _initOffset,
193
+ startLoc: _initLoc,
194
+ endLoc: _initLoc,
195
+ lastType: 14 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.LinkedAlias */) {
305
+ return false;
306
+ }
307
+ peekSpaces(scnr);
308
+ const ret = scnr.currentPeek() === "." /* TokenChars.LinkedDot */;
309
+ scnr.resetPeek();
310
+ return ret;
311
+ }
312
+ function isLinkedModifierStart(scnr, context) {
313
+ const { currentType } = context;
314
+ if (currentType !== 9 /* TokenTypes.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 /* TokenTypes.LinkedAlias */ ||
325
+ currentType === 12 /* TokenTypes.LinkedModifier */)) {
326
+ return false;
327
+ }
328
+ peekSpaces(scnr);
329
+ const ret = scnr.currentPeek() === ":" /* TokenChars.LinkedDelimiter */;
330
+ scnr.resetPeek();
331
+ return ret;
332
+ }
333
+ function isLinkedReferStart(scnr, context) {
334
+ const { currentType } = context;
335
+ if (currentType !== 10 /* TokenTypes.LinkedDelimiter */) {
336
+ return false;
337
+ }
338
+ const fn = () => {
339
+ const ch = scnr.currentPeek();
340
+ if (ch === "{" /* TokenChars.BraceLeft */) {
341
+ return isIdentifierStart(scnr.peek());
342
+ }
343
+ else if (ch === "@" /* TokenChars.LinkedAlias */ ||
344
+ ch === "%" /* TokenChars.Modulo */ ||
345
+ ch === "|" /* TokenChars.Pipe */ ||
346
+ ch === ":" /* TokenChars.LinkedDelimiter */ ||
347
+ ch === "." /* TokenChars.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() === "|" /* TokenChars.Pipe */;
368
+ scnr.resetPeek();
369
+ return ret;
370
+ }
371
+ function detectModuloStart(scnr) {
372
+ const spaces = peekSpaces(scnr);
373
+ const ret = scnr.currentPeek() === "%" /* TokenChars.Modulo */ &&
374
+ scnr.peek() === "{" /* TokenChars.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 === "{" /* TokenChars.BraceLeft */) {
385
+ return prev === "%" /* TokenChars.Modulo */ ? false : hasSpace;
386
+ }
387
+ else if (ch === "@" /* TokenChars.LinkedAlias */ || !ch) {
388
+ return prev === "%" /* TokenChars.Modulo */ ? true : hasSpace;
389
+ }
390
+ else if (ch === "%" /* TokenChars.Modulo */) {
391
+ scnr.peek();
392
+ return fn(hasSpace, "%" /* TokenChars.Modulo */, true);
393
+ }
394
+ else if (ch === "|" /* TokenChars.Pipe */) {
395
+ return prev === "%" /* TokenChars.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 !== "%" /* TokenChars.Modulo */) {
466
+ emitError(CompileErrorCodes.EXPECTED_TOKEN, currentPosition(), 0, ch);
467
+ }
468
+ scnr.next();
469
+ return "%" /* TokenChars.Modulo */;
470
+ }
471
+ function readText(scnr) {
472
+ let buf = '';
473
+ while (true) {
474
+ const ch = scnr.currentChar();
475
+ if (ch === "{" /* TokenChars.BraceLeft */ ||
476
+ ch === "}" /* TokenChars.BraceRight */ ||
477
+ ch === "@" /* TokenChars.LinkedAlias */ ||
478
+ ch === "|" /* TokenChars.Pipe */ ||
479
+ !ch) {
480
+ break;
481
+ }
482
+ else if (ch === "%" /* TokenChars.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 !== "{" /* TokenChars.BraceLeft */ &&
599
+ ch !== "}" /* TokenChars.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 === "{" /* TokenChars.BraceLeft */ ||
619
+ ch === "%" /* TokenChars.Modulo */ ||
620
+ ch === "@" /* TokenChars.LinkedAlias */ ||
621
+ ch === "|" /* TokenChars.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, "|" /* TokenChars.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 "{" /* TokenChars.BraceLeft */:
653
+ if (context.braceNest >= 1) {
654
+ emitError(CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER, currentPosition(), 0);
655
+ }
656
+ scnr.next();
657
+ token = getToken(context, 2 /* TokenTypes.BraceLeft */, "{" /* TokenChars.BraceLeft */);
658
+ skipSpaces(scnr);
659
+ context.braceNest++;
660
+ return token;
661
+ case "}" /* TokenChars.BraceRight */:
662
+ if (context.braceNest > 0 &&
663
+ context.currentType === 2 /* TokenTypes.BraceLeft */) {
664
+ emitError(CompileErrorCodes.EMPTY_PLACEHOLDER, currentPosition(), 0);
665
+ }
666
+ scnr.next();
667
+ token = getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.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 "@" /* TokenChars.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 /* TokenTypes.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 /* TokenTypes.Named */ ||
697
+ context.currentType === 6 /* TokenTypes.List */ ||
698
+ context.currentType === 7 /* TokenTypes.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 /* TokenTypes.Named */, readNamedIdentifier(scnr));
705
+ skipSpaces(scnr);
706
+ return token;
707
+ }
708
+ if ((validListIdentifier = isListIdentifierStart(scnr, context))) {
709
+ token = getToken(context, 6 /* TokenTypes.List */, readListIdentifier(scnr));
710
+ skipSpaces(scnr);
711
+ return token;
712
+ }
713
+ if ((validLiteral = isLiteralStart(scnr, context))) {
714
+ token = getToken(context, 7 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.LinkedAlias */ ||
735
+ currentType === 9 /* TokenTypes.LinkedDot */ ||
736
+ currentType === 12 /* TokenTypes.LinkedModifier */ ||
737
+ currentType === 10 /* TokenTypes.LinkedDelimiter */) &&
738
+ (ch === CHAR_LF || ch === CHAR_SP)) {
739
+ emitError(CompileErrorCodes.INVALID_LINKED_FORMAT, currentPosition(), 0);
740
+ }
741
+ switch (ch) {
742
+ case "@" /* TokenChars.LinkedAlias */:
743
+ scnr.next();
744
+ token = getToken(context, 8 /* TokenTypes.LinkedAlias */, "@" /* TokenChars.LinkedAlias */);
745
+ context.inLinked = true;
746
+ return token;
747
+ case "." /* TokenChars.LinkedDot */:
748
+ skipSpaces(scnr);
749
+ scnr.next();
750
+ return getToken(context, 9 /* TokenTypes.LinkedDot */, "." /* TokenChars.LinkedDot */);
751
+ case ":" /* TokenChars.LinkedDelimiter */:
752
+ skipSpaces(scnr);
753
+ scnr.next();
754
+ return getToken(context, 10 /* TokenTypes.LinkedDelimiter */, ":" /* TokenChars.LinkedDelimiter */);
755
+ default:
756
+ if (isPluralStart(scnr)) {
757
+ token = getToken(context, 1 /* TokenTypes.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 /* TokenTypes.LinkedModifier */, readLinkedModifier(scnr));
771
+ }
772
+ if (isLinkedReferStart(scnr, context)) {
773
+ skipSpaces(scnr);
774
+ if (ch === "{" /* TokenChars.BraceLeft */) {
775
+ // scan the placeholder
776
+ return readTokenInPlaceholder(scnr, context) || token;
777
+ }
778
+ else {
779
+ return getToken(context, 11 /* TokenTypes.LinkedKey */, readLinkedRefer(scnr));
780
+ }
781
+ }
782
+ if (currentType === 8 /* TokenTypes.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 /* TokenTypes.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 "{" /* TokenChars.BraceLeft */:
802
+ return readTokenInPlaceholder(scnr, context) || getEndToken(context);
803
+ case "}" /* TokenChars.BraceRight */:
804
+ emitError(CompileErrorCodes.UNBALANCED_CLOSING_BRACE, currentPosition(), 0);
805
+ scnr.next();
806
+ return getToken(context, 3 /* TokenTypes.BraceRight */, "}" /* TokenChars.BraceRight */);
807
+ case "@" /* TokenChars.LinkedAlias */:
808
+ return readTokenInLinked(scnr, context) || getEndToken(context);
809
+ default:
810
+ if (isPluralStart(scnr)) {
811
+ token = getToken(context, 1 /* TokenTypes.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 /* TokenTypes.Text */, readText(scnr))
821
+ : getToken(context, 4 /* TokenTypes.Modulo */, readModulo(scnr));
822
+ }
823
+ if (isTextStart(scnr)) {
824
+ return getToken(context, 0 /* TokenTypes.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 /* TokenTypes.EOF */);
840
+ }
841
+ return readToken(_scnr, _context);
842
+ }
843
+ return {
844
+ nextToken,
845
+ currentOffset,
846
+ currentPosition,
847
+ context
848
+ };
849
849
  }
850
850
 
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 /* NodeTypes.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 /* NodeTypes.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 /* NodeTypes.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 /* NodeTypes.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 /* NodeTypes.LinkedModifier */, offset, loc);
946
- if (token.type !== 12 /* TokenTypes.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 /* NodeTypes.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 /* NodeTypes.Linked */, context.offset, context.startLoc);
976
- let token = tokenizer.nextToken();
977
- if (token.type === 9 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.BraceLeft */) {
989
- token = tokenizer.nextToken();
990
- }
991
- switch (token.type) {
992
- case 11 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* NodeTypes.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 /* TokenTypes.Pipe */
1038
- ? tokenizer.currentOffset()
1039
- : context.offset;
1040
- const startLoc = context.currentType === 1 /* TokenTypes.Pipe */
1041
- ? context.endLoc
1042
- : context.startLoc;
1043
- const node = startNode(2 /* NodeTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.EOF */ &&
1081
- context.currentType !== 1 /* TokenTypes.Pipe */);
1082
- // adjust message node loc
1083
- const endOffset = context.currentType === 1 /* TokenTypes.Pipe */
1084
- ? context.lastOffset
1085
- : tokenizer.currentOffset();
1086
- const endLoc = context.currentType === 1 /* TokenTypes.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 /* NodeTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* NodeTypes.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 /* TokenTypes.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 /* TokenTypes.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;
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 /* NodeTypes.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 /* NodeTypes.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 /* NodeTypes.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 /* NodeTypes.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 /* NodeTypes.LinkedModifier */, offset, loc);
946
+ if (token.type !== 12 /* TokenTypes.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 /* NodeTypes.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 /* NodeTypes.Linked */, context.offset, context.startLoc);
976
+ let token = tokenizer.nextToken();
977
+ if (token.type === 9 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.BraceLeft */) {
989
+ token = tokenizer.nextToken();
990
+ }
991
+ switch (token.type) {
992
+ case 11 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* NodeTypes.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 /* TokenTypes.Pipe */
1038
+ ? tokenizer.currentOffset()
1039
+ : context.offset;
1040
+ const startLoc = context.currentType === 1 /* TokenTypes.Pipe */
1041
+ ? context.endLoc
1042
+ : context.startLoc;
1043
+ const node = startNode(2 /* NodeTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* TokenTypes.EOF */ &&
1081
+ context.currentType !== 1 /* TokenTypes.Pipe */);
1082
+ // adjust message node loc
1083
+ const endOffset = context.currentType === 1 /* TokenTypes.Pipe */
1084
+ ? context.lastOffset
1085
+ : tokenizer.currentOffset();
1086
+ const endLoc = context.currentType === 1 /* TokenTypes.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 /* NodeTypes.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 /* TokenTypes.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 /* TokenTypes.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 /* NodeTypes.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 /* TokenTypes.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 /* TokenTypes.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;
1145
1145
  }
1146
1146
 
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 /* NodeTypes.Plural */:
1169
- traverseNodes(node.cases, transformer);
1170
- transformer.helper("plural" /* HelperNameMap.PLURAL */);
1171
- break;
1172
- case 2 /* NodeTypes.Message */:
1173
- traverseNodes(node.items, transformer);
1174
- break;
1175
- case 6 /* NodeTypes.Linked */:
1176
- const linked = node;
1177
- traverseNode(linked.key, transformer);
1178
- transformer.helper("linked" /* HelperNameMap.LINKED */);
1179
- transformer.helper("type" /* HelperNameMap.TYPE */);
1180
- break;
1181
- case 5 /* NodeTypes.List */:
1182
- transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
1183
- transformer.helper("list" /* HelperNameMap.LIST */);
1184
- break;
1185
- case 4 /* NodeTypes.Named */:
1186
- transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
1187
- transformer.helper("named" /* HelperNameMap.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" /* HelperNameMap.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);
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 /* NodeTypes.Plural */:
1169
+ traverseNodes(node.cases, transformer);
1170
+ transformer.helper("plural" /* HelperNameMap.PLURAL */);
1171
+ break;
1172
+ case 2 /* NodeTypes.Message */:
1173
+ traverseNodes(node.items, transformer);
1174
+ break;
1175
+ case 6 /* NodeTypes.Linked */:
1176
+ const linked = node;
1177
+ traverseNode(linked.key, transformer);
1178
+ transformer.helper("linked" /* HelperNameMap.LINKED */);
1179
+ transformer.helper("type" /* HelperNameMap.TYPE */);
1180
+ break;
1181
+ case 5 /* NodeTypes.List */:
1182
+ transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
1183
+ transformer.helper("list" /* HelperNameMap.LIST */);
1184
+ break;
1185
+ case 4 /* NodeTypes.Named */:
1186
+ transformer.helper("interpolate" /* HelperNameMap.INTERPOLATE */);
1187
+ transformer.helper("named" /* HelperNameMap.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" /* HelperNameMap.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);
1202
1202
  }
1203
1203
 
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" /* HelperNameMap.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" /* HelperNameMap.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" /* HelperNameMap.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 /* NodeTypes.Resource */:
1307
- generateResource(generator, node);
1308
- break;
1309
- case 1 /* NodeTypes.Plural */:
1310
- generatePluralNode(generator, node);
1311
- break;
1312
- case 2 /* NodeTypes.Message */:
1313
- generateMessageNode(generator, node);
1314
- break;
1315
- case 6 /* NodeTypes.Linked */:
1316
- generateLinkedNode(generator, node);
1317
- break;
1318
- case 8 /* NodeTypes.LinkedModifier */:
1319
- generator.push(JSON.stringify(node.value), node);
1320
- break;
1321
- case 7 /* NodeTypes.LinkedKey */:
1322
- generator.push(JSON.stringify(node.value), node);
1323
- break;
1324
- case 5 /* NodeTypes.List */:
1325
- generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("list" /* HelperNameMap.LIST */)}(${node.index}))`, node);
1326
- break;
1327
- case 4 /* NodeTypes.Named */:
1328
- generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("named" /* HelperNameMap.NAMED */)}(${JSON.stringify(node.key)}))`, node);
1329
- break;
1330
- case 9 /* NodeTypes.Literal */:
1331
- generator.push(JSON.stringify(node.value), node);
1332
- break;
1333
- case 3 /* NodeTypes.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
- };
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" /* HelperNameMap.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" /* HelperNameMap.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" /* HelperNameMap.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 /* NodeTypes.Resource */:
1307
+ generateResource(generator, node);
1308
+ break;
1309
+ case 1 /* NodeTypes.Plural */:
1310
+ generatePluralNode(generator, node);
1311
+ break;
1312
+ case 2 /* NodeTypes.Message */:
1313
+ generateMessageNode(generator, node);
1314
+ break;
1315
+ case 6 /* NodeTypes.Linked */:
1316
+ generateLinkedNode(generator, node);
1317
+ break;
1318
+ case 8 /* NodeTypes.LinkedModifier */:
1319
+ generator.push(JSON.stringify(node.value), node);
1320
+ break;
1321
+ case 7 /* NodeTypes.LinkedKey */:
1322
+ generator.push(JSON.stringify(node.value), node);
1323
+ break;
1324
+ case 5 /* NodeTypes.List */:
1325
+ generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("list" /* HelperNameMap.LIST */)}(${node.index}))`, node);
1326
+ break;
1327
+ case 4 /* NodeTypes.Named */:
1328
+ generator.push(`${helper("interpolate" /* HelperNameMap.INTERPOLATE */)}(${helper("named" /* HelperNameMap.NAMED */)}(${JSON.stringify(node.key)}))`, node);
1329
+ break;
1330
+ case 9 /* NodeTypes.Literal */:
1331
+ generator.push(JSON.stringify(node.value), node);
1332
+ break;
1333
+ case 3 /* NodeTypes.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
+ };
1381
1381
  };
1382
1382
 
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);
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);
1392
1392
  }
1393
1393
 
1394
1394
  export { CompileErrorCodes, ERROR_DOMAIN, LocationStub, baseCompile, createCompileError, createLocation, createParser, createPosition, defaultOnError, detectHtmlTag, errorMessages };