a
    
d                     @   sV   d Z ddlZddlmZ ddlmZ G dd deZejfddZ	G d	d
 d
eZ
dS )a  Generic visitor pattern for pytrees.

The lib2to3 parser produces a "pytree" - syntax tree consisting of Node
and Leaf types. This module implements a visitor pattern for such trees.

It also exports a basic "dumping" visitor that dumps a textual representation of
a pytree into a stream.

  PyTreeVisitor: a generic visitor pattern for pytrees.
  PyTreeDumper: a configurable "dumper" for displaying pytrees.
  DumpPyTree(): a convenience function to dump a pytree.
    N)pytree)pytree_utilsc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	PyTreeVisitora  Visitor pattern for pytree trees.

  Methods named Visit_XXX will be invoked when a node with type XXX is
  encountered in the tree. The type is either a token type (for Leaf nodes) or
  grammar symbols (for Node nodes). The return value of Visit_XXX methods is
  ignored by the visitor.

  Visitors can modify node contents but must not change the tree structure
  (e.g. add/remove children and move nodes around).

  This is a very common visitor pattern in Python code; it's also used in the
  Python standard library ast module for providing AST visitors.

  Note: this makes names that aren't style conformant, so such visitor methods
  need to be marked with # pylint: disable=invalid-name We don't have a choice
  here, because lib2to3 nodes have under_separated names.

  For more complex behavior, the visit, DefaultNodeVisit and DefaultLeafVisit
  methods can be overridden. Don't forget to invoke DefaultNodeVisit for nodes
  that may have children - otherwise the children will not be visited.
  c                 C   sP   d t|}t| |r*t| || n"t|tjrB| | n
| 	| dS )zVisit a node.z	Visit_{0}N)
formatr   ZNodeNamehasattrgetattr
isinstancer   ZLeafDefaultLeafVisitDefaultNodeVisit)selfnodemethod r   c/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/yapf/pytree/pytree_visitor.pyVisit9   s    
zPyTreeVisitor.Visitc                 C   s   |j D ]}| | qdS )zDefault visitor for Node: visits the node's children depth-first.

    This method is invoked when no specific visitor for the node is defined.

    Arguments:
      node: the node to visit
    N)childrenr   )r   r   childr   r   r   r
   E   s    
zPyTreeVisitor.DefaultNodeVisitc                 C   s   dS )zDefault visitor for Leaf: no-op.

    This method is invoked when no specific visitor for the leaf is defined.

    Arguments:
      leaf: the leaf to visit
    Nr   r   Zleafr   r   r   r	   P   s    zPyTreeVisitor.DefaultLeafVisitN)__name__
__module____qualname____doc__r   r
   r	   r   r   r   r   r   "   s   r   c                 C   s   t |}||  dS )az  Convenience function for dumping a given pytree.

  This function presents a very minimal interface. For more configurability (for
  example, controlling how specific node types are displayed), use PyTreeDumper
  directly.

  Arguments:
    tree: the tree to dump.
    target_stream: the stream to dump the tree to. A file-like object. By
      default will dump into stdout.
  N)PyTreeDumperr   )treetarget_streamZdumperr   r   r   
DumpPyTree[   s    r   c                       s>   e Zd ZdZejfddZdd Z fddZdd	 Z	  Z
S )
r   zVVisitor that dumps the tree to a stream.

  Implements the PyTreeVisitor interface.
  c                 C   s   || _ d| _dS )zCreate a tree dumper.

    Arguments:
      target_stream: the stream to dump the tree to. A file-like object. By
        default will dump into stdout.
    r   N)_target_stream_current_indent)r   r   r   r   r   __init__q   s    zPyTreeDumper.__init__c                 C   s   | j dd| j | d S )Nz{0}{1}
 )r   writer   r   )r   sr   r   r   _DumpString{   s    zPyTreeDumper._DumpStringc                    s@   |  t| |  jd7  _tt| | |  jd8  _d S )N   )r"   r   DumpNodeToStringr   superr   r
   )r   r   	__class__r   r   r
   ~   s    zPyTreeDumper.DefaultNodeVisitc                 C   s   |  t| d S )N)r"   r   r$   r   r   r   r   r	      s    zPyTreeDumper.DefaultLeafVisit)r   r   r   r   sysstdoutr   r"   r
   r	   __classcell__r   r   r&   r   r   k   s
   
r   )r   r(   Zyapf_third_party._ylib2to3r   Zyapf.pytreer   objectr   r)   r   r   r   r   r   r   <module>   s   9