
    Xh                     
   d Z ddlmZmZmZmZmZmZ ddlm	Z	m
Z
 ddlmZ ee	e   ef   Zee	e   ef   Zeee	f   ZdZ G d d      Z G d d	e
ee	e   f         Z G d
 d      ZdededefdZ G d d      Zdedee   fdZy)zThis module defines utilities for matching and translation tree templates.

A tree templates is a tree that contains nodes that are template variables.

    )UnionOptionalMappingDictTupleIterator)TreeTransformer)MissingVariableError$c                       e Zd ZdZddZdeee   ef   dee   fdZ	de
dee   fdZdee   dd	fd
Zde
dedee   fdZy)TemplateConfz~Template Configuration

    Allows customization for different uses of Template

    parse() must return a Tree instance.
    Nc                     || _         y N)_parse)selfparses     G/var/www/html/myenv/lib/python3.12/site-packages/lark/tree_templates.py__init__zTemplateConf.__init__   s	        varreturnc                    t        |t              rt        |      S t        |t              r\|j                  dk(  rMt        |j                        dkD  r5t        |j                  d   t              rt        |j                  d         S y)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

        r   r   N)
isinstancestr_get_template_namer	   datalenchildren)r   r   s     r   test_varzTemplateConf.test_var   sj     c3%c** sD!E!CLL!A%3<<?C0%cll1o66r   templatec                     t        |t              r| j                  sJ | j                  |      }t        |t              st	        d      |S )Nz+template parser must return a Tree instance)r   r   r   r	   	TypeErrorr   r!   s     r   	_get_treezTemplateConf._get_tree3   sA    h$;;;{{8,H(D)IJJr   Templatec                     t        ||       S )N)conf)r&   r$   s     r   __call__zTemplateConf.__call__=   s    t,,r   treec                 &   | j                  |      }|r"t        |t              st        d|      ||iS t        |t              r||k(  ri S yt        |t              rt        |t              sJ d| d|        |j
                  |j
                  k(  r~t        |j                        t        |j                        k(  rSi }t        |j                  |j                        D ],  \  }}| j                  ||      }| y|j                  |       . |S y)zAReturns dict of {var: match} if found a match, else None
        z6Template variables can only match Tree instances. Not Nz	template=z tree=)r    r   r	   r#   r   r   r   r   zip_match_tree_templateupdate)r   r!   r*   template_varrest1t2matchess           r   r-   z!TemplateConf._match_tree_template@   s    }}X.dD)"XY]X` abb $''h$4	(D)jt.Dh	RZQ[[abfagFhhD==DII%#h.?.?*@CDV*VCh//? $B33B;?

7#$ Jr   r   )__name__
__module____qualname____doc__r   r   r	   r   r   r    
TreeOrCoder%   r)   BranchMatchResultr-    r   r   r   r      s~    E$s)S.1 hsm ,* c -c -z -Z v (S^J_ r   r   c                   R     e Zd Zdedeeee   f   ddf fdZdee   f fdZ xZ	S )_ReplaceVarsr(   varsr   Nc                 >    t         |           || _        || _        y r   )superr   _conf_vars)r   r(   r>   	__class__s      r   r   z_ReplaceVars.__init___   s    

r   c                     t         |   |||      }| j                  j                  |      }|r	 | j                  |   S |S # t
        $ r t        d| d      w xY w)Nz"No mapping for template variable ())r@   __default__rA   r    rB   KeyErrorr   )r   r   r   metar*   r   rC   s         r   rF   z_ReplaceVars.__default__d   sr    w"448jj!!$'Xzz#&   X*-OPSuTU+VWWXs   A A)
r4   r5   r6   r   r   r   r	   r   rF   __classcell__)rC   s   @r   r=   r=   ^   s?    \ d3i1H T 
	49 	 	r   r=   c                       e Zd ZdZ e       fdee   defdZdede	e
   fdZdedeeee   e
f      fdZdeeee   f   dee   fd	Zy
)r&   a  Represents 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                  |      | _        y r   )r(   r%   r*   )r   r*   r(   s      r   r   zTemplate.__init__x   s    	NN4(	r   r   c                     | j                   j                  |      }| j                   j                  | j                  |      S )a  Match 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*   )r   r*   s     r   matchzTemplate.match|   s3     yy""4(yy--dii>>r   c              #      K   | j                   j                  |      }|j                         D ]  }| j                  |      }|s||f  yw)zISearch for all occurrences of the tree template inside ``tree``.
        N)r(   r%   iter_subtreesrM   )r   r*   subtreer0   s       r   searchzTemplate.search   sO      yy""4())+ 	#G**W%Csl"	#s   AA	Ar>   c                 `    t        | j                  |      j                  | j                        S )z(Apply vars to the template tree
        )r=   r(   	transformr*   )r   r>   s     r   
apply_varszTemplate.apply_vars   s$     DIIt,66tyyAAr   N)r4   r5   r6   r7   r   r	   r   r   r8   r   r:   rM   r   r   rQ   r   rT   r;   r   r   r&   r&   p   s     >J^ )T#Y )l )?* ?+)> ? #: #(5cK9O3P*Q #BwsDI~6 B49 Br   r&   r1   r2   r*   c                     | j                   j                  |      }| j                  |      D ]<  \  }}|j                  |      }|j	                  |j
                  |j                         > |S )z=Search tree and translate each occurrence of t1 into t2.
    )r(   r%   rQ   rT   setr   r   )r1   r2   r*   rP   r>   r0   s         r   	translaterW      s^     77T"D4 ,mmD!CHHcll+, Kr   c                   8    e Zd ZdZdeeef   fdZdee   fdZ	y)TemplateTranslatorz;Utility class for translating a collection of patterns
    translationsc                 V    t        d |j                         D              sJ || _        y )Nc              3   f   K   | ])  \  }}t        |t              xr t        |t               + y wr   )r   r&   ).0kvs      r   	<genexpr>z.TemplateTranslator.__init__.<locals>.<genexpr>   s*     h41a:a*Fz!X/FFhs   /1)allitemsrZ   )r   rZ   s     r   r   zTemplateTranslator.__init__   s(    hS_SeSeSghhhh(r   r*   c                 d    | j                   j                         D ]  \  }}t        |||      } |S r   )rZ   rb   rW   )r   r*   r^   r_   s       r   rW   zTemplateTranslator.translate   s7    %%++- 	)DAqQ4(D	)r   N)
r4   r5   r6   r7   r   r&   r   r	   r   rW   r;   r   r   rY   rY      s-    )WXx-?%@ )d3i r   rY   valuer   c                 Z    | j                  t              r| j                  t              S d S r   )
startswith_TEMPLATE_MARKERlstrip)rd   s    r   r   r      s&    -2-=-=>N-O5<<()YUYYr   N)r7   typingr   r   r   r   r   r   larkr	   r
   lark.exceptionsr   r   r9   r8   r:   rg   r   r=   r&   rW   rY   r   r;   r   r   <module>rl      s    C B " 0	tCy#~	49c>"
39o I IX;sDI~. $(B (BV(  
  Zc Zhsm Zr   