
    %e                    t    d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ dgZ G d	 de          Zd
S )z"
Completer for a regular grammar.
    )annotations)Iterable)CompleteEvent	Completer
Completion)Document   )Match_CompiledGrammarGrammarCompleterc                  2    e Zd ZdZddZddZddZddZdS )r   af  
    Completer which can be used for autocompletion according to variables in
    the grammar. Each variable can have a different autocompleter.

    :param compiled_grammar: `GrammarCompleter` instance.
    :param completers: `dict` mapping variable names of the grammar to the
                       `Completer` instances to be used for each variable.
    compiled_grammarr   
completersdict[str, Completer]returnNonec                "    || _         || _        d S N)r   r   )selfr   r   s      Slib/python3.11/site-packages/prompt_toolkit/contrib/regular_languages/completion.py__init__zGrammarCompleter.__init__   s     !1$    documentr   complete_eventr   Iterable[Completion]c              #     K   | j                             |j                  }|r3|                     |                     ||                    }|E d {V  d S d S r   )r   match_prefixtext_before_cursor_remove_duplicates_get_completions_for_match)r   r   r   mcompletionss        r   get_completionsz GrammarCompleter.get_completions"   s{       !..x/JKK 	#11//>BB K #"""""""""	# 	#r   matchr
   c           	   #  $  K   |                                 D ]}|j        }|j        }| j                            |          }|r|j        }| j                            ||          }t          |t          |                    }	|
                    |	|          D ]v}
|dt          |          |
j        z            |
j        z   }t          | j                            ||          |t          |j                  z
  |
j        |
j                  V  wdS )z
        Yield all the possible completions for this input string.
        (The completer assumes that the cursor position was at the end of the
        input string.)
        N)textstart_positiondisplaydisplay_meta)	end_nodesvarnamestartr   getvaluer   unescaper   lenr#   r'   r&   r   escapestringr(   r)   )r   r$   r   match_variabler+   r,   	completerr&   unwrapped_textr   
completionnew_texts               r   r    z+GrammarCompleter._get_completions_for_match.   s<      $oo// 	 	N$,G"(E++G44I %+ "&!6!?!?!N!N $NC4G4GHH #,";";Hn"U"U  J&'NTZ5N)N'NO$/*  %!299'8LL',s5</@/@'@ * 2%/%<	      /	 	r   itemslist[Completion]c                F    g }|D ]}||vr|                     |           |S )z
        Remove duplicates, while keeping the order.
        (Sometimes we have duplicates, because the there several matches of the
        same grammar, each yielding similar completions.)
        )append)r   r8   resultis       r   r   z#GrammarCompleter._remove_duplicatesT   s9     $& 	! 	!Aa   r   N)r   r   r   r   r   r   )r   r   r   r   r   r   )r$   r
   r   r   r   r   )r8   r   r   r9   )__name__
__module____qualname____doc__r   r#   r    r    r   r   r   r      so         % % % %
# 
# 
# 
#$ $ $ $L
 
 
 
 
 
r   N)rA   
__future__r   typingr   prompt_toolkit.completionr   r   r   prompt_toolkit.documentr   compilerr
   r   __all__r   rB   r   r   <module>rI      s     # " " " " "       J J J J J J J J J J , , , , , , - - - - - - - - 
L L L L Ly L L L L Lr   