B
    dZ              	   @  sV  U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	m
Z
mZ e	rVddlmZ dZdZd	d
dddZG dd dZdddddZdddddZdddddZd	dddddZd	ddddd Zd!d!d!d"d#d$ZG d%d& d&eZd'd(d)d*d+d,d-d.hZded/ejfd0ed1d2e d3fd4ed5fd6ed7fd8ed9ejfgZd:ed;< dd<d=d>d?Z dld<dd@dAdBdCdDZ!dmd<dd@dEdFdGZ"dHdIdJdKdLZ#ddMdNdOdPZ$ddQdRdSdTZ%dUdVdWdXdYZ&G dZd[ d[Z'd\d] Z(d^d_ Z)d`da Z*G dbdc dcZ+G ddde dee+Z,G dfdg dge+Z-G dhdi die-Z.G djdk dke+Z/dS )nz
    babel.numbers
    ~~~~~~~~~~~~~

    CLDR Plural support.  See UTS #35.

    :copyright: (c) 2013-2023 by the Babel Team.
    :license: BSD, see LICENSE for more details.
    )annotationsN)IterableMapping)TYPE_CHECKINGAnyCallable)Literal)zeroonetwofewmanyotherr   zfloat | decimal.DecimalzMtuple[decimal.Decimal | int, int, int, int, int, int, Literal[0], Literal[0]])sourcereturnc             C  s   t | }t|}t|tr6||kr(|}ntt|}t|tjr| }|j}|dk rf|j	|d nd}d
dd |D }|d}t|}t|}	t|pd}
t|pd}nd } }	 }
}d }}||||	|
|||fS )u  Extract operands from a decimal, a float or an int, according to `CLDR rules`_.

    The result is a 8-tuple (n, i, v, w, f, t, c, e), where those symbols are as follows:

    ====== ===============================================================
    Symbol Value
    ------ ---------------------------------------------------------------
    n      absolute value of the source number (integer and decimals).
    i      integer digits of n.
    v      number of visible fraction digits in n, with trailing zeros.
    w      number of visible fraction digits in n, without trailing zeros.
    f      visible fractional digits in n, with trailing zeros.
    t      visible fractional digits in n, without trailing zeros.
    c      compact decimal exponent value: exponent of the power of 10 used in compact decimal formatting.
    e      currently, synonym for ‘c’. however, may be redefined in the future.
    ====== ===============================================================

    .. _`CLDR rules`: https://www.unicode.org/reports/tr35/tr35-61/tr35-numbers.html#Operands

    :param source: A real number
    :type source: int|float|decimal.Decimal
    :return: A n-i-v-w-f-t-c-e tuple
    :rtype: tuple[decimal.Decimal, int, int, int, int, int, int, int]
    r   N  c             s  s   | ]}t |V  qd S )N)str).0dr   r   Y/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/babel/plural.py	<genexpr>G   s    z#extract_operands.<locals>.<genexpr>0)absint
isinstancefloatdecimalDecimalr   as_tupleexponentdigitsjoinrstriplen)r   niZ	dec_tupleexpZfraction_digitsZtrailingZno_trailingvwftcer   r   r   extract_operands   s&    

r.   c               @  s   e Zd ZdZdZdddddZdd	d
dZedd dddZe	dd	ddZ
e	dd	ddZdd	ddZdddddZdddddZdS ) 
PluralRuleaf  Represents a set of language pluralization rules.  The constructor
    accepts a list of (tag, expr) tuples or a dict of `CLDR rules`_. The
    resulting object is callable and accepts one parameter with a positive or
    negative number (both integer and float) for the number that indicates the
    plural form for a string and returns the tag for the format:

    >>> rule = PluralRule({'one': 'n is 1'})
    >>> rule(1)
    'one'
    >>> rule(2)
    'other'

    Currently the CLDR defines these tags: zero, one, two, few, many and
    other where other is an implicit default.  Rules should be mutually
    exclusive; for a given numeric value, only one rule should apply (i.e.
    the condition should only be true for one of the plural rule elements.

    .. _`CLDR rules`: https://www.unicode.org/reports/tr35/tr35-33/tr35-numbers.html#Language_Plural_Rules
    )abstract_funcz-Mapping[str, str] | Iterable[tuple[str, str]]None)rulesr   c             C  s   t |tr| }t }g | _xlt|D ]`\}}|tkrHtd|n||kr`td|d|| t	|j
}|r(| j||f q(W dS )a$  Initialize the rule instance.

        :param rules: a list of ``(tag, expr)``) tuples with the rules
                      conforming to UTS #35 or a dict with the tags as keys
                      and expressions as values.
        :raise RuleError: if the expression is malformed
        zunknown tag ztag z defined twiceN)r   r   itemssetr0   sorted_plural_tags
ValueErroradd_Parserastappend)selfr3   foundkeyexprr;   r   r   r   __init__j   s    


zPluralRule.__init__r   )r   c               s6   | j  d fddtD }dt| j d|dS )Nz, c               s&   g | ]}| kr| d  |  qS )z: r   )r   tag)r3   r   r   
<listcomp>   s    z'PluralRule.__repr__.<locals>.<listcomp>< >)r3   r"   r7   type__name__)r=   argsr   )r3   r   __repr__   s    zPluralRule.__repr__z:Mapping[str, str] | Iterable[tuple[str, str]] | PluralRulec             C  s   t |tr|S | |S )a
  Create a `PluralRule` instance for the given rules.  If the rules
        are a `PluralRule` object, that object is returned.

        :param rules: the rules as list or dict, or a `PluralRule` object
        :raise RuleError: if the expression is malformed
        )r   r/   )clsr3   r   r   r   parse   s    
zPluralRule.parsezMapping[str, str]c               s   t  j  fdd| jD S )zThe `PluralRule` as a dict of unicode plural rules.

        >>> rule = PluralRule({'one': 'n is 1'})
        >>> rule.rules
        {'one': 'n is 1'}
        c               s   i | ]\}} ||qS r   r   )r   rB   r;   )_compiler   r   
<dictcomp>   s    z$PluralRule.rules.<locals>.<dictcomp>)_UnicodeCompilercompiler0   )r=   r   )rM   r   r3      s    zPluralRule.ruleszfrozenset[str]c             C  s   t dd | jD S )zA set of explicitly defined tags in this rule.  The implicit default
        ``'other'`` rules is not part of this set unless there is an explicit
        rule for it.
        c             s  s   | ]}|d  V  qdS )r   Nr   )r   r&   r   r   r   r      s    z"PluralRule.tags.<locals>.<genexpr>)	frozensetr0   )r=   r   r   r   tags   s    zPluralRule.tagszlist[tuple[str, Any]]c             C  s   | j S )N)r0   )r=   r   r   r   __getstate__   s    zPluralRule.__getstate__)r0   r   c             C  s
   || _ d S )N)r0   )r=   r0   r   r   r   __setstate__   s    zPluralRule.__setstate__zfloat | decimal.Decimal)r%   r   c             C  s   t | dst| | _| |S )Nr1   )hasattr	to_pythonr1   )r=   r%   r   r   r   __call__   s    

zPluralRule.__call__N)rH   
__module____qualname____doc__	__slots__rA   rJ   classmethodrL   propertyr3   rR   rS   rT   rW   r   r   r   r   r/   S   s   r/   z:Mapping[str, str] | Iterable[tuple[str, str]] | PluralRuler   )ruler   c             C  sZ   t  j}dg}x2t| jD ]"\}}||| d|d qW |dt  d|S )a  Convert a list/dict of rules or a `PluralRule` object into a JavaScript
    function.  This function depends on no external library:

    >>> to_javascript({'one': 'n is 1'})
    "(function(n) { return (n == 1) ? 'one' : 'other'; })"

    Implementation detail: The function generated will probably evaluate
    expressions involved into range operations multiple times.  This has the
    advantage that external helper functions are not required and is not a
    big performance hit for these simple calculations.

    :param rule: the rules as list or dict, or a `PluralRule` object
    :raise RuleError: if the expression is malformed
    z(function(n) { return z ? z : z%r; })r   )_JavaScriptCompilerrP   r/   rL   r0   r<   _fallback_tagr"   )r^   Zto_jsresultrB   r;   r   r   r   to_javascript   s    rb   z(Callable[[float | decimal.Decimal], str]c             C  s   t tttd}t j}ddg}x6t| jD ]&\}}|	d|| dt
| q,W |	dt td|dd	}t|| |d
 S )a<  Convert a list/dict of rules or a `PluralRule` object into a regular
    Python function.  This is useful in situations where you need a real
    function and don't are about the actual rule object:

    >>> func = to_python({'one': 'n is 1', 'few': 'n in 2..4'})
    >>> func(1)
    'one'
    >>> func(3)
    'few'
    >>> func = to_python({'one': 'n in 1,11', 'few': 'n in 3..10,13..19'})
    >>> func(11)
    'one'
    >>> func(15)
    'few'

    :param rule: the rules as list or dict, or a `PluralRule` object
    :raise RuleError: if the expression is malformed
    )INZWITHINZMODr.   zdef evaluate(n):z- n, i, v, w, f, t, c, e = extract_operands(n)z if (z
): return z return 
z<rule>execevaluate)in_range_listwithin_range_listcldr_modulor.   _PythonCompilerrP   r/   rL   r0   r<   r   r`   r"   eval)r^   	namespaceZto_python_funcra   rB   r;   coder   r   r   rV      s    "
rV   c               s   t | } | jthB  t j} fddtD j}dt  dg}x0| j	D ]&\}}|
|| d|| d qLW |
|t d d|S )	a  The plural rule as gettext expression.  The gettext expression is
    technically limited to integers and returns indices rather than tags.

    >>> to_gettext({'one': 'n is 1', 'two': 'n is 2'})
    'nplurals=3; plural=((n == 1) ? 0 : (n == 2) ? 1 : 2);'

    :param rule: the rules as list or dict, or a `PluralRule` object
    :raise RuleError: if the expression is malformed
    c               s   g | ]}| kr|qS r   r   )r   rB   )	used_tagsr   r   rC      s    zto_gettext.<locals>.<listcomp>z	nplurals=z
; plural=(z ? z : z);r   )r/   rL   rR   r`   _GettextCompilerrP   r7   indexr$   r0   r<   r"   )r^   rM   Z
_get_indexra   rB   r;   r   )rn   r   
to_gettext   s    

"rq   z+Iterable[Iterable[float | decimal.Decimal]]bool)num
range_listr   c             C  s   | t | kot| |S )a  Integer range list test.  This is the callback for the "in" operator
    of the UTS #35 pluralization rule language:

    >>> in_range_list(1, [(1, 3)])
    True
    >>> in_range_list(3, [(1, 3)])
    True
    >>> in_range_list(3, [(1, 3), (5, 8)])
    True
    >>> in_range_list(1.2, [(1, 4)])
    False
    >>> in_range_list(10, [(1, 4)])
    False
    >>> in_range_list(10, [(1, 4), (6, 8)])
    False
    )r   rh   )rs   rt   r   r   r   rg     s    rg   c               s   t  fdd|D S )a  Float range test.  This is the callback for the "within" operator
    of the UTS #35 pluralization rule language:

    >>> within_range_list(1, [(1, 3)])
    True
    >>> within_range_list(1.0, [(1, 3)])
    True
    >>> within_range_list(1.2, [(1, 4)])
    True
    >>> within_range_list(8.8, [(1, 4), (7, 15)])
    True
    >>> within_range_list(10, [(1, 4)])
    False
    >>> within_range_list(10.5, [(1, 4), (20, 30)])
    False
    c             3  s"   | ]\}} |ko |kV  qd S )Nr   )r   Zmin_Zmax_)rs   r   r   r   +  s    z$within_range_list.<locals>.<genexpr>)any)rs   rt   r   )rs   r   rh     s    rh   r   )abr   c             C  s@   d}| dk r| d9 } d}|dk r(|d9 }| | }|r<|d9 }|S )zJavaish modulo.  This modulo operator returns the value with the sign
    of the dividend rather than the divisor like Python does:

    >>> cldr_modulo(-3, 5)
    -3
    >>> cldr_modulo(-3, -5)
    -3
    >>> cldr_modulo(3, 5)
    3
    r      r   )rv   rw   reverservr   r   r   ri   .  s    ri   c               @  s   e Zd ZdZdS )	RuleErrorzRaised if a rule is malformed.N)rH   rX   rY   rZ   r   r   r   r   r|   E  s   r|   r%   r&   r(   r)   r*   r+   r,   r-   z\s+wordz"\b(and|or|is|(?:with)?in|not|mod|[r   z])\bvaluez\d+symbolz%|,|!=|=ellipsisz\.{2,3}|\u2026z(list[tuple[str | None, re.Pattern[str]]]_RULESzlist[tuple[str, str]])sr   c             C  s   |  dd } g }d}t| }xb||k rxTtD ]<\}}|| |}|d k	r.| }|rh||| f P q.W td| |  q W |d d d S )N@r   z5malformed CLDR pluralization rule.  Got unexpected %rrx   )splitr$   r   matchendr<   groupr|   )r   ra   posr   tokr^   r   r   r   r   tokenize_rule]  s    
r   z
str | Nonezlist[tuple[str, str]] | bool)tokenstype_r~   r   c             C  s,   | o*| d d |ko*|d kp*| d d |kS )Nrx   r   ry   r   )r   r   r~   r   r   r   test_next_tokenp  s    r   )r   r   r~   c             C  s   t | ||r|  S d S )N)r   pop)r   r   r~   r   r   r   
skip_tokeny  s    r   r   z#tuple[Literal['value'], tuple[int]])r~   r   c             C  s
   d| ffS )Nr~   r   )r~   r   r   r   
value_node~  s    r   ztuple[str, tuple[()]])namer   c             C  s   | dfS )Nr   r   )r   r   r   r   
ident_node  s    r   zItuple[Literal['range_list'], Iterable[Iterable[float | decimal.Decimal]]])rt   r   c             C  s   d| fS )Nrt   r   )rt   r   r   r   range_list_node  s    r   ztuple[Any, ...]z-tuple[Literal['not'], tuple[tuple[Any, ...]]])r{   r   c             C  s
   d| ffS )Nnotr   )r{   r   r   r   negate  s    r   c               @  sb   e Zd ZdZdd ZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )r:   u  Internal parser.  This class can translate a single rule into an abstract
    tree of tuples. It implements the following grammar::

        condition     = and_condition ('or' and_condition)*
                        ('@integer' samples)?
                        ('@decimal' samples)?
        and_condition = relation ('and' relation)*
        relation      = is_relation | in_relation | within_relation
        is_relation   = expr 'is' ('not')? value
        in_relation   = expr (('not')? 'in' | '=' | '!=') range_list
        within_relation = expr ('not')? 'within' range_list
        expr          = operand (('mod' | '%') value)?
        operand       = 'n' | 'i' | 'f' | 't' | 'v' | 'w'
        range_list    = (range | value) (',' range_list)*
        value         = digit+
        digit         = 0|1|2|3|4|5|6|7|8|9
        range         = value'..'value
        samples       = sampleRange (',' sampleRange)* (',' ('…'|'...'))?
        sampleRange   = decimalValue '~' decimalValue
        decimalValue  = value ('.' value)?

    - Whitespace can occur between or around any of the above tokens.
    - Rules should be mutually exclusive; for a given numeric value, only one
      rule should apply (i.e. the condition should only be true for one of
      the plural rule elements).
    - The in and within relations can take comma-separated lists, such as:
      'n in 3,5,7..15'.
    - Samples are ignored.

    The translator parses the expression on instantiation into an attribute
    called `ast`.
    c             C  sF   t || _| jsd | _d S |  | _| jrBtd| jd d d S )NzExpected end of rule, got rx   ry   )r   r   r;   	conditionr|   )r=   stringr   r   r   rA     s    

z_Parser.__init__Nc             C  sn   t | j||}|d k	r|S |d kr6t|d kr0|p2|}| jsLtd| dtd| d| jd d d S )Nz	expected z but end of rule reachedz	 but got rx   ry   )r   r   reprr|   )r=   r   r~   termtokenr   r   r   expect  s    z_Parser.expectc             C  s0   |   }x"t| jddr*d||   ff}q
W |S )Nr}   or)and_conditionr   r   )r=   opr   r   r   r     s    z_Parser.conditionc             C  s0   |   }x"t| jddr*d||   ff}q
W |S )Nr}   and)relationr   r   )r=   r   r   r   r   r     s    z_Parser.and_conditionc             C  s   |   }t| jddr8t| jddr(dp*d||  ffS t| jdd}d}t| jddr^d}n$t| jdds|rxtd| |S d|||  ff}|rt|S |S )	Nr}   isr   ZisnotinZwithinz#Cannot negate operator based rules.r   )r@   r   r   r~   r|   newfangled_relationrt   r   )r=   leftnegatedmethodr{   r   r   r   r     s    
z_Parser.relationc             C  sR   t | jddrd}nt | jddr(d}ntddd||  ff}|rNt|S |S )	Nr   =Fz!=Tz'Expected "=" or "!=" or legacy relationr   r   )r   r   r|   rt   r   )r=   r   r   r{   r   r   r   r     s    z_Parser.newfangled_relationc             C  s,   |   }t| jdr ||   fS ||fS d S )Nr   )r~   r   r   )r=   r   r   r   r   range_or_value  s    z_Parser.range_or_valuec             C  s4   |   g}x t| jddr*||    qW t|S )Nr   ,)r   r   r   r<   r   )r=   rt   r   r   r   rt     s    
z_Parser.range_listc             C  s|   t | jd}|d ks |d tkr(td|d }t | jddrRd|df|  ffS t | jddrtd|df|  ffS t|S )Nr}   ry   zExpected identifier variablemodr   r   %)r   r   _VARSr|   r~   r   )r=   r}   r   r   r   r   r@     s    z_Parser.exprc             C  s   t t| dd S )Nr~   ry   )r   r   r   )r=   r   r   r   r~     s    z_Parser.value)NN)rH   rX   rY   rZ   rA   r   r   r   r   r   r   rt   r@   r~   r   r   r   r   r:     s    


r:   c               s    fddS )z%Compiler factory for the `_Compiler`.c               s    |  ||  |f S )N)rP   )r=   r   right)tmplr   r   <lambda>      z"_binary_compiler.<locals>.<lambda>r   )r   r   )r   r   _binary_compiler
  s    r   c               s    fddS )z%Compiler factory for the `_Compiler`.c               s    |  | S )N)rP   )r=   x)r   r   r   r     r   z!_unary_compiler.<locals>.<lambda>r   )r   r   )r   r   _unary_compiler  s    r   c             C  s   dS )Nr   r   )r   r   r   r   r     r   r   c               @  s   e Zd ZdZdd Zdd Zdd Zdd Zdd Zd	d Z	d
d Z
dd Zdd Zdd ZedZedZedZedZedZedZdd ZdS )	_CompilerzZThe compilers are able to transform the expressions into multiple
    output formats.
    c             C  s   |\}}t | d| | S )NZcompile_)getattr)r=   argr   rI   r   r   r   rP     s    z_Compiler.compilec             C  s   dS )Nr%   r   )r   r   r   r   r      r   z_Compiler.<lambda>c             C  s   dS )Nr&   r   )r   r   r   r   r   !  r   c             C  s   dS )Nr(   r   )r   r   r   r   r   "  r   c             C  s   dS )Nr)   r   )r   r   r   r   r   #  r   c             C  s   dS )Nr*   r   )r   r   r   r   r   $  r   c             C  s   dS )Nr+   r   )r   r   r   r   r   %  r   c             C  s   dS )Nr,   r   )r   r   r   r   r   &  r   c             C  s   dS )Nr-   r   )r   r   r   r   r   '  r   c             C  s   t |S )N)r   )r   r(   r   r   r   r   (  r   z
(%s && %s)z
(%s || %s)z(!%s)z
(%s %% %s)z
(%s == %s)z
(%s != %s)c             C  s
   t  d S )N)NotImplementedError)r=   r   r@   rt   r   r   r   compile_relation0  s    z_Compiler.compile_relationN)rH   rX   rY   rZ   rP   	compile_n	compile_i	compile_v	compile_w	compile_f	compile_tZ	compile_cZ	compile_eZcompile_valuer   compile_and
compile_orr   compile_notcompile_mod
compile_iscompile_isnotr   r   r   r   r   r     s$   r   c               @  s8   e Zd ZdZedZedZedZedZ	dd Z
dS )	rj   z!Compiles an expression to Python.z(%s and %s)z
(%s or %s)z(not %s)zMOD(%s, %s)c               s<   d  fdd|d D }|  d | d| dS )Nr   c               s.   g | ]&\}}d   | d  | dqS )(z, ))rP   )r   rv   rw   )r=   r   r   rC   =  s    z4_PythonCompiler.compile_relation.<locals>.<listcomp>ry   r   z, [z]))r"   upperrP   )r=   r   r@   rt   rangesr   )r=   r   r   <  s    z _PythonCompiler.compile_relationN)rH   rX   rY   rZ   r   r   r   r   r   r   r   r   r   r   r   rj   4  s   rj   c               @  s.   e Zd ZdZejZeZeZ	eZ
eZdd ZdS )ro   z)Compile into a gettext plural expression.c             C  s   g }|  |}xx|d D ]l}|d |d krP|d| d|  |d  d qt| j |\}}|d| d| d| d| d	 qW dd	| dS )
Nry   r   r   z == r   z >= z && z <= z || )rP   r<   mapr"   )r=   r   r@   rt   r{   itemminmaxr   r   r   r   J  s    
$(z!_GettextCompiler.compile_relationN)rH   rX   rY   rZ   r   r   r   compile_zeror   r   r   r   r   r   r   r   r   ro   A  s   ro   c               @  s0   e Zd ZdZdd ZeZeZeZeZ	dd Z
dS )r_   z/Compiles the expression to plain of JavaScript.c             C  s   dS )NzparseInt(n, 10)r   )r   r   r   r   r   [  r   z_JavaScriptCompiler.<lambda>c             C  s>   t | |||}|dkr:| |}d| d| d| d}|S )Nr   z
(parseInt(z	, 10) == z && r   )ro   r   rP   )r=   r   r@   rt   rm   r   r   r   r   a  s    
z$_JavaScriptCompiler.compile_relationN)rH   rX   rY   rZ   r   r   r   r   r   r   r   r   r   r   r   r_   V  s   r_   c               @  sJ   e Zd ZdZedZedZedZedZedZ	dd Z
dd
dZdS )rO   z+Returns a unicode pluralization rule again.z%s is %sz%s is not %sz	%s and %sz%s or %sz	%s mod %sc             C  s   | j |d ddiS )Nry   r   T)r   )r=   r   r   r   r   r   w  s    z_UnicodeCompiler.compile_notFc          	   C  s   g }x^|d D ]R}|d |d kr8| | |d  q| | |d  d| |d   qW | | |rvdnd d| dd| S )Nry   r   z..z notr   rE   r   )r<   rP   r"   )r=   r   r@   rt   r   r   r   r   r   r   r   z  s    ,z!_UnicodeCompiler.compile_relationN)F)rH   rX   rY   rZ   r   r   r   r   r   r   r   r   r   r   r   r   rO   j  s   rO   )N)N)0rZ   
__future__r   r   recollections.abcr   r   typingr   r   r   typing_extensionsr   r7   r`   r.   r/   rb   rV   rq   rg   rh   ri   	Exceptionr|   r   rP   UNICODEr"   r   __annotations__r   r   r   r   r   r   r   r:   r   r   r   r   rj   ro   r_   rO   r   r   r   r   <module>	   s^   ;](z