
    `N`C0                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ ej	        Z
 G d de          Zd	 Z G d
 de          ZdS )zcPytree nodes with extra formatting information.

This is a thin wrapper around a pytree.Leaf node.
    N)token)	py3compat)pytree_utils)stylec                   ~    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d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dZdZdZdZdS )SubtypezkSubtype information about tokens.

  Gleaned from parsing the code. Helps determine the best formatting.
  r                           	   
                                                   N)__name__
__module____qualname____doc__NONEUNARY_OPERATORBINARY_OPERATORA_EXPR_OPERATORM_EXPR_OPERATORSUBSCRIPT_COLONSUBSCRIPT_BRACKETDEFAULT_OR_NAMED_ASSIGN DEFAULT_OR_NAMED_ASSIGN_ARG_LISTVARARGS_LISTVARARGS_STARKWARGS_STAR_STARASSIGN_OPERATORDICTIONARY_KEYDICTIONARY_KEY_PARTDICTIONARY_VALUEDICT_SET_GENERATOR	COMP_EXPRCOMP_FORCOMP_IFFUNC_DEF	DECORATOR
TYPED_NAMETYPED_NAME_ARG_LISTSIMPLE_EXPRESSIONPARAMETER_STARTPARAMETER_STOP     9lib/python3.11/site-packages/yapf/yapflib/format_token.pyr   r      s          
$.////%&",,/.)('()*/...rC   r   c                 Z    |dv r#| dk    rdt          | |z   dz
  |z            z  S dS d| z  S )a  Build padding string for continuation alignment in tabbed indentation.

  Arguments:
    spaces: (int) The number of spaces to place before the token for alignment.
    align_style: (str) The alignment style for continuation lines.
    tab_width: (int) Number of columns of each tab character.

  Returns:
    A padding string for alignment with style specified by align_style option.
  )FIXEDzVALIGN-RIGHTr   	r	     )int)spacesalign_style	tab_widths      rD   _TabbedContinuationAlignPaddingrN   A   sJ     ---zzC)+a/9<====2	vrC   c                      e Zd ZdZd Zed             Zd%dZd Zd Z	d Z
d	 Zd
 Ze ej                    d                         Zed             Zed             Zed             Zed             Ze ej                    d                         Ze ej                    d                         Ze ej                    d                         Ze ej                    d                         Ze ej                    d                         Ze ej                    d                         Ze ej                    d                         Ze ej                    d                         Zed             Zed             Ze ej                    d                         Ze ej                    d                         Zed             Z ed             Z!e ej                    d                         Z"e ej                    d                         Z#e ej                    d                          Z$ed!             Z%ed"             Z&ed#             Z'd$S )&FormatTokena  A wrapper around pytree Leaf nodes.

  This represents the token plus additional information useful for reformatting
  the code.

  Attributes:
    node: The PyTree node this token represents.
    next_token: The token in the unwrapped line after this token or None if this
      is the last token in the unwrapped line.
    previous_token: The token in the unwrapped line before this token or None if
      this is the first token in the unwrapped line.
    matching_bracket: If a bracket token ('[', '{', or '(') the matching
      bracket.
    parameters: If this and its following tokens make up a parameter list, then
      this is a list of those parameters.
    container_opening: If the object is in a container, this points to its
      opening bracket.
    container_elements: If this is the start of a container, a list of the
      elements in the container.
    whitespace_prefix: The prefix for the whitespace.
    spaces_required_before: The number of spaces required before a token. This
      is a lower-bound for the formatter and not a hard requirement. For
      instance, a comment may have n required spaces before it. But the
      formatter won't place n spaces before all comments. Only those that are
      moved to the end of a line of code. The formatter may use different
      spacing when appropriate.
    can_break_before: True if we're allowed to break before this token.
    must_break_before: True if we're required to break before this token.
    total_length: The total length of the unwrapped line up to and including
      whitespace and this token. However, this doesn't include the initial
      indentation amount.
    split_penalty: The penalty for splitting the line before this token.
  c                 x   || _         d| _        d| _        d| _        g | _        d| _        g | _        d| _        d| _        d| _	        d| _
        d| _        | j        rt          j        d          | _        nd| _        | j        r%| j         j                                        | _        dS | j         j        | _        dS )zZConstructor.

    Arguments:
      node: (pytree.Leaf) The node that's being wrapped.
    NrH   Fr   SPACES_BEFORE_COMMENT)node
next_tokenprevious_tokenmatching_bracket
parameterscontainer_openingcontainer_elementswhitespace_prefixcan_break_beforemust_break_beforetotal_lengthsplit_penalty
is_commentr   Getspaces_required_beforeis_continuationvaluerstrip)selfrS   s     rD   __init__zFormatToken.__init__v   s     DIDOD DDO!D DD!D"DDD &$)I.E$F$Fd!!$%d! #9?))++djjj9?djjjrC   c                     t          j        d          rH| j                            d          }t	          | j                  t	          |          z
  }|rd|z   |z  S | j        S )NINDENT_BLANK_LINES
)r   r`   rZ   lstriplen)re   without_newlinesheights      rD   formatted_whitespace_prefixz'FormatToken.formatted_whitespace_prefix   sl    y%&& 2/66t<<4)**S1A-B-BBf	 2''611!!rC   r   c                    t          j        d          rN|dk    r<d|z  t          |t          j        d          t          j        d                    z   }n,d|z  d|z  z   }n d|z  t          j        d          z  d|z  z   }| j        rVd | j                                        D             }d|z                       |          | j        _        | j        j        | _        | j        sd| j	        p|z  |z   | _        d	S | xj        |z  c_        d	S )
aG  Register a token's whitespace prefix.

    This is the whitespace that will be output before a token's string.

    Arguments:
      newlines_before: (int) The number of newlines to place before the token.
      spaces: (int) The number of spaces to place before the token.
      indent_level: (int) The indentation level.
    USE_TABSr   rG   CONTINUATION_ALIGN_STYLEINDENT_WIDTHrI   c                 6    g | ]}|                                 S rB   )rj   ).0ss     rD   
<listcomp>z3FormatToken.AddWhitespacePrefix.<locals>.<listcomp>   s     CCCaqxxzzCCCrC   ri   N)
r   r`   rN   r_   rc   
splitlinesjoinrS   rZ   newlines)re   newlines_beforerK   indent_levelindent_beforecomment_liness         rD   AddWhitespacePrefixzFormatToken.AddWhitespacePrefix   s7    y 	%	1		|+.MEI899In%%/' /' ' |+cFl:\)EIn,E,EEV|$m  #CC4:+@+@+B+BCCCm-33MBBdio 9?dj! . $(H I -!.d -rC   c                 P    d|z  | j                             d          z   | _         dS )z0Change the number of newlines before this token.ri   N)rZ   rj   )re   rz   s     rD   AdjustNewlinesBeforez FormatToken.AdjustNewlinesBefore   s/    "_4"4;;DAABDrC   c                 h   | j         }|sdS |j        r|j         }|sdS | j        }|j        }|j        r||j                            d          z  }||k    s"|j        rD|j        dk    r9||j         j        k    r)| j        |z
  |t          j        d          z  z   | _	        dS | j
        j        }|j
        j        }t          |j                  }|j        r|j        dk    r|dz  }d}|j        r8t          |j                            d          d                   }d|j        v rd}|||z   z
  | _	        dS )z%Retains a token's horizontal spacing.Nri   )rr   r	   r   )rU   is_pseudo_parenlinenois_multiline_stringrc   countcolumnr   r`   ra   rS   rk   split)	re   first_columndepthprevious
cur_linenoprev_lineno
cur_columnprev_columnprev_lens	            rD   RetainHorizontalSpacingz#FormatToken.RetainHorizontalSpacing   si   "H f (h J/K# 0X^))$///kk!!		! 	"&.n&;&;	x.5	5	5
+
$uuy/H/H'H
H !f!J-&K8>""H HNc$9$9Qkh# X^))$//344h			",h0F"GDrC   c                 (    | j         t          j        v S N)rc   r   OPENING_BRACKETSre   s    rD   
OpensScopezFormatToken.OpensScope       :666rC   c                 (    | j         t          j        v S r   )rc   r   CLOSING_BRACKETSr   s    rD   ClosesScopezFormatToken.ClosesScope   r   rC   c                 n    d                     | j        | j        | j                  }|| j        rdndz  }|S )Nz+FormatToken(name={0}, value={1}, lineno={2}z	, pseudo)r   )formatnamerc   r   r   )re   msgs     rD   __repr__zFormatToken.__repr__   s?    
7
>
>	4:t{, ,C$.7;;C7CJrC   c                 X    t          j        | j        t           j        j        d          S )z8Split penalty attached to the pytree node of this token.r   )default)r   GetNodeAnnotationrS   
AnnotationSPLIT_PENALTYr   s    rD   node_split_penaltyzFormatToken.node_split_penalty   s1     )	<*8!E E E ErC   c                 T    t          j        | j        t           j        j                  S )z0The number of newlines needed before this token.)r   r   rS   r   NEWLINESr   s    rD   ry   zFormatToken.newlines   s)     )$)*6*A*JL L LrC   c                 T    t          j        | j        t           j        j                  S )z4Return true if the token requires a split before it.)r   r   rS   r   
MUST_SPLITr   s    rD   
must_splitzFormatToken.must_split  s)     )$)*6*A*LN N NrC   c                     | j         j        S )z5The original column number of the node in the source.)rS   r   r   s    rD   r   zFormatToken.column       9rC   c                     | j         j        S )z3The original line number of the node in the source.)rS   r   r   s    rD   r   zFormatToken.lineno  r   rC   c                 v    t          j        | j        t           j        j                  }|t
          j        gn|S )z0Extra type information for directing formatting.)r   r   rS   r   SUBTYPEr   r'   )re   rc   s     rD   subtypeszFormatToken.subtypes  s7     *49+7+B+JL LE"]GL>>5rC   c                 (    t           j        | j        v S )zToken is a binary operator.)r   r)   r   r   s    rD   is_binary_opzFormatToken.is_binary_op       "dm33rC   c                 (    t           j        | j        v S )zToken is an a_expr operator.)r   r*   r   r   s    rD   is_a_expr_opzFormatToken.is_a_expr_op   r   rC   c                 (    t           j        | j        v S )zToken is an m_expr operator.)r   r+   r   r   s    rD   is_m_expr_opzFormatToken.is_m_expr_op&  r   rC   c                     | j         p| j        S )z Token is an arithmetic operator.)r   r   r   s    rD   is_arithmetic_opzFormatToken.is_arithmetic_op,  s     1 11rC   c                 (    t           j        | j        v S )z,Token is an operator in a simple expression.)r   r?   r   r   s    rD   is_simple_exprzFormatToken.is_simple_expr2  s     $55rC   c                 (    t           j        | j        v S )zToken is a subscript colon.)r   r,   r   r   s    rD   is_subscript_colonzFormatToken.is_subscript_colon8  r   rC   c                 4    t          j        | j                  S )z+A string representation of the node's name.)r   NodeNamerS   r   s    rD   r   zFormatToken.name>  s      +++rC   c                 6    | j         j        t          j        k    S r   )rS   typer   COMMENTr   s    rD   r_   zFormatToken.is_commentD  s    9>U]**rC   c                 ,    | j         j        t          k    S r   )rS   r   CONTINUATIONr   s    rD   rb   zFormatToken.is_continuationH  s    9>\))rC   c                 4    t          j        | j                  S r   )keyword	iskeywordrc   r   s    rD   
is_keywordzFormatToken.is_keywordL  s     TZ(((rC   c                 F    | j         j        t          j        k    o| j         S r   )rS   r   r   NAMEr   r   s    rD   is_namezFormatToken.is_nameQ  s     9>UZ'?,??rC   c                 6    | j         j        t          j        k    S r   )rS   r   r   NUMBERr   s    rD   	is_numberzFormatToken.is_numberV      9>U\))rC   c                 6    | j         j        t          j        k    S r   )rS   r   r   STRINGr   s    rD   	is_stringzFormatToken.is_stringZ  r   rC   c                 D    | j         o| j                            d          S )a  Test if this string is a multiline string.

    Returns:
      A multiline string always ends with triple quotes, so if it is a string
      token, inspect the last 3 characters and return True if it is a triple
      double or triple single quote mark.
    )z"""z''')r   rc   endswithr   s    rD   r   zFormatToken.is_multiline_string^  s!     >Adj11.AAArC   c                 *    | j         o| j        j         S r   )r   rS   prev_siblingr   s    rD   is_docstringzFormatToken.is_docstringj  s     #BDI,B(BBrC   c                 D    t          | j        d          o| j        j        S )N	is_pseudo)hasattrrS   r   r   s    rD   r   zFormatToken.is_pseudo_pareno  s      49k**Bty/BBrC   c                 D    | j         ot          j        d| j                  S )Nz #.*\bpylint:\s*(disable|enable)=r_   rematchrc   r   s    rD   is_pylint_commentzFormatToken.is_pylint_commentt  '    ? 4rx(K(,
 4  4 4rC   c                 D    | j         ot          j        d| j                  S )Nz #.*\bpytype:\s*(disable|enable)=r   r   s    rD   is_pytype_commentzFormatToken.is_pytype_commenty  r   rC   c                 D    | j         ot          j        d| j                  S )Nz$#.*\bcopybara:(strip|insert|replace)r   r   s    rD   is_copybara_commentzFormatToken.is_copybara_comment~  s'    ? 4rx(O(,
 4  4 4rC   N)r   r   )(r#   r$   r%   r&   rf   propertyrn   r~   r   r   r   r   r   r   	lru_cacher   ry   r   r   r   r   r   r   r   r   r   r   r   r_   rb   r   r   r   r   r   r   r   r   r   r   rB   rC   rD   rP   rP   S   s          D# # #: " " 8" .  .  .  .DC C C
$H $H $HL7 7 77 7 7   9E E  8E
 L L 8L
 N N 8N
   8   8 96 6  86 94 4  84 94 4  84 94 4  84 92 2  82 96 6  86 94 4  84 9, ,  8, + + 8+ * * 8* 9) )  8) 9@ @  8@ * * 8* * * 8* 9B B  8B 9C C  8C 9C C  8C 4 4 84 4 4 84 4 4 84 4 4rC   rP   )r&   r   r   lib2to3.pgen2r   yapf.yapflibr   r   r   N_TOKENSr   objectr   rN   rP   rB   rC   rD   <module>r      s    
  				       " " " " " " % % % % % %      ~    f   D  $n4 n4 n4 n4 n4& n4 n4 n4 n4 n4rC   