o
    f4                     @  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   h/var/www/html/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rn$|drt| d|d rt|d!| rn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