U at@sUdZddlZddlZddlmZddlmZddlm Z ddl m Z ddl mZdd lmZejrxddlZdd lmZed Zejejd fed <edZedZedejZedejej BZ!edejej BZ"e dZ#e dZ$e dZ%e dZ&e dZ'e dZ(e dZ)e dZ*e dZ+e dZ,e dZ-e dZ.e dZ/e d Z0e d!Z1e d"Z2e d#Z3e d$Z4e d%Z5e d&Z6e d'Z7e d(Z8e d)Z9e d*Z:e d+Z;e d,Ze d/Z?e d0Z@e d1ZAe d2ZBe d3ZCe d4ZDe d5ZEe d6ZFe d7ZGe d8ZHe d9ZIe d:ZJe d;ZKe d<ZLe d=ZMe d>ZNe d?ZOe d@ZPe dAZQe dBZRe dCZSe#e;e'e*e3e2e6ez>=srAzoperators droppedr0r9ccs|]}t|VqdSN)reescaper=xr<r<r@ srGcCs t| SrB)lenrFr<r<r@rJ)keyr1) token_typereturncCsL|tkrt|Stdtdtdtdtdtdtdtdt dt d t d t d i ||S) Nzbegin of commentzend of commentr$zbegin 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)rMr<r<r@_describe_token_types< r]TokentokenrNcCs|jtkr|jSt|jS)z#Returns a description of the token.)type TOKEN_NAMEvaluer])r`r<r<r@describe_tokens rdexprrNcCs2d|kr&|dd\}}|tkr*|Sn|}t|S)z0Like `describe_token` but for token expressions.r8r)splitrbr])rfrarcr<r<r@describe_token_exprs rhrcrNcCstt|S)zsCount the number of newline characters in the string. This is useful for extensions that filter a stream. )rH newline_refindall)rcr<r<r@count_newlinessrlr  environmentrNcCstj}t|jt||jft|jt||jft|jt||jfg}|j dk rp| t|j t d||j f|j dk r| t|j t d||j fddt|ddDS)zACompiles all the rules from the environment into a list of rules.Nz ^[ \t\v]*z(?:^|(?<=\S))[^\S\r\n]*cSsg|]}|ddqS)rNr<rEr<r<r@ sz!compile_rules..T)reverse)rCrDrHcomment_start_stringrPblock_start_stringrTvariable_start_stringrVline_statement_prefixappendrXline_comment_prefixTOKEN_LINECOMMENT_BEGINsorted)rnerulesr<r<r@ compile_ruless:    r{c@s>eZdZdZefeejeddddZe edddd Z dS) FailurezjClass that raises a `TemplateSyntaxError` if called. Used by the `Lexer` to specify known errors. N)messageclsrNcCs||_||_dSrB)r} error_class)selfr}r~r<r<r@__init__szFailure.__init__z te.NoReturn)linenofilenamerNcCs||j||dSrB)rr})rrrr<r<r@__call__szFailure.__call__) __name__ __module__ __qualname____doc__rstrtTyperintrr<r<r<r@r|s r|c@sTeZdZUeed<eed<eed<edddZeeddd Zeed d d Z d S)r^rrarcrNcCst|SrB)rdrr<r<r@__str__sz Token.__str__recCs2|j|krdSd|kr.|dd|j|jgkSdS)zTest a token against a token expression. This can either be a token type or ``'token_type:token_value'``. This can only test against string values and types. Tr8rF)rargrcrrfr<r<r@tests  z Token.test)iterablerNcstfdd|DS)z(Test against multiple token expressions.c3s|]}|VqdSrB)r)r=rfrr<r@rG$sz!Token.test_any..)any)rrr<rr@test_any"szToken.test_anyN) rrrr__annotations__rrboolrrr<r<r<r@r^ s c@s<eZdZdZdddddZdddd Zedd d ZdS) TokenStreamIteratorz`The iterator for tokenstreams. Iterate over the stream until the eof token is reached. TokenStreamN)streamrNcCs ||_dSrB)r)rrr<r<r@r,szTokenStreamIterator.__init__rcCs|SrBr<rr<r<r@__iter__/szTokenStreamIterator.__iter__cCs.|jj}|jtkr |jtt|j|SrB)rcurrentrar[close StopIterationnextrr`r<r<r@__next__2s    zTokenStreamIterator.__next__)rrrrrrr^rr<r<r<r@r'src@seZdZdZejeejeejedddZ e dddZ e ddd Z ee dd d Zed d ddZedddZd!ed dddZeejedddZee dddZedddZd dddZeeddd Zd S)"rzA token stream is an iterable that yields :class:`Token`\s. The parser however does not iterate over it but calls :meth:`next` to go one token ahead. The current active token is stored as :attr:`current`. ) generatorr!rcCs>t||_t|_||_||_d|_tdtd|_ t |dS)NFr) iter_iterr_pushedr!rclosedr^ TOKEN_INITIALrr)rrr!rr<r<r@rCs zTokenStream.__init__rcCst|SrB)rrr<r<r@rQszTokenStream.__iter__cCst|jp|jjtk SrB)rrrrar[rr<r<r@__bool__TszTokenStream.__bool__cCs| S)z Are we at the end of the stream?r<rr<r<r@eosWszTokenStream.eosNr_cCs|j|dS)z Push a token back to the stream.N)rrurr<r<r@push\szTokenStream.pushcCs"t|}|j}||||_|S)zLook at the next token.)rrr)rZ old_tokenresultr<r<r@look`s  zTokenStream.lookr)nrNcCst|D] }t|qdS)zGot n tokens ahead.N)ranger)rr_r<r<r@skiphs zTokenStream.skiprecCs|j|rt|SdS)zqPerform the token test and return the token if it matched. Otherwise the return value is `None`. N)rrrrr<r<r@next_ifms zTokenStream.next_ifcCs||dk S)z8Like :meth:`next_if` but only returns `True` or `False`.N)rrr<r<r@skip_ifvszTokenStream.skip_ifcCsX|j}|jr|j|_n:|jjtk rTzt|j|_Wntk rR|YnX|S)z|Go one token ahead and return the old one. Use the built-in :func:`next` instead of calling this directly. ) rrpopleftrar[rrrr)rrvr<r<r@rzs zTokenStream.__next__cCs&t|jjtd|_td|_d|_dS)zClose the stream.rr<TN)r^rrr[rrrrr<r<r@rs zTokenStream.closecCsn|j|sft|}|jjtkr>td|d|jj|j|jtd|dt |j|jj|j|jt |S)z}Expect a given token type and return it. This accepts the same argument as :meth:`jinja2.lexer.Token.test`. z%unexpected end of template, expected r7zexpected token z, got ) rrrhrar[rrr!rrdrrr<r<r@expects    zTokenStream.expect)r)rrrrrIterabler^Optionalrrrrrrpropertyrrrrrrrrrrr<r<r<r@r=s"  rc CsZ|j|j|j|j|j|j|j|j|j|j |j |j f }t |}|dkrVt|t |<}|S)z(Return a lexer which is probably cached.N)rrblock_end_stringrsvariable_end_stringrqcomment_end_stringrtrv trim_blocks lstrip_blocksnewline_sequencekeep_trailing_newliner r\r )rnrLlexerr<r<r@ get_lexers" rcs$eZdZdZdZfddZZS)OptionalLStripzWA special tuple for marking a point in the state that can have lstrip applied. r<cst||SrB)super__new__)r~memberskwargs __class__r<r@rszOptionalLStrip.__new__)rrrr __slots__r __classcell__r<r<rr@rsrc@sNeZdZUejeed<ejeejedfeje fed<ej eed<dS)_Ruler.tokenscommandN) rrrrPatternrrUnionTupler|rr<r<r<r@rs $rc @seZdZdZdddddZeeddd Zdeejeejeejee d d d Z dej ej e eefejeejeejed ddZdeejeejeejeejej e eefd ddZdS)r a 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. r Nrmc Cs0tj}ttjtddd}tttdttt dtt t dtt t dtttdtttdg}t|}||j}||j}||j}||j} |jrdnd} |jr|dnd|_|j|_|j|_d|d|d |d } d | gd d |D} dt|d| dttddt|dtdgtt|d|d|d || d t t!fdt|dt"dfdgt#t|d|d|d || dt$dg|t%t|d| d | t&dg|t't|d|d|d|d || d ttt(dt|dt"dfdgt)t|dt*dg|t+t|dt,t-fdgi|_.dS)N)rFrNcSst|tjtjBSrB)rCcompileMSrIr<r<r@cszLexer.__init__..cz\n?rz[^ \t]z(?Pz(\-|\+|)\s*raw\s*(?:\-z\s*|z))r9cSs"g|]\}}d|d|dqS)z(?P.rootz(.*?)(?:r1#bygroupz.+z (.*?)((?:\+z|\-#popz(.)zMissing end of comment tagz(?:\+z\-z (.*?)((?:z(\-|\+|))\s*endraw\s*(?:\+zMissing end of raw directivez \s*(\n|$)z(.*?)()(?=\n|$))/rCrDrrrr whitespace_reTOKEN_WHITESPACEfloat_re TOKEN_FLOAT integer_re TOKEN_INTEGERname_rerb string_re TOKEN_STRING operator_reTOKEN_OPERATORr{rrrrrrrlstrip_unless_rerrjoinrrZrPrRrQr|rTrUrVrWTOKEN_RAW_BEGIN TOKEN_RAW_ENDrXrYrwrSTOKEN_LINECOMMENT_ENDrz) rrnryrZ tag_rulesZroot_tag_rulesZblock_start_reZ block_end_reZcomment_end_reZvariable_end_reZblock_suffix_reZ root_raw_reZ root_parts_rer<r<r@rs                  zLexer.__init__ricCst|j|S)z`Replace all newlines with the configured sequence in strings and template data. )rjrr)rrcr<r<r@_normalize_newlinesVszLexer._normalize_newlines)sourcer!rstaterNcCs&|||||}t||||||S)z:Calls tokeniter + tokenize and wraps it in a token stream.) tokeniterrwrap)rrr!rrrr<r<r@tokenize\szLexer.tokenize)rr!rrNc csp|D]d\}}}|tkrq|}|tkr.t}n.|tkr>t}n|ttfkrPqn |tkrd||}n|dkrr|}n|t kr|}| st d|||n|t krz$||dd ddd}WnHtk r }z(t|dd} t | |||W5d }~XYnXnL|tkr.t|d d d }n.|tkrJt|d d }n|tkr\t|}t|||Vqd 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-escaper8Nrrr)ignored_tokensrXrTrYrUrrrZrrb isidentifierrrencodedecode Exceptionrrgstriprrreplacerrr operatorsr^) rrr!rrr`Z value_strrcrymsgr<r<r@rgsT    "   z Lexer.wrapccsHt|ddd}|js,|ddkr,|d=d|}d}d}dg}|dk rr|dkrr|d ksdtd ||d |j|d} t|} g} |j} d} d }| D]t\}}}| ||}|dkrq| r|t t t fkrqt |tr|}t |tr|d}td d|dddD}|dkrT|}|t|dd} |f|dd}nj|dkr| dk r|ts|dd}|dks|r| ||s|d|f|dd}t|D]\}}|jtkr|||n|dkrB|D]4\}}|dk r|||fV||d7}q|qt|dn:||}|sZ|tkrf|||fV||d| 7}d} qn|}|t kr*|dkr| dn|dkr| dnj|dkr| dnT|dkr*| st!d|d|||| "}||kr*t!d|d|d||||s:|tkrF|||fV||d7}|dddk}|#}|dk r|dkr|"nV|dkr|D]$\}}|dk r||qqt|dn |||j|d} n||kr t|d|}qq|| kr$dSt!d ||d!||||qdS)"aThis method tokenizes the text and returns the tokens in a generator. Use this method if you just want to tokenize a template. .. versionchanged:: 3.0 Only ``\n``, ``\r\n`` and ``\r`` are treated as line breaks. Nrr rrr)variableblockz invalid stateZ_beginTcss|]}|dk r|VqdSrBr<)r=gr<r<r@rGsz"Lexer.tokeniter..r)r(rz= wanted to resolve the token dynamically but no group matchedr2r3r0r1r.r/)r3r1r/z unexpected ''z ', expected 'rzA wanted to resolve the new state dynamically but no group matchedz* yielded empty string without stack changezunexpected char z at )$rjrgrrAssertionErrorrurzrHrmatchrWrUrY isinstancetuplegroupsrrrstripcount groupdictr\rVrfindsearch enumeraterr|items RuntimeErrorignore_if_emptygrouprrpopend)rrr!rrlinesposrstackZ statetokensZ source_lengthZbalancing_stackrZnewlines_strippedZ line_startingregexrZ new_statemrtextZ strip_signstrippedZl_posidxr`rLrcr%Z expected_oppos2r<r<r@rs                                    zLexer.tokeniter)NNN)NN)NN)rrrrrrrrrrrrrrIteratorr^rrr<r<r<r@r s<y   :)qrrCtypingrastr collectionsrsysrZ _identifierrr exceptionsrutilsr TYPE_CHECKINGZtyping_extensionsternr r MutableMappingrrrrrjrr IGNORECASEVERBOSErrZ TOKEN_ADDZ TOKEN_ASSIGNZ TOKEN_COLONZ TOKEN_COMMAZ TOKEN_DIVZ TOKEN_DOTZTOKEN_EQZTOKEN_FLOORDIVZTOKEN_GTZ TOKEN_GTEQZ TOKEN_LBRACEZTOKEN_LBRACKETZ TOKEN_LPARENZTOKEN_LTZ TOKEN_LTEQZ TOKEN_MODZ TOKEN_MULZTOKEN_NEZ TOKEN_PIPEZ TOKEN_POWZ TOKEN_RBRACEZTOKEN_RBRACKETZ TOKEN_RPARENZTOKEN_SEMICOLONZ TOKEN_SUBZ TOKEN_TILDErrrrbrrrTrUrVrWrrrPrQrRrXrYrwrrSrZrr[rr rOrHrrrxr frozensetrrrr]rdrhrrlListr{r| NamedTupler^rrrrrrr r<r<r<r@s            &    )m