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

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