
    ?d*                         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	 ddlm
Z
 ej                  Z eh d      Z eh d	      Zd
 Z G d de      Zy)z*Enhanced token information for formatting.    N)token)pytree_utils)	py3compat)style)subtypes>   ([{>   )]}c                 N    |dv r| dkD  rdt        | |z   dz
  |z        z  S y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   	     )int)spacesalign_style	tab_widths      ;/usr/lib/python3/dist-packages/yapf/yapflib/format_token.py_TabbedContinuationAlignPaddingr       s@     --zC)+a/9<===	v    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d
 Zed        Ze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d        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zy)FormatTokena  Enhanced token information for formatting.

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

  Attributes:
    node: The original token node.
    next_token: The token in the logical line after this token or None if this
      is the last token in the logical line.
    previous_token: The token in the logical line before this token or None if
      this is the first token in the logical 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.
    total_length: The total length of the logical 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.
    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.
    newlines: The number of newlines needed before this token.
  c                    || _         || _        |j                  | _        |j                  | _        |j                  | _        |j
                  | _        | j                  r|j
                  j                         | _        d| _        d| _	        d| _
        g | _        d| _        g | _        d| _        d| _        d| _        d| _        t%        j&                  |t$        j(                  j*                  d      | _        t%        j&                  |t$        j(                  j.                        | _        d| _        | j4                  rt7        j8                  d      | _        t%        j&                  |t$        j(                  j:                        }|st<        j>                  hn|| _        tA        |d      xr |jB                  | _!        y)zConstructor.

    Arguments:
      node: (pytree.Leaf) The node that's being wrapped.
      name: (string) The name of the node.
    Nr   r   FdefaultSPACES_BEFORE_COMMENT	is_pseudo)"nodenametypecolumnlinenovalueis_continuationrstrip
next_tokenprevious_tokenmatching_bracket
parameterscontainer_openingcontainer_elementswhitespace_prefixtotal_lengthsplit_penaltycan_break_beforer   GetNodeAnnotation
Annotation
MUST_SPLITmust_break_beforeNEWLINESnewlinesspaces_required_before
is_commentr   GetSUBTYPEr   NONEhasattrr!   )selfr"   r#   stypess       r   __init__zFormatToken.__init__V   s^    DIDI		DI++DK++DKDJ::$$&djDOD DDO!D DDDD!D);;l%%00%AD 22l%%..0DM"#D$)II.E$Fd!++D,8,C,C,K,KMF+1X]]OvDMT;/BDNNDNr   c                     t        j                  d      rF| j                  j                  d      }t	        | j                        t	        |      z
  }|rd|z   |z  S | j                  S )NINDENT_BLANK_LINES
)r   r<   r0   lstriplen)r@   without_newlinesheights      r   formatted_whitespace_prefixz'FormatToken.formatted_whitespace_prefix   sa    yy%&//66t<4))*S1A-BBf	''611!!!r   c                 `   t        j                  d      rK|dkD  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                  r_| j                  j                         D cg c]  }|j                          }}d|z   j                  |      | _        | j                  | _        | j                  sd| j                  xs |z  |z   | _        y| xj                  |z  c_        yc c}w )	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   r   CONTINUATION_ALIGN_STYLEINDENT_WIDTHr   rE   N)
r   r<   r   r;   r'   
splitlinesrF   joinr0   r9   )r@   newlines_beforer   indent_levelindent_beforescomment_liness          r   AddWhitespacePrefixzFormatToken.AddWhitespacePrefix   s    yy	1	|+.MEII89IIn%/' ' |+cFl:\)EIIn,EEV|$m +/::+@+@+BCaqxxzCmC=(..}=dj ::dj!! $(H I -!.d - Ds   *D+c                 P    d|z  | j                   j                  d      z   | _         y)z0Change the number of newlines before this token.rE   N)r0   rF   )r@   rQ   s     r   AdjustNewlinesBeforez FormatToken.AdjustNewlinesBefore   s)    "_4"44;;DABDr   c                    | j                   }|sy|j                  r|j                   }|sy| j                  }|j                  }|j                  r||j                  j                  d      z  }||k7  s4|j                  rV|j                  dk7  rG||j                   j                  k7  r.| j                  |z
  |t        j                  d      z  z   | _	        y| j                  }|j                  }t        |j                        }|j                  r|j                  dk(  r|dz  }d}|j                  r7t        |j                  j                  d      d         }d|j                  v rd}|||z   z
  | _	        y)z%Retains a token's horizontal spacing.NrE   r   rN   r   r   )r+   r!   r&   is_multiline_stringr'   countr%   r   r<   r:   rG   split)	r@   first_columndepthprevious
cur_linenoprev_lineno
cur_columnprev_columnprev_lens	            r   RetainHorizontalSpacingz#FormatToken.RetainHorizontalSpacing   sH   ""H((hJ//K##X^^))$//kk!			# 5	x..55	5
++
$uuyy/H'H
H !J//K8>>"Hhnn3Qkh##X^^))$/34h		",h0F"GDr   c                 &    | j                   t        v S N)r'   _OPENING_BRACKETSr@   s    r   
OpensScopezFormatToken.OpensScope       ::***r   c                 &    | j                   t        v S rh   )r'   _CLOSING_BRACKETSrj   s    r   ClosesScopezFormatToken.ClosesScope   rl   r   c                 :    | j                   j                  |       y rh   )r   add)r@   subtypes     r   
AddSubtypezFormatToken.AddSubtype   s    MMgr   c                     dj                  | j                  rdn| j                  | j                  | j                  | j
                  | j                        }|| j                  rdz  }|S dz  }|S )NzIFormatToken(name={0}, value={1}, column={2}, lineno={3}, splitpenalty={4}	DOCSTRINGz	, pseudo)r   )formatis_docstringr#   r'   r%   r&   r2   r!   )r@   msgs     r   __repr__zFormatToken.__repr__   sh    $f"//{TYY

{{DKK););=  $..;1CJ /21CJr   c                 v    t        j                  | j                  t         j                  j                  d      S )z8Split penalty attached to the pytree node of this token.r   r   )r   r4   r"   r5   SPLIT_PENALTYrj   s    r   node_split_penaltyzFormatToken.node_split_penalty   s0     ))		<**88!E Er   c                 :    t         j                  | j                   v S )zToken is a binary operator.)r   BINARY_OPERATORrj   s    r   is_binary_opzFormatToken.is_binary_op        ##t}}44r   c                 4    | j                   t        h d      v S )z Token is an arithmetic operator.>   **//<<>>*%&+-/@^|)r'   	frozensetrj   s    r   is_arithmetic_opzFormatToken.is_arithmetic_op   s!     :: $   r   c                 :    t         j                  | j                   v S )z,Token is an operator in a simple expression.)r   SIMPLE_EXPRESSIONrj   s    r   is_simple_exprzFormatToken.is_simple_expr  s     %%66r   c                 :    t         j                  | j                   v S )zToken is a subscript colon.)r   SUBSCRIPT_COLONrj   s    r   is_subscript_colonzFormatToken.is_subscript_colon
  r   r   c                 <    | j                   t        j                  k(  S rh   )r$   r   COMMENTrj   s    r   r;   zFormatToken.is_comment  s    99%%r   c                 (    | j                   t        k(  S rh   )r$   CONTINUATIONrj   s    r   r(   zFormatToken.is_continuation  s    99$$r   c                 @    t        j                  | j                        S rh   )keyword	iskeywordr'   rj   s    r   
is_keywordzFormatToken.is_keyword  s     TZZ((r   c                 Z    | j                   t        j                  k(  xr | j                   S rh   )r$   r   NAMEr   rj   s    r   is_namezFormatToken.is_name  s!    99

":4??'::r   c                 <    | j                   t        j                  k(  S rh   )r$   r   NUMBERrj   s    r   	is_numberzFormatToken.is_number       99$$r   c                 <    | j                   t        j                  k(  S rh   )r$   r   STRINGrj   s    r   	is_stringzFormatToken.is_string$  r   r   c                 T    | j                   xr | j                  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   r'   endswithrj   s    r   r[   zFormatToken.is_multiline_string(  s"     >>Adjj11.AAr   c                 :    | j                   xr | j                  d u S rh   )r   r+   rj   s    r   rw   zFormatToken.is_docstring3  s    >>9d11T99r   c                 ^    | j                   xr  t        j                  d| j                        S )Nz #.*\bpylint:\s*(disable|enable)=r;   rematchr'   rj   s    r   is_pylint_commentzFormatToken.is_pylint_comment7  (    ?? 4rxx(K(,

 4 4r   c                 ^    | j                   xr  t        j                  d| j                        S )Nz #.*\bpytype:\s*(disable|enable)=r   rj   s    r   is_pytype_commentzFormatToken.is_pytype_comment<  r   r   c                 ^    | j                   xr  t        j                  d| j                        S )Nz'#.*\bcopybara:\s*(strip|insert|replace)r   rj   s    r   is_copybara_commentzFormatToken.is_copybara_commentA  s*    ?? @rxx2DJJ @ @r   N)r   r   ) __name__
__module____qualname____doc__rB   propertyrJ   rV   rX   rf   rk   ro   rs   ry   r|   r   r   	lru_cacher   r   r   r;   r(   r   r   r   r   r[   rw   r   r   r    r   r   r   r   2   s   !F'CR " " .DC
$HL++ E E
 5 5 9  $ 7 7 5 5 & & % % 9)  ) ; ; % % % % B B : : 4 4 4 4 @ @r   r   )r   r   r   lib2to3.pgen2r   yapf.pytreer   yapf.yapflibr   r   r   N_TOKENSr   r   ri   rn   r   objectr   r   r   r   <module>r      sS    1  	  $ "  !~~o. o. $R@& R@r   