
    ړi>                         d dl mZmZ d dlmZmZmZmZ d dlm	Z	 ddde
ddfd	Z G d
 d      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    )abstractmethodabstractproperty)ListOptionalTupleUnion)split_linesnode
NodeOrLeaf
node_typesreturnOptional[BaseNode]c                       | j                   | S )an  
    Recursively looks at the parents of a node and returns the first found node
    that matches ``node_types``. Returns ``None`` if no matching node is found.

    This function is deprecated, use :meth:`NodeOrLeaf.search_ancestor` instead.

    :param node: The ancestors of this node will be checked.
    :param node_types: type names that are searched for.
    )search_ancestor)r
   r   s     1/tmp/pip-target-20744g9h/lib/python/parso/tree.pyr   r      s      4,,    c                      e Zd ZU dZdZeed<   	 ded<   	 d Zd Zd Z	d	 Z
d
 Zedeeef   fd       Zedeeef   fd       Zed        Zed        Zed        Zedd       ZdeddfdZdddeeeef      defdZy)r   z.
    The base class for nodes and leaves.
    parenttyper   r   c                 T    | }|j                   |j                   }|j                   |S )z
        Returns the root node of a parser tree. The returned node doesn't have
        a parent node like all the other nodes/leaves.
        r   )selfscopes     r   get_root_nodezNodeOrLeaf.get_root_node#   s-    
 ll&LLE ll&r   c                     | j                   }|yt        |j                        D ](  \  }}|| u s	 | j                   j                  |dz      c S  y# t        $ r Y  yw xY w)z
        Returns the node immediately following this node in this parent's
        children list. If this node does not have a next sibling, it is None
        N   )r   	enumeratechildren
IndexErrorr   r   ichilds       r   get_next_siblingzNodeOrLeaf.get_next_sibling-   sl    
 > "&//2 	 HAu} ;;//A66	  "   s   A	AAc                     | j                   }|yt        |j                        D ].  \  }}|| u s|dk(  r y| j                   j                  |dz
     c S  y)z
        Returns the node immediately preceding this node in this parent's
        children list. If this node does not have a previous sibling, it is
        None.
        Nr   r   )r   r   r   r    s       r   get_previous_siblingzNodeOrLeaf.get_previous_sibling>   s]     > "&//2 	3HAu}6{{++AE22		3r   c                    | j                   y| }	 |j                   j                  }|j                  |      }|dk(  r|j                   }|j                   
y||dz
     }nO	 	 |j                  d   }# t        $ r |cY S w xY w)z
        Returns the previous leaf in the parser tree.
        Returns `None` if this is the first element in the parser tree.
        Nr   r   )r   r   indexAttributeErrorr   r
   cr!   s       r   get_previous_leafzNodeOrLeaf.get_previous_leafO   s    
 ;;$$AAAv{{;;&Qx  }}R(  " s   "A2 2B ?B c                    | j                   y| }	 |j                   j                  }|j                  |      }|t        |      dz
  k(  r|j                   }|j                   
y||dz      }n[	 	 |j                  d   }# t        $ r |cY S w xY w)z
        Returns the next leaf in the parser tree.
        Returns None if this is the last element in the parser tree.
        Nr   r   )r   r   r(   lenr)   r*   s       r   get_next_leafzNodeOrLeaf.get_next_leafi   s    
 ;;$$AACFQJ{{;;&Qx  }}Q'  " s   .A> >BBr   c                      y)z
        Returns the starting position of the prefix as a tuple, e.g. `(3, 4)`.

        :return tuple of int: (line, column)
        N r   s    r   	start_poszNodeOrLeaf.start_pos       r   c                      y)z
        Returns the end position of the prefix as a tuple, e.g. `(3, 4)`.

        :return tuple of int: (line, column)
        Nr1   r2   s    r   end_poszNodeOrLeaf.end_pos   r4   r   c                      y)a-  
        Returns the start_pos of the prefix. This means basically it returns
        the end_pos of the last prefix. The `get_start_pos_of_prefix()` of the
        prefix `+` in `2 + 1` would be `(1, 1)`, while the start_pos is
        `(1, 2)`.

        :return tuple of int: (line, column)
        Nr1   r2   s    r   get_start_pos_of_prefixz"NodeOrLeaf.get_start_pos_of_prefix   r4   r   c                      y)zO
        Returns the first leaf of a node or itself if this is a leaf.
        Nr1   r2   s    r   get_first_leafzNodeOrLeaf.get_first_leaf   r4   r   c                      y)zN
        Returns the last leaf of a node or itself if this is a leaf.
        Nr1   r2   s    r   get_last_leafzNodeOrLeaf.get_last_leaf   r4   r   c                      y)z
        Returns the code that was the input for the parser for this node.

        :param include_prefix: Removes the prefix (whitespace and comments) of
            e.g. a statement.
        Nr1   r   include_prefixs     r   get_codezNodeOrLeaf.get_code   r4   r   r   c                 ^    | j                   }||j                  |v r|S |j                   }|y)a  
        Recursively looks at the parents of this node or leaf and returns the
        first found node that matches ``node_types``. Returns ``None`` if no
        matching node is found.

        :param node_types: type names that are searched for.
        N)r   r   )r   r   r
   s      r   r   zNodeOrLeaf.search_ancestor   s:     {{yyJ&;;D  r      )indentrC   c          	          |ddn;t        |t              rdd|z  n#t        |t              rd|nt        d|      ddt        dt        dt
        d	t        ffd
 |       S )a  
        Returns a formatted dump of the parser tree rooted at this node or leaf. This is
        mainly useful for debugging purposes.

        The ``indent`` parameter is interpreted in a similar way as :py:func:`ast.dump`.
        If ``indent`` is a non-negative integer or string, then the tree will be
        pretty-printed with that indent level. An indent level of 0, negative, or ``""``
        will only insert newlines. ``None`` selects the single line representation.
        Using a positive integer indent indents that many spaces per level. If
        ``indent`` is a string (such as ``"\t"``), that string is used to indent each
        level.

        :param indent: Indentation style as described above. The default indentation is
            4 spaces, which yields a pretty-printed dump.

        >>> import parso
        >>> print(parso.parse("lambda x, y: x + y").dump())
        Module([
            Lambda([
                Keyword('lambda', (1, 0)),
                Param([
                    Name('x', (1, 7), prefix=' '),
                    Operator(',', (1, 8)),
                ]),
                Param([
                    Name('y', (1, 10), prefix=' '),
                ]),
                Operator(':', (1, 11)),
                PythonNode('arith_expr', [
                    Name('x', (1, 13), prefix=' '),
                    Operator('+', (1, 15), prefix=' '),
                    Name('y', (1, 17), prefix=' '),
                ]),
            ]),
            EndMarker('', (1, 18)),
        ])
        F T z,expect 'indent' to be int, str or None, got r
   rC   	top_levelr   c                    d}t        |       j                  }t        | t              r|| | dz  }t        | t              r|| j
                  dz  }n"t        | t              r|| j                   dz  }|| j                  d| j                  z  }| j                  r|d| j                  z  }|dz  }nt        | t              rc|| | dz  }t        | t              r|| j                   dz  }|dz  }r|dz  }| j                  D ]  }| ||z   d	      z  } || d
z  }nt        d|       |sr|dz  }|S |dz  }|S )NrE   (, z	, prefix=)[
F)rC   rG   z])zunsupported node encountered: z,
)r   __name__
isinstanceLeaf	ErrorLeaf
token_type	TypedLeafvaluer3   prefixBaseNodeNoder   	TypeError)	r
   rC   rG   result	node_typer"   _format_dumpindent_stringnewlines	         r   r[   z%NodeOrLeaf.dump.<locals>._format_dump   s   FT
++I$%VHYKq11dI. 3266Fi0R00FTZZN"T^^,>??;;	$++99F#D(+VHYKq11dD)R00F#dNF!]] bEl5-9O[`aaFbVHB-'  "@ IJJeOF M dNFMr   )rE   T)rO   intstrrX   r   bool)r   rC   r[   r\   r]   s     @@@r   dumpzNodeOrLeaf.dump   s    L >GM$G&LM$G"MJ6*UVV"	z "	3 "	 "	X[ "	H D!!r   NT)rN   
__module____qualname____doc__	__slots__r_   __annotations__r   r#   r%   r,   r/   r   r   r^   r3   r6   r   r8   r:   r<   r@   r   r   r   ra   r1   r   r   r   r      s    I
I ! 
 "3"44 5c?   sCx      
  
  3 3G  ;< V"huS#X7 V" V"r   c            	           e Zd ZU dZdZeed<   ddedeeef   deddfdZ	e
deeef   fd	       Zej                  deeef   ddfd
       Zd Zd Zd ZddZe
deeef   fd       Zd Zy)rP   z
    Leafs are basically tokens with a better API. Leafs exactly know where they
    were defined and what text preceeds them.
    )rT   linecolumnrU   rU   rT   r3   r   Nc                 @    || _         	 || _        || _        	 d | _        y N)rT   r3   rU   r   )r   rT   r3   rU   s       r   __init__zLeaf.__init__#  s1    
	 #	 +/	r   c                 2    | j                   | j                  fS rl   ri   rj   r2   s    r   r3   zLeaf.start_pos3  s    yy$++%%r   c                 ,    |d   | _         |d   | _        y )Nr   r   ro   r   rT   s     r   r3   zLeaf.start_pos7  s    !H	Ahr   c                     | j                         }|2t        | j                        }| j                  t	        |      z
  dz   dfS |j
                  S )Nr   r   )r,   r	   rU   ri   r.   r6   )r   previous_leafliness      r   r8   zLeaf.get_start_pos_of_prefix<  sM    ..0 ,E99s5z)A-q00$$$r   c                     | S rl   r1   r2   s    r   r:   zLeaf.get_first_leafD      r   c                     | S rl   r1   r2   s    r   r<   zLeaf.get_last_leafG  rv   r   c                 P    |r| j                   | j                  z   S | j                  S rl   )rU   rT   r>   s     r   r@   zLeaf.get_codeJ  s"    ;;++::r   c                     t        | j                        }| j                  t        |      z   dz
  }| j                  |k(  r| j                  t        |d         z   }||fS t        |d         }||fS )Nr   r'   )r	   rT   ri   r.   rj   )r   rt   end_pos_lineend_pos_columns       r   r6   zLeaf.end_posP  sp    DJJ'yy3u:-199$![[3uRy>9N ^++ !r^N^++r   c                 n    | j                   }|s| j                  }dt        |       j                  d|dS )N<: >)rT   r   rN   rq   s     r   __repr__zLeaf.__repr__[  s-    

IIE!$Z00%88r   rE   rb   )rN   rc   rd   re   rf   r_   rg   r   r^   rm   propertyr3   setterr8   r:   r<   r@   r6   r   r1   r   r   rP   rP     s     6IKc eCHo s TX   &5c? & & uS#X 4  % ,sCx , ,9r   rP   c                   $     e Zd ZdZd fd	Z xZS )rS   r   c                 6    t         |   |||       || _        y rl   superrm   r   )r   r   rT   r3   rU   	__class__s        r   rm   zTypedLeaf.__init__e  s    	62	r   r   )rN   rc   rd   rf   rm   __classcell__r   s   @r   rS   rS   b  s    I r   rS   c                       e Zd ZdZdZddZedeeef   fd       Z	d Z
edeeef   fd       Zd	 Zdd
ZddZd Zd Zd Zy)rV   zd
    The super class for all nodes.
    A node has children, a type and possibly a parent node.
    )r   r   Nc                 @    || _         	 d | _        	 |D ]	  }| |_         y rl   )r   r   )r   r   r"   s      r   rm   zBaseNode.__init__q  s4     	 +/	  	 EEL	 r   c                 4    | j                   d   j                  S Nr   )r   r3   r2   s    r   r3   zBaseNode.start_pos~  s    }}Q)))r   c                 <    | j                   d   j                         S r   )r   r8   r2   s    r   r8   z BaseNode.get_start_pos_of_prefix  s    }}Q7799r   c                 4    | j                   d   j                  S Nr'   )r   r6   r2   s    r   r6   zBaseNode.end_pos  s    }}R (((r   c                     |rdj                  d |D              S |d   j                  d      }|dj                  d |dd  D              z   S )NrE   c              3   <   K   | ]  }|j                           y wrl   r@   .0r+   s     r   	<genexpr>z2BaseNode._get_code_for_children.<locals>.<genexpr>  s     :A1::<:   r   F)r?   c              3   <   K   | ]  }|j                           y wrl   r   r   s     r   r   z2BaseNode._get_code_for_children.<locals>.<genexpr>  s     "FA1::<"Fr   r   )joinr@   )r   r   r?   firsts       r   _get_code_for_childrenzBaseNode._get_code_for_children  sQ    77::::QK(((>E277"F!""FFFFr   c                 :    | j                  | j                  |      S rl   )r   r   r>   s     r   r@   zBaseNode.get_code  s    **4==.IIr   c                       fddcxk  r& j                   d   j                  k  st        d       t        d       dt         j                         dz
        S )ax  
        Get the :py:class:`parso.tree.Leaf` at ``position``

        :param tuple position: A position tuple, row, column. Rows start from 1
        :param bool include_prefixes: If ``False``, ``None`` will be returned if ``position`` falls
            on whitespace or comments before a leaf
        :return: :py:class:`parso.tree.Leaf` at ``position``, or ``None``
        c                     | |k(  r4j                   |    }s|j                  k  ry 	 |j                        S t	        | |z   dz        }j                   |   }|j
                  k  r	 | |      S  |dz   |      S # t        $ r |cY S w xY w)N   r   )r   r3   get_leaf_for_positionr)   r^   r6   )lowerupperelementr(   binary_searchinclude_prefixespositionr   s       r   r   z5BaseNode.get_leaf_for_position.<locals>.binary_search  s    ~--.'Hw7H7H,H#"88CSTT !+,EmmE*G7??*$UE22$UQY66 & #"N#s   A? ?BB)r   r   r'   z7Please provide a position that exists within this node.r   r   )r   r6   
ValueErrorr.   )r   r   r   r   s   ```@r   r   zBaseNode.get_leaf_for_position  s_    	7& (?dmmB&7&?&??VWW @VWWQDMM 2Q 677r   c                 <    | j                   d   j                         S r   )r   r:   r2   s    r   r:   zBaseNode.get_first_leaf  s    }}Q..00r   c                 <    | j                   d   j                         S r   )r   r<   r2   s    r   r<   zBaseNode.get_last_leaf  s    }}R ..00r   c           	          | j                         j                  dd      j                  dd      j                         }dt        |       j                  d|d| j
                  d   d| j
                  d	   d
	S )NrM   rF   r}   r~   @r   ,r   r   )r@   replacestripr   rN   r3   )r   codes     r   r   zBaseNode.__repr__  sa    }}&&tS199$DJJL$Z  $q(94>>!;LN 	Nr   )r   Nrb   )F)rN   rc   rd   re   rf   rm   r   r   r^   r3   r8   r6   r   r@   r   r:   r<   r   r1   r   r   rV   rV   j  s     I  *5c? * *: )sCx ) )GJ8@11Nr   rV   c                   ,     e Zd ZdZdZ fdZd Z xZS )rW   z+Concrete implementation for interior nodes.r   c                 2    t         |   |       || _        y rl   r   )r   r   r   r   s      r   rm   zNode.__init__  s    "	r   c                 h    | j                   j                  d| j                  d| j                  dS )NrI   rJ   rK   )r   rN   r   r   r2   s    r   r   zNode.__repr__  s!    #~~66		4==QQr   )rN   rc   rd   re   rf   rm   r   r   r   s   @r   rW   rW     s    5IRr   rW   c                       e Zd ZdZdZdZy)	ErrorNodez
    A node that contains valid nodes/leaves that we're follow by a token that
    was invalid. This basically means that the leaf after this node is where
    Python would mark a syntax error.
    r1   
error_nodeN)rN   rc   rd   re   rf   r   r1   r   r   r   r     s    
 IDr   r   c                   2     e Zd ZdZdZdZd fd	Zd Z xZS )rQ   z
    A leaf that is either completely invalid in a language (like `$` in Python)
    or is invalid at that position. Like the star in `1 +* 1`.
    )rR   
error_leafc                 6    t         |   |||       || _        y rl   )r   rm   rR   )r   rR   rT   r3   rU   r   s        r   rm   zErrorLeaf.__init__  s    	62$r   c           	          dt        |       j                  d| j                  dt        | j                        d| j
                  d	S )Nr}   r~   :rJ   r   )r   rN   rR   reprrT   r3   r2   s    r   r   zErrorLeaf.__repr__  s2    $Z  $//4

3CT^^U 	Ur   r   )	rN   rc   rd   re   rf   r   rm   r   r   r   s   @r   rQ   rQ     s      ID%Ur   rQ   N)abcr   r   typingr   r   r   r   parso.utilsr	   r_   r   r   rP   rS   rV   rW   r   rQ   r1   r   r   <module>r      s    0 / / #
-, 
-S 
-=Q 
-D" D"ND9: D9N RNz RNj	R8 	R U Ur   