o ?Og&@s|dZddlmZddlmZmZddlmZmZeZ eZ ej Z eZ dZdZdZGdd d eZdd d Zd dZddZd S)z0Utility functions used by the btm_matcher module)pytree)grammartoken)pattern_symbolspython_symbolsc@s:eZdZdZd ddZddZddZd d Zd d ZdS)MinNodezThis class serves as an intermediate representation of the pattern tree during the conversion to sets of leaf-to-root subpatternsNcCs.||_||_g|_d|_d|_g|_g|_dS)NF)typenamechildrenleafparent alternativesgroup)selfr r r8/opt/alt/python310/lib64/python3.10/lib2to3/btm_utils.py__init__s zMinNode.__init__cCst|jdt|jS)N )strr r )rrrr__repr__szMinNode.__repr__cCs|}g}|rt|jtkr0|j|t|jt|jkr(t|jg}g|_|j}q|j}d} |S|jtkrY|j |t|j t|jkrQt |j }g|_ |j}q|j}d} |S|jt j kri|j ri||j n||j|j}|s|S)zInternal 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)r TYPE_ALTERNATIVESrappendlenr tupler TYPE_GROUPrget_characteristic_subpattern token_labelsNAMEr )rnodesubprrr leaf_to_root!s>       "zMinNode.leaf_to_rootcCs&|D] }|}|r|SqdS)aDrives 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)leavesr#)rlr"rrrget_linear_subpatternKs zMinNode.get_linear_subpatternccs0|jD] }|EdHq|js|VdSdS)z-Generator that returns the leaves of the treeN)r r$)rchildrrrr$`s   zMinNode.leaves)NN) __name__ __module__ __qualname____doc__rrr#r&r$rrrrr s  * r Nc Csd}|jtjkr |jd}|jtjkrIt|jdkr$t|jd|}nttd}|jD]}|j |dr7q,t||}|durF|j |q,nw|jtj krt|jdkrutt d}|jD]}t||}|rm|j |q^|jssd}nKt|jd|}nA|jtj krt|jdtjr|jdjdkrt|jd|St|jdtjr|jdjdkst|jdkrt|jddr|jdjdkrdSd }d}d}d }d} d } |jD].}|jtjkrd }|}n|jtjkrd }|} n|jtjkr|}t|dr|jd krd } q| r!|jd} t| dr | jdkr |jd } n|jd} | jtjkrT| jd kr9ttd}nItt| jrJttt| jd}n8ttt| jd}n.| jtjkrv| jd} | tvrntt| d}nttj| d}n | jtjkrt||}|r| jdjdkrd}n | jdjdkrnt|r|dur|jddD]}t||}|dur|j |q|r||_|S)z 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*). N)r r([valueTF=any')r r *+r)r symsZMatcherr Z Alternativesr reduce_treer rindexrZ AlternativerZUnit isinstancerZLeafr0hasattrZDetailsZRepeaterrr TYPE_ANYgetattrpysymsSTRINGstriptokensNotImplementedErrorr) r!rZnew_noder'ZreducedrZ details_nodeZalternatives_nodeZ has_repeaterZ repeater_nodeZhas_variable_nameZ name_leafr rrrr8gs                           r8cst|ts|St|dkr|dSg}g}gdg}d|D]2}tt|ddrQtt|fddr;||qtt|fddrL||q||q|rW|}n |r\|}n|r`|}t|td S) zPicks the most characteristic from a list of linear patterns Current order used is: names > common_names > common_chars rr,)inforifnotNonez[]().,:cSs t|tuSN)r rxrrrs z/get_characteristic_subpattern..ct|to|vSrHr:rrI) common_charsrrrKcrLrHrMrI) common_namesrrrKrO)key)r:listrr3rec_testrmax)Z subpatternsZsubpatterns_with_namesZsubpatterns_with_common_namesZsubpatterns_with_common_chars subpatternr)rNrPrrs8        rccs:|D]}t|ttfrt||EdHq||VqdS)zPTests test_func on all items of sequence and items of included sub-iterablesN)r:rRrrS)ZsequenceZ test_funcrJrrrrSs  rSrH)r+rZpgen2rrZpygramrrr7r>ZopmaprArr<rrobjectr r8rrSrrrrs   W %