Skip to content

Commit

Permalink
Separate Expressions from Placeables
Browse files Browse the repository at this point in the history
These changes are inspired by the current code of the fluent.rs parser and the
discussion in projectfluent/fluent-rs#12.

The AST produced by the parser closely mimics the EBNF grammar:

  - patterns are vectors of text elements or placeables,
  - SelectExpression is only allowed in placeables,
  - the number type is used in tuple structs.

Renames in ASDL:
  - expr → plcbl
  - sel → expr
  - KeyValueArgument → NamedArgument

Renames in EBNF:
  - variable → external
  - expression merged into placeable
  - selector-expression → expression

Other changes:
  - added Pattern and Placeable constructors to the expr type
  - NamedArgument is no longer an Expression
  - CallExpression's callee is now a new type: fun
  • Loading branch information
stasm committed Feb 1, 2017
1 parent 61ef8fc commit 9453242
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
46 changes: 30 additions & 16 deletions fluent.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,49 @@ module Fluent
{
res = Resource(entry* body, comment? comment)

entry = Message(iden id, pat? value, attr* attrs, comment? comment)
entry = Message(iden id, pat? value, attr* attributes, comment? comment)
| Section(key key, comment? comment)
| Comment(comment)
| Junk(string body)

pat = Pattern(expr* elements, bool quoted)
-- Pattern values
pat = Pattern(elem* elements, bool quoted)
elem = String(string)
| Placeable(plcbl)

expr = Selector(sel)
| SelectExpression(sel? sel, var* vars)
-- Expressions allowed inside of braced Placeables
plcbl = Expression(expr)
| SelectExpression(expr? expr, var* vars)

sel = MessageReference(iden id)
| ExternalArgument(iden id)
| CallExpression(iden callee, expr* args)
| VariantExpression(iden msg, varkey key)
| AttributeExpression(iden msg, iden id)
| KeyValueArgument(iden name, argval val)
| Number(string value)
| String(string value)
-- Expressions allowed as selectors of SelectExpression and
-- arguments to CallExpression
expr = Pattern(pat)
| Number(number)
| MessageReference(iden id)
| ExternalArgument(iden id)
| AttributeExpression(iden id, iden name)
| VariantExpression(iden id, varkey key)
| CallExpression(fun callee, arg* args)
. | Placeable(plcbl)

-- Attributes of Message
attr = Attribute(iden id, pat value)

-- Variants of SelectExpression
var = Variant(varkey key, pat value, bool default)
varkey = Number(string value)
| Keyword(key name)
varkey = Number(number)
| Keyword(key)

argval = Number(string value)
| String(string value)
-- Arguments to CallExpression
arg = Expression(expr)
| NamedArgument(iden name, argval val)
argval = Number(number)
| String(string)

iden = (string name)
key = (string name)
fun = (string name)
number = (string value)

comment = (string body)
}
12 changes: 5 additions & 7 deletions fluent.ebnf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ __ ::= [#x20#x9]* /* space, tab */
NL ::= [#xA#xD]+ /* line feed, carriage return */

identifier ::= [a-zA-Z_?-] ([a-zA-Z0-9_?-])*
variable ::= '$' identifier
external ::= '$' identifier
keyword ::= [a-zA-Z_.?-] ([a-zA-Z0-9_.?- ]* [a-zA-Z0-9_.?-])?
builtin ::= [A-Z_?-]+
number ::= [0-9]+ ('.' [0-9]+)?
Expand All @@ -33,19 +33,17 @@ unquoted-text ::= ([^{] | '\{')+
quoted-text ::= ([^{"] | '\{' | '\"')+
block-text ::= NL __ '|' unquoted-pattern

placeable ::= '{' __ expression __ '}'
expression ::= selector-expression | select-expression | variant-list

selector-expression ::= quoted-pattern
placeable ::= '{' __ (expression | select-expression | variant-list) __ '}'
expression ::= quoted-pattern
| number
| identifier
| variable
| external
| attribute-expression
| variant-expression
| call-expression
| placeable

select-expression ::= selector-expression __ ' ->' __ variant-list
select-expression ::= expression __ ' ->' __ variant-list
attribute-expression ::= identifier '.' identifier
variant-expression ::= identifier '[' keyword ']'
call-expression ::= builtin '(' __ arglist? __ ')'
Expand Down

0 comments on commit 9453242

Please sign in to comment.