a
    IfS2                     @   s   d Z 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 )zProvide advanced parsing abilities for ParenMatch and other extensions.

HyperParser uses PyParser.  PyParser mostly gives information on the
proper indentation of code.  HyperParser gives additional information on
the structure of code.
    )	iskeywordN)pyparse_c                 C   s   g | ]}t |tv qS  )chr_ASCII_ID_CHARS.0xr   r   $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 )z;To initialize, analyze the surroundings of the given index.c                 S   s   t t| S )N)intfloat)indexr   r   r   
index2line"   s    z(HyperParser.__init__.<locals>.index2line   z.0z%d.endz 
Nr   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 )zgSet the index to which the functions relate.

        The index must be in the same statement.
        r   z(Index %s precedes the analyzed statementr   N)
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 )z2Is the index given to the HyperParser in a string?r   )"'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 )z5Is the index given to the HyperParser in normal code?r   )#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 )a  Return bracket indexes or None.

        If the index given to the HyperParser is surrounded by a
        bracket defined in openers (or at least has one before it),
        return the indices of the opening bracket and the closing
        bracket (or the end of line, whichever comes first).

        If it is not surrounded by brackets, or the end of line comes
        before the closing bracket and mustclose is True, returns None.
        r   r   Nz%s-%dc)	r   r.   r%   r!   minr$   r   r   r"   )r   ZopenersZ	mustcloseZbracketinglevelZbeforeZafterZbeforeindexZ
afterindexr   r   r   get_surrounding_bracketst   s>    


z$HyperParser.get_surrounding_brackets>   TrueNone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 )zGiven a string and pos, return the number of chars in the
        identifier which ends at pos, or 0 if there is no such one.

        This ignores non-identifier eywords are not identifiers.
        r   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 )zReturn a string with the Python expression which ends at the
        given index, which is empty if there is no real one.
        z?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_charsrG   )
r   r!   r   Z
brck_indexZ
brck_limitrF   Zlast_identifier_posZpostdot_phaseZretlevelr   r   r   get_expression   sf    

 zHyperParser.get_expressionN)r5   F)__name__
__module____qualname__r+   r&   r2   r4   r7   	frozensetrB   classmethodrG   rI   rK   r   r   r   r   r      s   7
,
9r   __main__)mainz"idlelib.idle_test.test_hyperparserr=   )	verbosity)__doc__keywordr   stringZidlelibr   rO   Zascii_lettersZdigitsr   r   r#   r>   rA   r   rL   ZunittestrR   r   r   r   r   <module>   s     