@formatjs/icu-messageformat-parser 2.0.12
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.
- package/LICENSE.md +9 -0
- package/README.md +25 -0
- package/error.d.ts +69 -0
- package/error.d.ts.map +1 -0
- package/error.js +66 -0
- package/index.d.ts +5 -0
- package/index.d.ts.map +1 -0
- package/index.js +47 -0
- package/lib/error.d.ts +69 -0
- package/lib/error.d.ts.map +1 -0
- package/lib/error.js +63 -0
- package/lib/index.d.ts +5 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +43 -0
- package/lib/manipulator.d.ts +14 -0
- package/lib/manipulator.d.ts.map +1 -0
- package/lib/manipulator.js +52 -0
- package/lib/no-parser.d.ts +3 -0
- package/lib/no-parser.d.ts.map +1 -0
- package/lib/no-parser.js +4 -0
- package/lib/parser.d.ts +143 -0
- package/lib/parser.d.ts.map +1 -0
- package/lib/parser.js +1267 -0
- package/lib/printer.d.ts +5 -0
- package/lib/printer.d.ts.map +1 -0
- package/lib/printer.js +91 -0
- package/lib/regex.generated.d.ts +3 -0
- package/lib/regex.generated.d.ts.map +1 -0
- package/lib/regex.generated.js +3 -0
- package/lib/types.d.ts +129 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +94 -0
- package/manipulator.d.ts +14 -0
- package/manipulator.d.ts.map +1 -0
- package/manipulator.js +56 -0
- package/no-parser.d.ts +3 -0
- package/no-parser.d.ts.map +1 -0
- package/no-parser.js +9 -0
- package/package.json +13 -0
- package/parser.d.ts +143 -0
- package/parser.d.ts.map +1 -0
- package/parser.js +1270 -0
- package/printer.d.ts +5 -0
- package/printer.d.ts.map +1 -0
- package/printer.js +97 -0
- package/regex.generated.d.ts +3 -0
- package/regex.generated.d.ts.map +1 -0
- package/regex.generated.js +6 -0
- package/types.d.ts +129 -0
- package/types.d.ts.map +1 -0
- package/types.js +110 -0
package/lib/printer.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { MessageFormatElement, DateTimeSkeleton } from './types';
|
|
2
|
+
export declare function printAST(ast: MessageFormatElement[]): string;
|
|
3
|
+
export declare function doPrintAST(ast: MessageFormatElement[], isInPlural: boolean): string;
|
|
4
|
+
export declare function printDateTimeSkeleton(style: DateTimeSkeleton): string;
|
|
5
|
+
//# sourceMappingURL=printer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"printer.d.ts","sourceRoot":"","sources":["../../../../../../packages/icu-messageformat-parser/printer.ts"],"names":[],"mappings":"AACA,OAAO,EACL,oBAAoB,EAoBpB,gBAAgB,EAEjB,MAAM,SAAS,CAAA;AAEhB,wBAAgB,QAAQ,CAAC,GAAG,EAAE,oBAAoB,EAAE,GAAG,MAAM,CAE5D;AAED,wBAAgB,UAAU,CACxB,GAAG,EAAE,oBAAoB,EAAE,EAC3B,UAAU,EAAE,OAAO,GAClB,MAAM,CA8BR;AA4CD,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,gBAAgB,GAAG,MAAM,CAErE"}
|
package/lib/printer.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { __spreadArray } from "tslib";
|
|
2
|
+
import { isLiteralElement, isTagElement, isSelectElement, isArgumentElement, isDateElement, isTimeElement, isNumberElement, isPluralElement, TYPE, SKELETON_TYPE, isPoundElement, } from './types';
|
|
3
|
+
export function printAST(ast) {
|
|
4
|
+
return doPrintAST(ast, false);
|
|
5
|
+
}
|
|
6
|
+
export function doPrintAST(ast, isInPlural) {
|
|
7
|
+
var printedNodes = ast.map(function (el) {
|
|
8
|
+
if (isLiteralElement(el)) {
|
|
9
|
+
return printLiteralElement(el, isInPlural);
|
|
10
|
+
}
|
|
11
|
+
if (isArgumentElement(el)) {
|
|
12
|
+
return printArgumentElement(el);
|
|
13
|
+
}
|
|
14
|
+
if (isDateElement(el) || isTimeElement(el) || isNumberElement(el)) {
|
|
15
|
+
return printSimpleFormatElement(el);
|
|
16
|
+
}
|
|
17
|
+
if (isPluralElement(el)) {
|
|
18
|
+
return printPluralElement(el);
|
|
19
|
+
}
|
|
20
|
+
if (isSelectElement(el)) {
|
|
21
|
+
return printSelectElement(el);
|
|
22
|
+
}
|
|
23
|
+
if (isPoundElement(el)) {
|
|
24
|
+
return '#';
|
|
25
|
+
}
|
|
26
|
+
if (isTagElement(el)) {
|
|
27
|
+
return printTagElement(el);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return printedNodes.join('');
|
|
31
|
+
}
|
|
32
|
+
function printTagElement(el) {
|
|
33
|
+
return "<" + el.value + ">" + printAST(el.children) + "</" + el.value + ">";
|
|
34
|
+
}
|
|
35
|
+
function printEscapedMessage(message) {
|
|
36
|
+
return message.replace(/([{}](?:.*[{}])?)/su, "'$1'");
|
|
37
|
+
}
|
|
38
|
+
function printLiteralElement(_a, isInPlural) {
|
|
39
|
+
var value = _a.value;
|
|
40
|
+
var escaped = printEscapedMessage(value);
|
|
41
|
+
return isInPlural ? escaped.replace('#', "'#'") : escaped;
|
|
42
|
+
}
|
|
43
|
+
function printArgumentElement(_a) {
|
|
44
|
+
var value = _a.value;
|
|
45
|
+
return "{" + value + "}";
|
|
46
|
+
}
|
|
47
|
+
function printSimpleFormatElement(el) {
|
|
48
|
+
return "{" + el.value + ", " + TYPE[el.type] + (el.style ? ", " + printArgumentStyle(el.style) : '') + "}";
|
|
49
|
+
}
|
|
50
|
+
function printNumberSkeletonToken(token) {
|
|
51
|
+
var stem = token.stem, options = token.options;
|
|
52
|
+
return options.length === 0
|
|
53
|
+
? stem
|
|
54
|
+
: "" + stem + options.map(function (o) { return "/" + o; }).join('');
|
|
55
|
+
}
|
|
56
|
+
function printArgumentStyle(style) {
|
|
57
|
+
if (typeof style === 'string') {
|
|
58
|
+
return printEscapedMessage(style);
|
|
59
|
+
}
|
|
60
|
+
else if (style.type === SKELETON_TYPE.dateTime) {
|
|
61
|
+
return "::" + printDateTimeSkeleton(style);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
return "::" + style.tokens.map(printNumberSkeletonToken).join(' ');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function printDateTimeSkeleton(style) {
|
|
68
|
+
return style.pattern;
|
|
69
|
+
}
|
|
70
|
+
function printSelectElement(el) {
|
|
71
|
+
var msg = [
|
|
72
|
+
el.value,
|
|
73
|
+
'select',
|
|
74
|
+
Object.keys(el.options)
|
|
75
|
+
.map(function (id) { return id + "{" + doPrintAST(el.options[id].value, false) + "}"; })
|
|
76
|
+
.join(' '),
|
|
77
|
+
].join(',');
|
|
78
|
+
return "{" + msg + "}";
|
|
79
|
+
}
|
|
80
|
+
function printPluralElement(el) {
|
|
81
|
+
var type = el.pluralType === 'cardinal' ? 'plural' : 'selectordinal';
|
|
82
|
+
var msg = [
|
|
83
|
+
el.value,
|
|
84
|
+
type,
|
|
85
|
+
__spreadArray([
|
|
86
|
+
el.offset ? "offset:" + el.offset : ''
|
|
87
|
+
], Object.keys(el.options).map(function (id) { return id + "{" + doPrintAST(el.options[id].value, true) + "}"; })).filter(Boolean)
|
|
88
|
+
.join(' '),
|
|
89
|
+
].join(',');
|
|
90
|
+
return "{" + msg + "}";
|
|
91
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"regex.generated.d.ts","sourceRoot":"","sources":["../../../../../../packages/icu-messageformat-parser/regex.generated.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,qBAAqB,QAAiD,CAAA;AACnF,eAAO,MAAM,iBAAiB,QAAyC,CAAA"}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
2
|
+
import { NumberSkeletonToken } from '@formatjs/icu-skeleton-parser';
|
|
3
|
+
export interface ExtendedNumberFormatOptions extends NumberFormatOptions {
|
|
4
|
+
scale?: number;
|
|
5
|
+
}
|
|
6
|
+
export declare enum TYPE {
|
|
7
|
+
/**
|
|
8
|
+
* Raw text
|
|
9
|
+
*/
|
|
10
|
+
literal = 0,
|
|
11
|
+
/**
|
|
12
|
+
* Variable w/o any format, e.g `var` in `this is a {var}`
|
|
13
|
+
*/
|
|
14
|
+
argument = 1,
|
|
15
|
+
/**
|
|
16
|
+
* Variable w/ number format
|
|
17
|
+
*/
|
|
18
|
+
number = 2,
|
|
19
|
+
/**
|
|
20
|
+
* Variable w/ date format
|
|
21
|
+
*/
|
|
22
|
+
date = 3,
|
|
23
|
+
/**
|
|
24
|
+
* Variable w/ time format
|
|
25
|
+
*/
|
|
26
|
+
time = 4,
|
|
27
|
+
/**
|
|
28
|
+
* Variable w/ select format
|
|
29
|
+
*/
|
|
30
|
+
select = 5,
|
|
31
|
+
/**
|
|
32
|
+
* Variable w/ plural format
|
|
33
|
+
*/
|
|
34
|
+
plural = 6,
|
|
35
|
+
/**
|
|
36
|
+
* Only possible within plural argument.
|
|
37
|
+
* This is the `#` symbol that will be substituted with the count.
|
|
38
|
+
*/
|
|
39
|
+
pound = 7,
|
|
40
|
+
/**
|
|
41
|
+
* XML-like tag
|
|
42
|
+
*/
|
|
43
|
+
tag = 8
|
|
44
|
+
}
|
|
45
|
+
export declare enum SKELETON_TYPE {
|
|
46
|
+
number = 0,
|
|
47
|
+
dateTime = 1
|
|
48
|
+
}
|
|
49
|
+
export interface LocationDetails {
|
|
50
|
+
offset: number;
|
|
51
|
+
line: number;
|
|
52
|
+
column: number;
|
|
53
|
+
}
|
|
54
|
+
export interface Location {
|
|
55
|
+
start: LocationDetails;
|
|
56
|
+
end: LocationDetails;
|
|
57
|
+
}
|
|
58
|
+
export interface BaseElement<T extends TYPE> {
|
|
59
|
+
type: T;
|
|
60
|
+
value: string;
|
|
61
|
+
location?: Location;
|
|
62
|
+
}
|
|
63
|
+
export declare type LiteralElement = BaseElement<TYPE.literal>;
|
|
64
|
+
export declare type ArgumentElement = BaseElement<TYPE.argument>;
|
|
65
|
+
export interface TagElement {
|
|
66
|
+
type: TYPE.tag;
|
|
67
|
+
value: string;
|
|
68
|
+
children: MessageFormatElement[];
|
|
69
|
+
location?: Location;
|
|
70
|
+
}
|
|
71
|
+
export interface SimpleFormatElement<T extends TYPE, S extends Skeleton> extends BaseElement<T> {
|
|
72
|
+
style?: string | S | null;
|
|
73
|
+
}
|
|
74
|
+
export declare type NumberElement = SimpleFormatElement<TYPE.number, NumberSkeleton>;
|
|
75
|
+
export declare type DateElement = SimpleFormatElement<TYPE.date, DateTimeSkeleton>;
|
|
76
|
+
export declare type TimeElement = SimpleFormatElement<TYPE.time, DateTimeSkeleton>;
|
|
77
|
+
export interface SelectOption {
|
|
78
|
+
id: string;
|
|
79
|
+
value: MessageFormatElement[];
|
|
80
|
+
location?: Location;
|
|
81
|
+
}
|
|
82
|
+
export declare type ValidPluralRule = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other' | string;
|
|
83
|
+
export interface PluralOrSelectOption {
|
|
84
|
+
value: MessageFormatElement[];
|
|
85
|
+
location?: Location;
|
|
86
|
+
}
|
|
87
|
+
export interface SelectElement extends BaseElement<TYPE.select> {
|
|
88
|
+
options: Record<string, PluralOrSelectOption>;
|
|
89
|
+
}
|
|
90
|
+
export interface PluralElement extends BaseElement<TYPE.plural> {
|
|
91
|
+
options: Record<ValidPluralRule, PluralOrSelectOption>;
|
|
92
|
+
offset: number;
|
|
93
|
+
pluralType: Intl.PluralRulesOptions['type'];
|
|
94
|
+
}
|
|
95
|
+
export interface PoundElement {
|
|
96
|
+
type: TYPE.pound;
|
|
97
|
+
location?: Location;
|
|
98
|
+
}
|
|
99
|
+
export declare type MessageFormatElement = ArgumentElement | DateElement | LiteralElement | NumberElement | PluralElement | PoundElement | SelectElement | TagElement | TimeElement;
|
|
100
|
+
export interface NumberSkeleton {
|
|
101
|
+
type: SKELETON_TYPE.number;
|
|
102
|
+
tokens: NumberSkeletonToken[];
|
|
103
|
+
location?: Location;
|
|
104
|
+
parsedOptions: ExtendedNumberFormatOptions;
|
|
105
|
+
}
|
|
106
|
+
export interface DateTimeSkeleton {
|
|
107
|
+
type: SKELETON_TYPE.dateTime;
|
|
108
|
+
pattern: string;
|
|
109
|
+
location?: Location;
|
|
110
|
+
parsedOptions: Intl.DateTimeFormatOptions;
|
|
111
|
+
}
|
|
112
|
+
export declare type Skeleton = NumberSkeleton | DateTimeSkeleton;
|
|
113
|
+
/**
|
|
114
|
+
* Type Guards
|
|
115
|
+
*/
|
|
116
|
+
export declare function isLiteralElement(el: MessageFormatElement): el is LiteralElement;
|
|
117
|
+
export declare function isArgumentElement(el: MessageFormatElement): el is ArgumentElement;
|
|
118
|
+
export declare function isNumberElement(el: MessageFormatElement): el is NumberElement;
|
|
119
|
+
export declare function isDateElement(el: MessageFormatElement): el is DateElement;
|
|
120
|
+
export declare function isTimeElement(el: MessageFormatElement): el is TimeElement;
|
|
121
|
+
export declare function isSelectElement(el: MessageFormatElement): el is SelectElement;
|
|
122
|
+
export declare function isPluralElement(el: MessageFormatElement): el is PluralElement;
|
|
123
|
+
export declare function isPoundElement(el: MessageFormatElement): el is PoundElement;
|
|
124
|
+
export declare function isTagElement(el: MessageFormatElement): el is TagElement;
|
|
125
|
+
export declare function isNumberSkeleton(el: NumberElement['style'] | Skeleton): el is NumberSkeleton;
|
|
126
|
+
export declare function isDateTimeSkeleton(el?: DateElement['style'] | TimeElement['style'] | Skeleton): el is DateTimeSkeleton;
|
|
127
|
+
export declare function createLiteralElement(value: string): LiteralElement;
|
|
128
|
+
export declare function createNumberElement(value: string, style?: string | null): NumberElement;
|
|
129
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../../packages/icu-messageformat-parser/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,4BAA4B,CAAA;AACnE,OAAO,EAAC,mBAAmB,EAAC,MAAM,+BAA+B,CAAA;AAEjE,MAAM,WAAW,2BAA4B,S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,cAAc,CAEtB;AACD,wBAAgB,kBAAkB,CAChC,EAAE,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,GAAG,WAAW,CAAC,OAAO,CAAC,GAAG,QAAQ,GAC1D,EAAE,IAAI,gBAAgB,CAExB;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc,CAKlE;AAED,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,EACb,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,GACpB,aAAa,CAMf"}
|
package/lib/types.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
export var TYPE;
|
|
2
|
+
(function (TYPE) {
|
|
3
|
+
/**
|
|
4
|
+
* Raw text
|
|
5
|
+
*/
|
|
6
|
+
TYPE[TYPE["literal"] = 0] = "literal";
|
|
7
|
+
/**
|
|
8
|
+
* Variable w/o any format, e.g `var` in `this is a {var}`
|
|
9
|
+
*/
|
|
10
|
+
TYPE[TYPE["argument"] = 1] = "argument";
|
|
11
|
+
/**
|
|
12
|
+
* Variable w/ number format
|
|
13
|
+
*/
|
|
14
|
+
TYPE[TYPE["number"] = 2] = "number";
|
|
15
|
+
/**
|
|
16
|
+
* Variable w/ date format
|
|
17
|
+
*/
|
|
18
|
+
TYPE[TYPE["date"] = 3] = "date";
|
|
19
|
+
/**
|
|
20
|
+
* Variable w/ time format
|
|
21
|
+
*/
|
|
22
|
+
TYPE[TYPE["time"] = 4] = "time";
|
|
23
|
+
/**
|
|
24
|
+
* Variable w/ select format
|
|
25
|
+
*/
|
|
26
|
+
TYPE[TYPE["select"] = 5] = "select";
|
|
27
|
+
/**
|
|
28
|
+
* Variable w/ plural format
|
|
29
|
+
*/
|
|
30
|
+
TYPE[TYPE["plural"] = 6] = "plural";
|
|
31
|
+
/**
|
|
32
|
+
* Only possible within plural argument.
|
|
33
|
+
* This is the `#` symbol that will be substituted with the count.
|
|
34
|
+
*/
|
|
35
|
+
TYPE[TYPE["pound"] = 7] = "pound";
|
|
36
|
+
/**
|
|
37
|
+
* XML-like tag
|
|
38
|
+
*/
|
|
39
|
+
TYPE[TYPE["tag"] = 8] = "tag";
|
|
40
|
+
})(TYPE || (TYPE = {}));
|
|
41
|
+
export var SKELETON_TYPE;
|
|
42
|
+
(function (SKELETON_TYPE) {
|
|
43
|
+
SKELETON_TYPE[SKELETON_TYPE["number"] = 0] = "number";
|
|
44
|
+
SKELETON_TYPE[SKELETON_TYPE["dateTime"] = 1] = "dateTime";
|
|
45
|
+
})(SKELETON_TYPE || (SKELETON_TYPE = {}));
|
|
46
|
+
/**
|
|
47
|
+
* Type Guards
|
|
48
|
+
*/
|
|
49
|
+
export function isLiteralElement(el) {
|
|
50
|
+
return el.type === TYPE.literal;
|
|
51
|
+
}
|
|
52
|
+
export function isArgumentElement(el) {
|
|
53
|
+
return el.type === TYPE.argument;
|
|
54
|
+
}
|
|
55
|
+
export function isNumberElement(el) {
|
|
56
|
+
return el.type === TYPE.number;
|
|
57
|
+
}
|
|
58
|
+
export function isDateElement(el) {
|
|
59
|
+
return el.type === TYPE.date;
|
|
60
|
+
}
|
|
61
|
+
export function isTimeElement(el) {
|
|
62
|
+
return el.type === TYPE.time;
|
|
63
|
+
}
|
|
64
|
+
export function isSelectElement(el) {
|
|
65
|
+
return el.type === TYPE.select;
|
|
66
|
+
}
|
|
67
|
+
export function isPluralElement(el) {
|
|
68
|
+
return el.type === TYPE.plural;
|
|
69
|
+
}
|
|
70
|
+
export function isPoundElement(el) {
|
|
71
|
+
return el.type === TYPE.pound;
|
|
72
|
+
}
|
|
73
|
+
export function isTagElement(el) {
|
|
74
|
+
return el.type === TYPE.tag;
|
|
75
|
+
}
|
|
76
|
+
export function isNumberSkeleton(el) {
|
|
77
|
+
return !!(el && typeof el === 'object' && el.type === SKELETON_TYPE.number);
|
|
78
|
+
}
|
|
79
|
+
export function isDateTimeSkeleton(el) {
|
|
80
|
+
return !!(el && typeof el === 'object' && el.type === SKELETON_TYPE.dateTime);
|
|
81
|
+
}
|
|
82
|
+
export function createLiteralElement(value) {
|
|
83
|
+
return {
|
|
84
|
+
type: TYPE.literal,
|
|
85
|
+
value: value,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export function createNumberElement(value, style) {
|
|
89
|
+
return {
|
|
90
|
+
type: TYPE.number,
|
|
91
|
+
value: value,
|
|
92
|
+
style: style,
|
|
93
|
+
};
|
|
94
|
+
}
|
package/manipulator.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { MessageFormatElement } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Hoist all selectors to the beginning of the AST & flatten the
|
|
4
|
+
* resulting options. E.g:
|
|
5
|
+
* "I have {count, plural, one{a dog} other{many dogs}}"
|
|
6
|
+
* becomes "{count, plural, one{I have a dog} other{I have many dogs}}".
|
|
7
|
+
* If there are multiple selectors, the order of which one is hoisted 1st
|
|
8
|
+
* is non-deterministic.
|
|
9
|
+
* The goal is to provide as many full sentences as possible since fragmented
|
|
10
|
+
* sentences are not translator-friendly
|
|
11
|
+
* @param ast AST
|
|
12
|
+
*/
|
|
13
|
+
export declare function hoistSelectors(ast: MessageFormatElement[]): MessageFormatElement[];
|
|
14
|
+
//# sourceMappingURL=manipulator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manipulator.d.ts","sourceRoot":"","sources":["../../../../../packages/icu-messageformat-parser/manipulator.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,oBAAoB,EAErB,MAAM,SAAS,CAAA;AAkBhB;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,oBAAoB,EAAE,GAC1B,oBAAoB,EAAE,CAyBxB"}
|
package/manipulator.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hoistSelectors = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var types_1 = require("./types");
|
|
6
|
+
function cloneDeep(obj) {
|
|
7
|
+
if (Array.isArray(obj)) {
|
|
8
|
+
// @ts-expect-error meh
|
|
9
|
+
return tslib_1.__spreadArray([], obj.map(cloneDeep));
|
|
10
|
+
}
|
|
11
|
+
if (typeof obj === 'object') {
|
|
12
|
+
// @ts-expect-error meh
|
|
13
|
+
return Object.keys(obj).reduce(function (cloned, k) {
|
|
14
|
+
// @ts-expect-error meh
|
|
15
|
+
cloned[k] = cloneDeep(obj[k]);
|
|
16
|
+
return cloned;
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
return obj;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Hoist all selectors to the beginning of the AST & flatten the
|
|
23
|
+
* resulting options. E.g:
|
|
24
|
+
* "I have {count, plural, one{a dog} other{many dogs}}"
|
|
25
|
+
* becomes "{count, plural, one{I have a dog} other{I have many dogs}}".
|
|
26
|
+
* If there are multiple selectors, the order of which one is hoisted 1st
|
|
27
|
+
* is non-deterministic.
|
|
28
|
+
* The goal is to provide as many full sentences as possible since fragmented
|
|
29
|
+
* sentences are not translator-friendly
|
|
30
|
+
* @param ast AST
|
|
31
|
+
*/
|
|
32
|
+
function hoistSelectors(ast) {
|
|
33
|
+
var _loop_1 = function (i) {
|
|
34
|
+
var el = ast[i];
|
|
35
|
+
if (types_1.isPluralElement(el) || types_1.isSelectElement(el)) {
|
|
36
|
+
// pull this out of the ast and move it to the top
|
|
37
|
+
var cloned = cloneDeep(el);
|
|
38
|
+
var options_1 = cloned.options;
|
|
39
|
+
cloned.options = Object.keys(options_1).reduce(function (all, k) {
|
|
40
|
+
var newValue = hoistSelectors(tslib_1.__spreadArray(tslib_1.__spreadArray(tslib_1.__spreadArray([], ast.slice(0, i)), options_1[k].value), ast.slice(i + 1)));
|
|
41
|
+
all[k] = {
|
|
42
|
+
value: newValue,
|
|
43
|
+
};
|
|
44
|
+
return all;
|
|
45
|
+
}, {});
|
|
46
|
+
return { value: [cloned] };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
for (var i = 0; i < ast.length; i++) {
|
|
50
|
+
var state_1 = _loop_1(i);
|
|
51
|
+
if (typeof state_1 === "object")
|
|
52
|
+
return state_1.value;
|
|
53
|
+
}
|
|
54
|
+
return ast;
|
|
55
|
+
}
|
|
56
|
+
exports.hoistSelectors = hoistSelectors;
|
package/no-parser.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-parser.d.ts","sourceRoot":"","sources":["../../../../../packages/icu-messageformat-parser/no-parser.ts"],"names":[],"mappings":"AAAA,wBAAgB,KAAK,SAIpB;AACD,cAAc,SAAS,CAAA"}
|
package/no-parser.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parse = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
function parse() {
|
|
6
|
+
throw new Error("You're trying to format an uncompiled message with react-intl without parser, please import from 'react-int' instead");
|
|
7
|
+
}
|
|
8
|
+
exports.parse = parse;
|
|
9
|
+
tslib_1.__exportStar(require("./types"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@formatjs/icu-messageformat-parser",
|
|
3
|
+
"version": "2.0.12",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"module": "lib/index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@formatjs/ecma402-abstract": "1.9.9",
|
|
10
|
+
"@formatjs/icu-skeleton-parser": "1.2.13",
|
|
11
|
+
"tslib": "^2.1.0"
|
|
12
|
+
}
|
|
13
|
+
}
|
package/parser.d.ts
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { ParserError } from './error';
|
|
2
|
+
import { MessageFormatElement } from './types';
|
|
3
|
+
export interface Position {
|
|
4
|
+
/** Offset in terms of UTF-16 *code unit*. */
|
|
5
|
+
offset: number;
|
|
6
|
+
line: number;
|
|
7
|
+
/** Column offset in terms of unicode *code point*. */
|
|
8
|
+
column: number;
|
|
9
|
+
}
|
|
10
|
+
export interface ParserOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Whether to treat HTML/XML tags as string literal
|
|
13
|
+
* instead of parsing them as tag token.
|
|
14
|
+
* When this is false we only allow simple tags without
|
|
15
|
+
* any attributes
|
|
16
|
+
*/
|
|
17
|
+
ignoreTag?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Should `select`, `selectordinal`, and `plural` arguments always include
|
|
20
|
+
* the `other` case clause.
|
|
21
|
+
*/
|
|
22
|
+
requiresOtherClause?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Whether to parse number/datetime skeleton
|
|
25
|
+
* into Intl.NumberFormatOptions and Intl.DateTimeFormatOptions, respectively.
|
|
26
|
+
*/
|
|
27
|
+
shouldParseSkeletons?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Capture location info in AST
|
|
30
|
+
* Default is false
|
|
31
|
+
*/
|
|
32
|
+
captureLocation?: boolean;
|
|
33
|
+
}
|
|
34
|
+
export declare type Result<T, E> = {
|
|
35
|
+
val: T;
|
|
36
|
+
err: null;
|
|
37
|
+
} | {
|
|
38
|
+
val: null;
|
|
39
|
+
err: E;
|
|
40
|
+
};
|
|
41
|
+
export declare class Parser {
|
|
42
|
+
private message;
|
|
43
|
+
private position;
|
|
44
|
+
private ignoreTag;
|
|
45
|
+
private requiresOtherClause;
|
|
46
|
+
private shouldParseSkeletons?;
|
|
47
|
+
constructor(message: string, options?: ParserOptions);
|
|
48
|
+
parse(): Result<MessageFormatElement[], ParserError>;
|
|
49
|
+
private parseMessage;
|
|
50
|
+
/**
|
|
51
|
+
* A tag name must start with an ASCII lower/upper case letter. The grammar is based on the
|
|
52
|
+
* [custom element name][] except that a dash is NOT always mandatory and uppercase letters
|
|
53
|
+
* are accepted:
|
|
54
|
+
*
|
|
55
|
+
* ```
|
|
56
|
+
* tag ::= "<" tagName (whitespace)* "/>" | "<" tagName (whitespace)* ">" message "</" tagName (whitespace)* ">"
|
|
57
|
+
* tagName ::= [a-z] (PENChar)*
|
|
58
|
+
* PENChar ::=
|
|
59
|
+
* "-" | "." | [0-9] | "_" | [a-z] | [A-Z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
|
|
60
|
+
* [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
|
|
61
|
+
* [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* [custom element name]: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
|
|
65
|
+
* NOTE: We're a bit more lax here since HTML technically does not allow uppercase HTML element but we do
|
|
66
|
+
* since other tag-based engines like React allow it
|
|
67
|
+
*/
|
|
68
|
+
private parseTag;
|
|
69
|
+
/**
|
|
70
|
+
* This method assumes that the caller has peeked ahead for the first tag character.
|
|
71
|
+
*/
|
|
72
|
+
private parseTagName;
|
|
73
|
+
private parseLiteral;
|
|
74
|
+
tryParseLeftAngleBracket(): string | null;
|
|
75
|
+
/**
|
|
76
|
+
* Starting with ICU 4.8, an ASCII apostrophe only starts quoted text if it immediately precedes
|
|
77
|
+
* a character that requires quoting (that is, "only where needed"), and works the same in
|
|
78
|
+
* nested messages as on the top level of the pattern. The new behavior is otherwise compatible.
|
|
79
|
+
*/
|
|
80
|
+
private tryParseQuote;
|
|
81
|
+
private tryParseUnquoted;
|
|
82
|
+
private parseArgument;
|
|
83
|
+
/**
|
|
84
|
+
* Advance the parser until the end of the identifier, if it is currently on
|
|
85
|
+
* an identifier character. Return an empty string otherwise.
|
|
86
|
+
*/
|
|
87
|
+
private parseIdentifierIfPossible;
|
|
88
|
+
private parseArgumentOptions;
|
|
89
|
+
private tryParseArgumentClose;
|
|
90
|
+
/**
|
|
91
|
+
* See: https://github.com/unicode-org/icu/blob/af7ed1f6d2298013dc303628438ec4abe1f16479/icu4c/source/common/messagepattern.cpp#L659
|
|
92
|
+
*/
|
|
93
|
+
private parseSimpleArgStyleIfPossible;
|
|
94
|
+
private parseNumberSkeletonFromString;
|
|
95
|
+
/**
|
|
96
|
+
* @param nesting_level The current nesting level of messages.
|
|
97
|
+
* This can be positive when parsing message fragment in select or plural argument options.
|
|
98
|
+
* @param parent_arg_type The parent argument's type.
|
|
99
|
+
* @param parsed_first_identifier If provided, this is the first identifier-like selector of
|
|
100
|
+
* the argument. It is a by-product of a previous parsing attempt.
|
|
101
|
+
* @param expecting_close_tag If true, this message is directly or indirectly nested inside
|
|
102
|
+
* between a pair of opening and closing tags. The nested message will not parse beyond
|
|
103
|
+
* the closing tag boundary.
|
|
104
|
+
*/
|
|
105
|
+
private tryParsePluralOrSelectOptions;
|
|
106
|
+
private tryParseDecimalInteger;
|
|
107
|
+
private offset;
|
|
108
|
+
private isEOF;
|
|
109
|
+
private clonePosition;
|
|
110
|
+
/**
|
|
111
|
+
* Return the code point at the current position of the parser.
|
|
112
|
+
* Throws if the index is out of bound.
|
|
113
|
+
*/
|
|
114
|
+
private char;
|
|
115
|
+
private error;
|
|
116
|
+
/** Bump the parser to the next UTF-16 code unit. */
|
|
117
|
+
private bump;
|
|
118
|
+
/**
|
|
119
|
+
* If the substring starting at the current position of the parser has
|
|
120
|
+
* the given prefix, then bump the parser to the character immediately
|
|
121
|
+
* following the prefix and return true. Otherwise, don't bump the parser
|
|
122
|
+
* and return false.
|
|
123
|
+
*/
|
|
124
|
+
private bumpIf;
|
|
125
|
+
/**
|
|
126
|
+
* Bump the parser until the pattern character is found and return `true`.
|
|
127
|
+
* Otherwise bump to the end of the file and return `false`.
|
|
128
|
+
*/
|
|
129
|
+
private bumpUntil;
|
|
130
|
+
/**
|
|
131
|
+
* Bump the parser to the target offset.
|
|
132
|
+
* If target offset is beyond the end of the input, bump the parser to the end of the input.
|
|
133
|
+
*/
|
|
134
|
+
private bumpTo;
|
|
135
|
+
/** advance the parser through all whitespace to the next non-whitespace code unit. */
|
|
136
|
+
private bumpSpace;
|
|
137
|
+
/**
|
|
138
|
+
* Peek at the *next* Unicode codepoint in the input without advancing the parser.
|
|
139
|
+
* If the input has been exhausted, then this returns null.
|
|
140
|
+
*/
|
|
141
|
+
private peek;
|
|
142
|
+
}
|
|
143
|
+
//# sourceMappingURL=parser.d.ts.map
|
package/parser.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../../../../packages/icu-messageformat-parser/parser.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,WAAW,EAAC,MAAM,SAAS,CAAA;AAC9C,OAAO,EAIL,oBAAoB,EAMrB,MAAM,SAAS,CAAA;AAgBhB,MAAM,WAAW,QAAQ;IACvB,6CAA6C;IAC7C,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,sDAAsD;IACtD,MAAM,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,aAAa;IAC5B;;;;;OAKG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB;;;OAGG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAA;IAC7B;;;OAGG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAA;CAC1B;AAED,oBAAY,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI;IAAC,GAAG,EAAE,CAAC,CAAC;IAAC,GAAG,EAAE,IAAI,CAAA;CAAC,GAAG;IAAC,GAAG,EAAE,IAAI,CAAC;IAAC,GAAG,EAAE,CAAC,CAAA;CAAC,CAAA;AA+KpE,qBAAa,MAAM;IACjB,OAAO,C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