a
    e	gÁ?  ã                   @   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 u 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   úM/var/www/html/llm_bihealth/app/venv/lib/python3.9/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   sT   zt ||f }W n8 tyH   |r.t ||¡nt |¡}|t ||f< Y n0 |  |¡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   sP  | 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i | 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   sV    

ÿÿþþþÿüÿÿzLexer.parse_until_textc                 O   sº  |  d| j¡ |  d| j¡ |  d| j¡ | j|d< ||i |¤Ž}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rF| j
 |¡ | j g ¡ np| j
rt| j
d  |j¡rt| jd  |¡ nB| j
r¶| j
d  |j¡s¶tjd|j| j
d jf fi | j¤Ž‚d S )Nr   r   r   r	   éÿÿÿÿz1Keyword '%s' not a legal ternary for keyword '%s')Ú
setdefaultr   r   r   r	   r8   r   Ú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urÐ| d¡dkrÐt
 d| d¡ | 	dd¡dd|¡‚n*| j | 	dd¡¡}|rÈ| d¡n|pÎd}|rz| 	|¡}W n2 ty   t
 d| | 	dd¡dd|¡‚Y n0 ||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)rA   Ú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i | j¤Ž‚t| jƒr8t 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')rU   r   r   r	   r   r    rN   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:   rC   r   r   r   r   r
   )r   Zpreprocr   r   r   ÚparseÚ   s^    ÿ

ÿþ
ÿ

ú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 ¡  nV|dkrð|   dtj¡}|sÔtjd| jd	 j fi | 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>)rV   r=   r   T)r$   r   ÚIr7   ÚXÚgroupsrC   ÚfindallÚreplacerK   r   rD   r   rG   r   r:   r   ÚTextr9   r\   )r   r#   r$   rC   ÚattrrF   Ú
attributesZattÚkeyZval1Zval2r   r   r   r   r[     s@    þÿ

ÿþzLexer.match_tag_startc                 C   sŽ   |   d¡}|r†t| jƒs8tjd| d¡ fi | j¤Ž‚n@| jd j| d¡krxtjd| d¡| jd jf fi | 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   rC   rG   ©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   rW   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   ÚMrK   r   rg   r9   rk   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   rc   r7   r9   rK   r   rg   )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   rK   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.   Ú ra   r%   rq   )	r$   r   r   r<   rf   rK   r   Ú
ExpressionÚstrip)r   r$   rr   r   r   r,   Zescapesr   r   r   rX   Ÿ  s"    
ûzLexer.match_expressionc                 C   sø   |   dtj¡}|sdS | d¡}| d¡}|dkræt  d|¡}|sZtjd| fi | j¤Ž‚| dd¡\}}|d u}|rÒt| jƒsžtjd||f fi | j¤Ž‚n4| jd	 j	|krÒtjd
|| jd	 j	f fi | j¤Ž‚|  
tj|||¡ n|  
tj|¡ dS )NzC(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\\r?\n)|[^\r\n])*)(?:\r?\n|\Z)Fr   rn   rm   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   ro   r9   r   r:   r   r8   r   rC   rK   r   rB   ÚComment)r   r$   Úoperatorr   Úm2rF   rC   r   r   r   rY   ´  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   rK   r   rv   r9   rk   r   r   r   rZ   Ú  s
    zLexer.match_comment)NNN)N)Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   r$   r    r<   rK   r   r   rN   rU   r`   r[   r\   rW   r^   r_   r]   rX   rY   rZ   r   r   r   r   r      s(    ÿ


(2
*:2
&r   )
Ú__doc__rP   r   Zmakor   r   Z
mako.pygenr   r   r   r   r   r   r   Ú<module>   s   