o
    e<                     @   s  d Z ddlmZ ddlZddlmZ dZdZdZd	Z	e
d
Zdd Zdd Zdd Zdd Zdd ZG dd deZdd ZG dd deZG dd de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e Zd'e_ d(e_d)d* Zd+d, Zd-d. Z d/d0 Z!e!d1Z"d2e"_ d3e"_dDd4d5Z#d6d7 Z$d8d9 Z%d:d; Z&d<d= Z'eeZ(d>e(_ d?e(_eeZ)d@e)_ dAe)_ee	Z*dBe*_ dCe*_dS )Ez.
Python Lexical Analyser

Regular Expressions
    )absolute_importN   )ErrorsiZboleoleof
c                 C   s   t | }|  d}t|}g }||k rPt|| }|d }|d7 }||k rB|t|| krB|d7 }|d7 }||k rB|t|| ks.|| || ||k s|S )z
    Return a list of character codes consisting of pairs
    [code1a, code1b, code2a, code2b,...] which cover all
    the characters in |s|.
    r   r   )listsortlenordappend)sZ	char_listinresultcode1code2 r   CD:\Projects\ConvertPro\env\Lib\site-packages\Cython/Plex/Regexps.pychars_to_ranges   s"   

	r   c                 C   L   t | td}t|tdd }||k r$tdtd }|| || fS dS )z
    If the range of characters from code1 to code2-1 includes any
    lower case letters, return the corresponding upper case range.
    azr   ANmaxr   minr   r   Zcode3Zcode4dr   r   r   uppercase_range4      r   c                 C   r   )z
    If the range of characters from code1 to code2-1 includes any
    upper case letters, return the corresponding lower case range.
    r   Zr   r   Nr   r   r   r   r   lowercase_rangeB   r    r"   c                    s&    fddt dt dD }t| S )z
    Given a list of codes as returned by chars_to_ranges, return
    an RE which will match a character in any of the ranges.
    c                    s"   g | ]}t  |  |d   qS )r   )	CodeRange).0r   	code_listr   r   
<listcomp>U   s   " zCodeRanges.<locals>.<listcomp>r      )ranger
   Alt)r&   re_listr   r%   r   
CodeRangesP   s   r,   c                 C   s>   | t   kr
|k rn ntt| t ttt d |S t| |S )z
    CodeRange(code1, code2) is an RE which matches any character
    with a code |c| in the range |code1| <= |c| < |code2|.
    r   )nl_coder*   RawCodeRange
RawNewliner   r   r   r   r   r#   Y   s   

r#   c                   @   sd   e Zd ZdZd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S )REa  RE is the base class for regular expression constructors.
    The following operators are defined on REs:

         re1 + re2         is an RE which matches |re1| followed by |re2|
         re1 | re2         is an RE which matches either |re1| or |re2|
    r   Nc                 C   s   t d| jj )aM  
        This method should add states to |machine| to implement this
        RE, starting at |initial_state| and ending at |final_state|.
        If |match_bol| is true, the RE must be able to match at the
        beginning of a line. If nocase is true, upper and lower case
        letters should be treated as equivalent.
        z %s.build_machine not implemented)NotImplementedError	__class____name__)selfmachineinitial_statefinal_state	match_bolnocaser   r   r   build_machinev   s   	zRE.build_machinec                 C   s"   |  }|| ||| |S )z~
        Given a state |s| of machine |m|, return a new state
        reachable from |s| on character |c| or epsilon.
        )	new_statelink_toadd_transition)r5   mr7   cr   r   r   r   	build_opt   s   
zRE.build_optc                 C   
   t | |S N)Seqr5   otherr   r   r   __add__      
z
RE.__add__c                 C   rB   rC   )r*   rE   r   r   r   __or__   rH   z	RE.__or__c                 C   s   | j r| j S |  S rC   )strcalc_strr5   r   r   r   __str__   s   z
RE.__str__c                 C   s    t |ts| ||d d S d S )NzPlex.RE instance)
isinstancer1   
wrong_typer5   numvaluer   r   r   check_re   s   
zRE.check_rec                 C   s&   t |t dkr| ||d d S d S )N string)typerO   rP   r   r   r   check_string   s   zRE.check_stringc                 C   s8   |  || t|dkrtd|| jjt|f d S )Nr   zOInvalid value for argument %d of Plex.%s.Expected a string of length 1, got: %s)rW   r
   r   ZPlexValueErrorr3   r4   reprrP   r   r   r   
check_char   s   zRE.check_charc                 C   sH   t |tjkrd|jj|jjf }nt |j}td|| jj||f )Nz%s.%s instancez<Invalid type for argument %d of Plex.%s (expected %s, got %s)rV   typesZInstanceTyper3   
__module__r4   r   ZPlexTypeError)r5   rQ   rR   expectedgotr   r   r   rO      s   
zRE.wrong_type)r4   r[   __qualname____doc__nullablematch_nlrJ   r;   rA   rG   rI   rM   rS   rW   rY   rO   r   r   r   r   r1   j   s    
r1   c                 C   s>   t | dkrtt| t| d }nt| }dt|  |_|S )z;
    Char(c) is an RE which matches the character |c|.
    r   zChar(%s))r
   r#   r   SpecialSymbolrX   rJ   )r@   r   r   r   r   Char   s
   rc   c                   @   s<   e Zd ZdZdZdZdZdZdZdd Z	dd Z
dd	 ZdS )
r.   z
    RawCodeRange(code1, code2) is a low-level RE which matches any character
    with a code |c| in the range |code1| <= |c| < |code2|, where the range
    does not include newline. For internal use only.
    r   Nc                 C   s&   ||f| _ t||| _t||| _d S rC   )r)   r   r"   )r5   r   r   r   r   r   __init__   s   
zRawCodeRange.__init__c                 C   sX   |r	|  ||t}|| j| |r(| jr|| j| | jr*|| j| d S d S d S rC   )rA   BOLr>   r)   r   r"   r5   r?   r7   r8   r9   r:   r   r   r   r;      s   zRawCodeRange.build_machinec                 C   s   d| j | jf S )NzCodeRange(%d,%d)r0   rL   r   r   r   rK      s   zRawCodeRange.calc_str)r4   r[   r^   r_   r`   ra   r)   r   r"   rd   r;   rK   r   r   r   r   r.      s    
r.   c                   @   s    e Zd ZdZdZdZdd ZdS )_RawNewlinezd
    RawNewline is a low-level RE which matches a newline character.
    For internal use only.
    r   r   c                 C   s8   |r	|  ||t}|  ||t}|ttd f| d S Nr   )rA   re   EOLr>   r-   )r5   r?   r7   r8   r9   r:   r   r   r   r   r;      s   z_RawNewline.build_machineN)r4   r[   r^   r_   r`   ra   r;   r   r   r   r   rg      s
    rg   c                   @   s,   e Zd ZdZdZdZdZdd Zdd ZdS )rb   zx
    SpecialSymbol(sym) is an RE which matches the special input
    symbol |sym|, which is one of BOL, EOL or EOF.
    r   Nc                 C   s
   || _ d S rC   )sym)r5   rj   r   r   r   rd      rH   zSpecialSymbol.__init__c                 C   s.   |r| j tkr| ||t}|| j | d S rC   )rj   ri   rA   re   r>   rf   r   r   r   r;     s   zSpecialSymbol.build_machine)	r4   r[   r^   r_   r`   ra   rj   rd   r;   r   r   r   r   rb      s    rb   c                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	rD   z]Seq(re1, re2, re3...) is an RE which matches |re1| followed by
    |re2| followed by |re3|...c                 G   sz   d}t |D ]\}}| || |o|j}q|| _|| _t|}d}|r8|d8 }|| }|jr2d}n|js6n|s$|| _d S )Nr   r   )	enumeraterS   r`   r+   r
   ra   )r5   r+   r`   r   rera   r   r   r   rd     s$   
zSeq.__init__c                 C   s   | j }t|dkr|| d S |}t|}t|D ]$\}	}
|	|d k r)| }n|}|
||||| |}|
jp=|o=|
j}qd S Nr   r   )r+   r
   r=   rl   r<   r;   ra   r`   )r5   r?   r7   r8   r9   r:   r+   s1r   r   rm   s2r   r   r   r;   !  s   
zSeq.build_machinec                 C      dd tt| j S )NzSeq(%s),joinmaprJ   r+   rL   r   r   r   rK   1     zSeq.calc_strNr4   r[   r^   r_   rd   r;   rK   r   r   r   r   rD   
  s
    rD   c                   @   rk   )	r*   zRAlt(re1, re2, re3...) is an RE which matches either |re1| or
    |re2| or |re3|...c                 G   s~   || _ d}d}g }g }d}|D ]!}| || |jr"|| d}n|| |jr,d}|d7 }q|| _|| _|| _|| _d S rn   )r+   rS   r`   r   ra   nullable_resnon_nullable_res)r5   r+   r`   ra   rx   ry   r   rm   r   r   r   rd   9  s&   



zAlt.__init__c                 C   s\   | j D ]}|||||| q| jr*|r| ||t}| jD ]}||||d| qd S d S )Nr   )rx   r;   ry   rA   re   )r5   r?   r7   r8   r9   r:   rm   r   r   r   r;   O  s   

zAlt.build_machinec                 C   rq   )NzAlt(%s)rr   rs   rL   r   r   r   rK   X  rv   zAlt.calc_strNrw   r   r   r   r   r*   5  s
    	r*   c                   @   rk   )	Rep1z@Rep1(re) is an RE which matches one or more repetitions of |re|.c                 C   s&   |  d| || _|j| _|j| _d S rh   )rS   rm   r`   ra   )r5   rm   r   r   r   rd   _  s   zRep1.__init__c                 C   sN   |  }|  }|| | j||||p| jj| || || d S rC   )r<   r=   rm   r;   ra   )r5   r?   r7   r8   r9   r:   ro   rp   r   r   r   r;   e  s   

zRep1.build_machinec                 C   s
   d| j  S )NzRep1(%s)rm   rL   r   r   r   rK   m  rH   zRep1.calc_strNrw   r   r   r   r   rz   \  s
    rz   c                   @   s0   e Zd ZdZdZdZdd Zdd Zdd ZdS )	
SwitchCasez
    SwitchCase(re, nocase) is an RE which matches the same strings as RE,
    but treating upper and lower case letters according to |nocase|. If
    |nocase| is true, case is ignored, otherwise it is not.
    Nc                 C   s    || _ || _|j| _|j| _d S rC   )rm   r:   r`   ra   )r5   rm   r:   r   r   r   rd   z  s   zSwitchCase.__init__c                 C   s   | j ||||| j d S rC   )rm   r;   r:   rf   r   r   r   r;     s   zSwitchCase.build_machinec                 C   s   | j rd}nd}d|| jf S )NNoCaseCasez%s(%s))r:   rm   )r5   namer   r   r   rK     s   zSwitchCase.calc_str)	r4   r[   r^   r_   rm   r:   rd   r;   rK   r   r   r   r   r|   q  s    r|   z8
    Empty is an RE which matches the empty string.
    Emptyc                 C   s$   t ttt|  }dt|  |_|S )z@
    Str1(s) is an RE which matches the literal string |s|.
    Str(%s))rD   tupleru   rc   rX   rJ   r   r   r   r   r   Str1  s   r   c                  G   sD   t | dkrt| d S tttt|  }ddtt|  |_|S )z
    Str(s) is an RE which matches the literal string |s|.
    Str(s1, s2, s3, ...) is an RE which matches any of |s1| or |s2| or |s3|...
    r   r   r   rr   )r
   r   r*   r   ru   rt   rX   rJ   )strsr   r   r   r   Str  s
   r   c                 C   s   t t| }dt|  |_|S )zH
    Any(s) is an RE which matches any character in the string |s|.
    zAny(%s))r,   r   rX   rJ   r   r   r   r   Any  s   r   c                 C   s:   t | }|dt  |t t|}dt|  |_|S )zp
    AnyBut(s) is an RE which matches any character (including
    newline) which is not in the string |s|.
    r   z
AnyBut(%s))r   insertmaxintr   r,   rX   rJ   )r   rangesr   r   r   r   AnyBut  s   
r   rT   zT
    AnyChar is an RE which matches any single character (including a newline).
    AnyCharc              	   C   s   |rt t| t|d }d| |f |_|S g }tdt| dD ]}|t t| | t| |d  d  q t| }dt|  |_|S )a  
    Range(c1, c2) is an RE which matches any single character in the range
    |c1| to |c2| inclusive.
    Range(s) where |s| is a string of even length is an RE which matches
    any single character in the ranges |s[0]| to |s[1]|, |s[2]| to |s[3]|,...
    r   zRange(%s,%s)r   r(   z	Range(%s))r#   r   rJ   r)   r
   r   r*   rX   )ro   rp   r   r   r   r   r   r   Range  s   *r   c                 C   s   t | t}d|  |_|S )zI
    Opt(re) is an RE which matches either |re| or the empty string.
    zOpt(%s))r*   r   rJ   rm   r   r   r   r   Opt  s   

r   c                 C   s   t t| }d|  |_|S )zJ
    Rep(re) is an RE which matches zero or more repetitions of |re|.
    zRep(%s))r   rz   rJ   r   r   r   r   Rep  s   
r   c                 C      t | ddS )z
    NoCase(re) is an RE which matches the same strings as RE, but treating
    upper and lower case letters as equivalent.
    r   r:   r|   r{   r   r   r   r}     s   r}   c                 C   r   )z
    Case(re) is an RE which matches the same strings as RE, but treating
    upper and lower case letters as distinct, i.e. it cancels the effect
    of any enclosing NoCase().
    r   r   r   r{   r   r   r   r~     s   r~   z=
    Bol is an RE which matches the beginning of a line.
    Bolz7
    Eol is an RE which matches the end of a line.
    Eolz9
    Eof is an RE which matches the end of the file.
    EofrC   )+r_   
__future__r   rZ   rT   r   r   re   ri   ZEOFr   r-   r   r   r"   r,   r#   objectr1   rc   r.   rg   r/   rb   rD   r*   rz   r|   r   rJ   r   r   r   r   r   r   r   r   r}   r~   r   r   r   r   r   r   r   <module>   sj    	O+'"		
		
