How to get lineno of "end-of-statement" in Python ast
<p>I am trying to work on a script that manipulates another script in Python, the script to be modified has structure like:</p>

<pre><code>class SomethingRecord(Record):
    description = 'This records something'
    author = 'john smith'
</code></pre>

<p>I use <code>ast</code> to locate the <code>description</code> line number, and I use some code to change the original file with new description string base on the line number. So far so good.</p>

<p>Now the only issue is <code>description</code> occasionally is a multi-line string, e.g.</p>

<pre><code>    description = ('line 1'
                   'line 2'
                   'line 3')
</code></pre>

<p>or</p>

<pre><code>    description = 'line 1' \
        'line 2' \
        'line 3'
</code></pre>

<p>and I only have the line number of the first line, not the following lines. So my one-line replacer would do</p>

<pre><code>    description = 'new value'
        'line 2' \
        'line 3'
</code></pre>

<p>and the code is broken. I figured that if I know both the lineno of start and end/number of lines of <code>description</code> assignment I could repair my code to handle such situation. How do I get such information with Python standard library?</p>

<p>I looked at the other answers; it appears people are doing backflips to get around the problems of computing line numbers, when your real problem is one of modifying the code.   That suggests the baseline machinery is not helping you the way you really need.</p>

<p>If you use a <a href="https://en.wikipedia.org/wiki/Program_transformation">program transformation system (PTS)</a>, you could avoid a lot of this nonsense.</p>

<p>A good PTS will parse your source code to an AST, and then let you apply source-level rewrite rules to modify the AST, and will finally convert the modified AST back into source text.   Generically PTSes accept transformation rules of essentially this form:</p>

<pre><code>   if you see *this*, replace it by *that*
</code></pre>

<p>[A parser that builds an AST is NOT a PTS.  They don't allow rules like this; you can write ad hoc code to hack at the tree, but that's usually pretty awkward.   Not do they do the AST to source text regeneration.]</p>

<p>(My PTS, see bio, called) DMS is a PTS that could accomplish this.  OP's specific example would be accomplished easily by using the following rewrite rule:</p>

<pre><code> source domain Python; -- tell DMS the syntax of pattern left hand sides
 target domain Python; -- tell DMS the syntax of pattern right hand sides

 rule replace_description(e: expression): statement -&gt; statement =
     " description = \e "
  -&gt;
     " description = ('line 1'
                      'line 2'
                      'line 3')";
</code></pre>

<p>The one transformation rule is given an name <em>replace_description</em> to distinguish it from all the other rule we might define.   The rule parameters (e: expression) indicate the pattern will allow an arbitrary expression as defined by the source language.   <em>statement->statement</em> means the rule maps a statement in the source language, to a statement in the target language; we could use any other syntax category from the Python grammar provided to DMS.  The <strong>"</strong> used here is a <em>metaquote</em>, used to distinguish the syntax of the rule language form the syntax of the subject language.  The second <strong>-></strong>  separates the source pattern <em>this</em> from the target pattern <em>that</em>.</p>

<p>You'll notice that there is no need to mention line numbers.  The PTS converts the rule surface syntax into corresponding ASTs by actually parsing the patterns with the same parser used to parse the source file.  The ASTs produced for the patterns are used to effect the pattern match/replacement.   Because this is driven from ASTs, the actual layout of the orginal code (spacing, linebreaks, comments) don't affect DMS's ability to match or replace.  Comments aren't a problem for matching because they are attached to tree nodes rather than being tree nodes; they are preserved in the transformed program.  DMS does capture line and precise column information for all tree elements; just not needed to implement transformations. Code layout is also preserved in the output by DMS, using that line/column information. </p>

<p>Other PTSes offer generally similar capabilities.</p>

python abstract-syntax-tree