a
    zeS2                     @   s   d dl mZ d dlZd dlmZ eejej d Zeejd Z	dd e
dD Zdd e
dD ZG d	d
 d
Zedkrd dlmZ eddd dS )    )	iskeywordN)pyparse_c                 C   s   g | ]}t |tv qS  )chr_ASCII_ID_CHARS.0xr   r   )/usr/lib/python3.9/idlelib/hyperparser.py
<listcomp>       r      c                 C   s   g | ]}t |tv qS r   )r   _ASCII_ID_FIRST_CHARSr   r   r   r   r      r   c                   @   sZ   e Zd Zdd Zdd Zdd Zdd ZdddZeh dZ	e
dd ZdZdd ZdS )HyperParserc                    sH  | _ |j  _}t|j|j}dd }|||}|js|jD ]`}t	|| d}t
|d }	d| }
|||	|
d  |||	}|d us|dkrD qqD||pd nF|d|}|r|d }	nd	}	d| }
|||	|
d  |d |jd d
  _|
 _|  _ fddtt jD  _ | d S )Nc                 S   s   t t| S )N)intfloat)indexr   r   r   
index2line"   s    z(HyperParser.__init__.<locals>.index2line   z.0z%d.endz 
r   Zconsolez1.0c                    s4   g | ],}|d ko. j | d  j |d  d kqS )r   r   )
bracketing)r	   iselfr   r   r   K   s   z(HyperParser.__init__.<locals>.<listcomp>)editwintextr   ZParserZindentwidthZtabwidthr   Zprompt_last_lineZnum_context_linesmaxreprZset_codegetZfind_good_parse_startZ_build_char_in_string_funcZset_loZtag_prevrangecoderawtextstopatindexZget_last_stmt_bracketingr   rangelenisopener	set_index)r   r   r   r   parserr   lnocontextZstartatZstartatindexr"   Zbodrr   r   r   __init__   s>    




zHyperParser.__init__c                 C   s   t | jt | j|| j }|dk r2td| || _d| _| jt | jd k r|| j| jd  d | jk r||  jd7  _q>| jt | jd k r| j| jd  d | jkr| j	| jd  s|  jd7  _d S )Nr   z(Index %s precedes the analyzed statementr   )
r$   r!   r   r   r"   
ValueErrorindexinrawtextindexbracketr   r%   )r   r   r-   r   r   r   r&   Q   s&    zHyperParser.set_indexc                 C   s&   | j | j o$| j| j| j d  dv S )Nr   )"'r%   r.   r!   r   r   r   r   r   is_in_stringf   s
    zHyperParser.is_in_stringc                 C   s(   | j | j  p&| j| j| j d  dvS )Nr   )#r/   r0   r1   r   r   r   r   
is_in_coden   s
    zHyperParser.is_in_code([{Fc                 C   sB  | j | j d }| j}| j| rJ| j| j | d  |vsJ| j | d |krt|d8 }|dk r^d S t|| j | d }q| jd }|t| j k r| j | d |kr|d7 }q~| jd| jt| j| j | d  f }|t| j ks| j | d t| jkr|rd S | j}n.| jd| jt| j| j | d d  f }||fS )Nr   r   z%s-%dc)	r   r.   r%   r!   minr$   r   r   r"   )r   ZopenersZ	mustcloseZbracketinglevelbeforeZafterZbeforeindexZ
afterindexr   r   r   get_surrounding_bracketst   s>    


z$HyperParser.get_surrounding_brackets>   NoneTrueFalsec                 C   sV  t }|}||krBt||d  dk rB|t||d   rB|d8 }q||krt||d  dkr|d |krd||d |   r|d8 }q^|d |krd||d |   r|d8 }|d |krd||d |   r|d8 }|||  sdS n ||k rtt||  sdS ||k rNt||| rN||| | jvrNdS || S )Nr   r      a   r   )_IS_ASCII_ID_CHARordisidentifier_IS_ASCII_ID_FIRST_CHARr   _ID_KEYWORDS)clsstrlimitposZis_ascii_id_charr   r   r   r   _eat_identifier   s6    
$
$$

zHyperParser._eat_identifierz 	
\c           
      C   s  |   std| j}| j}| j}|| d }| j}|}d}||kr`||d  | jv r`|d8 }q<|s||kr||d  dkr|d8 }d}q<||kr|dkr|||d  d  dkr|d8 }|| d }||d  d }q<qq<|sq| |||}|r
|| }|}d}q<||kr|| d }	|dkrL||d  d |	krL|d8 }q || d |krbq|| d }|d8 }|| d }|}|| d	v rn>|| d
v r|dkr||d  dv r|d8 }q|}qq<qq<||| j S )Nz?get_expression should only be called if index is inside a code.r   Tr   .r3   r>   Fz([z'"ZrRbBuU)r4   r,   r!   r   r.   r-   _whitespace_charsrH   )
r   r!   r   Z
brck_indexZ
brck_limitrG   Zlast_identifier_posZpostdot_phaseretlevelr   r   r   get_expression   sf    

 zHyperParser.get_expressionN)r5   F)__name__
__module____qualname__r+   r&   r2   r4   r8   	frozensetrC   classmethodrH   rJ   rM   r   r   r   r   r      s   7
,
9r   __main__)mainz"idlelib.idle_test.test_hyperparserr>   )	verbosity)keywordr   stringZidlelibr   rQ   ascii_lettersdigitsr   r   r#   r?   rB   r   rN   ZunittestrT   r   r   r   r   <module>   s     