a
    .=ic                  	   @   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)MatchOptionalSequenceMappingc                   @   s   e Zd ZU dZed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 )CodeTemplatez5(^[^\n\S]*[^\n\S]?)?\$([^\d\W]\w*|\{,?[^\d\W]\w*\,?})z\wz[a-zA-Z0-9_]patternfilename)r   returnc                 C   s<   t | d}t| | W  d    S 1 s.0    Y  d S )Nr)openr   read)r   f r   g/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/torchgen/code_template.py	from_file   s    zCodeTemplate.from_file N)r   r   r	   c                 C   s   || _ || _d S N)r   r   )selfr   r   r   r   r   __init__$   s    zCodeTemplate.__init__)envkwargsr	   c                    sb    d u 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 usJ | v r|  S  |  S r   r   )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>4       zACodeTemplate.substitute.<locals>.indent_lines.<locals>.<listcomp>)joinrstrip)r   r   r   r    r   indent_lines2   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 urt|ts|g} ||S t|trd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!   J   r"   z<CodeTemplate.substitute.<locals>.replace.<locals>.<listcomp>)group
isinstancelistr#   lenr   )r&   r   keyZcomma_beforeZcomma_afterr   middle)r%   r   r   r   replace7   s.    




z(CodeTemplate.substitute.<locals>.replace)r   objectr   r   substitutionsubr   )r   r   r   r3   r   )r   r%   r   r   r   
substitute(   s    zCodeTemplate.substitute)r   )N)__name__
__module____qualname__Zsubstitution_strr3   recompile	MULTILINEr5   r   __annotations__staticmethodr   r   r   r   r4   r7   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...others)argsbarabstuffempty)r;   typingr   r   r   r   r   r8   cprintr7   r   r   r   r   <module>   s    F