509318396
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!DOCTYPE html>
<html>
3

  \î&  ã               @   s|   d Z ddlmZ ddlmZmZ ddlmZmZ eZ	eZ
ejZeZdZdZdZG dd„ deƒZdd
d„Zdd„ Zdd„ Zd	S )z0Utility functions used by the btm_matcher moduleé   )Úpytree)ÚgrammarÚtoken)Úpattern_symbolsÚpython_symbolsé   é   c               @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Ú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    zMinNode.__init__c             C   s   t | jƒd t | jƒ S )Nú )Ústrr
   r   )r   r   r   r   Ú__repr__   s    zMinNode.__repr__c             C   sê   | }g }xÜ|rä|j tkr`|jj|ƒ t|jƒt|jƒkrTt|jƒg}g |_|j}q
n|j}d}P |j tkr°|j	j|ƒ t|j	ƒt|jƒkr¤t
|j	ƒ}g |_	|j}q
n|j}d}P |j tjkrÐ|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    



zMinNode.leaf_to_rootc             C   s&   x | j ƒ D ]}|jƒ }|r
|S q
W dS )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    zMinNode.get_linear_subpatternc             c   s.   x| j D ]}|jƒ E dH  qW | j s*| V  dS )z-Generator that returns the leaves of the treeN)r   r#   )r   Úchildr   r   r   r#   `   s    zMinNode.leaves)NN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r"   r%   r#   r   r   r   r   r	      s   
	*r	   Nc             C   s´  d}| j tjkr| jd } | j tjkr”t| jƒdkrFt| jd |ƒ}nJttd�}x>| jD ]4}| jj	|ƒd rnqXt||ƒ}|dk	rX|jj
|ƒ qXW �n| j tjk�rt| jƒdkrðttd�}x(| jD ]}t||ƒ}|rÂ|jj
|ƒ qÂW |jsîd}nt| jd |ƒ}�n | j tjk�r¤t| jd tjƒ�rH| jd jdk�rHt| jd |ƒS t| jd tjƒ�rn| jd jdk�s¢t| jƒdk�r¦t| jd dƒ�r¦| jd jdk�r¦dS d	}d}d}d
}d}	d
}
xn| jD ]d}|j tjk�râd
}|}n*|j tjk�rúd	}|}	n|j tjk�r|}t|dƒ�rÆ|jdk�rÆd	}
�qÆW |
�rb| jd }t|dƒ�rl|jdk�rl| jd }n
| jd }|j tjk�rÈ|jdk�r’ttd�}n4tt|jƒ�r´ttt|jƒd�}nttt|jƒd�}n\|j tjk�r|jjdƒ}|tk�rütt| d�}nttj|d�}n|j tjk�r$t||ƒ}|�rZ|	jd jdk�rBd}n|	jd jdk�rVnt‚|�r¤|dk	�r¤x8|jdd… D ]&}t||ƒ}|dk	�rz|j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
   r   ú(ú[ÚvalueTFú=r   Úanyú')r
   r   Ú*ú+éÿÿÿÿ)r
   ÚsymsZMatcherr   ZAlternativesr   Úreduce_treer	   r   Úindexr   ZAlternativer   ZUnitÚ
isinstancer   ZLeafr.   ÚhasattrZDetailsZRepeaterr   r   ÚTYPE_ANYÚgetattrÚpysymsÚSTRINGÚstripÚtokensÚNotImplementedErrorr   )r    r   Znew_noder&   Zreducedr   Zdetails_nodeZalternatives_nodeZhas_repeaterZrepeater_nodeZhas_variable_nameZ	name_leafr   r   r   r   r6   g   s¤    











r6   c                sÖ   t | tƒs| S t| ƒdkr"| d S g }g }dddddg‰g }d‰ xl| D ]d}tt|d	d
„ ƒƒrFtt|‡ fdd
„ƒƒr~|j|ƒ qFtt|‡fdd
„ƒƒr |j|ƒ qF|j|ƒ qFW |r¸|} n|rÂ|} n|rÊ|} t| td�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 | ƒtkS )N)r
   r   )Úxr   r   r   Ú<lambda>þ   s    z/get_characteristic_subpattern.<locals>.<lambda>c                s   t | tƒo| ˆ kS )N)r8   r   )rF   )Úcommon_charsr   r   rG      s    c                s   t | tƒo| ˆ kS )N)r8   r   )rF   )Úcommon_namesr   r   rG     s    )Úkey)r8   Úlistr   r0   Úrec_testr   Úmax)ZsubpatternsZsubpatterns_with_namesZsubpatterns_with_common_namesZsubpatterns_with_common_charsÚ
subpatternr   )rH   rI   r   r   í   s2    

r   c             c   s<   x6| D ].}t |ttfƒr*t||ƒE dH  q||ƒV  qW dS )zPTests test_func on all items of sequence and items of included
    sub-iterablesN)r8   rK   r   rL   )ZsequenceZ	test_funcrF   r   r   r   rL     s    
rL   r4   éþÿÿÿéýÿÿÿ)N)r*   Ú r   Zpgen2r   r   Zpygramr   r   r5   r<   Zopmapr?   r   r:   r   r   Úobjectr	   r6   r   rL   r   r   r   r   Ú<module>   s   W
 %