[ P@sddlZddlZddlmZddlmZed\ZZZ Z Z ej dej ejBjZej dej jZej dej ejBjZej dej jZej d ej jZej d ej jZGd d d eZGd ddZdS)N)Mapping)partialz ^ [ \t]* (?: while | else | def | return | assert | break | class | continue | elif | try | except | raise | import | yield ) \b z' [ \t]* (?: \# \S .* )? \n aK \""" [^"\\]* (?: (?: \\. | "(?!"") ) [^"\\]* )* (?: \""" )? | " [^"\\\n]* (?: \\. [^"\\\n]* )* "? | ''' [^'\\]* (?: (?: \\. | '(?!'') ) [^'\\]* )* (?: ''' )? | ' [^'\\\n]* (?: \\. [^'\\\n]* )* '? zM [ \t]* [^\s#\\] # if we match, m.end()-1 is the interesting char z_ \s* (?: return | break | continue | raise | pass ) \b z [^[\](){}#'"\\]+ c@sUeZdZdZddZddZddZdd Zd d d Zd S) StringTranslatePseudoMappingaTUtility class to be used with str.translate() This Mapping class wraps a given dict. When a value for a key is requested via __getitem__() or get(), the key is looked up in the given dict. If found there, the value from the dict is returned. Otherwise, the default value given upon initialization is returned. This allows using str.translate() to make some replacements, and to replace all characters for which no replacement was specified with a given character instead of leaving them as-is. For example, to replace everything except whitespace with 'x': >>> whitespace_chars = ' \t\n\r' >>> preserve_dict = {ord(c): ord(c) for c in whitespace_chars} >>> mapping = StringTranslatePseudoMapping(preserve_dict, ord('x')) >>> text = "a + b\tc\nd" >>> text.translate(mapping) 'x x x\tx\nx' cCs4||_||_|j|dd}||_dS)NcSs |||S)N)key_getZ_defaultrr%/usr/lib/python3.4/idlelib/PyParse.pyrzsz3StringTranslatePseudoMapping.__init__.._get) _non_defaultsZ_default_valuegetr)selfZ non_defaults default_valuerrrr __init__vs  z%StringTranslatePseudoMapping.__init__cCs |j|S)N)r)r itemrrr __getitem__~sz(StringTranslatePseudoMapping.__getitem__cCs t|jS)N)lenr )r rrr __len__sz$StringTranslatePseudoMapping.__len__cCs t|jS)N)iterr )r rrr __iter__sz%StringTranslatePseudoMapping.__iter__NcCs |j|S)N)r)r rdefaultrrr r sz StringTranslatePseudoMapping.get) __name__ __module__ __qualname____doc__rrrrr rrrr ras     rc@sBeZdZddZddZdeddZdd ZiZej d d d Dej d d dDej dd dDe ede dZddZ ddZ ddZddZddZddZdd Zd!d"Zd#d$ZdZd%d&ZdZd'd(ZdS))ParsercCs||_||_dS)N) indentwidthtabwidth)r rrrrr rs zParser.__init__cCs>t|dks(|ddks(t||_d|_dS)Nr )rAssertionErrorstr study_level)r srrr set_strs( zParser.set_strNc CsU|jd}}|sdSt|}xtdD]}|jdd|}|dkr^Pn|jdd|d}||||}|r||j r|j}Pn|}q3W|dkr||}|r||j r|j}n|S|d}xD|||}|rL|j\} }|| sM| }qMq Pq W|S)Nrz: rrr)r!rrangerfindstartspan) r Zis_char_in_string_synchrer!poslimitZtriesimr#rrr find_good_parse_starts8        zParser.find_good_parse_startcCsR|dks)|j|ddks)t|dkrN|j|d|_ndS)Nrrr)r!r )r lorrr set_los) z Parser.set_loccs'|]}t|tdfVqdS)(N)ord).0crrr szParser.z({[ccs'|]}t|tdfVqdS))N)r2)r3r4rrr r5sz)}]ccs'|]}t|t|fVqdS)N)r2)r3r4rrr r5sz"'\ #r xc Cs|jdkrdSd|_|j}|j|j}|jdd}|jdd}|jdd}|jdd}|jdd}t}d}}dg|_}|j}dt|}}x||kr||} |d}| dkrqn| dkr7|d}|dkr||qqn| d krS|d}qn| d krx|r|d}qqn| d ks| d kr| } ||d|d | dkr| d} n|} t| d} || }x||kr||} |d}| dkrqn||d|| | krF|| }Pn| dkr|d}| dkr|dkr||nPqqn| dkr||kst ||dkr|d}n|d}qqqW|d| krt }qt }qn| dkr5|j d|}|dkst qn| dksGt ||ksYt ||dkr|d}|d|krt }qn|d}qW|t kr|t kr|dkrt}n||_|tk|d|kkst |d|kr||ndS)NrZxxxxxxxxr7ZxxxxZxxz xrrr1r6"'\#rr)r"r! translate_tranreplaceC_NONE goodlinesappendrr C_STRING_FIRST_LINEC_STRING_NEXT_LINESfind C_BACKSLASH C_BRACKET continuation) r r!rIlevelZlnorBZ push_goodr,nchZquoteZfirstlnowrrr _study1s               "                      "zParser._study1cCs|j|jS)N)rNrI)r rrr get_continuation_typehs zParser.get_continuation_typecCs|jdkrdS|jd|_|j|j}}t|d}t|}x||r|sjt|}x@t||d||D]#}|jdd|dd}qWt||r|d}qXPqXW|dkr|dkst|}n|||_ |_ d}g}|j } |dfg} xu||krt |||} | r| j } | d}x*||kr||dkr|d}qrW||kr||}n| }||krPqn||} | dkr| || j |t|f| }|d}q5n| dkrh|r9|d =n| }|d}| j |t|fq5n| d ks| d kr| j |t|df| }t|||j }| j |t|fq5n| d krM| j |t|df|jd||d}|dks.t| j |t|fq5n| d ks_t|d}||ks{t||dkr| ||}n|d}q5W||_|r|d|_nt| |_dS)Nr:rrrz z([{z)]}r8r9r=r<rr)r"rNr!rBrr r%r&_junkre stmt_startstmt_endrC_chew_ordinaryreend_match_stringrerFlastchlastopenbracketpostuplestmt_bracketing)r r!rBr,pqZnothingrWstackZ push_stackZ bracketingr-ZnewprLrrr _study2}s     "!                   zParser._study2cCs|j|jtkst|j}|j}t|}|jdd|d}}|d}x||krt||}|r|j d}d}Pqg|j d|d}}qgW|}}x||dkr|d}qW|j }t|||j |j |S)Nrrrz )r^rIrHr rXr!rr&_itemrerUrFr expandtabsr)r jr!rKZorigir,r-Zextrarrr compute_bracket_indents&       zParser.compute_bracket_indentcCs#|j|j}|d|dS)Nrr:r)rNrB)r rBrrr get_num_lines_in_stmt s  zParser.get_num_lines_in_stmtcCs|j|jtkst|j}|j}x||dkrQ|d}q4W|}|jd|d}d}}x ||kr||}|dkr|d}|d}q{|dkr|r|d}n|d}q{|dks|dkrt|||j}q{|d kr#Pq{|dkry|d kry|dks[||dd kry||dd kryd}Pq{|d}q{W|r|d}t j d |||dk}n|s|}x!||d kr|d}qWnt ||j|j |j dS)Nz rrrz([{z)]}r8r9r==z=<>!z\s*\\z )r^rIrGr r!rRrFrVrUrematchrr`r)r r!r,startposendposfoundrJrLrrr compute_backslash_indentsJ              %zParser.compute_backslash_indentcCsg|j|j|j}}|}|j}x*||krX||dkrX|d}q/W|||S)Nz r)r^rRrSr!)r r,rKrar!rrr get_base_indent_stringGs  zParser.get_base_indent_stringcCs|j|jdkS)N:)r^rW)r rrr is_block_openerRs zParser.is_block_openercCs#|jt|j|jdk S)N)r^_closerer!rR)r rrr is_block_closerXs zParser.is_block_closercCs|j|jS)N)r^rX)r rrr get_last_open_bracket_pos_s z Parser.get_last_open_bracket_poscCs|j|jS)N)r^rZ)r rrr get_last_stmt_bracketinghs zParser.get_last_stmt_bracketing)rrrrr$r)r.r0r?updaterr2rNrOr^rbrdrkrlrnrprXrqrZrrrrrr rs.   3   m  5   r)rfsys collectionsr functoolsrr%rArGrDrErHcompileVERBOSE MULTILINEsearchr)rgrQDOTALLrVr_rorTrrrrrr s$   *