Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parameterized Attributes #212

Merged
merged 3 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions spec/fluent.ebnf
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ block_placeable ::= blank_block blank_inline? inline_placeable
* syntax/abstract.mjs. */
InlineExpression ::= StringLiteral
| NumberLiteral
| VariableReference
| CallExpression
| AttributeExpression
| VariantExpression
| MessageReference
| TermReference
| VariableReference
| inline_placeable

/* Literals */
Expand All @@ -71,10 +71,11 @@ NumberLiteral ::= "-"? digit+ ("." digit+)?
MessageReference ::= Identifier
TermReference ::= "-" Identifier
VariableReference ::= "$" Identifier
FunctionReference ::= Identifier
CallExpression ::= Callee blank? "(" blank? argument_list blank? ")"
Callee ::= FunctionReference
CallExpression ::= CalleeExpression blank? "(" blank? argument_list blank? ")"
CalleeExpression ::= AttributeExpression
| FunctionReference
| TermReference
FunctionReference ::= Identifier
argument_list ::= (Argument blank? "," blank?)* Argument?
Argument ::= NamedArgument
| InlineExpression
Expand Down
58 changes: 42 additions & 16 deletions syntax/abstract.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import {always, never} from "../lib/combinators.mjs";

export function list_into(Type) {
switch (Type) {
case FTL.AttributeExpression:
return ([ref, name]) => {
let ref_is_valid =
ref instanceof FTL.MessageReference
|| ref instanceof FTL.TermReference;
if (ref_is_valid) {
return always(new Type(ref, name));
}
return never(`Invalid ref type: ${ref.type}.`);
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part is not needed with the reduced changes after the rebase? Attributes are always on messages or terms still.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, but I'd like to keep this here for the sake of completeness. I think it complements the validation of CallExpression. Let's keep it for now and I'll see if I can remove it in a follow-up.

case FTL.Comment:
return ([sigil, content = ""]) => {
switch (sigil) {
Expand All @@ -20,11 +30,19 @@ export function list_into(Type) {
case "###":
return always(new FTL.ResourceComment(content));
default:
return never(`Unknown comment sigil: ${sigil}`);
return never(`Unknown comment sigil: ${sigil}.`);
}
};
case FTL.CallExpression:
return ([callee, args]) => {
let callee_is_valid =
callee instanceof FTL.FunctionReference
|| callee instanceof FTL.TermReference
|| (callee instanceof FTL.AttributeExpression
&& callee.ref instanceof FTL.TermReference);
if (!callee_is_valid) {
return never(`Invalid callee type: ${callee.type}.`);
}
let positional_args = [];
let named_map = new Map();
for (let arg of args) {
Expand Down Expand Up @@ -64,24 +82,29 @@ export function list_into(Type) {
.filter(remove_blank_lines)));
case FTL.SelectExpression:
return ([selector, variants]) => {
let invalid_selector_found =
selector instanceof FTL.MessageReference
|| selector instanceof FTL.TermReference
|| (selector instanceof FTL.CallExpression
&& selector.callee instanceof FTL.TermReference)
|| selector instanceof FTL.VariantExpression
let selector_is_valid =
selector instanceof FTL.StringLiteral
|| selector instanceof FTL.NumberLiteral
|| selector instanceof FTL.VariableReference
|| (selector instanceof FTL.AttributeExpression
&& selector.ref instanceof FTL.MessageReference);
if (invalid_selector_found) {
&& selector.ref instanceof FTL.TermReference)
|| (selector instanceof FTL.CallExpression
&& selector.callee instanceof FTL.FunctionReference)
|| (selector instanceof FTL.CallExpression
&& selector.callee instanceof FTL.AttributeExpression);
if (!selector_is_valid) {
return never(`Invalid selector type: ${selector.type}.`);
}

// DEPRECATED
let invalid_variants_found = variants.some(
variant => variant.value instanceof FTL.VariantList);
if (invalid_variants_found) {
return never(
"VariantLists are only allowed inside of " +
"other VariantLists.");
}

return always(new Type(selector, variants));
};
case FTL.VariantList:
Expand All @@ -98,18 +121,21 @@ export function into(Type) {
case FTL.FunctionReference:
const VALID_FUNCTION_NAME = /^[A-Z][A-Z0-9_?-]*$/;
return identifier => {
if (!VALID_FUNCTION_NAME.test(identifier.name)) {
return never(
`Invalid function name: ${identifier.name}. ` +
"Function names must be upper-case.");
if (VALID_FUNCTION_NAME.test(identifier.name)) {
return always(new Type(identifier));
}
return always(new Type(identifier));
return never(
`Invalid function name: ${identifier.name}. `
+ "Function names must be all upper-case ASCII letters.");
};
case FTL.Placeable:
return expression => {
let invalid_expression_found =
expression instanceof FTL.AttributeExpression
&& expression.ref instanceof FTL.TermReference;
expression instanceof FTL.FunctionReference
|| (expression instanceof FTL.AttributeExpression
&& expression.ref instanceof FTL.TermReference)
|| (expression instanceof FTL.CallExpression
&& expression.callee instanceof FTL.AttributeExpression);
if (invalid_expression_found) {
return never(
`Invalid expression type: ${expression.type}.`);
Expand Down
17 changes: 9 additions & 8 deletions syntax/grammar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ let InlineExpression = defer(() =>
either(
StringLiteral,
NumberLiteral,
VariableReference,
CallExpression, // Must be before MessageReference
CallExpression,
AttributeExpression,
VariantExpression,
MessageReference,
TermReference,
VariableReference,
inline_placeable));

/* -------- */
Expand Down Expand Up @@ -256,12 +256,9 @@ let VariableReference = defer(() =>
.map(element_at(1))
.chain(into(FTL.VariableReference)));

let FunctionReference = defer(() =>
Identifier.chain(into(FTL.FunctionReference)));

let CallExpression = defer(() =>
sequence(
Callee.abstract,
CalleeExpression.abstract,
maybe(blank),
string("("),
maybe(blank),
Expand All @@ -271,10 +268,14 @@ let CallExpression = defer(() =>
.map(keep_abstract)
.chain(list_into(FTL.CallExpression)));

let Callee =
let CalleeExpression = defer(() =>
either(
AttributeExpression,
FunctionReference,
TermReference);
TermReference));

let FunctionReference = defer(() =>
Identifier.chain(into(FTL.FunctionReference)));

let argument_list = defer(() =>
sequence(
Expand Down
13 changes: 0 additions & 13 deletions test/fixtures/call_expressions.ftl
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
## Callees

function-callee = {FUNCTION()}
term-callee = {-term()}

# ERROR Equivalent to a MessageReference callee.
mixed-case-callee = {Function()}

# ERROR MessageReference is not a valid callee.
message-callee = {message()}
# ERROR VariableReference is not a valid callee.
variable-callee = {$variable()}

## Arguments

positional-args = {FUN(1, "a", msg)}
Expand Down
89 changes: 0 additions & 89 deletions test/fixtures/call_expressions.json
Original file line number Diff line number Diff line change
@@ -1,95 +1,6 @@
{
"type": "Resource",
"body": [
{
"type": "GroupComment",
"content": "Callees"
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "function-callee"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "Placeable",
"expression": {
"type": "CallExpression",
"callee": {
"type": "FunctionReference",
"id": {
"type": "Identifier",
"name": "FUNCTION"
}
},
"positional": [],
"named": []
}
}
]
},
"attributes": [],
"comment": null
},
{
"type": "Message",
"id": {
"type": "Identifier",
"name": "term-callee"
},
"value": {
"type": "Pattern",
"elements": [
{
"type": "Placeable",
"expression": {
"type": "CallExpression",
"callee": {
"type": "TermReference",
"id": {
"type": "Identifier",
"name": "term"
}
},
"positional": [],
"named": []
}
}
]
},
"attributes": [],
"comment": null
},
{
"type": "Comment",
"content": "ERROR Equivalent to a MessageReference callee."
},
{
"type": "Junk",
"annotations": [],
"content": "mixed-case-callee = {Function()}\n\n"
},
{
"type": "Comment",
"content": "ERROR MessageReference is not a valid callee."
},
{
"type": "Junk",
"annotations": [],
"content": "message-callee = {message()}\n"
},
{
"type": "Comment",
"content": "ERROR VariableReference is not a valid callee."
},
{
"type": "Junk",
"annotations": [],
"content": "variable-callee = {$variable()}\n\n"
},
{
"type": "GroupComment",
"content": "Arguments"
Expand Down
46 changes: 46 additions & 0 deletions test/fixtures/callee_expressions.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Callees in placeables.

function-callee-placeable = {FUNCTION()}
term-callee-placeable = {-term()}

# ERROR Messages cannot be parameterized.
message-callee-placeable = {message()}
# ERROR Equivalent to a MessageReference callee.
mixed-case-callee-placeable = {Function()}
# ERROR Message attributes cannot be parameterized.
message-attr-callee-placeable = {message.attr()}
# ERROR Term attributes may not be used in Placeables.
term-attr-callee-placeable = {-term.attr()}
# ERROR Variables cannot be parameterized.
variable-callee-placeable = {$variable()}


## Callees in selectors.

function-callee-selector = {FUNCTION() ->
*[key] Value
}
term-attr-callee-selector = {-term.attr() ->
*[key] Value
}

# ERROR Messages cannot be parameterized.
message-callee-selector = {message() ->
*[key] Value
}
# ERROR Equivalent to a MessageReference callee.
mixed-case-callee-selector = {Function() ->
*[key] Value
}
# ERROR Message attributes cannot be parameterized.
message-attr-callee-selector = {message.attr() ->
*[key] Value
}
# ERROR Term values may not be used as selectors.
term-callee-selector = {-term() ->
*[key] Value
}
# ERROR Variables cannot be parameterized.
variable-callee-selector = {$variable() ->
*[key] Value
}
Loading