Edit File: btm_utils.cpython-36.opt-1.pyc
3 � \�&������������������@���s|���d�Z�ddlmZ�ddlmZmZ�ddlmZmZ�eZ eZ ejZeZ dZdZdZG�dd��de�Zdd d�Zdd ��Zdd��Zd S�)z0Utility functions used by the btm_matcher module����)�pytree)�grammar�token)�pattern_symbols�python_symbols��������c���������������@���s:���e�Zd�ZdZd dd�Zdd��Zdd��Zd d ��Zdd��ZdS�)�MinNodez�This class serves as an intermediate representation of the pattern tree during the conversion to sets of leaf-to-root subpatternsNc�������������C���s.���||�_�||�_g�|�_d|�_d�|�_g�|�_g�|�_d�S�)NF)�type�name�children�leaf�parent�alternatives�group)�selfr ���r�����r����)/usr/lib64/python3.6/lib2to3/btm_utils.py�__init__���s����zMinNode.__init__c�������������C���s���t�|�j�d�t�|�j��S�)N� )�strr ���r���)r���r���r���r����__repr__���s����zMinNode.__repr__c�������������C���s����|�}g�}x�|r�|j�tkr`|jj|��t|j�t|j�krTt|j�g}g�|_|j}q n|j}d}P�|j�tkr�|j j|��t|j �t|j�kr�t |j �}g�|_ |j}q n|j}d}P�|j�tjkr�|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�subpr���r���r����leaf_to_root!���s8���� zMinNode.leaf_to_rootc�������������C���s&���x |�j���D�]}|j��}|r |S�q W�dS�)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!���r���r���r����get_linear_subpatternK���s����zMinNode.get_linear_subpatternc�������������c���s.���x|�j�D�]}|j��E�dH��qW�|�j�s*|�V��dS�)z-Generator that returns the leaves of the treeN)r���r#���)r����childr���r���r���r#���`���s����zMinNode.leaves)NN) �__name__� __module__�__qualname__�__doc__r���r���r"���r%���r#���r���r���r���r���r ������s��� *r ���Nc������� ������C���s���d}|�j�tjkr|�jd�}�|�j�tjkr�t|�j�dkrFt|�jd�|�}nJttd�}x>|�jD�]4}|�jj |�d�rnqXt||�}|dk rX|jj |��qXW��n|�j�tjk�rt|�j�dkr�ttd�}x(|�jD�]}t||�}|r�|jj |��q�W�|js�d}nt|�jd�|�}�n�|�j�tj k�r�t|�jd�tj��rH|�jd�jdk�rHt|�jd�|�S�t|�jd�tj��rn|�jd�jdk�s�t|�j�dk�r�t|�jd�d��r�|�jd�jdk�r�dS�d }d}d}d }d} d } xn|�jD�]d}|j�tjk�r�d }|}n*|j�tjk�r�d }|} n|j�tjk�r|}t|d��r�|jdk�r�d } �q�W�| �rb|�jd�}t|d��rl|jdk�rl|�jd�}n |�jd�}|j�tjk�r�|jd k�r�ttd�}n4tt|j��r�ttt|j�d�}nttt|j�d�}n\|j�tjk�r|jjd�}|tk�r�tt|�d�}nttj|d�}n|j�tjk�r$t||�}|�rZ| jd�jdk�rBd}n| jd�jdk�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���ZAlternativesr����reduce_treer ���r����indexr���ZAlternativer���ZUnit� isinstancer���ZLeafr.����hasattrZDetailsZRepeaterr���r����TYPE_ANY�getattr�pysyms�STRING�strip�tokens�NotImplementedErrorr���) r ���r���Znew_noder&���Zreducedr ���Zdetails_nodeZalternatives_nodeZhas_repeaterZ repeater_nodeZhas_variable_nameZ name_leafr���r���r���r���r6���g���s����� r6���c����������������s����t�|�t�s|�S�t|��dkr"|�d�S�g�}g�}dddddg�g�}d��xl|�D�]d}tt|d d ����rFtt|��fdd ���r~|j|��qFtt|�fdd ���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 r���r+����in�for�if�not�Nonez[]().,:c�������������S���s���t�|��tkS�)N)r ���r���)�xr���r���r����<lambda>����s����z/get_characteristic_subpattern.<locals>.<lambda>c����������������s���t�|�t�o|���kS�)N)r8���r���)rF���)�common_charsr���r���rG������s����c����������������s���t�|�t�o|���kS�)N)r8���r���)rF���)�common_namesr���r���rG�����s����)�key)r8����listr���r0����rec_testr����max)ZsubpatternsZsubpatterns_with_namesZsubpatterns_with_common_namesZsubpatterns_with_common_chars� subpatternr���)rH���rI���r���r�������s2���� r���c�������������c���s<���x6|�D�].}t�|ttf�r*t||�E�dH��q||�V��qW�dS�)zPTests test_func on all items of sequence and items of included sub-iterablesN)r8���rK���r���rL���)ZsequenceZ test_funcrF���r���r���r���rL�����s���� rL���r4���������)N)r*�����r���Zpgen2r���r���Zpygramr���r���r5���r<���Zopmapr?���r���r:���r���r����objectr ���r6���r���rL���r���r���r���r����<module>���s���W �%