Your IP : 18.220.136.182
�
i f�&�@s�dZddlmZddlmZmZddlmZmZeZ eZ
ejZeZ
dZdZdZGdd�de�Zd d
d�Zdd
�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/python34/lib64/python3.4/lib2to3/btm_utils.py�__init__s zMinNode.__init__cCst|j�dt|j�S)N� )�strr
r)rrrr�__repr__szMinNode.__repr__cCsU|}g}xB|rP|jtkr�|jj|�t|j�t|j�kr|t|j�g}g|_|j}qq�|j}d}Pn|jtkr|j j|�t|j �t|j�kr�t
|j �}g|_ |j}qq|j}d}Pn|jtjkr4|j
r4|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_subpatternKszMinNode.get_linear_subpatternccs8x |jD]}|j�DdHq
W|js4|VndS)z-Generator that returns the leaves of the treeN)rr#)r�childrrrr#`s zMinNode.leaves) �__name__�
__module__�__qualname__�__doc__rrr"r%r#rrrrr s *r Nc
Csd}|jtjkr(|jd}n|jtjkr�t|j�dkrht|jd|�}q�tdt�}x�|jD]P}|jj |�dr�q�nt||�}|dk r�|jj
|�q�q�Wn'|jtjkrxt|j�dkr_tdt�}x9|jD].}t||�}|r|jj
|�qqW|jsud}quq�t|jd|�}n�|jtj
kr�t|jdtj�r�|jdjdkr�t|jd|�St|jdtj�r�|jdjdks=t|j�dkrAt|jdd�rA|jdjdkrAdSd }d}d}d
}d} d
}
x�|jD]�}|jtjkr�d
}|}n<|jtjkr�d }|} n|jtjkr�|}nt|d�ro|jdkrod }
qoqoW|
rA|jd}t|d�rN|jdkrN|jd}qNn
|jd}|jtjkr�|jd
kr�tdt�}qTtt|j�r�tdtt|j��}qTtdtt|j��}n�|jtjkr0|jjd�}|tkrtdt|�}qTtdtjd|�}n$|jtjkrTt||�}n|r�| jdjdkryd}q�| jdjdkr�q�t�n|r�|dk r�xL|jdd�D]4}t||�}|dk r�|jj
|�q�q�Wq�n|r||_n|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
�symsZMatcherrZAlternativesr�reduce_treer r�indexrZAlternativerZUnit�
isinstancerZLeafr.�hasattrZDetailsZRepeaterrr�TYPE_ANY�getattr�pysyms�STRING�strip�tokens�NotImplementedErrorr)
r rZnew_noder&Zreducedr
Zdetails_nodeZalternatives_nodeZhas_repeaterZ
repeater_nodeZhas_variable_nameZ name_leafrrrrr6gs�
r6cs5t|t�s|St|�dkr-|dSg}g}dddddg�g}d�x�|D]�}tt|d d
���ratt|�fdd
���r�|j|�q�tt|�fdd
���r�|j|�q�|j|�qaqaW|r|}n|r|}n|r%|}nt|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||�DdHq||�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�%
?>