a
    
d;                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ dd Zdd	d
Z	e
g dZdd Ze
dgeB Zdd Zdd Zdd ZdS )ap  Comment splicer for lib2to3 trees.

The lib2to3 syntax tree produced by the parser holds comments and whitespace in
prefix attributes of nodes, rather than nodes themselves. This module provides
functionality to splice comments out of prefixes and into nodes of their own,
making them easier to process.

  SpliceComments(): the main function exported by this module.
    )pygram)pytree)token)pytree_utilsc                    s(   dgt |   fdd  |  dS )a4  Given a pytree, splice comments into nodes of their own right.

  Extract comments from the prefixes where they are housed after parsing.
  The prefixes that previously housed the comments become empty.

  Args:
    tree: a pytree.Node - the tree to work on. The tree is modified by this
        function.
  Nc              
      s  | j dd D ]}t|tjr* | q|j dr|j}|j|d }|j	}|jd}|d|
d }d|v r||dd d }d|_|jtjkr|t| 8 }tt|||ddd  n|jtjkrg }d}|dD ]t}|
d}	|	dk r$|du rX|d7 }qn4|du s8|	|k rX|	}|d| }
|||
g f |d	 d	 | q|D ]^\}}
}t||
}|jtjkrtj}ntj}|td|d ||d
d| |t|7 }qpnt|}| D ]}|jtjkr qqt|t|krxt|}t|jdv rV||dd }||jd k rV|j}tt||dd
d|  qq|d jkr0|  }|d  }|dr0d j	}|td j!7 }|t|d t|d   7 }tj"tj#|dd||ffd}t|gd  d|dd }|d7 }d| vrBdn| $dd }t||d t||d   }t|||dd}t||  qq|d< qdS )z<Recursively visit each node to splice comments into the AST.N#
    F)
standaloner   T>   classdeffuncdeftypevaluecontext)%children
isinstancer   Nodeprefixlstrip
startswithlinenocountcolumnfindrfindr   r   NEWLINElenr   ZInsertNodesAfter_CreateCommentsFromPrefixDEDENTsplitappend_FindAncestorAtIndentZInsertNodesBeforejoin_FindStmtParentZleavesid!_FindNodeWithStandaloneLineParentNodeNameparentrstrip
splitlinesr   LeafCOMMENTrindex)nodechildcomment_prefixcomment_linenocomment_columnZchild_prefixZprefix_indentZcomment_groupsZcmtcolZcomment_indentZcomment_groupZancestor_at_indentZInsertNodesZstmt_parentZleaf_in_parentZnode_with_line_parentZcomment_endZcomment_linesr   comment_leafr.   comments_VisitNodeRecZ	prev_leaf d/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/yapf/pytree/comment_splicer.pyr8   .   s    








z%SpliceComments.<locals>._VisitNodeRec)_AnnotateIndents)treer9   r7   r:   SpliceComments   s
     r=   Fc                 C   s   g }|  d}d}|t|k rg }|t|k r\||  dr\|||   |d7 }q"|r|| d }|d  |d< |d  |d< tjtj	d
|d||ffd}	|s|	nttjj|	g}
||
 |t|k r||  s|d7 }qq|S )aG  Create pytree nodes to represent the given comment prefix.

  Args:
    comment_prefix: (unicode) the text of the comment from the node's prefix.
    comment_lineno: (int) the line number for the start of the comment.
    comment_column: (int) the column for the start of the comment.
    standalone: (bool) determines if the comment is standalone or not.

  Returns:
    The simple_stmt nodes if this is a standalone comment, otherwise a list of
    new COMMENT leafs. The prefix may consist of multiple comment blocks,
    separated by blank lines. Each block gets its own leaf.
  r   r   r   r   r   r	   r   )r!   r   r   r   r"   stripr   r,   r   r-   r$   r   r   Zpython_symbolssimple_stmt)r1   r2   r3   r
   r6   linesindexZcomment_blockZ
new_linenor5   Zcomment_noder9   r9   r:   r      s0    




r   )
ZsuiteZif_stmtZ
while_stmtZfor_stmtZtry_stmtZ	with_stmtr   r   Z	decoratedZ
file_inputc                 C   s"   t | jtv r| S t| jS dS )zFind a node whose parent is a 'standalone line' node.

  See the comment above _STANDALONE_LINE_NODES for more details.

  Arguments:
    node: node to start from

  Returns:
    Suitable node that's either the node itself or one of its ancestors.
  N)r   r(   r)   _STANDALONE_LINE_NODESr'   r/   r9   r9   r:   r'     s    r'   r?   c                 C   s    t | tv r| S t| jS dS )zFind the nearest parent of node that is a statement node.

  Arguments:
    node: node to start from

  Returns:
    Nearest parent (or node itself, if suitable).
  N)r   r(   _STATEMENT_NODESr%   r)   rC   r9   r9   r:   r%   '  s    	r%   c                 C   sH   | j j du r| S t| j tjj}|dur8||r8| S t| j |S dS )a  Find an ancestor of node with the given indentation.

  Arguments:
    node: node to start from. This must not be the tree root.
    indent: indentation string for the ancestor we're looking for.
        See _AnnotateIndents for more details.

  Returns:
    An ancestor node with suitable indentation. If no suitable ancestor is
    found, the closest ancestor to the tree root is returned.
  N)r)   r   GetNodeAnnotation
AnnotationCHILD_INDENTr   r#   )r/   indentZparent_indentr9   r9   r:   r#   6  s    
r#   c                 C   s   | j du rt| tjjd | jD ]\}|jtjkrvt	| tjj}|durb||j
krbtd| |ft| tjj|j
 t| q"dS )ae  Annotate the tree with child_indent annotations.

  A child_indent annotation on a node specifies the indentation (as a string,
  like "  ") of its children. It is inferred from the INDENT child of a node.

  Arguments:
    tree: root of a pytree. The pytree is modified to add annotations to nodes.

  Raises:
    RuntimeError: if the tree is malformed.
  Nr	   z"inconsistent indentation for child)r)   r   ZSetNodeAnnotationrF   rG   r   r   r   INDENTrE   r   RuntimeErrorr;   )r<   r0   Zchild_indentr9   r9   r:   r;   U  s    

r;   N)F)__doc__Zyapf_third_party._ylib2to3r   r   Z yapf_third_party._ylib2to3.pgen2r   Zyapf.pytreer   r=   r   	frozensetrB   r'   rD   r%   r#   r;   r9   r9   r9   r:   <module>   s   
 3 
;