
    ^h4                        S r SSKJr  SSKr/ SQ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S jrSS j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.
    )annotationsN)RepeatVariableRegex	Lookaheadtokenize_regexparse_regexc                  ,    \ rS rSrSrSS jrSS jrSrg)	Node    zH
Base class for all the grammar nodes.
(You don't initialize this one.)
c                    [        X/5      $ N)NodeSequenceself
other_nodes     o/home/james-whalen/.local/lib/python3.13/site-packages/prompt_toolkit/contrib/regular_languages/regex_parser.py__add__Node.__add__&   s    T.//    c                    [        X/5      $ r   )AnyNoder   s     r   __or__Node.__or__)   s    )**r    Nr   r   returnr   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes__r   r   r   r   r       s    
0+r   r   c                  6    \ rS rSrSrSS jrS	S jrS
S j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__AnyNode.__init__4        r   c                4    [        U R                  U/-   5      $ r   )r   r)   r   s     r   r   AnyNode.__or__7   s    t}}
|344r   c                P    U R                   R                   SU R                  < S3$ N()	__class__r   r)   r   s    r   __repr__AnyNode.__repr__:   %    ..))*!DMM+<A>>r   r(   Nr)   
list[Node]r   Noner   r   str)	r   r    r!   r"   r#   r+   r   r7   r$   r   r   r   r   r   -   s    !5?r   r   c                  6    \ rS rSrSrSS jrS	S jrS
S j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+   NodeSequence.__init__D   r-   r   c                4    [        U R                  U/-   5      $ r   )r   r)   r   s     r   r   NodeSequence.__add__G   s    DMMZL899r   c                P    U R                   R                   SU R                  < S3$ r1   r4   r6   s    r   r7   NodeSequence.__repr__J   r9   r   r(   Nr:   r   r=   )	r   r    r!   r"   r#   r+   r   r7   r$   r   r   r   r   r   >   s    
!:?r   r   c                  ,    \ rS rSrSrSS jrSS jrSrg)	r   N   z
Regular expression.
c                <    [         R                  " U5        Xl        g r   )recompileregex)r   rL   s     r   r+   Regex.__init__S   s    


5
r   c                N    U R                   R                   SU R                   S3$ )Nz(/z/))r5   r   rL   r6   s    r   r7   Regex.__repr__X   s$    ..))*"TZZL;;r   )rL   N)rL   r>   r   r<   r=   r   r    r!   r"   r#   r+   r7   r$   r   r   r   r   r   N   s    
<r   r   c                  0    \ rS rSrSrSSS jjrS	S jrSrg)
r   \   z
Lookahead expression.
c                    Xl         X l        g r   	childnodenegative)r   rU   rV   s      r   r+   Lookahead.__init__a   s    " r   c                P    U R                   R                   SU R                  < S3$ r1   r5   r   rU   r6   s    r   r7   Lookahead.__repr__e   s%    ..))*!DNN+=Q??r   rT   N)F)rU   r   rV   boolr   r<   r=   rP   r   r   r   r   r   \   s    !@r   r   c                  0    \ rS rSrSrSSS jjrS	S jrSrg)
r   i   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.
c                    Xl         X l        g r   rU   varname)r   rU   r`   s      r   r+   Variable.__init__r   s    "r   c                l    U R                   R                   SU R                  < SU R                  < S3$ )N(childnode=z
, varname=r3   )r5   r   rU   r`   r6   s    r   r7   Variable.__repr__v   s4    ..))*+dnn5GzRVR^R^Qaabccr   r_   N) )rU   r   r`   r>   r   r<   r=   rP   r   r   r   r   r   i   s    dr   r   c                  D    \ rS rSr   S         SS jjrSS jrSrg)	r   z   Nc                4    Xl         X l        X0l        X@l        g r   )rU   
min_repeat
max_repeatgreedy)r   rU   ri   rj   rk   s        r   r+   Repeat.__init__{   s     #$$r   c                P    U R                   R                   SU R                  < S3$ )Nrc   r3   rY   r6   s    r   r7   Repeat.__repr__   s%    ..))*+dnn5GqIIr   )rU   rk   rj   ri   )r   NT)
rU   r   ri   intrj   z
int | Nonerk   r[   r   r<   r=   )r   r    r!   r"   r+   r7   r$   r   r   r   r   r   z   sH     !%

 
 	

 
 

J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.)rJ   rK   VERBOSEmatchendisspaceappend	Exception)inputptokensmtokens        r   r   r      s     	

		4 	

7	A< F
GGEN 1557+U5579-=5==??e$=>> % Mr   c                   ^^^ S/U SSS2   -   mSS jmS	UUU4S j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.
r3   Nc                @    [        U 5      S:X  a  U S   $ [        U 5      $ )z7Turn list into sequence when it contains several items.   r   )lenr   )lsts    r   wrapparse_regex.<locals>.wrap   s!    s8q=q6M$$r   c                    >^^ / m/ mS UUU4S j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OyUS	;   a  US
:H  n[	        TS   SUS9TS'   GO[U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  OyUR                  S5      (       a  [        U S35      eUR                  S5      (       a  [        U< S3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   )ru   r   )ior_listresultr   s    r   wrapped_result3parse_regex.<locals>._parse.<locals>.wrapped_result   sA    "}F|#v&9AQ9::9s   Az(?P<   r}   )r`   )*z*?r   )rk   )+z+?r   r   )ri   rk   )?z??zNothing to repeat.r   r   )ri   rj   rk   |)r2   z(?:z(?!T)rV   z(?=Fr3   #{z#-style repetition not yet supportedz(?z not supportedzExpecting ')' tokenr   r   )
pop
startswithr   ru   r   rv   reprr   rt   r   )	r   tvariablerk   r   r   _parsery   r   s	       @@r   r   parse_regex.<locals>._parse   s   $&	; 	; 

A||F###FHa"g>h'k!c#F2Jv>r
k!c#F2J1VLr
k!R<#$84<$GHH#XF!'r
qQv"F2J cv&l"fh'ei4@Aei5ABc%''c""c""1#%H IJJd##1%~ 677eAh'k fn -..r   r   zUnmatched parentheses.)r   r;   r   r   r   )r   rv   )regex_tokensr   r   ry   r   s     @@@r   r	   r	      sR    
 TrT 22F%B/ B/H XF
6{a011r   )rw   r>   r   	list[str])r   r   r   r   )r#   
__future__r   rJ   __all__r   r   r   r   r   r   r   r   r	   r   r   r   <module>r      s}   " # 	
+ 
+?d ?"?4 ? <D <
@ 
@dt d"JT J"2jWr   