
    )               	       v   S SK r S SKJr  S SKJrJrJrJrJrJ	r	J
r
Jr  \(       a*  SSKJrJr   S SKr\ R"                  S:  a  S SKJr  OS SKJr  S SKJr   " S	 S
5      r\
" S5      r\\S4   r " S S\	\   5      r\S   r " S S\5      rSS\S\SSSS4S jjrSS\4S jjrSS\4S jjrg! \ a     Nf = f)    N)deepcopy)ListCallableIteratorUnionOptionalGenericTypeVarTYPE_CHECKING   )TerminalDefToken)      )Literal)OrderedDictc                   v    \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   \\S'   S	\S
'   \\S'   S rSrg)Meta   emptylinecolumn	start_posend_line
end_columnend_poszList[TerminalDef]orig_expansion
match_treec                     SU l         g )NTr   selfs    lib/third_party/lark/tree.py__init__Meta.__init__    s	    
    r    N)	__name__
__module____qualname____firstlineno__bool__annotations__intr$   __static_attributes__ r&   r#   r   r      s6    K
IKNMOL''r&   r   _Leaf_TTree[_Leaf_T]c            	       >   \ rS rSr% Sr\\S'   S\S'   S$S\SSS\\   SS4S	 jjr	\
S\4S
 j5       rS rS rS rS%S\S\4S jjrS$S\S   SS4S jjrS rS rS rS\4S jrS&S jrS rS'S jrS\SS4S jrS rSSS\\   4S jrS  rS(S! jrS\SSSS4S" j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
datazList[Branch[_Leaf_T]]childrenNmetareturnc                 (    Xl         X l        X0l        g N)r5   r6   _meta)r"   r5   r6   r7   s       r#   r$   Tree.__init__8   s    	 
r&   c                 R    U R                   c  [        5       U l         U R                   $ r:   )r;   r   r!   s    r#   r7   	Tree.meta=   s    ::DJzzr&   c                 @    SU R                   < SU R                  < S3$ )NzTree(z, )r5   r6   r!   s    r#   __repr__Tree.__repr__C   s    !%DMM::r&   c                     U R                   $ r:   r5   r!   s    r#   _pretty_labelTree._pretty_labelF   s    yyr&   c              #     #    X!-   U R                  5        3v   [        U R                  5      S:X  a8  [        U R                  S   [        5      (       d  SU R                  S    S3v   g Sv   U R                   HE  n[        U[        5      (       a  UR                  US-   U5       S h  vN   M7  X!S-   -   U S3v   MG     g  N7f)Nr   r   	
)rF   lenr6   
isinstancer3   _pretty)r"   level
indent_strns       r#   rM   Tree._prettyI   s     !"4#5#5#7"899t}}":dmmA6F+M+Mt}}Q'(++J]]a&& yyq*==='q121#R88	 #=s   B*C,C-CrO   c                 D    SR                  U R                  SU5      5      $ )zMReturns an indented string representation of the tree.

Great for debugging.
 r   )joinrM   )r"   rO   s     r#   prettyTree.prettyU   s    
 wwt||Az233r&   parentzrich.tree.Treec                 $    U R                  U5      $ )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"   rW   s     r#   __rich__Tree.__rich__\   s     zz&!!r&   c                 F   U(       a   UR                  SU R                   S35      nO)SS KnUR                  R	                  U R                  5      nU R
                   H@  n[        U[        5      (       a  UR                  U5        M+  UR                  SU S35        MB     U$ )Nz[bold]z[/bold]r   z[green]z[/green])addr5   	rich.treetreer3   r6   rL   rY   )r"   rW   r_   richcs        r#   rY   
Tree._richi   s{    ::tyyk9:D99>>$)),DA!T""71#X./	  r&   c                      U R                   UR                   :H  =(       a    U R                  UR                  :H  $ ! [         a     gf = f)NF)r5   r6   AttributeErrorr"   others     r#   __eq__Tree.__eq__x   s>    	99

*Nt}}/NN 		s   8; 
AAc                     X:X  + $ r:   r/   re   s     r#   __ne__Tree.__ne__~   s    ""r&   c                 V    [        U R                  [        U R                  5      45      $ r:   )hashr5   tupler6   r!   s    r#   __hash__Tree.__hash__   s    TYYdmm 4566r&   Iterator[Tree[_Leaf_T]]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reversedr6   rL   r3   listvalues)r"   queuesubtreessubtreera   s        r#   iter_subtreesTree.iter_subtrees   s    
 =G$+R[!'*:*:!; H!;A#At, 13Ah1F !; H HE  X__./00	Hs   BB&Bc              #      #    U /nUR                   nUR                  nU(       aP  U" 5       n[        U[        5      (       d  M%  Uv   [	        UR
                  5       H  nU" U5        M     U(       a  MO  gg7f)zdBreadth-first iteration.

Iterates over all the subtrees, return nodes in order like pretty() does.
N)appendpoprL   r3   rt   r6   )r"   stackstack_append	stack_popnodechilds         r#   iter_subtrees_topdownTree.iter_subtrees_topdown   sa     
 ||II	;DdD))J!$--0U# 1 es   A0A64A6predc                 4    [        XR                  5       5      $ )z?Returns all nodes of the tree that evaluate pred(node) as true.)filterrz   )r"   r   s     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:   rE   )tr5   s    r#   <lambda> Tree.find_data.<locals>.<lambda>   s    $r&   )r   )r"   r5   s    `r#   	find_dataTree.find_data   s    ~~677r&   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)rangerK   r6   rL   r3   r5   )r"   data_valueschangedir   s        r#   expand_kids_by_dataTree.expand_kids_by_data   sp    s4==)!+R4AMM!$E%&&5::+D',~~a!$	 5
 r&   z!Callable[[Branch[_Leaf_T]], bool]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)r6   rL   r3   scan_values)r"   r   ra   r   s       r#   r   Tree.scan_values   sJ      A!T""t,AG - 77G s   AA	Ac                 t    [        U 5      " U R                  [        U R                  U5      U R                  S9$ )N)r7   )typer5   r   r6   r;   )r"   memos     r#   __deepcopy__Tree.__deepcopy__   s(    Dz$))XdmmT%BTTr&   c                 N    [        U 5      " U R                  U R                  5      $ r:   )r   r5   r6   r!   s    r#   copy	Tree.copy   s    Dz$))T]]33r&   c                     Xl         X l        g r:   rA   )r"   r5   r6   s      r#   setTree.set   s    	 r&   )r;   r6   r5   r:   )z  )r8   rq   )r   zCallable[[Tree[_Leaf_T]], bool]r8   rq   )r8   r1   )"r'   r(   r)   r*   __doc__strr,   r   r   r$   propertyr7   rB   rF   rM   rU   rZ   rY   rg   rj   r-   ro   rz   r   r   r   r   r   r0   r   r   r   r   r.   r/   r&   r#   r3   r3   (   s   
 I%%S ,C 8TX> cg 
 d  
;
94 4c 4"h'78 "BR "#7# 71 $ 28c 8&? 8 C QXHY  U4! !'> !4 !r&   r3   r   c                       \ rS rSrSrSrg)SlottedTree   )r5   r6   ruler;   r/   N)r'   r(   r)   r*   	__slots__r.   r/   r&   r#   r   r      s    3Ir&   r   r_   filenamerankdirzLiteral["TB", "LR", "BT", "RL"]r8   c                 >    [        X40 UD6nUR                  U5        g r:   )pydot__tree_to_graph	write_pngr_   r   r   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)leafr   r   r   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   )	rm   r5   r6   rL   r3   r   r   add_edgeEdge)ry   colorr   subnodesr   subnode	_to_pydotr   r   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   r   r   r   s      @@@@@r#   r   r      sD     IIFGFvFE	
A  dOLr&   )LR) sysr   r   typingr   r   r   r   r   r	   r
   r   lexerr   r   r`   ImportErrorversion_infor   typing_extensionscollectionsr   r   r0   Branchr3   	ParseTreer   r   r   r   r   r/   r&   r#   <module>r      s    
  ] ] ]) 6!"- $   )
	w'	(i!77 i!X M	4$ 4T S ;\ pt 
T 
&t &w  s   B/ /B87B8