Mpj # ,ddlZddlmZddlmZmZmZmZmZm Z m Z m Z ddl m Z e rddl mZ ddlZddlmZGdd Ze d Zeed fZGd d e eZedZGddeZddededdddfdZddefdZddefdZy#e$rY`wxYw)N)deepcopy)ListCallableIteratorUnionOptionalGenericTypeVar TYPE_CHECKING)Token) TerminalDef)LiteralcneZdZUeed<eed<eed<eed<eed<eed<eed<ded <eed <d Zy ) Metaemptylinecolumn start_posend_line end_columnend_poszList[TerminalDef]orig_expansion match_treecd|_y)NT)rselfs W/mnt/ssd/data/Dropbox/adrian/scripts/msg_venv/lib/python3.12/site-packages/lark/tree.py__init__z Meta.__init__s  N)__name__ __module__ __qualname__bool__annotations__intrr rrrs6 K I KNMO L''r r_Leaf_T Tree[_Leaf_T]c 4eZdZUdZeed<ded<d%dedddeeddfdZe defd Z d Z d Z d Z d Zd&dedefdZd%dedddfdZdZdZdZdefdZd'dZdZd(dZdeddfdZdedeefdZdZdd deefd!Zd"Zd)d#Z deddddfd$Z!y)*TreeaThe 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]]childrenNmetareturnc.||_||_||_yN)r,r-_meta)rr,r-r.s rrz Tree.__init__;s    r cP|jt|_|jSr1)r2rrs rr.z Tree.meta@s :: DJzzr c<d|jd|jdS)NzTree(z, )r,r-rs r__repr__z Tree.__repr__Fs!%DMM::r r6c|jSr1r,rs r _pretty_labelzTree._pretty_labelKs yyr c#|K||z|jt|jdk(r3t|jdtsd|jddyd|jD]?}t|tr|j |dz|Ed{1||dzz|dAy7w)Nr r  )r:lenr- isinstancer+_pretty)rlevel indent_strns rr@z Tree._prettyNsE!"4#5#5#7"899 t}}  ":dmmA6F+Mt}}Q'(+ +J]] 9a& yyq*==='q121#R88  9=sB B<"B:#B<rBcDdj|jd|S)z]Returns an indented string representation of the tree. Great for debugging. r)joinr@)rrBs rprettyz Tree.prettyZs wwt||Az233r parentzrich.tree.Treec$|j|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)rrHs r__rich__z Tree.__rich__aszz&!!r c,|r |jd|jd}n)ddl}|jj |j}|j D]9}t |tr|j|%|jd|d;|S)Nz[bold]z[/bold]rz[green]z[/green])addr, rich.treetreer+r-r?rJ)rrHrOrichcs rrJz Tree._richns} ::tyyk9:D 99>>$)),D 0A!T" 71#X./  0  r c |j|jk(xr|j|jk(S#t$rYywxYw)NF)r,r-AttributeErrorrothers r__eq__z Tree.__eq__}s> 99 *Nt}}/N N  s36 AAc||k( Sr1r'rTs r__ne__z Tree.__ne__sEM""r cVt|jt|jfSr1)hashr,tupler-rs r__hash__z Tree.__hash__sTYYdmm 4566r Iterator[Tree[_Leaf_T]]c |g}t}|D]S}||t|<|t|jDcgc]!}t |t rt||vr|#c}z }U~tt |jScc}w)zDepth-first iteration. Iterates over all the subtrees, never returning to the same node twice (Lark's parse-tree is actually a DAG). )dictidreversedr-r?r+listvalues)rqueuesubtreessubtreerQs r iter_subtreeszTree.iter_subtreess 6 HG$+HR[ ! '*:*:!;HA#At,Ah1FH HE H X__./00 Hs&B c#K|g}|j}|j}|rB|}t|ts|t |j D] }|| |rAyyw)ztBreadth-first iteration. Iterates over all the subtrees, return nodes in order like pretty() does. N)appendpopr?r+rar-)rstack stack_append stack_popnodechilds riter_subtrees_topdownzTree.iter_subtrees_topdownsb || II ;DdD)J!$--0 $U# $ s AA#!A#predc6t||jS)z?Returns all nodes of the tree that evaluate pred(node) as true.)filterrg)rrqs r find_predzTree.find_predsdD..011r c,|jfdS)z?Returns all nodes of the tree whose data equals the given data.c"|jk(Sr1r9)tr,s rz Tree.find_data..s$r )rt)rr,s `r find_datazTree.find_datas~~677r token_typec,|jfdS)zReturns all tokens whose type equals the given token_type. This is a recursive function that will find tokens in all the subtrees. Example: >>> term_tokens = tree.find_token('TERM') cFt|txr|jk(Sr1)r?r type)vrzs rrxz!Tree.find_token..s*Q*>*W166ZCWr ) scan_values)rrzs `r find_tokenzTree.find_tokens WXXr cd}tt|jdz ddD]O}|j|}t|ts#|j |vs2|j|j||dzd}Q|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_valueschangediros rexpand_kids_by_datazTree.expand_kids_by_datasts4==)!+R4 AMM!$E%&5::+D',~~ a!$   r z!Callable[[Branch[_Leaf_T]], bool]c#K|jD]:}t|tr|j|D]}|.||s7|<yw)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+r)rrqrQrws rrzTree.scan_valuessP A!T"t,AG7G  s AA A czt||jt|j||jS)N)r.)r}r,rr-r2)rmemos r __deepcopy__zTree.__deepcopy__s*tDz$))XdmmT%BTTr cNt||j|jSr1)r}r,r-rs rcopyz Tree.copystDz$))T]]33r c ||_||_yr1r6)rr,r-s rsetzTree.sets   r r1)z )r/r])rqzCallable[[Tree[_Leaf_T]], bool]r/r])r/r))"r!r"r#__doc__strr%rrrpropertyr.r7__match_args__r:r@rGrKrJrVrXr&r\rgrprtryrr(rrrrrrr'r rr+r+&s8" I%%S,C8TX>cg d ;*N 944c4 "h'78 "BR "  #7#7 1$ 28c8&?8 YSYXg->Y CQXHY U4!!'>!4!r r+r ceZdZdZy) SlottedTree)r,r-ruler2N)r!r"r# __slots__r'r rrrs3Ir rrOfilenamerankdirzLiteral["TB", "LR", "BT", "RL"]r/c @t||fi|}|j|yr1)pydot__tree_to_graph write_pngrOrrkwargsgraphs rpydot__tree_to_pngrs w 9& 9E OOHr c @t||fi|}|j|yr1)rwriters rpydot__tree_to_dotrs w 9& 9E KKr c xddljdd|d|dgfdfd|S)aCreates 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. rNdigraph) graph_typercjdt|}dxxdz cc<j||S)Nr)labelr )Noderepradd_node)leafrnrrpydots rnew_leafz&pydot__tree_to_graph..new_leafs;zz!A$d4jz1 !  t r ct|jdz}|dz}|jDcgc]"}t|tr|n |$}} j ddd|z|j}dxxdz cc<j ||D]#}j j||%|Scc}w)Niirfilledz#%x)style fillcolorrr ) rZr,r-r?r+rradd_edgeEdge) rfcolorrosubnodesrnsubnode _to_pydotrrrrs rrz'pydot__tree_to_graph.._to_pydot sW\\"X- ")!1!13)35$(?Ie$Xe_T33zz!A$h%%-w||z\ !  t 6G NN5::dG4 5 6 3s'Cr')rDot)rOrrrrrrrs @@@@@rrrsF EII FG Fv FE A  dO Lr )LR)sysrrtypingrrrrrr r r lexerr rrP ImportErrorrrr(Branchr+ ParseTreerrrrrr'r rrs ]]]"    )  w' (y!77 y!x M 4$4TS;\pt T &t&O    sB BB