o
    èÇhÁ?  ã                   @   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   s|   || _ || _t | j¡| _d| _d| _d| _d| _g | _	g | _
g | _|| _|d u r.g | _d S t|dƒs9|g| _d S || _d S )Né   r   Ú__iter__)ÚtextÚfilenamer   ÚTemplateNodeÚtemplateÚmatched_linenoÚmatched_charposÚlinenoÚmatch_positionÚtagÚcontrol_lineÚternary_stackÚencodingÚpreprocessorÚhasattr)Úselfr   r	   Úinput_encodingr   © r   ú^/var/www/html/construction_image-detection-poc/venv/lib/python3.10/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 n ty$   |rt ||¡nt |¡}|t ||f< Y nw |  |¡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 ¡}|rP| ¡ \}}||kr|d n|| _ | j| _|d }|dkr;|| jk r;| 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   sJ  | j }d |¡}d}d}d}	 |  d¡}|rq|  dtj¡}|r!q|  d| ¡}|rL|r8|dksL|dksL|dksL| j|| j t| d¡ƒ … | d¡fS |sW|  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¤Ž‚)Nú|r   Tz#.*\nz+(\"\"\"|\'\'\'|\"|\')[^\\]*?(\\.[^\\]*?)*\1z(%s)r   z(.*?)(?=\"|\'|#|%s)Ú{Ú}ú(ú)ú[ú]zExpected: %sú,)r   Újoinr&   r    ÚSr   ÚlenÚgroupr+   r   ÚSyntaxExceptionr   )	r   Úwatch_nestingr   ÚstartposÚtext_reÚbrace_levelÚparen_levelÚbracket_levelr&   r   r   r   Úparse_until_textT   sN   

ÿÿÿüÿÿzLexer.parse_until_textc                 O   s¼  |  d| j¡ |  d| j¡ |  d| j¡ | j|d< ||i |¤Ž}t| jƒr0| jd j |¡ n| j	j |¡ | j
rd| j
d }|j |¡ t|tjƒrQ| |j¡sd| jrd| jd rd| jd d j |¡ t|tjƒr}t| jƒru| jd |_| j |¡ d S t|tjƒrØ|jr’| j
 ¡  | j ¡  d S |jr£| j
 |¡ | j g ¡ d S | j
r¹| j
d  |j¡r¹| jd  |¡ d S | j
rÚ| j
d  |j¡sÜtjd|j| j
d jf fi | j¤Ž‚d S d S d S )Nr   r   r   r	   éÿÿÿÿz1Keyword '%s' not a legal ternary for keyword '%s')Ú
setdefaultr   r   r   r	   r:   r   ÚnodesÚappendr   r   Ú
isinstancer   ÚControlLineÚ
is_ternaryÚkeywordr   ÚTagÚparentÚisendÚpopÚ
is_primaryr   r<   r   )r   ÚnodeclsÚargsÚkwargsÚnodeÚcontrol_framer   r   r   Úappend_node|   s^   



þ
ýûú

ÿÿÿÿýòzLexer.append_nodez #.*coding[:=]\s*([-\w.]+).*\r?\nc              
   C   s  t |tƒr| j |¡}|r| d¡p|pd}||fS | tj¡rS|ttjƒd… }d}| j | 	dd¡¡}|durR| d¡dkrRt
 d| d¡ | 	dd¡dd|¡‚n| j | 	dd¡¡}|rd| d¡n|pgd}|r‹z
| 	|¡}W ||fS  tyŠ   t
 d| | 	dd¡dd|¡‚w ||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)rH   ÚstrÚ
_coding_rer&   r;   Ú
startswithÚcodecsÚBOM_UTF8r:   Údecoder   ÚCompileExceptionÚUnicodeDecodeError)r   r   Ú
decode_rawÚknown_encodingr	   Úmr   Úparsed_encodingr   r   r   Údecode_raw_stream°   sJ   
ÿ
ú€	öÿ
úÿ
zLexer.decode_raw_streamc                 C   s<  |   | jd| j| j¡\| _| _| jD ]}|| jƒ| _q|  | j¡ t| jƒ| _	 | j	| jkr/n9|  
¡ r4n4|  ¡ r9q'|  ¡ r>q'|  ¡ rCq'|  ¡ rHq'|  ¡ rMq'|  ¡ rRq'|  ¡ rWq'|  ¡ r\q'| j	| jkrcnt d¡‚t| jƒr}tjd| jd j fi | j¤Ž‚t| jƒr›t d| jd j | j| jd j| jd j| j¡‚| jS )NTzassertion failedúUnclosed tag: <%%%s>rD   z"Unterminated control keyword: '%s')rd   r   r   r	   r   r"   rY   r:   r)   r   Ú	match_endÚmatch_expressionÚmatch_control_lineÚmatch_commentÚmatch_tag_startÚmatch_tag_endÚmatch_python_blockÚmatch_percentÚ
match_textr   ÚMakoExceptionr   r<   rK   r   r   r   r   r   )r   Úpreprocr   r   r   ÚparseÚ   s`   ÿ


ÿþ

ÿ

úzLexer.parsec                 C   sö   d}|   |tjtjB tjB ¡}|sdS | ¡ \}}}|| _i }|r=t d|¡D ]}|\}}	}
|	p1|
}| dd¡}|||< q'|  	t
j||¡ |rN| j ¡  dS |dkry|   dtj¡}|skt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>)re   rD   r   T)r&   r    ÚIr9   ÚXÚgroupsrK   ÚfindallÚreplacerV   r   rL   r   rO   r   r<   r   ÚTextr;   rk   )r   r%   r&   rK   ÚattrrN   Ú
attributesÚattÚkeyÚval1Úval2r   r   r   r   rj     sB   þÿ



÷ÿþzLexer.match_tag_startc                 C   sˆ   |   d¡}|rBt| jƒstjd| d¡ fi | j¤Ž‚| jd j| d¡kr;tjd| d¡| jd jf fi | j¤Ž‚| j ¡  dS dS )Nz\</%[\t ]*([^\t ]+?)[\t ]*>z(Closing tag without opening tag: </%%%s>r   rD   z.Closing tag </%%%s> does not match tag: <%%%s>TF)	r&   r:   r   r   r<   r;   r   rK   rO   ©r   r&   r   r   r   rk   F  s*   

ÿÿýÿÿý
zLexer.match_tag_endc                 C   s*   |   dtj¡}|sdS | ¡ }|r|S dS )Nz\ZFT)r&   r    r9   r;   )r   r&   Ústringr   r   r   rf   Z  s   zLexer.match_endc                 C   s<   |   dtj¡}|r|  tj| d¡d | d¡ ¡ dS dS )Nz(?<=^)(\s*)%%(%*)r   ú%é   TF)r&   r    ÚMrV   r   rx   r;   r   r   r   r   rm   e  s   ÿzLexer.match_percentc                 C   s<   |   dtjtjB ¡}|r| d¡}|r|  tj|¡ 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    rt   r9   r;   rV   r   rx   )r   r&   r   r   r   r   rn   o  s   
ï
zLexer.match_textc                 C   s`   |   d¡}|r.| j| j}}|  dd¡\}}t|ƒd }| jtj|| d¡dk||d dS dS )	Nz<%(!)?Fz%>r'   r   ú!©r   r   T)	r&   r   r   rC   r   rV   r   ÚCoder;   )r   r&   Úliner   r   r.   r   r   r   rl   Œ  s   
ûzLexer.match_python_blockc                 C   sz   |   d¡}|s	dS | j| j}}|  ddd¡\}}|dkr&|  dd¡\}}nd}| dd	¡}| jtj|| ¡ ||d
 dS )Nz\${FTz\|r2   r0   Ú rr   r'   r…   )	r&   r   r   rC   rw   rV   r   Ú
ExpressionÚstrip)r   r&   r‡   r   r   r.   Úescapesr   r   r   rg   Ÿ  s"   
ûzLexer.match_expressionc                 C   sø   |   dtj¡}|sdS | d¡}| d¡}|dkrst  d|¡}|s-tjd| fi | j¤Ž‚| dd¡\}}|d u}|rht| jƒsNtjd||f fi | j¤Ž‚| jd	 j	|krhtjd
|| jd	 j	f fi | j¤Ž‚|  
tj|||¡ dS |  
tj|¡ dS )NzC(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\\r?\n)|[^\r\n])*)(?:\r?\n|\Z)Fr   r‚   r   z(end)?(\w+)\s*(.*)zInvalid control line: '%s'z!No starting keyword '%s' for '%s'rD   z'Keyword '%s' doesn't match keyword '%s'T)r&   r    rƒ   r;   r   r<   r   r:   r   rK   rV   r   rI   ÚComment)r   r&   Úoperatorr   Úm2rN   rK   r   r   r   rh   ´  sJ   ý

ÿþ

ÿþÿÿýÿzLexer.match_control_linec                 C   s.   |   dtj¡}|r|  tj| d¡¡ dS dS )z*matches the multiline version of a commentz<%doc>(.*?)</%doc>r   TF)r&   r    r9   rV   r   rŒ   r;   r   r   r   r   ri   Ú  s
   zLexer.match_comment)NNN)N)Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   r&   r"   rC   rV   r    r!   rY   rd   rq   rj   rk   rf   rm   rn   rl   rg   rh   ri   r   r   r   r   r      s*    
ÿ

(
2*:2
&r   )
Ú__doc__r[   r    Úmakor   r   Ú
mako.pygenr   r   r   r   r   r   r   Ú<module>   s   