@hamur/i18n 0.0.0-stage → 0.0.2

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 (155) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +38 -2
  3. package/dist/chunks/bun-index-1ve1hj66.js +744 -0
  4. package/dist/chunks/bun-index-8bw4xpv8.js +93 -0
  5. package/dist/chunks/bun-index-ej6vjnjs.js +297 -0
  6. package/dist/chunks/bun-index-pdepkyy1.js +474 -0
  7. package/dist/src/adapters/angular.js +67 -0
  8. package/dist/src/adapters/html.js +34 -0
  9. package/dist/src/adapters/preact.js +23 -0
  10. package/dist/src/adapters/react.js +23 -0
  11. package/dist/src/adapters/solid.js +48 -0
  12. package/dist/src/adapters/svelte.js +74 -0
  13. package/dist/src/adapters/vue.js +48 -0
  14. package/dist/src/cli.js +1898 -0
  15. package/dist/src/index.js +2755 -0
  16. package/dist/src/load.js +35 -0
  17. package/dist/src/server/index.js +395 -0
  18. package/dist/types/src/adapters/angular.d.ts +20 -0
  19. package/dist/types/src/adapters/bind.d.ts +44 -0
  20. package/dist/types/src/adapters/html.d.ts +5 -0
  21. package/dist/types/src/adapters/preact.d.ts +6 -0
  22. package/dist/types/src/adapters/react.d.ts +6 -0
  23. package/dist/types/src/adapters/solid.d.ts +9 -0
  24. package/dist/types/src/adapters/svelte.d.ts +12 -0
  25. package/dist/types/src/adapters/vue.d.ts +8 -0
  26. package/dist/types/src/build/catalog.d.ts +5 -0
  27. package/dist/types/src/build/drop.d.ts +12 -0
  28. package/dist/types/src/build/extract.d.ts +11 -0
  29. package/dist/types/src/build/head.d.ts +4 -0
  30. package/dist/types/src/build/messages.d.ts +27 -0
  31. package/dist/types/src/build/prerender.d.ts +10 -0
  32. package/dist/types/src/build/routes.d.ts +14 -0
  33. package/dist/types/src/build/split.d.ts +37 -0
  34. package/dist/types/src/build/types.d.ts +15 -0
  35. package/dist/types/src/cli.d.ts +12 -0
  36. package/dist/types/src/compiler/compile.d.ts +17 -0
  37. package/dist/types/src/compiler/mf2-parse.d.ts +62 -0
  38. package/dist/types/src/compiler/mf2.d.ts +3 -0
  39. package/dist/types/src/compiler/parse.d.ts +58 -0
  40. package/dist/types/src/compiler/skeleton.d.ts +7 -0
  41. package/dist/types/src/index.d.ts +2 -0
  42. package/dist/types/src/load.d.ts +7 -0
  43. package/dist/types/src/module.d.ts +15 -0
  44. package/dist/types/src/options.d.ts +7 -0
  45. package/dist/types/src/runtime/catalog-client.d.ts +3 -0
  46. package/dist/types/src/runtime/catalog-loader.d.ts +15 -0
  47. package/dist/types/src/runtime/catalog-server.d.ts +5 -0
  48. package/dist/types/src/runtime/catalog.d.ts +60 -0
  49. package/dist/types/src/runtime/chain.d.ts +3 -0
  50. package/dist/types/src/runtime/format.d.ts +27 -0
  51. package/dist/types/src/runtime/head.d.ts +9 -0
  52. package/dist/types/src/runtime/interpret.d.ts +20 -0
  53. package/dist/types/src/runtime/link.d.ts +8 -0
  54. package/dist/types/src/runtime/locale.d.ts +73 -0
  55. package/dist/types/src/runtime/match.d.ts +54 -0
  56. package/dist/types/src/runtime/messages.d.ts +42 -0
  57. package/dist/types/src/runtime/middleware.d.ts +2 -0
  58. package/dist/types/src/runtime/negotiate.d.ts +9 -0
  59. package/dist/types/src/runtime/plugin.d.ts +4 -0
  60. package/dist/types/src/runtime/redirect.d.ts +32 -0
  61. package/dist/types/src/runtime/rich.d.ts +5 -0
  62. package/dist/types/src/runtime/translate.d.ts +21 -0
  63. package/dist/types/src/server/catalog.d.ts +2 -0
  64. package/dist/types/src/server/context.d.ts +48 -0
  65. package/dist/types/src/server/door-all-catalog.d.ts +3 -0
  66. package/dist/types/src/server/door-all.d.ts +2 -0
  67. package/dist/types/src/server/door-catalog.d.ts +3 -0
  68. package/dist/types/src/server/door-root-catalog.d.ts +3 -0
  69. package/dist/types/src/server/door-root.d.ts +2 -0
  70. package/dist/types/src/server/door.d.ts +2 -0
  71. package/dist/types/src/server/index.d.ts +7 -0
  72. package/dist/types/src/server/preferred.d.ts +6 -0
  73. package/dist/types/src/server/redirect.d.ts +3 -0
  74. package/dist/types/src/server/setup.d.ts +2 -0
  75. package/dist/types/src/types.d.ts +163 -0
  76. package/package.json +155 -4
  77. package/src/adapters/angular.ts +58 -0
  78. package/src/adapters/bind.ts +144 -0
  79. package/src/adapters/html.ts +35 -0
  80. package/src/adapters/preact.ts +18 -0
  81. package/src/adapters/react.ts +18 -0
  82. package/src/adapters/solid.ts +44 -0
  83. package/src/adapters/svelte.ts +75 -0
  84. package/src/adapters/test/SveltePage.svelte +18 -0
  85. package/src/adapters/test/SvelteReporting.svelte +20 -0
  86. package/src/adapters/test/SvelteRich.svelte +11 -0
  87. package/src/adapters/test/SvelteRichPlain.svelte +11 -0
  88. package/src/adapters/test/peer.ts +7 -0
  89. package/src/adapters/test/preact-client.ts +43 -0
  90. package/src/adapters/test/preact-page.ts +34 -0
  91. package/src/adapters/test/preact-rich.ts +9 -0
  92. package/src/adapters/test/react-client.ts +49 -0
  93. package/src/adapters/test/react-page.ts +34 -0
  94. package/src/adapters/test/react-rich.ts +15 -0
  95. package/src/adapters/test/runtime.ts +104 -0
  96. package/src/adapters/test/solid-client.ts +33 -0
  97. package/src/adapters/test/solid-page.ts +30 -0
  98. package/src/adapters/test/solid-rich.ts +5 -0
  99. package/src/adapters/test/svelte-client.ts +51 -0
  100. package/src/adapters/test/svelte-log.ts +2 -0
  101. package/src/adapters/test/svelte-plugin.ts +20 -0
  102. package/src/adapters/test/vue-page.ts +26 -0
  103. package/src/adapters/test/vue-rich.ts +16 -0
  104. package/src/adapters/vue.ts +44 -0
  105. package/src/build/catalog.ts +44 -0
  106. package/src/build/drop.ts +36 -0
  107. package/src/build/extract.ts +207 -0
  108. package/src/build/head.ts +10 -0
  109. package/src/build/messages.ts +347 -0
  110. package/src/build/prerender.ts +48 -0
  111. package/src/build/routes.ts +269 -0
  112. package/src/build/split.ts +240 -0
  113. package/src/build/types.ts +156 -0
  114. package/src/cli.ts +25 -0
  115. package/src/compiler/compile.ts +190 -0
  116. package/src/compiler/mf2-parse.ts +469 -0
  117. package/src/compiler/mf2.ts +393 -0
  118. package/src/compiler/parse.ts +265 -0
  119. package/src/compiler/skeleton.ts +85 -0
  120. package/src/env.d.ts +55 -0
  121. package/src/index.ts +24 -0
  122. package/src/load.ts +41 -0
  123. package/src/module.ts +66 -0
  124. package/src/options.ts +198 -0
  125. package/src/runtime/catalog-client.ts +28 -0
  126. package/src/runtime/catalog-loader.ts +28 -0
  127. package/src/runtime/catalog-server.ts +36 -0
  128. package/src/runtime/catalog.ts +190 -0
  129. package/src/runtime/chain.ts +9 -0
  130. package/src/runtime/format.ts +102 -0
  131. package/src/runtime/head.ts +57 -0
  132. package/src/runtime/interpret.ts +504 -0
  133. package/src/runtime/link.ts +36 -0
  134. package/src/runtime/locale.ts +392 -0
  135. package/src/runtime/match.ts +334 -0
  136. package/src/runtime/messages.ts +129 -0
  137. package/src/runtime/middleware.ts +32 -0
  138. package/src/runtime/negotiate.ts +61 -0
  139. package/src/runtime/plugin.ts +69 -0
  140. package/src/runtime/redirect.ts +191 -0
  141. package/src/runtime/rich.ts +21 -0
  142. package/src/runtime/translate.ts +131 -0
  143. package/src/server/catalog.ts +11 -0
  144. package/src/server/context.ts +191 -0
  145. package/src/server/door-all-catalog.ts +7 -0
  146. package/src/server/door-all.ts +5 -0
  147. package/src/server/door-catalog.ts +6 -0
  148. package/src/server/door-root-catalog.ts +7 -0
  149. package/src/server/door-root.ts +5 -0
  150. package/src/server/door.ts +4 -0
  151. package/src/server/index.ts +11 -0
  152. package/src/server/preferred.ts +12 -0
  153. package/src/server/redirect.ts +26 -0
  154. package/src/server/setup.ts +14 -0
  155. package/src/types.ts +182 -0
@@ -0,0 +1,469 @@
1
+ import { MessageSyntaxError } from "./parse.ts"
2
+
3
+ export interface Literal {
4
+ kind: "literal"
5
+ value: string
6
+ }
7
+ export interface Variable {
8
+ kind: "variable"
9
+ name: string
10
+ at: number
11
+ }
12
+ export interface Option {
13
+ name: string
14
+ value: Literal | Variable
15
+ at: number
16
+ }
17
+ export interface Call {
18
+ name: string
19
+ at: number
20
+ options: Option[]
21
+ }
22
+ export interface Expression {
23
+ operand?: Literal | Variable
24
+ call?: Call
25
+ at: number
26
+ }
27
+ export type Part =
28
+ | { type: "text"; value: string }
29
+ | { type: "expression"; expression: Expression }
30
+ | { type: "markup"; kind: "open" | "close" | "standalone"; name: string }
31
+ export interface Declaration {
32
+ input: boolean
33
+ name: string
34
+ expression: Expression
35
+ at: number
36
+ }
37
+ export interface Variant {
38
+ keys: (string | null)[]
39
+ parts: Part[]
40
+ at: number
41
+ }
42
+ export interface Matcher {
43
+ at: number
44
+ selectors: { name: string; at: number }[]
45
+ variants: Variant[]
46
+ }
47
+ export interface Message {
48
+ declarations: Declaration[]
49
+ parts: Part[]
50
+ matcher?: Matcher
51
+ }
52
+
53
+ const NUMBER_OPTIONS = [
54
+ "select",
55
+ "signDisplay",
56
+ "useGrouping",
57
+ "minimumIntegerDigits",
58
+ "minimumFractionDigits",
59
+ "maximumFractionDigits",
60
+ "minimumSignificantDigits",
61
+ "maximumSignificantDigits",
62
+ "trailingZeroDisplay",
63
+ "roundingPriority",
64
+ "roundingIncrement",
65
+ "roundingMode",
66
+ ]
67
+ const INTEGER_OPTIONS = ["select", "signDisplay", "useGrouping", "minimumIntegerDigits", "maximumSignificantDigits"]
68
+ export const FUNCTIONS: Record<string, readonly string[]> = {
69
+ string: [],
70
+ number: NUMBER_OPTIONS,
71
+ integer: INTEGER_OPTIONS,
72
+ datetime: ["dateFields", "dateLength", "timePrecision", "timeZoneStyle", "timeZone", "hour12", "calendar"],
73
+ date: ["fields", "length", "timeZone", "calendar"],
74
+ time: ["precision", "timeZoneStyle", "timeZone", "hour12", "calendar"],
75
+ }
76
+
77
+ const isBidi = (c: number) => c === 0x61c || c === 0x200e || c === 0x200f || (c >= 0x2066 && c <= 0x2069)
78
+ const isWs = (c: number) => c === 0x20 || c === 0x09 || c === 0x0d || c === 0x0a || c === 0x3000
79
+
80
+ function isNameStart(c: number): boolean {
81
+ if ((c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || c === 0x2b || c === 0x5f) return true
82
+ if (c < 0xa1 || c > 0x10ffff) return false
83
+ if (c === 0x61c || c === 0x1680 || c === 0x3000 || c === 0x205f) return false
84
+ if (
85
+ (c >= 0x2000 && c <= 0x200a) ||
86
+ (c >= 0x200e && c <= 0x200f) ||
87
+ (c >= 0x2028 && c <= 0x202f) ||
88
+ (c >= 0x2066 && c <= 0x2069)
89
+ )
90
+ return false
91
+ if ((c >= 0xd800 && c <= 0xdfff) || (c >= 0xfdd0 && c <= 0xfdef)) return false
92
+ return (c & 0xffff) < 0xfffe
93
+ }
94
+ const isNameChar = (c: number) => isNameStart(c) || (c >= 0x30 && c <= 0x39) || c === 0x2d || c === 0x2e
95
+
96
+ class Parser {
97
+ i = 0
98
+ ws = false
99
+
100
+ constructor(readonly src: string) {}
101
+
102
+ fail(reason: string, at = this.i): never {
103
+ throw new MessageSyntaxError(at, reason)
104
+ }
105
+
106
+ cp(at = this.i): number {
107
+ return this.src.codePointAt(at) ?? -1
108
+ }
109
+
110
+ at(text: string): boolean {
111
+ return this.src.startsWith(text, this.i)
112
+ }
113
+
114
+ o(): void {
115
+ for (;;) {
116
+ const c = this.cp()
117
+ if (c === -1 || !(isWs(c) || isBidi(c))) return
118
+ this.i++
119
+ }
120
+ }
121
+
122
+ s(): boolean {
123
+ let j = this.i
124
+ while (isBidi(this.cp(j))) j++
125
+ if (!isWs(this.cp(j))) return false
126
+ this.i = j + 1
127
+ this.o()
128
+ return true
129
+ }
130
+
131
+ sep(): void {
132
+ this.ws = this.s()
133
+ if (!this.ws) this.o()
134
+ }
135
+
136
+ textChar(c: number): void {
137
+ if (c === 0) this.fail("a NUL character is not allowed")
138
+ if (c >= 0xd800 && c <= 0xdfff) this.fail("an unpaired surrogate is not allowed")
139
+ }
140
+
141
+ message(): Message {
142
+ this.o()
143
+ if (!this.at(".") && !this.at("{{")) {
144
+ this.i = 0
145
+ return { declarations: [], parts: this.pattern(false) }
146
+ }
147
+ const declarations: Declaration[] = []
148
+ for (;;) {
149
+ const at = this.i
150
+ if (this.at(".input")) {
151
+ this.i += 6
152
+ this.o()
153
+ const expression = this.expression()
154
+ const operand = expression.operand
155
+ if (operand?.kind !== "variable") this.fail(".input needs a variable such as {$count :number}", expression.at)
156
+ declarations.push({ input: true, name: operand.name, expression, at })
157
+ } else if (this.at(".local")) {
158
+ this.i += 6
159
+ if (!this.s()) this.fail("expected whitespace after .local")
160
+ if (this.src[this.i] !== "$") this.fail("expected a variable such as $count")
161
+ const variable = this.variable()
162
+ this.o()
163
+ if (this.src[this.i] !== "=") this.fail("expected = after the variable")
164
+ this.i++
165
+ this.o()
166
+ declarations.push({ input: false, name: variable.name, expression: this.expression(), at })
167
+ } else break
168
+ this.o()
169
+ }
170
+ let parts: Part[] = []
171
+ let matcher: Matcher | undefined
172
+ if (this.at("{{")) parts = this.quotedPattern()
173
+ else if (this.at(".match")) matcher = this.matcher()
174
+ else if (this.at("."))
175
+ this.fail('unknown keyword "' + (/^\.[^\s{|}]*/.exec(this.src.slice(this.i))?.[0] ?? ".") + '"')
176
+ else this.fail("expected a quoted pattern {{…}} or .match")
177
+ this.o()
178
+ if (this.i < this.src.length) this.fail("unexpected content after the message")
179
+ return { declarations, parts, ...(matcher ? { matcher } : {}) }
180
+ }
181
+
182
+ quotedPattern(): Part[] {
183
+ this.i += 2
184
+ const parts = this.pattern(true)
185
+ this.i += 2
186
+ return parts
187
+ }
188
+
189
+ matcher(): Matcher {
190
+ const at = this.i
191
+ this.i += 6
192
+ const selectors: Matcher["selectors"] = []
193
+ for (;;) {
194
+ if (!this.s()) this.fail("expected whitespace")
195
+ if (this.src[this.i] !== "$") break
196
+ const variable = this.variable()
197
+ selectors.push({ name: variable.name, at: variable.at })
198
+ }
199
+ if (selectors.length === 0) this.fail("expected a selector such as $count")
200
+ const variants: Variant[] = []
201
+ for (;;) {
202
+ const start = this.i
203
+ const keys = [this.key()]
204
+ for (;;) {
205
+ const had = this.s()
206
+ if (!had) this.o()
207
+ if (this.at("{{")) break
208
+ if (!had) this.fail("expected whitespace or {{")
209
+ keys.push(this.key())
210
+ }
211
+ variants.push({ keys, parts: this.quotedPattern(), at: start })
212
+ this.o()
213
+ if (this.i >= this.src.length) break
214
+ }
215
+ return { at, selectors, variants }
216
+ }
217
+
218
+ key(): string | null {
219
+ if (this.src[this.i] === "*") {
220
+ this.i++
221
+ return null
222
+ }
223
+ return this.literal("expected a variant key").value
224
+ }
225
+
226
+ pattern(quoted: boolean): Part[] {
227
+ const parts: Part[] = []
228
+ let text = ""
229
+ const flush = () => {
230
+ if (text) parts.push({ type: "text", value: text })
231
+ text = ""
232
+ }
233
+ for (;;) {
234
+ const c = this.cp()
235
+ if (c === -1) {
236
+ if (quoted) this.fail("expected }} to close the pattern")
237
+ break
238
+ }
239
+ if (c === 0x7b) {
240
+ flush()
241
+ parts.push(this.placeholder())
242
+ } else if (c === 0x7d) {
243
+ if (quoted && this.at("}}")) break
244
+ this.fail("unexpected }; write \\} for a brace in text")
245
+ } else if (c === 0x5c) {
246
+ text += this.escape()
247
+ } else {
248
+ this.textChar(c)
249
+ text += String.fromCodePoint(c)
250
+ this.i += c > 0xffff ? 2 : 1
251
+ }
252
+ }
253
+ flush()
254
+ return parts
255
+ }
256
+
257
+ escape(): string {
258
+ const next = this.src[this.i + 1]
259
+ if (next !== "\\" && next !== "{" && next !== "|" && next !== "}")
260
+ this.fail("a backslash must be followed by \\, {, | or }")
261
+ this.i += 2
262
+ return next
263
+ }
264
+
265
+ placeholder(): Part {
266
+ const at = this.i
267
+ this.i++
268
+ this.o()
269
+ const c = this.src[this.i]
270
+ if (c === "#" || c === "/") return this.markup(c)
271
+ return { type: "expression", expression: this.expressionBody(at) }
272
+ }
273
+
274
+ expression(): Expression {
275
+ const at = this.i
276
+ if (this.src[this.i] !== "{") this.fail("expected {")
277
+ this.i++
278
+ this.o()
279
+ return this.expressionBody(at)
280
+ }
281
+
282
+ expressionBody(at: number): Expression {
283
+ const expression: Expression = { at }
284
+ const c = this.src[this.i]
285
+ if (c === "$") expression.operand = this.variable()
286
+ else if (c === ":") expression.call = this.call()
287
+ else expression.operand = this.literal("expected a literal, a variable, a function or markup")
288
+ if (expression.operand) {
289
+ this.sep()
290
+ if (this.ws && this.src[this.i] === ":") expression.call = this.call()
291
+ }
292
+ while (this.ws && this.src[this.i] === "@") this.attribute()
293
+ if (this.src[this.i] !== "}") this.fail("expected }")
294
+ this.i++
295
+ return expression
296
+ }
297
+
298
+ markup(kind: string): Part {
299
+ this.i++
300
+ const name = this.identifier()
301
+ this.options()
302
+ while (this.ws && this.src[this.i] === "@") this.attribute()
303
+ let standalone = false
304
+ if (kind === "#" && this.src[this.i] === "/") {
305
+ standalone = true
306
+ this.i++
307
+ this.o()
308
+ }
309
+ if (this.src[this.i] !== "}") this.fail("expected }")
310
+ this.i++
311
+ return { type: "markup", kind: standalone ? "standalone" : kind === "#" ? "open" : "close", name }
312
+ }
313
+
314
+ call(): Call {
315
+ const at = this.i
316
+ this.i++
317
+ const name = this.identifier()
318
+ return { name, at, options: this.options() }
319
+ }
320
+
321
+ options(): Option[] {
322
+ const options: Option[] = []
323
+ for (;;) {
324
+ this.sep()
325
+ if (!this.ws || !isNameStart(this.cp())) return options
326
+ const at = this.i
327
+ const name = this.identifier()
328
+ if (options.some((option) => option.name === name)) this.fail('duplicate option "' + name + '"', at)
329
+ this.o()
330
+ if (this.src[this.i] !== "=") this.fail("expected = after the option name")
331
+ this.i++
332
+ this.o()
333
+ const value = this.src[this.i] === "$" ? this.variable() : this.literal("expected an option value")
334
+ options.push({ name, value, at })
335
+ }
336
+ }
337
+
338
+ attribute(): void {
339
+ this.i++
340
+ this.identifier()
341
+ const save = this.i
342
+ this.o()
343
+ if (this.src[this.i] === "=") {
344
+ this.i++
345
+ this.o()
346
+ this.literal("expected an attribute value")
347
+ } else this.i = save
348
+ this.sep()
349
+ }
350
+
351
+ variable(): Variable {
352
+ const at = this.i
353
+ this.i++
354
+ return { kind: "variable", name: this.name().normalize("NFC"), at }
355
+ }
356
+
357
+ identifier(): string {
358
+ let id = this.name()
359
+ if (this.src[this.i] === ":") {
360
+ this.i++
361
+ id += ":" + this.name()
362
+ }
363
+ return id
364
+ }
365
+
366
+ name(): string {
367
+ if (isBidi(this.cp())) this.i++
368
+ if (!isNameStart(this.cp())) this.fail("expected a name")
369
+ const start = this.i
370
+ while (isNameChar(this.cp())) this.i += this.cp() > 0xffff ? 2 : 1
371
+ const value = this.src.slice(start, this.i)
372
+ if (isBidi(this.cp())) this.i++
373
+ return value
374
+ }
375
+
376
+ literal(reason: string): Literal {
377
+ if (this.src[this.i] === "|") return this.quoted()
378
+ const start = this.i
379
+ while (isNameChar(this.cp())) this.i += this.cp() > 0xffff ? 2 : 1
380
+ if (this.i === start) this.fail(reason)
381
+ return { kind: "literal", value: this.src.slice(start, this.i) }
382
+ }
383
+
384
+ quoted(): Literal {
385
+ const open = this.i
386
+ this.i++
387
+ let value = ""
388
+ for (;;) {
389
+ const c = this.cp()
390
+ if (c === -1) this.fail("unclosed |", open)
391
+ if (c === 0x7c) break
392
+ if (c === 0x5c) value += this.escape()
393
+ else {
394
+ this.textChar(c)
395
+ value += String.fromCodePoint(c)
396
+ this.i += c > 0xffff ? 2 : 1
397
+ }
398
+ }
399
+ this.i++
400
+ return { kind: "literal", value }
401
+ }
402
+ }
403
+
404
+ function expressionsOf(parts: Part[]): Expression[] {
405
+ return parts.flatMap((part) => (part.type === "expression" ? [part.expression] : []))
406
+ }
407
+
408
+ function check(message: Message): void {
409
+ const declared = new Map<string, Declaration>()
410
+ const seen = new Set<string>()
411
+ for (const declaration of message.declarations) {
412
+ const { name, expression } = declaration
413
+ const refs = (expression.call?.options ?? []).flatMap((option) =>
414
+ option.value.kind === "variable" ? [option.value.name] : [],
415
+ )
416
+ if (!declaration.input && expression.operand?.kind === "variable") refs.push(expression.operand.name)
417
+ if (declared.has(name) || seen.has(name) || refs.includes(name))
418
+ throw new MessageSyntaxError(
419
+ declaration.at,
420
+ "variable $" + name + " is declared twice or used before its declaration",
421
+ )
422
+ for (const ref of refs) seen.add(ref)
423
+ declared.set(name, declaration)
424
+ }
425
+ const { matcher } = message
426
+ if (matcher) {
427
+ const annotated = (name: string): boolean => {
428
+ const declaration = declared.get(name)
429
+ if (!declaration) return false
430
+ if (declaration.expression.call) return true
431
+ const operand = declaration.expression.operand
432
+ return !declaration.input && operand?.kind === "variable" && annotated(operand.name)
433
+ }
434
+ for (const selector of matcher.selectors) {
435
+ if (!annotated(selector.name))
436
+ throw new MessageSyntaxError(selector.at, ".match needs an annotated selector such as {$count :number}")
437
+ }
438
+ const ids = new Set<string>()
439
+ for (const variant of matcher.variants) {
440
+ if (variant.keys.length !== matcher.selectors.length) {
441
+ throw new MessageSyntaxError(
442
+ variant.at,
443
+ "a variant has " + variant.keys.length + " keys but .match has " + matcher.selectors.length + " selectors",
444
+ )
445
+ }
446
+ const id = JSON.stringify(variant.keys.map((key) => (key === null ? null : key.normalize("NFC"))))
447
+ if (ids.has(id)) throw new MessageSyntaxError(variant.at, "two variants have the same keys")
448
+ ids.add(id)
449
+ }
450
+ if (!matcher.variants.some((variant) => variant.keys.every((key) => key === null)))
451
+ throw new MessageSyntaxError(matcher.at, ".match needs a variant whose keys are all *")
452
+ }
453
+ const all = [
454
+ ...message.declarations.map((declaration) => declaration.expression),
455
+ ...expressionsOf(message.parts),
456
+ ...(matcher?.variants.flatMap((variant) => expressionsOf(variant.parts)) ?? []),
457
+ ]
458
+ for (const { call } of all) {
459
+ if (call && !Object.hasOwn(FUNCTIONS, call.name))
460
+ throw new MessageSyntaxError(call.at, "function :" + call.name + " is not supported")
461
+ }
462
+ }
463
+
464
+ /** One MessageFormat 2 message, parsed and checked; throws `MessageSyntaxError` with the offset of the fault. */
465
+ export function parseMf2(source: string): Message {
466
+ const message = new Parser(source).message()
467
+ check(message)
468
+ return message
469
+ }