o
    �õ±j`  ã                   @  sr   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ d
gZG dd
„ d
eƒZdS )z�
`GrammarLexer` is compatible with other lexers and can be used to highlight
the input using a regular grammar with annotations.
é    )Úannotations)ÚCallable)ÚDocument)ÚStyleAndTextTuples)Úsplit_lines)ÚLexeré   )Ú_CompiledGrammarÚGrammarLexerc                   @  s4   e Zd ZdZ		dddd„Zddd„Zddd„ZdS )r
   a„  
    Lexer which can be used for highlighting of fragments according to variables in the grammar.

    (It does not actual lexing of the string, but it exposes an API, compatible
    with the Pygments lexer class.)

    :param compiled_grammar: Grammar as returned by the `compile()` function.
    :param lexers: Dictionary mapping variable names of the regular grammar to
                   the lexers that should be used for this part. (This can
                   call other lexers recursively.) If you wish a part of the
                   grammar to just get one fragment, use a
                   `prompt_toolkit.lexers.SimpleLexer`.
    Ú NÚcompiled_grammarr	   Údefault_styleÚstrÚlexersúdict[str, Lexer] | NoneÚreturnÚNonec                 C  s   || _ || _|p	i | _d S ©N)r   r   r   )Úselfr   r   r   © r   ú¡/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/contrib/regular_languages/lexer.pyÚ__init__%   s   zGrammarLexer.__init__Útextr   c                   s,  ˆ j  |¡}|r‘‡ fdd„|D ƒ}| ¡ D ]_}ˆ j |j¡}|rtt||j|j… ƒ}| 	|¡}g }t
t|jƒƒD ]}	| ||	ƒ¡ | d¡ q8|rM| ¡  |j}	|D ]!^}
}}|D ]}||	 d ˆ jkrn|
||	 d f||	< |	d7 }	qYqRq| ¡ }|r�t
|j|jƒD ]}	d||	 d f||	< q‚|S d|fgS )Nc                   s   g | ]}ˆ j |f‘qS r   )r   )Ú.0Úc©r   r   r   Ú
<listcomp>3   s    z4GrammarLexer._get_text_fragments.<locals>.<listcomp>)r   Ú
r   r   zclass:trailing-inputr   )r   Úmatch_prefixÚ	variablesr   ÚgetÚvarnamer   ÚstartÚstopÚlex_documentÚrangeÚlenÚlinesÚextendÚappendÚpopr   Útrailing_input)r   r   ÚmÚ
charactersÚvÚlexerÚdocumentÚlexer_tokens_for_lineÚtext_fragmentsÚiÚtÚsÚ_r   r+   r   r   r   Ú_get_text_fragments/   s8   

ý€
z GrammarLexer._get_text_fragmentsr0   r   ú#Callable[[int], StyleAndTextTuples]c                   s&   t t|  |j¡ƒƒ‰ d‡ fdd„}|S )NÚlinenoÚintr   r   c                   s"   zˆ |  W S  t y   g  Y S w r   )Ú
IndexError)r9   ©r'   r   r   Úget_lineX   s
   
ÿz+GrammarLexer.lex_document.<locals>.get_line)r9   r:   r   r   )Úlistr   r7   r   )r   r0   r=   r   r<   r   r$   U   s   zGrammarLexer.lex_document)r   N)r   r	   r   r   r   r   r   r   )r   r   r   r   )r0   r   r   r8   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r7   r$   r   r   r   r   r
      s    ü

&N)rB   Ú
__future__r   Úcollections.abcr   Úprompt_toolkit.documentr   Ú"prompt_toolkit.formatted_text.baser   Ú#prompt_toolkit.formatted_text.utilsr   Úprompt_toolkit.lexersr   Úcompilerr	   Ú__all__r
   r   r   r   r   Ú<module>   s    ÿ