Python源代码-更新语法

时间:2018-08-17 19:18:12

标签: python python-3.x python-2.7 grammar cpython

我正在研究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

在对语法和词法分析器进行了更改之后,编译器是否应该将令牌解释为有效,我在哪里出错?

1 个答案:

答案 0 :(得分:2)

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