B
    –ìZ+  ã               @   sÒ   d dl Z d dlZd dlZd dl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„ ZG dd„ deƒZddd„Zdadd„ Zdd„ Zdd„ Zdd„ ZdS ) é    Nc               @   s&   e Zd Zdd„ Zddd„Zdd„ ZdS )	ÚChangeCollectorc             C   s   || _ g | _d S )N)ÚtextÚchanges)Úselfr   © r   ú4lib/python3.7/site-packages/rope/base/codeanalyze.pyÚ__init__	   s    zChangeCollector.__init__Nc             C   s,   |d kr| j ||… }| j |||f¡ d S )N)r   r   Úappend)r   ÚstartÚendZnew_textr   r   r   Ú
add_change   s    zChangeCollector.add_changec             C   s˜   | j s
d S | j jdd„ d g }d}x4| j D ]*}|\}}}| | j||… | ¡ |}q,W |t| jƒk r|| | j|d … ¡ d |¡}|| jkr”|S d S )Nc             S   s   | d d… S )Né   r   )Úxr   r   r   Ú<lambda>   s    z-ChangeCollector.get_changed.<locals>.<lambda>)Úkeyr   Ú )r   Úsortr	   r   ÚlenÚjoin)r   ÚpiecesZlast_changedZchanger
   r   r   Úresultr   r   r   Úget_changed   s    


zChangeCollector.get_changed)N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r      s   
r   c               @   sH   e 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S )ÚSourceLinesAdapterzYAdapts source to Lines interface

    Note: The creation of this class is expensive.
    c             C   s   || _ d | _|  ¡  d S )N)ÚcodeÚstartsÚ_initialize_line_starts)r   Zsource_coder   r   r   r   *   s    zSourceLinesAdapter.__init__c             C   sn   g | _ | j  d¡ y,d}x"| j d|¡d }| j  |¡ qW W n tk
rR   Y nX | j  t| jƒd ¡ d S )Nr   Ú
é   )r   r	   r   ÚindexÚ
ValueErrorr   )r   Úir   r   r   r   /   s    z*SourceLinesAdapter._initialize_line_startsc             C   s"   | j | j|d  | j| d … S )Nr    )r   r   )r   Úlinenor   r   r   Úget_line;   s    zSourceLinesAdapter.get_linec             C   s   t | jƒd S )Nr    )r   r   )r   r   r   r   Úlength?   s    zSourceLinesAdapter.lengthc             C   s   t   | j|¡S )N)Úbisectr   )r   Úoffsetr   r   r   Úget_line_numberB   s    z"SourceLinesAdapter.get_line_numberc             C   s   | j |d  S )Nr    )r   )r   r$   r   r   r   Úget_line_startE   s    z!SourceLinesAdapter.get_line_startc             C   s   | j | d S )Nr    )r   )r   r$   r   r   r   Úget_line_endH   s    zSourceLinesAdapter.get_line_endN)r   r   r   Ú__doc__r   r   r%   r&   r)   r*   r+   r   r   r   r   r   $   s   r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚArrayLinesAdapterc             C   s
   || _ d S )N)Úlines)r   r.   r   r   r   r   N   s    zArrayLinesAdapter.__init__c             C   s   | j |d  S )Nr    )r.   )r   Úline_numberr   r   r   r%   Q   s    zArrayLinesAdapter.get_linec             C   s
   t | jƒS )N)r   r.   )r   r   r   r   r&   T   s    zArrayLinesAdapter.lengthN)r   r   r   r   r%   r&   r   r   r   r   r-   L   s   r-   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚLinesToReadlinec             C   s   || _ || _d S )N)r.   Úcurrent)r   r.   r
   r   r   r   r   Z   s    zLinesToReadline.__init__c             C   s8   | j | j ¡ kr4|  j d7  _ | j | j d ¡d S dS )Nr    r   r   )r1   r.   r&   r%   )r   r   r   r   Úreadline^   s    zLinesToReadline.readlinec             C   s   |   ¡ S )N)r2   )r   r   r   r   Ú__call__d   s    zLinesToReadline.__call__N)r   r   r   r   r2   r3   r   r   r   r   r0   X   s   r0   c               @   s.   e Zd Zdd„ Zdd„ Ze d¡Zdd„ ZdS )	Ú_CustomGeneratorc             C   s   || _ d| _d| _d| _d S )Nr   r   F)r.   Ú	in_stringÚ
open_countÚcontinuation)r   r.   r   r   r   r   j   s    z_CustomGenerator.__init__c             C   s¬   | j  ¡ }g }d}x”||kr¦x$||kr@| j  |¡ ¡ s@|d7 }qW ||kr|}x>| j  |¡}|  |¡ | jsx| jsx| jr€||kr‚P |d7 }qPW | ||f¡ |d7 }qW |S )Nr    )	r.   r&   r%   ÚstripÚ_analyze_liner7   r6   r5   r	   )r   Úsizer   r#   r
   Úliner   r   r   r3   p   s$    


z_CustomGenerator.__call__z,(\\*)((\'\'\'|"""|\'|")|#|\[|\]|\{|\}|\(|\))c             C   sÎ   d }x | j  |¡D ]}| d¡}| d¡}t|ƒd dkr<q|dkrb| jsR|| _n| j|krbd| _| jrjq|dkrtP |dkrŒ|  jd7  _q|dkr|  jd8  _qW |rÄ|dkrÄ| d¡rÄd	| _nd
| _d S )Nr    r   )z'''z"""ú'ú"r   ú#z([{z)]}ú\TF)Ú_main_tokensÚfinditerÚgroupr   r5   r6   Úendswithr7   )r   r;   ÚtokenÚmatchÚprefixr   r   r   r9   ‡   s,    


z_CustomGenerator._analyze_lineN)	r   r   r   r   r3   ÚreÚcompiler@   r9   r   r   r   r   r4   h   s   
r4   c             C   s
   t | ƒƒ S )N)r4   )r.   r   r   r   Úcustom_generator¢   s    rI   c               @   sP   e Z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S )ÚLogicalLineFinderc             C   s
   || _ d S )N)r.   )r   r.   r   r   r   r   ¨   s    zLogicalLineFinder.__init__c          
   C   sŽ   t | j |¡ƒ}d}xtt| j||ƒ}y|  ||¡S  tk
r„ } z6|d7 }|dkrV|‚|j| d }t | j |¡ƒ}W d d }~X Y qX qW d S )Nr   r    é   )Úcount_line_indentsr.   r%   Úget_block_startÚ_block_logical_lineÚIndentationErrorr$   )r   r/   ÚindentsZtriesÚblock_startÚer$   r   r   r   Úlogical_line_in«   s    z!LogicalLineFinder.logical_line_inr    Nc             c   s$   x|   ||¡D ]\}}|V  qW d S )N)Úgenerate_regions)r   Ú
start_lineÚend_liner
   r   r   r   r   Úgenerate_starts¹   s    z!LogicalLineFinder.generate_startsc       	      c   sŽ   d}t | j|ƒ}ybx\|  |¡D ]N\}}|| d }|  |¡}|d k	rN||krNP || d }||kr||fV  qW W n tjk
rˆ   Y nX d S )Nr    )r0   r.   Ú_logical_linesÚ_first_non_blankÚtokenizeÚ
TokenError)	r   rU   rV   rQ   r2   r
   r   Z
real_startZreal_endr   r   r   rT   ½   s    
z"LogicalLineFinder.generate_regionsc             C   sj   t | j|ƒ}|| d }|  ||¡}|  |d | d ¡}|d d krR| j ¡ }n|d | d }||fS )Nr    r   )r0   r.   Ú_calculate_logicalrY   r&   )r   rQ   r/   r2   ZshiftedZregionr
   r   r   r   r   rN   Í   s    z%LogicalLineFinder._block_logical_linec          
   C   s‚   d}y4x.|   |¡D ] \}}||kr*||fS |d }qW W n@ tjk
rx } z |jd d }|t||d ƒfS d }~X Y nX |d fS )Nr    r   )rX   rZ   r[   ÚargsÚmax)r   r2   r/   Úlast_endr
   r   rR   r1   r   r   r   r\   Ø   s     z$LogicalLineFinder._calculate_logicalc             c   sH   d}x>t  |¡D ]0}|d d }|d tjkr||fV  |d }qW d S )Nr    r   r   )rZ   Úgenerate_tokensrD   ÚNEWLINE)r   r2   r_   Úcurrent_tokenr1   r   r   r   rX   ä   s    
z LogicalLineFinder._logical_linesc             C   sF   |}x<|| j  ¡ k r@| j  |¡ ¡ }|r6| d¡s6|S |d7 }qW |S )Nr>   r    )r.   r&   r%   r8   Ú
startswith)r   r/   r1   r;   r   r   r   rY   ì   s    z"LogicalLineFinder._first_non_blank)r    N)r    N)r   r   r   r   rS   rW   rT   rN   r\   rX   rY   r   r   r   r   rJ   ¦   s   

rJ   c             C   s   t | ƒ ¡ S )N)rJ   rT   )r.   r   r   r   Útokenizer_generatorö   s    rd   c               @   sR   e Zd Zefdd„ZdZedd„ ƒZdZedd„ ƒZ	dd	„ Z
d
d„ Zddd„ZdS )ÚCachingLogicalLineFinderc             C   s   || _ || _d S )N)r.   Ú	_generate)r   r.   Zgenerater   r   r   r   ü   s    z!CachingLogicalLineFinder.__init__Nc             C   s   | j d kr|  ¡  | j S )N)Ú_startsÚ_init_logicals)r   r   r   r   r     s    
zCachingLogicalLineFinder.startsc             C   s   | j d kr|  ¡  | j S )N)Ú_endsrh   )r   r   r   r   Úends
  s    
zCachingLogicalLineFinder.endsc             C   sX   | j  ¡ d }dg| | _dg| | _x,|  | j ¡D ]\}}d| j|< d| j|< q4W dS )z.Should initialize _starts and _ends attributesr    NT)r.   r&   rg   ri   rf   )r   r:   r
   r   r   r   r   rh     s    
z'CachingLogicalLineFinder._init_logicalsc             C   sj   |}x|dkr"| j | s"|d8 }qW |dkrXy| j  d|¡}W n tk
rV   ||fS X || j d|¡fS )Nr   r    T)r   r!   r"   rj   )r   r/   r
   r   r   r   rS     s    
z(CachingLogicalLineFinder.logical_line_inr    c             c   s:   |d kr| j  ¡ }x"t||ƒD ]}| j| r|V  qW d S )N)r.   r&   Úranger   )r   rU   rV   r!   r   r   r   rW   $  s
    

z(CachingLogicalLineFinder.generate_starts)r    N)r   r   r   rI   r   rg   Úpropertyr   ri   rj   rh   rS   rW   r   r   r   r   re   ú   s   	re   éP   c       
      C   sú   t ƒ }xît|ddƒD ]Þ}| |  |¡¡}|dk	rt|  |¡ƒ|kr|j ¡ }|dkr^| d¡sh| d¡rîd}xvt|t|d |  	¡ d ƒƒD ]V}xF|  |¡D ]8}	|	dkr¨P |	d	kr¸|d7 }|	d
krš|d8 }|dk ršP qšW |dk rŠP qŠW |dk rîq|S qW dS )zApproximate block startr   éÿÿÿÿNr    ÚifÚforrK   r>   z[(z)])
Úget_block_start_patternsrk   Úsearchr%   rL   ÚstringÚlstriprc   Úminr&   )
r.   r$   Zmaximum_indentsÚpatternr#   rE   ZstripedZbracsÚjÚcr   r   r   rM   ,  s0    
"rM   c              C   s   t sd} t | tj¡a t S )NzS^\s*(((def|class|if|elif|except|for|while|with)\s)|((try|else|finally|except)\s*:)))Ú_block_start_patternrG   rH   ÚM)rv   r   r   r   rq   L  s    rq   c             C   s>   d}x4| D ],}|dkr |d7 }q
|dkr2|d7 }q
|S q
W dS )Nr   ú r    ú	é   r   )r;   rP   Úcharr   r   r   rL   U  s    


rL   c           	   C   s6   d} d|  }d|  }d  || dd¡|| dd¡g¡S )Nz(\b[uU]?[rR]?)?z"%s"""(\\.|"(?!"")|\\\n|[^"\\])*"""z%s"(\\.|\\\n|[^"\\])*"ú|r=   r<   )r   Úreplace)r
   ZlongstrZshortstrr   r   r   Úget_string_patterna  s
    r   c               C   s   dS )Nz#[^\n]*r   r   r   r   r   Úget_comment_patterni  s    r‚   )rm   )r'   rG   rD   rZ   Úobjectr   r   r-   r0   r4   rI   rJ   rd   re   rM   ry   rq   rL   r   r‚   r   r   r   r   Ú<module>   s$   (:P2
	