
    `N`;                         d Z ddlmZ ddlmZ ddlmZ ddlmZ d Z	 ddZ	 e
g d	          Zd
 Z e
dg          ez  Zd Zd Z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                 P    dgt          |            f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           
      
   | j         dd         D ]P}t          |t          j                  r |           )|j                                                            d          r|j        }|j        |                    d          z
  }|j	        }|j                            d          }|d|
                    d                   }d|v r ||                    d          dz   d         }d|_        |j        t          j        k    rR|t          |                                          z  }t!          j        t%          |||d          d                    n|j        t          j        k    rg }d}|                    d          D ]s}|
                    d          }	|	dk     r	||dz  }%n,||	|k     r$|	}|d|         }
|                    ||
g f           |d	         d	                             |           t|D ]\  }}
}t-          ||
          }|j        t          j        k    rt           j        }nt           j        } |t%          d                    |          dz   ||d
          |           |t          |          z  }nt3          |          }|                                D ]}|j        t          j        k    rt7          |          t7          |          k    rt9          |          }t!          j        |j                  dv r@||                    d                              d          z   }||j        dz
  k     r|j        }t!          j        t%          ||dd
          |            n|d         j        k    r#|                                 }|d                                         }|                    d          rd         j	        }|t          d         j!                  z  }|t          |d                   t          |d                                                   z
  z  }t          j"        t          j#        |                    d          d||ff          }t!          j        |gd                    d                    |dd                   }|dz  }d|                                vrdn)|                                $                    d          dz   }t          ||d                   t          ||d                                                   z
  }t%          |||d          }t!          j        ||            |d<   RdS )z<Recursively visit each node to splice comments into the AST.N#
    F)
standaloner   T>   funcdefclassdeftypevaluecontext)%children
isinstancer   Nodeprefixlstrip
startswithlinenocountcolumnfindrfindr   r   NEWLINElenr   InsertNodesAfter_CreateCommentsFromPrefixDEDENTsplitappend_FindAncestorAtIndentInsertNodesBeforejoin_FindStmtParentleavesid!_FindNodeWithStandaloneLineParentNodeNameparentrstrip
splitlinesr   LeafCOMMENTrindex)nodechildcomment_prefixcomment_linenocomment_columnchild_prefixprefix_indentcomment_groupscmtcolcomment_indentcomment_groupancestor_at_indentInsertNodesstmt_parentleaf_in_parentnode_with_line_parentcomment_endcomment_linesr   comment_leafr4   comments_VisitNodeRec	prev_leafs                          <lib/python3.11/site-packages/yapf/yapflib/comment_splicer.pyrJ   z%SpliceComments.<locals>._VisitNodeRec.   s    qqq! X X	E6;	'	' We<  ++C00 Q	 <. <.*>*>t*D*DD. <.
 ,,T22,&'>(9(9#(>(>'>?-]"")-*=*=d*C*Ca*G*H*HIM%,Z5=(( c."7"7"9"9:::N))"""$	& & & (1|5 5 5 5 zU\)),  N!N%++D11 1 1HHSMMcq!) A%. * ")S>-A-A!$!$_n_!5%%~~r&JKKKR $++C0000 BP 3 3=#8#O#O #(EL88*<*;k+ii..5$$!%	' ' ' );< < < M 2 22nn32 *%00K"-"4"4"6"6 4 4$55n%%E22
 )J%(P(P%()02 25LM M
 %~'<'<T'B'B'H'H'N'NN  #8#?!#CCC,A,H).-&dL L L)+ + + !Yq\%888"0";";"="=-'*1133%\\$'' (%.q\%8N"c)A,*<&=&==N"M!,--M!4D4K4K4M4M0N0NNPN#);"]#ll400!#nn%E F$H $H $HL !1<.)A,OOO%)YY}QRR/@%A%AN"a'N#>+@+@+B+BBB!!(//1188>>B  vww/00vww/668899:  5"""$	& & &
 .x???	!qX X    )_AnnotateIndents)treerJ   rK   s    @@rL   SpliceCommentsrP      sX     f)4[ [ [ [ [ [z -rM   Fc                 "   g }|                      d          }d}|t          |          k     rg }|t          |          k     r||                                                             d          rr|                    ||                                                    |dz  }|t          |          k     r-||                                                             d          r|r||z   dz
  }|d                                         |d<   |d                                         |d<   t          j        t          j	        d
                    |          d||ff          }	|s|	n$t          j        t          j        j        |	g          }
|                    |
           |t          |          k     rL||                                         s2|dz  }|t          |          k     r||                                         2|t          |          k     |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   r2   r   r3   r)   r   r   python_symbolssimple_stmt)r7   r8   r9   r   rI   linesindexcomment_block
new_linenorH   comment_nodes              rL   r#   r#      s   ( (


t
$
$%
%E

M
#e**

u!4!4!6!6!A!A#!F!F
5<--//000qje #e**

u!4!4!6!6!A!A#!F!F
  
$!E)A-j&q)//11mA'+1133mB[}		-((
N346 6 6l *4 =\\


+l^:= :=lool###
#e**

U5\%8%8%:%:
qje #e**

U5\%8%8%:%:
% 	E

* 
/rM   )
suiteif_stmt
while_stmtfor_stmttry_stmt	with_stmtr   r   	decorated
file_inputc                 n    t          j        | j                  t          v r| S t	          | j                  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.
  )r   r.   r/   _STANDALONE_LINE_NODESr-   r5   s    rL   r-   r-     s4     4;''+AAAK -T[999rM   rT   c                 d    t          j        |           t          v r| S t          | j                  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).
  )r   r.   _STATEMENT_NODESr*   r/   rd   s    rL   r*   r*   '  s0     4  $444K4;'''rM   c                     | j         j         | S t          j        | j         t          j        j                  }||                    |          r| S t          | j         |          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.
  )r/   r   GetNodeAnnotation
AnnotationCHILD_INDENTr   r'   )r5   indentparent_indents      rL   r'   r'   6  sg     
[K 0
k<*79 9-6#4#4]#C#CK !f555rM   c                    | j         %t          j        | t          j        j        d           | j        D ]}|j        t          j        k    rmt          j	        | t          j        j                  }|||j
        k    rt          d| |f          t          j        | t          j        j        |j
                   t          |           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   SetNodeAnnotationri   rj   r   r   r   INDENTrh   r   RuntimeErrorrN   )rO   r6   child_indents      rL   rN   rN   U  s     
["4)@)M#%' ' '}  ezU\!!!3
'46 6l		!lek&A&A?$OOO$T<+B+O%*[2 2 2U rM   N)F)__doc__lib2to3r   r   lib2to3.pgen2r   yapf.yapflibr   rP   r#   	frozensetrc   r-   rf   r*   r'   rN    rM   rL   <module>rx      s	                      % % % % % %l l ld */- - - -v # $ $ $   : : :* 9m_--0FF ( ( (6 6 6>    rM   