a
    d                      @   s  d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
mZmZ erxddlmZmZ e jdkrld dlmZ nd dlmZ d dlmZ G d	d
 d
Ze
dZeedf ZG dd de	e Zed ZG dd deZdeedddddZdedddZdedddZdS )    N)deepcopy)	ListCallableIteratorUnionOptionalGenericTypeVarAnyTYPE_CHECKING   )TerminalDefToken)      )Literal)OrderedDictc                   @   s^   e Zd ZU eed< eed< eed< eed< eed< eed< eed< ded	< eed
< dd ZdS )MetaemptylinecolumnZ	start_posZend_lineZ
end_columnZend_poszList[TerminalDef]Zorig_expansionZ
match_treec                 C   s
   d| _ d S )NT)r   self r   R/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lark/tree.py__init__   s    zMeta.__init__N)__name__
__module____qualname__bool__annotations__intr   r   r   r   r   r      s   
r   _Leaf_TTree[_Leaf_T]c                   @   s*  e Zd ZU dZeed< ded< d;edee ddddZe	ed	d
dZ
dd Zdd Zdd Zd<eedddZd=dddddZdd Zdd Zdd Zed	d d!Zd"d	d#d$Zd%d"d&d'd(Zed"d)d*d+Zd,d- Zd.ee d&d/d0Zd1d2 Zd3d4 Zd5d	d6d7Zeddd8d9d:ZdS )>Treea  The main tree class.

    Creates a new tree, and stores "data" and "children" in attributes of the same name.
    Trees can be hashed and compared.

    Parameters:
        data: The name of the rule or alias
        children: List of matched sub-rules and terminals
        meta: Line & Column numbers (if ``propagate_positions`` is enabled).
            meta attributes: line, column, start_pos, end_line, end_column, end_pos
    datazList[Branch[_Leaf_T]]childrenN)r%   r&   metareturnc                 C   s   || _ || _|| _d S N)r%   r&   _meta)r   r%   r&   r'   r   r   r   r   4   s    zTree.__init__)r(   c                 C   s   | j d u rt | _ | j S r)   )r*   r   r   r   r   r   r'   9   s    
z	Tree.metac                 C   s   d| j | jf S )NzTree(%r, %r)r%   r&   r   r   r   r   __repr__?   s    zTree.__repr__c                 C   s   | j S r)   r%   r   r   r   r   _pretty_labelB   s    zTree._pretty_labelc                 C   s   t | jdkr@t| jd ts@|| |  dd| jd f dgS || |  dg}| jD ]@}t|tr||||d |7 }qX|||d  d|f dg7 }qX|S )Nr   r   	z%s
)lenr&   
isinstancer$   r.   _pretty)r   level
indent_strlnr   r   r   r3   E   s    "

zTree._pretty  )r5   r(   c                 C   s   d | d|S )z]Returns an indented string representation of the tree.

        Great for debugging.
         r   )joinr3   )r   r5   r   r   r   prettyR   s    zTree.prettyzrich.tree.Tree)parentr(   c                 C   s
   |  |S )zReturns a tree widget for the 'rich' library.

        Example:
            ::
                from rich import print
                from lark import Tree

                tree = Tree('root', ['node1', 'node2'])
                print(tree)
        )_rich)r   r<   r   r   r   __rich__Y   s    zTree.__rich__c                 C   sh   |r| d| j d}ndd l}|j| j}| jD ],}t|trP|| q6| d| d q6|S )Nz[bold]z[/bold]r   z[green]z[/green])addr%   Z	rich.treetreer$   r&   r2   r=   )r   r<   r@   Zrichcr   r   r   r=   f   s    

z
Tree._richc                 C   s4   z| j |j ko| j|jkW S  ty.   Y dS 0 d S )NF)r%   r&   AttributeErrorr   otherr   r   r   __eq__u   s    zTree.__eq__c                 C   s
   | |k S r)   r   rC   r   r   r   __ne__{   s    zTree.__ne__c                 C   s   t | jt| jfS r)   )hashr%   tupler&   r   r   r   r   __hash__~   s    zTree.__hash__zIterator[Tree[_Leaf_T]]c                    sP   | g}t   |D ],}| t|< | fddt|jD 7 }q~tt  S )zDepth-first iteration.

        Iterates over all the subtrees, never returning to the same node twice (Lark's parse-tree is actually a DAG).
        c                    s&   g | ]}t |trt| vr|qS r   )r2   r$   id).0rA   Zsubtreesr   r   
<listcomp>   s   z&Tree.iter_subtrees.<locals>.<listcomp>)r   rJ   reversedr&   listvalues)r   queuesubtreer   rL   r   iter_subtrees   s    zTree.iter_subtreeszCallable[[Tree[_Leaf_T]], bool])predr(   c                 C   s   t ||  S )z?Returns all nodes of the tree that evaluate pred(node) as true.)filterrS   )r   rT   r   r   r   	find_pred   s    zTree.find_pred)r%   r(   c                    s   |   fddS )z?Returns all nodes of the tree whose data equals the given data.c                    s
   | j  kS r)   r-   )tr-   r   r   <lambda>       z Tree.find_data.<locals>.<lambda>)rV   )r   r%   r   r-   r   	find_data   s    zTree.find_datac                 G   sZ   d}t t| jd ddD ]:}| j| }t|tr|j|v r|j| j||d < d}q|S )z\Expand (inline) children with any of the given data values. Returns True if anything changedFr   T)ranger1   r&   r2   r$   r%   )r   Zdata_valueschangedichildr   r   r   expand_kids_by_data   s    
zTree.expand_kids_by_dataz!Callable[[Branch[_Leaf_T]], bool]c                 c   s@   | j D ]4}t|tr,||D ]
}|V  qq||r|V  qdS )zReturn all values in the tree that evaluate pred(value) as true.

        This can be used to find all the tokens in the tree.

        Example:
            >>> all_tokens = tree.scan_values(lambda v: isinstance(v, Token))
        N)r&   r2   r$   scan_values)r   rT   rA   rW   r   r   r   ra      s    


zTree.scan_valuesc                 c   sD   | g}|r@|  }t|tsq|V  t|jD ]}|| q.qdS )ztBreadth-first iteration.

        Iterates over all the subtrees, return nodes in order like pretty() does.
        N)popr2   r$   rN   r&   append)r   stacknoder_   r   r   r   iter_subtrees_topdown   s    
zTree.iter_subtrees_topdownc                 C   s   t | | jt| j|| jdS )N)r'   )typer%   r   r&   r*   )r   memor   r   r   __deepcopy__   s    zTree.__deepcopy__r#   c                 C   s   t | | j| jS r)   )rg   r%   r&   r   r   r   r   copy   s    z	Tree.copy)r%   r&   r(   c                 C   s   || _ || _d S r)   r+   )r   r%   r&   r   r   r   set   s    zTree.set)N)r8   )N) r   r   r   __doc__strr    r   r   r   propertyr'   r,   r.   r3   r;   r>   r=   rE   rF   r!   rI   rS   rV   rZ   r`   r   r"   ra   rf   ri   rj   rk   r   r   r   r   r$   $   s0   
r$   r   c                   @   s   e Zd ZdZdS )SlottedTree)r%   r&   Zruler*   N)r   r   r   	__slots__r   r   r   r   ro      s   ro   LRzLiteral["TB", "LR", "BT", "RL"])r@   filenamerankdirr(   c                 K   s    t | |fi |}|| d S r)   )pydot__tree_to_graphZ	write_pngr@   rr   rs   kwargsgraphr   r   r   pydot__tree_to_png   s    rx   )r@   c                 K   s    t | |fi |}|| d S r)   )rt   writeru   r   r   r   pydot__tree_to_dot   s    rz   c                    sT   ddl jf d|d|dgfdd fdd  |  S )	a  Creates a colorful image that represents the tree (data+children, without meta)

    Possible values for `rankdir` are "TB", "LR", "BT", "RL", corresponding to
    directed graphs drawn from top to bottom, from left to right, from bottom to
    top, and from right to left, respectively.

    `kwargs` can be any graph attribute (e. g. `dpi=200`). For a list of
    possible attributes, see https://www.graphviz.org/doc/info/attrs.html.
    r   NZdigraph)Z
graph_typers   c                    s4   j d t| d}d  d7  <  | |S )Nr   )labelr   )Noderepradd_node)Zleafre   )rw   r^   pydotr   r   new_leaf   s    
z&pydot__tree_to_graph.<locals>.new_leafc                    s   t | jd@ }|dO } fdd| jD }jd dd| | jd}d  d	7  < | |D ]}|| qf|S )
Ni i c                    s&   g | ]}t |tr |n|qS r   )r2   r$   )rK   r_   )	_to_pydotr   r   r   rM      s   z;pydot__tree_to_graph.<locals>._to_pydot.<locals>.<listcomp>r   Zfilledz#%x)styleZ	fillcolorr{   r   )rG   r%   r&   r|   r~   Zadd_edgeZEdge)rR   colorZsubnodesre   Zsubnoder   rw   r^   r   r   r   r   r      s    
z'pydot__tree_to_graph.<locals>._to_pydot)r   ZDot)r@   rs   rv   r   r   r   rt      s    rt   )rq   )rq   )rq   )sysrj   r   typingr   r   r   r   r   r   r	   r
   r   lexerr   r   version_infor   Ztyping_extensionscollectionsr   r   r"   ZBranchr$   Z	ParseTreero   rm   rx   rz   rt   r   r   r   r   <module>   s$   ,
 ,