
    MhU                    F   d Z ddlmZ ddlZddlmZmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZ g dZd	Zeeeegef         f         Z G d
 d          Z G d d          Z e
d          Z G d d          Z G d d          Z	 	 dddZ 	 	 dddZ!dS ) a  
Compiler for a regular grammar.

Example usage::

    # Create and compile grammar.
    p = compile('add \s+ (?P<var1>[^\s]+)  \s+  (?P<var2>[^\s]+)')

    # Match input string.
    m = p.match('add 23 432')

    # Get variables.
    m.variables().get('var1')  # Returns "23"
    m.variables().get('var2')  # Returns "432"


Partial matches are possible::

    # Create and compile grammar.
    p = compile('''
        # Operators with two arguments.
        ((?P<operator1>[^\s]+)  \s+ (?P<var1>[^\s]+)  \s+  (?P<var2>[^\s]+)) |

        # Operators with only one arguments.
        ((?P<operator2>[^\s]+)  \s+ (?P<var1>[^\s]+))
    ''')

    # Match partial input string.
    m = p.match_prefix('add 23')

    # Get variables. (Notice that both operator1 and operator2 contain the
    # value "add".) This is because our input is incomplete, and we don't know
    # yet in which rule of the regex we we'll end up. It could also be that
    # `operator1` and `operator2` have a different autocompleter and we want to
    # call all possible autocompleters that would result in valid input.)
    m.variables().get('var1')  # Returns "23"
    m.variables().get('operator1')  # Returns "add"
    m.variables().get('operator2')  # Returns "add"

    )annotationsN)CallableDictIterableIteratorPatternTypeVaroverload)Match   )	AnyNode	LookaheadNodeNodeSequenceRegexRepeatVariableparse_regextokenize_regex)compiler   	Variablesinvalid_trailingc                  p    e Zd ZdZ	 	 ddd
ZddZddZedd            Zedd            Z	ddZ
ddZdS )_CompiledGrammaraI  
    Compiles a grammar. This will take the parse tree of a regular expression
    and compile the grammar.

    :param root_node: :class~`.regex_parser.Node` instance.
    :param escape_funcs: `dict` mapping variable names to escape callables.
    :param unescape_funcs: `dict` mapping variable names to unescape callables.
    N	root_noder   escape_funcsEscapeFuncDict | Noneunescape_funcsreturnNonec                    | _         |pi  _        |pi  _        i  _        dgd fd}d                     ||           d _        t                               ||                     _        t          j
        t          j         j                   _        fd	 j        D              _        fd
 j        D              _        d S )Nr   noder   r   strc                \    dd          }| j         j        |<   dxx         dz  cc<   |S )Nnr   r   )varname_group_names_to_nodes)r"   namecounterselfs     q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prompt_toolkit/contrib/regular_languages/compiler.pycreate_group_funcz4_CompiledGrammar.__init__.<locals>.create_group_funca   s=    #wqz##D/3|D&t,AJJJ!OJJJK    ^$c                :    g | ]}t          j        |          S  )rer   .0tflagss     r+   
<listcomp>z-_CompiledGrammar.__init__.<locals>.<listcomp>q   s%    RRRA2:a//RRRr-   c           	         g | ]C}t          j        d                     |                    d          t                              DS )z(?:{})(?P<{}>.*?)$r/   )r2   r   formatrstrip_INVALID_TRAILING_INPUTr3   s     r+   r7   z-_CompiledGrammar.__init__.<locals>.<listcomp>v   sX     /
 /
 /

 	 J%,,QXXc]]<STT /
 /
 /
r-   )r"   r   r   r#   )r   r   r   r'   
_transform_re_patternlist_transform_prefix_re_prefix_patternsr2   DOTALLr   _re
_re_prefix_re_prefix_with_trailing_input)r*   r   r   r   r,   r)   r6   s   `    @@r+   __init__z_CompiledGrammar.__init__Q   s    #(.B,2
  	" #	 	 	 	 	 	 	 Pty:KLLOOO#'""9.?@@$
 $
 
 	:d.66RRRR9QRRR
/
 /
 /
 /

 -/
 /
 /
+++r-   r&   r#   valuec                T    | j                             |          }|r ||          n|S )zW
        Escape `value` to fit in the place of this variable into the grammar.
        )r   getr*   r&   rF   fs       r+   escapez_CompiledGrammar.escape~   s1     !!'**'qqxxx%'r-   c                T    | j                             |          }|r ||          n|S )z#
        Unescape `value`.
        )r   rH   rI   s       r+   unescapez_CompiledGrammar.unescape   s1     ##G,,'qqxxx%'r-   r,   Callable[[Variable], str]c                *    dfd |          S )a  
        Turn a :class:`Node` object into a regular expression.

        :param root_node: The :class:`Node` instance for which we generate the grammar.
        :param create_group_func: A callable which takes a `Node` and returns the next
            free name for this node.
        r"   r   r   r#   c                b   t          | t                    r9d                    d                    fd| j        D                                 S t          | t
                    r&d                    fd| j        D                       S t          | t                    r| j        S t          | t                    r!| j	        rdnd}| | j
                  z   dz   S t          | t                    r d	 |            d
 | j
                   dS t          | t                    rx| j        | j        dk    rd}n5| j        dk    rd}n'd| j        | j        dnt          | j                  fz  }d                     | j
                  || j        rdnd          S t#          d|           )Nz(?:{})|c              3  .   K   | ]} |          V  d S Nr1   r4   c	transforms     r+   	<genexpr>zA_CompiledGrammar._transform.<locals>.transform.<locals>.<genexpr>   s+      /T/T		!/T/T/T/T/T/Tr-    c              3  .   K   | ]} |          V  d S rS   r1   rT   s     r+   rW   zA_CompiledGrammar._transform.<locals>.transform.<locals>.<genexpr>   s+      CCyy||CCCCCCr-   (?!z(=)(?P<>r   *r   +z{%i,%s}z
(?:{}){}{}?Got )
isinstancer   r9   joinchildrenr   r   regexr   negative	childnoder   r   
max_repeat
min_repeatr#   greedy	TypeError)r"   beforerepeat_signr,   rV   s      r+   rV   z._CompiledGrammar._transform.<locals>.transform   s   $(( &1sxx/T/T/T/Tdm/T/T/T'T'TUUU D,// "1wwCCCCT]CCCCCC D%(( 1z!D),, 1"&-9T		$. 9 99C?? D(++ 1T//55TT		$.8Q8QTTTT D&)) 1?*!++&)A--&)"+#6C<P<P/ #K
 $**Idn--;/RRC    t000r-   )r"   r   r   r#   r1   )clsr   r,   rV   s     `@r+   r<   z_CompiledGrammar._transform   s:    (	1 (	1 (	1 (	1 (	1 (	1 (	1T y###r-   Iterable[str]c              #  \    K   dfdd fd |          D ]
}d| d	V  d
S )a}  
        Yield all the regular expressions matching a prefix of the grammar
        defined by the `Node` instance.

        For each `Variable`, one regex pattern will be generated, with this
        named group at the end. This is required because a regex engine will
        terminate once a match is found. For autocompletion however, we need
        the matches for all possible paths, so that we can provide completions
        for each `Variable`.

        - So, in the case of an `Any` (`A|B|C)', we generate a pattern for each
          clause. This is one for `A`, one for `B` and one for `C`. Unless some
          groups don't contain a `Variable`, then these can be merged together.
        - In the case of a `NodeSequence` (`ABC`), we generate a pattern for
          each prefix that ends with a variable, and one pattern for the whole
          sequence. So, that's one for `A`, one for `AB` and one for `ABC`.

        :param root_node: The :class:`Node` instance for which we generate the grammar.
        :param create_group_func: A callable which takes a `Node` and returns the next
            free name for this node.
        r"   r   r   boolc                4   t          | t                    rdS t          | t                    rdS t          | t          t          f          r | j                  S t          | t          t          f          r t          fd| j	        D                       S dS )NFTc              3  .   K   | ]} |          V  d S rS   r1   )r4   childcontains_variables     r+   rW   zP_CompiledGrammar._transform_prefix.<locals>.contains_variable.<locals>.<genexpr>   s/      OO,,U33OOOOOOr-   )
rb   r   r   r   r   rg   r   r   anyrd   )r"   ru   s    r+   ru   z=_CompiledGrammar._transform_prefix.<locals>.contains_variable   s    $&& PuD(++ PtD9f"566 P((888D<"9:: POOOOOOOOOO5r-   ro   c              3    K   t          | t                    rg }g }| j        D ]8} |          r|                    |           #|                    |           9|D ]} |          E d {V  |r%d                    fd|D                       V  d S d S t          | t
                    rfd| j        D             }fd| j        D             }fd| j        D             }t          t          | j                            D ]7}||         r-||         D ]$}d                    |d |                   |z   V  %8t          |          sg }	t          t          | j                            D ]2}|	                    d           |	                    ||                    3t          t          | j                  dz
  d	d	          D ]}||         r|	                    d
            |	                    d           t          ||                   dk    sJ |	                    ||         d                    |	                    d           d                    |	          V  d S d S t          | t                    rd| j
         dV  d S t          | t                    r9| j        r#d                    | j                   d
V  d S t          d          t          | t                     r) | j                  D ]}d |            d| d
V  d S t          | t"                    r                    | j                  }
| j        dk    r | j                  E d {V  d S  | j                  D ]<}| j        rd| j        dz
  z  }nd}d                    |
|| j        rdnd|          V  =d S t+          d|           )NrQ   c              3  8   K   | ]} |          D ]}|V  d S rS   r1   )r4   rU   rrV   s      r+   rW   zH_CompiledGrammar._transform_prefix.<locals>.transform.<locals>.<genexpr>  sQ       # #iiPQll# #AB# # # # # # #r-   c                <    g | ]}                     |          S r1   )r<   )r4   rU   rn   r,   s     r+   r7   zI_CompiledGrammar._transform_prefix.<locals>.transform.<locals>.<listcomp>  s(    XXXQCNN1.?@@XXXr-   c                @    g | ]}t           |                    S r1   r>   rT   s     r+   r7   zI_CompiledGrammar._transform_prefix.<locals>.transform.<locals>.<listcomp>  s)    FFF1D1..FFFr-   c                &    g | ]} |          S r1   r1   )r4   rU   ru   s     r+   r7   zI_CompiledGrammar._transform_prefix.<locals>.transform.<locals>.<listcomp>  s%    !N!N!N1"3"3A"6"6!N!N!Nr-   rX   z(?:r   r[   z|(?:r   z))z)?rZ   z%Positive lookahead not yet supported.r\   r]   z{,%i}r^   z(?:{}){}{}{}r`   ra   )rb   r   rd   appendrc   r   rangelenallr   re   r   rf   r<   rg   	Exceptionr   r   rh   r9   rj   rk   )r"   children_with_variablechildren_without_variablerU   completeprefixesvariable_nodesic_strresultprefixrm   rn   ru   r,   rV   s               r+   rV   z5_CompiledGrammar._transform_prefix.<locals>.transform   s       $(( q1 *,&,.) < <A((++ <.55a8888188;;;;/ , ,A(y||++++++++ - (( # # # ##<# # #         D,// T1 YXXXX$-XXXFFFFFFF!N!N!N!N!N!N!N
 s4=1122 @ @A%a( @%-a[ @ @E"$''(2A2,"7"7%"????? >** *F #3t}#5#566 3 3e,,,hqk2222 #3t}#5#5#92rBB 
0 
0)!, 	0 #MM#...."MM&111#&x{#3#3q#8#8#8#8"MM(1+a.999"MM$////''&//)))))+* *. D%(( '1*DJ********D),, $1= MTt~?P Q QTTTTTTTT $$KLLLD(++ 1 'Yt~66 D DEC!2!24!8!8CC5CCCCCCCD D D&)) 1 8IJJ?a''(y88888888888!*4>!:!: 
 
? .*1T_q5H*IKK*-K,33"'#';7RRC!	     
 
  t000r-   z^(?:z)$N)r"   r   r   rq   )r"   r   r   ro   r1   )rn   r   r,   ry   ru   rV   s   ` ` @@r+   r?   z"_CompiledGrammar._transform_prefix   s      4
	 
	 
	 
	 
	 
	u	1 u	1 u	1 u	1 u	1 u	1 u	1 u	1 u	1n 9%% 	 	A,,,	 	r-   stringMatch | Nonec                    | j                             |          }|r$t          || j         |fg| j        | j                  S dS )z
        Match the string with the grammar.
        Returns a :class:`Match` instance or `None` when the input doesn't match the grammar.

        :param string: The input string.
        N)rB   matchr   r'   r   )r*   r   ms      r+   r   z_CompiledGrammar.matchd  sO     HNN6"" 	$(A)CTEX   tr-   c                    | j         | j        fD ]@}fd|D             }d |D             }|g k    rt          || j        | j                  c S AdS )aR  
        Do a partial match of the string with the grammar. The returned
        :class:`Match` instance can contain multiple representations of the
        match. This will never return `None`. If it doesn't match at all, the "trailing input"
        part will capture all of the input.

        :param string: The input string.
        c                >    g | ]}||                               fS r1   )r   )r4   ry   r   s     r+   r7   z1_CompiledGrammar.match_prefix.<locals>.<listcomp>  s(    >>>1776??+>>>r-   c                     g | ]\  }}|||fS r1   r1   )r4   ry   r   s      r+   r7   z1_CompiledGrammar.match_prefix.<locals>.<listcomp>  s%    88841aa8A888r-   N)rC   rD   r   r'   r   )r*   r   patternsmatchesmatches2s    `   r+   match_prefixz_CompiledGrammar.match_prefixs  s     $*MN 	 	H>>>>X>>>G887888H2~~Hd&@$BU     
 tr-   NN)r   r   r   r   r   r   r   r    r&   r#   rF   r#   r   r#   )r   r   r,   rN   r   r#   )r   r   r,   rN   r   ro   )r   r#   r   r   )__name__
__module____qualname____doc__rE   rK   rM   classmethodr<   r?   r   r   r1   r-   r+   r   r   G   s          /304	+
 +
 +
 +
 +
Z( ( ( (( ( ( ( 5$ 5$ 5$ [5$n ] ] ] []~        r-   r   c                  J    e Zd ZdZdd
ZddZddZddZddZddZ	d dZ
dS )!r   z
    :param string: The input string.
    :param re_matches: List of (compiled_re_pattern, re_match) tuples.
    :param group_names_to_nodes: Dictionary mapping all the re group names to the matching Node instances.
    r   r#   
re_matches*list[tuple[Pattern[str], RegexMatch[str]]]group_names_to_nodesdict[str, str]r   dict[str, Callable[[str], str]]c                >    || _         || _        || _        || _        d S rS   )r   _re_matchesr'   _unescape_funcs)r*   r   r   r   r   s        r+   rE   zMatch.__init__  s)     %%9"-r-   r   !list[tuple[str, tuple[int, int]]]c                >     d fd}t           |                      S )z9
        Return a list of (varname, reg) tuples.
        r   %Iterable[tuple[str, tuple[int, int]]]c               3     K   j         D ]Q\  } }| j                                        D ]2\  }}|t          k    r"|j        }||         }j        |         }||fV  3Rd S rS   )r   
groupindexitemsr;   regsr'   )ry   re_match
group_namegroup_indexr   regr"   r*   s          r+   
get_tuplesz(Match._nodes_to_regs.<locals>.get_tuples  s      #/ * *8/0|/A/A/C/C * *+J!%<<<'}";/#9*E#Sk)))** *r-   )r   r   r|   )r*   r   s   ` r+   _nodes_to_regszMatch._nodes_to_regs  s7    
	* 	* 	* 	* 	* 	* JJLL!!!r-   &list[tuple[str, str, tuple[int, int]]]c                ^     d	dd
 fdfd                                  D             S )z>
        Returns list of (Node, string_value) tuples.
        sltuple[int, int]r   rq   c                2    | d         dk    o| d         dk    S )Nr   r~   r   r1   )r   s    r+   is_nonez'Match._nodes_to_values.<locals>.is_none  s    a5B;.2a5B;.r-   r#   c                :    j         | d         | d                  S Nr   r   )r   )r   r*   s    r+   rH   z#Match._nodes_to_values.<locals>.get  s    ;r!ur!u}--r-   c                H    g | ]\  }} |          | |          |fS r1   r1   )r4   r&   slicerH   r   s      r+   r7   z*Match._nodes_to_values.<locals>.<listcomp>  sJ     
 
 
75>>
cc%jj%(
 
 
r-   )r   r   r   rq   )r   r   r   r#   )r   )r*   rH   r   s   `@@r+   _nodes_to_valueszMatch._nodes_to_values  ss    
	/ 	/ 	/ 	/	. 	. 	. 	. 	. 	.
 
 
 
 
"&"5"5"7"7
 
 
 	
r-   r&   rF   c                T    | j                             |          }|r ||          n|S rS   )r   rH   )r*   r&   rF   	unwrappers       r+   	_unescapezMatch._unescape  s2    (,,W55	#,7yy%7r-   r   c                ^     t           fd                                 D                       S )z6
        Returns :class:`Variables` instance.
        c                J    g | ]\  }}}|                     ||          |f S r1   )r   )r4   kvr   r*   s       r+   r7   z#Match.variables.<locals>.<listcomp>  s4    SSSxq!Ra1%%r*SSSr-   )r   r   r*   s   `r+   	variableszMatch.variables  s:     SSSS4;P;P;R;RSSS
 
 	
r-   MatchVariable | Nonec                x   g }| j         D ]O\  }}|j                                        D ]0\  }}|t          k    r |                    |j        |                    1P|r^t          d |D                       t          d |D                       f}| j        |d         |d                  }t          d||          S dS )a  
        Get the `MatchVariable` instance, representing trailing input, if there is any.
        "Trailing input" is input at the end that does not match the grammar anymore, but
        when this is removed from the end of the input, the input would be a valid string.
        c              3  &   K   | ]}|d          V  dS )r   Nr1   r4   r   s     r+   rW   z'Match.trailing_input.<locals>.<genexpr>  s&      ..!1......r-   c              3  &   K   | ]}|d          V  dS )r   Nr1   r   s     r+   rW   z'Match.trailing_input.<locals>.<genexpr>  s&      3I3IQAaD3I3I3I3I3I3Ir-   r   r   z<trailing_input>N)	r   r   r   r;   r   r   maxr   MatchVariable)r*   slicesry   r   r   r   r   rF   s           r+   trailing_inputzMatch.trailing_input  s     )+  + 	> 	>KAx+,<+=+=+?+? > >'
K!888MM(-"<===>  	C..v.....3I3I&3I3I3I0I0IJEKa58 34E !3UEBBBtr-   Iterable[MatchVariable]c              #    K   |                                  D ]s\  }}|d         t          | j                  k    rP|                     || j        |d         |d                            }t	          |||d         |d         f          V  tdS )z
        Yields `MatchVariable` instances for all the nodes having their end
        position at the end of the input string.
        r   r   N)r   r   r   r   r   )r*   r&   r   rF   s       r+   	end_nodeszMatch.end_nodes  s      
 !//11 	F 	FLGS1vT[))))wCFSVO0LMM#GUSVSV4DEEEEE		F 	Fr-   N)r   r#   r   r   r   r   r   r   )r   r   )r   r   r   )r   r   )r   r   )r   r   )r   r   r   r   rE   r   r   r   r   r   r   r1   r-   r+   r   r     s         
. 
. 
. 
." " " " 
 
 
 
"8 8 8 8
 
 
 
   ,	F 	F 	F 	F 	F 	Fr-   r   _Tc                  p    e Zd ZddZddZedd
            Zedd            ZdddZddZddZddZ	dS )r   tuplesr   r   r    c                    || _         d S rS   _tuples)r*   r   s     r+   rE   zVariables.__init__  s    r-   r#   c                    d                     | j        j        d                    d | j        D                                 S )Nz{}({}), c              3  ,   K   | ]\  }}}| d |V  dS )=Nr1   )r4   r   r   _s       r+   rW   z%Variables.__repr__.<locals>.<genexpr>  s2      ??wq!QllQll??????r-   )r9   	__class__r   rc   r   r   s    r+   __repr__zVariables.__repr__  s@    N#II??$,?????
 
 	
r-   key
str | Nonec                    d S rS   r1   r*   r   s     r+   rH   zVariables.get  s    +.3r-   defaultstr | _Tc                    d S rS   r1   )r*   r   r   s      r+   rH   zVariables.get  s    <?Cr-   Nstr | _T | Nonec                D    |                      |          }|r|d         n|S )Nr   )getall)r*   r   r   r   s       r+   rH   zVariables.get   s&    C   -uQxxg-r-   	list[str]c                *    fd| j         D             S )Nc                (    g | ]\  }}}|k    |S r1   r1   )r4   r   r   r   r   s       r+   r7   z$Variables.getall.<locals>.<listcomp>  s$    :::gaAcr-   r   r   s    `r+   r   zVariables.getall  s    ::::::::r-   c                ,    |                      |          S rS   )rH   r   s     r+   __getitem__zVariables.__getitem__  s    xx}}r-   Iterator[MatchVariable]c              #  L   K   | j         D ]\  }}}t          |||          V  dS )z2
        Yield `MatchVariable` instances.
        N)r   r   r*   r&   rF   r   s       r+   __iter__zVariables.__iter__
  sE       &*\ 	7 	7!GUE666666	7 	7r-   )r   r   r   r    r   r#   )r   r#   r   r   )r   r#   r   r   r   r   rS   )r   r#   r   r   r   r   )r   r#   r   r   )r   r   )
r   r   r   rE   r   r
   rH   r   r   r   r1   r-   r+   r   r     s           
 
 
 
 ... X.??? X?. . . . .; ; ; ;   7 7 7 7 7 7r-   r   c                  "    e Zd ZdZdd	Zdd
ZdS )r   a  
    Represents a match of a variable in the grammar.

    :param varname: (string) Name of the variable.
    :param value: (string) Value of this variable.
    :param slice: (start, stop) tuple, indicating the position of this variable
                  in the input string.
    r&   r#   rF   r   r   r   r    c                x    || _         || _        || _        | j        d         | _        | j        d         | _        d S r   )r&   rF   r   startstopr   s       r+   rE   zMatchVariable.__init__  s5    

Z]
JqM			r-   c                @    | j         j         d| j        d| j        dS )N(r   r[   )r   r   r&   rF   r   s    r+   r   zMatchVariable.__repr__$  s*    .)MMDLMMdjMMMMr-   N)r&   r#   rF   r#   r   r   r   r    r   )r   r   r   r   rE   r   r1   r-   r+   r   r     sL         " " " "N N N N N Nr-   r   
expressionr#   r   r   r   r   c                Z    t          t          t          |                     ||          S )z^
    Compile grammar (given as regex string), returning a `CompiledGrammar`
    instance.
    r   r   )_compile_from_parse_treer   r   )r   r   r   s      r+   r   r   (  s4     $N:..//!%   r-   r   r   c                &    t          | ||          S )z\
    Compile grammar (given as parse tree), returning a `CompiledGrammar`
    instance.
    r   )r   )r   r   r   s      r+   r   r   8  s"     ^   r-   r   )r   r#   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )"r   
__future__r   r2   typingr   r   r   r   r   r	   r
   r   
RegexMatchregex_parserr   r   r   r   r   r   r   r   r   __all__r;   r#   EscapeFuncDictr   r   r   r   r   r   r1   r-   r+   <module>r     s0  ' 'R # " " " " " 				 Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q & & & & & &
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 ,
+
+ - c8SE3J//0@ @ @ @ @ @ @ @F
_F _F _F _F _F _F _F _FD WT]] 7  7  7  7  7  7  7  7FN N N N N N N N0 +/,0    $ +/,0      r-   