# Copyright 2017 Google Inc.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import 'en/verbalizer/number_names.grm' as n;
import 'util/byte.grm' as bytelib;
import 'universal/thousands_punct.grm' as t;

cardinal = n.CARDINAL_NUMBER_NAME;
ordinal = n.ORDINAL_NUMBER_NAME;

# Putting these here since this grammar gets incorporated by all the others.

func I[expr] {
  return "" : expr;
}

func D[expr] {
  return expr : "";
}

separators = t.comma_thousands | t.no_delimiter;

# Language specific endings for ordinals.
d = bytelib.kDigit;
endings = "st" | "nd" | "rd" | "th";

st = (d* "1") - (d* "11");
nd = (d* "2") - (d* "12");
rd = (d* "3") - (d* "13");
th = Optimize[d* - st - nd - rd];
first = st ("st" : "");
second = nd ("nd" : "");
third = rd ("rd" : "");
other = th ("th" : "");
marked_ordinal = Optimize[first | second | third | other];

# The separator is a no-op here but will be needed once we replace
# the above targets.

export CARDINAL_NUMBERS = Optimize[separators @ cardinal];

export ORDINAL_NUMBERS =
  Optimize[(separators endings) @ marked_ordinal @ ordinal]
;

export ORDINAL_NUMBERS_UNMARKED = Optimize[separators @ ordinal];
