a
    þd·v  ã                   @   s‚   d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
mZmZ dZG dd„ deƒZG dd	„ d	eƒZG d
d„ deƒZ[dS )é    N)ÚIntervalSet)ÚToken)ÚATNState)ÚRecognitionExceptionÚNoViableAltExceptionÚInputMismatchExceptionÚFailedPredicateExceptionÚParseCancellationExceptionc                   @   sd   e Zd Zedœdd„Zedœdd„Zeedœdd„Zedœd	d
„Zedœdd„Z	eedœdd„Z
dS )ÚErrorStrategy©Ú
recognizerc                 C   s   d S ©N© ©Úselfr   r   r   úc/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/antlr4/error/ErrorStrategy.pyÚreset   s    zErrorStrategy.resetc                 C   s   d S r   r   r   r   r   r   ÚrecoverInline   s    zErrorStrategy.recoverInline©r   Úec                 C   s   d S r   r   ©r   r   r   r   r   r   Úrecover   s    zErrorStrategy.recoverc                 C   s   d S r   r   r   r   r   r   Úsync   s    zErrorStrategy.syncc                 C   s   d S r   r   r   r   r   r   ÚinErrorRecoveryMode   s    z!ErrorStrategy.inErrorRecoveryModec                 C   s   d S r   r   r   r   r   r   ÚreportError"   s    zErrorStrategy.reportErrorN)Ú__name__Ú
__module__Ú__qualname__ÚParserr   r   r   r   r   r   r   r   r   r   r   r
      s   r
   c                       sT  e Zd Z‡ fdd„Zedœdd„Zedœdd„Zedœdd	„Zedœd
d„Zedœdd„Z	ee
dœdd„Zee
dœdd„Zedœdd„Zeedœdd„Zeedœdd„Zdd„ Zedœdd„Zedœdd„Zedœdd „Zedœd!d"„Zedœd#d$„Zedœd%d&„Zedœd'd(„Zed)œd*d+„Zed,œd-d.„Zedœd/d0„Zeed1œd2d3„Z ‡  Z!S )4ÚDefaultErrorStrategyc                    s,   t ƒ  ¡  d| _d| _d | _d | _d| _d S )NFéÿÿÿÿr   )ÚsuperÚ__init__ÚerrorRecoveryModeÚlastErrorIndexÚlastErrorStatesÚnextTokensContextÚnextTokenState)r   ©Ú	__class__r   r   r"   +   s    
zDefaultErrorStrategy.__init__r   c                 C   s   |   |¡ d S r   ©ÚendErrorConditionr   r   r   r   r   B   s    zDefaultErrorStrategy.resetc                 C   s
   d| _ d S )NT©r#   r   r   r   r   ÚbeginErrorConditionK   s    z(DefaultErrorStrategy.beginErrorConditionc                 C   s   | j S r   r,   r   r   r   r   r   N   s    z(DefaultErrorStrategy.inErrorRecoveryModec                 C   s   d| _ d | _d| _d S )NFr    )r#   r%   r$   r   r   r   r   r+   W   s    z&DefaultErrorStrategy.endErrorConditionc                 C   s   |   |¡ d S r   r*   r   r   r   r   ÚreportMatcha   s    z DefaultErrorStrategy.reportMatchr   c                 C   sˆ   |   |¡rd S |  |¡ t|tƒr0|  ||¡ nTt|tƒrH|  ||¡ n<t|tƒr`|  ||¡ n$t	dt
|ƒj ƒ | |j|j|¡ d S )Nz unknown recognition error type: )r   r-   Ú
isinstancer   ÚreportNoViableAlternativer   ÚreportInputMismatchr   ÚreportFailedPredicateÚprintÚtyper   ÚnotifyErrorListenersÚmessageÚoffendingTokenr   r   r   r   r   w   s    




z DefaultErrorStrategy.reportErrorc                 C   sp   | j | ¡ jkr.| jd ur.|j| jv r.| ¡  |jj| _ | jd u rHg | _| j |j¡ |  |¡}|  	||¡ d S r   )
r$   ZgetInputStreamÚindexr%   ÚstateÚconsumeÚ_inputÚappendÚgetErrorRecoverySetÚconsumeUntil)r   r   r   Z	followSetr   r   r   r   Ž   s    ÿ
þ


zDefaultErrorStrategy.recoverc                 C   sö   |   |¡rd S |jjj|j }| ¡  d¡}|j |¡}||v rRd | _t	j
| _d S tj|v rz| jd u rv|j| _|j| _d S |jt	jt	jt	jt	jfv r°|  |¡d ur¦d S t|ƒ‚nB|jt	jt	jfv rò|  |¡ | ¡ }| |  |¡¡}|  ||¡ n d S ©Né   )r   Ú_interpÚatnÚstatesr9   ÚgetTokenStreamÚLAÚ
nextTokensr&   r   ZINVALID_STATE_NUMBERr'   r   ÚEPSILONÚ_ctxZ_stateNumberZnextTokensStateZ	stateTypeZBLOCK_STARTZSTAR_BLOCK_STARTZPLUS_BLOCK_STARTZSTAR_LOOP_ENTRYÚsingleTokenDeletionr   ZPLUS_LOOP_BACKZSTAR_LOOP_BACKÚreportUnwantedTokenÚgetExpectedTokensÚaddSetr=   r>   )r   r   ÚsZlarF   Ú	expectingZwhatFollowsLoopIterationOrRuler   r   r   r   Ì   s4    


ÿ

zDefaultErrorStrategy.syncc                 C   s\   |  ¡ }|d ur6|jjtjkr$d}q:| |j|j¡}nd}d|  |¡ }| ||j|¡ d S )Nú<EOF>z<unknown input>zno viable alternative at input )	rD   Z
startTokenr4   r   ÚEOFZgetTextr7   ÚescapeWSAndQuoter5   )r   r   r   ÚtokensÚinputÚmsgr   r   r   r0   û   s    z.DefaultErrorStrategy.reportNoViableAlternativec                 C   s<   d|   |j¡ d | ¡  |j|j¡ }| ||j|¡ d S )Nzmismatched input ú expecting )ÚgetTokenErrorDisplayr7   rK   ÚtoStringÚliteralNamesÚsymbolicNamesr5   )r   r   r   rT   r   r   r   r1     s    ÿÿz(DefaultErrorStrategy.reportInputMismatchc                 C   s6   |j |j ¡  }d| d |j }| ||j|¡ d S )Nzrule ú )Z	ruleNamesrH   ZgetRuleIndexr6   r5   r7   )r   r   r   ZruleNamerT   r   r   r   r2     s    z*DefaultErrorStrategy.reportFailedPredicatec                 C   sb   |   |¡rd S |  |¡ | ¡ }|  |¡}|  |¡}d| d | |j|j¡ }| ||d ¡ d S )Nzextraneous input rU   )	r   r-   ÚgetCurrentTokenrV   rK   rW   rX   rY   r5   )r   r   ÚtZ	tokenNamerN   rT   r   r   r   rJ   4  s    




ÿz(DefaultErrorStrategy.reportUnwantedTokenc                 C   s^   |   |¡rd S |  |¡ | ¡ }|  |¡}d| |j|j¡ d |  |¡ }| ||d ¡ d S )Nzmissing z at )	r   r-   r[   rK   rW   rX   rY   rV   r5   )r   r   r\   rN   rT   r   r   r   ÚreportMissingTokenP  s    


ÿÿz'DefaultErrorStrategy.reportMissingTokenc                 C   s>   |   |¡}|d ur| ¡  |S |  |¡r2|  |¡S t|ƒ‚d S r   )rI   r:   ÚsingleTokenInsertionÚgetMissingSymbolr   )r   r   ÚmatchedSymbolr   r   r   r   ‰  s    


z"DefaultErrorStrategy.recoverInlinec                 C   sZ   |  ¡  d¡}|jj}|j|j }|jd j}| ||j	¡}||v rR|  
|¡ dS dS d S )Nr@   r   TF)rD   rE   rA   rB   rC   r9   ÚtransitionsÚtargetrF   rH   r]   )r   r   ZcurrentSymbolTyperB   ZcurrentStateÚnextZexpectingAtLL2r   r   r   r^   ª  s    
z)DefaultErrorStrategy.singleTokenInsertionc                 C   sP   |  ¡  d¡}|  |¡}||v rH|  |¡ | ¡  | ¡ }|  |¡ |S d S d S )Né   )rD   rE   rK   rJ   r:   r[   r.   )r   r   ZnextTokenTyperN   r`   r   r   r   rI   Ë  s    


z(DefaultErrorStrategy.singleTokenDeletionc           	   
   C   sÄ   |  ¡ }|  |¡}|d }|tjkr*d}nLd }|t|jƒk rF|j| }|d u rf|t|jƒk rf|j| }dt|ƒ d }|}| ¡  	d¡}|j
tjkr |d ur |}| ¡  |j||tjdd|j|j¡S )Nr   z<missing EOF>z	<missing ú>r    )r[   rK   r   rP   ÚlenrX   rY   ÚstrrD   ÚLTr4   ZgetTokenFactoryÚcreateÚsourceZDEFAULT_CHANNELÚlineÚcolumn)	r   r   ZcurrentSymbolrN   ZexpectedTokenTypeZ	tokenTextÚnameÚcurrentZlookbackr   r   r   r_   ï  s&    



þz%DefaultErrorStrategy.getMissingSymbolc                 C   s   |  ¡ S r   )rK   r   r   r   r   rK     s    z&DefaultErrorStrategy.getExpectedTokens)r\   c                 C   sH   |d u rdS |j }|d u r>|jtjkr,d}ndt|jƒ d }|  |¡S )Nz
<no token>rO   ú<re   )Útextr4   r   rP   rg   rQ   )r   r\   rM   r   r   r   rV     s    z)DefaultErrorStrategy.getTokenErrorDisplay)rM   c                 C   s0   |  dd¡}|  dd¡}|  dd¡}d| d S )NÚ
z\núz\rú	z\tú')Úreplace)r   rM   r   r   r   rQ     s    z%DefaultErrorStrategy.escapeWSAndQuotec                 C   sj   |j j}|j}tƒ }|d urZ|jdkrZ|j|j }|jd }| |j¡}| 	|¡ |j
}q| tj¡ |S )Nr   )rA   rB   rH   r   ÚinvokingStaterC   ra   rF   ZfollowStaterL   Ú	parentCtxZ	removeOner   rG   )r   r   rB   ÚctxZ
recoverSetrv   ÚrtÚfollowr   r   r   r=   |  s    

z(DefaultErrorStrategy.getErrorRecoverySet)r   Úset_c                 C   s<   |  ¡  d¡}|tjkr8||vr8| ¡  |  ¡  d¡}qd S r?   )rD   rE   r   rP   r:   )r   r   r{   Úttyper   r   r   r>   ‹  s    z!DefaultErrorStrategy.consumeUntil)"r   r   r   r"   r   r   r-   r   r+   r.   r   r   r   r   r   r0   r   r1   r2   rJ   r]   r   r^   rI   r_   rK   r   rV   rg   rQ   r=   Úsetr>   Ú__classcell__r   r   r(   r   r   )   s.   		
>/9!!$br   c                   @   s8   e Zd Zeedœdd„Zedœdd„Zedœdd„Zd	S )
ÚBailErrorStrategyr   c                 C   s(   |j }|d ur||_|j}qt|ƒ‚d S r   )rH   Ú	exceptionrw   r	   )r   r   r   Úcontextr   r   r   r   ´  s
    zBailErrorStrategy.recoverr   c                 C   s   |   |t|ƒ¡ d S r   )r   r   r   r   r   r   r   ¾  s    zBailErrorStrategy.recoverInlinec                 C   s   d S r   r   r   r   r   r   r   Â  s    zBailErrorStrategy.syncN)r   r   r   r   r   r   r   r   r   r   r   r   r   ®  s   
r   )ÚsysZantlr4.IntervalSetr   Zantlr4.Tokenr   Zantlr4.atn.ATNStater   Zantlr4.error.Errorsr   r   r   r   r	   r   Úobjectr
   r   r   r   r   r   r   Ú<module>   s        
