U
    ouf
                     @  sR   d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 dgZ
G dd deZd	S )
z#
Validator for a regular language.
    )annotations)Document)ValidationError	Validator   )_CompiledGrammarGrammarValidatorc                   @  s2   e Zd ZdZddddddZddd	d
dZdS )r   aZ  
    Validator which can be used for validation according to variables in
    the grammar. Each variable can have its own validator.

    :param compiled_grammar: `GrammarCompleter` instance.
    :param validators: `dict` mapping variable names of the grammar to the
                       `Validator` instances to be used for each variable.
    r   zdict[str, Validator]None)compiled_grammar
validatorsreturnc                 C  s   || _ || _d S )N)r
   r   )selfr
   r    r   W/tmp/pip-unpacked-wheel-8milen2s/prompt_toolkit/contrib/regular_languages/validation.py__init__   s    zGrammarValidator.__init__r   )documentr   c                 C  s   | j |j}|r| D ]}| j|j}|r| j |j|j}t	|t
|}z|| W q tk
r } zt|j|j |jd|W 5 d }~X Y qX qntt
|jddd S )N)cursor_positionmessagezInvalid command)r
   matchtext	variablesr   getvarnameunescapevaluer   lenvalidater   startr   r   )r   r   mvZ	validatorZunwrapped_textZinner_documenter   r   r   r       s(    
 zGrammarValidator.validateN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s   	N)r$   
__future__r   Zprompt_toolkit.documentr   Zprompt_toolkit.validationr   r   compilerr   __all__r   r   r   r   r   <module>   s   