Edit File: btm_utils.cpython-37.pyc
B ����q}�c�&������������������@���s|���d�Z�ddlmZ�ddlmZmZ�ddlmZmZ�eZ eZ ejZeZ dZdZdZG�dd ��d e�Zddd�Zd d��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����6/opt/alt/python37/lib64/python3.7/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`|j�|��t|j�t|j�krTt|j�g}g�|_|j}q n|j}d}P�|j�tkr�|j �|��t|j �t|j�kr�t |j �}g�|_ |j}q n|j}d}P�|j�tjkr�|j r�|�|j ��n|�|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 |�����D�]}|���}|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�]}|���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}|�j� |�d�rnqXt||�}|dk rX|j� |��qXW��n|�j�tjk�rt|�j�dkr�ttd�}x(|�jD�]}t||�}|r�|j� |��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|j�d�}|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|j� |���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����(�[�valueTF�=�����any�')r���r����*�+r���)r����symsZMatcherr ���ZAlternativesr����reduce_treer ���r����indexr���ZAlternativer���ZUnit� isinstancer���ZLeafr0����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���r8���g���s����� r8���c����������������s����t�|�t�s|�S�t|��dkr"|�d�S�g�}g�}dddddg�g�}d��xl|�D�]d}tt|d d ����rFtt|��fdd ���r~|�|��qFtt|�fdd ���r�|�|��qF|�|��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>���������z/get_characteristic_subpattern.<locals>.<lambda>c����������������s���t�|�t�o|���kS�)N)r:���r���)rH���)�common_charsr���r���rI������rJ���c����������������s���t�|�t�o|���kS�)N)r:���r���)rH���)�common_namesr���r���rI�����rJ���)�key)r:����listr���r3����rec_testr����max)ZsubpatternsZsubpatterns_with_namesZsubpatterns_with_common_namesZsubpatterns_with_common_chars� subpatternr���)rK���rL���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)r:���rN���r���rO���)ZsequenceZ test_funcrH���r���r���r���rO�����s���� rO���)N)r+�����r���Zpgen2r���r���Zpygramr���r���r7���r>���ZopmaprA���r���r<���r���r����objectr ���r8���r���rO���r���r���r���r����<module>���s���W �%