@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.
Files changed (51) hide show
  1. package/LICENSE.md +9 -0
  2. package/README.md +25 -0
  3. package/error.d.ts +69 -0
  4. package/error.d.ts.map +1 -0
  5. package/error.js +66 -0
  6. package/index.d.ts +5 -0
  7. package/index.d.ts.map +1 -0
  8. package/index.js +47 -0
  9. package/lib/error.d.ts +69 -0
  10. package/lib/error.d.ts.map +1 -0
  11. package/lib/error.js +63 -0
  12. package/lib/index.d.ts +5 -0
  13. package/lib/index.d.ts.map +1 -0
  14. package/lib/index.js +43 -0
  15. package/lib/manipulator.d.ts +14 -0
  16. package/lib/manipulator.d.ts.map +1 -0
  17. package/lib/manipulator.js +52 -0
  18. package/lib/no-parser.d.ts +3 -0
  19. package/lib/no-parser.d.ts.map +1 -0
  20. package/lib/no-parser.js +4 -0
  21. package/lib/parser.d.ts +143 -0
  22. package/lib/parser.d.ts.map +1 -0
  23. package/lib/parser.js +1267 -0
  24. package/lib/printer.d.ts +5 -0
  25. package/lib/printer.d.ts.map +1 -0
  26. package/lib/printer.js +91 -0
  27. package/lib/regex.generated.d.ts +3 -0
  28. package/lib/regex.generated.d.ts.map +1 -0
  29. package/lib/regex.generated.js +3 -0
  30. package/lib/types.d.ts +129 -0
  31. package/lib/types.d.ts.map +1 -0
  32. package/lib/types.js +94 -0
  33. package/manipulator.d.ts +14 -0
  34. package/manipulator.d.ts.map +1 -0
  35. package/manipulator.js +56 -0
  36. package/no-parser.d.ts +3 -0
  37. package/no-parser.d.ts.map +1 -0
  38. package/no-parser.js +9 -0
  39. package/package.json +13 -0
  40. package/parser.d.ts +143 -0
  41. package/parser.d.ts.map +1 -0
  42. package/parser.js +1270 -0
  43. package/printer.d.ts +5 -0
  44. package/printer.d.ts.map +1 -0
  45. package/printer.js +97 -0
  46. package/regex.generated.d.ts +3 -0
  47. package/regex.generated.d.ts.map +1 -0
  48. package/regex.generated.js +6 -0
  49. package/types.d.ts +129 -0
  50. package/types.d.ts.map +1 -0
  51. package/types.js +110 -0
package/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 FormatJS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # MessageFormat Parser
2
+
3
+ Hand-written ICU MessageFormat parser with compatible output as
4
+ [`intl-messageformat-parser`](https://www.npmjs.com/package/intl-messageformat-parser)
5
+ but 6 - 10 times as fast.
6
+
7
+ ```
8
+ $ node benchmark
9
+ complex_msg AST length 10861
10
+ normal_msg AST length 1665
11
+ simple_msg AST length 364
12
+ string_msg AST length 131
13
+
14
+ == Baseline ==
15
+ complex_msg x 4,884 ops/sec ±0.97% (91 runs sampled)
16
+ normal_msg x 40,113 ops/sec ±1.08% (92 runs sampled)
17
+ simple_msg x 200,401 ops/sec ±1.12% (91 runs sampled)
18
+ string_msg x 241,103 ops/sec ±0.84% (92 runs sampled)
19
+
20
+ == This package ==
21
+ complex_msg x 31,590 ops/sec ±0.80% (88 runs sampled)
22
+ normal_msg x 278,703 ops/sec ±0.83% (95 runs sampled)
23
+ simple_msg x 2,038,061 ops/sec ±0.90% (96 runs sampled)
24
+ string_msg x 2,392,794 ops/sec ±0.67% (96 runs sampled)
25
+ ```
package/error.d.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { Location } from './types';
2
+ export interface ParserError {
3
+ kind: ErrorKind;
4
+ message: string;
5
+ location: Location;
6
+ }
7
+ export declare enum ErrorKind {
8
+ /** Argument is unclosed (e.g. `{0`) */
9
+ EXPECT_ARGUMENT_CLOSING_BRACE = 1,
10
+ /** Argument is empty (e.g. `{}`). */
11
+ EMPTY_ARGUMENT = 2,
12
+ /** Argument is malformed (e.g. `{foo!}``) */
13
+ MALFORMED_ARGUMENT = 3,
14
+ /** Expect an argument type (e.g. `{foo,}`) */
15
+ EXPECT_ARGUMENT_TYPE = 4,
16
+ /** Unsupported argument type (e.g. `{foo,foo}`) */
17
+ INVALID_ARGUMENT_TYPE = 5,
18
+ /** Expect an argument style (e.g. `{foo, number, }`) */
19
+ EXPECT_ARGUMENT_STYLE = 6,
20
+ /** The number skeleton is invalid. */
21
+ INVALID_NUMBER_SKELETON = 7,
22
+ /** The date time skeleton is invalid. */
23
+ INVALID_DATE_TIME_SKELETON = 8,
24
+ /** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
25
+ EXPECT_NUMBER_SKELETON = 9,
26
+ /** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
27
+ EXPECT_DATE_TIME_SKELETON = 10,
28
+ /** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
29
+ UNCLOSED_QUOTE_IN_ARGUMENT_STYLE = 11,
30
+ /** Missing select argument options (e.g. `{foo, select}`) */
31
+ EXPECT_SELECT_ARGUMENT_OPTIONS = 12,
32
+ /** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
33
+ EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE = 13,
34
+ /** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
35
+ INVALID_PLURAL_ARGUMENT_OFFSET_VALUE = 14,
36
+ /** Expecting a selector in `select` argument (e.g `{foo, select}`) */
37
+ EXPECT_SELECT_ARGUMENT_SELECTOR = 15,
38
+ /** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
39
+ EXPECT_PLURAL_ARGUMENT_SELECTOR = 16,
40
+ /** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
41
+ EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT = 17,
42
+ /**
43
+ * Expecting a message fragment after the `plural` or `selectordinal` selector
44
+ * (e.g. `{foo, plural, one}`)
45
+ */
46
+ EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT = 18,
47
+ /** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
48
+ INVALID_PLURAL_ARGUMENT_SELECTOR = 19,
49
+ /**
50
+ * Duplicate selectors in `plural` or `selectordinal` argument.
51
+ * (e.g. {foo, plural, one {#} one {#}})
52
+ */
53
+ DUPLICATE_PLURAL_ARGUMENT_SELECTOR = 20,
54
+ /** Duplicate selectors in `select` argument.
55
+ * (e.g. {foo, select, apple {apple} apple {apple}})
56
+ */
57
+ DUPLICATE_SELECT_ARGUMENT_SELECTOR = 21,
58
+ /** Plural or select argument option must have `other` clause. */
59
+ MISSING_OTHER_CLAUSE = 22,
60
+ /** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
61
+ INVALID_TAG = 23,
62
+ /** The tag name is invalid. (e.g. `<123>foo</123>`) */
63
+ INVALID_TAG_NAME = 25,
64
+ /** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
65
+ UNMATCHED_CLOSING_TAG = 26,
66
+ /** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
67
+ UNCLOSED_TAG = 27
68
+ }
69
+ //# sourceMappingURL=error.d.ts.map
package/error.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../../../../packages/icu-messageformat-parser/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,SAAS,CAAA;AAEhC,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,SAAS,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,QAAQ,CAAA;CACnB;AAED,oBAAY,SAAS;IACnB,uCAAuC;IACvC,6BAA6B,IAAI;IACjC,qCAAqC;IACrC,cAAc,IAAI;IAClB,6CAA6C;IAC7C,kBAAkB,IAAI;IACtB,8CAA8C;IAC9C,oBAAoB,IAAI;IACxB,mDAAmD;IACnD,qBAAqB,IAAI;IACzB,wDAAwD;IACxD,qBAAqB,IAAI;IACzB,sCAAsC;IACtC,uBAAuB,IAAI;IAC3B,yCAAyC;IACzC,0BAA0B,IAAI;IAC9B,6EAA6E;IAC7E,sBAAsB,IAAI;IAC1B,8EAA8E;IAC9E,yBAAyB,KAAK;IAC9B,+EAA+E;IAC/E,gCAAgC,KAAK;IACrC,6DAA6D;IAC7D,8BAA8B,KAAK;IAEnC,sGAAsG;IACtG,mCAAmC,KAAK;IACxC,+FAA+F;IAC/F,oCAAoC,KAAK;IAEzC,sEAAsE;IACtE,+BAA+B,KAAK;IACpC,yFAAyF;IACzF,+BAA+B,KAAK;IAEpC,6FAA6F;IAC7F,wCAAwC,KAAK;IAC7C;;;OAGG;IACH,wCAAwC,KAAK;IAE7C,0FAA0F;IAC1F,gCAAgC,KAAK;IAErC;;;OAGG;IACH,kCAAkC,KAAK;IACvC;;OAEG;IACH,kCAAkC,KAAK;IAEvC,iEAAiE;IACjE,oBAAoB,KAAK;IAEzB,uDAAuD;IACvD,WAAW,KAAK;IAChB,uDAAuD;IACvD,gBAAgB,KAAK;IACrB,kFAAkF;IAClF,qBAAqB,KAAK;IAC1B,oEAAoE;IACpE,YAAY,KAAK;CAClB"}
package/error.js ADDED
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorKind = void 0;
4
+ var ErrorKind;
5
+ (function (ErrorKind) {
6
+ /** Argument is unclosed (e.g. `{0`) */
7
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_CLOSING_BRACE"] = 1] = "EXPECT_ARGUMENT_CLOSING_BRACE";
8
+ /** Argument is empty (e.g. `{}`). */
9
+ ErrorKind[ErrorKind["EMPTY_ARGUMENT"] = 2] = "EMPTY_ARGUMENT";
10
+ /** Argument is malformed (e.g. `{foo!}``) */
11
+ ErrorKind[ErrorKind["MALFORMED_ARGUMENT"] = 3] = "MALFORMED_ARGUMENT";
12
+ /** Expect an argument type (e.g. `{foo,}`) */
13
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_TYPE"] = 4] = "EXPECT_ARGUMENT_TYPE";
14
+ /** Unsupported argument type (e.g. `{foo,foo}`) */
15
+ ErrorKind[ErrorKind["INVALID_ARGUMENT_TYPE"] = 5] = "INVALID_ARGUMENT_TYPE";
16
+ /** Expect an argument style (e.g. `{foo, number, }`) */
17
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_STYLE"] = 6] = "EXPECT_ARGUMENT_STYLE";
18
+ /** The number skeleton is invalid. */
19
+ ErrorKind[ErrorKind["INVALID_NUMBER_SKELETON"] = 7] = "INVALID_NUMBER_SKELETON";
20
+ /** The date time skeleton is invalid. */
21
+ ErrorKind[ErrorKind["INVALID_DATE_TIME_SKELETON"] = 8] = "INVALID_DATE_TIME_SKELETON";
22
+ /** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
23
+ ErrorKind[ErrorKind["EXPECT_NUMBER_SKELETON"] = 9] = "EXPECT_NUMBER_SKELETON";
24
+ /** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
25
+ ErrorKind[ErrorKind["EXPECT_DATE_TIME_SKELETON"] = 10] = "EXPECT_DATE_TIME_SKELETON";
26
+ /** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
27
+ ErrorKind[ErrorKind["UNCLOSED_QUOTE_IN_ARGUMENT_STYLE"] = 11] = "UNCLOSED_QUOTE_IN_ARGUMENT_STYLE";
28
+ /** Missing select argument options (e.g. `{foo, select}`) */
29
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_OPTIONS"] = 12] = "EXPECT_SELECT_ARGUMENT_OPTIONS";
30
+ /** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
31
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE"] = 13] = "EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE";
32
+ /** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
33
+ ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_OFFSET_VALUE"] = 14] = "INVALID_PLURAL_ARGUMENT_OFFSET_VALUE";
34
+ /** Expecting a selector in `select` argument (e.g `{foo, select}`) */
35
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR"] = 15] = "EXPECT_SELECT_ARGUMENT_SELECTOR";
36
+ /** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
37
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR"] = 16] = "EXPECT_PLURAL_ARGUMENT_SELECTOR";
38
+ /** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
39
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT"] = 17] = "EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT";
40
+ /**
41
+ * Expecting a message fragment after the `plural` or `selectordinal` selector
42
+ * (e.g. `{foo, plural, one}`)
43
+ */
44
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT"] = 18] = "EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT";
45
+ /** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
46
+ ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_SELECTOR"] = 19] = "INVALID_PLURAL_ARGUMENT_SELECTOR";
47
+ /**
48
+ * Duplicate selectors in `plural` or `selectordinal` argument.
49
+ * (e.g. {foo, plural, one {#} one {#}})
50
+ */
51
+ ErrorKind[ErrorKind["DUPLICATE_PLURAL_ARGUMENT_SELECTOR"] = 20] = "DUPLICATE_PLURAL_ARGUMENT_SELECTOR";
52
+ /** Duplicate selectors in `select` argument.
53
+ * (e.g. {foo, select, apple {apple} apple {apple}})
54
+ */
55
+ ErrorKind[ErrorKind["DUPLICATE_SELECT_ARGUMENT_SELECTOR"] = 21] = "DUPLICATE_SELECT_ARGUMENT_SELECTOR";
56
+ /** Plural or select argument option must have `other` clause. */
57
+ ErrorKind[ErrorKind["MISSING_OTHER_CLAUSE"] = 22] = "MISSING_OTHER_CLAUSE";
58
+ /** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
59
+ ErrorKind[ErrorKind["INVALID_TAG"] = 23] = "INVALID_TAG";
60
+ /** The tag name is invalid. (e.g. `<123>foo</123>`) */
61
+ ErrorKind[ErrorKind["INVALID_TAG_NAME"] = 25] = "INVALID_TAG_NAME";
62
+ /** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
63
+ ErrorKind[ErrorKind["UNMATCHED_CLOSING_TAG"] = 26] = "UNMATCHED_CLOSING_TAG";
64
+ /** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
65
+ ErrorKind[ErrorKind["UNCLOSED_TAG"] = 27] = "UNCLOSED_TAG";
66
+ })(ErrorKind = exports.ErrorKind || (exports.ErrorKind = {}));
package/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { ParserOptions } from './parser';
2
+ import { MessageFormatElement } from './types';
3
+ export declare function parse(message: string, opts?: ParserOptions): MessageFormatElement[];
4
+ export * from './types';
5
+ //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../packages/icu-messageformat-parser/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,aAAa,EAAC,MAAM,UAAU,CAAA;AAC9C,OAAO,EASL,oBAAoB,EACrB,MAAM,SAAS,CAAA;AAuBhB,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,GAAE,aAAkB,0BAoB9D;AACD,cAAc,SAAS,CAAA"}
package/index.js ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parse = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var error_1 = require("./error");
6
+ var parser_1 = require("./parser");
7
+ var types_1 = require("./types");
8
+ function pruneLocation(els) {
9
+ els.forEach(function (el) {
10
+ delete el.location;
11
+ if (types_1.isSelectElement(el) || types_1.isPluralElement(el)) {
12
+ for (var k in el.options) {
13
+ delete el.options[k].location;
14
+ pruneLocation(el.options[k].value);
15
+ }
16
+ }
17
+ else if (types_1.isNumberElement(el) && types_1.isNumberSkeleton(el.style)) {
18
+ delete el.style.location;
19
+ }
20
+ else if ((types_1.isDateElement(el) || types_1.isTimeElement(el)) &&
21
+ types_1.isDateTimeSkeleton(el.style)) {
22
+ delete el.style.location;
23
+ }
24
+ else if (types_1.isTagElement(el)) {
25
+ pruneLocation(el.children);
26
+ }
27
+ });
28
+ }
29
+ function parse(message, opts) {
30
+ if (opts === void 0) { opts = {}; }
31
+ opts = tslib_1.__assign({ shouldParseSkeletons: true, requiresOtherClause: true }, opts);
32
+ var result = new parser_1.Parser(message, opts).parse();
33
+ if (result.err) {
34
+ var error = SyntaxError(error_1.ErrorKind[result.err.kind]);
35
+ // @ts-expect-error Assign to error object
36
+ error.location = result.err.location;
37
+ // @ts-expect-error Assign to error object
38
+ error.originalMessage = result.err.message;
39
+ throw error;
40
+ }
41
+ if (!(opts === null || opts === void 0 ? void 0 : opts.captureLocation)) {
42
+ pruneLocation(result.val);
43
+ }
44
+ return result.val;
45
+ }
46
+ exports.parse = parse;
47
+ tslib_1.__exportStar(require("./types"), exports);
package/lib/error.d.ts ADDED
@@ -0,0 +1,69 @@
1
+ import { Location } from './types';
2
+ export interface ParserError {
3
+ kind: ErrorKind;
4
+ message: string;
5
+ location: Location;
6
+ }
7
+ export declare enum ErrorKind {
8
+ /** Argument is unclosed (e.g. `{0`) */
9
+ EXPECT_ARGUMENT_CLOSING_BRACE = 1,
10
+ /** Argument is empty (e.g. `{}`). */
11
+ EMPTY_ARGUMENT = 2,
12
+ /** Argument is malformed (e.g. `{foo!}``) */
13
+ MALFORMED_ARGUMENT = 3,
14
+ /** Expect an argument type (e.g. `{foo,}`) */
15
+ EXPECT_ARGUMENT_TYPE = 4,
16
+ /** Unsupported argument type (e.g. `{foo,foo}`) */
17
+ INVALID_ARGUMENT_TYPE = 5,
18
+ /** Expect an argument style (e.g. `{foo, number, }`) */
19
+ EXPECT_ARGUMENT_STYLE = 6,
20
+ /** The number skeleton is invalid. */
21
+ INVALID_NUMBER_SKELETON = 7,
22
+ /** The date time skeleton is invalid. */
23
+ INVALID_DATE_TIME_SKELETON = 8,
24
+ /** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
25
+ EXPECT_NUMBER_SKELETON = 9,
26
+ /** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
27
+ EXPECT_DATE_TIME_SKELETON = 10,
28
+ /** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
29
+ UNCLOSED_QUOTE_IN_ARGUMENT_STYLE = 11,
30
+ /** Missing select argument options (e.g. `{foo, select}`) */
31
+ EXPECT_SELECT_ARGUMENT_OPTIONS = 12,
32
+ /** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
33
+ EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE = 13,
34
+ /** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
35
+ INVALID_PLURAL_ARGUMENT_OFFSET_VALUE = 14,
36
+ /** Expecting a selector in `select` argument (e.g `{foo, select}`) */
37
+ EXPECT_SELECT_ARGUMENT_SELECTOR = 15,
38
+ /** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
39
+ EXPECT_PLURAL_ARGUMENT_SELECTOR = 16,
40
+ /** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
41
+ EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT = 17,
42
+ /**
43
+ * Expecting a message fragment after the `plural` or `selectordinal` selector
44
+ * (e.g. `{foo, plural, one}`)
45
+ */
46
+ EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT = 18,
47
+ /** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
48
+ INVALID_PLURAL_ARGUMENT_SELECTOR = 19,
49
+ /**
50
+ * Duplicate selectors in `plural` or `selectordinal` argument.
51
+ * (e.g. {foo, plural, one {#} one {#}})
52
+ */
53
+ DUPLICATE_PLURAL_ARGUMENT_SELECTOR = 20,
54
+ /** Duplicate selectors in `select` argument.
55
+ * (e.g. {foo, select, apple {apple} apple {apple}})
56
+ */
57
+ DUPLICATE_SELECT_ARGUMENT_SELECTOR = 21,
58
+ /** Plural or select argument option must have `other` clause. */
59
+ MISSING_OTHER_CLAUSE = 22,
60
+ /** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
61
+ INVALID_TAG = 23,
62
+ /** The tag name is invalid. (e.g. `<123>foo</123>`) */
63
+ INVALID_TAG_NAME = 25,
64
+ /** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
65
+ UNMATCHED_CLOSING_TAG = 26,
66
+ /** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
67
+ UNCLOSED_TAG = 27
68
+ }
69
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../../../../../packages/icu-messageformat-parser/error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAC,MAAM,SAAS,CAAA;AAEhC,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,SAAS,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,QAAQ,CAAA;CACnB;AAED,oBAAY,SAAS;IACnB,uCAAuC;IACvC,6BAA6B,IAAI;IACjC,qCAAqC;IACrC,cAAc,IAAI;IAClB,6CAA6C;IAC7C,kBAAkB,IAAI;IACtB,8CAA8C;IAC9C,oBAAoB,IAAI;IACxB,mDAAmD;IACnD,qBAAqB,IAAI;IACzB,wDAAwD;IACxD,qBAAqB,IAAI;IACzB,sCAAsC;IACtC,uBAAuB,IAAI;IAC3B,yCAAyC;IACzC,0BAA0B,IAAI;IAC9B,6EAA6E;IAC7E,sBAAsB,IAAI;IAC1B,8EAA8E;IAC9E,yBAAyB,KAAK;IAC9B,+EAA+E;IAC/E,gCAAgC,KAAK;IACrC,6DAA6D;IAC7D,8BAA8B,KAAK;IAEnC,sGAAsG;IACtG,mCAAmC,KAAK;IACxC,+FAA+F;IAC/F,oCAAoC,KAAK;IAEzC,sEAAsE;IACtE,+BAA+B,KAAK;IACpC,yFAAyF;IACzF,+BAA+B,KAAK;IAEpC,6FAA6F;IAC7F,wCAAwC,KAAK;IAC7C;;;OAGG;IACH,wCAAwC,KAAK;IAE7C,0FAA0F;IAC1F,gCAAgC,KAAK;IAErC;;;OAGG;IACH,kCAAkC,KAAK;IACvC;;OAEG;IACH,kCAAkC,KAAK;IAEvC,iEAAiE;IACjE,oBAAoB,KAAK;IAEzB,uDAAuD;IACvD,WAAW,KAAK;IAChB,uDAAuD;IACvD,gBAAgB,KAAK;IACrB,kFAAkF;IAClF,qBAAqB,KAAK;IAC1B,oEAAoE;IACpE,YAAY,KAAK;CAClB"}
package/lib/error.js ADDED
@@ -0,0 +1,63 @@
1
+ export var ErrorKind;
2
+ (function (ErrorKind) {
3
+ /** Argument is unclosed (e.g. `{0`) */
4
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_CLOSING_BRACE"] = 1] = "EXPECT_ARGUMENT_CLOSING_BRACE";
5
+ /** Argument is empty (e.g. `{}`). */
6
+ ErrorKind[ErrorKind["EMPTY_ARGUMENT"] = 2] = "EMPTY_ARGUMENT";
7
+ /** Argument is malformed (e.g. `{foo!}``) */
8
+ ErrorKind[ErrorKind["MALFORMED_ARGUMENT"] = 3] = "MALFORMED_ARGUMENT";
9
+ /** Expect an argument type (e.g. `{foo,}`) */
10
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_TYPE"] = 4] = "EXPECT_ARGUMENT_TYPE";
11
+ /** Unsupported argument type (e.g. `{foo,foo}`) */
12
+ ErrorKind[ErrorKind["INVALID_ARGUMENT_TYPE"] = 5] = "INVALID_ARGUMENT_TYPE";
13
+ /** Expect an argument style (e.g. `{foo, number, }`) */
14
+ ErrorKind[ErrorKind["EXPECT_ARGUMENT_STYLE"] = 6] = "EXPECT_ARGUMENT_STYLE";
15
+ /** The number skeleton is invalid. */
16
+ ErrorKind[ErrorKind["INVALID_NUMBER_SKELETON"] = 7] = "INVALID_NUMBER_SKELETON";
17
+ /** The date time skeleton is invalid. */
18
+ ErrorKind[ErrorKind["INVALID_DATE_TIME_SKELETON"] = 8] = "INVALID_DATE_TIME_SKELETON";
19
+ /** Exepct a number skeleton following the `::` (e.g. `{foo, number, ::}`) */
20
+ ErrorKind[ErrorKind["EXPECT_NUMBER_SKELETON"] = 9] = "EXPECT_NUMBER_SKELETON";
21
+ /** Exepct a date time skeleton following the `::` (e.g. `{foo, date, ::}`) */
22
+ ErrorKind[ErrorKind["EXPECT_DATE_TIME_SKELETON"] = 10] = "EXPECT_DATE_TIME_SKELETON";
23
+ /** Unmatched apostrophes in the argument style (e.g. `{foo, number, 'test`) */
24
+ ErrorKind[ErrorKind["UNCLOSED_QUOTE_IN_ARGUMENT_STYLE"] = 11] = "UNCLOSED_QUOTE_IN_ARGUMENT_STYLE";
25
+ /** Missing select argument options (e.g. `{foo, select}`) */
26
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_OPTIONS"] = 12] = "EXPECT_SELECT_ARGUMENT_OPTIONS";
27
+ /** Expecting an offset value in `plural` or `selectordinal` argument (e.g `{foo, plural, offset}`) */
28
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE"] = 13] = "EXPECT_PLURAL_ARGUMENT_OFFSET_VALUE";
29
+ /** Offset value in `plural` or `selectordinal` is invalid (e.g. `{foo, plural, offset: x}`) */
30
+ ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_OFFSET_VALUE"] = 14] = "INVALID_PLURAL_ARGUMENT_OFFSET_VALUE";
31
+ /** Expecting a selector in `select` argument (e.g `{foo, select}`) */
32
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR"] = 15] = "EXPECT_SELECT_ARGUMENT_SELECTOR";
33
+ /** Expecting a selector in `plural` or `selectordinal` argument (e.g `{foo, plural}`) */
34
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR"] = 16] = "EXPECT_PLURAL_ARGUMENT_SELECTOR";
35
+ /** Expecting a message fragment after the `select` selector (e.g. `{foo, select, apple}`) */
36
+ ErrorKind[ErrorKind["EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT"] = 17] = "EXPECT_SELECT_ARGUMENT_SELECTOR_FRAGMENT";
37
+ /**
38
+ * Expecting a message fragment after the `plural` or `selectordinal` selector
39
+ * (e.g. `{foo, plural, one}`)
40
+ */
41
+ ErrorKind[ErrorKind["EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT"] = 18] = "EXPECT_PLURAL_ARGUMENT_SELECTOR_FRAGMENT";
42
+ /** Selector in `plural` or `selectordinal` is malformed (e.g. `{foo, plural, =x {#}}`) */
43
+ ErrorKind[ErrorKind["INVALID_PLURAL_ARGUMENT_SELECTOR"] = 19] = "INVALID_PLURAL_ARGUMENT_SELECTOR";
44
+ /**
45
+ * Duplicate selectors in `plural` or `selectordinal` argument.
46
+ * (e.g. {foo, plural, one {#} one {#}})
47
+ */
48
+ ErrorKind[ErrorKind["DUPLICATE_PLURAL_ARGUMENT_SELECTOR"] = 20] = "DUPLICATE_PLURAL_ARGUMENT_SELECTOR";
49
+ /** Duplicate selectors in `select` argument.
50
+ * (e.g. {foo, select, apple {apple} apple {apple}})
51
+ */
52
+ ErrorKind[ErrorKind["DUPLICATE_SELECT_ARGUMENT_SELECTOR"] = 21] = "DUPLICATE_SELECT_ARGUMENT_SELECTOR";
53
+ /** Plural or select argument option must have `other` clause. */
54
+ ErrorKind[ErrorKind["MISSING_OTHER_CLAUSE"] = 22] = "MISSING_OTHER_CLAUSE";
55
+ /** The tag is malformed. (e.g. `<bold!>foo</bold!>) */
56
+ ErrorKind[ErrorKind["INVALID_TAG"] = 23] = "INVALID_TAG";
57
+ /** The tag name is invalid. (e.g. `<123>foo</123>`) */
58
+ ErrorKind[ErrorKind["INVALID_TAG_NAME"] = 25] = "INVALID_TAG_NAME";
59
+ /** The closing tag does not match the opening tag. (e.g. `<bold>foo</italic>`) */
60
+ ErrorKind[ErrorKind["UNMATCHED_CLOSING_TAG"] = 26] = "UNMATCHED_CLOSING_TAG";
61
+ /** The opening tag has unmatched closing tag. (e.g. `<bold>foo`) */
62
+ ErrorKind[ErrorKind["UNCLOSED_TAG"] = 27] = "UNCLOSED_TAG";
63
+ })(ErrorKind || (ErrorKind = {}));
package/lib/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { ParserOptions } from './parser';
2
+ import { MessageFormatElement } from './types';
3
+ export declare function parse(message: string, opts?: ParserOptions): MessageFormatElement[];
4
+ export * from './types';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../../packages/icu-messageformat-parser/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,aAAa,EAAC,MAAM,UAAU,CAAA;AAC9C,OAAO,EASL,oBAAoB,EACrB,MAAM,SAAS,CAAA;AAuBhB,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,GAAE,aAAkB,0BAoB9D;AACD,cAAc,SAAS,CAAA"}
package/lib/index.js ADDED
@@ -0,0 +1,43 @@
1
+ import { __assign } from "tslib";
2
+ import { ErrorKind } from './error';
3
+ import { Parser } from './parser';
4
+ import { isDateElement, isDateTimeSkeleton, isNumberElement, isNumberSkeleton, isPluralElement, isSelectElement, isTagElement, isTimeElement, } from './types';
5
+ function pruneLocation(els) {
6
+ els.forEach(function (el) {
7
+ delete el.location;
8
+ if (isSelectElement(el) || isPluralElement(el)) {
9
+ for (var k in el.options) {
10
+ delete el.options[k].location;
11
+ pruneLocation(el.options[k].value);
12
+ }
13
+ }
14
+ else if (isNumberElement(el) && isNumberSkeleton(el.style)) {
15
+ delete el.style.location;
16
+ }
17
+ else if ((isDateElement(el) || isTimeElement(el)) &&
18
+ isDateTimeSkeleton(el.style)) {
19
+ delete el.style.location;
20
+ }
21
+ else if (isTagElement(el)) {
22
+ pruneLocation(el.children);
23
+ }
24
+ });
25
+ }
26
+ export function parse(message, opts) {
27
+ if (opts === void 0) { opts = {}; }
28
+ opts = __assign({ shouldParseSkeletons: true, requiresOtherClause: true }, opts);
29
+ var result = new Parser(message, opts).parse();
30
+ if (result.err) {
31
+ var error = SyntaxError(ErrorKind[result.err.kind]);
32
+ // @ts-expect-error Assign to error object
33
+ error.location = result.err.location;
34
+ // @ts-expect-error Assign to error object
35
+ error.originalMessage = result.err.message;
36
+ throw error;
37
+ }
38
+ if (!(opts === null || opts === void 0 ? void 0 : opts.captureLocation)) {
39
+ pruneLocation(result.val);
40
+ }
41
+ return result.val;
42
+ }
43
+ export * from './types';
@@ -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"}
@@ -0,0 +1,52 @@
1
+ import { __spreadArray } from "tslib";
2
+ import { isPluralElement, isSelectElement, } from './types';
3
+ function cloneDeep(obj) {
4
+ if (Array.isArray(obj)) {
5
+ // @ts-expect-error meh
6
+ return __spreadArray([], obj.map(cloneDeep));
7
+ }
8
+ if (typeof obj === 'object') {
9
+ // @ts-expect-error meh
10
+ return Object.keys(obj).reduce(function (cloned, k) {
11
+ // @ts-expect-error meh
12
+ cloned[k] = cloneDeep(obj[k]);
13
+ return cloned;
14
+ }, {});
15
+ }
16
+ return obj;
17
+ }
18
+ /**
19
+ * Hoist all selectors to the beginning of the AST & flatten the
20
+ * resulting options. E.g:
21
+ * "I have {count, plural, one{a dog} other{many dogs}}"
22
+ * becomes "{count, plural, one{I have a dog} other{I have many dogs}}".
23
+ * If there are multiple selectors, the order of which one is hoisted 1st
24
+ * is non-deterministic.
25
+ * The goal is to provide as many full sentences as possible since fragmented
26
+ * sentences are not translator-friendly
27
+ * @param ast AST
28
+ */
29
+ export function hoistSelectors(ast) {
30
+ var _loop_1 = function (i) {
31
+ var el = ast[i];
32
+ if (isPluralElement(el) || isSelectElement(el)) {
33
+ // pull this out of the ast and move it to the top
34
+ var cloned = cloneDeep(el);
35
+ var options_1 = cloned.options;
36
+ cloned.options = Object.keys(options_1).reduce(function (all, k) {
37
+ var newValue = hoistSelectors(__spreadArray(__spreadArray(__spreadArray([], ast.slice(0, i)), options_1[k].value), ast.slice(i + 1)));
38
+ all[k] = {
39
+ value: newValue,
40
+ };
41
+ return all;
42
+ }, {});
43
+ return { value: [cloned] };
44
+ }
45
+ };
46
+ for (var i = 0; i < ast.length; i++) {
47
+ var state_1 = _loop_1(i);
48
+ if (typeof state_1 === "object")
49
+ return state_1.value;
50
+ }
51
+ return ast;
52
+ }
@@ -0,0 +1,3 @@
1
+ export declare function parse(): void;
2
+ export * from './types';
3
+ //# sourceMappingURL=no-parser.d.ts.map
@@ -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"}
@@ -0,0 +1,4 @@
1
+ export function parse() {
2
+ throw new Error("You're trying to format an uncompiled message with react-intl without parser, please import from 'react-int' instead");
3
+ }
4
+ export * from './types';