o
    ¡ØÆh–A  ã                   @   s0  d Z ddlmZ ddlmZmZmZmZ ddlZ	ddl
mZmZmZ ddlmZmZ ddlmZ dd	„ Zd
d„ Zeƒ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ ZG dd„ deƒZ d(d!d"„Z!d)d$d%„Z"d(d&d'„Z#dS )*zESubexpressions that make up a parsed grammar

These do the parsing.

é    )Údefaultdict)ÚgetfullargspecÚ
isfunctionÚismethodÚismethoddescriptorN)Ú
ParseErrorÚIncompleteParseErrorÚLeftRecursionError)ÚNodeÚ	RegexNode)Ú
StrAndReprc                    s    t ttg}t‡ fdd„|D ƒƒS )Nc                    s   g | ]}|ˆ ƒ‘qS © r   )Ú.0Ú	criterion©Úvaluer   úe/var/www/html/HyperOracle/crypto_trader/venv/lib/python3.10/site-packages/parsimonious/expressions.pyÚ
<listcomp>   s    zis_callable.<locals>.<listcomp>)r   r   r   Úany)r   Úcriteriar   r   r   Úis_callable   s   
r   c                    s€   t ˆ ƒrtˆ dƒrˆ j‰ ttˆ ƒjƒ}tˆ ƒr|d8 }|dkr"d‰n|dkr)d‰ntd| ƒ‚G ‡ ‡‡fdd	„d	tƒ}||d
�S )aW  Turn a plain callable into an Expression.

    The callable can be of this simple form::

        def foo(text, pos):
            '''If this custom expression matches starting at text[pos], return
            the index where it stops matching. Otherwise, return None.'''
            if the expression matched:
                return end_pos

    If there child nodes to return, return a tuple::

        return end_pos, children

    If the expression doesn't match at the given ``pos`` at all... ::

        return None

    If your callable needs to make sub-calls to other rules in the grammar or
    do error reporting, it can take this form, gaining additional arguments::

        def foo(text, pos, cache, error, grammar):
            # Call out to other rules:
            node = grammar['another_rule'].match_core(text, pos, cache, error)
            ...
            # Return values as above.

    The return value of the callable, if an int or a tuple, will be
    automatically transmuted into a :class:`~parsimonious.Node`. If it returns
    a Node-like class directly, it will be passed through unchanged.

    :arg rule_name: The rule name to attach to the resulting
        :class:`~parsimonious.Expression`
    :arg grammar: The :class:`~parsimonious.Grammar` this expression will be a
        part of, to make delegating to other rules possible

    Ú__func__é   é   Té   Fz@Custom rule functions must take either 2 or 5 arguments, not %s.c                       s(   e Zd Z‡ ‡‡fdd„Z‡ fdd„ZdS )z#expression.<locals>.AdHocExpressionc                    s^   ˆrˆ ||ƒnˆ ||||ˆƒ}t |tƒr|d }}nt |tƒr$|\}}n|S t| ||||d�S ©N)Úchildren)Ú
isinstanceÚintÚtupler
   )ÚselfÚtextÚposÚcacheÚerrorÚresultÚendr   ©ÚcallableÚgrammarÚ	is_simpler   r   Ú_uncached_matchP   s   ÿ


z3expression.<locals>.AdHocExpression._uncached_matchc                    s
   dˆ j  S )Nz{custom function "%s"})Ú__name__©r    )r(   r   r   Ú_as_rhs]   ó   
z+expression.<locals>.AdHocExpression._as_rhsN)r,   Ú
__module__Ú__qualname__r+   r.   r   r'   r   r   ÚAdHocExpressionO   s    r2   )Úname)	r   Úhasattrr   Úlenr   Úargsr   ÚRuntimeErrorÚ
Expression)r(   Ú	rule_namer)   Únum_argsr2   r   r'   r   Ú
expression   s   )ÿ
r;   c                   @   s†   e Zd ZdZddgZd!dd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
d"dd„Zd"dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd S )#r8   z3A thing that can be matched against a piece of textr3   Úidentity_tupleÚ c                 C   s   || _ | j f| _d S ©N)r3   r<   )r    r3   r   r   r   Ú__init__o   s   zExpression.__init__c                 C   ó
   t | jƒS r>   )Úhashr<   r-   r   r   r   Ú__hash__s   r/   zExpression.__hash__c                 C   s   |   |tƒ ¡S r>   )Ú_eq_check_cyclesÚset©r    Úotherr   r   r   Ú__eq__v   s   zExpression.__eq__c                 C   s
   | |k S r>   r   rE   r   r   r   Ú__ne__y   r/   zExpression.__ne__c                 C   s.   |  t| ƒt|ƒf¡ |j| ju o| j|jkS r>   )ÚaddÚidÚ	__class__r<   ©r    rF   Úcheckedr   r   r   rC   |   s   zExpression._eq_check_cyclesc                 C   s   | S r>   r   ©r    Úrule_mapr   r   r   Úresolve_refs�   s   zExpression.resolve_refsr   c                 C   s.   | j ||d�}|jt|ƒk rt||j| ƒ‚|S )zßReturn a parse tree of ``text``.

        Raise ``ParseError`` if the expression wasn't satisfied. Raise
        ``IncompleteParseError`` if the expression was satisfied but didn't
        consume the full string.

        )r"   )Úmatchr&   r5   r   )r    r!   r"   Únoder   r   r   Úparse…   s   zExpression.parsec                 C   s,   t |ƒ}|  ||ttƒ|¡}|du r|‚|S )a  Return the parse tree matching this expression at the given
        position, not necessarily extending all the way to the end of ``text``.

        Raise ``ParseError`` if there is no match there.

        :arg pos: The index at which to start matching

        N)r   Ú
match_corer   Údict)r    r!   r"   r$   rR   r   r   r   rQ   ’   s
   	zExpression.matchc                 C   sŽ   |t | ƒ }||v r|| }nt||< |  ||||¡ }||< |tu r*t|d| d�‚|du rE||jkrE| js?t|jddƒdu rE| |_||_|S )a�  Internal guts of ``match()``

        This is appropriate to call only from custom rules or Expression
        subclasses.

        :arg cache: The packrat cache::

            {(oid, pos): Node tree matched by object `oid` at index `pos` ...}

        :arg error: A ParseError instance with ``text`` already filled in but
            otherwise blank. We update the error reporting info on this object
            as we go. (Sticking references on an existing instance is faster
            than allocating a new one for each expression that fails.) We
            return None rather than raising and catching ParseErrors because
            catching is slow.

        éÿÿÿÿ)r"   ÚexprNr3   )rJ   ÚIN_PROGRESSr+   r	   r"   r3   ÚgetattrrW   )r    r!   r"   r#   r$   Ú
expr_cacherR   r   r   r   rT   ¡   s   
ÿzExpression.match_corec                 C   s   d| j j|  ¡ f S )Nz<%s %s>)rK   r,   Úas_ruler-   r   r   r   Ú__str__Ö   s   þzExpression.__str__c                 C   sD   |   ¡  ¡ }| d¡r| d¡r|dd… }| jr d| j|f S |S )z–Return the left- and right-hand sides of a rule that represents me.

        Return unicode. If I have no ``name``, omit the left-hand side.

        ú(ú)r   rV   z%s = %s)r.   ÚstripÚ
startswithÚendswithr3   )r    Úrhsr   r   r   r[   Û   s   zExpression.as_rulec                 C   s   dd„ | j D ƒS )z Return an iterable of my unicode-represented children, stopping
        descent when we hit a named node so the returned value resembles the
        input rule.c                 S   s   g | ]	}|j p
| ¡ ‘qS r   )r3   r.   ©r   Úmr   r   r   r   ë   s    z/Expression._unicode_members.<locals>.<listcomp>)Úmembersr-   r   r   r   Ú_unicode_membersç   ó   zExpression._unicode_membersc                 C   s   t ‚)zfReturn the right-hand side of a rule that represents me.

        Implemented by subclasses.

        )ÚNotImplementedErrorr-   r   r   r   r.   í   s   zExpression._as_rhsN©r=   )r   )r,   r0   r1   Ú__doc__Ú	__slots__r?   rB   rG   rH   rC   rP   rS   rQ   rT   r\   r[   rf   r.   r   r   r   r   r8   f   s     


5r8   c                       s8   e Zd ZdZdgZd
‡ fdd„	Zdd„ Zdd	„ Z‡  ZS )ÚLiteralzFA string literal

    Use these if you can; they're the fastest.

    Úliteralr=   c                    s    t ƒ  |¡ || _||f| _d S r>   )Úsuperr?   rm   r<   )r    rm   r3   ©rK   r   r   r?   þ   s   zLiteral.__init__c                 C   s*   |  | j|¡rt| |||t| jƒ ƒS d S r>   )r`   rm   r
   r5   )r    r!   r"   r#   r$   r   r   r   r+     s   ÿzLiteral._uncached_matchc                 C   r@   r>   )Úreprrm   r-   r   r   r   r.     r/   zLiteral._as_rhsri   )	r,   r0   r1   rj   rk   r?   r+   r.   Ú__classcell__r   r   ro   r   rl   ö   s    rl   c                   @   s   e Zd ZdZdd„ ZdS )ÚTokenMatcherziAn expression matching a single token of a given type

    This is for use only with TokenGrammars.

    c                 C   s&   || j | jkrt| |||d ƒS d S )Nr   )Útyperm   r
   )r    Ú
token_listr"   r#   r$   r   r   r   r+     s   ÿzTokenMatcher._uncached_matchN)r,   r0   r1   rj   r+   r   r   r   r   rr     s    rr   c                       sD   e Zd ZdZdgZ		d‡ fdd„	Zdd„ Zd	d
„ Zdd„ Z‡  Z	S )ÚRegexz‘An expression that matches what a regex does.

    Use these as much as you can and jam as much into each one as you can;
    they're fast.

    Úrer=   Fc
           
         sn   t ƒ  |¡ t ||otj|otjB |otjB |otjB |o tjB |o%tj	B |	o*tj
B ¡| _| j| jf| _d S r>   )rn   r?   rv   ÚcompileÚIÚLÚMÚSÚUÚXÚAr3   r<   )
r    Úpatternr3   Úignore_caseÚlocaleÚ	multilineÚdot_allÚunicodeÚverboseÚasciiro   r   r   r?     s   ÿþýüûúzRegex.__init__c                 C   sJ   | j  ||¡}|dur#| ¡ }t| ||||d  |d  ƒ}||_|S dS )z-Return length of match, ``None`` if no match.Nr   r   )rv   rQ   Úspanr   )r    r!   r"   r#   r$   rd   r‡   rR   r   r   r   r+   +  s   üzRegex._uncached_matchc                    s,   d‰d  ‡ ‡fdd„tdtˆƒd ƒD ƒ¡S )zAReturn the textual equivalent of numerically encoded regex flags.Úilmsuxar=   c                 3   s,   � | ]}d |> ˆ @ rˆ|d   ndV  qdS )r   r=   Nr   )r   Úi©ÚbitsÚflagsr   r   Ú	<genexpr>7  s   €* z/Regex._regex_flags_from_bits.<locals>.<genexpr>r   )ÚjoinÚranger5   )r    r‹   r   rŠ   r   Ú_regex_flags_from_bits4  s   (zRegex._regex_flags_from_bitsc                 C   s   d  | jj|  | jj¡¡S )Nz~{!r}{})Úformatrv   r   r�   rŒ   r-   r   r   r   r.   9  s   
ÿzRegex._as_rhs)r=   FFFFFFF)
r,   r0   r1   rj   rk   r?   r+   r�   r.   rq   r   r   ro   r   ru     s    ÿ	ru   c                       sB   e Zd ZdZdgZ‡ fdd„Zdd„ Z‡ fdd„Zd	d
„ Z‡  Z	S )ÚCompoundz7An abstract expression which contains other expressionsre   c                    s   t ƒ  | dd¡¡ || _dS )z)``members`` is a sequence of expressions.r3   r=   N)rn   r?   Úgetre   )r    re   Úkwargsro   r   r   r?   C  s   
zCompound.__init__c                    s   t ‡ fdd„| jD ƒƒ| _| S )Nc                 3   s   � | ]}|  ˆ ¡V  qd S r>   )rP   rc   ©rO   r   r   r�   I  s   € z(Compound.resolve_refs.<locals>.<genexpr>)r   re   rN   r   r•   r   rP   H  s   zCompound.resolve_refsc                    sB   t ƒ  |ˆ ¡o t| jƒt|jƒko t‡ fdd„t| j|jƒD ƒƒS )Nc                 3   s4   � | ]\}}t |ƒt |ƒfˆ vr| |ˆ ¡V  qd S r>   )rJ   rC   )r   rd   Úmo©rM   r   r   r�   P  s   €2 z,Compound._eq_check_cycles.<locals>.<genexpr>)rn   rC   r5   re   ÚallÚziprL   ro   r—   r   rC   L  s
   ÿýzCompound._eq_check_cyclesc                 C   s   t | j| jfƒS r>   )rA   rK   r3   r-   r   r   r   rB   S  rg   zCompound.__hash__)
r,   r0   r1   rj   rk   r?   rP   rC   rB   rq   r   r   ro   r   r’   >  s    r’   c                   @   ó    e Zd ZdZdd„ Zdd„ ZdS )ÚSequencez¼A series of expressions that must match contiguous, ordered pieces of
    the text

    In other words, it's a concatenation operator: each piece has to match, one
    after another.

    c           
      C   s`   |}g }| j D ] }| ||||¡}|d u r d S | |¡ |j|j }	||	7 }qt| ||||ƒS r>   )re   rT   Úappendr&   Ústartr
   )
r    r!   r"   r#   r$   Únew_posr   rd   rR   Úlengthr   r   r   r+   b  s   


zSequence._uncached_matchc                 C   ó   d  d |  ¡ ¡¡S )Nú({0})Ú ©r‘   rŽ   rf   r-   r   r   r   r.   o  ó   zSequence._as_rhsN©r,   r0   r1   rj   r+   r.   r   r   r   r   r›   Z  s    r›   c                   @   rš   )ÚOneOfzŽA series of expressions, one of which must match

    Expressions are tested in order from first to last. The first to succeed
    wins.

    c                 C   sB   | j D ]}| ||||¡}|d urt| |||j|gd�  S qd S r   )re   rT   r
   r&   )r    r!   r"   r#   r$   rd   rR   r   r   r   r+   z  s   
þþzOneOf._uncached_matchc                 C   r    )Nr¡   z / r£   r-   r   r   r   r.   �  r¤   zOneOf._as_rhsNr¥   r   r   r   r   r¦   s  s    r¦   c                       sH   e Zd ZdZdgZddœ‡ fdd„
Zdd„ Zd	d
„ Z‡ fdd„Z‡  Z	S )Ú	LookaheadzSAn expression which consumes nothing, even if its contained expression
    succeedsÚ
negativityF©Únegativec                   s"   t ƒ j|fi |¤Ž t|ƒ| _d S r>   )rn   r?   Úboolr¨   )r    Úmemberrª   r”   ro   r   r   r?   ‹  s   zLookahead.__init__c                 C   s6   | j d  ||||¡}|d u | jkrt| |||ƒS d S ©Nr   )re   rT   r¨   r
   )r    r!   r"   r#   r$   rR   r   r   r   r+   �  s   ÿzLookahead._uncached_matchc                 C   s   d| j rdnd|  ¡ d f S )Nú%s%sú!ú&r   )r¨   rf   r-   r   r   r   r.   ”  s   zLookahead._as_rhsc                    s   t ƒ  ||¡o| j|jkS r>   )rn   rC   r¨   rL   ro   r   r   rC   —  s   
þzLookahead._eq_check_cycles)
r,   r0   r1   rj   rk   r?   r+   r.   rC   rq   r   r   ro   r   r§   …  s    r§   c                 C   s   t | dd�S )NTr©   )r§   )Útermr   r   r   ÚNot�  s   r²   c                       sR   e Zd ZdZddgZdedƒddœ‡ fdd	„
Zd
d„ Zdd„ Z‡ fdd„Z	‡  Z
S )Ú
QuantifierzAAn expression wrapper like the */+/?/{n,m} quantifier in regexes.ÚminÚmaxr   Úinfr=   )r´   rµ   r3   c                   s(   t ƒ j|fd|i|¤Ž || _|| _d S )Nr3   )rn   r?   r´   rµ   )r    r¬   r´   rµ   r3   r”   ro   r   r   r?   §  s   
zQuantifier.__init__c           
      C   s´   |}g }t |ƒ}||k rIt |ƒ| jk rI| jd  ||||¡}|d u r#n&| |¡ |j|j }	t |ƒ| jkr:|	dkr:n||	7 }||k rIt |ƒ| jk st |ƒ| jkrXt| ||||ƒS d S r­   )	r5   rµ   re   rT   rœ   r&   r�   r´   r
   )
r    r!   r"   r#   r$   rž   r   ÚsizerR   rŸ   r   r   r   r+   ¬  s    
ø	ÿzQuantifier._uncached_matchc                 C   sª   | j dkr| jdkrd}n>| j dkr| jtdƒkrd}n/| j dkr+| jtdƒkr+d}n | jtdƒkr8d| j  }n| j dkrCd| j }nd	| j | jf }d
|  ¡ d |f S )Nr   r   Ú?r¶   Ú*ú+z{%d,}z{,%d}z{%d,%d}r®   )r´   rµ   Úfloatrf   )r    Ú	qualifierr   r   r   r.   ¼  s   
zQuantifier._as_rhsc                    s&   t ƒ  ||¡o| j|jko| j|jkS r>   )rn   rC   r´   rµ   rL   ro   r   r   rC   Ë  s
   
ÿ
ýzQuantifier._eq_check_cycles)r,   r0   r1   rj   rk   r»   r?   r+   r.   rC   rq   r   r   ro   r   r³   ¢  s    r³   r=   c                 C   s   t | |dtdƒd�S )Nr   r¶   ©r3   r´   rµ   ©r³   r»   ©r¬   r3   r   r   r   Ú
ZeroOrMoreÒ  r¤   rÀ   r   c                 C   s   t | ||tdƒd�S )Nr¶   r½   r¾   )r¬   r3   r´   r   r   r   Ú	OneOrMoreÕ  r¤   rÁ   c                 C   s   t | |ddd�S )Nr   r   r½   )r³   r¿   r   r   r   ÚOptionalØ  s   rÂ   ri   )r=   r   )$rj   Úcollectionsr   Úinspectr   r   r   r   Úregexrv   Úparsimonious.exceptionsr   r   r	   Úparsimonious.nodesr
   r   Úparsimonious.utilsr   r   r;   ÚobjectrX   r8   rl   rr   ru   r’   r›   r¦   r§   r²   r³   rÀ   rÁ   rÂ   r   r   r   r   Ú<module>   s0    L (
0
