Skip to content

Commit f4a11ad

Browse files
committed
Variant keys can now be numbers, identifiers or quoted text
1 parent 73170f0 commit f4a11ad

File tree

9 files changed

+326
-45
lines changed

9 files changed

+326
-45
lines changed

spec/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
These issues have been fixed and the new test suite will help ensure the
3838
correctness of the grammar in the future.
3939

40+
- Variant keys can now be numbers, identifiers or quoted text.
41+
42+
Previously, variant keys could either be numbers (`NumberExpressions`) or
43+
text (`VariantNames`). Text keys allowed inline whitespace; the whitespace
44+
at the extremes was trimmed. Special characters like `{` or `[` where
45+
forbidden, but no espace sequences were allowed.
46+
47+
To fix this, variant keys can now be numbers (as before), identifiers
48+
(`Identifier`) or quoted text (`StringExpressions`). The `VariantName`
49+
AST node has been removed.
50+
4051
## 0.5.0 (January 31, 2018)
4152

4253
- Added terms. (#62, #85)

spec/fluent.ebnf

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ VariantList ::= variant_list break_indent
6060
variant_list ::= Variant* DefaultVariant Variant*
6161
Variant ::= break_indent VariantKey inline_space? Pattern
6262
DefaultVariant ::= break_indent "*" VariantKey inline_space? Pattern
63-
VariantKey ::= "[" inline_space? (NumberExpression | VariantName) inline_space? "]"
64-
VariantName ::= word (inline_space word)*
63+
VariantKey ::= "[" inline_space? (NumberExpression | StringExpression | Identifier) inline_space? "]"
6564

6665
/* Identifiers */
6766
Identifier ::= identifier
@@ -73,7 +72,6 @@ Function ::= [A-Z] [A-Z_?-]*
7372
identifier ::= [a-zA-Z] [a-zA-Z0-9_-]*
7473
comment_line ::= (line_end)
7574
| ("\u0020" /.*/ line_end)
76-
word ::= (regular_char - backslash - "}" - "{" - "]" - "[")+
7775

7876
/* Characters */
7977
backslash ::= "\\"

syntax/ast.mjs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,6 @@ export class Identifier extends SyntaxNode {
191191
}
192192
}
193193

194-
export class VariantName extends Identifier {
195-
constructor(name) {
196-
super(name);
197-
this.type = "VariantName";
198-
}
199-
}
200-
201194
export class BaseComment extends Entry {
202195
constructor(content) {
203196
super();

syntax/grammar.mjs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -307,24 +307,13 @@ let VariantKey = defer(() =>
307307
char("["),
308308
maybe(inline_space),
309309
either(
310-
// Meh. It's not really an expression.
311310
NumberExpression,
312-
VariantName),
311+
StringExpression,
312+
Identifier),
313313
maybe(inline_space),
314314
char("]"))
315315
.map(element_at(2)));
316316

317-
let VariantName = defer(() =>
318-
sequence(
319-
word,
320-
repeat(
321-
sequence(
322-
inline_space,
323-
word)))
324-
.map(flatten(2))
325-
.map(join)
326-
.map(into(FTL.VariantName)));
327-
328317
/* ----------- */
329318
/* Identifiers */
330319

@@ -375,17 +364,6 @@ let comment_line = defer(() =>
375364
.map(keep_abstract)
376365
.map(join));
377366

378-
let word = defer(() =>
379-
repeat1(
380-
and(
381-
not(char("[")),
382-
not(char("]")),
383-
not(char("{")),
384-
not(char("}")),
385-
not(backslash),
386-
regular_char))
387-
.map(join));
388-
389367
/* ---------- */
390368
/* Characters */
391369

test/fixtures/leading_dots.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@
366366
{
367367
"type": "Variant",
368368
"key": {
369-
"type": "VariantName",
369+
"type": "Identifier",
370370
"name": "one"
371371
},
372372
"value": {
@@ -383,7 +383,7 @@
383383
{
384384
"type": "Variant",
385385
"key": {
386-
"type": "VariantName",
386+
"type": "Identifier",
387387
"name": "other"
388388
},
389389
"value": {

test/fixtures/member_expressions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"name": "-term"
2121
},
2222
"key": {
23-
"type": "VariantName",
23+
"type": "Identifier",
2424
"name": "case"
2525
}
2626
}

test/fixtures/select_expressions.ftl

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ new-messages =
66
77
valid-selector =
88
{ -term.case ->
9-
*[ many words ] value
9+
*[key] value
1010
}
1111
1212
invalid-selector =
@@ -23,3 +23,49 @@ empty-variant =
2323
{ 1 ->
2424
*[one] {""}
2525
}
26+
27+
28+
## Variant keys
29+
30+
valid-variant-key-identifier-simple =
31+
{ 1 ->
32+
*[key] value
33+
}
34+
35+
valid-variant-key-identifier-padded =
36+
{ 1 ->
37+
*[ key ] value
38+
}
39+
40+
# ERROR
41+
invalid-variant-key-identifier-with-space-inside =
42+
{ 1 ->
43+
*[many words] value
44+
}
45+
46+
# ERROR
47+
invalid-variant-key-identifier-non-latin =
48+
{ 1 ->
49+
*[ĸəʎ] value
50+
}
51+
52+
# ERROR
53+
invalid-variant-key-number =
54+
{ 1 ->
55+
*[15 ducks] value
56+
}
57+
58+
valid-variant-key-string-with-whitespace =
59+
{ 1 ->
60+
*[" many words "] value
61+
}
62+
63+
valid-variant-key-string-non-latin =
64+
{ 1 ->
65+
*["keʎ"] value
66+
}
67+
68+
valid-variant-key-string-start-with-digits =
69+
{ 1 ->
70+
*["15 ducks"] value
71+
}

0 commit comments

Comments
 (0)