Ź˙f' c @ 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 Gd d d e Z d d d Z d d Z d d Z d S( u0 Utility functions used by the btm_matcher modulei ( u pytree( u grammaru token( u pattern_symbolsu python_symbolsi i c B s\ | Ee Z d Z d Z d d d d Z d d Z d d Z d d Z d d Z d S( u MinNodeu This class serves as an intermediate representation of the pattern tree during the conversion to sets of leaf-to-root subpatternsc C sC | | _ | | _ g | _ d | _ d | _ g | _ g | _ d S( NF( u typeu nameu childrenu Falseu leafu Noneu parentu alternativesu group( u selfu typeu name( ( u6 /opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu __init__ s u MinNode.__init__c C s t | j d t | j S( Nu ( u stru typeu name( u self( ( u6 /opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu __repr__ s u MinNode.__repr__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( u° 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( u typeu TYPE_ALTERNATIVESu alternativesu appendu lenu childrenu tupleu parentu Noneu TYPE_GROUPu groupu get_characteristic_subpatternu token_labelsu NAMEu name( u selfu nodeu subp( ( u6 /opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu leaf_to_root! s8 u MinNode.leaf_to_rootc C s1 x* | j D] } | j } | r | Sq Wd S( u 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( u leavesu leaf_to_root( u selfu lu subp( ( u6 /opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu get_linear_subpatternK s u MinNode.get_linear_subpatternc c sE x- | j D]" } x | j D] } | Vq Wq W| j sA | Vn d S( u- Generator that returns the leaves of the treeN( u childrenu leaves( u selfu childu x( ( u6 /opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu leaves` s u MinNode.leavesN( u __name__u __module__u __qualname__u __doc__u Noneu __init__u __repr__u leaf_to_rootu get_linear_subpatternu leaves( u __locals__( ( u6 /opt/alt/python33/lib64/python3.3/lib2to3/btm_utils.pyu MinNode s *u MinNodec 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 rxt | j d k r_t d t } x9 | j D]. } t | | } | r| j j | qqW| j sud } quq˙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 rAt | j d d rA| j d j d k rAd Sd } d } d } d } d } d } x | j D] } | j t j k rd } | } n<