o
    ˷e{                     @   s   d 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edee fddZdee def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.
    N)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   l/var/www/ideatree/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                   @   F   e Zd ZdZdee ddfddZdedd fdd	Zdef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.
    childrenr   Nc                 C   
   || _ d S r   r   r   r   r   r   r   __init__2      
zAnyNode.__init__r   c                 C      t | j|g S r   )r   r   r   r   r   r   r   5      zAnyNode.__or__c                 C      | j j d| jdS N()	__class__r   r   r   r   r   r   __repr__8      zAnyNode.__repr__)
r   r   r   r   r   r
   r!   r   strr,   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   Nc                 C   r   r   r   r    r   r   r   r!   B   r"   zNodeSequence.__init__r   c                 C   r#   r   )r   r   r   r   r   r   r   E   r$   zNodeSequence.__add__c                 C   r%   r&   r)   r+   r   r   r   r,   H   r-   zNodeSequence.__repr__)
r   r   r   r   r   r
   r!   r   r.   r,   r   r   r   r   r   <   s
    r   c                   @   s0   e Zd ZdZdeddfddZdefddZdS )	r   z
    Regular expression.
    regexr   Nc                 C   s   t | || _d S r   )recompiler/   )r   r/   r   r   r   r!   Q   s   

zRegex.__init__c                 C   s   | j j d| j dS )Nz(/z/))r*   r   r/   r+   r   r   r   r,   V   r-   zRegex.__repr__)r   r   r   r   r.   r!   r,   r   r   r   r   r   L   s    r   c                   @   s6   e Zd ZdZddededdfddZdefd	d
ZdS )r   z
    Lookahead expression.
    F	childnodenegativer   Nc                 C      || _ || _d S r   )r2   r3   )r   r2   r3   r   r   r   r!   _      
zLookahead.__init__c                 C   r%   r&   r*   r   r2   r+   r   r   r   r,   c   r-   zLookahead.__repr__)F)	r   r   r   r   r
   boolr!   r.   r,   r   r   r   r   r   Z   s    r   c                   @   s6   e Zd ZdZddededdfddZdef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.
     r2   varnamer   Nc                 C   r4   r   )r2   r9   )r   r2   r9   r   r   r   r!   p   r5   zVariable.__init__c                 C   s   d | jj| j| jS )Nz {}(childnode={!r}, varname={!r}))formatr*   r   r2   r9   r+   r   r   r   r,   t   s
   zVariable.__repr__)r8   )r   r   r   r   r
   r.   r!   r,   r   r   r   r   r   g   s    r   c                   @   sD   e Zd Z			ddededee deddf
d	d
ZdefddZ	dS )r   r   NTr2   
min_repeat
max_repeatgreedyr   c                 C   s   || _ || _|| _|| _d S r   )r2   r;   r<   r=   )r   r2   r;   r<   r=   r   r   r   r!   }   s   
zRepeat.__init__c                 C   r%   )Nz(childnode=r(   r6   r+   r   r   r   r,      r-   zRepeat.__repr__)r   NT)
r   r   r   r
   intr   r7   r!   r.   r,   r   r   r   r   r   |   s     
r   inputr   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.)r0   r1   VERBOSEmatchendisspaceappend	Exception)r?   ptokensmtokenr   r   r   r      s   	
"
	r   regex_tokensc                    s^   dg| ddd  dt t dtfdddtf 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   c                 S   s   t | dkr
| d S t| S )z7Turn list into sequence when it contains several items.   r   )lenr   )rL   r   r   r   wrap   s   zparse_regex.<locals>.wrapc                     s  g  g dt f fdd} rڈ }|dr+t |dd d}| n|dv r>|d	k}td |d
d< n|dv rR|dk}td d|dd< n|dv rsg krbt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   c                      s0    g krS    tfdd D S )Nc                    s   g | ]} |qS r   r   ).0i)rO   r   r   
<listcomp>   s    zGparse_regex.<locals>._parse.<locals>.wrapped_result.<locals>.<listcomp>)rD   r   r   )or_listresultrO   r   r   wrapped_result   s   
z3parse_regex.<locals>._parse.<locals>.wrapped_resultz(?P<   rK   )r9   )*z*?rW   )r=   )+z+?rX   rM   )r;   r=   )?z??zNothing to repeat.rY   r   )r;   r<   r=   |)r'   z(?:z(?!T)r3   z(?=Fr(   #{z#-style repetition not yet supportedz(?z%r not supportedzExpecting ')' token)r
   pop
startswithr   rD   r   rE   reprr   rC   r   )rU   tvariabler=   _parserG   rO   )rS   rT   r   rc      sV   




7zparse_regex.<locals>._parser   zUnmatched parentheses.)r   r
   rN   rE   )rJ   rT   r   rb   r   r	      s   Dr	   )r   r0   typingr   r   __all__r
   r   r   r   r   r   r   r.   r   r	   r   r   r   r   <module>   s    
5