
    b                         S r SSKJr  SSKrSr " S S\5      r " S S\5      r " S	 S
\5      r " S S\5      r	 " S S\5      r
 " S S\5      r " S S\5      rS rS rg)a  
Parser for parsing a regular expression.
Take a string representing a regular expression and return the root node of its
parse tree.

usage::

    root_node = parse_regex('(hello|world)')

Remarks:
- The regex parser processes multiline, it ignores all whitespace and supports
  multiple named groups with the same name and #-style comments.

Limitations:
- Lookahead is not supported.
    )unicode_literalsN)RepeatVariableRegex	Lookaheadtokenize_regexparse_regexc                   $    \ rS rSrSrS rS rSrg)Node   zH
Base class for all the grammar nodes.
(You don't initialize this one.)
c                     [        X/5      $ N)Sequenceself
other_nodes     Hlib/third_party/prompt_toolkit/contrib/regular_languages/regex_parser.py__add__Node.__add__$   s    *++    c                     [        X/5      $ r   )Anyr   s     r   __or__Node.__or__'   s    D%&&r    N)__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__r   r   r   r   r      s    ,'r   r   c                   *    \ rS rSrSrS rS rS rSrg)r   +   z
Union operation (OR operation) between several grammars. You don't
initialize this yourself, but it's a result of a "Grammar1 | Grammar2"
operation.
c                     Xl         g r   childrenr   r&   s     r   __init__Any.__init__1        r   c                 4    [        U R                  U/-   5      $ r   )r   r&   r   s     r   r   
Any.__or__4   s    4==J</00r   c                 R    U R                   R                  < SU R                  < S3$ N()	__class__r   r&   r   s    r   __repr__Any.__repr__7       >>22DMMBBr   r%   N)	r   r   r   r   r    r(   r   r4   r!   r   r   r   r   r   +   s    
!1Cr   r   c                   *    \ rS rSrSrS rS rS rSrg)r   ;   z
Concatenation operation of several grammars. You don't initialize this
yourself, but it's a result of a "Grammar1 + Grammar2" operation.
c                     Xl         g r   r%   r'   s     r   r(   Sequence.__init__@   r*   r   c                 4    [        U R                  U/-   5      $ r   )r   r&   r   s     r   r   Sequence.__add__C   s    455r   c                 R    U R                   R                  < SU R                  < S3$ r.   r1   r3   s    r   r4   Sequence.__repr__F   r6   r   r%   N)	r   r   r   r   r    r(   r   r4   r!   r   r   r   r   r   ;   s    !6Cr   r   c                   $    \ rS rSrSrS rS rSrg)r   J   z
Regular expression.
c                 <    [         R                  " U5        Xl        g r   )recompileregex)r   rD   s     r   r(   Regex.__init__N   s    


5
r   c                 R    U R                   R                  < SU R                  < S3$ )Nz(/z/))r2   r   rD   r3   s    r   r4   Regex.__repr__S   s    !^^44djjAAr   )rD   Nr   r   r   r   r    r(   r4   r!   r   r   r   r   r   J   s    
Br   r   c                   (    \ rS rSrSrSS jrS rSrg)r   W   z
Lookahead expression.
c                     Xl         X l        g r   	childnodenegative)r   rM   rN   s      r   r(   Lookahead.__init__[   s    " r   c                 R    U R                   R                  < SU R                  < S3$ r.   r2   r   rM   r3   s    r   r4   Lookahead.__repr___   s    >>22DNNCCr   rL   N)FrH   r   r   r   r   r   W   s    !Dr   r   c                   (    \ rS rSrSrSS jrS rSrg)r   c   z
Mark a variable in the regular grammar. This will be translated into a
named group. Each variable can have his own completer, validator, etc..

:param childnode: The grammar which is wrapped inside this variable.
:param varname: String.
Nc                     Xl         X l        g r   rM   varname)r   rM   rW   s      r   r(   Variable.__init__k   s    "r   c                 n    U R                   R                  < SU R                  < SU R                  < S3$ )N(childnode=z
, varname=r0   )r2   r   rM   rW   r3   s    r   r4   Variable.__repr__o   s'    NN##T^^T\\C 	Cr   rV   r   rH   r   r   r   r   r   c   s    Cr   r   c                   $    \ rS rSrSS jrS rSrg)r   t   Nc                 4    Xl         X l        X0l        X@l        g r   )rM   
min_repeat
max_repeatgreedy)r   rM   r_   r`   ra   s        r   r(   Repeat.__init__u   s    "$$r   c                 R    U R                   R                  < SU R                  < S3$ )NrZ   r0   rQ   r3   s    r   r4   Repeat.__repr__{   s    %)^^%<%<dnnMMr   )rM   ra   r`   r_   )r   NT)r   r   r   r   r(   r4   r!   r   r   r   r   r   t   s    Nr   r   c                 P   [         R                  " S[         R                  5      n/ nU (       aw  UR                  U 5      nU(       aK  U SUR	                  5        XR	                  5       S pUR                  5       (       d  UR                  U5        O[        S5      eU (       a  Mw  U$ )z
Takes a string, representing a regular expression as input, and tokenizes
it.

:param input: string, representing a regular expression.
:returns: List of tokens.
a  ^(
        \(\?P\<[a-zA-Z0-9_-]+\>  | # Start of named group.
        \(\?#[^)]*\)             | # Comment
        \(\?=                    | # Start of lookahead assertion
        \(\?!                    | # Start of negative lookahead assertion
        \(\?<=                   | # If preceded by.
        \(\?<                    | # If not preceded by.
        \(?:                     | # Start of group. (non capturing.)
        \(                       | # Start of group.
        \(?[iLmsux]              | # Flags.
        \(?P=[a-zA-Z]+\)         | # Back reference to named group
        \)                       | # End of group.
        \{[^{}]*\}               | # Repetition
        \*\? | \+\? | \?\?\      | # Non greedy repetition.
        \* | \+ | \?             | # Repetition
        \#.*\n                   | # Comment
        \\. |

        # Character group.
        \[
            ( [^\]\\]  |  \\.)*
        \]                  |

        [^(){}]             |
        .
    )NzCould not tokenize input regex.)rB   rC   VERBOSEmatchendisspaceappend	Exception)inputptokensmtokens        r   r   r      s     	

 	2 **3	A6 F
GGEN !%%'?E%%'(O5==??e$=>> % Mr   c                 |   ^^^ S/U SSS2   -   mS mUUU4S jmT" 5       n[        T5      S:w  a  [        S5      eU$ )zF
Takes a list of tokens from the tokenizer, and returns a parse tree.
r0   Nc                 @    [        U 5      S:X  a  U S   $ [        U 5      $ )z8Turn list into sequence when it contains several items.    r   )lenr   )lsts    r   wrapparse_regex.<locals>.wrap   s     s8q=q6MC= r   c                    >^^ / m/ mUUU4S jn T(       Ga  TR                  5       nUR                  S5      (       a%  [        T" 5       USS S9nTR                  U5        GOUS;   a  US:H  n[	        TS   US9TS'   GOxUS	;   a  US
:H  n[	        TS   SUS9TS'   GOZUS;   a6  T/ :X  a  [        S[        T5      -   5      eUS:H  n[	        TS   SSUS9TS'   GOUS:X  a  TR                  T5        / mGOUS;   a  TR                  T" 5       5        OUS:X  a  TR                  [        T" 5       SS95        OUS:X  a  TR                  [        T" 5       SS95        OUS:X  a  U " 5       $ UR                  S5      (       a  OxUR                  S5      (       a  [        SU-  5      eUR                  S5      (       a  [        SU-  5      eUR                  5       (       a  OTR                  [        U5      5        T(       a  GM  [        S5      e) Nc                     > T/ :X  a  T" T5      $ TR                  T5        [        T V s/ s H  n T" U 5      PM     sn 5      $ s  sn f r   )rj   r   )ior_listresultrw   s    r   wrapped_result3parse_regex.<locals>._parse.<locals>.wrapped_result   sA    "}F|#v&W5WDGW5665s   Az(?P<   rr   )rW   )*z*?r   )ra   )+z+?r   rt   )r_   ra   )?z??zNothing to repeat.r   r   )r_   r`   ra   |)r/   z(?:z(?!T)rN   z(?=Fr0   #{z%{}-style repitition not yet supportedz(?z%r not supportedzExpecting ')' token)
pop
startswithr   rj   r   rk   reprr   ri   r   )	r~   tvariablera   r|   r}   _parsern   rw   s	       @@r   r   parse_regex.<locals>._parse   s   	7 

A||F###FHa"g>h'k!s(#F2Jv>r
k!s(#F2J1VLr
k!R<#$84<$GHH3hF!'r
qQW]!^F2Jcv&l"fh'ei4@Aei5ABc%''c""c"" G! KLLd## 2Q 677eAh'g fj -..r   r   zUnmatched parantheses.)ru   rk   )regex_tokensr}   r   rn   rw   s     @@@r   r	   r	      sL    
 U\$B$''F!@/D XF
6{a011r   )r    
__future__r   rB   __all__objectr   r   r   r   r   r   r   r   r	   r   r   r   <module>r      s     ( 		'6 	'C$ C Ct C
BD 
B	D 	DCt C"NT N/dUr   