cpython/Parser
Ivan Levkivskyi 9932a22897
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.
2019-01-22 11:18:22 +00:00
..
acceler.c
asdl.py Add ast.Constant 2016-01-26 00:40:57 +01:00
asdl_c.py bpo-33416: Add end positions to Python AST (GH-11605) 2019-01-22 11:18:22 +00:00
bitset.c
firstsets.c
grammar.c properly free memory in pgen 2016-09-18 18:00:25 -07:00
grammar1.c bpo-31338 (#3374) 2017-09-14 18:13:16 -07:00
listnode.c Use NULL rather than 0. (#778) 2017-03-23 17:53:47 +02:00
metagrammar.c
myreadline.c bpo-35436: Add missing PyErr_NoMemory() calls and other minor bug fixes. (GH-11015) 2018-12-07 12:11:30 +02:00
node.c bpo-33416: Add end positions to Python AST (GH-11605) 2019-01-22 11:18:22 +00:00
parser.c bpo-33416: Add end positions to Python AST (GH-11605) 2019-01-22 11:18:22 +00:00
parser.h bpo-33416: Add end positions to Python AST (GH-11605) 2019-01-22 11:18:22 +00:00
parsetok.c bpo-33416: Add end positions to Python AST (GH-11605) 2019-01-22 11:18:22 +00:00
parsetok_pgen.c
pgen.c properly free memory in pgen 2016-09-18 18:00:25 -07:00
pgenmain.c bpo-35081: Rename internal headers (GH-10275) 2018-11-12 16:53:38 +01:00
printgrammar.c closes bpo-34646: Remove PyAPI_* macros from declarations. (GH-9218) 2018-09-12 12:06:42 -07:00
Python.asdl bpo-33416: Add end positions to Python AST (GH-11605) 2019-01-22 11:18:22 +00:00
token.c bpo-30455: Generate all token related code and docs from Grammar/Tokens. (GH-10370) 2018-12-22 11:18:40 +02:00
tokenizer.c bpo-16806: Fix lineno and col_offset for multi-line string tokens (GH-10021) 2019-01-13 13:05:13 +09:00
tokenizer.h bpo-16806: Fix lineno and col_offset for multi-line string tokens (GH-10021) 2019-01-13 13:05:13 +09:00
tokenizer_pgen.c