a
    If&                     @   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_symbolsc                   @   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 )MinNodezThis 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   "lib/python3.9/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 }|r|j tkr^|j| t|jt|jkrRt|jg}g |_|j}qn|j}d}q|j tkr|j	| t|j	t|jkrt
|j	}g |_	|j}qn|j}d}q|j tjkr|jr||j n||j  |j}q|S )zInternal 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&   |   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   s*   | j D ]}| E dH  q| 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rt| jdkrFt| jd |}nFttd}| jD ]4}| j	|d rlqVt||}|durV|j
| qVn| j tjkrt| jdkrttd}| jD ]}t||}|r|j
| q|jsd}nt| jd |}n| j tjkrt| jd tjr>| jd jdkr>t| jd |S t| jd tjrd| jd jdkst| jdkrt| jd dr| jd jdkrdS d	}d}d}d
}d}	d
}
| jD ]d}|j tjkrd
}|}n*|j tjkrd	}|}	n|j tjkr |}t|dr|jdkrd	}
q|
rT| jd }t|dr^|jdkr^| jd }n
| jd }|j tjkr|jdkrttd}n4tt|jrttt|jd}nttt|jd}n\|j tjkr|jd}|tv rtt| d}nttj|d}n|j tjkrt||}|rL|	jd jdkr4d}n|	jd jdkrHnt|r|dur|jdd D ]&}t||}|durj|j
| qj|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   symsZMatcherr   ZAlternativesr   reduce_treer
   r   indexr   ZAlternativer   ZUnit
isinstancer   ZLeafr0   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   r8   g   s    













r8   c                    s   t | ts| S t| dkr"| d S g }g }g dg }d | D ]d}tt|dd r>tt| fddrv|| q>tt|fddr|| q>|| q>|r|} n|r|} n|r|} t| td	S )
zPicks 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>       z/get_characteristic_subpattern.<locals>.<lambda>c                    s   t | to|  v S rH   r:   r   rI   )common_charsr   r   rK      rL   c                    s   t | to|  v S rH   rM   rI   )common_namesr   r   rK     rL   )key)r:   listr   r3   rec_testr   max)ZsubpatternsZsubpatterns_with_namesZsubpatterns_with_common_namesZsubpatterns_with_common_chars
subpatternr   )rN   rO   r   r      s6    


r   c                 c   s8   | 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:   rQ   r   rR   )ZsequenceZ	test_funcrJ   r   r   r   rR     s    rR   )N)r+    r   Zpgen2r   r   Zpygramr   r   r7   r>   ZopmaprA   r   r<   r   r   objectr
   r8   r   rR   r   r   r   r   <module>   s   W
 %