� K�gA#� ��ddlZddlmZddlmZmZmZmZmZm Z m Z m Z e r1ddl m Z mZ ddlZn #e$rYnwxYwejdkrddlmZnddlmZddlmZGd �d ��Ze d ��Zeed fZGd �de e��ZedZGd�de��Zddededdddfd�Zddefd�Zddefd�ZdS)�N)�deepcopy)�List�Callable�Iterator�Union�Optional�Generic�TypeVar� TYPE_CHECKING�)� TerminalDef�Token)��)�Literal)� OrderedDictc�p�eZdZUeed<eed<eed<eed<eed<eed<eed<ded <eed <d �Zd S) �Meta�empty�line�column� start_pos�end_line� end_column�end_poszList[TerminalDef]�orig_expansion� match_treec��d|_dS)NT)r��selfs �Y/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/lark/tree.py�__init__z Meta.__init__ s ���� � � �N)�__name__� __module__� __qualname__�bool�__annotations__�intr"�r#r!rrs|������� �K�K�K� �I�I�I� �K�K�K��N�N�N��M�M�M��O�O�O� �L�L�L�'�'�'�'���������r#r�_Leaf_T� Tree[_Leaf_T]c �4�eZdZUdZeed<ded<d%dedddeeddfd�Ze defd ���Z d �Z d �Z d �Z d&dedefd�Zd%dedddfd�Zd�Zd�Zd�Zdefd�Zd'd�Zd�Zd(d�Zdeddfd�Zd�Zdddeefd �Zd!�Zd)d#�Zdeddddfd$�ZdS)*�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. �datazList[Branch[_Leaf_T]]�childrenN�meta�returnc�0�||_||_||_dS�N)r/r0�_meta)r r/r0r1s r!r"z Tree.__init__=s���� � �� ��� � � r#c�D�|j�t��|_|jSr4)r5rrs r!r1z Tree.metaBs�� �:� ����D�J��z�r#c�(�d|j�d|j�d�S)NzTree(z, �)�r/r0rs r!�__repr__z Tree.__repr__Hs���!%����D�M�M�M�:�:r#c��|jSr4�r/rs r!� _pretty_labelzTree._pretty_labelKs ���y�r#c#�K�||z�|�����V�t|j��dkr5t|jdt��sd|jd�d�V�dSdV�|jD]F}t|t��r |�|dz|��Ed{V���7||dzz�|�d�V��GdS)Nr r� � )r=�lenr0� isinstancer.�_pretty)r �level� indent_str�ns r!rCz Tree._prettyNs�����E�!�9�4�#5�#5�#7�#7�9�9�9�9�9� �t�}� � �� "� "�:�d�m�A�6F��+M�+M� "�+�t�}�Q�'�+�+�+� +� +� +� +� +��J�J�J��]� 9� 9���a��&�&�9� �y�y��q��*�=�=�=�=�=�=�=�=�=�=�'��q��1�8�1�8�8�8�8�8�8�8�  9� 9r#� rEc�T�d�|�d|����S)z]Returns an indented string representation of the tree. Great for debugging. �r)�joinrC)r rEs r!�prettyz Tree.prettyZs$�� �w�w�t�|�|�A�z�2�2�3�3�3r#�parentzrich.tree.Treec�,�|�|��S)z�Returns 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 rLs r!�__rich__z Tree.__rich__as���z�z�&�!�!�!r#c�*�|r|�d|j�d���}n#ddl}|j�|j��}|jD]F}t |t��r|�|���-|�d|�d����G|S)Nz[bold]z[/bold]rz[green]z[/green])�addr/� rich.tree�treer.r0rBrN)r rLrS�rich�cs r!rNz Tree._richns��� � -��:�:�9�t�y�9�9�9�:�:�D�D� � � � ��9�>�>�$�)�,�,�D��� 0� 0�A��!�T�"�"� 0����� � � � ����.�1�.�.�.�/�/�/�/�� r#c�f� |j|jko|j|jkS#t$rYdSwxYw)NF)r/r0�AttributeError�r �others r!�__eq__z Tree.__eq__}sF�� ��9�� �*�N�t�}���/N� N��� � � ��5�5� ���s �"� 0�0c��||k Sr4r*rXs r!�__ne__z Tree.__ne__�s���E�M�"�"r#c�R�t|jt|j��f��Sr4)�hashr/�tupler0rs r!�__hash__z Tree.__hash__�s!���T�Y��d�m� 4� 4�5�6�6�6r#�Iterator[Tree[_Leaf_T]]c����|g}t���|D]7}|�t|��<|�fd�t|j��D��z }�8~tt ��������S)z�Depth-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��rt|���v�(|��+Sr*)rBr.�id)�.0rU�subtreess �r!� <listcomp>z&Tree.iter_subtrees.<locals>.<listcomp>�sI���H�H�H�A�#�A�t�,�,�H�13�A���h�1F�1F��1F�1F�1Fr#)rrd�reversedr0�list�values)r �queue�subtreerfs @r!� iter_subtreeszTree.iter_subtrees�s���� ����=�=��� H� H�G�$+�H�R��[�[� !� �H�H�H�H��'�*:�!;�!;�H�H�H� H�E�E� ���X�_�_�.�.�/�/�0�0�0r#c#��K�|g}|j}|j}|rJ|��}t|t��s�"|V�t |j��D] }||���|�HdSdS)ztBreadth-first iteration. Iterates over all the subtrees, return nodes in order like pretty() does. N)�append�poprBr.rhr0)r �stack� stack_append� stack_pop�node�childs r!�iter_subtrees_topdownzTree.iter_subtrees_topdown�s����� ����|� ��I� �� $��9�;�;�D��d�D�)�)� ���J�J�J�!�$�-�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.)�filterrm)r rws r!� find_predzTree.find_pred�s���d�D�.�.�0�0�1�1�1r#c�4��|��fd���S)z?Returns all nodes of the tree whose data equals the given data.c���|j�kSr4r<)�tr/s �r!�<lambda>z Tree.find_data.<locals>.<lambda>�s �����$��r#)r{)r r/s `r!� find_datazTree.find_data�s����~�~�6�6�6�6�7�7�7r#c���d}tt|j��dz dd��D]C}|j|}t|t��r|j|vr|j|j||dz�<d}�D|S)z\Expand (inline) children with any of the given data values. Returns True if anything changedFr �����T)�rangerAr0rBr.r/)r � data_values�changed�irus r!�expand_kids_by_datazTree.expand_kids_by_data�s|�����s�4�=�)�)�!�+�R��4�4� � �A��M�!�$�E��%��&�&� �5�:��+D�+D�',�~�� �a��!��e�$�����r#z!Callable[[Branch[_Leaf_T]], bool]c#�K�|jD]C}t|t��r|�|��D]}|V���4||��r|V��DdS)z�Return 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)r0rBr.� scan_values)r rwrUr~s r!r�zTree.scan_values�s|������ � �A��!�T�"�"� ����t�,�,���A��G�G�G�G���4��7�7���G�G�G��  � r#c�r�t|��|jt|j|��|j���S)N)r1)�typer/rr0r5)r �memos r!� __deepcopy__zTree.__deepcopy__�s/���t�D�z�z�$�)�X�d�m�T�%B�%B���T�T�T�Tr#r,c�H�t|��|j|j��Sr4)r�r/r0rs r!�copyz Tree.copy�s���t�D�z�z�$�)�T�]�3�3�3r#c�"�||_||_dSr4r9)r r/r0s r!�setzTree.set�s���� � �� � � r#r4)rG)r2ra)rwrxr2ra)r2r,) r$r%r&�__doc__�strr(rrr"�propertyr1r:r=rCrKrOrNrZr\r)r`rmrvr{r�r�rr+r�r�r�r�r*r#r!r.r.(sF���������" �I�I�I�%�%�%�%���S��,C��8�TX�>��cg����� ��d�����X�� ;�;�;���� 9� 9� 9�4�4��4�c�4�4�4�4� "� "�h�'7�8� "�BR� "� "� "� "� � � ���� #�#�#�7�#�7�7�7�7�1�1�1�1� $�$�$� 2�2�2�2�8�c�8�&?�8�8�8�8� ���� C���QX�HY����� U�U�U�4�4�4�4�!��!�'>�!�4�!�!�!�!�!�!r#r.rc��eZdZdZdS)� SlottedTree)r/r0�ruler5N)r$r%r&� __slots__r*r#r!r�r��s������3�I�I�Ir#r��LRrS�filename�rankdirzLiteral["TB", "LR", "BT", "RL"]r2c �L�t||fi|��}|�|��dSr4)�pydot__tree_to_graph� write_png�rSr�r��kwargs�graphs r!�pydot__tree_to_pngr��s1�� ��w� 9� 9�&� 9� 9�E� �O�O�H�����r#c �L�t||fi|��}|�|��dSr4)r��writer�s r!�pydot__tree_to_dotr��s1�� ��w� 9� 9�&� 9� 9�E� �K�K������r#c �t������ddl��jdd|d�|���dg����fd�������fd���|���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. rN�digraph)� graph_typer�c������dt|�����}�dxxdz cc<��|��|S)Nr)�labelr )�Node�repr�add_node)�leafrtr�r��pydots ���r!�new_leafz&pydot__tree_to_graph.<locals>.new_leaf�sN����z�z�!�A�$�d�4�j�j�z�1�1�� �!����� ���� ���t����� r#c�\��t|j��dz}|dz}��fd�|jD��}� ��ddd|z|j���}�dxxdz cc<��|��|D]+}��� �||�����,|S) Ni���i���c�f��g|]-}t|t��r �|��n �|����.Sr*)rBr.)reru� _to_pydotr�s ��r!rgz;pydot__tree_to_graph.<locals>._to_pydot.<locals>.<listcomp>sO���3�3�3��)3�5�$�(?�(?�T�I�I�e�$�$�$�X�X�e�_�_�3�3�3r#r�filledz#%x)�style� fillcolorr�r )r^r/r0r�r��add_edge�Edge) rl�color�subnodesrt�subnoder�r�r�r�r�s �����r!r�z'pydot__tree_to_graph.<locals>._to_pydots�����W�\�"�"�X�-�� ����3�3�3�3�3�!(�!1�3�3�3���z�z�!�A�$�h�%�%�-�w�|�z�\�\�� �!����� ���� ���t����� 6� 6�G� �N�N�5�:�:�d�G�4�4� 5� 5� 5� 5�� r#r*)r��Dot)rSr�r�r�r�r�r�r�s @@@@@r!r�r��s���������L�L�L� �E�I� F��G� F� F�v� F� F�E� ��A�������� � � � � � � � � ��I�d�O�O�O� �Lr#)r�) �sysr�r�typingrrrrrr r r �lexerr rrT� ImportError� version_infor�typing_extensions� collectionsrrr+�Branchr.� ParseTreer�r�r�r�r�r*r#r!�<module>r�s+�� � � � �������]�]�]�]�]�]�]�]�]�]�]�]�]�]�]�]�]�]�]�]�� .�)�)�)�)�)�)�)�)� �� � � � �� � � � � �� ���� ��6�!�!�"�"�"�"�"�"�"�-�-�-�-�-�-�$�#�#�#�#�#� � � � � � � � � �'�)� � �� �w��'� (��n!�n!�n!�n!�n!�7�7� �n!�n!�n!�b ��M� �4�4�4�4�4�$�4�4�4���T��S��;\��pt����� ��T����� &�&�t�&�&�&�&�&�&s �/�7�7
Memory