Skip to content

Add FunctionReference #210

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

Merged
merged 2 commits into from
Nov 8, 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
10 changes: 10 additions & 0 deletions spec/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@
Junk represents a literal slice of unparsed content and shouldn't have
its line endings normalized to LF.

- Add the `FunctionReference` production. (#210)

Function references in `CallExpressions` are now stored as
`FunctionReference` AST nodes, with an `id` field which is an
`Identifier`.

The `Function` production and its corresponding AST node have been
removed. The logic validating that function names are all upper-case has
been moved to `abstract.mjs`.

## 0.7.0 (October 15, 2018)

- Relax the indentation requirement. (#87)
Expand Down
6 changes: 3 additions & 3 deletions spec/fluent.ebnf
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ NumberLiteral ::= "-"? digit+ ("." digit+)?
MessageReference ::= Identifier
TermReference ::= "-" Identifier
VariableReference ::= "$" Identifier
CallExpression ::= Function blank? "(" blank? argument_list blank? ")"
FunctionReference ::= Identifier
CallExpression ::= FunctionReference blank? "(" blank? argument_list blank? ")"
argument_list ::= (Argument blank? "," blank?)* Argument?
Argument ::= NamedArgument
| InlineExpression
Expand All @@ -79,9 +80,8 @@ Variant ::= line_end blank? VariantKey blank_inline? Value
DefaultVariant ::= line_end blank? "*" VariantKey blank_inline? Value
VariantKey ::= "[" blank? (NumberLiteral | Identifier) blank? "]"

/* Identifiers */
/* Identifier */
Identifier ::= [a-zA-Z] [a-zA-Z0-9_-]*
Function ::= [A-Z] [A-Z_?-]*

/* Content Characters
*
Expand Down
14 changes: 12 additions & 2 deletions syntax/abstract.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function list_into(Type) {
|| (selector instanceof FTL.AttributeExpression
&& selector.ref instanceof FTL.MessageReference);
if (invalid_selector_found) {
return never("Invalid selector type: ${selector.type}.");
return never(`Invalid selector type: ${selector.type}.`);
}
let invalid_variants_found = variants.some(
variant => variant.value instanceof FTL.VariantList);
Expand All @@ -94,14 +94,24 @@ export function list_into(Type) {

export function into(Type) {
switch (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.");
}
return always(new Type(identifier));
};
case FTL.Placeable:
return expression => {
let invalid_expression_found =
expression instanceof FTL.AttributeExpression
&& expression.ref instanceof FTL.TermReference;
if (invalid_expression_found) {
return never(
"Invalid expression type: ${expression.type}.");
`Invalid expression type: ${expression.type}.`);
}
return always(new Type(expression));
};
Expand Down
15 changes: 8 additions & 7 deletions syntax/ast.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ export class VariableReference extends Expression {
}
}

export class FunctionReference extends Expression {
constructor(id) {
super();
this.type = "FunctionReference";
this.id = id;
}
}

export class SelectExpression extends Expression {
constructor(selector, variants) {
super();
Expand Down Expand Up @@ -222,13 +230,6 @@ export class ResourceComment extends BaseComment {
}
}

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

export class Junk extends SyntaxNode {
constructor(content) {
super();
Expand Down
18 changes: 6 additions & 12 deletions syntax/grammar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,12 @@ let VariableReference = defer(() =>
.map(element_at(1))
.chain(into(FTL.VariableReference)));

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

let CallExpression = defer(() =>
sequence(
Function.abstract,
FunctionReference.abstract,
maybe(blank),
string("("),
maybe(blank),
Expand Down Expand Up @@ -350,8 +353,8 @@ let VariantKey = defer(() =>
string("]"))
.map(element_at(2)));

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

let Identifier =
sequence(
Expand All @@ -362,15 +365,6 @@ let Identifier =
.map(join)
.chain(into(FTL.Identifier));

let Function =
sequence(
charset("A-Z"),
repeat(
charset("A-Z_?-")))
.map(flatten(1))
.map(join)
.chain(into(FTL.Function));

/* -------------------------------------------------------------------------- */
/* Content Characters
*
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/call_expressions.ftl
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## Callees

function-callee = {FUNCTION()}

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

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

## Arguments

positional-args = {FUN(1, "a", msg)}
named-args = {FUN(x: 1, y: "Y")}
dense-named-args = {FUN(x:1, y:"Y")}
Expand Down
Loading