
    'iA                     b   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 Zd	 Z e       Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Z G d de      Z d dZ!d!dZ"d dZ#y)"zESubexpressions that make up a parsed grammar

These do the parsing.

    )defaultdict)getfullargspec
isfunctionismethodismethoddescriptorN)
ParseErrorIncompleteParseErrorLeftRecursionError)Node	RegexNode)
StrAndReprc                 l    t         t        t        g}t        |D cg c]
  } ||        c}      S c c}w N)r   r   r   any)valuecriteria	criterions      I/var/www/br/venv/lib/python3.12/site-packages/parsimonious/expressions.pyis_callabler      s-    H&89H(;Y	% ;<<;s   1c                     t               rt         d      r j                   t        t	               j
                        }t               r|dz  }|dk(  rdn|dk(  rdnt        d|z         G  fdd	t              } ||
      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                   &    e Zd Z fdZ fdZy)#expression.<locals>.AdHocExpressionc                     
r	 ||      n ||||	      }t        |t              r|d }}nt        |t              r|\  }}n|S t        | ||||      S N)children)
isinstanceinttupler   )selftextposcacheerrorresultendr   callablegrammar	is_simples           r   _uncached_matchz3expression.<locals>.AdHocExpression._uncached_matchP   sf    -6htS)tS%@  &#& &XFE* &X dCx@@    c                 "    dj                   z  S )Nz{custom function "%s"})__name__)r#   r*   s    r   _as_rhsz+expression.<locals>.AdHocExpression._as_rhs]   s    +h.?.???r.   N)r0   
__module____qualname__r-   r1   )r*   r+   r,   s   r   AdHocExpressionr   O   s    	A	@r.   r4   )name)	r   hasattrr   lenr   argsr   RuntimeError
Expression)r*   	rule_namer+   num_argsr4   r,   s   ` `  @r   
expressionr=      s    R (#*(E$$>(+001HA1}		Q	 02:; < 	<@ @* @" 	**r.   c                   l    e Zd ZdZddgZddZd Zd Zd Zd Z	d	 Z
dd
ZddZd Zd Zd Zd Zd Zy)r:   z3A thing that can be matched against a piece of textr5   identity_tuplec                 6    || _         | j                   f| _        y r   )r5   r?   )r#   r5   s     r   __init__zExpression.__init__o   s    	#yymr.   c                 ,    t        | j                        S r   )hashr?   r#   s    r   __hash__zExpression.__hash__s   s    D''((r.   c                 6    | j                  |t                     S r   )_eq_check_cyclessetr#   others     r   __eq__zExpression.__eq__v   s    $$UCE22r.   c                     | |k(   S r    rI   s     r   __ne__zExpression.__ne__y   s    EM""r.   c                     |j                  t        |       t        |      f       |j                  | j                  u xr | j                  |j                  k(  S r   )addid	__class__r?   )r#   rJ   checkeds      r   rG   zExpression._eq_check_cycles|   sD    RXr%y)*$..0`T5H5HEL`L`5``r.   c                     | S r   rM   r#   rule_maps     r   resolve_refszExpression.resolve_refs   s    r.   c                     | j                  ||      }|j                  t        |      k  rt        ||j                  |       |S )zReturn 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)   r7   r	   )r#   r$   r%   nodes       r   parsezExpression.parse   s>     zz$Cz(88c$i&tTXXt<<r.   c                 f    t        |      }| j                  ||t        t              |      }|||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

        )r   
match_corer   dict)r#   r$   r%   r'   rZ   s        r   rY   zExpression.match   s6     4 tS+d*;UC<Kr.   c                 *   |t        |          }||v r||   }n"t        ||<   | j                  ||||      x}||<   |t        u rt        |d|       |@||j                  k\  r1| j
                  st        |j                  dd      | |_        ||_        |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%   exprNr5   )rQ   IN_PROGRESSr-   r
   r%   r5   getattrra   )r#   r$   r%   r&   r'   
expr_cacherZ   s          r   r]   zExpression.match_core   s    > 2d8_
*c?D *JsO%)%9%9$UE%RRD:c?;$Tr== <C599,		WUZZ>F
 EJEIr.   c                 X    d| j                   j                  d| j                         dS )N< >)rR   r0   as_rulerD   s    r   __str__zExpression.__str__   s"    NN##LLN 	r.   c                     | j                         j                         }|j                  d      r|j                  d      r|dd }| j                  r| j                  d|S |S )zReturn 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   r`   z = )r1   strip
startswithendswithr5   )r#   rhss     r   ri   zExpression.as_rule   sT     lln""$>>#3<<#4a)C15TYY,CCr.   c                 x    | j                   D cg c]   }|j                  xs |j                         " c}S c c}w )zReturn an iterable of my unicode-represented children, stopping
        descent when we hit a named node so the returned value resembles the
        input rule.)membersr5   r1   )r#   ms     r   _unicode_memberszExpression._unicode_members   s.     26>A&199;&>>>s   %7c                     t         )zfReturn the right-hand side of a rule that represents me.

        Implemented by subclasses.

        )NotImplementedErrorrD   s    r   r1   zExpression._as_rhs   s
     "!r.   N )r   )r0   r2   r3   __doc__	__slots__rA   rE   rK   rN   rG   rW   r[   rY   r]   rj   ri   ru   r1   rM   r.   r   r:   r:   f   sV    = )*I,)3#a
3j

D?"r.   r:   c                   6     e Zd ZdZdgZd fd	Zd Zd Z xZS )LiteralzFA string literal

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

    literalc                 D    t         |   |       || _        ||f| _        y r   )superrA   r~   r?   )r#   r~   r5   rR   s      r   rA   zLiteral.__init__   s#    #Wor.   c           	          |j                  | j                  |      r$t        | |||t        | j                        z         S y r   )ro   r~   r   r7   )r#   r$   r%   r&   r'   s        r   r-   zLiteral._uncached_match  s8    ??4<<-dCs4<</@)@AA .r.   c                 ,    t        | j                        S r   )reprr~   rD   s    r   r1   zLiteral._as_rhs  s    DLL!!r.   rx   )	r0   r2   r3   rz   r{   rA   r-   r1   __classcell__rR   s   @r   r}   r}      s!    
 I.
B"r.   r}   c                       e Zd ZdZd Zy)TokenMatcherziAn expression matching a single token of a given type

    This is for use only with TokenGrammars.

    c                 ^    ||   j                   | j                  k(  rt        | |||dz         S y )Nr   )typer~   r   )r#   
token_listr%   r&   r'   s        r   r-   zTokenMatcher._uncached_match  s2    c?4<</j#sQw77 0r.   N)r0   r2   r3   rz   r-   rM   r.   r   r   r     s    
8r.   r   c                   @     e Zd ZdZdgZ	 	 d fd	Zd Zd Zd Z xZ	S )RegexzAn 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.

    rec
                    t         
|   |       t        j                  ||xr t        j                  |xr t        j
                  z  |xr t        j                  z  |xr t        j                  z  |xr t        j                  z  |xr t        j                  z  |	xr t        j                  z        | _        | j                  | j                  f| _        y r   )r   rA   r   compileILMSUXAr5   r?   )r#   patternr5   ignore_caselocale	multilinedot_allunicodeverboseasciirR   s             r   rA   zRegex.__init__  s    **W{';rtt'-"$$'8'0'9RTT'; (/'7244'9 (/'7244	'9
 (/'7244'9 (-~'7 8  $yy$''2r.   c                     | j                   j                  ||      }|3|j                         }t        | ||||d   z   |d   z
        }||_        |S y)z-Return length of match, ``None`` if no match.Nr   r   )r   rY   spanr   )r#   r$   r%   r&   r'   rt   r   rZ   s           r   r-   zRegex._uncached_match+  sX    GGMM$$=668DT4cDGmd1g.EFDDJK	 r.   c           	      l    ddj                  fdt        dt              dz         D              S )zAReturn the textual equivalent of numerically encoded regex flags.ilmsuxary   c              3   B   K   | ]  }d |z  z  r|d z
     nd  yw)r   ry   NrM   ).0ibitsflagss     r   	<genexpr>z/Regex._regex_flags_from_bits.<locals>.<genexpr>7  s(     a1Q$uQU|B>as   r   )joinranger7   )r#   r   r   s    `@r   _regex_flags_from_bitszRegex._regex_flags_from_bits4  s1    wwaaQTUZQ[^_Q_H`aaar.   c                     dj                  | j                  j                  | j                  | j                  j                              S )Nz~{!r}{})formatr   r   r   r   rD   s    r   r1   zRegex._as_rhs9  s7     $ ; ;DGGMM JL 	Lr.   )ry   FFFFFFF)
r0   r2   r3   rz   r{   rA   r-   r   r1   r   r   s   @r   r   r     s-     ICHUZ
3b
Lr.   r   c                   >     e Zd ZdZdgZ fdZd Z fdZd Z xZ	S )Compoundz7An abstract expression which contains other expressionsrs   c                 R    t         |   |j                  dd             || _        y)z)``members`` is a sequence of expressions.r5   ry   N)r   rA   getrs   )r#   rs   kwargsrR   s      r   rA   zCompound.__init__C  s"    FB/0r.   c                 N    t        fd| j                  D              | _        | S )Nc              3   @   K   | ]  }|j                          y wr   )rW   )r   rt   rV   s     r   r   z(Compound.resolve_refs.<locals>.<genexpr>I  s     L!Q^^H5Ls   )r"   rs   rU   s    `r   rW   zCompound.resolve_refsH  s    Lt||LLr.   c                     t         |   |      xr_ t        | j                        t        |j                        k(  xr2 t	        fdt        | j                  |j                        D              S )Nc              3   z   K   | ]2  \  }}t        |      t        |      fvs|j                  |       4 y wr   )rQ   rG   )r   rt   morS   s      r   r   z,Compound._eq_check_cycles.<locals>.<genexpr>P  sC       AEArbdefbgiklnioapxa""2w/  As    ;;)r   rG   r7   rs   allzipr#   rJ   rS   rR   s     `r   rG   zCompound._eq_check_cyclesL  sc    G$UG4 AU]]!33A  ASu}}=]  A  A	
r.   c                 D    t        | j                  | j                  f      S r   )rC   rR   r5   rD   s    r   rE   zCompound.__hash__S  s     T^^TYY/00r.   )
r0   r2   r3   rz   r{   rA   rW   rG   rE   r   r   s   @r   r   r   >  s!    AI

1r.   r   c                       e Zd ZdZd Zd Zy)SequencezA 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                     |}g }| j                   D ]I  }|j                  ||||      }| y |j                  |       |j                  |j                  z
  }	||	z  }K t        | ||||      S r   )rs   r]   appendr)   startr   )
r#   r$   r%   r&   r'   new_posr   rt   rZ   lengths
             r   r-   zSequence._uncached_matchb  sx     	A<<gue<D|OOD!XX

*FvG	 D$Wh77r.   c                 ^    dj                  dj                  | j                                     S )N({0})rg   r   r   ru   rD   s    r   r1   zSequence._as_rhso  s#    ~~chht'<'<'>?@@r.   Nr0   r2   r3   rz   r-   r1   rM   r.   r   r   r   Z  s    8Ar.   r   c                       e Zd ZdZd Zd Zy)OneOfzA series of expressions, one of which must match

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

    c                     | j                   D ]5  }|j                  ||||      }|t        | |||j                  |g      c S  y r   )rs   r]   r   r)   )r#   r$   r%   r&   r'   rt   rZ   s          r   r-   zOneOf._uncached_matchz  sJ     	HA<<c5%8DD$TXXGG		Hr.   c                 ^    dj                  dj                  | j                                     S )Nr   z / r   rD   s    r   r1   zOneOf._as_rhs  s#    ~~ejj)>)>)@ABBr.   Nr   rM   r.   r   r   r   s  s    HCr.   r   c                   D     e Zd ZdZdgZdd fd
Zd Zd Z fdZ xZ	S )		LookaheadzSAn expression which consumes nothing, even if its contained expression
    succeeds
negativityFnegativec                F    t        |   |fi | t        |      | _        y r   )r   rA   boolr   )r#   memberr   r   rR   s       r   rA   zLookahead.__init__  s     *6*x.r.   c                     | j                   d   j                  ||||      }|d u | j                  k(  rt        | |||      S y Nr   )rs   r]   r   r   )r#   r$   r%   r&   r'   rZ   s         r   r-   zLookahead._uncached_match  sF    ||A))$UEBDLT__,dC-- -r.   c                 L    | j                   rdnd| j                         d   S )N!&r   )r   ru   rD   s    r   r1   zLookahead._as_rhs  s'     $S8$:O:O:QRS:TUUr.   c                 Z    t         |   ||      xr | j                  |j                  k(  S r   )r   rG   r   r   s      r   rG   zLookahead._eq_check_cycles  s.    G$UG4 0OOu///	
r.   )
r0   r2   r3   rz   r{   rA   r-   r1   rG   r   r   s   @r   r   r     s.     I+0 ).
V
 
r.   r   c                     t        | d      S )NTr   )r   )terms    r   Notr     s    TD))r.   c                   V     e Zd ZdZddgZd ed      dd fd
Zd	 Zd
 Z fdZ	 xZ
S )
QuantifierzAAn expression wrapper like the */+/?/{n,m} quantifier in regexes.minmaxr   infry   )r   r   r5   c                F    t        |   |fd|i| || _        || _        y )Nr5   )r   rA   r   r   )r#   r   r   r   r5   r   rR   s         r   rA   zQuantifier.__init__  s(    5d5f5r.   c                    |}g }t        |      }||k  rt        |      | j                  k  r| j                  d   j                  ||||      }|nk|j	                  |       |j
                  |j                  z
  }	t        |      | j                  k\  r|	dk(  rn#||	z  }||k  rt        |      | j                  k  rt        |      | j                  k\  rt        | ||||      S y r   )	r7   r   rs   r]   r   r)   r   r   r   )
r#   r$   r%   r&   r'   r   r   sizerZ   r   s
             r   r-   zQuantifier._uncached_match  s    4ynX!9<<?--dGUEJD|OOD!XX

*F8}(Vq[vG nX!9 x=DHH$dC(;; %r.   c                    | 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}nb| j                  t        d      k(  rd| j                   z  }n:| j                   dk(  rd| j                  z  }nd	| j                   | j                  fz  }| j                         d   |S )
Nr   r   ?r   *+z{%d,}z{,%d}z{%d,%d})r   r   floatru   )r#   	qualifiers     r   r1   zQuantifier._as_rhs  s    88q=TXX]IXX]txx5<7IXX]txx5<7IXXu%$((*IXX]$((*I!TXXtxx$88I..03Y??r.   c                     t         |   ||      xr4 | j                  |j                  k(  xr | j                  |j                  k(  S r   )r   rG   r   r   r   s      r   rG   zQuantifier._eq_check_cycles  sA    G$UG4 "HH		!"HH		!	
r.   )r0   r2   r3   rz   r{   r   rA   r-   r1   rG   r   r   s   @r   r   r     s4    KI&'U5\ 
< @
 
r.   r   c                 2    t        | |dt        d            S )Nr   r   r5   r   r   r   r   r   r5   s     r   
ZeroOrMorer     s    f4QE%LAAr.   c                 2    t        | ||t        d            S )Nr   r   r   )r   r5   r   s      r   	OneOrMorer     s    f4SeElCCr.   c                      t        | |dd      S )Nr   r   r   )r   r   s     r   Optionalr     s    f4QA66r.   rx   )ry   r   )$rz   collectionsr   inspectr   r   r   r   regexr   parsimonious.exceptionsr   r	   r
   parsimonious.nodesr   r   parsimonious.utilsr   r   r=   objectrb   r:   r}   r   r   r   r   r   r   r   r   r   r   r   rM   r.   r   <module>r      s    $ L L  X X . )=
I+X hM" M"`"j "*87 8%LJ %LP1z 18Ax A2CH C$
 
0*
.
 .
`BD7r.   