o
    iL8                     @   s"  d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddl
mZmZ dd	lmZmZ dd
lmZ G dd dZG dd dZdd ZG dd dZG dd deZG dd deZdd Zdee fddZG dd dZdd ZG d d! d!Zd"d# Zd$d% Z G d&d' d'Z!d(S ))zLProvides functions for the automatic building and shaping of the parse-tree.    )List   )GrammarErrorConfigurationError)Token)Tree)Transformer_InPlace)_vargs_meta_vargs_meta_inline)partialwraps)productc                   @      e Zd Zdd Zdd ZdS )ExpandSingleChildc                 C   s
   || _ d S N)node_builder)selfr    r   T/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/lark/parse_tree_builder.py__init__   s   
zExpandSingleChild.__init__c                 C   s   t |dkr
|d S | |S )Nr   r   )lenr   )r   childrenr   r   r   __call__   s   
zExpandSingleChild.__call__N__name__
__module____qualname__r   r   r   r   r   r   r      s    r   c                   @   s&   e Zd ZdddZdd Zdd ZdS )	PropagatePositionsNc                 C   s   || _ || _d S r   )r   node_filter)r   r   r   r   r   r   r         
zPropagatePositions.__init__c                 C   s&  |  |}t|tr|j}| |}|d urNt|ds6t|d|j|_t|d|j|_t|d|j	|_	d|_
t|d|j|_t|d|j|_t|d|j	|_| t|}|d urt|dsyt|d|j|_t|d|j|_t|d	|j|_d|_
t|d|j|_t|d|j|_t|d	|j|_|S )
Nlinecontainer_linecontainer_columncontainer_start_posFend_linecontainer_end_linecontainer_end_columncontainer_end_pos)r   
isinstancer   meta_pp_get_metahasattrgetattrr    column	start_posemptyr!   r"   r#   reversedr$   
end_columnend_posr%   r&   r'   )r   r   resres_meta
first_meta	last_metar   r   r   r   !   s0   




zPropagatePositions.__call__c                 C   sj   |D ]0}| j d ur|  |sqt|tr|jjs|j  S qt|tr'|  S t|dr2|   S qd S )N__lark_meta__)r   r(   r   r)   r/   r   r+   r7   )r   r   cr   r   r   r*   G   s   



zPropagatePositions._pp_get_metar   )r   r   r   r   r   r*   r   r   r   r   r      s    
&r   c                 C   s8   t | r
tt| dS | du rtS | du rd S td|  )N)r   TFz*Invalid option for propagate_positions: %r)callabler   r   r   )optionr   r   r   make_propagate_positionsS   s   r;   c                   @   r   )ChildFilterc                 C   s   || _ || _|| _d S r   )r   
to_includeappend_none)r   r=   r>   r   r   r   r   r   _      
zChildFilter.__init__c                 C   sj   g }| j D ]\}}}|r|d g| 7 }|r||| j7 }q|||  q| jr0|d g| j 7 }| |S r   r=   r   appendr>   r   r   r   filteredi	to_expandadd_noner   r   r   r   d   s   
zChildFilter.__call__Nr   r   r   r   r   r<   ^   s    r<   c                   @   s   e Zd ZdZdd ZdS )ChildFilterLALR`Optimized childfilter for LALR (assumes no duplication in parse tree, so it's safe to change it)c                 C   sz   g }| j D ]'\}}}|r|d g| 7 }|r%|r||| j7 }q|| j}q|||  q| jr8|d g| j 7 }| |S r   r@   rB   r   r   r   r   x   s   
zChildFilterLALR.__call__N)r   r   r   __doc__r   r   r   r   r   rG   u   s    rG   c                   @       e Zd ZdZdd Zdd ZdS )ChildFilterLALR_NoPlaceholdersrH   c                 C      || _ || _d S r   )r   r=   )r   r=   r   r   r   r   r      r   z'ChildFilterLALR_NoPlaceholders.__init__c                 C   sP   g }| j D ]\}}|r|r||| j7 }q|| j}q|||  q| |S r   )r=   r   rA   r   )r   r   rC   rD   rE   r   r   r   r      s   
z'ChildFilterLALR_NoPlaceholders.__call__Nr   r   r   rI   r   r   r   r   r   r   rK      s    rK   c                 C   s   | j  o	| jdS )N_)is_termname
startswith)symr   r   r   _should_expand      rS   _empty_indicesc           
      C   s8  |r4| dt| ksJ ddd |D }dd |dD }t|t| d ks3J |t| fn	d	gt| d  }g }d	}t| D ]\}}	||| 7 }|sW|	jrW|	jsc||t|	|f d	}qE||t|  7 }|st|t| k st	d
d |D r|s|rt
|rt||S t||S t
tdd |D S d S )NF c                 s   s    | ]	}t t|V  qd S r   )strint).0br   r   r   	<genexpr>   s    z,maybe_create_child_filter.<locals>.<genexpr>c                 S   s   g | ]}t |qS r   )r   )rY   onesr   r   r   
<listcomp>   s    z-maybe_create_child_filter.<locals>.<listcomp>0r   r   c                 s   s    | ]\}}}|V  qd S r   r   )rY   rD   rE   rN   r   r   r   r[      s    c                 S   s   g | ]	\}}}||fqS r   r   )rY   rD   xrN   r   r   r   r]          )countr   joinsplit	enumeraterO   
filter_outrA   rS   anyr   r<   rG   rK   )
	expansionkeep_all_tokens	ambiguousrU   sempty_indicesr=   nones_to_addrD   rR   r   r   r   maybe_create_child_filter   s(   &&rm   c                   @   rJ   )AmbiguousExpandera  Deal with the case where we're expanding children ('_rule') into a parent but the children
       are ambiguous. i.e. (parent->_ambig->_expand_this_rule). In this case, make the parent itself
       ambiguous with as many copies as there are ambiguous children, and then copy the ambiguous children
       into the right parents in the right places, essentially shifting the ambiguity up the tree.c                 C   s   || _ || _|| _d S r   )r   
tree_classrE   )r   rE   ro   r   r   r   r   r      r?   zAmbiguousExpander.__init__c                    s   dd }g  t |D ]\}}||r!|jv r | |d q
 s)|S  fddt |D }dfddt| D S )Nc                 S      t | do	| jdkS )Ndata_ambigr+   rq   )tr   r   r   _is_ambig_tree   rT   z2AmbiguousExpander.__call__.<locals>._is_ambig_treerr   c                    s$   g | ]\}}| v r|j n|fqS r   r   )rY   rD   child)ri   r   r   r]      s   $ z.AmbiguousExpander.__call__.<locals>.<listcomp>c                    s   g | ]	}  t|qS r   )r   list)rY   fr   r   r   r]      r`   )rd   rE   rA   expand_kids_by_datar   ro   r   )r   r   ru   rD   rw   expandr   )ri   r   r   r      s   



zAmbiguousExpander.__call__NrM   r   r   r   r   rn      s    rn   c                    s*    fddt |D }|rtt|| S d S )Nc                    s,   g | ]\}} s|j r|jst|r|qS r   )rO   re   rS   )rY   rD   rR   rh   r   r   r]      s    
z3maybe_create_ambiguous_expander.<locals>.<listcomp>)rd   r   rn   )ro   rg   rh   rE   r   r}   r   maybe_create_ambiguous_expander   s   r~   c                   @   rJ   )AmbiguousIntermediateExpanderaq  
    Propagate ambiguous intermediate nodes and their derivations up to the
    current rule.

    In general, converts

    rule
      _iambig
        _inter
          someChildren1
          ...
        _inter
          someChildren2
          ...
      someChildren3
      ...

    to

    _ambig
      rule
        someChildren1
        ...
        someChildren3
        ...
      rule
        someChildren2
        ...
        someChildren3
        ...
      rule
        childrenFromNestedIambigs
        ...
        someChildren3
        ...
      ...

    propagating up any nested '_iambig' nodes along the way.
    c                 C   rL   r   )r   ro   )r   ro   r   r   r   r   r     r   z&AmbiguousIntermediateExpander.__init__c                    sL   dd  fdd  |}|r!fdd|D } d|S |S )Nc                 S   rp   )Nrq   _iambigrs   )rw   r   r   r   _is_iambig_tree  rT   z?AmbiguousIntermediateExpander.__call__.<locals>._is_iambig_treec                    s   | rD| d rF| d }g }|j D ]0} |j }|r/|D ]}| j | dd 7  _ q||7 }qd|j | dd  }|| q|S dS dS )a  
            Recursively flatten the derivations of the parent of an '_iambig'
            node. Returns a list of '_inter' nodes guaranteed not
            to contain any nested '_iambig' nodes, or None if children does
            not contain an '_iambig' node.
            r   r   N_inter)r   ro   rA   )r   iambig_noderesult
grandchild	collapsedrw   new_tree_collapse_iambigr   r   r   r   r     s   



z@AmbiguousIntermediateExpander.__call__.<locals>._collapse_iambigc                    s   g | ]}  |jqS r   )r   r   )rY   r8   rz   r   r   r]   1  s    z:AmbiguousIntermediateExpander.__call__.<locals>.<listcomp>rr   )ro   r   )r   r   r   processed_nodesr   r   r   r     s   
z&AmbiguousIntermediateExpander.__call__NrM   r   r   r   r   r      s    (r   c                    s   t   fdd}|S )Nc                    s   t  j| } |S r   )r   r   )r   treefuncr   r   ry   9  s   zinplace_transformer.<locals>.f)r   )r   ry   r   r   r   inplace_transformer8  s   r   c                    s4   t u stu rtdt  fdd}|S )Nz0Meta args not supported for internal transformerc                    s    | d S r   r   rv   r   rP   wrapperr   r   ry   E  s   zapply_visit_wrapper.<locals>.f)r	   r
   NotImplementedErrorr   )r   rP   r   ry   r   r   r   apply_visit_wrapperA  s
   r   c                   @   s(   e Zd Zd	ddZdd Zd
ddZdS )ParseTreeBuilderFc                 C   s,   || _ || _|| _|| _t| || _d S r   )ro   propagate_positionsri   maybe_placeholdersrx   _init_buildersrule_builders)r   rulesro   r   ri   r   r   r   r   r   L  s
   zParseTreeBuilder.__init__c                 c   s    t | j}|D ]?}|j}|j}|j}ttd |o|j ott	|j
|| j| jr)|jnd || jo6t| j|j
|| jo>tt| jg}||fV  qd S r   )r;   r   optionsrh   expand1rx   filteraliasr   rm   rg   ri   r   rk   r~   ro   r   r   )r   r   r   ruler   rh   expand_single_childwrapper_chainr   r   r   r   T  s   
zParseTreeBuilder._init_buildersNc           
   	      s   i }t |dd   r fdd}n| j}| jD ]W\}}|jp%|jjp%|jj}z!t ||}t |dd }|d ur=t|||}n	t	|t
rFt|}W n tyU   t||}Y nw |D ]}	|	|}qX||v rjtd|f |||< q|S )N__default__c                    s    | |d S r   r   )rq   r   default_handlerr   r   default_callbackk  s   z:ParseTreeBuilder.create_callback.<locals>.default_callbackvisit_wrapperzRule '%s' already exists)r,   ro   r   r   r   template_sourceoriginrP   r   r(   r   r   AttributeErrorr   r   )
r   transformer	callbacksr   r   r   user_callback_namery   r   wr   r   r   create_callbackf  s0   



z ParseTreeBuilder.create_callback)FFFr   )r   r   r   r   r   r   r   r   r   r   r   K  s    
r   N)"rI   typingr   
exceptionsr   r   lexerr   r   r   visitorsr   r	   r
   	functoolsr   r   	itertoolsr   r   r   r;   r<   rG   rK   rS   boolrm   rn   r~   r   r   r   r   r   r   r   r   <module>   s.    7!R	
