o
    i                     @  s   d Z ddlmZ ddlZddlmZm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)ListOptional)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   p/var/www/edux/Edux_v2/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   r2   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/   r1   r   r   r   r2   J   r3   zNodeSequence.__repr__Nr4   r   r5   )r   r   r   r   r&   r   r2   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compiler6   )r   r6   r   r   r   r&   S   s   

zRegex.__init__c                 C  s   | j j d| j dS )Nz(/z/))r0   r   r6   r1   r   r   r   r2   X   r3   zRegex.__repr__N)r6   r*   r   r"   r5   r   r   r   r   r&   r2   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   )r:   r;   )r   r:   r;   r   r   r   r&   a      
zLookahead.__init__r*   c                 C  r+   r,   r0   r   r:   r1   r   r   r   r2   e   r3   zLookahead.__repr__N)F)r:   r   r;   r<   r   r"   r5   r9   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.
     r:   r   varnamer*   r   r"   c                 C  r=   r   )r:   rA   )r   r:   rA   r   r   r   r&   r   r>   zVariable.__init__c                 C  s   d | jj| j| jS )Nz {}(childnode={!r}, varname={!r}))formatr0   r   r:   rA   r1   r   r   r   r2   v   s
   zVariable.__repr__N)r@   )r:   r   rA   r*   r   r"   r5   r9   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   NTr:   r   
min_repeatint
max_repeat
int | Nonegreedyr<   r   r"   c                 C  s   || _ || _|| _|| _d S r   )r:   rC   rE   rG   )r   r:   rC   rE   rG   r   r   r   r&      s   
zRepeat.__init__r*   c                 C  r+   )Nz(childnode=r.   r?   r1   r   r   r   r2      r3   zRepeat.__repr__)r   NT)
r:   r   rC   rD   rE   rF   rG   r<   r   r"   r5   )r   r   r   r&   r2   r   r   r   r   r   ~   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.)r7   r8   VERBOSEmatchendisspaceappend	Exception)rH   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   )rV   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< ne|dkr}  g nY|dv r  nN|dkrt dd n?|dkrt dd n0|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)rY   r   r   
<listcomp>   s    zGparse_regex.<locals>._parse.<locals>.wrapped_result.<locals>.<listcomp>)rN   r   r   )or_listresultrY   r   r   wrapped_result   s   
z3parse_regex.<locals>._parse.<locals>.wrapped_resultz(?P<   rU   )rA   )*z*?ra   )rG   )+z+?rb   rW   )rC   rG   )?z??zNothing to repeat.rc   r   )rC   rE   rG   |)r-   z(?:z(?!T)r;   z(?=Fr.   #{z#-style repetition not yet supportedz(?z%r not supportedzExpecting ')' tokenr   r   )
pop
startswithr   rN   r   rO   reprr   rM   r   )r_   tvariablerG   _parserQ   rY   )r]   r^   r   rn      sV   




7zparse_regex.<locals>._parser   zUnmatched parentheses.)rV   r!   r   r   rg   )rX   rO   )rT   r^   r   rm   r   r
      s   
Dr
   )rH   r*   r   rI   )rT   rI   r   r   )r   
__future__r   r7   typingr   r   __all__r   r   r   r   r   r   r   r	   r
   r   r   r   r   <module>   s    

5