o
    �õ±j4  ã                   @  s¦   d Z ddlmZ ddlZg d¢ZG dd„ dƒZG dd„ deƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZ	G dd„ deƒZ
G dd„ deƒZddd„Zd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                   @  s$   e Zd ZdZddd„Zddd	„Zd
S )ÚNodezT
    Base class for all the grammar nodes.
    (You don't initialize this one.)
    Ú
other_nodeÚreturnÚNodeSequencec                 C  ó   t | |gƒS ©N)r   ©Úselfr
   © r   ú¨/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/contrib/regular_languages/regex_parser.pyÚ__add__&   ó   zNode.__add__ÚAnyNodec                 C  r   r   )r   r   r   r   r   Ú__or__)   r   zNode.__or__N©r
   r	   r   r   ©r
   r	   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r	       s    
r	   c                   @  ó.   e Zd ZdZddd„Zdd
d„Zd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                 C  ó
   || _ d S r   ©r   ©r   r   r   r   r   Ú__init__4   ó   
zAnyNode.__init__r
   r	   c                 C  ó   t | j|g ƒS r   )r   r   r   r   r   r   r   7   ó   zAnyNode.__or__Ústrc                 C  ó   | j j› d| j›d�S ©Nú(ú)©Ú	__class__r   r   ©r   r   r   r   Ú__repr__:   ó   zAnyNode.__repr__N©r   r   r   r    r   ©r   r(   )r   r   r   r   r$   r   r0   r   r   r   r   r   -   s
    

r   c                   @  r   )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                 C  r!   r   r"   r#   r   r   r   r$   D   r%   zNodeSequence.__init__r
   r	   c                 C  r&   r   )r   r   r   r   r   r   r   G   r'   zNodeSequence.__add__r(   c                 C  r)   r*   r-   r/   r   r   r   r0   J   r1   zNodeSequence.__repr__Nr2   r   r3   )r   r   r   r   r$   r   r0   r   r   r   r   r   >   s
    

r   c                   @  s$   e Zd ZdZddd„Zddd	„Zd
S )r   z
    Regular expression.
    Úregexr(   r   r    c                 C  s   t  |¡ || _d S r   )ÚreÚcompiler4   )r   r4   r   r   r   r$   S   s   

zRegex.__init__c                 C  s   | j j› d| j› d�S )Nz(/z/))r.   r   r4   r/   r   r   r   r0   X   r1   zRegex.__repr__N)r4   r(   r   r    r3   ©r   r   r   r   r$   r0   r   r   r   r   r   N   s    
r   c                   @  s&   e Zd ZdZddd	d
„Zddd„ZdS )r   z
    Lookahead expression.
    FÚ	childnoder	   ÚnegativeÚboolr   r    c                 C  ó   || _ || _d S r   )r8   r9   )r   r8   r9   r   r   r   r$   a   ó   
zLookahead.__init__r(   c                 C  r)   r*   ©r.   r   r8   r/   r   r   r   r0   e   r1   zLookahead.__repr__N)F)r8   r	   r9   r:   r   r    r3   r7   r   r   r   r   r   \   s    r   c                   @  s&   e Zd ZdZddd	d
„Zd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.
    Ú r8   r	   Úvarnamer(   r   r    c                 C  r;   r   )r8   r?   )r   r8   r?   r   r   r   r$   r   r<   zVariable.__init__c                 C  s   | j j› d| j›d| j›d�S )Nú(childnode=z
, varname=r,   )r.   r   r8   r?   r/   r   r   r   r0   v   s   zVariable.__repr__N)r>   )r8   r	   r?   r(   r   r    r3   r7   r   r   r   r   r   i   s    r   c                   @  s(   e Zd Z			dddd„Zddd„ZdS )r   r   NTr8   r	   Ú
min_repeatÚintÚ
max_repeatú
int | NoneÚgreedyr:   r   r    c                 C  s   || _ || _|| _|| _d S r   )r8   rA   rC   rE   )r   r8   rA   rC   rE   r   r   r   r$   {   s   
zRepeat.__init__r(   c                 C  r)   )Nr@   r,   r=   r/   r   r   r   r0   ‡   r1   zRepeat.__repr__)r   NT)
r8   r	   rA   rB   rC   rD   rE   r:   r   r    r3   )r   r   r   r$   r0   r   r   r   r   r   z   s    ûr   Úinputr(   r   ú	list[str]c                 C  sj   t  dt j¡}g }| r3| | ¡}|r-| d| ¡ … | | ¡ d… }} | ¡ s,| |¡ ntdƒ‚| s|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.)r5   r6   ÚVERBOSEÚmatchÚendÚisspaceÚappendÚ	Exception)rF   ÚpÚtokensÚmÚtokenr   r   r   r   ‹   s   	å
"
€ù	r   Úregex_tokensc                   sN   dg| ddd…  ‰ddd	„‰d‡ ‡‡fd
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                 S  s   t | ƒdkr
| d S t| ƒS )z7Turn list into sequence when it contains several items.é   r   )Úlenr   )rT   r   r   r   ÚwrapÇ   s   zparse_regex.<locals>.wrapc                    sº  g ‰ g ‰d#‡ ‡‡fdd„} ˆrÙˆ  ¡ }| d¡r)tˆƒ |dd… d�}ˆ |¡ n®|d	v r<|d
k}tˆd |d�ˆd< n›|dv rP|dk}tˆd d|d�ˆd< n‡|dv rqˆg kr`tdtˆƒ ƒ‚|dk}tˆd dd|d�ˆd< nf|dkr}ˆ  ˆ¡ g ‰nZ|dv rˆˆ ˆƒ ¡ nO|dkr—ˆ tˆƒ dd�¡ n@|dkr¦ˆ tˆƒ dd�¡ n1|dkr­| ƒ S | d¡r³n$| d¡r¿t|› d�ƒ‚| d ¡rËt|›d!�ƒ‚| ¡ rÐnˆ t	|ƒ¡ ˆstd"ƒ‚)$Nr   r	   c                     s0   ˆ g krˆˆƒS ˆ   ˆ¡ t‡fdd„ˆ D ƒƒS )Nc                   s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0Úi)rW   r   r   Ú
<listcomp>×   s    zGparse_regex.<locals>._parse.<locals>.wrapped_result.<locals>.<listcomp>)rL   r   r   )Úor_listÚresultrW   r   r   Úwrapped_resultÒ   s   
z3parse_regex.<locals>._parse.<locals>.wrapped_resultz(?P<é   rS   )r?   )Ú*z*?r_   )rE   )ú+z+?r`   rU   )rA   rE   )ú?z??zNothing to repeat.ra   r   )rA   rC   rE   ú|)r+   z(?:z(?!T)r9   z(?=Fr,   ú#Ú{z#-style repetition not yet supportedz(?z not supportedzExpecting ')' token©r   r	   )
ÚpopÚ
startswithr   rL   r   rM   Úreprr   rK   r   )r]   ÚtÚvariablerE   ©Ú_parserO   rW   )r[   r\   r   rl   Î   sV   
ÿ



Ë7zparse_regex.<locals>._parser   zUnmatched parentheses.)rT   r   r   r	   re   )rV   rM   )rR   r\   r   rk   r   r   À   s   
Dr   )rF   r(   r   rG   )rR   rG   r   r	   )r   Ú
__future__r   r5   Ú__all__r	   r   r   r   r   r   r   r   r   r   r   r   r   Ú<module>   s    

5