B
    `                 @   sN  d 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lm
Z
 ddlmZ ddlZerdd	lmZ d
dgZG dd dejZejdddG dd dZG dd deZG dd dZdZeejdddZeejdddZeejdddZeejdddZG dd  d eeef ZG d!d
 d
ZdS )"a  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
ident:      (\w|:|\+|-|\.|\[|\])+

The semantics are:

- Empty expression evaluates to False.
- ident evaluates to True of False according to a provided matcher function.
- or/and/not evaluate according to the usual boolean semantics.
    N)Callable)Iterator)Mapping)Optional)Sequence)TYPE_CHECKING)NoReturn
Expression
ParseErrorc               @   s(   e 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 inputN)
__name__
__module____qualname__LPARENRPARENORANDNOTIDENTEOF r   r   _/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/_pytest/mark/expression.pyr   (   s   r   T)frozenslotsc               @   s0   e Zd ZejedZejedZeje	dZ
dS )Token)typeN)r   r   r   attribr   r   strvalueintposr   r   r   r   r   2   s   r   c               @   s0   e Zd ZdZeeddddZe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.
    N)columnmessagereturnc             C   s   || _ || _d S )N)r&   r'   )selfr&   r'   r   r   r   __init__@   s    zParseError.__init__)r(   c             C   s   d| j  d| j S )Nz
at column z: )r&   r'   )r)   r   r   r   __str__D   s    zParseError.__str__)r   r   r   __doc__r$   r"   r*   r+   r   r   r   r   r
   9   s   c               @   sd   e Zd ZdZeddddZeee dddZdd	e	e
ee d
ddZee	 ddddZdS )Scanner)tokenscurrentN)inputr(   c             C   s   |  || _t| j| _d S )N)lexr.   nextr/   )r)   r0   r   r   r   r*   K   s    zScanner.__init__c             c   s8  d}x|t |k r"|| dkr,|d7 }q|| dkrRttjd|V  |d7 }q|| dkrxttjd|V  |d7 }qtd||d  }|r|d}|dkrttj||V  nD|dkrttj	||V  n*|d	krttj
||V  nttj||V  |t |7 }qt|d d
|| qW ttjd|V  d S )Nr   ) 	   ()z(:?\w|:|\+|-|\.|\[|\])+r   r   r   zunexpected character "{}" )lenr   r   r   r   rematchgroupr   r   r   r   r
   formatr   )r)   r0   r%   r;   r#   r   r   r   r1   O   s0    



zScanner.lexF)reject)r   r>   r(   c            C   sB   | j j|kr.| j }|jtjk	r*t| j| _ |S |r>| |f d S )N)r/   r   r   r   r2   r.   r>   )r)   r   r>   tokenr   r   r   acceptm   s    zScanner.acceptr   )expectedr(   c             C   s4   t | jjd dddd |D | jjjd S )Nr5   zexpected {}; got {}z OR c             s   s   | ]}|j V  qd S )N)r#   ).0r   r   r   r   	<genexpr>{   s    z!Scanner.reject.<locals>.<genexpr>)r
   r/   r%   r=   joinr   r#   )r)   rA   r   r   r   r>   w   s    
zScanner.reject)r   r   r   	__slots__r"   r*   r   r   r1   r   boolr   r@   r   r>   r   r   r   r   r-   H   s
   
r-   $)sr(   c             C   s@   |  tjrtd}nt| }| j tjdd tt|S )NFT)r>   )r@   r   r   astNameConstantexprZfix_missing_locationsr	   )rH   retr   r   r   
expression   s
    rM   c             C   s:   t | }x,| tjr4t | }tt ||g}q
W |S )N)and_exprr@   r   r   rI   BoolOpOr)rH   rL   rhsr   r   r   rK      s
    rK   c             C   s:   t | }x,| tjr4t | }tt ||g}q
W |S )N)not_exprr@   r   r   rI   rO   And)rH   rL   rQ   r   r   r   rN      s
    rN   c             C   s   |  tjr tt t| S |  tjrHt| }| j tj	dd |S |  tj
}|rntt|j t S | tjtjtj
f d S )NT)r>   )r@   r   r   rI   ZUnaryOpZNotrR   r   rK   r   r   NameIDENT_PREFIXr#   Loadr>   )rH   rL   identr   r   r   rR      s    rR   c               @   sZ   e Zd ZdZeegef ddddZeedddZe	e d	d
dZ
ed	ddZdS )MatcherAdapterzDAdapts a matcher function to a locals mapping as required by eval().N)matcherr(   c             C   s
   || _ d S )N)rY   )r)   rY   r   r   r   r*      s    zMatcherAdapter.__init__)keyr(   c             C   s   |  |ttd  S )N)rY   r9   rU   )r)   rZ   r   r   r   __getitem__   s    zMatcherAdapter.__getitem__)r(   c             C   s
   t  d S )N)NotImplementedError)r)   r   r   r   __iter__   s    zMatcherAdapter.__iter__c             C   s
   t  d S )N)r\   )r)   r   r   r   __len__   s    zMatcherAdapter.__len__)r   r   r   r,   r   r"   rF   r*   r[   r   r]   r$   r^   r   r   r   r   rX      s
   rX   c               @   sT   e Zd ZdZdZejddddZee	d ddd	Z
ee	gef ed
ddZdS )r	   zwA compiled match expression as used by -k and -m.

    The expression can be evaulated against different matchers.
    )codeN)r_   r(   c             C   s
   || _ d S )N)r_   )r)   r_   r   r   r   r*      s    zExpression.__init__)r0   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)rM   r-   compiler	   )r)   r0   Zastexprr_   r   r   r   rc      s    zExpression.compile)rY   r(   c             C   s   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__)r`   r_   rX   )r)   rY   rL   r   r   r   evaluate   s    	zExpression.evaluate)r   r   r   r,   rE   typesCodeTyper*   classmethodr"   rc   r   rF   re   r   r   r   r   r	      s   )r,   rI   enumr:   rf   typingr   r   r   r   r   r   r    r   __all__Enumr   rH   r   	Exceptionr
   r-   rU   r	   rM   rK   rN   rR   r"   rF   rX   r   r   r   r   <module>   s4   
;	