
    `N`3:                        d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ dd
lmZ d Z eej        ej        ej        ej        g          Z G d de
j                  ZddddZd Zd Zd Zd Zd Zd ZdS )ag  PyTreeUnwrapper - produces a list of unwrapped lines from a pytree.

[for a description of what an unwrapped line is, see unwrapped_line.py]

This is a pytree visitor that goes over a parse tree and produces a list of
UnwrappedLine containers from it, each with its own depth and containing all
the tokens that could fit on the line if there were no maximal line-length
limitations.

Note: a precondition to running this visitor and obtaining correct results is
for the tree to have its comments spliced in as nodes. Prefixes are ignored.

For most uses, the convenience function UnwrapPyTree should be sufficient.
    )pytree)token)format_token)object_state)pytree_utils)pytree_visitor)split_penalty)style)unwrapped_linec                     t                      }|                    |            |                                }|                    d            |S )zCreate and return a list of unwrapped lines from the given pytree.

  Arguments:
    tree: the top-level pytree node to unwrap.

  Returns:
    A list of UnwrappedLine objects.
  c                     | j         S N)lineno)xs    =lib/python3.11/site-packages/yapf/yapflib/pytree_unwrapper.py<lambda>zUnwrapPyTree.<locals>.<lambda>7   s    QX     )key)PyTreeUnwrapperVisitGetUnwrappedLinessort)tree	unwrapperuwliness      r   UnwrapPyTreer   +   sO     )//$''))'	,,%%,&&&	.r   c                      e Zd ZdZd Zd Zd Z eh d          Zd Z	d Z
 eh d          Zd	 Z ed
dh          Zd Z eddh          Zd Z eh d          Zd Z edh          Zd Z edh          Zd Zd Z edh          Zd Zd Zd Zd Zd Z edh          Zd Zd Z d Z!d  Z"d! Z#d" Z$d# Z%d$ Z&d% Z'd&S )'r   a  PyTreeUnwrapper - see file-level docstring for detailed description.

  Note: since this implements PyTreeVisitor and node names in lib2to3 are
  underscore_separated, the visiting methods of this class are named as
  Visit_node_name. invalid-name pragmas are added to each such method to silence
  a style warning. This is forced on us by the usage of lib2to3, and re-munging
  method names to make them different from actual node names sounded like a
  confusing and brittle affair that wasn't worth it for this small & controlled
  deviation from the style guide.

  To understand the connection between visitor methods in this class, some
  familiarity with the Python grammar is required.
  c                 T    g | _         t          j        d          | _        d| _        d S )Nr   )_unwrapped_linesr   UnwrappedLine_cur_unwrapped_line
_cur_depthselfs    r   __init__zPyTreeUnwrapper.__init__Q   s,    D  .;A>>D DOOOr   c                 8    |                                   | j        S )zFetch the result of the tree walk.

    Note: only call this after visiting the whole tree.

    Returns:
      A list of UnwrappedLine objects.
    )_StartNewLiner   r#   s    r   r   z!PyTreeUnwrapper.GetUnwrappedLines\   s     	  r   c                    | j         j        r[| j                            | j                    t	          | j                    t          | j                    t          | j                    t          j        | j	                  | _         dS )zFinish current line and start a new one.

    Place the currently accumulated line into the _unwrapped_lines list and
    start a new one.
    N)
r!   tokensr   append_MatchBrackets_IdentifyParameterLists_AdjustSplitPenaltyr   r    r"   r#   s    r   r'   zPyTreeUnwrapper._StartNewLineh   sz     & 4
""4#;<<<T-...d6777$2333-;DOLLDr   >   funcdefif_stmtclassdeffor_stmttry_stmt	with_stmt
while_stmtexpect_clausec                 &   |j         ot          j        |j                   | j        v }t          j        |          }|r|s| xj        dz  c_        |                                  |                     |           |r|s| xj        dz  c_        d S d S d S N   )parentr   NodeName_STMT_TYPESIsCommentStatementr"   r'   DefaultNodeVisit)r$   nodesingle_stmt_suiteis_comment_stmts       r   Visit_simple_stmtz!PyTreeUnwrapper.Visit_simple_stmt   s     	N-dk::d>NN "5d;;O  
oooo$  
oooooo   r   c                     |j         D ]I}|j        t          j        k    r|j        |v r|                                  |                     |           JdS )a  Helper for visiting compound statements.

    Python compound statements serve as containers for other statements. Thus,
    when we encounter a new compound statement we start a new unwrapped line.

    Arguments:
      node: the node to visit.
      substatement_names: set of node names. A compound statement will be
        recognized as a NAME node with a name in this set.
    N)childrentypegrammar_tokenNAMEvaluer'   r   )r$   r>   substatement_nameschilds       r   _VisitCompoundStatementz'PyTreeUnwrapper._VisitCompoundStatement   sf      	 	 **
*
*
++
+
+
jj	 	r   >   ifelifelsec                 <    |                      || j                   d S r   )rJ   _IF_STMT_ELEMSr$   r>   s     r   Visit_if_stmtzPyTreeUnwrapper.Visit_if_stmt   s!      t':;;;;;r   whilerM   c                 <    |                      || j                   d S r   )rJ   _WHILE_STMT_ELEMSrP   s     r   Visit_while_stmtz PyTreeUnwrapper.Visit_while_stmt   s!      t'=>>>>>r   forc                 <    |                      || j                   d S r   )rJ   _FOR_STMT_ELEMSrP   s     r   Visit_for_stmtzPyTreeUnwrapper.Visit_for_stmt   !      t';<<<<<r   >   tryrM   exceptfinallyc                 <    |                      || j                   d S r   )rJ   _TRY_STMT_ELEMSrP   s     r   Visit_try_stmtzPyTreeUnwrapper.Visit_try_stmt   rZ   r   r\   c                 <    |                      || j                   d S r   )rJ   _EXCEPT_STMT_ELEMSrP   s     r   Visit_except_clausez#PyTreeUnwrapper.Visit_except_clause   s!      t'>?????r   defc                 <    |                      || j                   d S r   )rJ   _FUNC_DEF_ELEMSrP   s     r   Visit_funcdefzPyTreeUnwrapper.Visit_funcdef   rZ   r   c                    |                                   d}|j        D ]6}|dz  }|                     |           t          j        |          dk    r n7|j        |         j        D ]}|                     |           d S )Nr   r8   ASYNC)r'   rC   r   r   r:   r$   r>   indexrI   s       r   Visit_async_funcdefz#PyTreeUnwrapper.Visit_async_funcdef   s    E  qje
jj		u	%	%	0	0 
1u%.  
jj r   classc                 <    |                      || j                   d S r   )rJ   _CLASS_DEF_ELEMSrP   s     r   Visit_classdefzPyTreeUnwrapper.Visit_classdef   !      t'<=====r   c                 p   |                                   d}|j        D ]6}|dz  }|                     |           t          j        |          dk    r n7|j        |         j        D ]N}t          j        |          dk    r|j        dk    r|                                   |                     |           Od S )Nr   r8   ri   rF   rM   )r'   rC   r   r   r:   rG   rj   s       r   Visit_async_stmtz PyTreeUnwrapper.Visit_async_stmt   s    E  qje
jj		u	%	%	0	0 
1u%.  		u	%	%	/	/EK64I4I
jj r   c                     |j         D ]T}|                     |           t          j        |          dk    r%||j         d         k    r|                                  Ud S )NCOMMENTr   )rC   r   r   r:   r'   r$   r>   rI   s      r   Visit_decoratorzPyTreeUnwrapper.Visit_decorator   sj      
jj


&
&)
3
3
4=#
#
#	 r   c                 l    |j         D ]+}|                                  |                     |           ,d S r   rC   r'   r   rv   s      r   Visit_decoratorsz PyTreeUnwrapper.Visit_decorators   D      

jj r   c                 l    |j         D ]+}|                                  |                     |           ,d S r   ry   rv   s      r   Visit_decoratedzPyTreeUnwrapper.Visit_decorated   r{   r   withc                 <    |                      || j                   d S r   )rJ   _WITH_STMT_ELEMSrP   s     r   Visit_with_stmtzPyTreeUnwrapper.Visit_with_stmt   rq   r   c                     | xj         dz  c_         |                                  |                     |           | xj         dz  c_         d S r7   )r"   r'   r=   rP   s     r   Visit_suitezPyTreeUnwrapper.Visit_suite   sL    OOqOO$OOqOOOOr   c                 N    t          |           |                     |           d S r   _DetermineMustSplitAnnotationr=   rP   s     r   Visit_listmakerzPyTreeUnwrapper.Visit_listmaker  *    !$'''$r   c                 N    t          |           |                     |           d S r   r   rP   s     r   Visit_dictsetmakerz"PyTreeUnwrapper.Visit_dictsetmaker  r   r   c                 n    |j         j        dk    rt          |           |                     |           d S )N()prev_siblingrG   r   r=   rP   s     r   Visit_import_as_namesz%PyTreeUnwrapper.Visit_import_as_names  s;    #%%#D)))$r   c                 N    t          |           |                     |           d S r   r   rP   s     r   Visit_testlist_gexpz#PyTreeUnwrapper.Visit_testlist_gexp  r   r   c                 N    t          |           |                     |           d S r   r   rP   s     r   Visit_arglistzPyTreeUnwrapper.Visit_arglist  r   r   c                 N    t          |           |                     |           d S r   r   rP   s     r   Visit_typedargslistz#PyTreeUnwrapper.Visit_typedargslist  r   r   c                     |j         t          v r|                                  dS |j         t          j        k    s|j                                        r| j                            |           dS dS )zDefault visitor for tree leaves.

    A tree leaf is always just gets appended to the current unwrapped line.

    Arguments:
      leaf: the leaf to visit.
    N)	rD   _WHITESPACE_TOKENSr'   rE   ru   rG   stripr!   
AppendNode)r$   leafs     r   DefaultLeafVisitz PyTreeUnwrapper.DefaultLeafVisit  sr     y&&&
	m+	+	+tz/?/?/A/A	+
))$///// 
,	+r   N)(__name__
__module____qualname____doc__r%   r   r'   	frozensetr;   rA   rJ   rO   rQ   rT   rU   rX   rY   r_   r`   rb   rc   rf   rg   rl   ro   rp   rs   rw   rz   r}   r   r   r   r   r   r   r   r   r   r    r   r   r   r   B   sw        	 	 	
! 
! 
!M M M 	 	 	 	 	 	+  *  , 933344.< < <  i& 122? ? ? Iufo../= = = IBBBCC/= = = !y(,,@ @ @ Iug&&/= = =	 	 	 Yy))> > >      
  
 Yx((> > >                 
               0 0 0 0 0r   r   r   {[))}]c                    g }| j         D ]}|j        t          j        v r|                    |           nA|j        t          j        v r.||d         _        |d         |_        |                                 |D ]_}t          t          j	        |j
                            t          |j
                  k    r!|j                            |           ||_        `dS )a  Visit the node and match the brackets.

  For every open bracket ('[', '{', or '('), find the associated closing bracket
  and "match" them up. I.e., save in the token a pointer to its associated open
  or close bracket.

  Arguments:
    uwline: (UnwrappedLine) An unwrapped line.
  N)r)   rG   r   OPENING_BRACKETSr*   CLOSING_BRACKETSmatching_bracketpopidGetOpeningBracketr>   container_elementscontainer_opening)uwlinebracket_stackr   brackets       r   r+   r+   /  s     -} * *e{l3335!!!!	5	5	5+0mB(,R0e  * *	L*5:66	7	72gl;K;K	K	K"))%000")** *r   c                 2   g }g }| j         D ]	}t          j        j        |j        v r-|j        j        dk    sJ |                    |j                   H|r1|j        dk    r&||d         j        k    r|	                                 {t          j        j
        |j        v r|                    |           |r_t          j        j        |j        v rG|	                                }|d         j                            t          j        ||                     dS )zVisit the node to create a state for parameter lists.

  For instance, a parameter is considered an "object" with its first and last
  token uniquely identifying the object.

  Arguments:
    uwline: (UnwrappedLine) An unwrapped line.
  r   r   r   N)r)   r   SubtypeFUNC_DEFsubtypes
next_tokenrG   r*   r   r   PARAMETER_STARTPARAMETER_STOP
parametersr   	Parameter)r   
func_stackparam_stacktokstarts        r   r,   r,   H  s&    *+] K Kc$44^!S((((''' ci3&&	
2/	/	/ +s|;;  K|+:clJJooen&&|'=eS'I'IJJJ'K Kr   c                    d}t          | j                  D ]n\  }}|r6|s4t          j        |j        t          j        j        t          j                   |j	        t          j
        v r|dz  }V|j	        t          j        v r|dz  }odS )zVisit the node and adjust the split penalties if needed.

  A token shouldn't be split if it's not within a bracket pair. Mark any token
  that's not within a bracket pair as "unbreakable".

  Arguments:
    uwline: (UnwrappedLine) An unwrapped line.
  r   r8   N)	enumerater)   r   SetNodeAnnotationr>   
AnnotationSPLIT_PENALTYr	   UNBREAKABLErG   r   r   )r   bracket_levelrk   r   s       r   r-   r-   i  s     -..  leU @] @$UZ%1%<%J%2%>@ @ @ {l333qmm	5	5	5qm r   c                    t          j        d          rdS t          |           st          | j                                                  }|j        dk    r$t          d | j        D                       dk     rdS t          | j        d         t          j                  r| j        d         j        dk    rdS t          | j                  }d}t          | j        d                    ||d	z
  k     r| j        |         }t          |t          j                  r]|j        dk    rR| j        |d	z            }|j        t          j        k    r|d	z  }||d	z
  k    rdS t          | j        |d	z                       |d	z  }||d	z
  k     dS dS )
z:Enforce a split in the list if the list ends with a comma.DISABLE_ENDING_COMMA_HEURISTICNr   c              3   J   K   | ]}t          j        |          d k    dV  dS )COMMAr8   N)r   r:   ).0chs     r   	<genexpr>z0_DetermineMustSplitAnnotation.<locals>.<genexpr>  sC       6 62%b))W44 44446 6r      r   ,r   r8   )r
   Get_ContainsCommentsnextr9   leavesrG   sumrC   
isinstancer   Leaflen_SetMustSplitOnFirstLeafrD   rE   ru   )r>   r   num_childrenrk   rI   
next_childs         r   r   r   ~  s   
Y/00 
F	4	 	  ##%%&&E{c	 6 6 6 6 6 
6 
689
: 
:t}R(&+66 b3&&fT]##,
%4=+,,,q   M% E%%% 9%+*<*<=+j	M1	1	1
L1$$$
%t}UQY7888	QJE 	q       r   c                     t          | t          j                  r| j        t          j        k    S | j        D ]}t          |          r dS dS )z,Return True if the list has a comment in it.TF)r   r   r   rD   rE   ru   rC   r   )r>   rI   s     r   r   r     sX    fk"" .9---}  e TT	r   c                 t    t          j        t          j        |           t           j        j        d           dS )z7Set the "must split" annotation on the first leaf node.TN)r   r   FirstLeafNoder   
MUST_SPLIT)r>   s    r   r   r     s:      &&(?(J
    r   N)r   lib2to3r   lib2to3.pgen2r   rE   yapf.yapflibr   r   r   r   r	   r
   r   r   r   NEWLINEDEDENTINDENT	ENDMARKERr   PyTreeVisitorr   _BRACKET_MATCHr+   r,   r-   r   r   r   r   r   r   <module>r      s   "       0 0 0 0 0 0 % % % % % % % % % % % % % % % % % % ' ' ' ' ' ' & & & & & &       ' ' ' ' ' '  " Y=/1E    g0 g0 g0 g0 g0n2 g0 g0 g0T 3//* * *2K K KB  *  8      r   