Ñò
V}òOc           @   sT   d  Z  d d k l Z d d k Z d e f d „  ƒ  YZ d e i e i d „ Z	 d S(   sŸ    @package antlr3.dottreegenerator
@brief ANTLR3 runtime package, tree module

This module contains all support classes for AST construction and tree parsers.

iÿÿÿÿ(   t   CommonTreeAdaptorNt   DOTTreeGeneratorc           B   s¦   e  Z d  Z e i d d d d d d d d d	 d
 ƒ Z e i d d ƒ Z e i d d ƒ Z d „  Z d e e d „ Z
 d d „ Z d „  Z d „  Z d „  Z RS(   s¾   
    A utility class to generate DOT diagrams (graphviz) from
    arbitrary trees.  You can pass in your own templates and
    can pass in any kind of tree or use Tree interface method.
    t   templates
   digraph {
s     ordering=out;
s     ranksep=.4;
sJ     node [shape=plaintext, fixedsize=true, fontsize=11, fontname="Courier",
s            width=.25, height=.25];
s     edge [arrowsize=.5]
s
     $nodes$
s
     $edges$
s   }
s   $name$ [label="$text$"];
s7   $parent$ -> $child$ // "$parentText$" -> "$childText$"
c         C   s   h  |  _  d |  _ d  S(   Ni    (   t   nodeToNumberMapt
   nodeNumber(   t   self(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/dottreegen.pyt   __init__H   s    	c         C   se   | d  j o t ƒ  } n | i ƒ  } d |  _ |  i | | | ƒ d |  _ |  i | | | | ƒ | S(   Ni    (   t   NoneR    t   getInstanceOfR   t   toDOTDefineNodest   toDOTDefineEdges(   R   t   treet   adaptort   treeSTt   edgeST(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/dottreegen.pyt   toDOTP   s    		c         C   s.  | d  j o t ƒ  } n | d  j o d  S| i | ƒ } | d j o d  S|  i | ƒ } | | j o3 |  i | | ƒ } | i d | ƒ | i | ƒ n x‹ t | ƒ D]} } | i | | ƒ }	 |  i |	 ƒ } | | j o3 |  i | |	 ƒ }
 | i d |
 ƒ | i | ƒ n |  i	 |	 | | | ƒ q© Wd  S(   Ni    t   nodes(
   R   t   sett   getChildCountt   getNodeNumbert	   getNodeSTt   setAttributet   addt   ranget   getChildR	   (   R   R   R   R   t
   knownNodest   nt   numbert   parentNodeSTt   it   childt   nodeST(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/dottreegen.pyR	   ^   s*     c         C   s  | d  j o d  S| i | ƒ } | d j o d  Sd |  i | ƒ } | i | ƒ } xº t | ƒ D]¬ } | i | | ƒ }	 | i |	 ƒ }
 d |  i |	 ƒ } | i ƒ  } | i d | ƒ | i d | ƒ | i d | ƒ | i d |
 ƒ | i d | ƒ |  i |	 | | | ƒ qb Wd  S(   Ni    s   n%dt   parentR   t
   parentTextt	   childTextt   edges(	   R   R   R   t   getTextR   R   R   R   R
   (   R   R   R   R   R   R   t
   parentNameR!   R   R   R"   t	   childName(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/dottreegen.pyR
      s&     c         C   sx   | i  | ƒ } |  i i ƒ  } d |  i | ƒ } | i d | ƒ | d  j	 o | i d d ƒ } n | i d | ƒ | S(   Ns   n%dt   namet   "s   \\"t   text(   R$   t   _nodeSTR   R   R   R   t   replace(   R   R   t   tR)   R   t
   uniqueName(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/dottreegen.pyR   š   s    c         C   sP   y |  i  | SWn: t j
 o. |  i |  i  | <|  i d 7_ |  i d SXd  S(   Ni   (   R   t   KeyErrorR   (   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/dottreegen.pyR   ¥   s    N(   t   __name__t
   __module__t   __doc__t   stringtemplate3t   StringTemplatet   _treeSTR*   t   _edgeSTR   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/dottreegen.pyR   ,   s   		
					!		c         C   s   t  ƒ  } | i |  | | | ƒ S(   s  
    Generate DOT (graphviz) for a whole tree not just a node.
    For example, 3+4*5 should generate:

    digraph {
        node [shape=plaintext, fixedsize=true, fontsize=11, fontname="Courier",
            width=.4, height=.2];
        edge [arrowsize=.7]
        "+"->3
        "+"->"*"
        "*"->4
        "*"->5
    }

    Return the ST not a string in case people want to alter.

    Takes a Tree interface object.

    Example of invokation:

        import antlr3
        import antlr3.extras

        input = antlr3.ANTLRInputStream(sys.stdin)
        lex = TLexer(input)
        tokens = antlr3.CommonTokenStream(lex)
        parser = TParser(tokens)
        tree = parser.e().tree
        print tree.toStringTree()
        st = antlr3.extras.toDOT(t)
        print st
        
    (   R   R   (   R   R   R   R   t   gen(    (    sx   /afs/athena.mit.edu/user/x/a/xavid/lib/python2.7/site-packages/antlr_python_runtime-3.1.3-py2.6.egg/antlr3/dottreegen.pyR   ®   s    #	(
   R1   t   antlr3.treeR    R2   t   objectR   R   R4   R5   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/dottreegen.pyt   <module>   s   #‚