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

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