
    
`iJ!              	       R   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 erddlmZmZ 	 d dlZn# e$ r Y nw xY wd dlmZ  G d d          Z e
d	          Zeed
f         Z G d de	e                   Zed         Z G d de          ZddededdddfdZddefdZddefdZdS )    N)deepcopy)ListCallableIteratorUnionOptionalGenericTypeVarTYPE_CHECKING   )TerminalDefToken)Literalc                   p    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 ZdS )Metaemptylinecolumn	start_posend_line
end_columnend_poszList[TerminalDef]orig_expansion
match_treec                     d| _         d S )NT)r   selfs    ]/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/lark/tree.py__init__zMeta.__init__   s    


    N)__name__
__module____qualname__bool__annotations__intr    r    r   r   r      s|         KKK
IIIKKKNNNMMMOOOLLL''''    r    r   _Leaf_TTree[_Leaf_T]c            	       4   e Zd ZU dZeed<   ded<   d%dedddee         ddfdZe	defd	            Z
d
 Zd Zd Zd&dedefdZd%ded         ddfdZd Zd Zd ZdefdZd'dZd Zd(dZdeddfdZd Zdddee         fd Zd! Zd)d#Zdeddddfd$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, end_line, end_column, start_pos, end_pos,
                              container_line, container_column, container_end_line, container_end_column)
            container_* attributes consider all symbols, including those that have been inlined in the tree.
            For example, in the rule 'a: _A B _C', the regular attributes will mark the start and end of B,
            but the container_* attributes will also include _A and _C in the range. However, rules that
            contain 'a' will consider it in full, including _A and _C for all attributes.
    datazList[Branch[_Leaf_T]]childrenNmetareturnc                 0    || _         || _        || _        d S N)r,   r-   _meta)r   r,   r-   r.   s       r   r   zTree.__init__9   s    	 


r    c                 D    | j         t                      | _         | j         S r1   )r2   r   r   s    r   r.   z	Tree.meta>   s    :DJzr    c                 (    d| j         d| j        dS )NzTree(z, )r,   r-   r   s    r   __repr__zTree.__repr__D   s     !%DMMM::r    c                     | j         S r1   r,   r   s    r   _pretty_labelzTree._pretty_labelG   s
    yr    c              #     K   ||z   |                                   V  t          | j                  dk    r5t          | j        d         t                    sd| j        d          dV  d S dV  | j        D ]F}t          |t                    r |                    |dz   |          E d {V  7||dz   z   | dV  Gd S )Nr   r   	
)r:   lenr-   
isinstancer+   _pretty)r   level
indent_strns       r   r@   zTree._prettyJ   s     E!94#5#5#7#799999t}"":dmA6F+M+M"+t}Q'++++++++JJJ] 9 9a&& 9 yyq*=========='q1818888888	9 9r      rB   c                 T    d                     |                     d|                    S )z]Returns an indented string representation of the tree.

        Great for debugging.
         r   )joinr@   )r   rB   s     r   prettyzTree.prettyV   s$    
 wwt||Az22333r    parentzrich.tree.Treec                 ,    |                      |          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   rI   s     r   __rich__zTree.__rich__]   s     zz&!!!r    c                 *   |r|                     d| j         d          }n#dd l}|j                            | j                  }| j        D ]F}t          |t                    r|                    |           -|                     d| d           G|S )Nz[bold]z[/bold]r   z[green]z[/green])addr,   	rich.treetreer+   r-   r?   rK   )r   rI   rP   richcs        r   rK   z
Tree._richj   s     	-::9ty999::DD9>>$),,D 	0 	0A!T"" 0.1...////r    c                 f    	 | j         |j         k    o| j        |j        k    S # t          $ r Y dS w xY w)NF)r,   r-   AttributeErrorr   others     r   __eq__zTree.__eq__y   sF    	9
*Nt}/NN 	 	 	55	s   " 
00c                     | |k     S r1   r'   rU   s     r   __ne__zTree.__ne__   s    EM""r    c                 R    t          | j        t          | j                  f          S r1   )hashr,   tupler-   r   s    r   __hash__zTree.__hash__   s!    TYdm 4 45666r    Iterator[Tree[_Leaf_T]]c                     | g}t                      |D ]7}|t          |          <   |fdt          |j                  D             z  }8~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                 `    g | ]*}t          |t                    rt          |          v(|+S r'   )r?   r+   id).0rR   subtreess     r   
<listcomp>z&Tree.iter_subtrees.<locals>.<listcomp>   sI     H H HA#At,,H13Ah1F1F 1F1F1Fr    )dictra   reversedr-   listvalues)r   queuesubtreerc   s      @r   iter_subtreeszTree.iter_subtrees   s    
 66 	H 	HG$+HR[[! H H H H'*:!;!; H H H HEE X__..//000r    c              #      K   | g}|j         }|j        }|rJ |            }t          |t                    s"|V  t	          |j                  D ]} ||           |HdS dS )ztBreadth-first iteration.

        Iterates over all the subtrees, return nodes in order like pretty() does.
        N)appendpopr?   r+   rf   r-   )r   stackstack_append	stack_popnodechilds         r   iter_subtrees_topdownzTree.iter_subtrees_topdown   s      
 |I	 	$9;;DdD)) JJJ!$-00 $ $U####  	$ 	$ 	$ 	$ 	$r    predCallable[[Tree[_Leaf_T]], bool]c                 F    t          ||                                           S )z?Returns all nodes of the tree that evaluate pred(node) as true.)filterrk   )r   ru   s     r   	find_predzTree.find_pred   s    dD..00111r    c                 4    |                      fd          S )z?Returns all nodes of the tree whose data equals the given data.c                     | j         k    S r1   r9   )tr,   s    r   <lambda>z Tree.find_data.<locals>.<lambda>   s    $ r    )ry   )r   r,   s    `r   	find_datazTree.find_data   s    ~~6666777r    c                     d}t          t          | j                  dz
  dd          D ]C}| j        |         }t          |t                    r|j        |v r|j        | j        ||dz   <   d}D|S )z\Expand (inline) children with any of the given data values. Returns True if anything changedFr   T)ranger>   r-   r?   r+   r,   )r   data_valueschangedirs   s        r   expand_kids_by_datazTree.expand_kids_by_data   s|    s4=))!+R44 	 	AM!$E%&& 5:+D+D',~a!e$r    z!Callable[[Branch[_Leaf_T]], bool]c              #      K   | j         D ]C}t          |t                    r|                    |          D ]}|V  4 ||          r|V  Dd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-   r?   r+   scan_values)r   ru   rR   r|   s       r   r   zTree.scan_values   s|        	 	A!T"" t,,  AGGGG 477 GGG	 	r    c                 r     t          |           | j        t          | j        |          | j                  S )N)r.   )typer,   r   r-   r2   )r   memos     r   __deepcopy__zTree.__deepcopy__   s/    tDzz$)XdmT%B%BTTTTr    r)   c                 H     t          |           | j        | j                  S r1   )r   r,   r-   r   s    r   copyz	Tree.copy   s    tDzz$)T]333r    c                 "    || _         || _        d S r1   r6   )r   r,   r-   s      r   setzTree.set   s    	 r    r1   )rD   )r/   r^   )ru   rv   r/   r^   )r/   r)   ) r!   r"   r#   __doc__strr%   r   r   r   propertyr.   r7   r:   r@   rH   rL   rK   rW   rY   r&   r]   rk   rt   ry   r~   r   r   r(   r   r   r   r   r'   r    r   r+   r+   $   sF         " III%%%% S ,C 8TX> cg    
 d    X
; ; ;  
9 
9 
94 4 4c 4 4 4 4" "h'78 "BR " " " "    # # #7# 7 7 7 71 1 1 1$ $ $ 2 2 2 28c 8&? 8 8 8 8   C QXHY     U U U4 4 4 4! !'> !4 ! ! ! ! ! !r    r+   r   c                       e Zd ZdZdS )SlottedTree)r,   r-   ruler2   N)r!   r"   r#   	__slots__r'   r    r   r   r      s        3IIIr    r   LRrP   filenamerankdirzLiteral["TB", "LR", "BT", "RL"]r/   c                 L    t          | |fi |}|                    |           d S r1   )pydot__tree_to_graph	write_pngrP   r   r   kwargsgraphs        r   pydot__tree_to_pngr      s1     w99&99E	OOHr    c                 L    t          | |fi |}|                    |           d S r1   )r   writer   s        r   pydot__tree_to_dotr      s1     w99&99E	KKr    c                 t    ddl  j        dd|d|dgfdf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   Ndigraph)
graph_typer   c                                          d         t          |                     }dxx         dz  cc<                       |           |S )Nr   )labelr   )Noderepradd_node)leafrr   r   r   pydots     r   new_leafz&pydot__tree_to_graph.<locals>.new_leaf   sN    zz!A$d4jjz11	!	tr    c                 \   t          | j                  dz  }|dz  }fd| j        D             }	                    d         dd|z  | j                  }dxx         dz  cc<                       |           |D ]+}                    	                    ||                     ,|S )	Ni i c                 f    g | ]-}t          |t                    r |          n
 |          .S r'   )r?   r+   )rb   rs   	_to_pydotr   s     r   rd   z;pydot__tree_to_graph.<locals>._to_pydot.<locals>.<listcomp>   sO     3 3 3 )35$(?(?TIIe$$$XXe__ 3 3 3r    r   filledz#%x)style	fillcolorr   r   )r[   r,   r-   r   r   add_edgeEdge)
rj   colorsubnodesrr   subnoder   r   r   r   r   s
        r   r   z'pydot__tree_to_graph.<locals>._to_pydot   s    W\""X-3 3 3 3 3!(!13 3 3zz!A$h%%-w|z\\	!	t 	6 	6GNN5::dG445555r    r'   )r   Dot)rP   r   r   r   r   r   r   r   s      @@@@@r   r   r      s     LLLEIFGFFvFFE	
A               IdOOOLr    )r   )sysr   r   typingr   r   r   r   r   r	   r
   r   lexerr   r   rQ   ImportErrorr   r   r(   Branchr+   	ParseTreer   r   r   r   r   r'   r    r   <module>r      s   



       ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ] ))))))))            ')

	w'	(m! m! m! m! m!77 m! m! m!` M	4 4 4 4 4$ 4 4 4 T S ;\ pt    
 T    
& &t & & & & & &s   / 77