Packages
ex_cldr_numbers
2.4.3
2.38.3
2.38.2
2.38.1
2.38.0
2.37.0
2.36.0
2.35.2
2.35.1
2.35.0
2.34.1
2.34.0
2.33.6
2.33.5
2.33.4
2.33.3
2.33.2
2.33.1
2.33.0
2.32.4
2.32.3
2.32.2
2.32.1
2.32.0
retired
2.31.3
retired
2.31.2
2.31.1
2.31.0
2.30.1
2.30.0
2.29.0
2.28.0
2.27.3
2.27.2
2.27.1
2.27.0
2.26.0
2.25.2
2.25.1
2.25.0
2.24.0
2.23.3
2.23.2
2.23.1
2.23.0
2.23.0-rc.4
2.23.0-rc.3
2.23.0-rc.2
2.23.0-rc.1
2.23.0-rc.0
2.22.1
2.22.0
2.21.0
2.20.0
2.19.0
2.18.4
2.18.3
2.18.2
2.18.1
2.18.0
2.17.0
2.17.0-rc.1
2.17.0-rc.0
2.16.1
2.16.0
2.16.0-rc.0
2.15.4
2.15.3
2.15.2
2.15.1
2.15.0
2.14.0
2.13.2
2.13.1
2.13.0
2.13.0-rc.0
2.12.1
2.12.0
2.11.0
2.10.0
2.9.0
2.8.0
2.7.2
2.7.1
2.7.0
2.6.4
2.6.3
2.6.2
2.6.1
2.6.0
2.5.0
2.4.4
2.4.3
2.4.2
2.4.1
2.4.0
2.3.0
2.2.0
2.1.1
2.1.0
2.0.0
2.0.0-rc.0
1.6.0
1.5.2
1.5.1
1.5.0
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.1
1.3.0
1.3.0-rc.1
1.3.0-rc.0
retired
1.2.0
1.1.0
1.0.1
1.0.0
1.0.0-rc.0
retired
0.3.3
0.3.2
retired
0.3.1
retired
0.3.0
retired
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0
Number and currency localization and formatting functions for the Common Locale Data Repository (CLDR).
Current section
Files
Jump to
Current section
Files
src/rbnf_parser.yrl
Nonterminals quotient_rule modulo_rule invoke_rule cardinal_rule ordinal_rule
rule literal rbnf_rule rule_part character.
Terminals rule_cardinal_start rule_ordinal_start number_format
rule_name conditional_start conditional_end modulo_call
quotient_call rule_call plural_rules right_paren
left_paren dollar comma char.
Rootsymbol rbnf_rule.
Left 100 rbnf_rule.
Right 400 modulo_call.
Right 500 quotient_call.
Right 600 character.
rbnf_rule -> rule_part rbnf_rule : ['$1'] ++ '$2'.
rbnf_rule -> rule_part : ['$1'].
rule_part -> literal : '$1'.
rule_part -> quotient_rule : '$1'.
rule_part -> modulo_rule : '$1'.
rule_part -> invoke_rule : '$1'.
rule_part -> cardinal_rule : '$1'.
rule_part -> ordinal_rule : '$1'.
rule_part -> conditional_start rbnf_rule conditional_end : {conditional, '$2'}.
quotient_rule -> quotient_call rule quotient_call quotient_call : {quotient, '$2'}.
quotient_rule -> quotient_call rule quotient_call : {quotient, '$2'}.
quotient_rule -> quotient_call quotient_call : {quotient, nil}.
modulo_rule -> modulo_call rule modulo_call : {modulo, '$2'}.
modulo_rule -> modulo_call modulo_call : {modulo, nil}.
% Note that here we are treating >>> as equivalent to >>
% This is not strictly true since the spec says we should
% ... but bypass the normal rule-selection process and just
% use the rule that precedes this one in this rule list.
modulo_rule -> modulo_call modulo_call modulo_call : {modulo, nil}.
invoke_rule -> rule_call rule rule_call : {call, '$2'}.
cardinal_rule -> dollar left_paren rule_cardinal_start comma
plural_rules right_paren dollar : {cardinal, to_map('$5')}.
ordinal_rule -> dollar left_paren rule_ordinal_start comma
plural_rules right_paren dollar : {ordinal, to_map('$5')}.
rule -> rule_name : {rule, normalize_rule_name('$1')}.
rule -> number_format : {format, unwrap('$1')}.
literal -> character literal : append('$1', '$2').
literal -> character : {literal, unwrap('$1')}.
character -> char : '$1'.
character -> comma : '$1'.
character -> number_format : '$1'.
Erlang code.
% Consolidate characters into a binary
append({char, _, _} = Char, {literal, Literal}) ->
{literal, list_to_binary([unwrap(Char), Literal])};
append({comma, _, _} = Char, {literal, Literal}) ->
{literal, list_to_binary([unwrap(Char), Literal])};
append({number_format, _, _} = Char, {literal, Literal}) ->
{literal, list_to_binary([unwrap(Char), Literal])};
append({literal, Literal1}, {literal, Literal2}) ->
{literal, list_to_binary([Literal2, Literal1])}.
% We will turn rule names into functions later on so
% we normalise the names to a format that is acceptable.
normalize_rule_name({_,_,[$%, $% | Name]}) ->
erlang:binary_to_atom(unicode:characters_to_binary(underscore(Name)), utf8);
normalize_rule_name({_,_,[$% | Name]}) ->
erlang:binary_to_atom(unicode:characters_to_binary(underscore(Name)), utf8).
% Return a token value as a binary
unwrap({_,_,V}) when is_list(V) -> unicode:characters_to_binary(V);
unwrap({_,_,V}) -> V.
% Substitute "_" for "-" since we will use these rule names
% as functions later on.
underscore([$-| Rest]) ->
[$_ | underscore(Rest)];
underscore([]) ->
[];
underscore([Char | Rest]) ->
[Char | underscore(Rest)].
% Convert ordinal and cardinal rules into a map
to_map(Plurals) ->
String = unwrap(Plurals),
Parts = binary:split(String, [<<"{">>,<<"}">>], [global, trim]),
Proplist = to_proplist(Parts),
maps:from_list(Proplist).
% Convert a list into a proplist
to_proplist([K,V | T]) -> [{erlang:binary_to_atom(K, utf8),V} | to_proplist(T)];
to_proplist([]) -> [].