lf{v dZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd l mZdd lmZed ZejdejZejdZejdejZejdZejdejejzZ edddddlmZdZn#e $rejdZdZYnwxYwe dZ!e dZ"e dZ#e dZ$e dZ%e dZ&e d Z'e d!Z(e d"Z)e d#Z*e d$Z+e d%Z,e d&Z-e d'Z.e d(Z/e d)Z0e d*Z1e d+Z2e d,Z3e d-Z4e d.Z5e d/Z6e d0Z7e d1Z8e d2Z9e d3Z:e d4Z;e d5Z<e d6Z=e d7Z>e d8Z?e d9Z@e d:ZAe d;ZBe d<ZCe d=ZDe d>ZEe d?ZFe d@ZGe dAZHe dBZIe dCZJe dDZKe dEZLe dFZMe dGZNe dHZOe dIZPe dJZQidKe!dLe9dMe%dNe(dOe1dPe0dQe4dRe:dSe,dTe6dUe-dVe7dWe+dXe5dYe'dZe2d[e)e*e.e/e"e&e#e3e$e8d\ ZReSd]e eRDZTeUeReUeTks Jd^ejd_d`VdaeWeRdbcDzZXeYeGeIeHe;eLeMeNgZZeYe;eOeIeNgZ[ddZ\deZ]dfZ^dgZ_dhZ`GdidjeaZbGdkdlecZde GdmdneaZee GdodpeaZfdqZgGdrdsecZhGdtdueaZidS)vzImplements a Jinja / Python combination lexer. The ``Lexer`` class is used to do some preprocessing. It filters out invalid operators like the bitshift operators we don't allow in templates. It separates template code and python code in expressions. N) literal_eval)deque) itemgetter)implements_iterator)intern) iteritems) text_type)TemplateSyntaxError)LRUCache2z\s+z (\r\n|\r|\n)z7('([^'\\]*(?:\\.[^'\\]*)*)'|"([^"\\]*(?:\\.[^"\\]*)*)")z (\d+_)*\d+z (?eval)patternTz[a-zA-Z_][a-zA-Z0-9_]*Faddassigncoloncommadivdoteqfloordivgtgteqlbracelbracketlparenltlteqmodmulnepipepowrbracerbracketrparen semicolonsubtilde whitespacefloatintegernamestringoperator block_begin block_endvariable_begin variable_end raw_beginraw_end comment_begin comment_endcommentlinestatement_beginlinestatement_endlinecomment_beginlinecomment_end linecommentdatainitialeof+-/z//*%z**~[](){}z==z!=>) z>=r[s BBBTQ1a&BBBzoperators droppedz(%s)rRc#>K|]}tj|VdSN)reescaperWxs rZ rcs*UUqbillUUUUUUr\c"t| Sr^)lenrbs rZrgsSQRVVGr\)keyc|tvr t|Stdtdtdtdt dt dtdtdtdtd td td i ||S) Nzbegin of commentzend of commentr8zbegin of statement blockzend of statement blockzbegin of print statementzend of print statementzbegin of line statementzend of line statementztemplate data / textzend of template)reverse_operatorsTOKEN_COMMENT_BEGINTOKEN_COMMENT_END TOKEN_COMMENTTOKEN_LINECOMMENTTOKEN_BLOCK_BEGINTOKEN_BLOCK_ENDTOKEN_VARIABLE_BEGINTOKEN_VARIABLE_ENDTOKEN_LINESTATEMENT_BEGINTOKEN_LINESTATEMENT_END TOKEN_DATA TOKEN_EOFget) token_types rZ_describe_token_typerysz&&& ,,/+y95184!#<!8*$  c*j!! "r\cX|jtkr|jSt|jS)z#Returns a description of the token.)type TOKEN_NAMEvaluery)tokens rZdescribe_tokenrs' zZ{  + ++r\czd|vr'|dd\}}|tkr|Sn|}t|S)z0Like `describe_token` but for token expressions.rQr)splitr|ry)exprr{r}s rZdescribe_token_exprrsL d{{jja(( e :  L   % %%r\cPtt|S)zsCount the number of newline characters in the string. This is useful for extensions that filter a stream. )re newline_refindall)r}s rZcount_newlinesrs  z!!%(( ) ))r\cftj}t|jt||jft|jt ||jft|jt||jfg}|j @| t|j td||j zf|j @| t|j td||j zfdt|dDS)zACompiles all the rules from the environment into a list of rules.Nz ^[ \t\v]*z(?:^|(?<=\S))[^\S\r\n]*c"g|] }|dd S)rNrVras rZr[z!compile_rules..s 7 7 7aAabbE 7 7 7r\T)reverse)r_r`recomment_start_stringrkblock_start_stringrovariable_start_stringrqline_statement_prefixappendrsline_comment_prefixTOKEN_LINECOMMENT_BEGINsorted) environmenteruless rZ compile_rulesrsC A  0 1 1  Ak. / /  . / /  Ak, - -  1 2 2 Ak/ 0 0  E$(4 K566)qq!BCCC    &2 K344'*QQ{/N-O-OO     8 76%666 7 7 77r\c"eZdZdZefdZdZdS)FailurezjClass that raises a `TemplateSyntaxError` if called. Used by the `Lexer` to specify known errors. c"||_||_dSr^)message error_class)selfrclss rZ__init__zFailure.__init__s r\c:||j||r^)rr)rlinenofilenames rZ__call__zFailure.__call__st|VX>>>r\N)__name__ __module__ __qualname____doc__r rrrVr\rZrrsF%8?????r\rcfeZdZdZdZdedD\ZZZdZ dZ dZ dZ d Z d S) Tokenz Token class.rVc#NK|] }tt|V!dSr^)propertyrras rZrczToken.s0EEq8JqMM22EEEEEEr\c rt||tt||fSr^)tuple__new__rstr)rrr{r}s rZrz Token.__new__ s+}}S66#d))+<+EL'IM  &%/3tt^DL5Q5Q5QR #     < JJJJDJJJJs %B;;C N)r)rrrrrrr __nonzero__rrrrrrrrrrrVr\rZrr@s )))HHHK X### ...    r\rc |j|j|j|j|j|j|j|j|j|j |j |j f }t |}|t|}|t|<|S)z(Return a lexer which is probably cached.)rblock_end_stringrvariable_end_stringrcomment_end_stringrr trim_blocks lstrip_blocksnewline_sequencekeep_trailing_newline _lexer_cacherwLexer)rrhlexers rZ get_lexerrs &$)'(&)'!$) C   S ! !E }k""! S Lr\c&eZdZdZdZfdZxZS)OptionalLStripzWA special tuple for marking a point in the state that can have lstrip applied. rVcVtt|||Sr^)superrr)rmemberskwargs __class__s rZrzOptionalLStrip.__new__s#^S))11#w???r\)rrrrrr __classcell__)rs@rZrrsRI@@@@@@@@@r\rc6eZdZdZdZdZddZd dZd dZdS) ra Class that implements a lexer for a given environment. Automatically created by the environment class, usually you don't have to do that. Note that the lexer is not automatically bound to an environment. Multiple environments can share the same lexer. ctj}d}ttdftt dft tdfttdfttdfttdfg}t|}|jrdpd}|jr |dnd|_|j|_|j|_d|ddd||jd ||jd ||jd gd |Dzzt/t0d d f|dt0dfgt2|d||jd ||jd|dt6t8fdf|dt;dfdfgt<|d||jd ||jd|t>dfg|zt@|d||j!d ||j!tDdfg|ztF|d||jd||jd ||j|d t/t0tHdf|dt;dfdfgtJ|dtLdfg|ztN|dtPtRfdfgi|_*dS)NcZtj|tjtjzSr^)r_compileMSrfs rZczLexer.__init__..cs:a-- -r\z\n?rz[^ \t]rootz (.*?)(?:%s)rRz(?Pz(\-|\+|)\s*raw\s*(?:\-z\s*|z))c&g|]\}}d|d|dS)z(?P.s;$(Aqq:;AAA >r\#bygroupz.+z (.*?)((?:\-rJ#popz(.)zMissing end of comment tagz(?:\-z\-z (.*?)((?:z(\-|\+|))\s*endraw\s*(?:\-zMissing end of raw directivez \s*(\n|$)z(.*?)()(?=\n|$))+r_r` whitespace_reTOKEN_WHITESPACEfloat_re TOKEN_FLOAT integer_re TOKEN_INTEGERname_rer| string_re TOKEN_STRING operator_reTOKEN_OPERATORrrrlstrip_unless_rerrjoinrrrrurkrrmrlrrorprqrrrTOKEN_RAW_BEGIN TOKEN_RAW_ENDrsrtrrnTOKEN_LINECOMMENT_ENDr)rrrr  tag_rulesroot_tag_rulesblock_suffix_res rZrzLexer.__init__s I . . . ,d 3 {D )  - j$ '  d + .$ /  '{33&1>>>Ak=>>>'  # # AAAk<====Ak:;;;;Ak:;;;+OO #:}== 5G$BCCEtL" &<"96B(( $A())&(=>&Ch  r\cBt|j|S)z@Called for strings and template data to normalize it to unicode.)rr(r)rr}s rZ_normalize_newlineszLexer._normalize_newlinesas~~d3U;;;r\Nc~|||||}t||||||S)z:Calls tokeniter + tokenize and wraps it in a token stream.) tokeniterrwrap)rsourcer-rstaters rZtokenizezLexer.tokenizees;h>>499VT8<t|}tr&| std|||n%|tkr ||ddddd}n#t"$rQ}t|dd}t||||d }~wwxYw|t(kr$t+|d d }nG|t.kr$t1|d d }n|t2kr t4|}t7|||Vd S) zThis is called with the stream as returned by `tokenize` and wraps every token in a :class:`Token` and converts the value. keywordzInvalid character in identifierrasciibackslashreplacezunicode-escaperQNrr)ignored_tokensrsrortrprrrur#r|r check_ident isidentifierr rencodedecode Exceptionrstriprintreplacerrr operatorsr) rrr-rrr~r}rmsgs rZr&z Lexer.wrapjs#%+& .& . FE5&&333)111'?M:::*$$0077)##*$$E u'9'9';';-964,&&K00qt==);<< 011E !KKKa&&,,s++B/5577C-c64JJJK-''EMM#r2233+%%$U]]3%;%;<<.((!%(u-- - - - -M& .& .sAD E0A E++E0c#8 Kt|}|}|jr3|r1dD].}||r|dn/d|}d}d}dg} |,|dkr&|dvs Jd | |d z|j| d } t|} g} |j} d}d } | D]\}}}| ||}| | r|tttfvr9t|tr|}t|t r|d}t#d |dddD}|dkrM|}|t|dd}|f|ddz}n|dkrz| x|t,sL|ddz}|dks|r,| ||s|d|f|ddz}t3|D]\}}|jt6ur ||||dkr]t9|D](\}}|!|||fV||dz }n)t;d|z||}|s |t<vr|||fV||d|zz }d}n|}|t@kr|dkr| dn|dkr| dnj|dkr| dnN|dvrJ| stCd|z|||| "}||krtCd|d|d||||s |t<vr|||fV||dz }|d ddk}|#}||dkr| "nn|dkrSt9|D]\}}|| |nt;d|zn| ||j| d } n||krt;d |z|}n%|| krdStCd!|||fz|||B)"zThis method tokenizes the text and returns the tokens in a generator. Use this method if you just want to tokenize a template. )z   rr<rrr N)variableblockz invalid state_beginr,Tc3K|]}||V dSr^rV)rWgs rZrcz"Lexer.tokeniter..s")S)SQ]!]]]])S)Sr\rBrAr z?%r wanted to resolve the token dynamically but no group matchedrKrLrIrJrGrH)rLrJrHzunexpected '%s'z unexpected 'z ', expected ''rzC%r wanted to resolve the new state dynamically but no group matchedz,%r yielded empty string without stack changezunexpected char %r at %d)$r splitlinesrendswithrrrrermatchrrrprt isinstancergroupsrrrstripcount groupdictrwrqrfindsearch enumeraterrr RuntimeErrorignore_if_emptygrouprr popend) rr'r-rr(linesnewlineposrstack statetokens source_lengthbalancing_stackrnewlines_stripped line_startingregextokens new_statemrHtext strip_signstrippedl_posidxr~rhr}r> expected_oppos2s rZr%zLexer.tokenitersq6""!!##  % & /  ??7++LL$$$E5!!  &1111?111 LL) * * *jr+ F 0 a ,7_ _ (vyKK,,9 #v&#+2(( fe,,Y/XXZZF!&.99J &ay &*)S)SVADqD\)S)S)S%S%S %,,'+{{}}H04S]]__0E0K0KD0Q0Q-&.[6!"":%=FF'#-- 0 <$%KKMM$5$56J$K$K!= %)JJt$4$4q$8E$qyyMy(8'>'>tU'K'K!J.26E6l_vabbz-IF&/&7&722 U ?g55"'%"9"99#j00.7 .F.F " " U#(#4*0#u*<$<$<$<$*ekk$.?.?$?F$)E$5 '3%<>C%D'"'"!"%*$*#;D#:uO'C'C&,eT&9 9 9 9"djj&6&69J&JJF01--32:7799D//3;;+2237777!S[[+2237777!S[[+2237777!_44#2"&9$5$rs  ((((((++++++x||  624(( RZ ( ( BJBBD   RZ & & 2: MBJ    GG[&)))0/////KKbj233GKKK F5MM vh fWoo fWoo F5MM F5MM 6$<< ## 6$<< VF^^ vh  ##vh 6$<< VF^^ F5MM F5MM 6$<< VF^^ F5MM vh  ##vh &%% F5MM fWoo 6,''fWoo y!! VF^^ vh  ##F=))&%%v.//VN++&%%y!! f_--F=))y!! "F#899 &!455 &!455011F=)) VF^^ y!! F5MM      .         )         (   (! "# $         5    :DBBYYy-A-ABBBCC s9~~./////1D///bj SXXUUFF9BSBS,T,T,TUUU U UU   )z=2CD """&,,,&&&***&8&8&8R ? ? ? ? ?f ? ? ?%J%J%J%J%JE%J%J%JP&(ccccc&cccL. @ @ @ @ @U @ @ @EEEEEFEEEEEs3 C C#"C#