Your IP : 3.22.79.165


Current Path : /opt/alt/python35/lib64/python3.5/lib2to3/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/lib2to3/__pycache__/btm_utils.cpython-35.opt-1.pyc



��Yf�&�@s�dZddlmZddlmZmZddlmZmZeZ	eZ
ejZeZ
dZdZdZGdd�de�Zd	d
d�Zdd
�Zdd�Zd	S)z0Utility functions used by the btm_matcher module�)�pytree)�grammar�token)�pattern_symbols�python_symbols��c@sXeZdZdZdddd�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
    subpatternsNcCsC||_||_g|_d|_d|_g|_g|_dS)NF)�type�name�children�leaf�parent�alternatives�group)�selfr
r�r�6/opt/alt/python35/lib64/python3.5/lib2to3/btm_utils.py�__init__s						zMinNode.__init__cCst|j�dt|j�S)N� )�strr
r)rrrr�__repr__szMinNode.__repr__cCsO|}g}x<|rJ|jtkr�|jj|�t|j�t|j�kr|t|j�g}g|_|j}qn|j}d}P|jtkr|j	j|�t|j	�t|j�kr�t
|j	�}g|_	|j}qn|j}d}P|jtjkr.|j
r.|j|j
�n|j|j�|j}qW|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_rootcCs1x*|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_subpatternKszMinNode.get_linear_subpatternccs5x |jD]}|j�EdHq
W|js1|VdS)z-Generator that returns the leaves of the treeN)rr#)r�childrrrr#`s	zMinNode.leaves)	�__name__�
__module__�__qualname__�__doc__rrr"r%r#rrrrr	s	*r	Nc
Cs�d}|jtjkr%|jd}|jtjkr�t|j�dkret|jd|�}q�tdt�}xa|jD]J}|jj	|�dr�q~t||�}|dk	r~|jj
|�q~Wn	|jtjkrit|j�dkrPtdt�}x6|jD]+}t||�}|r|jj
|�qW|jsfd}q�t|jd|�}no|jtj
kr�t|jdtj�r�|jdjdkr�t|jd|�St|jdtj�r�|jdjdks.t|j�dkr2t|jdd�r2|jdjdkr2dSd	}d}d}d
}d}	d
}
x�|jD]�}|jtjkr�d
}|}n9|jtjkr�d	}|}	n|jtjkr�|}t|d�r`|jdkr`d	}
q`W|
r)|jd}t|d�r6|jdkr6|jd}n
|jd}|jtjkr�|jd
kritdt�}q9tt|j�r�tdtt|j��}q9tdtt|j��}n�|jtjkr|jjd�}|tkr�tdt|�}q9tdtjd|�}n!|jtjkr9t||�}|r}|	jdjdkr^d}n|	jdjdkrwnt�|r�|dk	r�xF|jdd�D]1}t||�}|dk	r�|jj
|�q�W|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�rr
r�(�[�valueTF�=r�any�'r�*�+���)r
�symsZMatcherrZAlternativesr�reduce_treer	r�indexrZAlternativerZUnit�
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_leafrrrrr6gs�
				


		r6cs/t|t�s|St|�dkr-|dSg}g}dddddg�g}d�x�|D]�}tt|d	d
���ratt|�fdd
���r�|j|�qatt|�fdd
���r�|j|�qa|j|�qaW|r|}n|r|}n|r|}t|d
t�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[]().,:cSst|�tkS)N)r
r)�xrrr�<lambda>�sz/get_characteristic_subpattern.<locals>.<lambda>cst|t�o|�kS)N)r8r)rF)�common_charsrrrGscst|t�o|�kS)N)r8r)rF)�common_namesrrrGs�key)r8�listrr0�rec_testr�max)ZsubpatternsZsubpatterns_with_namesZsubpatterns_with_common_namesZsubpatterns_with_common_chars�
subpatternr)rHrIrr�s2
				rccsJxC|D];}t|ttf�r7t||�EdHq||�VqWdS)zPTests test_func on all items of sequence and items of included
    sub-iterablesN)r8rKrrL)ZsequenceZ	test_funcrFrrrrLs
rLr4������)r*�rZpgen2rrZpygramrrr5r<Zopmapr?rr:rr�objectr	r6rrLrrrr�<module>s	W�%

?>