B
    v9a&                 @   sx   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 )   )pytree)grammartoken)pattern_symbolspython_symbolsc               @   s6   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdS )MinNodeNc             C   s.   || _ || _g | _d| _d | _g | _g | _d S )NF)typenamechildrenleafparentalternativesgroup)selfr   r    r   /usr/lib/python3.7/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| t|jt|jkrTt|jg}g |_|j}q
n|j}d }P |j tkr|j	| t|j	t|jkrt
|j	}g |_	|j}q
n|j}d }P |j tjkr|jr||j n||j  |j}q
W |S )N)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 |   D ]}| }|r
|S q
W d S )N)leavesr#   )r   lr"   r   r   r   get_linear_subpatternK   s    zMinNode.get_linear_subpatternc             c   s.   x| j D ]}| E d H  qW | j s*| V  d S )N)r   r$   )r   childr   r   r   r$   `   s    zMinNode.leaves)NN)__name__
__module____qualname__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 |}nJttd}x>| jD ]4}| j	|d rnqXt||}|d k	rX|j
| qXW n| j tjkrt| jdkrttd}x(| jD ]}t||}|r|j
| qW |jsd }nt| jd |}n| j tjkrt| 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st| jdkrt| jd dr| jd jdkrd S d}d }d }d	}d }	d	}
xn| 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}
qW |
rb| jd }t|drl|jdkrl| 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krt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	rx8|jdd D ]&}t||}|d k	rz|j
| qzW |r||_|S )N       )r   r   ([valueTF=   any')r   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   r7   g   s    











r7   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~|| qFtt|fdd
r|| qF|| qFW |r|} n|r|} n|r|} t| tdS )Nr   r+   inforifnotNonez[]().,:c             S   s   t | tkS )N)r   r   )xr   r   r   <lambda>       z/get_characteristic_subpattern.<locals>.<lambda>c                s   t | to|  kS )N)r9   r   )rG   )common_charsr   r   rH      rI   c                s   t | to|  kS )N)r9   r   )rG   )common_namesr   r   rH     rI   )key)r9   listr   r2   rec_testr   max)ZsubpatternsZsubpatterns_with_namesZsubpatterns_with_common_namesZsubpatterns_with_common_chars
subpatternr   )rJ   rK   r   r      s2    

r   c             c   s<   x6| D ].}t |ttfr*t||E d H  q||V  qW d S )N)r9   rM   r   rN   )ZsequenceZ	test_funcrG   r   r   r   rN     s    
rN   )N) r   Zpgen2r   r   Zpygramr   r   r6   r=   Zopmapr@   r   r;   r   r   objectr
   r7   r   rN   r   r   r   r   <module>   s   W
 %