o
    ٷi                     @   sf   U d dl Z d dlZd dlmZmZmZmZmZ d dlm	Z	 dZ
eed< G dd dZG dd	 d	ZdS )
    N)ListOptionalSetTupleUnion)EinopsErroru   …	_ellipsisc                   @   s&   e Zd ZdZdefddZdd ZdS )AnonymousAxiszHImportant thing: all instances of this class are not equal to each othervaluec                 C   s:   t || _| jdkr| jdkrtdtd| j d S )N   zENo need to create anonymous axis of length 1. Report this as an issuez0Anonymous axis should have positive length, not )intr
   r   )selfr
    r   B/home/ubuntu/.local/lib/python3.10/site-packages/einops/parsing.py__init__   s   


zAnonymousAxis.__init__c                 C   s   t | j dS )Nz-axis)strr
   )r   r   r   r   __repr__   s   zAnonymousAxis.__repr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r	   
   s    r	   c                
   @   s   e Zd ZdZddddededefddZd	efd
dZd	efddZ	e
ddeded	eeef fddZe
ded	efddZdS )ParsedExpressionz
    non-mutable structure that contains information about one side of expression (e.g. 'b c (h w)')
    and keeps some information important for downstream
    F)allow_underscoreallow_duplicates
expressionr   r   c                   sf  d_ d _t _d_g _d|v r9d|vrtdt|ddks,t|ddkr0td|	dt
}d_ d  fd	d
}d }|D ]R}|dv r||d urV|| d }|dkrgd urdtdg qH|dkr{d u rstdj d qHt|s|dt
fv r|d u r|}qH||7 }qHtd| dd urtd| d|d ur|| d S d S )NF.z...z6Expression may contain dots only inside ellipsis (...)r      zUExpression may contain dots only inside ellipsis (...); only one ellipsis for tensor Tc                    s   | j v rr| dks std|  d| tkr8j t d u r.jt d_d S t d_d S t| }|rTt	| dkrTd u rQjg  d S 	 d S j
| d\}}|sk|sktd|  d	| |rqt| } j |  |r|d_d u rj| g d S |  d S )
N_z2Indexing expression contains duplicate dimension ""FTr   )r   zInvalid axis identifier: 
)identifiersr   r   addcompositionappendhas_ellipsis_parenthesizedr   	isdecimalr   check_axis_name_return_reasonr	   has_non_unitary_anonymous_axes)x	is_numberis_axis_namereasonr   r   bracket_groupr   r   r   add_axis_name3   s8   




z0ParsedExpression.__init__.<locals>.add_axis_namez() (zDAxis composition is one-level (brackets inside brackets not allowed))zBrackets are not balancedr   zUnknown character ''z'Imbalanced parentheses in expression: "r   )has_ellipsisr$   setr    r'   r"   r   r   countreplacer   r#   isalnum)r   r   r   r   r.   current_identifiercharr   r,   r   r      sT    "
zParsedExpression.__init__returnc                 C   s:   g }| j D ]}t|tsJ d|D ]}|| qq|S )Nzdoes not work with ellipsis)r"   
isinstancelistr#   )r   resultcomposed_axisaxisr   r   r   flat_axes_orderq   s   
z ParsedExpression.flat_axes_orderc                 C   s,   | j D ]}t|trt|dkr dS qdS )Nr   TF)r"   r:   r;   len)r   axesr   r   r   has_composed_axesy   s
   
z"ParsedExpression.has_composed_axesnamec                 C   sv   t | sdS | d dks| d dkr| dkr|rdS dS t| r-tjd|  tdd	 | d
v r9tjdtdd	 dS )N)Fznot a valid python identifierr   r   )T )Fz8axis name should should not start or end with underscorez7It is discouraged to use axes names that are keywords:    )
stacklevel)r>   zQIt is discouraged to use 'axis' as an axis name and will raise an error in future)r   isidentifierkeyword	iskeywordwarningswarnRuntimeWarningFutureWarning)rC   r   r   r   r   r&      s&   

z.ParsedExpression.check_axis_name_return_reasonc                 C   s   t | \}}|S )z
        Valid axes names are python identifiers except keywords,
        and additionally should not start or end with underscore
        )r   r&   )rC   is_valid_reasonr   r   r   check_axis_name   s   z ParsedExpression.check_axis_nameN)F)r   r   r   r   r   boolr   r   r?   rB   staticmethodr   r&   rQ   r   r   r   r   r      s    R"r   )rI   rK   typingr   r   r   r   r   einopsr   r   r   __annotations__r	   r   r   r   r   r   <module>   s   
 