
    Qf4                        d Z ddlmZ ddlZg dZ G d d          Z G d de          Z G d	 d
e          Z G d de          Z G d de          Z	 G d de          Z
 G d de          ZddZddZdS )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                  "    e Zd ZdZd	dZd
dZdS )NodezT
    Base class for all the grammar nodes.
    (You don't initialize this one.)
    
other_nodereturnNodeSequencec                $    t          | |g          S N)r   selfr   s     g/var/www/html/env/lib/python3.11/site-packages/prompt_toolkit/contrib/regular_languages/regex_parser.py__add__zNode.__add__&   s    T:.///    AnyNodec                $    t          | |g          S r   )r   r   s     r   __or__zNode.__or__)   s    j)***r   Nr   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r   r    r   r   r   r       sF         
0 0 0 0+ + + + + +r   r   c                  *    e Zd ZdZddZdd	ZddZdS )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.
    children
list[Node]r   Nonec                    || _         d S r   r!   r   r!   s     r   __init__zAnyNode.__init__4        r   r   r   c                2    t          | j        |gz             S r   )r   r!   r   s     r   r   zAnyNode.__or__7   s    t}
|3444r   strc                0    | j         j         d| j        dS N()	__class__r   r!   r   s    r   __repr__zAnyNode.__repr__:        .)>>DM>>>>r   Nr!   r"   r   r#   r   r   r*   )r   r   r   r   r'   r   r2   r   r   r   r   r   -   sZ         ! ! ! !5 5 5 5? ? ? ? ? ?r   r   c                  *    e Zd ZdZddZdd	ZddZdS )r   z
    Concatenation operation of several grammars. You don't initialize this
    yourself, but it's a result of a "Grammar1 + Grammar2" operation.
    r!   r"   r   r#   c                    || _         d S r   r%   r&   s     r   r'   zNodeSequence.__init__D   r(   r   r   r   c                2    t          | j        |gz             S r   )r   r!   r   s     r   r   zNodeSequence.__add__G   s    DMZL8999r   r*   c                0    | j         j         d| j        dS r,   r/   r1   s    r   r2   zNodeSequence.__repr__J   r3   r   Nr4   r   r5   )r   r   r   r   r'   r   r2   r   r   r   r   r   >   sZ         
! ! ! !: : : :? ? ? ? ? ?r   r   c                  "    e Zd ZdZd	dZd
dZdS )r   z
    Regular expression.
    regexr*   r   r#   c                <    t          j        |           || _        d S r   )recompiler;   )r   r;   s     r   r'   zRegex.__init__S   s    

5


r   c                0    | j         j         d| j         dS )Nz(/z/))r0   r   r;   r1   s    r   r2   zRegex.__repr__X   s     .);;TZ;;;;r   N)r;   r*   r   r#   r5   r   r   r   r   r'   r2   r   r   r   r   r   N   sF            
< < < < < <r   r   c                  $    e Zd ZdZddd	ZddZdS )r   z
    Lookahead expression.
    F	childnoder   negativeboolr   r#   c                "    || _         || _        d S r   )rB   rC   )r   rB   rC   s      r   r'   zLookahead.__init__a   s    " r   r*   c                0    | j         j         d| j        dS r,   r0   r   rB   r1   s    r   r2   zLookahead.__repr__e   s     .)??DN????r   N)F)rB   r   rC   rD   r   r#   r5   r@   r   r   r   r   r   \   sQ         ! ! ! ! !@ @ @ @ @ @r   r   c                  $    e Zd ZdZddd	Zdd
ZdS )r   a  
    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.
     rB   r   varnamer*   r   r#   c                "    || _         || _        d S r   )rB   rJ   )r   rB   rJ   s      r   r'   zVariable.__init__r   s    "r   c                @    | j         j         d| j        d| j        dS )N(childnode=z
, varname=r.   )r0   r   rB   rJ   r1   s    r   r2   zVariable.__repr__v   s,    .)ccdnccRVR^ccccr   N)rI   )rB   r   rJ   r*   r   r#   r5   r@   r   r   r   r   r   i   sQ             d d d d d dr   r   c                  &    e Zd Z	 	 	 dddZddZdS )r   r   NTrB   r   
min_repeatint
max_repeat
int | NonegreedyrD   r   r#   c                >    || _         || _        || _        || _        d S r   )rB   rO   rQ   rS   )r   rB   rO   rQ   rS   s        r   r'   zRepeat.__init__{   s$     #$$r   r*   c                0    | j         j         d| j        dS )NrM   r.   rG   r1   s    r   r2   zRepeat.__repr__   s     .)IIdnIIIIr   )r   NT)
rB   r   rO   rP   rQ   rR   rS   rD   r   r#   r5   )r   r   r   r'   r2   r   r   r   r   r   z   sR         !%
 
 
 
 
J J J J J Jr   r   inputr*   r   	list[str]c                `   t          j        dt           j                  }g }| r|                    |           }|rb| d|                                         | |                                d         } }|                                s|                    |           nt          d          | |S )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.)r=   r>   VERBOSEmatchendisspaceappend	Exception)rV   ptokensmtokens        r   r   r      s     	
		4 	
7	 	A< F
 ?GGENN 	? 15577+U1557799-=5E==?? %e$$$=>>>  ? Mr   regex_tokensc                    dg| ddd         z   dddfd	             }t                    d
k    rt          d          |S )zN
    Takes a list of tokens from the tokenizer, and returns a parse tree.
    r.   Nlstr"   r   r   c                V    t          |           dk    r| d         S t          |           S )z7Turn list into sequence when it contains several items.   r   )lenr   )rf   s    r   wrapzparse_regex.<locals>.wrap   s(    s88q==q6M$$$r   c                    g g d"fd} rQ                                 }|                    d          r8t                       |dd                   }                    |           n|dv r"|d	k    }t	          d         |
          d<   n|dv r#|dk    }t	          d         d|          d<   n|dv rIg k    rt          dt                    z             |dk    }t	          d         dd|          d<   nS|dk    r                               g n4|dv r                                            n|dk    r-                    t                       d                     n|dk    r-                    t                       d                     n|dk    r
 |             S |                    d          rn|                    d          rt          | d          |                    d          rt          |d           |                                rn"                    t          |                     Qt          d!          )#Nr   r   c                      g k    r           S                                  t          fd D                       S )Nc                &    g | ]} |          S r   r   ).0irj   s     r   
<listcomp>zGparse_regex.<locals>._parse.<locals>.wrapped_result.<locals>.<listcomp>   s!    999AQ999r   )r]   r   )or_listresultrj   s   r   wrapped_resultz3parse_regex.<locals>._parse.<locals>.wrapped_result   sO    "}}tF||#v&&&9999999:::r   z(?P<   re   )rJ   )*z*?ru   )rS   )+z+?rv   rh   )rO   rS   )?z??zNothing to repeat.rw   r   )rO   rQ   rS   |)r-   z(?:z(?!T)rC   z(?=Fr.   #{z#-style repetition not yet supportedz(?z not supportedzExpecting ')' tokenr   r   )
pop
startswithr   r]   r   r^   reprr   r\   r   )	rs   tvariablerS   rq   rr   _parser`   rj   s	       @@r   r   zparse_regex.<locals>._parse   s   $&	; 	; 	; 	; 	; 	; 	; 	;  5	(

A||F## 2(#FFHHa"g>>>h''''k!!c#F2Jv>>>r

k!!c#F2J1VLLLr

k!!R<<#$84<<$GHHH#XF!'r
qQv" " "F2JJ cv&&&l""ffhh''''ei4@@@AAAAei5AAABBBBc%~'''c"" (c"" 
(1 I I IJJJd## (1 6 6 6777 (eAhh'''k  5	(n -...r   r   zUnmatched parentheses.)rf   r"   r   r   r{   )ri   r^   )rc   rr   r   r`   rj   s     @@@r   r	   r	      s    
 TTrT 22F% % % %B/ B/ B/ B/ B/ B/ B/ B/H VXXF
6{{a0111r   )rV   r*   r   rW   )rc   rW   r   r   )r   
__future__r   r=   __all__r   r   r   r   r   r   r   r   r	   r   r   r   <module>r      s   " # " " " " " 				  
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+? ? ? ? ?d ? ? ?"? ? ? ? ?4 ? ? ? < < < < <D < < <
@ 
@ 
@ 
@ 
@ 
@ 
@ 
@d d d d dt d d d"J J J J JT J J J"2 2 2 2jW W W W W Wr   