ž zyMa'c@s°dZddlmZddlmZmZddlmZmZeZ eZ ej Z eZ dZdZdZGdd„deƒZdd d „Zd d „Zd d„ZdS(u0Utility functions used by the btm_matcher modulei(upytree(ugrammarutoken(upattern_symbolsupython_symbolsiicBs\|EeZdZdZd d dd„Zdd„Zdd„Zdd „Zd d „Z d S( uMinNodeu‰This class serves as an intermediate representation of the pattern tree during the conversion to sets of leaf-to-root subpatternscCsC||_||_g|_d|_d|_g|_g|_dS(NF( utypeunameuchildrenuFalseuleafuNoneuparentu alternativesugroup(uselfutypeuname((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu__init__s      uMinNode.__init__cCst|jƒdt|jƒS(Nu (ustrutypeuname(uself((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu__repr__suMinNode.__repr__cCsU|}g}xB|rP|jtkr|jj|ƒt|jƒt|jƒkr|t|jƒg}g|_|j}qq|j}d}Pn|jt kr|j j|ƒt|j ƒt|jƒkrót |j ƒ}g|_ |j}qq|j}d}Pn|jt j kr4|jr4|j|jƒn|j|jƒ|j}qW|S(u°Internal 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(utypeuTYPE_ALTERNATIVESu alternativesuappendulenuchildrenutupleuparentuNoneu TYPE_GROUPugroupuget_characteristic_subpatternu token_labelsuNAMEuname(uselfunodeusubp((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu leaf_to_root!s8        uMinNode.leaf_to_rootcCs1x*|jƒD]}|jƒ}|r |Sq WdS(u‹Drives 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(uleavesu leaf_to_root(uselfulusubp((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyuget_linear_subpatternKs uMinNode.get_linear_subpatternccsEx-|jD]"}x|jƒD] }|VqWq W|jsA|VndS(u-Generator that returns the leaves of the treeN(uchildrenuleaves(uselfuchildux((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyuleaves`s   uMinNode.leavesN( u__name__u __module__u __qualname__u__doc__uNoneu__init__u__repr__u leaf_to_rootuget_linear_subpatternuleaves(u __locals__((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyuMinNodes   * uMinNodec Csd}|jtjkr(|jd}n|jtjkrŘt|jƒdkrht|jd|ƒ}q˙tdt ƒ}x…|jD]P}|jj |ƒdrŁqnt||ƒ}|dk r|jj |ƒqqWn'|jtj krxt|jƒdkr_tdt ƒ}x9|jD].}t||ƒ}|r|jj |ƒqqW|jsud}quq˙t|jd|ƒ}n‡|jtjkr˙t|jdtjƒrÍ|jdjdkrÍt|jd|ƒSt|jdtjƒrü|jdjdks=t|jƒdkrAt|jddƒrA|jdjdkrAdSd}d}d}d}d} d} x•|jD]Š}|jtjkr–d}|}n<|jtjkrˇd}|} n|jtjkrŇ|}nt|dƒro|jdkrod} qoqoW| rA|jd} t| dƒrN| jdkrN|jd } qNn |jd} | jtjkrĎ| jd krtdtƒ}qTtt| jƒrątdtt| jƒƒ}qTtdtt| jƒƒ}n…| jtjkr0| jjd ƒ} | tkrtdt| ƒ}qTtdtjd | ƒ}n$| jtjkrTt||ƒ}n|r›| jdjd kryd}q›| jdjdkr’q›t ‚n|r˙|dk r˙xL|jdd…D]4}t||ƒ}|dk rÁ|jj |ƒqÁqÁWq˙n|r||_!n|S(uĎ 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*). iiutypeiu(u[uvalueu=iuanyu'unameu*u+NTFi˙˙˙˙("uNoneutypeusymsuMatcheruchildrenu Alternativesulenu reduce_treeuMinNodeuTYPE_ALTERNATIVESuindexuappendu Alternativeu TYPE_GROUPuUnitu isinstanceupytreeuLeafuvalueuhasattruTrueuFalseuDetailsuRepeateru token_labelsuNAMEuTYPE_ANYugetattrupysymsuSTRINGustriputokensuNotImplementedErroruparent( unodeuparentunew_nodeuchildureduceduleafu details_nodeualternatives_nodeu has_repeateru repeater_nodeuhas_variable_nameu name_leafuname((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu reduce_treehsŚ             u reduce_treecs5t|tƒs|St|ƒdkr-|dSg}g}dddddg‰g}d‰x›|D]“}tt|d d „ƒƒratt|‡fd d †ƒƒrł|j|ƒqôtt|‡fd d †ƒƒrä|j|ƒqô|j|ƒqaqaW|r|}n|r|}n|r%|}nt|d tƒS(u„Picks the most characteristic from a list of linear patterns Current order used is: names > common_names > common_chars iiuinuforuifunotuNoneu[]().,:cSst|ƒtkS(N(utypeustr(ux((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu˙su/get_characteristic_subpattern..cst|tƒo|ˆkS(N(u isinstanceustr(ux(u common_chars(u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyuscst|tƒo|ˆkS(N(u isinstanceustr(ux(u common_names(u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyusukey(u isinstanceulistulenuanyurec_testuappendumax(u subpatternsusubpatterns_with_namesusubpatterns_with_common_namesusubpatterns_with_common_charsu subpattern((u common_charsu common_namesu6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyuget_characteristic_subpatternîs2      uget_characteristic_subpatternccsWxP|D]H}t|ttfƒrDx*t||ƒD] }|Vq2Wq||ƒVqWdS(uPTests test_func on all items of sequence and items of included sub-iterablesN(u isinstanceulistutupleurec_test(usequenceu test_funcuxuy((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyurec_tests   urec_testNi˙˙˙˙iţ˙˙˙iý˙˙˙(u__doc__uupytreeupgen2ugrammarutokenupygramupattern_symbolsupython_symbolsusymsupysymsuopmaputokensu token_labelsuTYPE_ANYuTYPE_ALTERNATIVESu TYPE_GROUPuobjectuMinNodeuNoneu reduce_treeuget_characteristic_subpatternurec_test(((u6/opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyus X† %