Python 源代码 - 更新语法



我正在研究一些关于Python的源代码,我决定对语法进行一些更改,所以我下载了3.7版本的源代码。

我遵循 PEP 0306:
https://www.python.org/dev/peps/pep-0306/的准则

以Hackernoon为例:
https://hackernoon.com/modifying-the-python-language-in-7-minutes-b94b0a99ce14

这个想法来自装饰器语法的改进(记住,这只是一个研究的例子,我已经知道还有其他方法可以做同样的事情(:

@test
def mydef (self):
pass

它运行良好,遵循语法/语法文件的行:

decorated: decorators (classdef | funcdef | async_funcdef)

现在的目标是更改装饰器以接受声明,从示例开始:

@test
id: int = 1

分析语法,我找到了 annassign,它将是:

annassign: ':' test ['=' test]
# or even use small_stmt

给定表示id:int = 1的令牌,我将令牌更改为:

decorated: decorators (classdef | funcdef | async_funcdef | annassign)

这样做(在 PEP 0306 之后(,我去了ast.c并确定了ast_for_decorated方法,并获得了这段代码:

[...]
assert(TYPE(CHILD(n, 1)) == funcdef ||
TYPE(CHILD(n, 1)) == async_funcdef ||
TYPE(CHILD(n, 1)) == classdef);
if (TYPE(CHILD(n, 1)) == funcdef) {
thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
} else if (TYPE(CHILD(n, 1)) == classdef) {
thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
} else if (TYPE(CHILD(n, 1)) == async_funcdef) {
thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
}
[...]

您可以验证下一个令牌(函数、类或异步(是否存在验证,然后调用负责任的方法 (ast_for(。 所以我根据ast.c进行了更改:

[...]
assert(TYPE(CHILD(n, 1)) == funcdef ||
TYPE(CHILD(n, 1)) == async_funcdef ||
TYPE(CHILD(n, 1)) == annassign ||
TYPE(CHILD(n, 1)) == classdef);
if (TYPE(CHILD(n, 1)) == funcdef) {
thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq);
} else if (TYPE(CHILD(n, 1)) == annassign) {
thing = ast_for_annassign(c, CHILD(n, 1));
} else if (TYPE(CHILD(n, 1)) == classdef) {
thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq);
} else if (TYPE(CHILD(n, 1)) == async_funcdef) {
thing = ast_for_async_funcdef(c, CHILD(n, 1), decorator_seq);
}
[...]

请注意,我创建了ast_for_annassign方法,该方法包含与用于分析的ast_for_expr_stmt中存在的相同验证码:

static stmt_ty 
ast_for_annassign(struct compiling *c, const node *n)
{
REQ(n, expr_stmt);
expr_ty expr1, expr2, expr3;
node *ch = CHILD(n, 0);
node *deep, *ann = CHILD(n, 1);
int simple = 1;
/* we keep track of parens to qualify (x) as expression not name */
deep = ch;
while (NCH(deep) == 1) {
deep = CHILD(deep, 0);
}
if (NCH(deep) > 0 && TYPE(CHILD(deep, 0)) == LPAR) {
simple = 0;
}
expr1 = ast_for_testlist(c, ch);
if (!expr1) {
return NULL;
}
switch (expr1->kind) {
case Name_kind:
if (forbidden_name(c, expr1->v.Name.id, n, 0)) {
return NULL;
}
expr1->v.Name.ctx = Store;
break;
case Attribute_kind:
if (forbidden_name(c, expr1->v.Attribute.attr, n, 1)) {
return NULL;
}
expr1->v.Attribute.ctx = Store;
break;
case Subscript_kind:
expr1->v.Subscript.ctx = Store;
break;
case List_kind:
ast_error(c, ch,
"only single target (not list) can be annotated");
return NULL;
case Tuple_kind:
ast_error(c, ch,
"only single target (not tuple) can be annotated");
return NULL;
default:
ast_error(c, ch,
"illegal target for annotation");
return NULL;
}
if (expr1->kind != Name_kind) {
simple = 0;
}
ch = CHILD(ann, 1);
expr2 = ast_for_expr(c, ch);
if (!expr2) {
return NULL;
}
if (NCH(ann) == 2) {
return AnnAssign(expr1, expr2, NULL, simple,
LINENO(n), n->n_col_offset, c->c_arena);
}
else {
ch = CHILD(ann, 3);
expr3 = ast_for_expr(c, ch);
if (!expr3) {
return NULL;
}
return AnnAssign(expr1, expr2, expr3, simple,
LINENO(n), n->n_col_offset, c->c_arena);
}
}

现在是时候测试(配置/make -j/make install(、python3.7 和:

File "__init__.py", line 13
id: int = 1
^
SyntaxError: invalid syntax

随着语法和词法解析器的更改,编译器是否应该将标记解释为有效,我哪里出错了?

id: int = 1不是annassign: int = 1部分是annassign.(即使是行终止符也不算作annassign的一部分。Python 语法中没有专门用于带注释的赋值语句的非终端;你可能必须写一个。

最新更新