Ñò
V}òOc           @   sý   d  Z  d d k l Z d d k l Z d d k l Z l Z d „  Z d Z	 d Z
 d Z d Z d	 Z d
 Z d Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s&   @package antlr3.tree
@brief ANTLR3 runtime package, treewizard module

A utility module to create ASTs at runtime.
See <http://www.antlr.org/wiki/display/~admin/2007/07/02/Exploring+Concept+of+TreeWizard> for an overview. Note that the API of the Python implementation is slightly different.

iÿÿÿÿ(   t   INVALID_TOKEN_TYPE(   t   CommonToken(   t
   CommonTreet   CommonTreeAdaptorc         C   s,   |  d j o h  St d „  t |  ƒ Dƒ ƒ S(   sk   
    Compute a dict that is an inverted index of
    tokenNames (which maps int token types to names).
    c         s   s%   x |  ] \ } } | | f Vq Wd  S(   N(    (   t   .0t   typet   name(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pys	   <genexpr>5   s   	 N(   t   Nonet   dictt	   enumerate(   t
   tokenNames(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   computeTokenTypes,   s    i   i   i   i   i   i   i   t   TreePatternLexerc           B   s?   e  Z d  „  Z e d ƒ Z e e d ƒ BZ d „  Z d „  Z RS(   c         C   sJ   | |  _  d |  _ d  |  _ t | ƒ |  _ d  |  _ t |  _ |  i	 ƒ  d  S(   Niÿÿÿÿ(
   t   patternt   pR   t   ct   lent   nt   svalt   Falset   errort   consume(   t   selfR   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   __init__C   s    					t5   abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_t
   0123456789c         C   s  d |  _  x |  i t j oï|  i d j o |  i ƒ  q n |  i |  i j oX |  i  |  i 7_  |  i ƒ  x4 |  i |  i j o  |  i  |  i 7_  |  i ƒ  qo Wt S|  i d j o |  i ƒ  t S|  i d j o |  i ƒ  t S|  i d j o |  i ƒ  t	 S|  i d	 j o |  i ƒ  t
 S|  i d
 j o |  i ƒ  t S|  i d j o  |  i ƒ  x„ |  i d j os |  i d j oC |  i ƒ  |  i d j o |  i  d 7_  n |  i  |  i 7_  n |  i  |  i 7_  |  i ƒ  q`W|  i ƒ  t S|  i ƒ  t |  _ t SWt S(   Nt    t    s   
s   s   	t   (t   )t   %t   :t   .t   [t   ]s   \(   R   s   
s   s   	(   R   R   t   EOFR   t   _TreePatternLexer__idStartChart   _TreePatternLexer__idChart   IDt   BEGINt   ENDt   PERCENTt   COLONt   DOTt   ARGt   TrueR   (   R   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt	   nextToken]   s\    	 

 





 


	c         C   sF   |  i  d 7_  |  i  |  i j o t |  _ n |  i |  i  |  _ d  S(   Ni   (   R   R   R#   R   R   (   R   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR   š   s    (   t   __name__t
   __module__R   t	   frozensetR$   R%   R.   R   (    (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR   B   s   			=t   TreePatternParserc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s.   | |  _  | |  _ | |  _ | i ƒ  |  _ d  S(   N(   t	   tokenizert   wizardt   adaptorR.   t   ttype(   R   R3   R4   R5   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR   ¤   s    			c         C   sU   |  i  t j o |  i ƒ  S|  i  t j o& |  i ƒ  } |  i  t j o | Sd  Sd  S(   N(   R6   R'   t	   parseTreeR&   t	   parseNodeR#   R   (   R   t   node(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR   «   s    c         C   sø   |  i  t j o d  S|  i i ƒ  |  _  |  i ƒ  } | d  j o d  Sx… |  i  t t t t f j oh |  i  t j o# |  i	 ƒ  } |  i
 i | | ƒ qH |  i ƒ  } | d  j o d  S|  i
 i | | ƒ qH W|  i  t j o d  S|  i i ƒ  |  _  | S(   N(   R6   R'   R   R3   R.   R8   R&   R)   R+   R7   R5   t   addChildR(   (   R   t   roott   subtreet   child(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR7   ¹   s&     c         C   s  d  } |  i t j op |  i i ƒ  |  _ |  i t j o d  S|  i i } |  i i ƒ  |  _ |  i t j o d  S|  i i ƒ  |  _ n |  i t j oL |  i i ƒ  |  _ t	 d d ƒ } t
 | ƒ } | d  j	 o | | _ n | S|  i t j o d  S|  i i } |  i i ƒ  |  _ | d j o |  i i ƒ  S| } d  } |  i t j o( |  i i } | } |  i i ƒ  |  _ n |  i i | ƒ } | t j o d  S|  i i | | ƒ } | d  j	 o t | t ƒ o | | _ n | d  j	 o t | t ƒ o t | _ n | S(   Ni    R    t   nil(   R   R6   R)   R3   R.   R&   R   R*   R+   R   t   WildcardTreePatternt   labelR5   R>   R,   R4   t   getTokenTypeR    t   createFromTypet
   isinstancet   TreePatternR-   t
   hasTextArg(   R   R@   t   wildcardPayloadR9   t	   tokenNamet   textt   argt   treeNodeType(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR8   Õ   sL    (   R/   R0   R   R   R7   R8   (    (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR2   £   s   			RD   c           B   s    e  Z d  Z d „  Z d „  Z RS(   sY   
    When using %label:TOKENNAME in a tree for parse(), we must
    track the label.
    c         C   s&   t  i |  | ƒ d  |  _ d  |  _ d  S(   N(   R   R   R   R@   RE   (   R   t   payload(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR     s    	c         C   s>   |  i  d  j	 o d |  i  d t i |  ƒ St i |  ƒ Sd  S(   NR   R   (   R@   R   R   t   toString(   R   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRL     s    (   R/   R0   t   __doc__R   RL   (    (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRD     s   	R?   c           B   s   e  Z RS(    (   R/   R0   (    (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR?   $  s   t   TreePatternTreeAdaptorc           B   s   e  Z d  Z d „  Z RS(   s>   This adaptor creates TreePattern objects for use during scan()c         C   s
   t  | ƒ S(   N(   RD   (   R   RK   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   createWithPayload+  s    (   R/   R0   RM   RO   (    (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRN   (  s   t
   TreeWizardc           B   s¤   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d d „ Z d „  Z d d „ Z d „  Z RS(   s6  
    Build and navigate trees with this object.  Must know about the names
    of tokens so you have to pass in a map or array of token names (from which
    this class can build the map).  I.e., Token DECL means nothing unless the
    class can translate it to a token type.

    In order to create nodes and navigate, this class needs a TreeAdaptor.

    This class can build a token type -> node index for repeated use or for
    iterating over the various nodes with a particular type.

    This class works in conjunction with the TreeAdaptor rather than moving
    all this functionality into the adaptor.  An adaptor helps build and
    navigate trees using methods.  This class helps you do it with string
    patterns like "(A B C)".  You can create a tree from that pattern or
    match subtrees against it.
    c         C   sS   | |  _  | d  j o t | ƒ |  _ n' | d  j	 o t d ƒ ‚ n | |  _ d  S(   Ns&   Can't have both tokenNames and typeMap(   R5   R   R   t   tokenNameToTypeMapt
   ValueError(   R   R5   R
   t   typeMap(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR   B  s    	c         C   s*   y |  i  | SWn t j
 o t SXd S(   s=   Using the map of token names to token types, return the type.N(   RQ   t   KeyErrorR    (   R   RG   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRA   N  s    c         C   s+   t  | ƒ } t | |  |  i ƒ } | i ƒ  S(   sü  
        Create a tree or node from the indicated tree pattern that closely
        follows ANTLR tree grammar tree element syntax:
        
        (root child1 ... child2).
        
        You can also just pass in a node: ID
         
        Any node can have a text argument: ID[foo]
        (notice there are no quotes around foo--it's clear it's a string).
        
        nil is a special name meaning "give me a nil node".  Useful for
        making lists: (nil A B C) is a list of A B C.
        (   R   R2   R5   R   (   R   R   R3   t   parser(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   createW  s    c         C   s   h  } |  i  | | ƒ | S(   s  Walk the entire tree and make a node name to nodes mapping.
        
        For now, use recursion but later nonrecursive version may be
        more efficient.  Returns a dict int -> list where the list is
        of your AST node type.  The int is the token type of the node.
        (   t   _index(   R   t   treet   m(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   indexl  s    c         C   s«   | d j o d S|  i i | ƒ } | i | ƒ } | d j o g  | | <} n | i | ƒ xE t |  i i | ƒ ƒ D]+ } |  i i | | ƒ } |  i | | ƒ qx Wd S(   s   Do the work for indexN(	   R   R5   t   getTypet   gett   appendt   ranget   getChildCountt   getChildRW   (   R   t   tRY   R6   t   elementst   iR=   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRW   y  s     c         C   sX   t  | t t f ƒ o |  i | | ƒ St  | t ƒ o |  i | | ƒ St d ƒ ‚ d S(   s´   Return a list of matching token.

        what may either be an integer specifzing the token type to find or
        a string with a pattern that must be matched.
        
        s    'what' must be string or integerN(   RC   t   intt   longt   _findTokenTypet
   basestringt   _findPatternt	   TypeError(   R   RX   t   what(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   findŠ  s
    c            s,   g  ‰  ‡  f d †  } |  i  | | | ƒ ˆ  S(   s1   Return a List of tree nodes with token type ttypec            s   ˆ  i  |  ƒ d  S(   N(   R]   (   RX   t   parentt
   childIndext   labels(   t   nodes(    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   visitor¡  s    (   t   visit(   R   Ra   R6   Rp   (    (   Ro   sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRf   œ  s    c            sš   g  ‰ t  | ƒ } t | ˆ t ƒ  ƒ } | i ƒ  ‰  ˆ  d j p ˆ  i ƒ  p t ˆ  t ƒ o d Sˆ  i ƒ  } ‡  ‡ ‡ f d †  } ˆ i	 | | | ƒ ˆ S(   s+   Return a List of subtrees matching pattern.c            s+   ˆ i  |  ˆ  d  ƒ o ˆ i |  ƒ n d  S(   N(   t   _parseR   R]   (   RX   Rl   Rm   R@   (   t   tpatternt   subtreesR   (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRp   º  s    N(
   R   R2   RN   R   R   t   isNilRC   R?   R[   Rq   (   R   Ra   R   R3   RU   t   rootTokenTypeRp   (    (   Rs   Rt   R   sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRh   ©  s    c         C   sj   t  | t t f ƒ o |  i | d d | | ƒ n4 t  | t ƒ o |  i | | | ƒ n t d ƒ ‚ d S(   sÇ  Visit every node in tree matching what, invoking the visitor.

        If what is a string, it is parsed as a pattern and only matching
        subtrees will be visited.
        The implementation uses the root node of the pattern in combination
        with visit(t, ttype, visitor) so nil-rooted patterns are not allowed.
        Patterns with wildcard roots are also not allowed.

        If what is an integer, it is used as a token type and visit will match
        all nodes of that type (this is faster than the pattern match).
        The labels arg of the visitor action method is never set (it's None)
        since using a token type rather than a pattern doesn't let us set a
        label.
        i    s    'what' must be string or integerN(   RC   Rd   Re   t
   _visitTypeR   Rg   t   _visitPatternRi   (   R   RX   Rj   Rp   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRq   Ã  s
    c         C   s—   | d j o d S|  i i | ƒ | j o | | | | d ƒ n xN t |  i i | ƒ ƒ D]4 } |  i i | | ƒ } |  i | | | | | ƒ q[ Wd S(   s   Do the recursive work for visitN(   R   R5   R[   R^   R_   R`   Rw   (   R   Ra   Rl   Rm   R6   Rp   Rc   R=   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRw   Ý  s     c            s”   t  | ƒ } t | ˆ t ƒ  ƒ } | i ƒ  ‰  ˆ  d j p ˆ  i ƒ  p t ˆ  t ƒ o d Sˆ  i ƒ  } ‡  ‡ ‡ f d †  } ˆ i	 | | | ƒ d S(   sT   
        For all subtrees that match the pattern, execute the visit action.
        Nc            s7   h  } ˆ i  |  ˆ  | ƒ o ˆ |  | | | ƒ n d  S(   N(   Rr   (   RX   Rl   Rm   Rn   (   Rs   Rp   R   (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   rootvisitorü  s    (
   R   R2   RN   R   R   Ru   RC   R?   R[   Rq   (   R   RX   R   Rp   R3   RU   Rv   Ry   (    (   Rs   R   Rp   sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRx   ë  s    c         C   s@   t  | ƒ } t | |  t ƒ  ƒ } | i ƒ  } |  i | | | ƒ S(   sÍ  
        Given a pattern like (ASSIGN %lhs:ID %rhs:.) with optional labels
        on the various nodes and '.' (dot) as the node/subtree wildcard,
        return true if the pattern matches and fill the labels Map with
        the labels pointing at the appropriate nodes.  Return false if
        the pattern is malformed or the tree does not match.

        If a node specifies a text arg in pattern, then that must match
        for that node in t.
        (   R   R2   RN   R   Rr   (   R   Ra   R   Rn   R3   RU   Rs   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   parse  s    c   	      C   s=  | d j p | d j o t St | t ƒ pV |  i i | ƒ | i ƒ  j o t S| i o$ |  i i | ƒ | i ƒ  j o t Sn | i d j	 o | d j	 o | | | i <n |  i i	 | ƒ } | i	 ƒ  } | | j o t SxS t
 | ƒ D]E } |  i i | | ƒ } | i | ƒ } |  i | | | ƒ p t Sqð Wt S(   s  
        Do the work for parse. Check to see if the tpattern fits the
        structure and token types in t1.  Check text if the pattern has
        text arguments on nodes.  Fill labels map with pointers to nodes
        in tree matched against nodes in pattern with labels.
	N(   R   R   RC   R?   R5   R[   RE   t   getTextR@   R_   R^   R`   Rr   R-   (	   R   t   t1Rs   Rn   t   n1t   n2Rc   t   child1t   child2(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRr     s*    	
	 	c         C   s-   | d j o |  i } n |  i | | | ƒ S(   sÔ   
        Compare t1 and t2; return true if token types/text, structure match
        exactly.
        The trees are examined in their entirety so that (A B) does not match
        (A B C) nor (A (B C)). 
        N(   R   R5   t   _equals(   R   R|   t   t2R5   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   equals@  s    c   	      C   sñ   | d  j p | d  j o t S| i | ƒ | i | ƒ j o t S| i | ƒ | i | ƒ j o t S| i | ƒ } | i | ƒ } | | j o t SxS t | ƒ D]E } | i | | ƒ } | i | | ƒ } |  i | | | ƒ p t Sq¤ Wt S(   N(	   R   R   R[   R{   R_   R^   R`   R   R-   (	   R   R|   R‚   R5   R}   R~   Rc   R   R€   (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyR   N  s"     	N(   R/   R0   RM   R   R   RA   RV   RZ   RW   Rk   Rf   Rh   Rq   Rw   Rx   Rz   Rr   Rƒ   R   (    (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyRP   /  s    												)N(   RM   t   antlr3.constantsR    t   antlr3.tokensR   t   antlr3.treeR   R   R   R#   R'   R(   R&   R,   R)   R*   R+   t   objectR   R2   RD   R?   RN   RP   (    (    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/treewizard.pyt   <module>   s$    	al