a
    d                     @   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dZG dd dZG dd	 d	e
ee	e f ZG d
d dZeeedddZG dd dZeee d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eee ef ee dddZ	e
ee dd	d
Zee ddddZe
e
eeee
f  dddZdS )TemplateConfzTTemplate Configuration

    Allows customization for different uses of Template
    Nc                 C   s
   || _ d S N)_parse)selfparse r   \/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lark/tree_templates.py__init__   s    zTemplateConf.__init__)varreturnc                 C   sV   t |trt|S t |trR|jdkrRt|jdkrRt |jd trRt|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)templater   c                 C   s0   t |tr| jsJ | |}t |ts,J |S r   )r   r   r   r   r   r   r   r   r   	_get_tree/   s
    


zTemplateConf._get_treeTemplatec                 C   s   t || dS )N)conf)r    r   r   r   r   __call__7   s    zTemplateConf.__call__)r   treer   c                 C   s   |  |}|r||iS t|tr0||kr,i S d S t|trDt|tsXJ d| d| |j|jkrt|jt|jkri }t|j|jD ],\}}| ||}|d u r d S |	| q|S d S )Nz	template=z tree=)
r   r   r   r   r   r   r   zip_match_tree_templateupdate)r   r   r#   Ztemplate_varrest1t2matchesr   r   r   r%   :   s"    

( z!TemplateConf._match_tree_template)N)__name__
__module____qualname____doc__r   r   r   r   r   r   
TreeOrCoder   r"   r   r%   r   r   r   r   r      s   
 r   c                       sH   e Zd Zeeeee f dd fddZee d fddZ  Z	S )_ReplaceVarsN)r!   varsr   c                    s   t    || _|| _d S r   )superr   _conf_vars)r   r!   r1   	__class__r   r   r   U   s    
z_ReplaceVars.__init__)r   c                    sT   t  |||}| j|}|rPz| j| W S  tyN   td| dY n0 |S )Nz"No mapping for template variable ())r2   __default__r3   r   r4   KeyErrorr
   )r   r   r   metar#   r   r5   r   r   r8   Z   s    z_ReplaceVars.__default__)
r+   r,   r-   r   r   r   r   r   r8   __classcell__r   r   r5   r   r0   T   s   "r0   c                   @   s   e Zd ZdZe fee edddZee	e
eef  dddZeeeee e
eef f  ddd	Zeeee f ee d
ddZdS )r    a  Represents a tree templates, 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   n   s    zTemplate.__init__)r#   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   matchr   s    zTemplate.matchc                 c   s6   | j |}| D ]}| |}|r||fV  qdS )zHSearch for all occurances of the tree template inside ``tree``.
        N)r!   r   Ziter_subtreesr<   )r   r#   subtreer'   r   r   r   search   s
    
zTemplate.search)r1   r   c                 C   s   t | j|| jS )z(Apply vars to the template tree
        )r0   r!   Z	transformr#   )r   r1   r   r   r   
apply_vars   s    zTemplate.apply_varsN)r+   r,   r-   r.   r   r   r   r   r/   r   r   r<   r   r   r>   r   r?   r   r   r   r   r    f   s
   (	r    )r(   r)   r#   c                 C   s>   | j |}| |D ]"\}}||}||j|j q|S )z=Search tree and translate each occurrance of t1 into t2.
    )r!   r   r>   r?   setr   r   )r(   r)   r#   r=   r1   r'   r   r   r   	translate   s
    
rA   c                   @   s8   e Zd ZdZeeef dddZee d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>       z.TemplateTranslator.__init__.<locals>.<genexpr>)allitemsrC   )r   rC   r   r   r   r      s    zTemplateTranslator.__init__)r#   c                 C   s$   | j  D ]\}}t|||}q
|S r   )rC   rJ   rA   )r   r#   rE   rF   r   r   r   rA      s    zTemplateTranslator.translateN)
r+   r,   r-   r.   r   r    r   r   r   rA   r   r   r   r   rB      s   rB   )valuer   c                 C   s   |  tr| tS d S r   )
startswith_TEMPLATE_MARKERlstrip)rK   r   r   r   r      s    r   N)r.   typingr   r   r   r   r   r   Zlarkr   r	   Zlark.exceptionsr
   r   r/   rM   r   r0   r    rA   rB   r   r   r   r   r   <module>   s    D+
