o
    bi                     @   s   d Z ddlmZmZmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZmZmZ ddlmZmZmZ dd	lmZmZ dd
lmZ dd ZG dd deZG dd deZdS )zbThis is an experimental tool for reconstructing text from a shaped tree, based on a Lark grammar.
    )DictCallableIterableOptional   )Lark)Tree	ParseTree)Transformer_InPlace)Token
PatternStrTerminalDef)TerminalNonTerminalSymbol)TreeMatcheris_discarded_terminal)is_id_continuec                 C   s$   zt | }W dS  ty   Y dS w )NFT)nextStopIteration)i_ r   D/home/ubuntu/.local/lib/python3.10/site-packages/lark/reconstruct.pyis_iter_empty   s   r   c                   @   st   e Zd ZU dZeeef ed< eeee	gef f ed< deeef deeee	gef f ddfddZ
dd	 ZdS )
WriteTokensTransformerzTInserts discarded tokens into their correct place, according to the rules of grammartokens	term_subsreturnNc                 C   s   || _ || _d S N)r   r   )selfr   r   r   r   r   __init__   s   
zWriteTokensTransformer.__init__c           
   	   C   s  t |ddst||S t|}g }|jD ]g}t|rJz
| j|j |}W n tyC   | j|j }t	|j
ts=td| |j
j}Y nw || qt|}	t	|	trX||	7 }qt	|	trit|	j|kshJ |	nt|	j|ksvJ ||	f||	 qt|sJ |S )N
match_treeFz,Reconstructing regexps not supported yet: %s)getattrr   iterorig_expansionr   r   nameKeyErrorr   
isinstancepatternr   NotImplementedErrorvalueappendr   listr   r   typer   datar   )
r    r/   childrenmeta	iter_argsto_writesymvtxr   r   r   __default__!   s0   




z"WriteTokensTransformer.__default__)__name__
__module____qualname____doc__r   strr   __annotations__r   r   r!   r8   r   r   r   r   r      s   
 0r   c                
   @   s   e Zd ZU dZeed< ddedeee	e
ege	f f  ddfddZd	d
 Zddedee
ee	 gee	 f  dede	fddZdS )Reconstructora  
    A Reconstructor that will, given a full parse Tree, generate source code.

    Note:
        The reconstructor cannot generate values from regexps. If you need to produce discarded
        regexes, such as newlines, use `term_subs` and provide default values for them.

    Parameters:
        parser: a Lark instance
        term_subs: a dictionary of [Terminal name as str] to [output text as str]
    write_tokensNparserr   r   c                 C   s,   t | | tdd | jD |pi | _d S )Nc                 S   s   i | ]}|j |qS r   )r&   ).0r6   r   r   r   
<dictcomp>S   s    z*Reconstructor.__init__.<locals>.<dictcomp>)r   r!   r   r   r@   )r    rA   r   r   r   r   r!   P   s    zReconstructor.__init__c                 c   sL    |  ||j}| j|}|D ]}t|tr | |E d H  q|V  qd S r   )r"   r/   r@   	transformr(   r   _reconstruct)r    treeunreduced_treeresitemr   r   r   rE   U   s   
zReconstructor._reconstructTrF   postprocinsert_spacesc                 C   sn   |  |}|r||}g }d}|D ] }|r*|r*|r*t|d r*t|d r*|d || |}qd|S )N r    )rE   r   r,   join)r    rF   rJ   rK   r7   y	prev_itemrI   r   r   r   reconstruct`   s   
$


zReconstructor.reconstructr   )NT)r9   r:   r;   r<   r   r>   r   r   r   r=   r   r   r!   rE   r	   r   boolrR   r   r   r   r   r?   A   s   
 .6r?   N)r<   typingr   r   r   r   larkr   rF   r   r	   visitorsr
   lexerr   r   r   grammarr   r   r   tree_matcherr   r   utilsr   r   r   r?   r   r   r   r   <module>   s    *