@intlify/message-compiler 12.0.0-alpha.3 → 12.0.0-alpha.4

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