L20n Grammar

Axel Hecht, Zbigniew Braniecki, Stanisław Małolepszy
© 2007-2012 Mozilla Foundation
Version: 0.9

This is briefly annotated version of the grammar used for the proof-of-concept implementation of l20n.

The fileformat to be used for l20n is completely described by the following Localizable Objects List (lol) production. The file consists of entries and whitespaces.

lol : (WS | entry)* EOF ;

An entry is either an entity, a macro or a comment.

entry : entity | macro | comment | statement ;

A entity consists of an identifier, a value and an optional set of attributes. The optional index specifies which value to actually return when askng for the value of this entity, for example, this can specify which plural form to use.

entity : '<' identifier index? WS+ value attributes? WS? '>' ;
identifier : [_a-zA-Z]\w* ;
index : '[' WS? expression WS? ( ',' WS? expression WS? )*']' ;
attributes : ( WS+ keyValuePair )* ;

Statements are similar to C-style statements.

statement : import_statement ;
import_statement : 'import' WS? '(' WS? expression WS? ')' ;

Expressions are closely modeled after C expressions, merely binary operators and some C-specifics have been removed.

expression : conditional_expression ;
conditional_expression : logical_expression WS? ( '?' WS? expression ':' WS? expression )? ;
logical_expression : binary_expression (WS? ('||' | '&&') WS? logical_expression)? ;
binary_expression : unary_expression (WS? ('==' | '!=' | '<' | '>' | '<=' | '>=' | '+' | '-' | '*' | '/' | '%') WS? binary_expression)? ;
unary_expression : ( ('+' | '-' | '!') WS? unary_expression ) | member_expression ;
member_expression : call_expression | property_expression | attr_expression | parenthesis_expression ;
call_expression : member_expression WS? '(' WS? expression WS? ( ', ' WS? expression WS? )* ')' ;
property_expression : member_expression ( WS? '[' WS? expression WS? ']' | '.' identifier ) ;
attr_expression : member_expression ( WS? '.[' WS? expression WS? ']' | '..' identifier ) ;
parenthesis_expression : '(' WS? expression WS? ')' | primary_expression ;
primary_expression : literal | value | identifier_expression ;
identifier_expression : identifier | variable | globals_expression | this_expression ;
variable : '$' identifier ;
globals_expression : '@' identifier ;
this_expression : '~' ;
literal : number ;

One of the fundamental elements of l20n are values, which can be plain strings, strings with parameters, arrays or hashes. Both arrays and hashes have values as values, that is, they can be hierarchical.

value : string | array | hash ;
macro : '<' identifier '(' WS? ( variable WS? ( ',' WS? variable WS? )* )? ')' WS+ '{' WS? expression WS? '}' WS? '>' ;

The expander production used inside strings is used to reference expressions to be expanded within a string.

string : '\'' ([^'] | escape | expander )* '\'' | '"' ([^"] | escape | expander )* '"' ; | '\'\'\'' ( . | escape | expander )*? '\'\'\'' ; | '"""' ( . | escape | expander )*? '"""' ;
escape : '\' ('\' | '\'' | '"' | '{{') ;
expander : '{{' expression '}}' ;
array : '[' WS? value WS? ( ',' WS? value WS? )* ']' ;
hash : '{' WS? hashItem WS? ( ',' WS? hashItem WS? )* '}' ;
keyValuePair : identifier WS? ':' WS? value ;
hashItem : '*'? identifier WS? ':' WS? value ;

Comments are following doxygen style. The specified set of @-rules is yet to be determined.

comment : '/*' .*? '*/' ;
WS : \s+ ;

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 License.