[M2@sdZddlZddlmZddlmZeejejdZ eejdZ dde dDZ d de dDZ Gd d d Zed krddlZejd ddndS)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. N) iskeyword)PyParse_cCs"g|]}t|tkqS)chr_ASCII_ID_CHARS).0xrr)/usr/lib/python3.4/idlelib/HyperParser.py s r cCs"g|]}t|tkqSr)r_ASCII_ID_FIRST_CHARS)rr rrr r s c@seZdZddZddZddZddZd d d d Zed ddhZ e ddZ dZ ddZ dS) HyperParserc s|_|j_}tj|j|j}dd}||j|}|jsx|jD]}t ||d}t |d} d|} |j |j | | d|j |j| } | dk s|dkrePqeqeW|j| pdn_|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.cSstt|S)N)intfloat)indexrrr index2line#sz(HyperParser.__init__..index2linez.0z%d.endz NrZconsolez1.0csBg|]8}|dko;j|dj|ddkqS)rr) bracketing)ri)selfrr r Ls z(HyperParser.__init__..)editwintextrZParserZ indentwidthZtabwidthrZcontext_use_ps1Znum_context_linesmaxreprZset_strgetZfind_good_parse_startZ_build_char_in_string_funcZset_loZ tag_prevrangestrrawtext stopatindexZget_last_stmt_bracketingrrangelenisopener set_index) rrrrparserrZlnocontextZstartatZ startatindexr Zbodrr)rr __init__s:        zHyperParser.__init__cCst|jt|jj||j}|dkrJtd|n||_d|_xP|jt|jdkr|j|jdd|jkr|jd7_q_W|jt|jdkr|j|jdd|jkr|j |jd r|jd7_ndS)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$Rs     !!zHyperParser.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_stringgszHyperParser.is_in_stringcCs0|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_codeoszHyperParser.is_in_codez([{FcCs|j|jd}|j}x{|j| sc|j|j|d|ksc|j|d|kr|d8}|dkr}dSt||j|d}q W|jd}x:|t|jkr|j|d|kr|d7}qW|jjd|jt|j|j|df}|t|jksQ|j|dt|jkrg|r[dS|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 mustcloseZbracketinglevelZbeforeZafterZ beforeindexZ afterindexrrr get_surrounding_bracketsus0      +    )z$HyperParser.get_surrounding_bracketsTrueFalseNonecCst}|}xL||krZt||ddkrZ|t||drZ|d8}qW||krSt||ddkrSx<|d|krd||d|jr|d8}qW|d|krd||d|jr|d8}n|d|kr6d||d|jr6|d8}n|||jszdSn'||krztt||szdSn||krt|||r||||jkrdS||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)clsrlimitposZis_ascii_id_charrrrr _eat_identifiers.&1. .   zHyperParser._eat_identifierz \c Csw|jstdn|j}|j}|j}||d}|j}|}d}x x||kr||d|jkr|d8}q_| r||kr||ddkr|d8}d}q_||kr*|dkr*|||dddkr*|d8}||d}||dd}q_Pq_W|s9Pn|j|||}|rm||}|}d}q\||kra||d} x2|dkr||dd| kr|d8}qW||d|krPn||d}|d8}||d}|}||d krqb||d kr]x.|dkrS||dd krS|d8}q&W|}nPq\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 calledif index is inside a code.rTr.r/rFz([z'"ZrRbBuU)r0r)rrr+r*_whitespace_charsr@) rrrZ brck_indexZ brck_limitr?Zlast_identifier_posZ postdot_phaseZretlevelrrr get_expressionsb     #        ' # zHyperParser.get_expressionN)__name__ __module__ __qualname__r(r$r.r0r2 frozensetr< classmethodr@rBrDrrrr rs  7   ,:r__main__z"idlelib.idle_test.test_hyperparser verbosityr)__doc__stringkeywordrZidlelibrrHZ ascii_lettersZdigitsrr r!r8r;rrEZunittestmainrrrr s