
    d&                         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	e          ZddZd Zd Zd
S )z0Utility functions used by the btm_matcher module   )pytree)grammartoken)pattern_symbolspython_symbolsc                   2    e Zd ZdZddZd Zd Zd Z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                 h    || _         || _        g | _        d| _        d | _        g | _        g | _        d S )NF)typenamechildrenleafparentalternativesgroup)selfr   r   s      #  /croot/python-split_1694437901252/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac/lib/python3.11/lib2to3/btm_utils.py__init__zMinNode.__init__   s8    			


    c                 Z    t          | j                  dz   t          | j                  z   S )N )strr   r   )r   s    r   __repr__zMinNode.__repr__   s"    49~~#c$)nn44r   c                    | }g }|r^|j         t          k    rr|j                            |           t	          |j                  t	          |j                  k    r$t          |j                  g}g |_        |j        }{|j        }d}n|j         t          k    rq|j	                            |           t	          |j	                  t	          |j                  k    r#t          |j	                  }g |_	        |j        }|j        }d}n[|j         t          j        k    r"|j        r|                    |j                   n|                    |j                    |j        }|^|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subps      r   leaf_to_rootzMinNode.leaf_to_root!   sZ     !	y---!((...t())S-?-???!$"3445D(*D%;D;DDyJ&&
!!$'''tz??c$-&8&8888DDD!#DJ;D;DDyL---$)-DI&&&&DI&&&;DC  !	D r   c                 h    |                                  D ]}|                                }|r|c S 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'   s      r   get_linear_subpatternzMinNode.get_linear_subpatternK   sJ       	 	A>>##D 	 	r   c              #   l   K   | j         D ]}|                                E d{V  | j         s| V  dS dS )z-Generator that returns the leaves of the treeN)r   r*   )r   childs     r   r*   zMinNode.leaves`   s[      ] 	& 	&E||~~%%%%%%%%} 	JJJJJ	 	r   )NN)	__name__
__module____qualname____doc__r   r   r(   r,   r*    r   r   r   r      so            5 5 5( ( (T  *    r   r   Nc                 	   d}| j         t          j        k    r| j        d         } | j         t          j        k    rt          | j                  dk    rt          | j        d         |          }nst          t                    }| j        D ]L}| j        	                    |          dz  r t          ||          }||j        
                    |           Mn| j         t          j        k    rt          | j                  dk    rVt          t                    }| j        D ].}t          ||          }|r|j        
                    |           /|j        sd}nt          | j        d         |          }nh| j         t          j        k    rRt          | j        d         t          j                  r1| j        d         j        dk    rt          | j        d         |          S t          | j        d         t          j                  r| j        d         j        dk    sIt          | j                  dk    r3t%          | j        d         d          r| j        d         j        dk    rdS d	}d}d}d
}d}	d
}
| j        D ]j}|j         t          j        k    rd
}|}n1|j         t          j        k    rd	}|}	n|j         t          j        k    r|}t%          |d          r|j        dk    rd	}
k|
r6| j        d         }t%          |d          r|j        dk    r| j        d         }n| j        d         }|j         t*          j        k    r|j        dk    rt          t.                    }nt%          t*          |j                  r)t          t1          t*          |j                            }nt          t1          t2          |j                            }n|j         t*          j        k    r[|j                            d          }|t8          v rt          t8          |                   }nAt          t*          j        |          }n%|j         t          j        k    rt          ||          }|r7|	j        d         j        dk    rd}n|	j        d         j        dk    rnt:          |r@|>|j        dd         D ].}t          ||          }||j        
                    |           /|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   Leafr9   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   s                r   rC   rC   g   s    HyDL  }QyD%%%t}"""4=#3V<<HH $5666H 6 6=&&u--a/ %eX66&%,,W5556 
d&	&	&t}!!J///H 6 6%eX66 6%,,W555$   #4=#3V<<HH	di		t}Q'55 	9M!"c))t}Q/888a(&+66 	}Q%,,DM""1$$t}Q'11 %}Q%,,4 !] 
	) 
	)EzT\))$t},,# %t000$)!ug&& )5;#+=+=$(!  	)a(Iy'** -y#/E/E M!,	a(I >\...%''"111<99 N&GL)/,R,RSSSHH&GFIO,L,LMMMHH^|222 ?((--Dv~~"t555"(9EEE^t000"#4f==H  		*%a(.#55'*0C77 *)  	6H0%.qt4 6 6%eX66&%,,W555 ! Or   c                    t          | t                    s| S t          |           dk    r| d         S g }g }g dg }d| D ]}t          t	          |d                     rt          t	          |fd                    r|                    |           Vt          t	          |fd                    r|                    |           |                    |           |r|} n	|r|} n|r|} t          | t                    S )	zPicks the most characteristic from a list of linear patterns
    Current order used is:
    names > common_names > common_chars
    r   r5   )inforifnotNonez[]().,:c                 .    t          |           t          u S N)r   r   )xs    r   <lambda>z/get_characteristic_subpattern.<locals>.<lambda>   s    d1ggn r   c                 6    t          | t                    o| v S rb   rG   r   )rc   common_charss    r   rd   z/get_characteristic_subpattern.<locals>.<lambda>   s    jC&8&8&NQ,=N r   c                 6    t          | t                    o| v S rb   rf   )rc   common_namess    r   rd   z/get_characteristic_subpattern.<locals>.<lambda>  s    
1c(:(:(PqL?P r   )key)rG   listr    r<   rec_testr   max)subpatternssubpatterns_with_namessubpatterns_with_common_namessubpatterns_with_common_chars
subpatternrg   ri   s        @@r   r#   r#      so   
 k4(( 
;1~  $&!666L$&!L! 
: 
:
x
$<$<==>> 		:8JNNNNP P Q Q :-44Z@@@@XjPPPPR R S S :-44Z@@@@ '--j999 4,	& 43	& 43{$$$$r   c              #      K   | D ]B}t          |t          t          f          rt          ||          E d{V  5 ||          V  CdS )zPTests test_func on all items of sequence and items of included
    sub-iterablesN)rG   rk   r!   rl   )sequence	test_funcrc   s      r   rl   rl     ss         a$'' 	9----------)A,,	 r   rb   )r2    r   pgen2r   r   pygramr   r   r@   rN   opmaprQ   r$   rL   r   r"   objectr   rC   r#   rl   r3   r   r   <module>r{      s    2 2       ! ! ! ! ! ! ! ! 3 3 3 3 3 3 3 3		 
U U U U Uf U U UnB B B BJ#% #% #%J    r   