o
    Š�bcî&  ã                   @   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/lib/python3.10/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 }|rt|j tkr0|j |¡ t|jƒt|jƒkr(t|jƒg}g |_|j}q|j}d}	 |S |j tkrY|j	 |¡ t|j	ƒt|jƒkrQt
|j	ƒ}g |_	|j}q|j}d}	 |S |j tjkri|jri| |j¡ n| |j ¡ |j}|s|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!   s>   

ì
	ùß"zMinNode.leaf_to_rootc                 C   s&   |   ¡ D ]}| ¡ }|r|  S q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   s0   � | j D ]	}| ¡ E dH  q| j s| V  dS 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rIt| jƒdkr$t| jd |ƒ}�nœttd�}| jD ]}| j 	|¡d r7q,t||ƒ}|durF|j 
|¡ q,�nw| j tjkrt| jƒdkruttd�}| jD ]}t||ƒ}|rm|j 
|¡ q^|jssd}�nKt| jd |ƒ}�nA| j tjk�rÀt| jd tjƒrŸ| jd jdkrŸt| jd |ƒS t| jd tjƒr°| 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
}
| j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Ø|
�r!| jd }t|dƒ�r |jdk�r | jd }n| jd }|j tjk�rT|jdk�r9ttd�}nItt|jƒ�rJttt|jƒd�}n8ttt|jƒd�}n.|j tjk�rv|j d¡}|tv �rntt| d�}nttj|d�}n|j tjk�r‚t||ƒ}|�r�|	jd jdk�r‘d}n|	jd jdk�r›nt‚|�rÀ|du�rÀ|jdd… D ]}t||ƒ}|du�r¾|j 
|¡ �q¬|�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   ÚsymsÚMatcherr   ÚAlternativesr   Úreduce_treer
   r   Úindexr   ÚAlternativer   ÚUnitÚ
isinstancer   ÚLeafr0   ÚhasattrÚDetailsÚRepeaterr   r    ÚTYPE_ANYÚgetattrÚpysymsÚSTRINGÚstripÚtokensÚNotImplementedErrorr   )r!   r   Únew_noder'   Úreducedr   Údetails_nodeÚalternatives_nodeÚhas_repeaterÚrepeater_nodeÚhas_variable_nameÚ	name_leafr   r   r   r   r:   g   s´   



€û


€€ÿ
€

€




€r:   c                    sÌ   t | tƒs| S t| ƒdkr| d S g }g }g d¢‰g }d‰ | D ]2}tt|dd„ ƒƒrQtt|‡ fdd„ƒƒr;| |¡ qtt|‡fdd„ƒƒrL| |¡ q| |¡ q|rW|} 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u S ©N)r   r   ©Úxr   r   r   Ú<lambda>þ   s    z/get_characteristic_subpattern.<locals>.<lambda>c                    ó   t | tƒo| ˆ v S rW   ©r>   r   rX   )Úcommon_charsr   r   rZ      ó    c                    r[   rW   r\   rX   )Úcommon_namesr   r   rZ     r^   )Úkey)r>   Úlistr   r3   Úrec_testr   Úmax)ÚsubpatternsÚsubpatterns_with_namesÚsubpatterns_with_common_namesÚsubpatterns_with_common_charsÚ
subpatternr   )r]   r_   r   r   í   s8   

ÿ
ÿ
€r   c                 c   s:   � | D ]}t |ttfƒrt||ƒE dH  q||ƒV  qdS )zPTests test_func on all items of sequence and items of included
    sub-iterablesN)r>   ra   r   rb   )ÚsequenceÚ	test_funcrY   r   r   r   rb     s   €ürb   rW   )r+   Ú r   Úpgen2r   r   Úpygramr   r   r7   rE   ÚopmaprH   r   rC   r   r   Úobjectr
   r:   r   rb   r   r   r   r   Ú<module>   s     
W %