ó
±xYc           @  sl   d  Z  d d l m Z d d l m Z m Z d d l m Z d d l m	 Z	 d Z
 d e f d	 „  ƒ  YZ d
 S(   u"   
Completer for a regular grammar.
iÿÿÿÿ(   t   unicode_literals(   t	   Completert
   Completion(   t   Documenti   (   t   _CompiledGrammaru   GrammarCompletert   GrammarCompleterc           B  s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   uf  
    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.
    c         C  s@   t  | t ƒ s t ‚ t  | t ƒ s* t ‚ | |  _ | |  _ d  S(   N(   t
   isinstanceR   t   AssertionErrort   dictt   compiled_grammart
   completers(   t   selfR	   R
   (    (    sR   lib/python2.7/site-packages/prompt_toolkit/contrib/regular_languages/completion.pyt   __init__   s    	c         c  sS   |  j  j | j ƒ } | rO |  j |  j | | ƒ ƒ } x | D] } | Vq= Wn  d  S(   N(   R	   t   match_prefixt   text_before_cursort   _remove_duplicatest   _get_completions_for_match(   R   t   documentt   complete_eventt   mt   completionst   c(    (    sR   lib/python2.7/site-packages/prompt_toolkit/contrib/regular_languages/completion.pyt   get_completions    s    c         c  sø   xñ | j  ƒ  D]ã } | j } | j } |  j j | ƒ } | r | j } |  j j | | ƒ } t | t	 | ƒ ƒ }	 x} | j
 |	 | ƒ D]f }
 | t	 | ƒ |
 j  |
 j } t d |  j j | | ƒ d | t	 | j ƒ d |
 j d |
 j ƒ Vqƒ Wq q Wd S(   u°   
        Yield all the possible completions for this input string.
        (The completer assumes that the cursor position was at the end of the
        input string.)
        t   textt   start_positiont   displayt   display_metaN(   t	   end_nodest   varnamet   startR
   t   gett   valueR	   t   unescapeR   t   lenR   R   R   R   t   escapet   stringR   R   (   R   t   matchR   t   match_variableR   R   t	   completerR   t   unwrapped_textR   t
   completiont   new_text(    (    sR   lib/python2.7/site-packages/prompt_toolkit/contrib/regular_languages/completion.pyR   *   s    				c         C  s7   g  } x* | D]" } | | k r | j  | ƒ q q W| S(   uÇ   
        Remove duplicates, while keeping the order.
        (Sometimes we have duplicates, because the there several matches of the
        same grammar, each yielding similar completions.)
        (   t   append(   R   t   itemst   resultt   i(    (    sR   lib/python2.7/site-packages/prompt_toolkit/contrib/regular_languages/completion.pyR   J   s
    (   t   __name__t
   __module__t   __doc__R   R   R   R   (    (    (    sR   lib/python2.7/site-packages/prompt_toolkit/contrib/regular_languages/completion.pyR      s
   		
	 N(   u   GrammarCompleter(   R0   t
   __future__R    t   prompt_toolkit.completionR   R   t   prompt_toolkit.documentR   t   compilerR   t   __all__R   (    (    (    sR   lib/python2.7/site-packages/prompt_toolkit/contrib/regular_languages/completion.pyt   <module>   s   