Expect a given token type and return it. This accepts the same argument as :meth:`jinja2.lexer.Token.test`.
(self, expr: str)
| 401 | self.closed = True |
| 402 | |
| 403 | def expect(self, expr: str) -> Token: |
| 404 | """Expect a given token type and return it. This accepts the same |
| 405 | argument as :meth:`jinja2.lexer.Token.test`. |
| 406 | """ |
| 407 | if not self.current.test(expr): |
| 408 | expr = describe_token_expr(expr) |
| 409 | |
| 410 | if self.current.type is TOKEN_EOF: |
| 411 | raise TemplateSyntaxError( |
| 412 | f"unexpected end of template, expected {expr!r}.", |
| 413 | self.current.lineno, |
| 414 | self.name, |
| 415 | self.filename, |
| 416 | ) |
| 417 | |
| 418 | raise TemplateSyntaxError( |
| 419 | f"expected token {expr!r}, got {describe_token(self.current)!r}", |
| 420 | self.current.lineno, |
| 421 | self.name, |
| 422 | self.filename, |
| 423 | ) |
| 424 | |
| 425 | return next(self) |
| 426 | |
| 427 | |
| 428 | def get_lexer(environment: "Environment") -> "Lexer": |