This repository was archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresource.js
More file actions
467 lines (388 loc) · 13.7 KB
/
Copy pathresource.js
File metadata and controls
467 lines (388 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import FluentError from "./error.js";
// This regex is used to iterate through the beginnings of messages and terms.
// With the /m flag, the ^ matches at the beginning of every line.
const RE_MESSAGE_START = /^(-?[a-zA-Z][a-zA-Z0-9_-]*) *= */mg;
// Both Attributes and Variants are parsed in while loops. These regexes are
// used to break out of them.
const RE_ATTRIBUTE_START = /\.([a-zA-Z][a-zA-Z0-9_-]*) *= */y;
// [^] matches all characters, including newlines.
// XXX Use /s (dotall) when it's widely supported.
const RE_VARIANT_START = /\*?\[[^]*?] */y;
const RE_IDENTIFIER = /(-?[a-zA-Z][a-zA-Z0-9_-]*)/y;
const RE_NUMBER_LITERAL = /(-?[0-9]+(\.[0-9]+)?)/y;
// A "run" is a sequence of text or string literal characters which don't
// require any special handling. For TextElements such special characters are:
// { (starts a placeable), \ (starts an escape sequence), and line breaks which
// require additional logic to check if the next line is indented. For
// StringLiterals they are: \ (starts an escape sequence), " (ends the
// literal), and line breaks which are not allowed in StringLiterals. Also note
// that string runs may be empty, but text runs may not.
const RE_TEXT_RUN = /([^\\{\n\r]+)/y;
const RE_STRING_RUN = /([^\\"\n\r]*)/y;
// Escape sequences.
const RE_UNICODE_ESCAPE = /\\u([a-fA-F0-9]{4})/y;
const RE_STRING_ESCAPE = /\\([\\"])/y;
const RE_TEXT_ESCAPE = /\\([\\{])/y;
// Used for trimming TextElements and indents. With the /m flag, the $ matches
// the end of every line.
const RE_TRAILING_SPACES = / +$/mg;
// CRLFs are normalized to LF.
const RE_CRLF = /\r\n/g;
// Common tokens.
const TOKEN_BRACE_OPEN = /{\s*/y;
const TOKEN_BRACE_CLOSE = /\s*}/y;
const TOKEN_BRACKET_OPEN = /\[\s*/y;
const TOKEN_BRACKET_CLOSE = /\s*]/y;
const TOKEN_PAREN_OPEN = /\(\s*/y;
const TOKEN_ARROW = /\s*->\s*/y;
const TOKEN_COLON = /\s*:\s*/y;
// Note the optional comma. As a deviation from the Fluent EBNF, the parser
// doesn't enforce commas between call arguments.
const TOKEN_COMMA = /\s*,?\s*/y;
const TOKEN_BLANK = /\s+/y;
// Maximum number of placeables in a single Pattern to protect against Quadratic
// Blowup attacks. See https://msdn.microsoft.com/en-us/magazine/ee335713.aspx.
const MAX_PLACEABLES = 100;
/**
* Fluent Resource is a structure storing a map of parsed localization entries.
*/
export default class FluentResource extends Map {
/**
* Create a new FluentResource from Fluent code.
*/
static fromString(source) {
RE_MESSAGE_START.lastIndex = 0;
let resource = new this();
let cursor = 0;
// Iterate over the beginnings of messages and terms to efficiently skip
// comments and recover from errors.
while (true) {
let next = RE_MESSAGE_START.exec(source);
if (next === null) {
break;
}
cursor = RE_MESSAGE_START.lastIndex;
try {
resource.set(next[1], parseMessage());
} catch (err) {
if (err instanceof FluentError) {
// Don't report any Fluent syntax errors. Skip directly to the
// beginning of the next message or term.
continue;
}
throw err;
}
}
return resource;
// The parser implementation is inlined below for performance reasons.
// The parser focuses on minimizing the number of false negatives at the
// expense of increasing the risk of false positives. In other words, it
// aims at parsing valid Fluent messages with a success rate of 100%, but it
// may also parse a few invalid messages which the reference parser would
// reject. The parser doesn't perform any validation and may produce entries
// which wouldn't make sense in the real world. For best results users are
// advised to validate translations with the fluent-syntax parser
// pre-runtime.
// The parser makes an extensive use of sticky regexes which can be anchored
// to any offset of the source string without slicing it. Errors are thrown
// to bail out of parsing of ill-formed messages.
function test(re) {
re.lastIndex = cursor;
return re.test(source);
}
// Advance the cursor by the char if it matches. May be used as a predicate
// (was the match found?) or, if errorClass is passed, as an assertion.
function consumeChar(char, errorClass) {
if (source[cursor] === char) {
cursor++;
return true;
}
if (errorClass) {
throw new errorClass(`Expected ${char}`);
}
return false;
}
// Advance the cursor by the token if it matches. May be used as a predicate
// (was the match found?) or, if errorClass is passed, as an assertion.
function consumeToken(re, errorClass) {
if (test(re)) {
cursor = re.lastIndex;
return true;
}
if (errorClass) {
throw new errorClass(`Expected ${re.toString()}`);
}
return false;
}
// Execute a regex, advance the cursor, and return the capture group.
function match(re) {
re.lastIndex = cursor;
let result = re.exec(source);
if (result === null) {
throw new FluentError(`Expected ${re.toString()}`);
}
cursor = re.lastIndex;
return result[1];
}
function parseMessage() {
let value = parsePattern();
let attrs = parseAttributes();
if (attrs === null) {
return value;
}
return {value, attrs};
}
function parseAttributes() {
let attrs = {};
let hasAttributes = false;
while (test(RE_ATTRIBUTE_START)) {
if (!hasAttributes) {
hasAttributes = true;
}
let name = match(RE_ATTRIBUTE_START);
attrs[name] = parsePattern();
}
return hasAttributes ? attrs : null;
}
function parsePattern() {
// First try to parse any simple text on the same line as the id.
if (test(RE_TEXT_RUN)) {
var first = match(RE_TEXT_RUN);
}
// If there's a backslash escape or a placeable on the first line, fall
// back to parsing a complex pattern.
switch (source[cursor]) {
case "{":
case "\\":
return first
// Re-use the text parsed above, if possible.
? parsePatternElements(first)
: parsePatternElements();
}
// RE_TEXT_VALUE stops at newlines. Only continue parsing the pattern if
// what comes after the newline is indented.
let indent = parseIndent();
if (indent) {
return first
// If there's text on the first line, the blank block is part of the
// translation content.
? parsePatternElements(first, trim(indent))
// Otherwise, we're dealing with a block pattern. The blank block is
// the leading whitespace; discard it.
: parsePatternElements();
}
if (first) {
// It was just a simple inline text after all.
return trim(first);
}
return null;
}
// Parse a complex pattern as an array of elements.
function parsePatternElements(...elements) {
let placeableCount = 0;
let needsTrimming = false;
while (true) {
if (test(RE_TEXT_RUN)) {
elements.push(match(RE_TEXT_RUN));
needsTrimming = true;
continue;
}
if (source[cursor] === "{") {
if (++placeableCount > MAX_PLACEABLES) {
throw new FluentError("Too many placeables");
}
elements.push(parsePlaceable());
needsTrimming = false;
continue;
}
let indent = parseIndent();
if (indent) {
elements.push(trim(indent));
needsTrimming = false;
continue;
}
if (source[cursor] === "\\") {
elements.push(parseEscapeSequence(RE_TEXT_ESCAPE));
needsTrimming = false;
continue;
}
break;
}
if (needsTrimming) {
// Trim the trailing whitespace of the last element if it's a
// TextElement. Use a flag rather than a typeof check to tell
// TextElements and StringLiterals apart (both are strings).
let lastIndex = elements.length - 1;
elements[lastIndex] = trim(elements[lastIndex]);
}
return elements;
}
function parsePlaceable() {
consumeToken(TOKEN_BRACE_OPEN, FluentError);
// VariantLists are parsed as selector-less SelectExpressions.
let onlyVariants = parseVariants();
if (onlyVariants) {
consumeToken(TOKEN_BRACE_CLOSE, FluentError);
return {type: "select", selector: null, ...onlyVariants};
}
let selector = parseInlineExpression();
if (consumeToken(TOKEN_BRACE_CLOSE)) {
return selector;
}
if (consumeToken(TOKEN_ARROW)) {
let variants = parseVariants();
consumeToken(TOKEN_BRACE_CLOSE, FluentError);
return {type: "select", selector, ...variants};
}
throw new FluentError("Unclosed placeable");
}
function parseInlineExpression() {
if (source[cursor] === "{") {
// It's a nested placeable.
return parsePlaceable();
}
if (consumeChar("$")) {
return {type: "var", name: match(RE_IDENTIFIER)};
}
if (test(RE_IDENTIFIER)) {
let ref = {type: "ref", name: match(RE_IDENTIFIER)};
if (consumeChar(".")) {
let name = match(RE_IDENTIFIER);
return {type: "getattr", ref, name};
}
if (source[cursor] === "[") {
return {type: "getvar", ref, selector: parseVariantKey()};
}
if (consumeToken(TOKEN_PAREN_OPEN)) {
let callee = {...ref, type: "func"};
return {type: "call", callee, args: parseArguments()};
}
return ref;
}
return parseLiteral();
}
function parseArguments() {
let args = [];
while (true) {
switch (source[cursor]) {
case ")": // End of the argument list.
cursor++;
return args;
case undefined: // EOF
throw new FluentError("Unclosed argument list");
}
args.push(parseArgument());
// Commas between arguments are treated as whitespace.
consumeToken(TOKEN_COMMA);
}
}
function parseArgument() {
let ref = parseInlineExpression();
if (ref.type !== "ref") {
return ref;
}
if (consumeToken(TOKEN_COLON)) {
// The reference is the beginning of a named argument.
return {type: "narg", name: ref.name, value: parseLiteral()};
}
// It's a regular message reference.
return ref;
}
function parseVariants() {
let variants = [];
let count = 0;
let star;
while (test(RE_VARIANT_START)) {
if (consumeChar("*")) {
star = count;
}
let key = parseVariantKey();
cursor = RE_VARIANT_START.lastIndex;
variants[count++] = {key, value: parsePattern()};
}
return count > 0 ? {variants, star} : null;
}
function parseVariantKey() {
consumeToken(TOKEN_BRACKET_OPEN, FluentError);
let key = test(RE_NUMBER_LITERAL)
? parseNumberLiteral()
: match(RE_IDENTIFIER);
consumeToken(TOKEN_BRACKET_CLOSE, FluentError);
return key;
}
function parseLiteral() {
if (test(RE_NUMBER_LITERAL)) {
return parseNumberLiteral();
}
if (source[cursor] === "\"") {
return parseStringLiteral();
}
throw new FluentError("Invalid expression");
}
function parseNumberLiteral() {
return {type: "num", value: match(RE_NUMBER_LITERAL)};
}
function parseStringLiteral() {
consumeChar("\"", FluentError);
let value = "";
while (true) {
value += match(RE_STRING_RUN);
if (source[cursor] === "\\") {
value += parseEscapeSequence(RE_STRING_ESCAPE);
continue;
}
if (consumeChar("\"")) {
return value;
}
// We've reached an EOL of EOF.
throw new FluentError("Unclosed string literal");
}
}
// Unescape known escape sequences.
function parseEscapeSequence(reSpecialized) {
if (test(RE_UNICODE_ESCAPE)) {
let sequence = match(RE_UNICODE_ESCAPE);
return String.fromCodePoint(parseInt(sequence, 16));
}
if (test(reSpecialized)) {
return match(reSpecialized);
}
throw new FluentError("Unknown escape sequence");
}
// Parse blank space. Return it if it looks like indent before a pattern
// line. Skip it othwerwise.
function parseIndent() {
let start = cursor;
consumeToken(TOKEN_BLANK);
// Check the first non-blank character after the indent.
switch (source[cursor]) {
case ".":
case "[":
case "*":
case "}":
case undefined: // EOF
// A special character. End the Pattern.
return false;
case "{":
// Placeables don't require indentation (in EBNF: block-placeable).
// Continue the Pattern.
return source.slice(start, cursor).replace(RE_CRLF, "\n");
}
// If the first character on the line is not one of the special characters
// listed above, it's a regular text character. Check if there's at least
// one space of indent before it.
if (source[cursor - 1] === " ") {
// It's an indented text character (in EBNF: indented-char). Continue
// the Pattern.
return source.slice(start, cursor).replace(RE_CRLF, "\n");
}
// A not-indented text character is likely the identifier of the next
// message. End the Pattern.
return false;
}
// Trim spaces trailing on every line of text.
function trim(text) {
return text.replace(RE_TRAILING_SPACES, "");
}
}
}