o
    0 i                     @   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   _/home/app/PaddleOCR-VL-test/.venv_paddleocr/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$   Z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"   r4   	__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 ())r5   __default__r6   r   r7   KeyErrorr
   )r   r   r   metar$   r   r8   r   r   r;   d   s   z_ReplaceVars.__default__)
r,   r-   r.   r   r   r   r   r   r;   __classcell__r   r   r8   r   r3   ^   s    &r3   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    Ziter_subtreesr?   )r   r$   subtreer(   r   r   r   search   s   

zTemplate.searchr4   c                 C   s   t | j|| jS )z(Apply vars to the template tree
        )r3   r"   Z	transformr$   )r   r4   r   r   r   
apply_vars   s   zTemplate.apply_varsN)r,   r-   r.   r/   r   r   r   r   r0   r   r2   r?   r   r   rA   r   rB   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    rA   rB   setr   r   )r)   r*   r$   r@   r4   r(   r   r   r   	translate   s
   
rD   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itemsrF   )r   rF   r   r   r   r      s   
zTemplateTranslator.__init__r$   c                 C   s$   | j  D ]
\}}t|||}q|S r   )rF   rL   rD   )r   r$   rH   rI   r   r   r   rD      s   zTemplateTranslator.translateN)
r,   r-   r.   r/   r   r!   r   r   r   rD   r   r   r   r   rE      s    rE   valuer   c                 C   s   |  tr
| tS d S r   )
startswith_TEMPLATE_MARKERlstrip)rM   r   r   r   r      s   r   N)r/   typingr   r   r   r   r   r   Zlarkr   r	   Zlark.exceptionsr
   r   r1   r0   r2   rO   r   r3   r!   rD   rE   r   r   r   r   r   <module>   s     L+
