
\c           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z m	 Z	 m
 Z
 m Z m Z m Z m Z d d l m Z m Z d d l m Z m Z e r d d l m Z m Z m Z m Z m Z n  e j d  Z e j d  Z e j d	  Z e j d k r0e j  e j! f Z" n	 e j  Z" d   Z# d   Z$ d d  Z& d   Z' d e( f d     YZ) d e( f d     YZ* d e* f d     YZ+ d e j, f d     YZ- d e* f d     YZ. d e( f d     YZ/ d S(   s   
    sphinx.pycode.parser
    ~~~~~~~~~~~~~~~~~~~~

    Utilities parsing and analyzing Python code.

    :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
iN(   t   NAMEt   NEWLINEt   INDENTt   DEDENTt   NUMBERt   OPt   STRING(   t   COMMENTt   NL(   t   PY2t	   text_type(   t   Anyt   Dictt   IOt   Listt   Tupleu   ^\s*#: ?(.*)?
?$u   ^\s*$u   ^\s*(#.*)?$i   i   c         C   s   |  j  d d  S(   Ns   t    (   t   replace(   t   code(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   filter_whitespace%   s    c         C   s'   t  |  t j  r |  j S|  j g Sd S(   s3   Get list of targets from Assign and AnnAssign node.N(   t
   isinstancet   astt   Assignt   targetst   target(   t   node(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   get_assign_targets*   s    c         C   s  | r$ t  r | j } q$ | j } n  |  j j } | d k rO t d |    nB| d k r | d k sv |  j | k r |  j g St d |    n | d k r g  } x? |  j D]4 } y | j t	 | |   Wq t k
 r q Xq W| S| d k rR|  j
 j j d k r?| r?|  j
 j | k r?d t	 |  j |  d g St d |    n? | d k re|  g S| d k rt	 |  j
 |  St d |   d S(   s   Convert assignment-AST to variable names.

    This raises `TypeError` if the assignment does not create new variable::

        ary[0] = 'foo'
        dic["bar"] = 'baz'
        # => TypeError
    t   Indext   Numt   Slicet   Strt	   Subscripts   %r does not create new variablet   Names*   The assignment %r is not instance variableR   R   t	   Attributes   %si    t   strt   Starreds   Unexpected node name %rN(   R   R   R   R   R   (   R   R   (   R	   t   idt   argt	   __class__t   __name__t	   TypeErrort   Nonet   eltst   extendt   get_lvar_namest   valuet   attrt   NotImplementedError(   R   t   selft   self_idt	   node_namet   memberst   elt(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyR,   3   s8    

-c         C   s7   d   } |  | _  t j |  } | j d  j d  S(   s1   Remove common leading indentation from docstring.c           S   s   d  S(   N(    (    (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   dummyd   s    s   
(   t   __doc__t   inspectt   getdoct   lstript   rstrip(   t   sR5   t	   docstring(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   dedent_docstringa   s    		t   Tokenc           B   s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s)   Better token wrapper for tokenize module.c         C   s1   | |  _  | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   kindR-   t   startt   endt   source(   R0   R?   R-   R@   RA   RB   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   __init__q   s
    				c         C   s   t  | t  r |  j | k St  | t  r8 |  j | k St  | t t f  ri |  j |  j g t |  k S| d  k ry t St	 d |   d  S(   Ns   Unknown value: %r(
   R   t   intR?   R"   R-   t   listt   tupleR)   t   Falset
   ValueError(   R0   t   other(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   __eq__y   s    c         C   s   |  | k S(   N(    (   R0   RI   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   __ne__   s    c            s   t    f d   | D  S(   Nc         3   s   |  ] }   | k Vq d  S(   N(    (   t   .0t	   candidate(   R0   (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pys	   <genexpr>   s    (   t   any(   R0   t
   conditions(    (   R0   s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   match   s    c         C   s!   d t  j |  j |  j j   f S(   Ns   <Token kind=%r value=%r>(   t   tokenizet   tok_nameR?   R-   t   strip(   R0   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   __repr__   s    (   R'   t
   __module__R6   RC   RJ   RK   RP   RT   (    (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyR>   n   s   				t   TokenProcessorc           B   s,   e  Z d    Z d   Z d   Z d   Z RS(   c            sF   t  |    | |  _ t j   f d    |  _ d  |  _ d  |  _ d  S(   Nc              s
   t     S(   N(   t   next(    (   t   lines(    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   <lambda>   s    (   t   itert   buffersRQ   t   generate_tokenst   tokensR)   t   currentt   previous(   R0   R[   (    (   RX   s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRC      s
    		c         C   s   |  j  | d S(   s   Returns specified line.i   (   R[   (   R0   t   lineno(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   get_line   s    c         C   sL   y( |  j  |  _ t t |  j    |  _  Wn t k
 rD d |  _  n X|  j  S(   s^   Fetch a next token from source code.

        Returns ``False`` if sequence finished.
        N(   R^   R_   R>   RW   R]   t   StopIterationR)   (   R0   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   fetch_token   s    c         C   s   g  } x |  j    r | j |  j  |  j | k r8 Pq	 |  j t d g k ri | |  j t d g  7} q	 |  j t d g k r | |  j t d g  7} q	 |  j t d g k r	 | |  j t d g  7} q	 q	 W| S(   sl   Fetch tokens until specified token appeared.

        .. note:: This also handles parenthesis well.
        t   (t   )t   {t   }t   [t   ](   Rc   t   appendR^   R   t   fetch_until(   R0   t	   conditionR]   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRk      s     (   R'   RU   RC   Ra   Rc   Rk   (    (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRV      s   			t   AfterCommentParserc           B   s)   e  Z d  Z d   Z d   Z d   Z RS(   s   Python source code parser to pick up comment after assignment.

    This parser takes a python code starts with assignment statement,
    and returns the comments for variable if exists.
    c         C   s#   t  t |   j |  d  |  _ d  S(   N(   t   superRm   RC   R)   t   comment(   R0   RX   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRC      s    c         C   s   g  } x|  j    r| j |  j  |  j t d g k rV | |  j t d g  7} q	 |  j t d g k r | |  j t d g  7} q	 |  j t d g k r | |  j t d g  7} q	 |  j t k r | |  j t  7} q	 |  j t d g k r Pq	 |  j j t t t	 t
 f k r	 Pq	 q	 W| S(   s%   Fetch right-hand value of assignment.Rd   Re   Rf   Rg   Rh   Ri   t   ;(   Rc   Rj   R^   R   Rk   R   R   R?   R    R   R   (   R0   R]   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   fetch_rvalue   s     c         C   s~   x4 |  j    j t d g t t  s6 |  j s t  q W|  j t d g k rY |  j   n  |  j t k rz |  j j |  _	 n  d S(   s3   Parse the code and obtain comment after assignment.t   =N(
   Rc   RP   R   R   R   R^   t   AssertionErrorRq   R-   Ro   (   R0   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   parse   s    $(   R'   RU   R6   RC   Rq   Rt   (    (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRm      s   		t   VariableCommentPickerc           B   sq   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z RS(   s7   Python source code parser to pick up variable comments.c         C   sn   t  j   |  _ | |  _ | |  _ g  |  _ g  |  _ d  |  _ i  |  _	 d  |  _
 i  |  _ t t |   j   d  S(   N(   t	   itertoolst   countt   counterR[   t   encodingt   contextt   current_classesR)   t   current_functiont   commentsR_   t	   defordersRn   Ru   RC   (   R0   R[   Ry   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRC      s    								c         C   sv   |  j  rC |  j r< |  j d d k r< |  j d  | g } qS d  Sn |  j | g } t |  j  |  j d j |  <d  S(   NiRC   t   .(   R|   R{   Rz   RW   Rx   R~   t   join(   R0   t   namet
   definition(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt	   add_entry  s    	c         C   sn   |  j  rE |  j r> |  j d d k r> d j |  j d   } qW d  Sn d j |  j  } | |  j | | f <d  S(   NiRC   R   (   R|   R{   Rz   R   R}   (   R0   R   Ro   Rz   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   add_variable_comment  s    	c         C   s1   |  j  r) |  j  j j r) |  j  j j d Sd Sd S(   s2   Returns the name of first argument if in function.i    N(   R|   t   argsR)   (   R0   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   get_self  s    c         C   s   |  j  | d S(   s   Returns specified line.i   (   R[   (   R0   R`   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRa   '  s    c         C   s#   t  t |   j |  | |  _ d S(   s   Updates self.previous to .N(   Rn   Ru   t   visitR_   (   R0   R   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyR   ,  s    c         C   s  yV t  |  } t g  | D] } t | d |  j   ^ q g   } |  j | j  } Wn t k
 rj d SXt | | j g |  j	 | j  } | j
   | j r t j | j  r x: | D]2 } |  j | t j d | j   |  j |  q Wd St j | | j   rg  } xa t | j d  D]L }	 |  j | j d |	  }
 t j |
  rp| j t j d |
   q%Pq%W| rt d j t |    } x+ | D]# } |  j | |  |  j |  qWd Sn  x | D] } |  j |  qWd S(   s3   Handles Assign node and pick up a variable comment.R0   Ns   \1i   s   
(   R   t   sumR,   R   Ra   R`   R(   Rm   t
   col_offsetR[   Rt   Ro   t
   comment_reRP   R   t   subR   t	   indent_ret   rangeRj   R=   R   t   reversed(   R0   R   R   t   tt   varnamest   current_linet   parsert   varnamet   comment_linest   it   before_lineRo   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   visit_Assign2  s:    4
c         C   s   |  j  |  d S(   s6   Handles AnnAssign node and pick up a variable comment.N(   R   (   R0   R   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   visit_AnnAssign[  s    c         C   s   t  |  j t  r t  | j t j  r y t |  j  } t | d |  j    } xs | D]k } t  | j j	 t
  r | j j	 } n | j j	 j |  j p d  } |  j | t |   |  j |  qY WWq t k
 r q Xn  d S(   s2   Handles Expr node and pick up a comment if string.i    s   utf-8N(   R   R_   t   ASSIGN_NODESR-   R   R   R   R,   R   R;   R
   t   decodeRy   R   R=   R   R(   (   R0   R   R   R   R   R<   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt
   visit_Expr`  s    'c         C   s~   |  j  j | j  |  j | j  |  j j | j  | |  _ x | j D] } |  j |  qI W|  j j   |  j  j   d S(   s&   Handles ClassDef node and set context.N(	   R{   Rj   R   R   Rz   R_   t   bodyR   t   pop(   R0   R   t   child(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   visit_ClassDefr  s    	c         C   sy   |  j  d k ru |  j | j  |  j j | j  | |  _  x | j D] } |  j |  qE W|  j j   d |  _  n  d S(   s)   Handles FunctionDef node and set context.N(	   R|   R)   R   R   Rz   Rj   R   R   R   (   R0   R   R   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   visit_FunctionDef~  s    	(   R'   RU   R6   RC   R   R   R   Ra   R   R   R   R   R   R   (    (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRu      s   							)			t   DefinitionFinderc           B   s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         C   s>   t  t |   j |  d  |  _ g  |  _ g  |  _ i  |  _ d  S(   N(   Rn   R   RC   R)   t	   decoratorRz   t   indentst   definitions(   R0   RX   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRC     s
    			c         C   sD   |  j  r3 |  j  d d d k r3 | d d k r3 n | |  j | <d  S(   Nii    t   def(   R   R   (   R0   R   t   entry(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   add_definition  s    0c         C   s  xt  r|  j   } | d  k r% Pq | t k r4 q | t d g k r |  j d  k sp |  j j t t t	 t
  r |  j d  k r| |  _ qq | j t d g  r |  j d  q | j t d g  r |  j d  q | t	 k r |  j j d  q | t
 k r |  j   q q Wd  S(   Nt   @t   classR   RI   (   RI   NN(   t   TrueRc   R)   R   R   R_   RP   R   R   R   R   R   R    t   parse_definitionR   Rj   t   finalize_block(   R0   t   token(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRt     s$    	!c         C   s   |  j    } |  j j | j  d j |  j  } |  j rV |  j j d } d  |  _ n | j d } |  j t	 d g  |  j    j
 t t  r |  j t  |  j j | | | f  n- |  j | | | | j d f  |  j j   d  S(   NR   i    t   :(   Rc   Rz   Rj   R-   R   R   R@   R)   Rk   R   RP   R   R   R   R   R   RA   R   (   R0   t   typR   t   funcnamet	   start_pos(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyR     s    	 c         C   s   |  j  j   } | d d k r | \ } } } |  j j d d } x& t j |  j |   rj | d 8} qE W|  j | | | | f  |  j j   n  d  S(   Ni    RI   i   (	   R   R   R^   RA   t   emptyline_reRP   Ra   R   Rz   (   R0   R   R   R   R   t   end_pos(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyR     s    (   R'   RU   RC   R   Rt   R   R   (    (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyR     s
   				t   Parserc           B   s5   e  Z d  Z d d  Z d   Z d   Z d   Z RS(   sy   Python source code parser to pick up variable comments.

    This is a better wrapper for ``VariableCommentPicker``.
    s   utf-8c         C   s7   t  |  |  _ | |  _ i  |  _ i  |  _ i  |  _ d  S(   N(   R   R   Ry   R}   R~   R   (   R0   R   Ry   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRC     s
    			c         C   s   |  j    |  j   d S(   s   Parse the source code.N(   t   parse_commentsR   (   R0   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyRt     s    
c         C   sb   t  j |  j j d   } t |  j j t  |  j  } | j |  | j	 |  _	 | j
 |  _
 d S(   s$   Parse the code and pick up comments.s   utf-8N(   R   Rt   R   t   encodeRu   t
   splitlinesR   Ry   R   R}   R~   (   R0   t   treet   picker(    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyR     s
    c         C   s2   t  |  j j t   } | j   | j |  _ d S(   s0   Parse the location of definitions from the code.N(   R   R   R   R   Rt   R   (   R0   R   (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyR     s    
(   R'   RU   R6   RC   Rt   R   R   (    (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyR     s
   			(   i   i   (0   R6   R   R7   Rv   t   ret   sysRQ   R   R    R   R   R   R   R   R   R   R   t   sixR	   R
   RG   t   typingR   R   R   R   R   t   compileR   R   R   t   version_infoR   t	   AnnAssignR   R   R   R)   R,   R=   t   objectR>   RV   Rm   t   NodeVisitorRu   R   R   (    (    (    s3   lib/python2.7/site-packages/sphinx/pycode/parser.pyt   <module>
   s6   4+				.	&10H