
    X-Ph6"                    ^   U d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZ ddlmZmZmZmZmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z& e G d de                      Z'd2dZ(d3dZ) e            Z*de+d<   d3dZ,d4d'Z-d5d-Z.d4d.Z/d6d0Z0d1S )7z.Tokenizers for three string formatting methods    )annotations)Enumunique)Final)ConversionSpecifierparse_conversion_specifiersparse_format_value)Errors)MessageBuilder)Context
Expression)Options)IntegerValue)c_pyssize_t_rprimitiveis_bytes_rprimitiveis_int_rprimitiveis_short_int_rprimitiveis_str_rprimitive)	IRBuilder)bytes_build_op)int_to_str_op)str_build_opstr_opc                      e Zd ZdZdZdZdZdS )FormatOpaw  FormatOp represents conversion operations of string formatting during
    compile time.

    Compare to ConversionSpecifier, FormatOp has fewer attributes.
    For example, to mark a conversion from any object to string,
    ConversionSpecifier may have several representations, like '%s', '{}'
    or '{:{}}'. However, there would only exist one corresponding FormatOp.
    sdbN)__name__
__module____qualname____doc__STRINTBYTES     b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/mypyc/irbuild/format_str_tokenizer.pyr   r      s)          C
CEEEr(   r   
specifierslist[ConversionSpecifier]returnlist[FormatOp] | Nonec                   g }| D ]}|j         dk    s|j         dk    rt          j        }nF|j         dk    rt          j        }n.|j         dk    rt          j        }n|j         r dS t          j        }|                    |           |S )zlConvert ConversionSpecifier to FormatOp.

    Different ConversionSpecifiers may share a same FormatOp.
    z%sz{:{}}z%dz%bN)	whole_seqr   r$   r%   r&   append)r*   
format_opsspec	format_ops       r)   generate_format_opsr4   /   s    
 J % %>T!!T^w%>%> II^t## II^t## II^ 	%44 I)$$$$r(   
format_strstr'tuple[list[str], list[FormatOp]] | Nonec                   g }t          |           }t          |          }|dS d}|D ]=}|j        }|                    | ||                    |t	          |j                  z   }>|                    | |d                    ||fS )zTokenize a printf-style format string using regex.

    Return:
        A list of string literals and a list of FormatOps.
    Nr   )r   r4   	start_posr0   lenr/   )r5   literalsr*   r1   last_endr2   	cur_starts          r)   tokenizer_printf_styler>   E   s     H,G
,S,SJ$Z00JtH 3 3N	
8I#56777s4>222OOJxyy)***Zr(   r   EMPTY_CONTEXTc           
        t          | t          t          t          t	                                i                     }|dS t          |          }|dS g }d}|D ]F}|                    | ||j        dz
                      |j        t          |j	                  z   dz   }G|                    | |d                    d |D             }||fS )a	  Tokenize a str.format() format string.

    The core function parse_format_value() is shared with mypy.
    With these specifiers, we then parse the literal substrings
    of the original format string and convert `ConversionSpecifier`
    to `FormatOp`.

    Return:
        A list of string literals and a list of FormatOps. The literals
        are interleaved with FormatOps and the length of returned literals
        should be exactly one more than FormatOps.
        Return None if it cannot parse the string.
    Nr      c                b    g | ],}|                     d d                               dd          -S )z{{{z}}})replace).0xs     r)   
<listcomp>z)tokenizer_format_call.<locals>.<listcomp>   s6    JJJA		$$$,,T377JJJr(   )
r	   r?   r   r
   r   r4   r0   r9   r:   r/   )r5   r*   r1   r;   r<   r2   s         r)   tokenizer_format_callrI   `   s      $M>&2C2CR#H#H J t$Z00JtHH < <
8dnq.@#@ABBB>C$7$77!;OOJxyy)***JJJJJHZr(   builderr   r1   list[FormatOp]exprslist[Expression]lineintlist[Value] | Nonec                   t          |          t          |          k    rdS g }t          ||          D ]J\  }}|                     |          }|t          j        k    rt          |          r|                     |          }nt          |          st          |          r1| 	                    t          |                     |          g|          }n| 	                    t          |                     |          g|          }ne|t          j        k    rRt          |          st          |          r1| 	                    t          |                     |          g|          }n dS  dS |                    |           L|S )zkConvert expressions into string literal objects with the guidance
    of FormatOps. Return None when fails.N)r:   zip	node_typer   r$   r   acceptr   r   primitive_opr   r   r%   r0   )	rJ   r1   rL   rN   	convertedrG   r3   rS   var_strs	            r)   convert_format_expr_to_strrX      sz   
 :#e**$$tIE:.. " "9%%a((	$$ ++ R!..++"9-- R1H1S1S R!..}w~~a?P?P>QSWXX!..vq8I8I7JDQQ(,&& ++ /Fy/Q/Q !..}w~~a?P?P>QSWXXtt44!!!!r(   r;   list[str] | Nonesubstitutionslist[Value]r   c                   t          dt                    g}|t          ||          D ]D\  }}|r(|                    |                     |                     |                    |           E|d         r.|                    |                     |d                              n|                    |           t          |          dk    r|                     d          S |st          |          dk    r|d         S t          t          |          dz
  t                    |d<   |                     t          ||          S )ab  Merge the list of literals and the list of substitutions
    alternatively using 'str_build_op'.

    `substitutions` is the result value of formatting conversions.

    If the `literals` is set to None, we simply join the substitutions;
    Otherwise, the `literals` is the literal substrings of the original
    format string and its length should be exactly one more than
    substitutions.

    For example:
    (1)    'This is a %s and the value is %d'
        -> literals: ['This is a ', ' and the value is', '']
    (2)    '{} and the value is {}'
        -> literals: ['', ' and the value is', '']
    r   NrA       )	r   r   rR   r0   load_strextendr:   call_cr   rJ   r;   rZ   rN   result_listar   s          r)   join_formatted_stringsrf      sF   * !(+A B BCK-00 	" 	"DAq 8""7#3#3A#6#6777q!!!!B< 	?w//==>>>=))) ;1### S--221~S--13IJJKN>>,T:::r(   c                d   t          |          t          |          k    rdS g }t          ||          D ]z\  }}|                     |          }|t          j        k    s|t          j        k    r(t          |          r|                     |          }n dS  dS |                    |           {|S )zjConvert expressions into bytes literal objects with the guidance
    of FormatOps. Return None when fails.N)	r:   rR   rS   r   r&   r$   r   rT   r0   )	rJ   r1   rL   rN   rV   rG   r3   rS   	var_bytess	            r)   convert_format_expr_to_bytesri      s    
 :#e**$$tIE:.. 
$ 
$9%%a((	&&)x|*C*C"9-- #NN1--		tt44####r(   	list[str]c                V   t          dt                    g}t          ||          D ]D\  }}|r(|                    |                     |                     |                    |           E|d         r.|                    |                     |d                              t          |          dk    r|                     d          S |st          |          dk    r|d         S t          t          |          dz
  t                    |d<   |                     t          ||          S )zbMerge the list of literals and the list of substitutions
    alternatively using 'bytes_build_op'.r   r]   rA   r^   r_   )r   r   rR   r0   load_bytes_from_str_literalr:   rb   r   rc   s          r)   join_formatted_bytesrm      s-   
 !(+A B BCKHm,,  1 	GwBB1EEFFF1| N7>>x|LLMMM ;1222666 S--221~S--13IJJKN>>.+t<<<r(   N)r*   r+   r,   r-   )r5   r6   r,   r7   )
rJ   r   r1   rK   rL   rM   rN   rO   r,   rP   )
rJ   r   r;   rY   rZ   r[   rN   rO   r,   r   )
rJ   r   r;   rj   rZ   r[   rN   rO   r,   r   )1r#   
__future__r   enumr   r   typingr   mypy.checkstrformatr   r   r	   mypy.errorsr
   mypy.messagesr   
mypy.nodesr   r   mypy.optionsr   mypyc.ir.opsr   r   mypyc.ir.rtypesr   r   r   r   r   mypyc.irbuild.builderr   mypyc.primitives.bytes_opsr   mypyc.primitives.int_opsr   mypyc.primitives.str_opsr   r   r   r4   r>   r?   __annotations__rI   rX   rf   ri   rm   r'   r(   r)   <module>r}      sa   4 4 4 " " " " " "                       
       ( ( ( ( ( ( * * * * * * * *             ' ' ' ' ' ' ' '              , + + + + + 5 5 5 5 5 5 2 2 2 2 2 2 9 9 9 9 9 9 9 9     t      ,       0 wyy        #  #  #  # L   :(; (; (; (;V   .= = = = = =r(   