U
    •Ê¹jW  ã                	   @   sh   d dl Z d dlmZmZmZmZ G dd„ dƒZedkrdedƒZe	ej
ddgd	d
gddddgg d�ƒ dS )é    N)ÚMappingÚMatchÚOptionalÚSequencec                   @   sx   e Zd ZU dZe eej¡Zee	d< ee	d< e
ed dœdd„ƒZdeedd	œd
d„Zdeeeef  eedœdd„ZdS )ÚCodeTemplatez-(^[^\n\S]*)?\$([^\d\W]\w*|\{,?[^\d\W]\w*\,?})ÚpatternÚfilename)r   Úreturnc              
   C   s.   t | ƒ�}t| ¡ | ƒW  5 Q R £ S Q R X d S ©N)Úopenr   Úread)r   Úf© r   úU/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchgen/code_template.pyÚ	from_file   s    
zCodeTemplate.from_fileÚ N)r   r   r	   c                 C   s   || _ || _d S r
   )r   r   )Úselfr   r   r   r   r   Ú__init__   s    zCodeTemplate.__init__)ÚenvÚkwargsr	   c                    sb   ˆ d kri ‰ t tdœ‡ ‡fdd„‰t tt t dœdd„‰tt  t dœ‡‡fdd	„}| j || j¡S )
N)Úvr	   c                    s$   ˆ d k	st ‚| ˆkrˆ|  S ˆ |  S r
   )ÚAssertionError)r   )r   r   r   r   Úlookup$   s    z'CodeTemplate.substitute.<locals>.lookup)Úindentr   r	   c                    s   d  ‡ fdd„|D ƒ¡ ¡ S )Nr   c                    s*   g | ]"}t |ƒ ¡ D ]}ˆ | d  ‘qqS )Ú
)ÚstrÚ
splitlines)Ú.0ÚeÚl©r   r   r   Ú
<listcomp>*   s       zACodeTemplate.substitute.<locals>.indent_lines.<locals>.<listcomp>)ÚjoinÚrstrip)r   r   r   r    r   Úindent_lines(   s    ÿz-CodeTemplate.substitute.<locals>.indent_lines)Úmatchr	   c                    sÜ   |   d¡}|   d¡}d}d}|d dkrl|dd… }|d dkrPd}|dd … }|d dkrld}|d d… }ˆ|ƒ}|d k	r–t|tƒsŒ|g}ˆ ||ƒS t|tƒrÐd d	d
„ |D ƒ¡}t|ƒdkrÄ|S || | S t|ƒS d S )Né   é   r   r   Ú{éÿÿÿÿú,z, c                 S   s   g | ]}t |ƒ‘qS r   )r   )r   Úxr   r   r   r!   @   s     z<CodeTemplate.substitute.<locals>.replace.<locals>.<listcomp>)ÚgroupÚ
isinstanceÚlistr"   Úlenr   )r%   r   ÚkeyZcomma_beforeZcomma_afterr   Úmiddle)r$   r   r   r   Úreplace-   s.    




z(CodeTemplate.substitute.<locals>.replace)r   Úobjectr   r   ÚsubstitutionÚsubr   )r   r   r   r2   r   )r   r$   r   r   r   Ú
substitute   s    zCodeTemplate.substitute)r   )N)Ú__name__Ú
__module__Ú__qualname__Zsubstitution_strÚreÚcompileÚ	MULTILINEr4   r   Ú__annotations__Ústaticmethodr   r   r   r   r3   r6   r   r   r   r   r      s   
 ÿ þr   Ú__main__zŽ    int foo($args) {

        $bar
            $bar
        $a+$b
    }
    int commatest(int a${,stuff})
    int notest(int a${,empty,})
    Úhié   Úwhaté   é   é   z	things...Zothers)ÚargsÚbarÚaÚbÚstuffÚempty)r:   Útypingr   r   r   r   r   r7   ÚcÚprintr6   r   r   r   r   Ú<module>   s    <ÿúÿ