o
    bi                     @   s   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 dZG dd	 d	e
ee	e f ZG d
d dZdededefddZG dd dZdedee fd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                   @   s   e Zd ZdZdddZdeee ef dee fddZ	d	e
dee fd
dZd	ee ddfddZd	e
dedee fddZdS )TemplateConfz~Template Configuration

    Allows customization for different uses of Template

    parse() must return a Tree instance.
    Nc                 C   s
   || _ d S N)_parse)selfparse r   G/home/ubuntu/.local/lib/python3.10/site-packages/lark/tree_templates.py__init__   s   
zTemplateConf.__init__varreturnc                 C   sV   t |tr	t|S t |tr)|jdkr)t|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   r   r   r   test_var   s   
	
zTemplateConf.test_vartemplatec                 C   s4   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   r   r   r   	_get_tree3   s   



zTemplateConf._get_treeTemplatec                 C   s   t || dS )N)conf)r!   r   r   r   r   __call__=   s   zTemplateConf.__call__treec                 C   s   |  |}|rt|tstd|||iS t|tr$||kr"i S dS t|tr.t|ts8J d| d| |j|jkrjt|jt|jkrji }t|j|jD ]\}}| 	||}|du rb dS |
| qQ|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matchesr   r   r   r&   @   s&   


( z!TemplateConf._match_tree_templater   )__name__
__module____qualname____doc__r   r   r   r   r   r   
TreeOrCoder    r#   BranchMatchResultr&   r   r   r   r   r      s    
"
r   c                       sL   e Zd Zdedeeee f ddf fddZdee f fddZ  Z	S )	_ReplaceVarsr"   varsr   Nc                    s   t    || _|| _d S r   )superr   _conf_vars)r   r"   r5   	__class__r   r   r   _   s   

z_ReplaceVars.__init__c                    sP   t  |||}| j|}|r&z| j| W S  ty%   td| dw |S )Nz"No mapping for template variable ())r6   __default__r7   r   r8   KeyErrorr
   )r   r   r   metar$   r   r9   r   r   r<   d   s   z_ReplaceVars.__default__)
r-   r.   r/   r   r   r   r   r   r<   __classcell__r   r   r9   r   r4   ^   s    &r4   c                   @   s   e Zd ZdZe fdee defddZdede	e
 fddZdedeeee e
f  fd	d
Zdeeee f dee fd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                 C   s   || _ ||| _d S r   )r"   r    r$   )r   r$   r"   r   r   r   r   x   s   zTemplate.__init__r   c                 C   s   | 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$   r   r   r   match|   s   zTemplate.matchc                 c   s8    | j |}| D ]}| |}|r||fV  qdS )zISearch for all occurrences of the tree template inside ``tree``.
        N)r"   r    iter_subtreesr@   )r   r$   subtreer)   r   r   r   search   s   

zTemplate.searchr5   c                 C   s   t | j|| jS )z(Apply vars to the template tree
        )r4   r"   	transformr$   )r   r5   r   r   r   
apply_vars   s   zTemplate.apply_varsN)r-   r.   r/   r0   r   r   r   r   r1   r   r3   r@   r   r   rC   r   rE   r   r   r   r   r!   p   s    "&	r!   r*   r+   r$   c                 C   s>   | j |}| |D ]\}}||}||j|j q|S )z=Search tree and translate each occurrence of t1 into t2.
    )r"   r    rC   rE   setr   r   )r*   r+   r$   rB   r5   r)   r   r   r   	translate   s
   
rG   c                   @   s8   e Zd ZdZdeeef fddZdee fddZ	dS )	TemplateTranslatorz;Utility class for translating a collection of patterns
    translationsc                 C   s$   t dd | D sJ || _d S )Nc                 s   s(    | ]\}}t |tot |tV  qd S r   )r   r!   ).0kvr   r   r   	<genexpr>   s   & z.TemplateTranslator.__init__.<locals>.<genexpr>)allitemsrI   )r   rI   r   r   r   r      s   
zTemplateTranslator.__init__r$   c                 C   s$   | j  D ]
\}}t|||}q|S r   )rI   rO   rG   )r   r$   rK   rL   r   r   r   rG      s   zTemplateTranslator.translateN)
r-   r.   r/   r0   r   r!   r   r   r   rG   r   r   r   r   rH      s    rH   valuer   c                 C   s   |  tr
| tS d S r   )
startswith_TEMPLATE_MARKERlstrip)rP   r   r   r   r      s   r   N)r0   typingr   r   r   r   r   r   larkr   r	   lark.exceptionsr
   r   r2   r1   r3   rR   r   r4   r!   rG   rH   r   r   r   r   r   <module>   s     L+
