
    J                     v    S r SSKJr  SSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SS	KJr  S
r " S S\5      rg)z
`GrammarLexer` is compatible with Pygments lexers and can be used to highlight
the input using a regular grammar with token annotations.
    )unicode_literals)Document)Lexer)split_lines)Token   )_CompiledGrammar)range)GrammarLexerc                   .    \ rS rSrSrSS jrS rS rSrg)	r      a]  
Lexer which can be used for highlighting of tokens 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 token, use a
               `prompt_toolkit.layout.lexers.SimpleLexer`.
Nc                 B   [        U[        5      (       d   eUb  [        U[        5      (       d   eUb'  [        S UR	                  5        5       5      (       d   eUb  [        U[
        5      (       d   eXl        U=(       d    [        U l        U=(       d    0 U l	        g )Nc              3   F   #    U  H  u  p[        U[        5      v   M     g 7fN)
isinstancer   ).0kvs      Alib/third_party/prompt_toolkit/contrib/regular_languages/lexer.py	<genexpr>(GrammarLexer.__init__.<locals>.<genexpr>$   s     $UndaZ5%9%9ns   !)
r   r	   tupleallitemsdictcompiled_grammarr   default_tokenlexers)selfr   r   r   s       r   __init__GrammarLexer.__init__!   s    *,<====$
=%(H(HHH~$Uflln$U!U!UUU~FD!9!999 0*3el    c                    U R                   R                  U5      nU(       Ga  U Vs/ s H  o@R                  U/PM     nnUR                  5        GH  nU R                  R                  UR                  5      nU(       d  M2  [        X&R                  UR                   5      nUR                  X5      n	/ n
[        [        UR                  5      5       H1  nU
R                  U	" U5      5        U
R                  [         S45        M3     U
(       a  U
R#                  5         UR                  nU
 H/  u  pU H$  nX[   S   U R                  :X  a  XU   S'   US-  nM&     M1     GM     UR%                  5       nU(       a<  [        UR                  UR                  5       H  n[         R&                  X[   S'   M     U$ [         U4/$ s  snf )N
r   r   )r   match_prefixr   	variablesr   getvarnamer   startstoplex_documentr
   lenlinesextendappendr   poptrailing_inputTrailingInput)r   clitextmc
charactersr   lexerdocumentlexer_tokens_for_linelexer_tokensitsr1   s                  r   _get_tokensGrammarLexer._get_tokens+   s   !!..t4;?@4a--q14J@[[] 		25'WWQVV(<=H,1,>,>s,M)#%L"3x~~#67$++,A!,DE$++UDM: 8 $$((*A ,!"A)}Q/43E3EE341a 0FA "# !-! #. --/N~33^5H5HIA',':':JM!$ J DM?"A As   Gc                 n   ^ [        [        U R                  XR                  5      5      5      mU4S jnU$ )Nc                 4   >  TU    $ ! [          a    / s $ f = fr   )
IndexError)linenor-   s    r   get_line+GrammarLexer.lex_document.<locals>.get_lineT   s&    V}$ 	s    )listr   r?   r4   )r   r3   r9   rE   r-   s       @r   r+   GrammarLexer.lex_documentQ   s-    [!1!1#}}!EFG	 r"   )r   r   r   )NN)	__name__
__module____qualname____firstlineno____doc__r    r?   r+   __static_attributes__ r"   r   r   r      s    #$#L	r"   r   N)rM   
__future__r   prompt_toolkit.documentr   prompt_toolkit.layout.lexersr   prompt_toolkit.layout.utilsr   prompt_toolkit.tokenr   compilerr	   	six.movesr
   __all__r   rO   r"   r   <module>rX      s6    ( , . 3 & & 
G5 Gr"   