
    Ii                          S SK Jr  S SKJr  S SKJr   " S S5      r " S S\5      r	 " S S	\	5      r
SS
 jrSS jrSS jrg! \ a     NDf = f)    )filter)deepcopy)OrderedDictc                       \ rS rSrS rSrg)Meta   c                     SU l         g )NTemptyselfs    //venv/lib/python3.13/site-packages/lark/tree.py__init__Meta.__init__   s	    
    r
   N)__name__
__module____qualname____firstlineno__r   __static_attributes__ r   r   r   r      s    r   r   c                       \ rS rSrSrSS jr\S 5       rS rS r	S r
SS	 jrS
 rS rS rS rS rS rS rS rS rS rS rS rS r\S 5       r\S 5       r\S 5       r\S 5       rSrg)Tree   a  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
Nc                 (    Xl         X l        X0l        g N)datachildren_meta)r   r   r   metas       r   r   Tree.__init__   s    	 
r   c                 R    U R                   c  [        5       U l         U R                   $ r   )r   r   r   s    r   r    	Tree.meta#   s    ::DJzzr   c                 @    SU R                   < SU R                  < S3$ )NzTree(z, )r   r   r   s    r   __repr__Tree.__repr__)   s    !%DMM::r   c                     U R                   $ r   r   r   s    r   _pretty_labelTree._pretty_label,   s    yyr   c                    [        U R                  5      S:X  aH  [        U R                  S   [        5      (       d&  X!-  U R	                  5       SU R                  S   < S/$ X!-  U R	                  5       S/nU R                   H@  n[        U[        5      (       a  X4R                  US-   U5      -  nM1  X2US-   -  U< S/-  nMB     U$ )N   r   	
)lenr   
isinstancer   r+   _pretty)r   level
indent_strlns        r   r3   Tree._pretty/   s    t}}":dmmA6F+M+M$d&8&8&:D$--XYJZ^bcct113T:A!T""YYuQw
33%'*A>>	  r   c                 D    SR                  U R                  SU5      5      $ )zMReturns an indented string representation of the tree.

Great for debugging.
 r   )joinr3   )r   r5   s     r   prettyTree.pretty<   s    
 wwt||Az233r   c                      U R                   UR                   :H  =(       a    U R                  UR                  :H  $ ! [         a     gf = f)NF)r   r   AttributeErrorr   others     r   __eq__Tree.__eq__C   s>    	99

*Nt}}/NN 		s   8; 
AAc                     X:X  + $ r   r   r@   s     r   __ne__Tree.__ne__I   s    ""r   c                 V    [        U R                  [        U R                  5      45      $ r   )hashr   tupler   r   s    r   __hash__Tree.__hash__L   s    TYYdmm 4566r   c           	      :   U /n[        5       nU Ha  nX2[        U5      '   U[        UR                  5       Vs/ s H-  n[	        U[
        5      (       d  M  [        U5      U;  d  M+  UPM/     sn-  nMc     A[        [        UR                  5       5      5      $ s  snf )zDepth-first iteration.

Iterates over all the subtrees, never returning to the same node twice (Lark's parse-tree is actually a DAG).
)r   idreversedr   r2   r   listvalues)r   queuesubtreessubtreecs        r   iter_subtreesTree.iter_subtreesO   s    
 =G$+R[!'*:*:!; H!;A#At, 13Ah1F !; H HE 
 X__./00	Hs   BB&Bc                 4    [        XR                  5       5      $ )z?Returns all nodes of the tree that evaluate pred(node) as true.)r   rU   )r   preds     r   	find_predTree.find_pred^   s    d..011r   c                 .   ^ U R                  U4S j5      $ )z?Returns all nodes of the tree whose data equals the given data.c                 "   > U R                   T:H  $ r   r*   )tr   s    r   <lambda> Tree.find_data.<locals>.<lambda>d   s    $r   )rY   )r   r   s    `r   	find_dataTree.find_datab   s    ~~677r   c                 z    [        USS9 H-  nU R                  U   nUR                  U R                  X"S-   & M/     g)z-Expand (inline) children at the given indicesT)reverser.   N)sortedr   )r   indicesikids       r   expand_kids_by_indexTree.expand_kids_by_indexh   s7    .A--"C#&<<DMM!aC  /r   c                    Sn[        [        U R                  5      S-
  SS5       HX  nU R                  U   n[        U[        5      (       d  M)  UR
                  U;   d  M;  UR                  U R                  X3S-   & SnMZ     U$ )z\Expand (inline) children with any of the given data values. Returns True if anything changedFr.   T)ranger1   r   r2   r   r   )r   data_valueschangedrf   childs        r   expand_kids_by_dataTree.expand_kids_by_datan   sp    s4==)!+R4AMM!$E%&&5::+D',~~a!$	 5
 r   c              #      #    U R                    HI  n[        U[        5      (       a  UR                  U5       H  nUv   M	     M6  U" U5      (       d  ME  Uv   MK     g7f)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   rX   rT   r]   s       r   rs   Tree.scan_valuesy   sJ      A!T""t,AG - 77G s   AA	Ac              #      #    U /nU(       ab  UR                  5       n[        U[        5      (       d  M.  Uv   [        UR                  5       H  nUR                  U5        M     U(       a  Ma  gg7f)zdBreadth-first iteration.

Iterates over all the subtrees, return nodes in order like pretty() does.
N)popr2   r   rN   r   append)r   stacknoder7   s       r   iter_subtrees_topdownTree.iter_subtrees_topdown   sV     
 99;DdD))Jdmm,Q - es   A*A0.A0c                 t    [        U 5      " U R                  [        U R                  U5      U R                  S9$ )N)r    )typer   r   r   r   )r   memos     r   __deepcopy__Tree.__deepcopy__   s(    Dz$))XdmmT%BTTr   c                 N    [        U 5      " U R                  U R                  5      $ r   )r}   r   r   r   s    r   copy	Tree.copy   s    Dz$))T]]33r   c                     Xl         X l        g r   r&   )r   r   r   s      r   setTree.set   s    	 r   c                 .    U R                   R                  $ r   )r    liner   s    r   r   	Tree.line   s    yy~~r   c                 .    U R                   R                  $ r   )r    columnr   s    r   r   Tree.column   s    yyr   c                 .    U R                   R                  $ r   )r    end_liner   s    r   r   Tree.end_line   s    yy!!!r   c                 .    U R                   R                  $ r   )r    
end_columnr   s    r   r   Tree.end_column   s    yy###r   )r   r   r   r   )z  )r   r   r   r   __doc__r   propertyr    r'   r+   r3   r<   rB   rE   rJ   rU   rY   r`   rh   rp   rs   rz   r   r   r   r   r   r   r   r   r   r   r   r   r      s    

  
;4#71280  U4!
       " " $ $r   r   c                       \ rS rSrSrSrg)SlottedTree   )r   r   ruler   r   N)r   r   r   r   	__slots__r   r   r   r   r   r      s    3Ir   r   c                 >    [        X40 UD6nUR                  U5        g r   )pydot__tree_to_graph	write_pngtreefilenamerankdirkwargsgraphs        r   pydot__tree_to_pngr      s     9&9E	OOHr   c                 >    [        X40 UD6nUR                  U5        g r   )r   writer   s        r   pydot__tree_to_dotr      s     9&9E	KKr   c                 |   ^^^^^ SSK mTR                  " SSUS.UD6mS/mUUU4S jmUUUUU4S jmT" U 5        T$ )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                 |   > TR                  TS   [        U 5      S9nTS==   S-  ss'   TR                  U5        U$ )Nr   )labelr.   )Noderepradd_node)leafry   r   rf   pydots     r   new_leaf&pydot__tree_to_graph.<locals>.new_leaf   s;    zz!A$d4jz1	!	tr   c                   > [        U R                  5      S-  nUS-  nU R                   Vs/ s H(  n[        U[        5      (       a  T" U5      OT	" U5      PM*     nnT
R                  TS   SSU-  U R                  S9nTS==   S-  ss'   TR                  U5        U H#  nTR                  T
R                  XE5      5        M%     U$ s  snf )Ni i r   filledz#%x)style	fillcolorr   r.   )	rH   r   r   r2   r   r   r   add_edgeEdge)rS   colorro   subnodesry   subnode	_to_pydotr   rf   r   r   s         r   r   'pydot__tree_to_graph.<locals>._to_pydot   s    W\\"X- ")!1!13!1 )35$(?(?Ie$Xe_T!1 	 3zz!A$h%%-w||z\	!	tGNN5::d45   3s   /C	r   )r   Dot)r   r   r   r   r   rf   r   r   s      @@@@@r   r   r      sD     IIFGFvFE	
A  dOLr   N)LR)future_builtinsr   ImportErrorr   r   collectionsr   r   objectr   r   r   r   r   r   r   r   <module>r      s`   	&  $ 
^$6 ^$B4$ 4

&}  		s   A AA