Mpj dZddlmZmZmZmZmZmZddlm Z m Z ddl m Z ee e e fZee e e fZee e fZdZGddZGdd e e e e fZGd d Zd ed edefdZGddZde dee fdZy)zThis module defines utilities for matching and translation tree templates. A tree templates is a tree that contains nodes that are template variables. )UnionOptionalMappingDictTupleIterator)Tree Transformer)MissingVariableError$ceZdZdZd dZdeeeefdeefdZ de deefdZ deedd fd Z de d e deefd Zy) TemplateConfz~Template Configuration Allows customization for different uses of Template parse() must return a Tree instance. Nc||_yN)_parse)selfparses a/mnt/ssd/data/Dropbox/adrian/scripts/msg_venv/lib/python3.12/site-packages/lark/tree_templates.py__init__zTemplateConf.__init__s  varreturnct|tr t|St|tr\|jdk(rMt |j dkDr5t|j dtrt|j dSy)zGiven a tree node, if it is a template variable return its name. Otherwise, return None. This method may be overridden for customization Parameters: var: Tree | str - The tree node to test rrN) isinstancestr_get_template_namer datalenchildren)rrs rtest_varzTemplateConf.test_varsj c3 %c* * sD !E!CLL!A%3<<?C0%cll1o6 6rtemplatect|tr|jsJ|j|}t|ts t d|S)Nz+template parser must return a Tree instance)rrrr TypeErrorrr!s r _get_treezTemplateConf._get_tree3sA h $;; ;{{8,H(D)IJ JrTemplatect||S)N)conf)r&r$s r__call__zTemplateConf.__call__=st,,rtreec&|j|}|r"t|tstd|||iSt|tr||k(riSyt|trt|ts Jd|d||j |j k(r~t |jt |jk(rSi}t|j|jD],\}}|j||}|y|j|.|Sy)zAReturns dict of {var: match} if found a match, else None z6Template variables can only match Tree instances. Not Nz template=z tree=) r rr r#rrrrzip_match_tree_templateupdate)rr!r* template_varrest1t2matchess rr-z!TemplateConf._match_tree_template@s }}X. dD)"XY]X` abb $' ' h $4 (D)jt.Dh RZQ[[abfagFhhD ==DII %#h.?.?*@C DV*VCh//? $B33B;? 7#  $Jrr)__name__ __module__ __qualname____doc__rrr rrr TreeOrCoder%r)Branch MatchResultr-rrrrs~E$s)S.1hsm,*c-c-z-Zv(S^J_rrcReZdZdedeeeefddffd Zdeeffd ZxZ S) _ReplaceVarsr(varsrNc>t|||_||_yr)superr_conf_vars)rr(r> __class__s rrz_ReplaceVars.__init___s   rct||||}|jj|}|r |j|S|S#t $rt d|dwxYw)Nz"No mapping for template variable ())r@ __default__rAr rBKeyErrorr )rrrmetar*rrCs rrFz_ReplaceVars.__default__dsrw"448jj!!$'  Xzz#&  X*-OPSuTU+VWW Xs AA) r4r5r6rrrr rrF __classcell__)rCs@rr=r=^s?\d3i1HT 49  rr=ceZdZdZefdeedefdZdede e fdZ dede e eee ffdZdeeeefdeefd Zy ) r&aRepresents a tree template, tied to a specific configuration A tree template is a tree that contains nodes that are template variables. Those variables will match any tree. (future versions may support annotations on the variables, to allow more complex templates) r*r(c>||_|j||_yr)r(r%r*)rr*r(s rrzTemplate.__init__xs NN4( rrc|jj|}|jj|j|S)aMatch a tree template to a tree. A tree template without variables will only match ``tree`` if it is equal to the template. Parameters: tree (Tree): The tree to match to the template Returns: Optional[Dict[str, Tree]]: If match is found, returns a dictionary mapping template variable names to their matching tree nodes. If no match was found, returns None. )r(r%r-r*)rr*s rmatchzTemplate.match|s3yy""4(yy--dii>>rc#K|jj|}|jD]}|j|}|s||fyw)zISearch for all occurrences of the tree template inside ``tree``. N)r(r% iter_subtreesrM)rr*subtreer0s rsearchzTemplate.searchsOyy""4())+ #G**W%Csl" #s AA Ar>c`t|j|j|jS)z(Apply vars to the template tree )r=r( transformr*)rr>s r apply_varszTemplate.apply_varss$DIIt,66tyyAArN)r4r5r6r7rr rrr8rr:rMrrrQrrTr;rrr&r&ps>J^)T#Y)l)?*?+)>? #:#(5cK9O3P*Q#BwsDI~6B49Brr&r1r2r*c|jj|}|j|D]<\}}|j|}|j |j |j >|S)z=Search tree and translate each occurrence of t1 into t2. )r(r%rQrTsetrr)r1r2r*rPr>r0s r translaterWs^ 77  T "D4, mmD! CHHcll+, Krc8eZdZdZdeeeffdZdeefdZ y)TemplateTranslatorz;Utility class for translating a collection of patterns translationscVtd|jDsJ||_y)Nc3fK|])\}}t|txrt|t+ywr)rr&).0kvs r z.TemplateTranslator.__init__..s*h41a:a*Fz!X/FFhs/1)allitemsrZ)rrZs rrzTemplateTranslator.__init__s(hS_SeSeSghhhh(rr*cd|jjD]\}}t|||}|Sr)rZrbrW)rr*r^r_s rrWzTemplateTranslator.translates7%%++- )DAqQ4(D ) rN) r4r5r6r7rr&rr rrWr;rrrYrYs-)WXx-?%@)d3irrYvaluercZ|jtr|jtSdSr) startswith_TEMPLATE_MARKERlstrip)rds rrrs&-2-=-=>N-O5<<( )YUYYrN)r7typingrrrrrrlarkr r lark.exceptionsr rr9r8r:rgrr=r&rWrYrr;rrrls CB"0 tCy#~  49c> " 39o IIX;sDI~.$(B(BV(   ZcZhsmZr