U
    .en                     @  s   d Z ddlmZ ddlZdddddd	g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ddZdd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                   @  s0   e Zd ZdZd ddddZd ddddZd	S )
NodezT
    Base class for all the grammar nodes.
    (You don't initialize this one.)
    NodeSequence
other_nodereturnc                 C  s   t | |gS N)r
   selfr    r   Y/tmp/pip-unpacked-wheel-_x9ru1vw/prompt_toolkit/contrib/regular_languages/regex_parser.py__add__%   s    zNode.__add__AnyNodec                 C  s   t | |gS r   )r   r   r   r   r   __or__(   s    zNode.__or__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r	      s   r	   c                   @  s>   e Zd ZdZdddddZdd dd	d
Zd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.
    
list[Node]Nonechildrenr   c                 C  s
   || _ d S r   r   r   r   r   r   r   __init__3   s    zAnyNode.__init__r	   r   c                 C  s   t | j|g S r   )r   r   r   r   r   r   r   6   s    zAnyNode.__or__strr   c                 C  s   | j j d| jdS N()	__class__r   r   r   r   r   r   __repr__9   s    zAnyNode.__repr__N)r   r   r   r   r    r   r)   r   r   r   r   r   ,   s   r   c                   @  s>   e Zd ZdZdddddZdd dd	d
Zdd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   c                 C  s
   || _ d S r   r   r   r   r   r   r    C   s    zNodeSequence.__init__r	   r   c                 C  s   t | j|g S r   )r
   r   r   r   r   r   r   F   s    zNodeSequence.__add__r!   r"   c                 C  s   | j j d| jdS r#   r&   r(   r   r   r   r)   I   s    zNodeSequence.__repr__N)r   r   r   r   r    r   r)   r   r   r   r   r
   =   s   r
   c                   @  s.   e Zd ZdZdddddZdddd	Zd
S )r   z
    Regular expression.
    r!   r   )regexr   c                 C  s   t | || _d S r   )recompiler*   )r   r*   r   r   r   r    R   s    
zRegex.__init__r"   c                 C  s   | j j d| j dS )Nz(/z/))r'   r   r*   r(   r   r   r   r)   W   s    zRegex.__repr__Nr   r   r   r   r    r)   r   r   r   r   r   M   s   c                   @  s2   e Zd ZdZdddddddZd	d
ddZdS )r   z
    Lookahead expression.
    Fr	   boolr   )	childnodenegativer   c                 C  s   || _ || _d S r   )r/   r0   )r   r/   r0   r   r   r   r    `   s    zLookahead.__init__r!   r"   c                 C  s   | j j d| jdS r#   r'   r   r/   r(   r   r   r   r)   d   s    zLookahead.__repr__N)Fr-   r   r   r   r   r   [   s   c                   @  s2   e Zd ZdZdddddddZd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!   r   )r/   varnamer   c                 C  s   || _ || _d S r   )r/   r3   )r   r/   r3   r   r   r   r    q   s    zVariable.__init__r"   c                 C  s   d | jj| j| jS )Nz {}(childnode={!r}, varname={!r}))formatr'   r   r/   r3   r(   r   r   r   r)   u   s
    zVariable.__repr__N)r2   r-   r   r   r   r   r   h   s   c                   @  s2   e Zd Zddddddd	d
dZddddZdS )r   r   NTr	   intz
int | Noner.   r   )r/   
min_repeat
max_repeatgreedyr   c                 C  s   || _ || _|| _|| _d S r   )r/   r6   r7   r8   )r   r/   r6   r7   r8   r   r   r   r    ~   s    zRepeat.__init__r!   r"   c                 C  s   | j j d| jdS )Nz(childnode=r%   r1   r(   r   r   r   r)      s    zRepeat.__repr__)r   NT)r   r   r   r    r)   r   r   r   r   r   }   s
      r!   z	list[str])inputr   c                 C  sh   t dt j}g }| rd|| }|rZ| d|  | | d  }} | sb|| qtdq|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)r9   ptokensmtokenr   r   r   r      s    	
"
)regex_tokensr   c                   s^   dg| ddd  ddddddd	 fd
d   }t dkrVtdn|S dS )zN
    Takes a list of tokens from the tokenizer, and returns a parse tree.
    r%   Nr   r	   )lstr   c                 S  s    t | dkr| d S t| S dS )z7Turn list into sequence when it contains several items.   r   N)lenr
   )rF   r   r   r   wrap   s    zparse_regex.<locals>.wrapr"   c                    s  g  g dd fdd} r̈  }|drXt |dd d}| q|d	kr~|d
k}td |dd< q|dkr|dk}td d|dd< q|dkrg krtdt n |dk}td dd|dd< q|dkr  g q|dkr  q|dkr<t dd q|dkr\t dd q|dkrl|  S |drzq|drt| dq|d rtd!| q| rqt	| qtd"d S )#Nr	   r"   c                     s4    g krS    tfdd D S d S )Nc                   s   g | ]} |qS r   r   ).0i)rI   r   r   
<listcomp>   s     zGparse_regex.<locals>._parse.<locals>.wrapped_result.<locals>.<listcomp>)r>   r   r   )or_listresultrI   r   r   wrapped_result   s    
z3parse_regex.<locals>._parse.<locals>.wrapped_resultz(?P<   rE   )r3   )*z*?rQ   )r8   )+z+?rR   rG   )r6   r8   )?z??zNothing to repeat.rS   r   )r6   r7   r8   |)r$   z(?:z(?!T)r0   z(?=Fr%   #{z#-style repetition not yet supportedz(?z%r not supportedzExpecting ')' token)
pop
startswithr   r>   r   r?   reprr   r=   r   )rO   tvariabler8   _parserA   rI   )rM   rN   r   r]      sZ    
   






zparse_regex.<locals>._parser   zUnmatched parentheses.)rH   r?   )rD   rN   r   r\   r   r      s    D
)r   
__future__r   r+   __all__r	   r   r
   r   r   r   r   r   r   r   r   r   r   <module>   s$   
5