3 § fR2ã@sœdZddlmZddlZddlmZeejejdƒZ eejdƒZ dd„e dƒDƒZ d d„e dƒDƒZ Gd d „d ƒZed kr˜dd lmZeddd�dS)zîProvide 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Ú_cCsg|]}t|ƒtk‘qS©)ÚchrÚ_ASCII_ID_CHARS)Ú.0Úxrrú8/opt/alt/python36/lib64/python3.6/idlelib/hyperparser.pyú sr é€cCsg|]}t|ƒtk‘qSr)rÚ_ASCII_ID_FIRST_CHARS)rr rrr r sc@s\eZdZdd„Zdd„Zdd„Zdd„Zdd d „Zed ddhƒZ e dd„ƒZ dZ dd„Z dS)Ú HyperParserc sJ|ˆ_|jˆ_}tj|j|jƒ}dd„}||j|ƒƒ}|js¸xh|jD]^}t ||dƒ}t |ƒd} d|} |j |j | | ƒdƒ|j |j| ƒƒ} | dk s¢|dkrFPqFW|j| p²dƒnF|jd |ƒ} | rÒ| d} nd } d|} |j |j | | ƒdƒ|jdƒ|jdd…ˆ_| ˆ_|jƒˆ_‡fd d „ttˆjƒƒDƒˆ_ˆj|ƒdS)z;To initialize, analyze the surroundings of the given index.cSs tt|ƒƒS)N)ÚintÚfloat)Úindexrrr Ú index2line"sz(HyperParser.__init__..index2lineéz.0z%d.endz NrZconsolez1.0écs4g|],}|dko.ˆj|dˆj|ddk‘qS)rr)Ú bracketing)rÚi)Úselfrr r Ksz(HyperParser.__init__..éþÿÿÿ)ÚeditwinÚtextrZParserZ indentwidthZtabwidthrZcontext_use_ps1Znum_context_linesÚmaxÚreprZset_codeÚgetZfind_good_parse_startZ_build_char_in_string_funcZset_loZ tag_prevrangeÚcodeÚrawtextÚ stopatindexZget_last_stmt_bracketingrÚrangeÚlenÚisopenerÚ set_index) rrrrÚparserrZlnoÚcontextZstartatZ startatindexr ZbodÚrr)rr Ú__init__s:         zHyperParser.__init__cCsÒt|jƒt|jj||jƒƒ}|dkr2td|ƒ‚||_d|_x@|jt|jƒdkr~|j|jdd|jkr~|jd7_q@W|jt|jƒdkrÎ|j|jdd|jkrÎ|j |jd rÎ|jd7_dS)zgSet the index to which the functions relate. The index must be in the same statement. rz(Index %s precedes the analyzed statementrN) r"rrrr Ú ValueErrorÚindexinrawtextÚ indexbracketrr#)rrr*rrr r$QszHyperParser.set_indexcCs&|j|jo$|j|j|jddkS)z2Is the index given to the HyperParser in a string?rú"ú')r,r-)r#r+rr)rrrr Ú is_in_stringfs zHyperParser.is_in_stringcCs(|j|j p&|j|j|jddkS)z5Is the index given to the HyperParser in normal code?rú#r,r-)r/r,r-)r#r+rr)rrrr Ú is_in_codenszHyperParser.is_in_codeú([{FcCsN|j|jd}|j}xb|j| sN|j|j|d|ksN|j|d|krx|d8}|dkrbdSt||j|dƒ}qW|jd}x,|t|jƒkr°|j|d|kr°|d7}q†W|jjd|jt|jƒ|j|dfƒ}|t|jƒk�s|j|dt|jƒk�r|�rdS|j}n.|jjd|jt|jƒ|j|ddfƒ}||fS)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. rrNz%s-%dc) rr+r#rÚminr"rrr )rZopenersZ mustcloseZbracketinglevelZbeforeÚafterZ beforeindexZ afterindexrrr Úget_surrounding_bracketsts0   ""z$HyperParser.get_surrounding_bracketsÚTrueÚFalseÚNonecCsbt}|}x<||krDt||dƒdkrD|t||dƒrD|d8}q W||ko`t||dƒdk�rx0|d|kr”d||d|…jƒr”|d8}qfW|d|krÂd||d|…jƒrÂ|d8}|d|krîd||d|…jƒrî|d8}|||…jƒ�s&dSn ||k�r&tt||ƒ�s&dS||k�rZt|||…ƒ�rZ|||…|jk�rZdS||S)zÈGiven 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. rr éÚarr)Ú_IS_ASCII_ID_CHARÚordÚ isidentifierÚ_IS_ASCII_ID_FIRST_CHARrÚ _ID_KEYWORDS)ÚclsÚstrÚlimitÚposZis_ascii_id_charrrrr Ú_eat_identifier¢s.  & $$  zHyperParser._eat_identifierz \c Csö|jƒstdƒ‚|j}|j}|j}||d}|j}|}d}�x¨x¤||krf||d|jkrf|d8}qB| r’||kr’||ddkr’|d8}d}qB||krà|dkrà|||dddkrà|d8}||d}||dd}qBPqBW|sìP|j|||ƒ}|�r||}|}d}q@||k�râ||d} x.|dk�rV||dd| k�rV|d8}�q*W||d|k�rlP||d}|d8}||d}|}||d k�r n@||d k�rÞx*|dk�rØ||dd k�rØ|d8}�q°W|}Pq@Pq@W|||j…S) z‚Return 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.rTrÚ.r/rFz([z'"ZrRbBuU)r0r)rrr+r*Ú_whitespace_charsrC) rrrZ brck_indexZ brck_limitrBZlast_identifier_posZ postdot_phaseZretÚlevelrrr Úget_expressionÞsb     "  zHyperParser.get_expressionN)r1F)Ú__name__Ú __module__Ú __qualname__r(r$r.r0r4Ú frozensetr>Ú classmethodrCrErGrrrr rs7 , :rÚ__main__)Úmainz"idlelib.idle_test.test_hyperparserr)Ú verbosity)Ú__doc__ÚkeywordrÚstringZidlelibrrKZ ascii_lettersZdigitsrr r!r:r=rrHZunittestrNrrrr Ús