3  fR2@sdZddlmZddlZddlmZeejejdZ eejdZ dde dDZ d de dDZ Gd d d Zed krdd lmZeddddS)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_cCsg|]}t|tkqS)chr_ASCII_ID_CHARS).0xrr8/opt/alt/python36/lib64/python3.6/idlelib/hyperparser.py sr cCsg|]}t|tkqSr)r_ASCII_ID_FIRST_CHARS)rr rrr r sc@s\eZdZddZddZddZddZdd d Zed ddhZ e ddZ dZ ddZ dS) HyperParserc sJ|_|j_}tj|j|j}dd}||j|}|jsxh|jD]^}t ||d}t |d} d|} |j |j | | d|j |j| } | dk s|dkrFPqFW|j| pdnF|jd |} | r| d} nd } d|} |j |j | | d|jd|jdd_| _|j_fd d ttjD_j|dS)z;To initialize, analyze the surroundings of the given index.cSs tt|S)N)intfloat)indexrrr index2line"sz(HyperParser.__init__..index2linez.0z%d.endz NrZconsolez1.0cs4g|],}|dko.j|dj|ddkqS)rr) bracketing)ri)selfrr r Ksz(HyperParser.__init__..)editwintextrZParserZ indentwidthZtabwidthrZcontext_use_ps1Znum_context_linesmaxreprZset_codegetZfind_good_parse_startZ_build_char_in_string_funcZset_loZ tag_prevrangecoderawtext stopatindexZget_last_stmt_bracketingrrangelenisopener set_index) rrrrparserrZlnocontextZstartatZ startatindexr Zbodrr)rr __init__s:         zHyperParser.__init__cCst|jt|jj||j}|dkr2td|||_d|_x@|jt|jdkr~|j|jdd|jkr~|jd7_q@W|jt|jdkr|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 ValueErrorindexinrawtext 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|jkr|j|d|kr|d7}qW|jjd|jt|j|j|df}|t|jks|j|dt|jkr|rdS|j}n.|jjd|jt|j|j|ddf}||fS)aReturn 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#rminr"rrr )rZopenersZ mustcloseZbracketinglevelZbeforeafterZ beforeindexZ afterindexrrr get_surrounding_bracketsts0   ""z$HyperParser.get_surrounding_bracketsTrueFalseNonecCsbt}|}x<||krDt||ddkrD|t||drD|d8}q W||ko`t||ddkrx0|d|krd||d|jr|d8}qfW|d|krd||d|jr|d8}|d|krd||d|jr|d8}|||js&dSn ||kr&tt||s&dS||krZt|||rZ||||jkrZdS||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. rr arr)_IS_ASCII_ID_CHARord isidentifier_IS_ASCII_ID_FIRST_CHARr _ID_KEYWORDS)clsstrlimitposZis_ascii_id_charrrrr _eat_identifiers.  & $$  zHyperParser._eat_identifierz \c Cs|jstd|j}|j}|j}||d}|j}|}d}xx||krf||d|jkrf|d8}qB| r||kr||ddkr|d8}d}qB||kr|dkr|||dddkr|d8}||d}||dd}qBPqBW|sP|j|||}|r||}|}d}q@||kr||d} x.|dkrV||dd| krV|d8}q*W||d|krlP||d}|d8}||d}|}||d krn@||d krx*|dkr||dd kr|d8}qW|}Pq@Pq@W|||jS) 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.rTr.r/rFz([z'"ZrRbBuU)r0r)rrr+r*_whitespace_charsrC) rrrZ brck_indexZ brck_limitrBZlast_identifier_posZ postdot_phaseZretlevelrrr get_expressionsb     "  zHyperParser.get_expressionN)r1F)__name__ __module__ __qualname__r(r$r.r0r4 frozensetr> classmethodrCrErGrrrr rs7 , :r__main__)mainz"idlelib.idle_test.test_hyperparserr) verbosity)__doc__keywordrstringZidlelibrrKZ ascii_lettersZdigitsrr r!r:r=rrHZunittestrNrrrr s