ó
è¾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 d e f d	 „  ƒ  YZ d d
 „ Z d „  Z d „  Z d S(   s0   Utility functions used by the btm_matcher modulei   (   t   pytree(   t   grammart   token(   t   pattern_symbolst   python_symbolsiÿÿÿÿiþÿÿÿiýÿÿÿt   MinNodec           B   sA   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s‰   This class serves as an intermediate representation of the
    pattern tree during the conversion to sets of leaf-to-root
    subpatternsc         C   sC   | |  _  | |  _ g  |  _ t |  _ d  |  _ g  |  _ g  |  _ d  S(   N(	   t   typet   namet   childrent   Falset   leaft   Nonet   parentt   alternativest   group(   t   selfR   R   (    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyt   __init__   s    						c         C   s   t  |  j ƒ d t  |  j ƒ S(   Nt    (   t   strR   R   (   R   (    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyt   __repr__   s    c         C   sU  |  } g  } xB| rP| j  t k r | j j | ƒ t | j ƒ t | j ƒ k r| t | j ƒ g } g  | _ | j } q q | j } d } Pn  | j  t	 k r| j
 j | ƒ t | j
 ƒ t | j ƒ k ró t | j
 ƒ } g  | _
 | j } q q| j } d } Pn  | j  t j k r4| j r4| j | j ƒ n | j | j  ƒ | j } q W| S(   s°   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   t   TYPE_ALTERNATIVESR   t   appendt   lenR   t   tupleR   R   t
   TYPE_GROUPR   t   get_characteristic_subpatternt   token_labelst   NAMER   (   R   t   nodet   subp(    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyt   leaf_to_root!   s8    							c         C   s1   x* |  j  ƒ  D] } | j ƒ  } | r | Sq Wd S(   s‹  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(   t   leavesR   (   R   t   lR   (    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyt   get_linear_subpatternK   s    c         c   sE   x- |  j  D]" } x | j ƒ  D] } | Vq Wq
 W|  j  sA |  Vn  d S(   s-   Generator that returns the leaves of the treeN(   R   R   (   R   t   childt   x(    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyR   `   s
    	N(	   t   __name__t
   __module__t   __doc__R   R   R   R   R!   R   (    (    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyR      s   			*	c         C   s  d } |  j t j k r( |  j d }  n  |  j t j k rØ t |  j ƒ d k rh t |  j d | ƒ } qüt d t	 ƒ } x‚|  j D]P } |  j j
 | ƒ d r£ q n  t | | ƒ } | d k	 r | j j | ƒ q q Wn$|  j t j k rxt |  j ƒ d k r_t d t ƒ } x9 |  j D]. } t | | ƒ } | r| j j | ƒ qqW| j sud } quqüt |  j d | ƒ } n„|  j t j k rüt |  j d t j ƒ rÍ|  j d j d k rÍt |  j d | ƒ St |  j d t j ƒ rü|  j d j d k s=t |  j ƒ d k rAt |  j d d ƒ rA|  j d j d k rAd St } d } d } t } d }	 t }
 x• |  j D]Š } | j t j k r–t } | } n< | j t j k r·t } | }	 n | j t j k rÒ| } n  t | d ƒ ro| j d k rot }
 qoqoW|
 rA|  j d } t | d ƒ rN| j d k rN|  j d	 } qNn |  j d } | j t j k rÏ| j d
 k rt d t ƒ } qTt t | j ƒ r±t d t t | j ƒ ƒ } qTt d t t | j ƒ ƒ } n… | j t j k r0| j j d ƒ } | t k rt d t | ƒ } qTt d t j d | ƒ } n$ | j t j k rTt | | ƒ } n  | r›|	 j d j d k ryd } q›|	 j d j d k r’q›t  ‚ n  | rü| d k	 rüxI | j d d !D]4 } t | | ƒ } | d k	 r¾| j j | ƒ q¾q¾Wqün  | r| | _! n  | S(   sÏ   
    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*).
    i    i   R   i   t   (t   [t   valuet   =i   t   anyt   'R   t   *t   +iÿÿÿÿN("   R   R   t   symst   MatcherR   t   AlternativesR   t   reduce_treeR   R   t   indexR   t   AlternativeR   t   Unitt
   isinstanceR    t   LeafR)   t   hasattrt   TrueR	   t   Detailst   RepeaterR   R   t   TYPE_ANYt   getattrt   pysymst   STRINGt   stript   tokenst   NotImplementedErrorR   (   R   R   t   new_nodeR"   t   reducedR
   t   details_nodet   alternatives_nodet   has_repeatert   repeater_nodet   has_variable_namet	   name_leafR   (    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyR2   h   s¦    					c            s,  t  |  t ƒ s |  St |  ƒ d k r- |  d Sg  } g  } d d d d d g ‰ g  } d ‰  x’ |  D]Š } t t | d	 „  ƒ ƒ ra t t | ‡  f d
 †  ƒ ƒ r­ | j | ƒ që t t | ‡ f d †  ƒ ƒ rÛ | j | ƒ që | j | ƒ qa qa W| rþ | }  n | r| }  n | r| }  n  t |  d t ƒS(   s„   Picks the most characteristic from a list of linear patterns
    Current order used is:
    names > common_names > common_chars
    i   i    t   int   fort   ift   notR   s   []().,:c         S   s   t  |  ƒ t k S(   N(   R   R   (   R#   (    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyt   <lambda>ÿ   t    c            s   t  |  t ƒ o |  ˆ  k S(   N(   R6   R   (   R#   (   t   common_chars(    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyRO     RP   c            s   t  |  t ƒ o |  ˆ  k S(   N(   R6   R   (   R#   (   t   common_names(    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyRO     RP   t   key(   R6   t   listR   R+   t   rec_testR   t   max(   t   subpatternst   subpatterns_with_namest   subpatterns_with_common_namest   subpatterns_with_common_charst
   subpattern(    (   RQ   RR   s'   /usr/lib/python2.7/lib2to3/btm_utils.pyR   î   s2    					c         c   sW   xP |  D]H } t  | t t f ƒ rD x* t | | ƒ D] } | Vq2 Wq | | ƒ Vq Wd S(   sP   Tests test_func on all items of sequence and items of included
    sub-iterablesN(   R6   RT   R   RU   (   t   sequencet	   test_funcR#   t   y(    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyRU     s
    N(   R&   RP   R    t   pgen2R   R   t   pygramR   R   R/   R>   t   opmapRA   R   R<   R   R   t   objectR   R   R2   R   RU   (    (    (    s'   /usr/lib/python2.7/lib2to3/btm_utils.pyt   <module>   s   	X†	%