Skip to content

Commit

Permalink
Variant keys can now be numbers, identifiers or quoted text
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed May 21, 2018
1 parent 73170f0 commit f4a11ad
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 45 deletions.
11 changes: 11 additions & 0 deletions spec/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@
These issues have been fixed and the new test suite will help ensure the
correctness of the grammar in the future.

- Variant keys can now be numbers, identifiers or quoted text.

Previously, variant keys could either be numbers (`NumberExpressions`) or
text (`VariantNames`). Text keys allowed inline whitespace; the whitespace
at the extremes was trimmed. Special characters like `{` or `[` where
forbidden, but no espace sequences were allowed.

To fix this, variant keys can now be numbers (as before), identifiers
(`Identifier`) or quoted text (`StringExpressions`). The `VariantName`
AST node has been removed.

## 0.5.0 (January 31, 2018)

- Added terms. (#62, #85)
Expand Down
4 changes: 1 addition & 3 deletions spec/fluent.ebnf
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ VariantList ::= variant_list break_indent
variant_list ::= Variant* DefaultVariant Variant*
Variant ::= break_indent VariantKey inline_space? Pattern
DefaultVariant ::= break_indent "*" VariantKey inline_space? Pattern
VariantKey ::= "[" inline_space? (NumberExpression | VariantName) inline_space? "]"
VariantName ::= word (inline_space word)*
VariantKey ::= "[" inline_space? (NumberExpression | StringExpression | Identifier) inline_space? "]"

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

/* Characters */
backslash ::= "\\"
Expand Down
7 changes: 0 additions & 7 deletions syntax/ast.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,6 @@ export class Identifier extends SyntaxNode {
}
}

export class VariantName extends Identifier {
constructor(name) {
super(name);
this.type = "VariantName";
}
}

export class BaseComment extends Entry {
constructor(content) {
super();
Expand Down
26 changes: 2 additions & 24 deletions syntax/grammar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -307,24 +307,13 @@ let VariantKey = defer(() =>
char("["),
maybe(inline_space),
either(
// Meh. It's not really an expression.
NumberExpression,
VariantName),
StringExpression,
Identifier),
maybe(inline_space),
char("]"))
.map(element_at(2)));

let VariantName = defer(() =>
sequence(
word,
repeat(
sequence(
inline_space,
word)))
.map(flatten(2))
.map(join)
.map(into(FTL.VariantName)));

/* ----------- */
/* Identifiers */

Expand Down Expand Up @@ -375,17 +364,6 @@ let comment_line = defer(() =>
.map(keep_abstract)
.map(join));

let word = defer(() =>
repeat1(
and(
not(char("[")),
not(char("]")),
not(char("{")),
not(char("}")),
not(backslash),
regular_char))
.map(join));

/* ---------- */
/* Characters */

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/leading_dots.json
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "one"
},
"value": {
Expand All @@ -383,7 +383,7 @@
{
"type": "Variant",
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "other"
},
"value": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/member_expressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"name": "-term"
},
"key": {
"type": "VariantName",
"type": "Identifier",
"name": "case"
}
}
Expand Down
48 changes: 47 additions & 1 deletion test/fixtures/select_expressions.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ new-messages =
valid-selector =
{ -term.case ->
*[ many words ] value
*[key] value
}
invalid-selector =
Expand All @@ -23,3 +23,49 @@ empty-variant =
{ 1 ->
*[one] {""}
}
## Variant keys

valid-variant-key-identifier-simple =
{ 1 ->
*[key] value
}
valid-variant-key-identifier-padded =
{ 1 ->
*[ key ] value
}
# ERROR
invalid-variant-key-identifier-with-space-inside =
{ 1 ->
*[many words] value
}
# ERROR
invalid-variant-key-identifier-non-latin =
{ 1 ->
*[ĸəʎ] value
}
# ERROR
invalid-variant-key-number =
{ 1 ->
*[15 ducks] value
}
valid-variant-key-string-with-whitespace =
{ 1 ->
*[" many words "] value
}
valid-variant-key-string-non-latin =
{ 1 ->
*["keʎ"] value
}
valid-variant-key-string-start-with-digits =
{ 1 ->
*["15 ducks"] value
}
Loading

0 comments on commit f4a11ad

Please sign in to comment.