I'm not sure what kind of source analyze tool you're using, so I can only propose a suggestion. However, it would be too long for a comment, so I wrote a proof-of-concept script.
The idea is to look at the source code with Python's tokenize
module, which generates tokens from Python expressions. If well-formed Python code contains implicitly continued string literals, it will show up as a STRING
token followed by NL
.
For example, let's use the following source file source.py
as a test case.
x = ("a"
"b" # some trailing spaces
# Coment line
"c"
""
# The following is an explicit continuation
"d" \
"e")
Running the command python check.py < source.py
on the file generates:
1:8: implicit continuation:
x = ("a"
~~~^
2:35: implicit continuation:
"b" # some trailing spaces
~~~^
4:3: implicit continuation:
"c"
~~~^
5:2: implicit continuation:
""
^
The program, check.py
, is just a proof-of-concept and it does not check syntax errors or other edge cases:
import sys
import tokenize
LOOKNEXT = False
tok_gen = tokenize.generate_tokens(sys.stdin.readline)
for tok, tok_str, start, end, line_text in tok_gen:
if tok == tokenize.STRING:
LOOKNEXT = True
continue
if LOOKNEXT and (tok == tokenize.NL):
warn_header = "%d:%d: implicit continuation: " % start
print >> sys.stderr, warn_header
print >> sys.stderr, line_text
indents = start[1] - 3
if indents >= 0:
print >> sys.stderr, "%s~~~^" % (" " * indents)
else:
print >> sys.stderr, "%s^" % (" " * start[1])
LOOKNEXT = False
Hopefully the idea might help you extend your lint tool or IDE for your purpose.