ofc@sdZddlmZddlmZmZddlmZmZeZ eZ ej Z eZ dZdZdZdefd YZd d Zd Zd Zd S(s0Utility functions used by the btm_matcher modulei(tpytree(tgrammarttoken(tpattern_symbolstpython_symbolsiiitMinNodecBsAeZdZdddZdZdZdZdZRS(sThis class serves as an intermediate representation of the pattern tree during the conversion to sets of leaf-to-root subpatternscCsC||_||_g|_t|_d|_g|_g|_dS(N( ttypetnametchildrentFalsetleaftNonetparentt alternativestgroup(tselfRR((s)/usr/lib64/python2.7/lib2to3/btm_utils.pyt__init__s      cCst|jdt|jS(Nt (tstrRR(R((s)/usr/lib64/python2.7/lib2to3/btm_utils.pyt__repr__scCsU|}g}xB|rP|jtkr|jj|t|jt|jkr|t|jg}g|_|j}qq|j}d}Pn|jt kr|j j|t|j t|jkrt |j }g|_ |j}qq|j}d}Pn|jt j kr4|jr4|j|jn|j|j|j}qW|S(sInternal method. Returns a characteristic path of the pattern tree. This method must be run for all leaves until the linear subpatterns are merged into a singleN(RtTYPE_ALTERNATIVESR tappendtlenRttupleR R t TYPE_GROUPRtget_characteristic_subpatternt token_labelstNAMER(Rtnodetsubp((s)/usr/lib64/python2.7/lib2to3/btm_utils.pyt leaf_to_root!s8        cCs1x*|jD]}|j}|r |Sq WdS(sDrives the leaf_to_root method. The reason that leaf_to_root must be run multiple times is because we need to reject 'group' matches; for example the alternative form (a | b c) creates a group [b c] that needs to be matched. Since matching multiple linear patterns overcomes the automaton's capabilities, leaf_to_root merges each group into a single choice based on 'characteristic'ity, i.e. (a|b c) -> (a|b) if b more characteristic than c Returns: The most 'characteristic'(as defined by get_characteristic_subpattern) path for the compiled pattern tree. N(tleavesR(RtlR((s)/usr/lib64/python2.7/lib2to3/btm_utils.pytget_linear_subpatternKs ccsEx-|jD]"}x|jD] }|VqWq W|jsA|VndS(s-Generator that returns the leaves of the treeN(RR(Rtchildtx((s)/usr/lib64/python2.7/lib2to3/btm_utils.pyR`s   N( t__name__t __module__t__doc__R RRRR!R(((s)/usr/lib64/python2.7/lib2to3/btm_utils.pyRs   * c Csd}|jtjkr(|jd}n|jtjkrt|jdkrht|jd|}qtdt }x|jD]P}|jj |drqnt||}|dk r|jj |qqWn$|jtj krxt|jdkr_tdt }x9|jD].}t||}|r|jj |qqW|jsud}quqt|jd|}n|jtjkrt|jdtjr|jdjdkrt|jd|St|jdtjr|jdjdks=t|jdkrAt|jddrA|jdjdkrAdSt}d}d}t}d} t} x|jD]}|jtjkrt}|}n<|jtjkrt}|} n|jtjkr|}nt|dro|jdkrot} qoqoW| rA|jd} t| drN| jdkrN|jd } qNn |jd} | jtjkr| jd krtdt}qTtt| jrtdtt| j}qTtdtt| j}n| jtjkr0| jjd } | tkrtdt| }qTtdtjd | }n$| jtjkrTt||}n|r| jdjd kryd}q| jdjdkrqt n|r|dk rxI|jdd!D]4}t||}|dk r|jj |qqWqn|r||_!n|S(s Internal function. Reduces a compiled pattern tree to an intermediate representation suitable for feeding the automaton. This also trims off any optional pattern elements(like [a], a*). iiRit(t[tvaluet=itanyt'Rt*t+iN("R RtsymstMatcherRt AlternativesRt reduce_treeRRtindexRt AlternativeRtUnitt isinstanceRtLeafR)thasattrtTrueR tDetailstRepeaterRRtTYPE_ANYtgetattrtpysymstSTRINGtstripttokenstNotImplementedErrorR ( RR tnew_nodeR"treducedR t details_nodetalternatives_nodet has_repeatert repeater_nodethas_variable_namet name_leafR((s)/usr/lib64/python2.7/lib2to3/btm_utils.pyR2hs             cs,t|ts|St|dkr-|dSg}g}dddddgg}dx|D]}tt|d ratt|fd r|j|qtt|fd r|j|q|j|qaqaW|r|}n|r |}n|r|}nt|d tS( sPicks the most characteristic from a list of linear patterns Current order used is: names > common_names > common_chars iitintfortiftnotR s[]().,:cSst|tkS(N(RR(R#((s)/usr/lib64/python2.7/lib2to3/btm_utils.pytscst|to|kS(N(R6R(R#(t common_chars(s)/usr/lib64/python2.7/lib2to3/btm_utils.pyROscst|to|kS(N(R6R(R#(t common_names(s)/usr/lib64/python2.7/lib2to3/btm_utils.pyROstkey(R6tlistRR+trec_testRtmax(t subpatternstsubpatterns_with_namestsubpatterns_with_common_namestsubpatterns_with_common_charst subpattern((RPRQs)/usr/lib64/python2.7/lib2to3/btm_utils.pyRs2      ccsWxP|D]H}t|ttfrDx*t||D] }|Vq2Wq||VqWdS(sPTests test_func on all items of sequence and items of included sub-iterablesN(R6RSRRT(tsequencet test_funcR#ty((s)/usr/lib64/python2.7/lib2to3/btm_utils.pyRTs   N(R&tRtpgen2RRtpygramRRR/R>topmapRARR<RRtobjectRR R2RRT(((s)/usr/lib64/python2.7/lib2to3/btm_utils.pyts X %