
    
`i                     T   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dS )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dS )TemplateConfz~Template Configuration

    Allows customization for different uses of Template

    parse() must return a Tree instance.
    Nc                     || _         d S N)_parse)selfparses     g/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/lark/tree_templates.py__init__zTemplateConf.__init__   s        varreturnc                 2   t          |t                    rt          |          S t          |t                    r]|j        dk    rRt          |j                  dk    r:t          |j        d         t                    rt          |j        d                   S dS )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   s     c3 	+%c*** sD!!	7E!!CL!!A%%3<?C00 & &cl1o666tr   templatec                     t          |t                    r| j        s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   sY    h$$ 	-;;{{8,,H(D)) 	KIJJJr   Templatec                 $    t          ||           S )N)conf)r&   r$   s     r   __call__zTemplateConf.__call__=   s    t,,,,r   treec                 X   |                      |          }|r+t          |t                    st          d|          ||iS t          |t                    r
||k    ri S dS t          |t                    rt          |t                    sJ d| d|             |j        |j        k    r~t          |j                  t          |j                  k    rTi }t          |j        |j                  D ]5\  }}| 	                    ||          }| dS |
                    |           6|S dS )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@   sO    }}X.. 	(dD)) c aY] a abbb $''h$$ 	4	4(D))hjt.D.DhhFhRZFhFhbfFhFhhhD=DI%%#h.?*@*@CDVDV*V*VCh/?? $ $B33B;;?44

7####Jtr   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                   d     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                 d    t                                                       || _        || _        d S r   )superr   _conf_vars)r   r(   r>   	__class__s      r   r   z_ReplaceVars.__init___   s+    



r   c                     t                                          |||          }| j                            |          }|r/	 | j        |         S # t
          $ r t          d| d          w xY w|S )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   s    ww""4488j!!$'' 	XXz#& X X X*+VPS+V+V+VW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d
S )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                 H    || _         |                    |          | _        d S r   )r(   r%   r*   )r   r*   r(   s      r   r   zTemplate.__init__x   s     	NN4((			r   r   c                 v    | 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     y""4((y--di>>>r   c              #      K   | j                             |          }|                                D ]}|                     |          }|r||fV   dS )zISearch for all occurrences of the tree template inside ``tree``.
        N)r(   r%   iter_subtreesrM   )r   r*   subtreer0   s       r   searchzTemplate.search   sl       y""4(())++ 	# 	#G**W%%C #sl"""	# 	#r   r>   c                 \    t          | j        |                              | j                  S )z(Apply vars to the template tree
        )r=   r(   	transformr*   )r   r>   s     r   
apply_varszTemplate.apply_vars   s&     DIt,,66tyA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 B B B B Br   r&   r1   r2   r*   c                     | j                             |          }|                     |          D ]:\  }}|                    |          }|                    |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      sg     7T""D4 , ,mmD!!CHcl++++Kr   c                   F    e Zd ZdZdeeef         fdZdee         fdZ	dS )TemplateTranslatorz;Utility class for translating a collection of patterns
    translationsc                 n    t          d |                                D                       sJ || _        d S )Nc              3   p   K   | ]1\  }}t          |t                    ot          |t                    V  2d S r   )r   r&   ).0kvs      r   	<genexpr>z.TemplateTranslator.__init__.<locals>.<genexpr>   s?      hh41a:a**Fz!X/F/Fhhhhhhr   )allitemsrZ   )r   rZ   s     r   r   zTemplateTranslator.__init__   s@    hhS_SeSeSgSghhhhhhhh(r   r*   c                 f    | j                                         D ]\  }}t          |||          }|S r   )rZ   rb   rW   )r   r*   r^   r_   s       r   rW   zTemplateTranslator.translate   s<    %++-- 	) 	)DAqQ4((DDr   N)
r4   r5   r6   r7   r   r&   r   r	   r   rW   r;   r   r   rY   rY      sb         )WXx-?%@ ) ) ) )d3i      r   rY   valuer   c                 n    |                      t                    r|                     t                    nd S r   )
startswith_TEMPLATE_MARKERlstrip)rd   s    r   r   r      s/    -2-=-=>N-O-OY5<<()))UY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 B B B B B B B B B B B B B B " " " " " " " " 0 0 0 0 0 0	tCy#~	49c>"
39o I I I I I I I IX    ;sDI~.   $(B (B (B (B (B (B (B (BV(  
           Zc Zhsm Z Z Z Z Z Zr   