o
    h'                     @  s  d Z ddlmZ ddlZddlmZ ddlmZ ddlmZ ddlZddl	Z	ddl
Z
ddlZddlZddlmZ ddlmZ dd	lmZ dd
lmZ ddgZG dd de	jZejddG dd dZG dd deZG dd dZdZd2ddZd3ddZd3dd Zd3d!d"Zdd#dd$Zd4d&d'Z d5d)d*Z!G d+d, d,eZ"ejG d-d. d.Z#G d/d0 d0ee$e#f Z%G d1d dZ&dS )6a  Evaluate match expressions, as used by `-k` and `-m`.

The grammar is:

expression: expr? EOF
expr:       and_expr ('or' and_expr)*
and_expr:   not_expr ('and' not_expr)*
not_expr:   'not' not_expr | '(' expr ')' | ident kwargs?

ident:      (\w|:|\+|-|\.|\[|\]|\\|/)+
kwargs:     ('(' name '=' value ( ', ' name '=' value )*  ')')
name:       a valid ident, but not a reserved keyword
value:      (unescaped) string literal | (-)?[0-9]+ | 'False' | 'True' | 'None'

The semantics are:

- Empty expression evaluates to False.
- ident evaluates to True or False according to a provided matcher function.
- or/and/not evaluate according to the usual boolean semantics.
- ident with parentheses and keyword arguments evaluates to True or False according to a provided matcher function.
    )annotationsN)Iterator)Mapping)Sequence)Literal)NoReturn)overload)Protocol
Expression
ParseErrorc                   @  s4   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdS )	TokenTypezleft parenthesiszright parenthesisorandnot
identifierzend of input=zstring literal,N)__name__
__module____qualname__LPARENRPARENORANDNOTIDENTEOFEQUALSTRINGCOMMA r    r    X/var/www/html/openai_agents/venv/lib/python3.10/site-packages/_pytest/mark/expression.pyr   .   s    r   T)frozenc                   @  s*   e Zd ZU dZded< ded< ded< dS )	Token)postypevaluer   r%   strr&   intr$   N)r   r   r   	__slots____annotations__r    r    r    r!   r#   ;   s
   
 r#   c                   @  s$   e Zd ZdZddd	Zdd
dZdS )r   zThe expression contains invalid syntax.

    :param column: The column in the line where the error occurred (1-based).
    :param message: A description of the error.
    columnr(   messager'   returnNonec                 C  s   || _ || _d S Nr+   r,   )selfr+   r,   r    r    r!   __init__J   s   
zParseError.__init__c                 C  s   d| j  d| j S )Nz
at column z: r0   r1   r    r    r!   __str__N      zParseError.__str__N)r+   r(   r,   r'   r-   r.   )r-   r'   )r   r   r   __doc__r2   r4   r    r    r    r!   r   C   s    
c                   @  s`   e Zd ZdZdddZd d	d
Zed!ddZeddd"ddZddd#ddZd$ddZdS )%Scanner)currenttokensinputr'   r-   r.   c                 C  s   |  || _t| j| _d S r/   )lexr9   nextr8   )r1   r:   r    r    r!   r2   U   s   zScanner.__init__Iterator[Token]c                 c  s   d}|t |k r|| dv r|d7 }n|| dkr(ttjd|V  |d7 }n|| dkr;ttjd|V  |d7 }n|| dkrNttjd|V  |d7 }n|| dkrattjd|V  |d7 }n||  }dv r|||d }|d	krt|d d
| d|||d  }|d }d	krt|d dttj	||V  |t |7 }nTt
d||d  }|r|d}|dkrttj||V  n"|dkrttj||V  n|dkrttj||V  nttj||V  |t |7 }nt|d d||  d|t |k s
ttjd|V  d S )Nr   ) 	   ()r   r   )'"zclosing quote "z" is missing\z4escaping with "\" not supported in marker expressionz(:?\w|:|\+|-|\.|\[|\]|\\|/)+r   r   r   zunexpected character "rD    )lenr#   r   r   r   r   r   findr   r   rematchgroupr   r   r   r   r   )r1   r:   r$   
quote_charend_quote_posr&   backslash_posrK   r    r    r!   r;   Y   sb   






0zScanner.lexr%   r   rejectLiteral[True]r#   c                C     d S r/   r    r1   r%   rP   r    r    r!   accept   s   zScanner.acceptFrP   Literal[False]Token | Nonec                C  rR   r/   r    rS   r    r    r!   rT      s   boolc                C  sB   | j j|u r| j }|jtjurt| j| _ |S |r| |f d S r/   )r8   r%   r   r   r<   r9   rP   )r1   r%   rP   tokenr    r    r!   rT      s   expectedSequence[TokenType]r   c                 C  s0   t | jjd dddd |D | jjj)Nr@   zexpected {}; got {}z OR c                 s  s    | ]}|j V  qd S r/   )r&   ).0r%   r    r    r!   	<genexpr>   s    z!Scanner.reject.<locals>.<genexpr>)r   r8   r$   formatjoinr%   r&   )r1   rZ   r    r    r!   rP      s   
zScanner.rejectN)r:   r'   r-   r.   )r:   r'   r-   r=   )r%   r   rP   rQ   r-   r#   )r%   r   rP   rV   r-   rW   )r%   r   rP   rX   r-   rW   )rZ   r[   r-   r   )	r   r   r   r)   r2   r;   r   rT   rP   r    r    r    r!   r7   R   s    

4
r7   $sr-   ast.Expressionc                 C  s@   |  tjrtd}nt| }| j tjdd tt|S )NFTrU   )rT   r   r   astConstantexprfix_missing_locationsr
   ra   retr    r    r!   
expression   s
   ri   ast.exprc                 C  @   t | }| tjrt | }tt ||g}| tjs
|S r/   )and_exprrT   r   r   rc   BoolOpOrra   rh   rhsr    r    r!   re         re   c                 C  rk   r/   )not_exprrT   r   r   rc   rm   Andro   r    r    r!   rl      rq   rl   c                 C  s   |  tjrtt t| S |  tjr$t| }| j tj	dd |S |  tj
}|rUtt|j t }|  tjrQtj|g t| d}| j tj	dd |S |}|S | tjtjtj
f d S )NTrU   )funcargskeywords)rT   r   r   rc   UnaryOpNotrr   r   re   r   r   NameIDENT_PREFIXr&   LoadCall
all_kwargsrP   )ra   rh   identnamer    r    r!   rr      s    rr   F)TrueFalser.   ast.keywordc                 C  s  | j tjdd}|j st|jd d|j t|jr,t|jd d|j d| j tj	dd |  tj
 }rD|jdd }n9| j tjdd}|j } s`|dre|dd   ret|}n|jtv rpt|j }nt|jd d	|j d
t|jt|}|S )NTrU   r@   znot a valid python identifier z$unexpected reserved python keyword ``rE   -zunexpected character/s "rD   )rT   r   r   r&   isidentifierr   r$   keyword	iskeywordr   r   isdigit
startswithr(   BUILTIN_MATCHERSrc   rd   )ra   keyword_namevalue_tokenr&   numberrh   r    r    r!   single_kwarg   s:   



r   list[ast.keyword]c                 C  s4   t | g}| tjr|t |  | tjs|S r/   )r   rT   r   r   appendrg   r    r    r!   r}     s
   
r}   c                   @  s   e Zd Zd
ddZd	S )MatcherCallr   r'   kwargsstr | int | bool | Noner-   rX   c                K  rR   r/   r    )r1   r   r   r    r    r!   __call__	  s    zMatcherCall.__call__N)r   r'   r   r   r-   rX   )r   r   r   r   r    r    r    r!   r     s    r   c                   @  s2   e Zd ZU ded< ded< dddZdddZdS )MatcherNameAdapterr   matcherr'   r   r-   rX   c                 C  s   |  | jS r/   r   r   r3   r    r    r!   __bool__  s   zMatcherNameAdapter.__bool__r   r   c                 K  s   | j | jfi |S r/   r   )r1   r   r    r    r!   r     r5   zMatcherNameAdapter.__call__N)r-   rX   )r   r   r-   rX   )r   r   r   r*   r   r   r    r    r    r!   r     s
   
 
r   c                   @  s8   e Zd ZdZdddZdddZdddZdddZdS )MatcherAdapterzDAdapts a matcher function to a locals mapping as required by eval().r   r   r-   r.   c                 C  
   || _ d S r/   )r   )r1   r   r    r    r!   r2        
zMatcherAdapter.__init__keyr'   r   c                 C  s   t | j|ttd  dS )Nr   )r   r   rH   rz   )r1   r   r    r    r!   __getitem__  s   zMatcherAdapter.__getitem__Iterator[str]c                 C     t  r/   NotImplementedErrorr3   r    r    r!   __iter__!     zMatcherAdapter.__iter__r(   c                 C  r   r/   r   r3   r    r    r!   __len__$  r   zMatcherAdapter.__len__N)r   r   r-   r.   )r   r'   r-   r   )r-   r   )r-   r(   )r   r   r   r6   r2   r   r   r   r    r    r    r!   r     s    


r   c                   @  s6   e Zd ZdZdZdddZedddZdddZdS )r
   zwA compiled match expression as used by -k and -m.

    The expression can be evaluated against different matchers.
    coder   types.CodeTyper-   r.   c                 C  r   r/   r   )r1   r   r    r    r!   r2   0  r   zExpression.__init__r:   r'   c                 C  s"   t t|}t|ddd}t|S )z\Compile a match expression.

        :param input: The input expression - one line.
        z<pytest match expression>eval)filenamemode)ri   r7   compiler
   )clsr:   astexprr   r    r    r!   r   3  s   zExpression.compiler   r   rX   c                 C  s   t t| jdi it|}|S )a	  Evaluate the match expression.

        :param matcher:
            Given an identifier, should return whether it matches or not.
            Should be prepared to handle arbitrary strings as input.

        :returns: Whether the expression matches or not.
        __builtins__)rX   r   r   r   )r1   r   rh   r    r    r!   evaluateA  s   	zExpression.evaluateN)r   r   r-   r.   )r:   r'   r-   r
   )r   r   r-   rX   )	r   r   r   r6   r)   r2   classmethodr   r   r    r    r    r!   r
   (  s    
)ra   r7   r-   rb   )ra   r7   r-   rj   )ra   r7   r-   r   )ra   r7   r-   r   )'r6   
__future__r   rc   collections.abcr   r   r   dataclassesenumr   rJ   typestypingr   r   r   r	   __all__Enumr   	dataclassr#   	Exceptionr   r7   rz   ri   re   rl   rr   r   r   r}   r   r   r'   r   r
   r    r    r    r!   <module>   sH    
Z

	



"