B
    è¹`B  ã               @   s\   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 ddlmZ i Z	G dd„ de
ƒZdS )	zGprovides the Lexer class for parsing template strings into parse trees.é    N)Úcompat)Ú
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S )&ÚLexerNFc             C   s’   || _ || _t | j¡| _d| _d| _d| _d| _g | _	g | _
g | _|| _|| _tjrd|rdt d¡‚|d krtg | _nt|dƒsˆ|g| _n|| _d S )Né   r   z4Mako for Python 3 does not support disabling UnicodeÚ__iter__)ÚtextÚfilenamer   ZTemplateNodeÚtemplateÚmatched_linenoÚmatched_charposÚlinenoÚmatch_positionÚtagÚcontrol_lineÚternary_stackÚdisable_unicodeÚencodingr   Zpy3kr   ZUnsupportedErrorÚpreprocessorÚhasattr)Úselfr	   r
   r   Zinput_encodingr   © r   úR/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/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_kwargs6   s    zLexer.exception_kwargsc             C   sX   yt ||f }W n< tk
rL   |r2t ||¡}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   Úmatch?   s    
zLexer.matchc             C   s¶   | j }| | j| j ¡}|r²| ¡ \}}||kr:|d | _ n|| _ | j| _t d| j|| j … ¡}|d }x,|dkr”|| jk r”| j| dkr”|d8 }qjW || | _	|  jt
|ƒ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   z\nr   Ú
)r   r&   r	   Úspanr   r   r    ÚfindallÚ
textlengthr   Úlen)r   r%   Úmpr&   ÚstartÚendÚlinesÚcpr   r   r   r"   M   s    "
zLexer.match_regc       	      G   sR  | j }d |¡}d}d}d}x.|  d¡}|r0q |  dtj¡}|rDq |  d| ¡}|rš|rr|dksš|dksš|dksš| j|| j t| d¡ƒ … | d¡fS |s°|  d| tj¡}|r0|| 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 W d S )Nú|r   z#.*\nz+(\"\"\"|\'\'\'|\"|\')[^\\]*?(\\.[^\\]*?)*\1z(%s)r   z(.*?)(?=\"|\'|#|%s)Ú{Ú}ú(ú)ú[ú]zExpected: %sú,)r   Újoinr&   r    ÚSr	   r+   ÚgroupÚcountr   ÚSyntaxExceptionr   )	r   Zwatch_nestingr	   ÚstartposZtext_reZbrace_levelZparen_levelZbracket_levelr&   r   r   r   Úparse_until_textk   s@    


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
   r+   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“   sF    







zLexer.append_nodez #.*coding[:=]\s*([-\w.]+).*\r?\nc          
   C   s&  t |tjƒr6| 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y| 
|¡}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)rC   r   Ú	text_typeÚ
_coding_rer&   r;   Ú
startswithÚcodecsÚBOM_UTF8r+   Údecoder   ÚCompileExceptionÚUnicodeDecodeError)r   r	   Z
decode_rawZknown_encodingr
   Úmr   Zparsed_encodingr   r   r   Údecode_raw_streamÄ   s@    


zLexer.decode_raw_streamc             C   s<  |   | j| j | j| j¡\| _| _x| jD ]}|| jƒ| _q*W |  | j¡ t| jƒ| _	xz| j
| j	krfP |  ¡ rpP |  ¡ rzqX|  ¡ r„qX|  ¡ rŽqX|  ¡ r˜qX|  ¡ r¢qX|  ¡ r¬qX|  ¡ r¶qX| j
| j	krÄP t d¡‚qXW t| jƒrøtjd| jd j f| jŽ‚t| jƒr6t d| jd j | j| jd j| jd j| j¡‚| jS )Nzassertion failedzUnclosed tag: <%%%s>r@   z"Unterminated control keyword: '%s')rX   r	   r   r   r
   r   r"   rP   r+   r*   r   Ú	match_endÚmatch_expressionÚmatch_control_lineÚmatch_commentÚmatch_tag_startÚmatch_tag_endÚmatch_python_blockÚ
match_textr   rU   r   r=   rE   r   r   r   r   r   )r   Zpreprocr   r   r   Úparseò   sR    


zLexer.parsec             C   sô   |   dtjtjB tjB ¡}|rì| ¡ \}}}|| _i }|rvx:t d|¡D ]*}|\}}}	|p\|	}
|
 dd¡}
|
||< qHW |  	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 dS d S )Na+  
            \<%     # opening tag

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

            ((?:\s+\w+|\s*=\s*|".*?"|'.*?')*)  # attrname, = \
                                               #        sign, string expression

            \s*     # more whitespace

            (/)?>   # closing

            z)\s*(\w+)\s*=\s*(?:'([^']*)'|\"([^\"]*)\")z
r'   r	   z(.*?)(?=\</%text>)zUnclosed tag: <%%%s>r@   r   TF)r&   r    ÚIr:   ÚXÚgroupsrE   r)   ÚreplacerM   r   rF   r   rI   r   r=   r   ÚTextr;   r^   )r   r&   rE   ÚattrrH   Ú
attributesZattÚkeyZval1Zval2r	   r   r   r   r]   )  s6    
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 ]*>z(Closing tag without opening tag: </%%%s>r   r@   z.Closing tag </%%%s> does not match tag: <%%%s>TF)	r&   r+   r   r   r=   r;   r   rE   rI   )r   r&   r   r   r   r^   X  s    



zLexer.match_tag_endc             C   s0   |   dtj¡}|r(| ¡ }|r"|S dS ndS d S )Nz\ZTF)r&   r    r:   r;   )r   r&   Ústringr   r   r   rY   l  s    zLexer.match_endc             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   r:   r;   rM   r   rf   )r   r&   r	   r   r   r   r`   w  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   rM   r   ZCoder;   )r   r&   Úliner   r	   r.   r   r   r   r_   ”  s    
zLexer.match_python_blockc             C   s~   |   d¡}|rv| j| j }}|  ddd¡\}}|dkrH|  dd¡\}}nd}| dd¡}| jtj|| ¡ ||d	 dS d
S d S )Nz\${Tz\|r3   r1   Ú z
r'   )r   r   F)	r&   r   r   r?   re   rM   r   Z
ExpressionÚstrip)r   r&   rl   r   r	   r.   Zescapesr   r   r   rZ   §  s     
zLexer.match_expressionc             C   sð   |   dtj¡}|rè| d¡}| d¡}|dkrÖt  d|¡}|sRt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 dS d S )NzB(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\r?\n)|[^\r\n])*)(?:\r?\n|\Z)r   é   ú%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'TF)r&   r    ÚMr;   r   r=   r   r+   r   rE   rM   r   rD   ÚComment)r   r&   Úoperatorr	   Úm2rH   rE   r   r   r   r[   ¼  s:    




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    r:   rM   r   rr   r;   )r   r&   r   r   r   r\   ã  s
    zLexer.match_comment)NFNN)N)Ú__name__Ú
__module__Ú__qualname__r   Úpropertyr   r&   r"   r?   rM   r    r!   rP   rX   ra   r]   r^   rY   r`   r_   rZ   r[   r\   r   r   r   r   r      s(      
	
(/
.7/'r   )Ú__doc__rR   r    Zmakor   r   r   Z
mako.pygenr   r   Úobjectr   r   r   r   r   Ú<module>   s   