@formatjs/icu-messageformat-parser 3.2.1 → 3.4.0

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/parser.d.ts CHANGED
@@ -1,147 +1,150 @@
1
- import { ParserError } from './error.js';
2
- import { MessageFormatElement } from './types.js';
1
+ import { type ParserError } from "./error.js";
2
+ import { type MessageFormatElement } from "./types.js";
3
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;
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
9
  }
10
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
- * Instance of Intl.Locale to resolve locale-dependent skeleton
35
- */
36
- locale?: Intl.Locale;
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
+ * Instance of Intl.Locale to resolve locale-dependent skeleton
35
+ */
36
+ locale?: Intl.Locale;
37
37
  }
38
- export type Result<T, E> = {
39
- val: T;
40
- err: null;
38
+ export type Result<
39
+ T,
40
+ E
41
+ > = {
42
+ val: T;
43
+ err: null;
41
44
  } | {
42
- val: null;
43
- err: E;
45
+ val: null;
46
+ err: E;
44
47
  };
45
48
  export declare class Parser {
46
- private message;
47
- private position;
48
- private locale?;
49
- private ignoreTag;
50
- private requiresOtherClause;
51
- private shouldParseSkeletons?;
52
- constructor(message: string, options?: ParserOptions);
53
- parse(): Result<MessageFormatElement[], ParserError>;
54
- private parseMessage;
55
- /**
56
- * A tag name must start with an ASCII lower/upper case letter. The grammar is based on the
57
- * [custom element name][] except that a dash is NOT always mandatory and uppercase letters
58
- * are accepted:
59
- *
60
- * ```
61
- * tag ::= "<" tagName (whitespace)* "/>" | "<" tagName (whitespace)* ">" message "</" tagName (whitespace)* ">"
62
- * tagName ::= [a-z] (PENChar)*
63
- * PENChar ::=
64
- * "-" | "." | [0-9] | "_" | [a-z] | [A-Z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
65
- * [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
66
- * [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
67
- * ```
68
- *
69
- * [custom element name]: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
70
- * NOTE: We're a bit more lax here since HTML technically does not allow uppercase HTML element but we do
71
- * since other tag-based engines like React allow it
72
- */
73
- private parseTag;
74
- /**
75
- * This method assumes that the caller has peeked ahead for the first tag character.
76
- */
77
- private parseTagName;
78
- private parseLiteral;
79
- tryParseLeftAngleBracket(): string | null;
80
- /**
81
- * Starting with ICU 4.8, an ASCII apostrophe only starts quoted text if it immediately precedes
82
- * a character that requires quoting (that is, "only where needed"), and works the same in
83
- * nested messages as on the top level of the pattern. The new behavior is otherwise compatible.
84
- */
85
- private tryParseQuote;
86
- private tryParseUnquoted;
87
- private parseArgument;
88
- /**
89
- * Advance the parser until the end of the identifier, if it is currently on
90
- * an identifier character. Return an empty string otherwise.
91
- */
92
- private parseIdentifierIfPossible;
93
- private parseArgumentOptions;
94
- private tryParseArgumentClose;
95
- /**
96
- * See: https://github.com/unicode-org/icu/blob/af7ed1f6d2298013dc303628438ec4abe1f16479/icu4c/source/common/messagepattern.cpp#L659
97
- */
98
- private parseSimpleArgStyleIfPossible;
99
- private parseNumberSkeletonFromString;
100
- /**
101
- * @param nesting_level The current nesting level of messages.
102
- * This can be positive when parsing message fragment in select or plural argument options.
103
- * @param parent_arg_type The parent argument's type.
104
- * @param parsed_first_identifier If provided, this is the first identifier-like selector of
105
- * the argument. It is a by-product of a previous parsing attempt.
106
- * @param expecting_close_tag If true, this message is directly or indirectly nested inside
107
- * between a pair of opening and closing tags. The nested message will not parse beyond
108
- * the closing tag boundary.
109
- */
110
- private tryParsePluralOrSelectOptions;
111
- private tryParseDecimalInteger;
112
- private offset;
113
- private isEOF;
114
- private clonePosition;
115
- /**
116
- * Return the code point at the current position of the parser.
117
- * Throws if the index is out of bound.
118
- */
119
- private char;
120
- private error;
121
- /** Bump the parser to the next UTF-16 code unit. */
122
- private bump;
123
- /**
124
- * If the substring starting at the current position of the parser has
125
- * the given prefix, then bump the parser to the character immediately
126
- * following the prefix and return true. Otherwise, don't bump the parser
127
- * and return false.
128
- */
129
- private bumpIf;
130
- /**
131
- * Bump the parser until the pattern character is found and return `true`.
132
- * Otherwise bump to the end of the file and return `false`.
133
- */
134
- private bumpUntil;
135
- /**
136
- * Bump the parser to the target offset.
137
- * If target offset is beyond the end of the input, bump the parser to the end of the input.
138
- */
139
- private bumpTo;
140
- /** advance the parser through all whitespace to the next non-whitespace code unit. */
141
- private bumpSpace;
142
- /**
143
- * Peek at the *next* Unicode codepoint in the input without advancing the parser.
144
- * If the input has been exhausted, then this returns null.
145
- */
146
- private peek;
49
+ private message;
50
+ private position;
51
+ private locale?;
52
+ private ignoreTag;
53
+ private requiresOtherClause;
54
+ private shouldParseSkeletons?;
55
+ constructor(message: string, options?: ParserOptions);
56
+ parse(): Result<MessageFormatElement[], ParserError>;
57
+ private parseMessage;
58
+ /**
59
+ * A tag name must start with an ASCII lower/upper case letter. The grammar is based on the
60
+ * [custom element name][] except that a dash is NOT always mandatory and uppercase letters
61
+ * are accepted:
62
+ *
63
+ * ```
64
+ * tag ::= "<" tagName (whitespace)* "/>" | "<" tagName (whitespace)* ">" message "</" tagName (whitespace)* ">"
65
+ * tagName ::= [a-z] (PENChar)*
66
+ * PENChar ::=
67
+ * "-" | "." | [0-9] | "_" | [a-z] | [A-Z] | #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] |
68
+ * [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
69
+ * [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
70
+ * ```
71
+ *
72
+ * [custom element name]: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
73
+ * NOTE: We're a bit more lax here since HTML technically does not allow uppercase HTML element but we do
74
+ * since other tag-based engines like React allow it
75
+ */
76
+ private parseTag;
77
+ /**
78
+ * This method assumes that the caller has peeked ahead for the first tag character.
79
+ */
80
+ private parseTagName;
81
+ private parseLiteral;
82
+ tryParseLeftAngleBracket(): string | null;
83
+ /**
84
+ * Starting with ICU 4.8, an ASCII apostrophe only starts quoted text if it immediately precedes
85
+ * a character that requires quoting (that is, "only where needed"), and works the same in
86
+ * nested messages as on the top level of the pattern. The new behavior is otherwise compatible.
87
+ */
88
+ private tryParseQuote;
89
+ private tryParseUnquoted;
90
+ private parseArgument;
91
+ /**
92
+ * Advance the parser until the end of the identifier, if it is currently on
93
+ * an identifier character. Return an empty string otherwise.
94
+ */
95
+ private parseIdentifierIfPossible;
96
+ private parseArgumentOptions;
97
+ private tryParseArgumentClose;
98
+ /**
99
+ * See: https://github.com/unicode-org/icu/blob/af7ed1f6d2298013dc303628438ec4abe1f16479/icu4c/source/common/messagepattern.cpp#L659
100
+ */
101
+ private parseSimpleArgStyleIfPossible;
102
+ private parseNumberSkeletonFromString;
103
+ /**
104
+ * @param nesting_level The current nesting level of messages.
105
+ * This can be positive when parsing message fragment in select or plural argument options.
106
+ * @param parent_arg_type The parent argument's type.
107
+ * @param parsed_first_identifier If provided, this is the first identifier-like selector of
108
+ * the argument. It is a by-product of a previous parsing attempt.
109
+ * @param expecting_close_tag If true, this message is directly or indirectly nested inside
110
+ * between a pair of opening and closing tags. The nested message will not parse beyond
111
+ * the closing tag boundary.
112
+ */
113
+ private tryParsePluralOrSelectOptions;
114
+ private tryParseDecimalInteger;
115
+ private offset;
116
+ private isEOF;
117
+ private clonePosition;
118
+ /**
119
+ * Return the code point at the current position of the parser.
120
+ * Throws if the index is out of bound.
121
+ */
122
+ private char;
123
+ private error;
124
+ /** Bump the parser to the next UTF-16 code unit. */
125
+ private bump;
126
+ /**
127
+ * If the substring starting at the current position of the parser has
128
+ * the given prefix, then bump the parser to the character immediately
129
+ * following the prefix and return true. Otherwise, don't bump the parser
130
+ * and return false.
131
+ */
132
+ private bumpIf;
133
+ /**
134
+ * Bump the parser until the pattern character is found and return `true`.
135
+ * Otherwise bump to the end of the file and return `false`.
136
+ */
137
+ private bumpUntil;
138
+ /**
139
+ * Bump the parser to the target offset.
140
+ * If target offset is beyond the end of the input, bump the parser to the end of the input.
141
+ */
142
+ private bumpTo;
143
+ /** advance the parser through all whitespace to the next non-whitespace code unit. */
144
+ private bumpSpace;
145
+ /**
146
+ * Peek at the *next* Unicode codepoint in the input without advancing the parser.
147
+ * If the input has been exhausted, then this returns null.
148
+ */
149
+ private peek;
147
150
  }