Staging
v0.5.0
https://github.com/python/cpython
Revision 9932a22897ef9905161dac7476e6976370e13515 authored by Ivan Levkivskyi on 22 January 2019, 11:18:22 UTC, committed by GitHub on 22 January 2019, 11:18:22 UTC
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.
1 parent 7a23680
History
Tip revision: 9932a22897ef9905161dac7476e6976370e13515 authored by Ivan Levkivskyi on 22 January 2019, 11:18:22 UTC
bpo-33416: Add end positions to Python AST (GH-11605)
Tip revision: 9932a22
File Mode Size
.azure-pipelines
.github
Doc
Grammar
Include
Lib
Mac
Misc
Modules
Objects
PC
PCbuild
Parser
Programs
Python
Tools
m4
.gitattributes -rw-r--r-- 1.8 KB
.gitignore -rw-r--r-- 1.6 KB
.travis.yml -rw-r--r-- 7.9 KB
CODE_OF_CONDUCT.md -rw-r--r-- 609 bytes
LICENSE -rw-r--r-- 12.5 KB
Makefile.pre.in -rw-r--r-- 64.4 KB
README.rst -rw-r--r-- 9.8 KB
aclocal.m4 -rw-r--r-- 10.7 KB
config.guess -rwxr-xr-x 43.1 KB
config.sub -rwxr-xr-x 35.4 KB
configure -rwxr-xr-x 486.9 KB
configure.ac -rw-r--r-- 159.3 KB
install-sh -rwxr-xr-x 15.0 KB
pyconfig.h.in -rw-r--r-- 42.8 KB
setup.py -rw-r--r-- 99.2 KB

README.rst

back to top