U
    \g%iÁ?  ã                   @   sN   d Z ddlZddlZddlmZ ddlmZ ddlmZ i ZG dd„ dƒZ	dS )zGprovides the Lexer class for parsing template strings into parse trees.é    N)Ú
exceptions)Ú	parsetree)Úadjust_whitespacec                   @   s¦   e Zd Zd%dd„Zedd„ ƒZd&dd„Zdd	„ Zd
d„ Zdd„ Z	e
 d¡Zdd„ Zdd„ Zdd„ Zd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 )'ÚLexerNc                 C   sx   || _ || _t | j¡| _d| _d| _d| _d| _g | _	g | _
g | _|| _|d krZg | _nt|dƒsn|g| _n|| _d S )Né   r   Ú__iter__)ÚtextÚfilenamer   ZTemplateNodeÚtemplateÚmatched_linenoÚmatched_charposÚlinenoÚmatch_positionÚtagÚcontrol_lineÚternary_stackÚencodingÚpreprocessorÚhasattr)Úselfr   r	   Zinput_encodingr   © r   úI/var/www/html/api-medvista/venv/lib/python3.8/site-packages/mako/lexer.pyÚ__init__   s     

zLexer.__init__c                 C   s   | j | j| j| jdœS )N)Úsourcer   Úposr	   )r   r   r   r	   )r   r   r   r   Úexception_kwargs*   s
    üzLexer.exception_kwargsc                 C   sV   zt ||f }W n: tk
rJ   |r0t ||¡nt |¡}|t ||f< Y nX |  |¡S )z>compile the given regexp, cache the reg, and call match_reg().)Ú_regexp_cacheÚKeyErrorÚreÚcompileÚ	match_reg)r   ÚregexpÚflagsÚregr   r   r   Úmatch3   s    zLexer.matchc                 C   s¤   | j }| | j| j ¡}|r | ¡ \}}||kr6|d n|| _ | j| _|d }|dkrv|| jk rv| jd|d …  d¡}|| | _|  j| j|| j …  	d¡7  _|S )z match the given regular expression object to the current text
        position.

        if a match occurs, update the current text and line position.

        r   r   NÚ
)
r   r$   r   Úspanr   r   Ú
textlengthÚrfindr   Úcount)r   r#   Úmpr$   ÚstartÚendÚcpr   r   r   r    >   s    
 zLexer.match_regc           	      G   sL  | j }d |¡}d}d}d}|  d¡}|r,q|  dtj¡}|r@q|  d| ¡}|r–|rn|dks–|dks–|dks–| j|| j t| d¡ƒ … | d¡fS |s¬|  d| tj¡}|r,|| d¡ d¡7 }|| d¡ d	¡8 }|| d¡ d
¡7 }|| d¡ d¡8 }|| d¡ d¡7 }|| d¡ d¡8 }qt	j
dd |¡ f| jŽ‚qd S )Nú|r   z#.*\nz+(\"\"\"|\'\'\'|\"|\')[^\\]*?(\\.[^\\]*?)*\1z(%s)r   z(.*?)(?=\"|\'|#|%s)Ú{Ú}ú(ú)ú[ú]zExpected: %sú,)r   Újoinr$   r   ÚSr   ÚlenÚgroupr)   r   ÚSyntaxExceptionr   )	r   Zwatch_nestingr   ÚstartposZtext_reZbrace_levelZparen_levelZbracket_levelr$   r   r   r   Úparse_until_textT   sZ    

 ÿÿþþþ ÿüÿÿzLexer.parse_until_textc                 O   s²  |  d| j¡ |  d| j¡ |  d| j¡ | j|d< |||Ž}t| jƒr\| jd j |¡ n| j	j |¡ | j
rÄ| j
d }|j |¡ t|tjƒrž| |j¡sÄ| jrÄ| jd rÄ| jd d j |¡ t|tjƒrôt| jƒræ| jd |_| j |¡ nºt|tjƒr®|jr | j
 ¡  | j ¡  nŽ|jrB| j
 |¡ | j g ¡ nl| j
rp| j
d  |j¡rp| jd  |¡ n>| j
r®| j
d  |j¡s®tjd|j| j
d jf f| jŽ‚d S )Nr   r   r   r	   éÿÿÿÿz1Keyword '%s' not a legal ternary for keyword '%s')Ú
setdefaultr   r   r   r	   r8   r   ZnodesÚappendr
   r   Ú
isinstancer   ÚControlLineZ
is_ternaryÚkeywordr   ÚTagÚparentÚisendÚpopZ
is_primaryr   r:   r   )r   ZnodeclsÚargsÚkwargsÚnodeZcontrol_framer   r   r   Úappend_node|   sZ    




þ
ýûú

ÿÿÿÿýzLexer.append_nodez #.*coding[:=]\s*([-\w.]+).*\r?\nc              
   C   s"  t |tƒr4| j |¡}|r$| d¡p*|p*d}||fS | tj¡r¦|ttjƒd… }d}| j | 	dd¡¡}|dk	rÐ| d¡dkrÐt
 d| d¡ | 	dd¡dd|¡‚n*| j | 	dd¡¡}|rÈ| d¡n|pÎd}|rz| 	|¡}W n4 tk
r   t
 d| | 	dd¡dd|¡‚Y nX ||fS )z¡given string/unicode or bytes/string, determine encoding
        from magic encoding comment, return body as unicode
        or raw if decode_raw=False

        r   zutf-8NÚignorezHFound utf-8 BOM in file, with conflicting magic encoding comment of '%s'r   z0Unicode decode operation of encoding '%s' failed)r@   ÚstrÚ
_coding_rer$   r9   Ú
startswithÚcodecsÚBOM_UTF8r8   Údecoder   ZCompileExceptionÚUnicodeDecodeError)r   r   Z
decode_rawZknown_encodingr	   Úmr   Zparsed_encodingr   r   r   Údecode_raw_stream°   sD    
ÿ
ú	ÿ
ú
	zLexer.decode_raw_streamc                 C   s:  |   | jd| j| j¡\| _| _| jD ]}|| jƒ| _q$|  | j¡ t| jƒ| _| j	| jkr\qÐ|  
¡ rfqÐ|  ¡ rpqN|  ¡ rzqN|  ¡ r„qN|  ¡ rŽqN|  ¡ r˜qN|  ¡ r¢qN|  ¡ r¬qN|  ¡ r¶qN| j	| jkrÄqÐt d¡‚qNt| jƒrötjd| jd j f| jŽ‚t| jƒr4t d| jd j | j| jd j| jd j| j¡‚| jS )NTzassertion failedúUnclosed tag: <%%%s>r=   z"Unterminated control keyword: '%s')rT   r   r   r	   r   r    rM   r8   r'   r   Ú	match_endÚmatch_expressionÚmatch_control_lineÚmatch_commentÚmatch_tag_startÚmatch_tag_endÚmatch_python_blockÚmatch_percentÚ
match_textr   ZMakoExceptionr   r:   rB   r   r   r   r   r
   )r   Zpreprocr   r   r   ÚparseÚ   sd       ÿ

ÿþ
ÿ

úzLexer.parsec                 C   sð   d}|   |tjtjB tjB ¡}|s&dS | ¡ \}}}|| _i }|rzt d|¡D ]*}|\}}	}
|	pb|
}| dd¡}|||< qN|  	t
j||¡ |rš| j ¡  nR|dkrì|   dtj¡}|sÐtjd| jd	 j f| jŽ‚|  	t
j| d
¡¡ |  ¡ S dS )NaÈ  
            \<%     # opening tag

            ([\w\.\:]+)   # keyword

            ((?:\s+\w+|\s*=\s*|"[^"]*?"|'[^']*?'|\s*,\s*)*)  # attrname, = \
                                               #        sign, string expression
                                               # comma is for backwards compat
                                               # identified in #366

            \s*     # more whitespace

            (/)?>   # closing

        Fz)\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")ú
r%   r   z(.*?)(?=\</%text>)rU   r=   r   T)r$   r   ÚIr7   ÚXÚgroupsrB   ÚfindallÚreplacerJ   r   rC   r   rF   r   r:   r   ÚTextr9   r[   )r   r#   r$   rB   ÚattrrE   Ú
attributesZattÚkeyZval1Zval2r   r   r   r   rZ     sB    þ ÿ

ÿþzLexer.match_tag_startc                 C   s†   |   d¡}|r~t| jƒs4tjd| d¡ f| jŽ‚n<| jd j| d¡krptjd| d¡| jd jf f| jŽ‚| j ¡  dS dS d S )Nz\</%[\t ]*([^\t ]+?)[\t ]*>z(Closing tag without opening tag: </%%%s>r   r=   z.Closing tag </%%%s> does not match tag: <%%%s>TF)	r$   r8   r   r   r:   r9   r   rB   rF   ©r   r$   r   r   r   r[   F  s*    

ÿÿýÿÿý
zLexer.match_tag_endc                 C   s.   |   dtj¡}|sdS | ¡ }|r&|S dS d S )Nz\ZFT)r$   r   r7   r9   )r   r$   Ústringr   r   r   rV   Z  s    zLexer.match_endc                 C   s@   |   dtj¡}|r8|  tj| d¡d | d¡ ¡ dS dS d S )Nz(?<=^)(\s*)%%(%*)r   ú%é   TF)r$   r   ÚMrJ   r   rf   r9   rj   r   r   r   r]   e  s     ÿzLexer.match_percentc                 C   s@   |   dtjtjB ¡}|r8| d¡}|r4|  tj|¡ dS dS d S )Na—  
                (.*?)         # anything, followed by:
                (
                 (?<=\n)(?=[ \t]*(?=%|\#\#))  # an eval or line-based
                                            # comment, preceded by a
                                            # consumed newline and whitespace
                 |
                 (?=\${)      # an expression
                 |
                 (?=</?%)  # a substitution or block or call start or end
                              # - don't consume
                 |
                 (\\\r?\n)    # an escaped newline  - throw away
                 |
                 \Z           # end of string
                )r   TF)r$   r   rb   r7   r9   rJ   r   rf   )r   r$   r   r   r   r   r^   o  s    
ï
zLexer.match_textc                 C   sd   |   d¡}|r\| j| j }}|  dd¡\}}t|ƒd }| jtj|| d¡dk||d dS dS d S )	Nz<%(!)?Fz%>r%   r   ú!©r   r   T)	r$   r   r   r<   r   rJ   r   ZCoder9   )r   r$   Úliner   r   r,   r   r   r   r\   Œ  s    
ûzLexer.match_python_blockc                 C   sz   |   d¡}|sdS | j| j }}|  ddd¡\}}|dkrL|  dd¡\}}nd}| dd	¡}| jtj|| ¡ ||d
 dS )Nz\${FTz\|r0   r.   Ú r`   r%   rp   )	r$   r   r   r<   re   rJ   r   Ú
ExpressionÚstrip)r   r$   rq   r   r   r,   Úescapesr   r   r   rW   Ÿ  s"    
ûzLexer.match_expressionc                 C   sì   |   dtj¡}|sdS | d¡}| d¡}|dkrÚt  d|¡}|sVtjd| f| jŽ‚| dd¡\}}|d k	}|rÆt| jƒs–tjd||f f| jŽ‚n0| jd	 j	|krÆtjd
|| jd	 j	f f| jŽ‚|  
tj|||¡ n|  
tj|¡ dS )NzC(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\\r?\n)|[^\r\n])*)(?:\r?\n|\Z)Fr   rm   rl   z(end)?(\w+)\s*(.*)zInvalid control line: '%s'z!No starting keyword '%s' for '%s'r=   z'Keyword '%s' doesn't match keyword '%s'T)r$   r   rn   r9   r   r:   r   r8   r   rB   rJ   r   rA   ÚComment)r   r$   Úoperatorr   Úm2rE   rB   r   r   r   rX   ´  sH    ý

ÿþ

ÿþÿÿýzLexer.match_control_linec                 C   s2   |   dtj¡}|r*|  tj| d¡¡ dS dS dS )z*matches the multiline version of a commentz<%doc>(.*?)</%doc>r   TFN)r$   r   r7   rJ   r   rv   r9   rj   r   r   r   rY   Ú  s
    zLexer.match_comment)NNN)N)Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   r$   r    r<   rJ   r   r   rM   rT   r_   rZ   r[   rV   r]   r^   r\   rW   rX   rY   r   r   r   r   r      s,        ÿ


(2
*:2
&r   )
Ú__doc__rO   r   Úmakor   r   Z
mako.pygenr   r   r   r   r   r   r   Ú<module>   s   