grthtrhthjhtyjytjytkergtrhtrjytjerhrfh3 \î&ã@s|dZddlmZddlmZmZddlmZmZeZ eZ ej Z eZ dZdZdZGdd„deƒZdd d „Zd d „Zdd„Zd S)z0Utility functions used by the btm_matcher moduleé)Úpytree)ÚgrammarÚtoken)Úpattern_symbolsÚpython_symbolsééc@s:eZdZdZd dd„Zdd„Zdd„Zd d „Zd d „ZdS)ÚMinNodez‰This 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)ÚtypeÚnameÚchildrenÚleafÚparentÚ alternativesÚgroup)Úselfr r ©rú)/usr/lib64/python3.6/lib2to3/btm_utils.pyÚ__init__szMinNode.__init__cCst|jƒdt|jƒS)Nú )Ústrr r )rrrrÚ__repr__szMinNode.__repr__cCsê|}g}xÜ|rä|jtkr`|jj|ƒt|jƒt|jƒkrTt|jƒg}g|_|j}q n |j}d}P|jtkr°|j j|ƒt|j ƒt|jƒkr¤t |j ƒ}g|_ |j}q n |j}d}P|jt j krÐ|j rÐ|j|j ƒn |j|jƒ|j}q W|S)z°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)r ÚTYPE_ALTERNATIVESrÚappendÚlenr ÚtuplerÚ TYPE_GROUPrÚget_characteristic_subpatternÚ token_labelsÚNAMEr )rÚnodeÚsubprrrÚ leaf_to_root!s8        zMinNode.leaf_to_rootcCs&x |jƒD]}|jƒ}|r |Sq WdS)a‹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)Úleavesr")rÚlr!rrrÚget_linear_subpatternKszMinNode.get_linear_subpatternccs.x|jD]}|jƒEdHqW|js*|VdS)z-Generator that returns the leaves of the treeN)r r#)rÚchildrrrr#`s zMinNode.leaves)NN) Ú__name__Ú __module__Ú __qualname__Ú__doc__rrr"r%r#rrrrr s  *r Nc Cs´d}|jtjkr|jd}|jtjkr”t|jƒdkrFt|jd|ƒ}nJttd�}x>|jD]4}|jj |ƒdrnqXt||ƒ}|dk rX|jj |ƒqXW�n|jtj k�rt|jƒdkrðtt d�}x(|jD]}t||ƒ}|rÂ|jj |ƒqÂW|jsîd}nt|jd|ƒ}�n |jtj k�r¤t|jdtjƒ�rH|jdjdk�rHt|jd|ƒSt|jdtjƒ�rn|jdjdk�s¢t|jƒdk�r¦t|jddƒ�r¦|jdjdk�r¦dSd }d}d}d }d} d } xn|jD]d}|jtjk�râd }|}n*|jtjk�rúd }|} n|jtjk�r |}t|dƒ�rÆ|jd k�rÆd } �qÆW| �rb|jd} t| dƒ�rl| jdk�rl|jd } n |jd} | jtjk�rÈ| jd k�r’ttd�}n4tt| jƒ�r´ttt| jƒd�}nttt| jƒd�}n\| jtjk�r | jjdƒ} | tk�rütt| d�}nttj| d�}n| jtjk�r$t||ƒ}|�rZ| jdjdk�rBd}n| jdjdk�rVnt‚|�r¤|dk �r¤x8|jdd…D]&}t||ƒ}|dk �rz|jj |ƒ�qzW|�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 rú(ú[ÚvalueTFú=rÚanyú')r r Ú*ú+éÿÿÿÿ)r ÚsymsZMatcherr Z AlternativesrÚ reduce_treer rÚindexrZ AlternativerZUnitÚ isinstancerZLeafr.ÚhasattrZDetailsZRepeaterrrÚTYPE_ANYÚgetattrÚpysymsÚSTRINGÚstripÚtokensÚNotImplementedErrorr) r rZnew_noder&Zreducedr Z details_nodeZalternatives_nodeZ has_repeaterZ repeater_nodeZhas_variable_nameZ name_leafr rrrr6gs¤                     r6csÖt|tƒs|St|ƒdkr"|dSg}g}dddddg‰g}d‰xl|D]d}tt|d d „ƒƒrFtt|‡fd d „ƒƒr~|j|ƒqFtt|‡fd d „ƒƒr |j|ƒqF|j|ƒqFW|r¸|}n|rÂ|}n|rÊ|}t|td �S)z„Picks the most characteristic from a list of linear patterns Current order used is: names > common_names > common_chars rr+ÚinÚforÚifÚnotÚNonez[]().,:cSs t|ƒtkS)N)r r)ÚxrrrÚþsz/get_characteristic_subpattern..cst|tƒo|ˆkS)N)r8r)rF)Ú common_charsrrrGscst|tƒo|ˆkS)N)r8r)rF)Ú common_namesrrrGs)Úkey)r8Úlistrr0Úrec_testrÚmax)Z subpatternsZsubpatterns_with_namesZsubpatterns_with_common_namesZsubpatterns_with_common_charsÚ subpatternr)rHrIrrís2     rccs<x6|D].}t|ttfƒr*t||ƒEdHq||ƒVqWdS)zPTests test_func on all items of sequence and items of included sub-iterablesN)r8rKrrL)ZsequenceZ test_funcrFrrrrLs rLr4éþÿÿÿéýÿÿÿ)N)r*ÚrZpgen2rrZpygramrrr5r<Zopmapr?rr:rrÚobjectr r6rrLrrrrÚs W %