3
՛gY                 @   st   d Z ddlmZmZmZ ddlZG dd deZG dd deZG dd	 d	eZ	G d
d deZ
G dd de
ZdS )zjutilities for analyzing expressions and blocks of Python
code, as well as generating Python from AST nodes    )
exceptionspyparsercompatNc               @   s   e Zd ZdZdd ZdS )
PythonCodez<represents information about a string containing Python codec             K   sX   || _ t | _t | _t|tjr8tj|j	 df|}n|}tj
| f|}|j| d S )Nexec)codesetdeclared_identifiersundeclared_identifiers
isinstancer   string_typesr   parselstripZFindIdentifiersvisit)selfr   exception_kwargsexprf r   */usr/lib/python3/dist-packages/mako/ast.py__init__   s    zPythonCode.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r      s   r   c               @   s   e Zd ZdZdd ZdS )ArgumentListzBparses a fragment of code as a comma-separated list of expressionsc             K   s~   g | _ g | _t | _t | _t|tjr\tj	d|rJtj	d| rJ|d7 }t
j|df|}n|}t
j| tf|}|j| d S )Nz\Sz,\s*$,r   )Zcodeargsargsr   r	   r
   r   r   r   rematchr   r   Z	FindTupler   r   )r   r   r   r   r   r   r   r   r   4   s    zArgumentList.__init__N)r   r   r   r   r   r   r   r   r   r   0   s   r   c                   s    e Zd ZdZ fddZ  ZS )PythonFragmentzextends PythonCode to provide identifier lookups in partial control
    statements

    e.g.
        for x in 5:
        elif y==9:
        except (MyException, e):
    etc.
    c                s   t jd|j t j}|s*tjd| f||jdrF|d |jd }|jdd\}}|dkrh|d	 }nj|d
krz|d }nX|dks|dkrd| d	 }n:|dkrd| d	 }n$|dkr|d	 }ntjd| f|tt	| j
|f| d S )Nz^(\w+)(?:\s+(.*?))?:\s*(#|$)z0Fragment '%s' is not a partial control statement         forifwhilepasstryzpass
except:passelifelsezif False:pass
exceptz	try:pass
withz!Unsupported control keyword: '%s')r$   r%   r&   )r   r   stripSr   CompileExceptiongroupstartsuperr    r   )r   r   r   mkeywordr   )	__class__r   r   r   R   s,    



zPythonFragment.__init__)r   r   r   r   r   __classcell__r   r   )r5   r   r    F   s   
r    c               @   s0   e Zd ZdZdddZdddZedd	 Zd
S )FunctionDeclzfunction declarationTc             K   sr   || _ tj|df|}tj| f|}|j| t| dsJtjd| f|| rn| jrntjd| j	d  f|d S )Nr   funcnamez'Code '%s' is not a function declarationz('**%s' keyword argument not allowed herer"   )
r   r   r   Z	ParseFuncr   hasattrr   r/   kwargs
kwargnames)r   r   Zallow_kwargsr   r   r   r   r   r   r   p   s    

zFunctionDecl.__init__Fc       	      C   sB  g }| j ddd }| jddd }| jddd	 }| jddd
 }| jr^|jd|jd  xn|D ]f}|r|jd||f  qd|r|jd}|dkr|j| q|jd|tj|j	 f  qd|j| qdW | j
r|jd|jd  xL|D ]D}|s | r|j| q|jd}|jd|tj|j	 f  qW |j  |S )aE  Return the argument declarations of this FunctionDecl as a printable
        list.

        By default the return value is appropriate for writing in a ``def``;
        set `as_call` to true to build arguments to be passed to the function
        instead (assuming locals with the same names as the arguments exist).
        Nr"   z**r   z%s=%s*r9   r9   r9   r9   )argnamesr<   defaults
kwdefaultsr;   appendpopr   ZExpressionGeneratorvalueZvarargsreverse)	r   Zas_callZ	namedeclsr>   r<   r?   r@   namedefaultr   r   r   get_argument_expressions   s6    	



z%FunctionDecl.get_argument_expressionsc             C   s   t | jt | j S )N)tupler>   r<   )r   r   r   r   allargnames   s    zFunctionDecl.allargnamesN)T)F)r   r   r   r   r   rG   propertyrI   r   r   r   r   r7   l   s   

5r7   c                   s    e Zd ZdZ fddZ  ZS )FunctionArgsz.the argument portion of a function declarationc                s   t t| jd| f| d S )Nzdef ANON(%s):pass)r2   rK   r   )r   r   r;   )r5   r   r   r      s    zFunctionArgs.__init__)r   r   r   r   r   r6   r   r   )r5   r   rK      s   rK   )r   Zmakor   r   r   r   objectr   r   r    r7   rK   r   r   r   r   <module>   s   "&M