diff options
| author | Matthew Suozzo <msuozzo@google.com> | 2021-04-10 16:56:28 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-10 22:56:28 +0200 |
| commit | 75a06f067bd0a2687312e5f8e78f9075be76ad3a (patch) | |
| tree | a0bb72532b0579b5949a00b620c83049ae803f8c /Parser | |
| parent | e05a703848473b0365886dcc593cbddc46609f29 (diff) | |
| download | cpython-git-75a06f067bd0a2687312e5f8e78f9075be76ad3a.tar.gz | |
bpo-43798: Add source location attributes to alias (GH-25324)
* Add source location attributes to alias.
* Move alias star construction to pegen helper.
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Diffstat (limited to 'Parser')
| -rw-r--r-- | Parser/Python.asdl | 1 | ||||
| -rw-r--r-- | Parser/parser.c | 60 | ||||
| -rw-r--r-- | Parser/pegen.c | 6 | ||||
| -rw-r--r-- | Parser/pegen.h | 2 |
4 files changed, 62 insertions, 7 deletions
diff --git a/Parser/Python.asdl b/Parser/Python.asdl index ddc019da48..e224f5f484 100644 --- a/Parser/Python.asdl +++ b/Parser/Python.asdl @@ -124,6 +124,7 @@ module Python -- import name with optional 'as' alias. alias = (identifier name, identifier? asname) + attributes (int lineno, int col_offset, int? end_lineno, int? end_col_offset) withitem = (expr context_expr, expr? optional_vars) diff --git a/Parser/parser.c b/Parser/parser.c index cf8b624ce7..af0c088694 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -3310,6 +3310,15 @@ import_from_targets_rule(Parser *p) } asdl_alias_seq* _res = NULL; int _mark = p->mark; + if (p->mark == p->fill && _PyPegen_fill_token(p) < 0) { + p->error_indicator = 1; + D(p->level--); + return NULL; + } + int _start_lineno = p->tokens[_mark]->lineno; + UNUSED(_start_lineno); // Only used by EXTRA macro + int _start_col_offset = p->tokens[_mark]->col_offset; + UNUSED(_start_col_offset); // Only used by EXTRA macro { // '(' import_from_as_names ','? ')' if (p->error_indicator) { D(p->level--); @@ -3377,7 +3386,16 @@ import_from_targets_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ import_from_targets[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "'*'")); - _res = ( asdl_alias_seq * ) _PyPegen_singleton_seq ( p , CHECK ( alias_ty , _PyPegen_alias_for_star ( p ) ) ); + Token *_token = _PyPegen_get_last_nonnwhitespace_token(p); + if (_token == NULL) { + D(p->level--); + return NULL; + } + int _end_lineno = _token->end_lineno; + UNUSED(_end_lineno); // Only used by EXTRA macro + int _end_col_offset = _token->end_col_offset; + UNUSED(_end_col_offset); // Only used by EXTRA macro + _res = ( asdl_alias_seq * ) _PyPegen_singleton_seq ( p , CHECK ( alias_ty , _PyPegen_alias_for_star ( p , EXTRA ) ) ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; D(p->level--); @@ -3466,6 +3484,15 @@ import_from_as_name_rule(Parser *p) } alias_ty _res = NULL; int _mark = p->mark; + if (p->mark == p->fill && _PyPegen_fill_token(p) < 0) { + p->error_indicator = 1; + D(p->level--); + return NULL; + } + int _start_lineno = p->tokens[_mark]->lineno; + UNUSED(_start_lineno); // Only used by EXTRA macro + int _start_col_offset = p->tokens[_mark]->col_offset; + UNUSED(_start_col_offset); // Only used by EXTRA macro { // NAME ['as' NAME] if (p->error_indicator) { D(p->level--); @@ -3481,7 +3508,16 @@ import_from_as_name_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ import_from_as_name[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "NAME ['as' NAME]")); - _res = _PyAST_alias ( a -> v . Name . id , ( b ) ? ( ( expr_ty ) b ) -> v . Name . id : NULL , p -> arena ); + Token *_token = _PyPegen_get_last_nonnwhitespace_token(p); + if (_token == NULL) { + D(p->level--); + return NULL; + } + int _end_lineno = _token->end_lineno; + UNUSED(_end_lineno); // Only used by EXTRA macro + int _end_col_offset = _token->end_col_offset; + UNUSED(_end_col_offset); // Only used by EXTRA macro + _res = _PyAST_alias ( a -> v . Name . id , ( b ) ? ( ( expr_ty ) b ) -> v . Name . id : NULL , EXTRA ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; D(p->level--); @@ -3551,6 +3587,15 @@ dotted_as_name_rule(Parser *p) } alias_ty _res = NULL; int _mark = p->mark; + if (p->mark == p->fill && _PyPegen_fill_token(p) < 0) { + p->error_indicator = 1; + D(p->level--); + return NULL; + } + int _start_lineno = p->tokens[_mark]->lineno; + UNUSED(_start_lineno); // Only used by EXTRA macro + int _start_col_offset = p->tokens[_mark]->col_offset; + UNUSED(_start_col_offset); // Only used by EXTRA macro { // dotted_name ['as' NAME] if (p->error_indicator) { D(p->level--); @@ -3566,7 +3611,16 @@ dotted_as_name_rule(Parser *p) ) { D(fprintf(stderr, "%*c+ dotted_as_name[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "dotted_name ['as' NAME]")); - _res = _PyAST_alias ( a -> v . Name . id , ( b ) ? ( ( expr_ty ) b ) -> v . Name . id : NULL , p -> arena ); + Token *_token = _PyPegen_get_last_nonnwhitespace_token(p); + if (_token == NULL) { + D(p->level--); + return NULL; + } + int _end_lineno = _token->end_lineno; + UNUSED(_end_lineno); // Only used by EXTRA macro + int _end_col_offset = _token->end_col_offset; + UNUSED(_end_col_offset); // Only used by EXTRA macro + _res = _PyAST_alias ( a -> v . Name . id , ( b ) ? ( ( expr_ty ) b ) -> v . Name . id : NULL , EXTRA ); if (_res == NULL && PyErr_Occurred()) { p->error_indicator = 1; D(p->level--); diff --git a/Parser/pegen.c b/Parser/pegen.c index 57759f7512..f841ace28e 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -1555,8 +1555,8 @@ _PyPegen_seq_count_dots(asdl_seq *seq) /* Creates an alias with '*' as the identifier name */ alias_ty -_PyPegen_alias_for_star(Parser *p) -{ +_PyPegen_alias_for_star(Parser *p, int lineno, int col_offset, int end_lineno, + int end_col_offset, PyArena *arena) { PyObject *str = PyUnicode_InternFromString("*"); if (!str) { return NULL; @@ -1565,7 +1565,7 @@ _PyPegen_alias_for_star(Parser *p) Py_DECREF(str); return NULL; } - return _PyAST_alias(str, NULL, p->arena); + return _PyAST_alias(str, NULL, lineno, col_offset, end_lineno, end_col_offset, arena); } /* Creates a new asdl_seq* with the identifiers of all the names in seq */ diff --git a/Parser/pegen.h b/Parser/pegen.h index 53d8e5221b..f3e0876afb 100644 --- a/Parser/pegen.h +++ b/Parser/pegen.h @@ -237,7 +237,7 @@ asdl_seq *_PyPegen_seq_append_to_end(Parser *, asdl_seq *, void *); asdl_seq *_PyPegen_seq_flatten(Parser *, asdl_seq *); expr_ty _PyPegen_join_names_with_dot(Parser *, expr_ty, expr_ty); int _PyPegen_seq_count_dots(asdl_seq *); -alias_ty _PyPegen_alias_for_star(Parser *); +alias_ty _PyPegen_alias_for_star(Parser *, int, int, int, int, PyArena *); asdl_identifier_seq *_PyPegen_map_names_to_ids(Parser *, asdl_expr_seq *); CmpopExprPair *_PyPegen_cmpop_expr_pair(Parser *, cmpop_ty, expr_ty); asdl_int_seq *_PyPegen_get_cmpops(Parser *p, asdl_seq *); |
