笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册

扩展

发布者: 笨鸟自学网



解析器 API

传递到 Extension.parse() 的解析器提供解析不同类型表达式的方式。下 面的方法可能会在扩展中使用:

classjinja2.parser.Parser(environmentsourcename=Nonefilename=Nonestate=None)

This is the central parsing class Jinja2 uses. It’s passed to extensions and can be used to parse expressions or statements.

filename

解析器处理的模板文件名。这 不是 模板的加载名。加载名见 name 。对于不是从文件系统中加载的模板,这个值为 None 。

name

模板的加载名。

stream

当前的 TokenStream 

fail(msglineno=Noneexc=<class 'jinja2.exceptions.TemplateSyntaxError'>)

Convenience method that raises exc with the message, passed line number or last line number as well as the current name and filename.

free_identifier(lineno=None)

Return a new free identifier as InternalName.

parse_assign_target(with_tuple=Truename_only=Falseextra_end_rules=None)

Parse an assignment target. As Jinja2 allows assignments to tuples, this function can parse all allowed assignment targets. Per default assignments to tuples are parsed, that can be disable however by setting with_tuple to False. If only assignments to names are wanted name_only can be set to True. The extra_end_rules parameter is forwarded to the tuple parsing function.

parse_expression(with_condexpr=True)

Parse an expression. Per default all expressions are parsed, if the optional with_condexpr parameter is set to False conditional expressions are not parsed.

parse_statements(end_tokensdrop_needle=False)

Parse multiple statements into a list until one of the end tokens is reached. This is used to parse the body of statements as it also parses template data if appropriate. The parser checks first if the current token is a colon and skips it if there is one. Then it checks for the block end and parses until if one of the end_tokens is reached. Per default the active token in the stream at the end of the call is the matched end token. If this is not wanted drop_needle can be set to True and the end token is removed.

parse_tuple(simplified=Falsewith_condexpr=Trueextra_end_rules=Noneexplicit_parentheses=False)

Works like parse_expression but if multiple expressions are delimited by a comma a Tuple node is created. This method could also return a regular expression instead of a tuple if no commas where found.

The default parsing mode is a full tuple. If simplified is True only names and literals are parsed. The no_condexpr parameter is forwarded to parse_expression().

Because tuples do not require delimiters and may end in a bogus comma an extra hint is needed that marks the end of a tuple. For example for loops support tuples between for and in. In that case the extra_end_rules is set to ['name:in'].

explicit_parentheses is true if the parsing was triggered by an expression in parentheses. This is used to figure out if an empty tuple is a valid expression or not.

classjinja2.lexer.TokenStream(generatornamefilename)

A token stream is an iterable that yields Tokens. The parser however does not iterate over it but calls next() to go one token ahead. The current active token is stored as current.

current

当前的 Token 

eos

Are we at the end of the stream?

expect(expr)

Expect a given token type and return it. This accepts the same argument as jinja2.lexer.Token.test().

look()

Look at the next token.

next()

Go one token ahead and return the old one

next_if(expr)

Perform the token test and return the token if it matched. Otherwise the return value is None.

push(token)

Push a token back to the stream.

skip(n=1)

Got n tokens ahead.

skip_if(expr)

Like next_if() but only returns True or False.

classjinja2.lexer.Token

Token class.

lineno

token 的行号。

type

token 的类型。这个值是被禁锢的,所以你可以用 is 运算符同任意字符 串比较。

value

token 的值。

test(expr)

Test 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.

test_any(*iterable)

Test against multiple token expressions.

同样,在词法分析模块中也有一个实用函数可以计算字符串中的换行符数目:

.. autofunction:: jinja2.lexer.count_newlines

AST

AST(抽象语法树: Abstract Syntax Tree)用于表示解析后的模板。它有编译器之后 转换到可执行的 Python 代码对象的节点构建。提供自定义语句的扩展可以返回执行自 定义 Python 代码的节点。

下面的清单展示了所有当前可用的节点。 AST 在 Jinja2 的各个版本中有差异,但会向 后兼容。

更多信息请见 jinja2.Environment.parse() 

classjinja2.nodes.Node

Baseclass for all Jinja2 nodes. There are a number of nodes available of different types. There are four major types:

  • Stmt: statements
  • Expr: expressions
  • Helper: helper nodes
  • Template: the outermost wrapper node

All nodes have fields and attributes. Fields may be other nodes, lists, or arbitrary values. Fields are passed to the constructor as regular positional arguments, attributes as keyword arguments. Each node has two attributes: lineno (the line number of the node) and environment. The environment attribute is set at the end of the parsing process for all nodes automatically.

find(node_type)

Find the first node of a given type. If no such node exists the return value is None.

上一篇:模板设计者文档下一篇:集成

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-12-22 13:53 , Processed in 0.029927 second(s), 17 queries .

© 2001-2020