a
    
d(                     @   s  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
g dZG d	d
 d
eZdd Zdd Zdd Zej Zejd= dd Zdd Zdd Zdd Zd2ddZdZdd Zd3d d!Zd"d# Zd$d% Zd&d' Zd(d) Z d*d+ Z!d,d- Z"d.d/ Z#d0d1 Z$dS )4a  pytree-related utilities.

This module collects various utilities related to the parse trees produced by
the lib2to3 library.

  NodeName(): produces a string name for pytree nodes.
  ParseCodeToTree(): convenience wrapper around lib2to3 interfaces to parse
                     a given string with code to a pytree.
  InsertNodeBefore(): insert a node before another in a pytree.
  InsertNodeAfter(): insert a node after another in a pytree.
  {Get,Set}NodeAnnotation(): manage custom annotations on pytree nodes.
    N)pygram)pytree)driver)parse)token)DEDENTINDENTNEWLINE	ENDMARKERc                   @   s$   e Zd ZdZdZdZdZdZdZdS )
Annotationz)Annotation names associated with pytrees.Zchild_indentnewlinesZ
must_splitZsplit_penaltysubtypeN)	__name__
__module____qualname____doc__CHILD_INDENTZNEWLINESZ
MUST_SPLITSPLIT_PENALTYSUBTYPE r   r   a/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/yapf/pytree/pytree_utils.pyr   +   s   r   c                 C   s(   | j dk rtj| j  S tjj| j  S dS )zProduce a string name for a given node.

  For a Leaf this is the token name, and for a Node this is the type.

  Arguments:
    node: a tree node

  Returns:
    Name as a string.
     N)typer   tok_namer   Zpython_grammarZnumber2symbolnoder   r   r   NodeName4   s    
r   c                 C   s   t | tjr| S t| jd S )Nr   )
isinstancer   LeafFirstLeafNodechildrenr   r   r   r   r   F   s    r   c                 C   s   t | tjr| S t| jd S )N)r   r   r   LastLeafNoder    r   r   r   r   r"   L   s    r"   execc                 C   sb   |  tjs| tj7 } z"tjttjd}|j| dd}W n  t	j
yX   t	|   Y n0 t|S )a  Parse the given code to a lib2to3 pytree.

  Arguments:
    code: a string with the code to parse.

  Raises:
    SyntaxError if the code is invalid syntax.
    parse.ParseError if some other parsing failure.

  Returns:
    The root node of the parsed tree.
  )convertF)debug)endswithoslinesepr   ZDriver_PYTHON_GRAMMARr   r$   parse_stringr   
ParseErrorast_WrapEndMarker)codeZparser_drivertreer   r   r   ParseCodeToTree]   s    

r0   c                 C   s.   t | tjr*| jtjkr*ttjj	| gS | S )aS  Wrap a single ENDMARKER token in a "file_input" node.

  Arguments:
    tree: (pytree.Node) The root node of the parsed tree.

  Returns:
    The root node of the parsed tree. If the tree is a single ENDMARKER node,
    then that node is wrapped in a "file_input" node. That will ensure we don't
    skip comments attached to that node.
  )
r   r   r   r   r   r
   Noder   Zpython_symbolsZ
file_input)r/   r   r   r   r-   y   s    r-   c                 C   s   | D ]}t ||dd qdS )aT  Insert new_nodes before the given target location in the tree.

  Arguments:
    new_nodes: a sequence of new nodes to insert (the nodes should not be in the
      tree).
    target: the target node before which the new node node will be inserted.

  Raises:
    RuntimeError: if the tree is corrupted, or the insertion would corrupt it.
  FafterN)_InsertNodeAtZ	new_nodestargetr   r   r   r   InsertNodesBefore   s    r7   c                 C   s    t | D ]}t||dd qdS )aR  Insert new_nodes after the given target location in the tree.

  Arguments:
    new_nodes: a sequence of new nodes to insert (the nodes should not be in the
      tree).
    target: the target node after which the new node node will be inserted.

  Raises:
    RuntimeError: if the tree is corrupted, or the insertion would corrupt it.
  Tr2   N)reversedr4   r5   r   r   r   InsertNodesAfter   s    r9   Fc                 C   s   | j durtd| | j f|j }|du r4td|ft|jD ]2\}}||u r>|rZ|d n|}|||   dS q>td|fdS )a|  Underlying implementation for node insertion.

  Arguments:
    new_node: a new node to insert (this node should not be in the tree).
    target: the target node.
    after: if True, new_node is inserted after target. Otherwise, it's inserted
      before target.

  Returns:
    nothing

  Raises:
    RuntimeError: if the tree is corrupted, or the insertion would corrupt it.
  Nz)inserting node which already has a parentz%expected target node to have a parent   z.unable to find insertion point for target node)parentRuntimeError	enumerater    Zinsert_child)new_noder6   r3   Zparent_of_targetichildZinsertion_indexr   r   r   r4      s    
r4   Z_yapf_annotation_c                 C   s0   t | D ]"}|trt||t| |d qdS )zCopy all YAPF annotations from the source node to the destination node.

  Arguments:
    src: the source node.
    dst: the destination node.
  N)dir
startswith_NODE_ANNOTATION_PREFIXsetattrgetattr)srcdst
annotationr   r   r   CopyYapfAnnotations   s    
rI   c                 C   s   t | t| |S )aC  Get annotation value from a node.

  Arguments:
    node: the node.
    annotation: annotation name - a string.
    default: the default value to return if there's no annotation.

  Returns:
    Value of the annotation in the given node. If the node doesn't have this
    particular annotation name yet, returns default.
  rE   rC   )r   rH   defaultr   r   r   GetNodeAnnotation   s    rL   c                 C   s   t | t| | dS )zSet annotation value on a node.

  Arguments:
    node: the node.
    annotation: annotation name - a string.
    value: annotation value to set.
  NrD   rC   )r   rH   valuer   r   r   SetNodeAnnotation   s    rO   c                 C   s(   t | |t }|| t| || dS )zAppends an annotation value to a list of annotations on the node.

  Arguments:
    node: the node.
    annotation: annotation name - a string.
    value: annotation value to set.
  N)rL   setaddrO   )r   rH   rN   attrr   r   r   AppendNodeAnnotation   s    
rS   c                 C   s4   t | tj}|r0||v r0|| t| tj| dS )zRemoves an annotation value from the subtype annotations on the node.

  Arguments:
    node: the node.
    value: annotation value to remove.
  N)rL   r   r   removerO   )r   rN   rR   r   r   r   RemoveSubtypeAnnotation  s    
rU   c                 C   s   t | td dS )zGet opening bracket value from a node.

  Arguments:
    node: the node.

  Returns:
    The opening bracket node or None if it couldn't find one.
  container_bracketNrJ   r   r   r   r   GetOpeningBracket  s    	rW   c                 C   s   t | td | dS )zoSet opening bracket value for a node.

  Arguments:
    node: the node.
    bracket: opening bracket to set.
  rV   NrM   )r   Zbracketr   r   r   SetOpeningBracket  s    rX   c              
   C   sl   t | tjrBd}|jt| t| | j| jt| j	t
| tjddS d}|jt| t| jt
| tjdS dS )zDump a string representation of the given node. For debugging.

  Arguments:
    node: the node.

  Returns:
    The string representation.
  zV{name}({value}) [lineno={lineno}, column={column}, prefix={prefix}, penalty={penalty}]N)namerN   linenocolumnprefixZpenaltyz1{node} [{len} children] [child_indent="{indent}"])r   lenindent)r   r   r   formatr   _PytreeNodeReprrZ   r[   reprr\   rL   r   r   r]   r    r   )r   fmtr   r   r   DumpNodeToString(  s     	
rc   c                 C   sV   t | tjr.d| jjt| dd | jD f S t | tjrRd| jjt| | jf S dS )zCLike pytree.Node.__repr__, but names instead of numbers for tokens.z
%s(%s, %r)c                 S   s   g | ]}t |qS r   )r`   ).0cr   r   r   
<listcomp>G      z#_PytreeNodeRepr.<locals>.<listcomp>N)	r   r   r1   	__class__r   r   r    r   rN   r   r   r   r   r`   C  s    r`   c                 C   s   t | dko| jd jtjkS )NZsimple_stmtr   )r   r    r   r   COMMENTr   r   r   r   IsCommentStatementL  s    rj   )F)N)%r   r,   r'   Zyapf_third_party._ylib2to3r   r   Z yapf_third_party._ylib2to3.pgen2r   r   r   	frozensetZNONSEMANTIC_TOKENSobjectr   r   r   r"   Z!python_grammar_no_print_statementcopyr)   keywordsr0   r-   r7   r9   r4   rC   rI   rL   rO   rS   rU   rW   rX   rc   r`   rj   r   r   r   r   <module>   s<   	

(

	