U  üeÄ?ã@sNdZddlZddlZddlmZddlmZddlmZiZGdd„dƒZ dS)zGprovides the Lexer class for parsing template strings into parse trees.éN)Ú exceptions)Ú parsetree)Úadjust_whitespacec@s¦eZdZd%dd„Zedd„ƒZd&dd„Zdd „Zd d „Zd d „Z e   d¡Z dd„Z dd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd „Zd!d"„Zd#d$„ZdS)'ÚLexerNcCsx||_||_t |j¡|_d|_d|_d|_d|_g|_ g|_ g|_ ||_ |dkrZg|_ nt|dƒsn|g|_ n||_ dS)NérÚ__iter__)ÚtextÚfilenamerZ TemplateNodeÚtemplateÚmatched_linenoÚmatched_charposÚlinenoÚmatch_positionÚtagÚ control_lineÚ ternary_stackÚencodingÚ preprocessorÚhasattr)Úselfrr Zinput_encodingr©rú8/opt/hc_python/lib/python3.8/site-packages/mako/lexer.pyÚ__init__s   zLexer.__init__cCs|j|j|j|jdœS)N)Úsourcer Úposr )rr r r )rrrrÚexception_kwargs*s üzLexer.exception_kwargscCsVzt||f}Wn:tk rJ|r0t ||¡nt |¡}|t||f<YnX| |¡S)z>compile the given regexp, cache the reg, and call match_reg().)Ú _regexp_cacheÚKeyErrorÚreÚcompileÚ match_reg)rÚregexpÚflagsÚregrrrÚmatch3s z Lexer.matchcCs¤|j}| |j|j¡}|r | ¡\}}||kr6|dn||_|j|_|d}|dkrv||jkrv|jd|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. rrNÚ ) rr$rÚspanr r Ú textlengthÚrfindr Úcount)rr#Úmpr$ÚstartÚendÚcprrrr >s   zLexer.match_regc GsL|j}d |¡}d}d}d}| d¡}|r,q| dtj¡}|r@q| d|¡}|r–|rn|dks–|dks–|dks–|j||jt| d¡ƒ…| d¡fS|s¬| d|tj¡}|r,|| d¡ d¡7}|| d¡ d ¡8}|| d¡ d ¡7}|| d¡ d ¡8}|| d¡ d ¡7}|| d¡ d ¡8}qt j dd |¡f|j Ž‚qdS)Nú|rz#.*\nz+(\"\"\"|\'\'\'|\"|\')[^\\]*?(\\.[^\\]*?)*\1z(%s)rz(.*?)(?=\"|\'|#|%s)Ú{Ú}ú(ú)ú[ú]z Expected: %sú,) rÚjoinr$rÚSrÚlenÚgroupr)rÚSyntaxExceptionr) rZ watch_nestingrÚstartposZtext_reZ brace_levelZ paren_levelZ bracket_levelr$rrrÚparse_until_textTsZ  ÿÿþþþÿü ÿÿzLexer.parse_until_textcOs²| d|j¡| d|j¡| d|j¡|j|d<|||Ž}t|jƒr\|jdj |¡n|j j |¡|j rÄ|j d}|j |¡t |t j ƒrž| |j¡sÄ|jrÄ|jdrÄ|jddj |¡t |t jƒrôt|jƒræ|jd|_|j |¡nºt |t j ƒr®|jr |j  ¡|j ¡nŽ|jrB|j  |¡|j g¡nl|j rp|j d |j¡rp|jd |¡n>|j r®|j d |j¡s®tjd|j|j djff|jŽ‚dS)Nrr rr éÿÿÿÿz1Keyword '%s' not a legal ternary for keyword '%s')Ú setdefaultrr r r r8rZnodesÚappendr rÚ isinstancerÚ ControlLineZ is_ternaryÚkeywordrÚTagÚparentÚisendÚpopZ is_primaryrr:r)rZnodeclsÚargsÚkwargsÚnodeZ control_framerrrÚ append_node|sZ      þ ýûú      ÿÿÿÿýzLexer.append_nodez #.*coding[:=]\s*([-\w.]+).*\r?\nc Cs"t|tƒr4|j |¡}|r$| d¡p*|p*d}||fS| tj¡r¦|ttjƒd…}d}|j |  dd¡¡}|dk rÐ| d¡dkrÐt   d| d¡|  dd¡dd|¡‚n*|j |  dd¡¡}|rÈ| d¡n|pÎd}|rz|  |¡}Wn4t k rt   d||  dd¡dd|¡‚YnX||fS)z¡given string/unicode or bytes/string, determine encoding from magic encoding comment, return body as unicode or raw if decode_raw=False rzutf-8NÚignorezHFound utf-8 BOM in file, with conflicting magic encoding comment of '%s'rz0Unicode decode operation of encoding '%s' failed) r@ÚstrÚ _coding_rer$r9Ú startswithÚcodecsÚBOM_UTF8r8ÚdecoderZCompileExceptionÚUnicodeDecodeError)rrZ decode_rawZknown_encodingr ÚmrZparsed_encodingrrrÚdecode_raw_stream°sD   ÿ ú ÿ ú zLexer.decode_raw_streamcCs:| |jd|j|j¡\|_|_|jD]}||jƒ|_q$| |j¡t|jƒ|_|j |jkr\qÐ|  ¡rfqÐ|  ¡rpqN|  ¡rzqN|  ¡r„qN| ¡rŽqN| ¡r˜qN| ¡r¢qN| ¡r¬qN| ¡r¶qN|j |jkrÄqÐt d¡‚qNt|jƒrötjd|jdjf|jŽ‚t|jƒr4t d|jdj|j|jdj|jdj|j¡‚|jS)NTzassertion failedúUnclosed tag: <%%%s>r=z"Unterminated control keyword: '%s')rTrrr rr rMr8r'rÚ match_endÚmatch_expressionÚmatch_control_lineÚ match_commentÚmatch_tag_startÚ match_tag_endÚmatch_python_blockÚ match_percentÚ match_textrZ MakoExceptionrr:rBrrr rr )rZpreprocrrrÚparseÚsdÿ        ÿþ  ÿ  úz Lexer.parsec Csðd}| |tjtjBtjB¡}|s&dS| ¡\}}}||_i}|rzt d|¡D]*}|\}} } | pb| } |  dd¡} | ||<qN|  t j ||¡|rš|j   ¡nR|dkrì| dtj¡}|sÐtjd|j d jf|jŽ‚|  t j| d ¡¡| ¡Sd 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%rz(.*?)(?=\)rUr=rT)r$rÚIr7ÚXÚgroupsrBÚfindallÚreplacerJrrCrrFrr:rÚTextr9r[) rr#r$rBÚattrrEÚ attributesZattÚkeyZval1Zval2rrrrrZsBþÿ    ÿþzLexer.match_tag_startcCs†| d¡}|r~t|jƒs4tjd| d¡f|jŽ‚n<|jdj| d¡krptjd| d¡|jdjff|jŽ‚|j ¡dSdSdS)Nz\z(Closing tag without opening tag: rr=z.Closing tag does not match tag: <%%%s>TF) r$r8rrr:r9rrBrF©rr$rrrr[Fs*  ÿÿýÿÿý zLexer.match_tag_endcCs.| dtj¡}|sdS| ¡}|r&|SdSdS)Nz\ZFT)r$rr7r9)rr$ÚstringrrrrVZszLexer.match_endcCs@| dtj¡}|r8| tj| d¡d| d¡¡dSdSdS)Nz(?<=^)(\s*)%%(%*)rú%éTF)r$rÚMrJrrfr9rjrrrr]esÿzLexer.match_percentcCs@| dtjtjB¡}|r8| d¡}|r4| tj|¡dSdSdS)Naš (.*?) # anything, followed by: ( (?<=\n)(?=[ \t]*(?=%|\#\#)) # an eval or line-based # comment, preceded by a # consumed newline and whitespace | (?=\${) # an expression | (?=r%rú!©r rT) r$r r r<rrJrZCoder9)rr$Úlinerrr,rrrr\Œs   ûzLexer.match_python_blockcCsz| d¡}|sdS|j|j}}| ddd¡\}}|dkrL| dd¡\}}nd}| dd ¡}|jtj|| ¡||d dS) Nz\${FTz\|r0r.Úr`r%rp) r$r r r<rerJrÚ ExpressionÚstrip)rr$rqrrr,ÚescapesrrrrWŸs"  ûzLexer.match_expressioncCsì| dtj¡}|sdS| d¡}| d¡}|dkrÚt d|¡}|sVtjd|f|jŽ‚| dd¡\}}|dk }|rÆt|jƒs–tjd||ff|jŽ‚n0|jd j |krÆtjd ||jd j ff|jŽ‚|  t j |||¡n|  t j |¡d S) NzC(?<=^)[\t ]*(%(?!%)|##)[\t ]*((?:(?:\\\r?\n)|[^\r\n])*)(?:\r?\n|\Z)Frrmrlz(end)?(\w+)\s*(.*)zInvalid control line: '%s'z!No starting keyword '%s' for '%s'r=z'Keyword '%s' doesn't match keyword '%s'T)r$rrnr9rr:rr8rrBrJrrAÚComment)rr$ÚoperatorrÚm2rErBrrrrX´sHý   ÿþ  ÿþÿÿýzLexer.match_control_linecCs2| dtj¡}|r*| tj| d¡¡dSdSdS)z*matches the multiline version of a commentz<%doc>(.*?)rTFN)r$rr7rJrrvr9rjrrrrYÚs zLexer.match_comment)NNN)N)Ú__name__Ú __module__Ú __qualname__rÚpropertyrr$r r<rJrrrMrTr_rZr[rVr]r^r\rWrXrYrrrrrs,ÿ   (2 *:2  &r) Ú__doc__rOrÚmakorrZ mako.pygenrrrrrrrÚs