aboutsummaryrefslogtreecommitdiff
path: root/Python/ast_opt.c
diff options
context:
space:
mode:
authorIvan Levkivskyi <levkivskyi@gmail.com>2019-01-22 11:18:22 +0000
committerGitHub <noreply@github.com>2019-01-22 11:18:22 +0000
commit9932a22897ef9905161dac7476e6976370e13515 (patch)
tree5cfbec44c7ecb01f4817274280881a74ec15c605 /Python/ast_opt.c
parent7a2368063f25746d4008a74aca0dc0b82f86ff7b (diff)
downloadcpython3-9932a22897ef9905161dac7476e6976370e13515.tar.gz
bpo-33416: Add end positions to Python AST (GH-11605)
The majority of this PR is tediously passing `end_lineno` and `end_col_offset` everywhere. Here are non-trivial points: * It is not possible to reconstruct end positions in AST "on the fly", some information is lost after an AST node is constructed, so we need two more attributes for every AST node `end_lineno` and `end_col_offset`. * I add end position information to both CST and AST. Although it may be technically possible to avoid adding end positions to CST, the code becomes more cumbersome and less efficient. * Since the end position is not known for non-leaf CST nodes while the next token is added, this requires a bit of extra care (see `_PyNode_FinalizeEndPos`). Unless I made some mistake, the algorithm should be linear. * For statements, I "trim" the end position of suites to not include the terminal newlines and dedent (this seems to be what people would expect), for example in ```python class C: pass pass ``` the end line and end column for the class definition is (2, 8). * For `end_col_offset` I use the common Python convention for indexing, for example for `pass` the `end_col_offset` is 4 (not 3), so that `[0:4]` gives one the source code that corresponds to the node. * I added a helper function `ast.get_source_segment()`, to get source text segment corresponding to a given AST node. It is also useful for testing. An (inevitable) downside of this PR is that AST now takes almost 25% more memory. I think however it is probably justified by the benefits.
Diffstat (limited to 'Python/ast_opt.c')
-rw-r--r--Python/ast_opt.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/Python/ast_opt.c b/Python/ast_opt.c
index 6f72a7f63b..96c766fc09 100644
--- a/Python/ast_opt.c
+++ b/Python/ast_opt.c
@@ -439,7 +439,8 @@ astfold_body(asdl_seq *stmts, PyArena *ctx_, int optimize_)
return 0;
}
asdl_seq_SET(values, 0, st->v.Expr.value);
- expr_ty expr = JoinedStr(values, st->lineno, st->col_offset, ctx_);
+ expr_ty expr = JoinedStr(values, st->lineno, st->col_offset,
+ st->end_lineno, st->end_col_offset, ctx_);
if (!expr) {
return 0;
}