Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 2 | |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 3 | """The Tab Nanny despises ambiguous indentation. She knows no mercy. |
| 4 | |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 5 | tabnanny -- Detection of ambiguous indentation |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 6 | |
| 7 | For the time being this module is intended to be called as a script. |
| 8 | However it is possible to import it into an IDE and use the function |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 9 | check() described below. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 10 | |
| 11 | Warning: The API provided by this module is likely to change in future |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 12 | releases; such changes may not be backward compatible. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 13 | """ |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | aa2a7a4 | 1998-06-09 19:02:21 +0000 | [diff] [blame] | 15 | # Released to the public domain, by Tim Peters, 15 April 1998. |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | dc68833 | 2000-02-23 15:32:19 +0000 | [diff] [blame] | 17 | # XXX Note: this is now a standard library module. |
| 18 | # XXX The API needs to undergo changes however; the current code is too |
| 19 | # XXX script-like. This will be addressed later. |
| 20 | |
Guido van Rossum | a74c556 | 1999-07-30 17:48:20 +0000 | [diff] [blame] | 21 | __version__ = "6" |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 22 | |
| 23 | import os |
| 24 | import sys |
| 25 | import getopt |
| 26 | import tokenize |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 27 | if not hasattr(tokenize, 'NL'): |
| 28 | raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | c5943b1 | 2001-08-07 17:19:25 +0000 | [diff] [blame] | 30 | __all__ = ["check", "NannyNag", "process_tokens"] |
Skip Montanaro | 40fc160 | 2001-03-01 04:27:19 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 32 | verbose = 0 |
Andrew M. Kuchling | dc86a4e | 1998-12-18 13:56:58 +0000 | [diff] [blame] | 33 | filename_only = 0 |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | f9a6d7d | 1998-09-14 16:22:21 +0000 | [diff] [blame] | 35 | def errprint(*args): |
| 36 | sep = "" |
| 37 | for arg in args: |
| 38 | sys.stderr.write(sep + str(arg)) |
| 39 | sep = " " |
| 40 | sys.stderr.write("\n") |
| 41 | |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 42 | def main(): |
Andrew M. Kuchling | dc86a4e | 1998-12-18 13:56:58 +0000 | [diff] [blame] | 43 | global verbose, filename_only |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 44 | try: |
Andrew M. Kuchling | dc86a4e | 1998-12-18 13:56:58 +0000 | [diff] [blame] | 45 | opts, args = getopt.getopt(sys.argv[1:], "qv") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 46 | except getopt.error as msg: |
Guido van Rossum | f9a6d7d | 1998-09-14 16:22:21 +0000 | [diff] [blame] | 47 | errprint(msg) |
Guido van Rossum | 8053d89 | 1998-04-06 14:45:26 +0000 | [diff] [blame] | 48 | return |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 49 | for o, a in opts: |
Andrew M. Kuchling | dc86a4e | 1998-12-18 13:56:58 +0000 | [diff] [blame] | 50 | if o == '-q': |
| 51 | filename_only = filename_only + 1 |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 52 | if o == '-v': |
| 53 | verbose = verbose + 1 |
Guido van Rossum | 8053d89 | 1998-04-06 14:45:26 +0000 | [diff] [blame] | 54 | if not args: |
Guido van Rossum | f9a6d7d | 1998-09-14 16:22:21 +0000 | [diff] [blame] | 55 | errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...") |
Guido van Rossum | 8053d89 | 1998-04-06 14:45:26 +0000 | [diff] [blame] | 56 | return |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 57 | for arg in args: |
| 58 | check(arg) |
| 59 | |
Neal Norwitz | f74e46c | 2002-03-31 13:59:18 +0000 | [diff] [blame] | 60 | class NannyNag(Exception): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 61 | """ |
| 62 | Raised by tokeneater() if detecting an ambiguous indent. |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 63 | Captured and handled in check(). |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 64 | """ |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 65 | def __init__(self, lineno, msg, line): |
| 66 | self.lineno, self.msg, self.line = lineno, msg, line |
| 67 | def get_lineno(self): |
| 68 | return self.lineno |
| 69 | def get_msg(self): |
| 70 | return self.msg |
| 71 | def get_line(self): |
| 72 | return self.line |
| 73 | |
| 74 | def check(file): |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 75 | """check(file_or_dir) |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 76 | |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 77 | If file_or_dir is a directory and not a symbolic link, then recursively |
| 78 | descend the directory tree named by file_or_dir, checking all .py files |
| 79 | along the way. If file_or_dir is an ordinary Python source file, it is |
| 80 | checked for whitespace related problems. The diagnostic messages are |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 81 | written to standard output using the print statement. |
Raymond Hettinger | d1fa3db | 2002-05-15 02:56:03 +0000 | [diff] [blame] | 82 | """ |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 84 | if os.path.isdir(file) and not os.path.islink(file): |
| 85 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 86 | print("%r: listing directory" % (file,)) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 87 | names = os.listdir(file) |
| 88 | for name in names: |
| 89 | fullname = os.path.join(file, name) |
| 90 | if (os.path.isdir(fullname) and |
| 91 | not os.path.islink(fullname) or |
| 92 | os.path.normcase(name[-3:]) == ".py"): |
| 93 | check(fullname) |
| 94 | return |
| 95 | |
| 96 | try: |
Victor Stinner | 58c0752 | 2010-11-09 01:08:59 +0000 | [diff] [blame] | 97 | f = tokenize.open(file) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 98 | except IOError as msg: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 99 | errprint("%r: I/O Error: %s" % (file, msg)) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 100 | return |
| 101 | |
| 102 | if verbose > 1: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 103 | print("checking %r ..." % file) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 105 | try: |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 106 | process_tokens(tokenize.generate_tokens(f.readline)) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 108 | except tokenize.TokenError as msg: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 109 | errprint("%r: Token Error: %s" % (file, msg)) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 110 | return |
| 111 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 112 | except IndentationError as msg: |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 113 | errprint("%r: Indentation Error: %s" % (file, msg)) |
| 114 | return |
| 115 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 116 | except NannyNag as nag: |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 117 | badline = nag.get_lineno() |
| 118 | line = nag.get_line() |
| 119 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 120 | print("%r: *** Line %d: trouble in tab city! ***" % (file, badline)) |
| 121 | print("offending line: %r" % (line,)) |
| 122 | print(nag.get_msg()) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 123 | else: |
Guido van Rossum | a74c556 | 1999-07-30 17:48:20 +0000 | [diff] [blame] | 124 | if ' ' in file: file = '"' + file + '"' |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 125 | if filename_only: print(file) |
| 126 | else: print(file, badline, repr(line)) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 127 | return |
| 128 | |
Ezio Melotti | 103f17e | 2012-11-16 13:17:08 +0200 | [diff] [blame] | 129 | finally: |
| 130 | f.close() |
| 131 | |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 132 | if verbose: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 133 | print("%r: Clean bill of health." % (file,)) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 134 | |
| 135 | class Whitespace: |
| 136 | # the characters used for space and tab |
| 137 | S, T = ' \t' |
| 138 | |
| 139 | # members: |
| 140 | # raw |
| 141 | # the original string |
| 142 | # n |
| 143 | # the number of leading whitespace characters in raw |
| 144 | # nt |
| 145 | # the number of tabs in raw[:n] |
| 146 | # norm |
| 147 | # the normal form as a pair (count, trailing), where: |
| 148 | # count |
| 149 | # a tuple such that raw[:n] contains count[i] |
| 150 | # instances of S * i + T |
| 151 | # trailing |
| 152 | # the number of trailing spaces in raw[:n] |
| 153 | # It's A Theorem that m.indent_level(t) == |
| 154 | # n.indent_level(t) for all t >= 1 iff m.norm == n.norm. |
| 155 | # is_simple |
| 156 | # true iff raw[:n] is of the form (T*)(S*) |
| 157 | |
| 158 | def __init__(self, ws): |
| 159 | self.raw = ws |
| 160 | S, T = Whitespace.S, Whitespace.T |
| 161 | count = [] |
| 162 | b = n = nt = 0 |
| 163 | for ch in self.raw: |
| 164 | if ch == S: |
| 165 | n = n + 1 |
| 166 | b = b + 1 |
| 167 | elif ch == T: |
| 168 | n = n + 1 |
| 169 | nt = nt + 1 |
| 170 | if b >= len(count): |
| 171 | count = count + [0] * (b - len(count) + 1) |
| 172 | count[b] = count[b] + 1 |
| 173 | b = 0 |
| 174 | else: |
| 175 | break |
| 176 | self.n = n |
| 177 | self.nt = nt |
| 178 | self.norm = tuple(count), b |
| 179 | self.is_simple = len(count) <= 1 |
| 180 | |
| 181 | # return length of longest contiguous run of spaces (whether or not |
| 182 | # preceding a tab) |
| 183 | def longest_run_of_spaces(self): |
| 184 | count, trailing = self.norm |
| 185 | return max(len(count)-1, trailing) |
| 186 | |
| 187 | def indent_level(self, tabsize): |
| 188 | # count, il = self.norm |
| 189 | # for i in range(len(count)): |
| 190 | # if count[i]: |
Ezio Melotti | 103f17e | 2012-11-16 13:17:08 +0200 | [diff] [blame] | 191 | # il = il + (i//tabsize + 1)*tabsize * count[i] |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 192 | # return il |
| 193 | |
| 194 | # quicker: |
Ezio Melotti | 103f17e | 2012-11-16 13:17:08 +0200 | [diff] [blame] | 195 | # il = trailing + sum (i//ts + 1)*ts*count[i] = |
| 196 | # trailing + ts * sum (i//ts + 1)*count[i] = |
| 197 | # trailing + ts * sum i//ts*count[i] + count[i] = |
| 198 | # trailing + ts * [(sum i//ts*count[i]) + (sum count[i])] = |
| 199 | # trailing + ts * [(sum i//ts*count[i]) + num_tabs] |
| 200 | # and note that i//ts*count[i] is 0 when i < ts |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 201 | |
| 202 | count, trailing = self.norm |
| 203 | il = 0 |
| 204 | for i in range(tabsize, len(count)): |
Ezio Melotti | 103f17e | 2012-11-16 13:17:08 +0200 | [diff] [blame] | 205 | il = il + i//tabsize * count[i] |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 206 | return trailing + tabsize * (il + self.nt) |
| 207 | |
| 208 | # return true iff self.indent_level(t) == other.indent_level(t) |
| 209 | # for all t >= 1 |
| 210 | def equal(self, other): |
| 211 | return self.norm == other.norm |
| 212 | |
| 213 | # return a list of tuples (ts, i1, i2) such that |
| 214 | # i1 == self.indent_level(ts) != other.indent_level(ts) == i2. |
| 215 | # Intended to be used after not self.equal(other) is known, in which |
| 216 | # case it will return at least one witnessing tab size. |
| 217 | def not_equal_witness(self, other): |
| 218 | n = max(self.longest_run_of_spaces(), |
| 219 | other.longest_run_of_spaces()) + 1 |
| 220 | a = [] |
| 221 | for ts in range(1, n+1): |
| 222 | if self.indent_level(ts) != other.indent_level(ts): |
| 223 | a.append( (ts, |
| 224 | self.indent_level(ts), |
| 225 | other.indent_level(ts)) ) |
| 226 | return a |
| 227 | |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 228 | # Return True iff self.indent_level(t) < other.indent_level(t) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 229 | # for all t >= 1. |
| 230 | # The algorithm is due to Vincent Broman. |
| 231 | # Easy to prove it's correct. |
| 232 | # XXXpost that. |
| 233 | # Trivial to prove n is sharp (consider T vs ST). |
| 234 | # Unknown whether there's a faster general way. I suspected so at |
| 235 | # first, but no longer. |
| 236 | # For the special (but common!) case where M and N are both of the |
| 237 | # form (T*)(S*), M.less(N) iff M.len() < N.len() and |
| 238 | # M.num_tabs() <= N.num_tabs(). Proof is easy but kinda long-winded. |
| 239 | # XXXwrite that up. |
| 240 | # Note that M is of the form (T*)(S*) iff len(M.norm[0]) <= 1. |
| 241 | def less(self, other): |
| 242 | if self.n >= other.n: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 243 | return False |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 244 | if self.is_simple and other.is_simple: |
| 245 | return self.nt <= other.nt |
| 246 | n = max(self.longest_run_of_spaces(), |
| 247 | other.longest_run_of_spaces()) + 1 |
| 248 | # the self.n >= other.n test already did it for ts=1 |
| 249 | for ts in range(2, n+1): |
| 250 | if self.indent_level(ts) >= other.indent_level(ts): |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 251 | return False |
| 252 | return True |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 253 | |
| 254 | # return a list of tuples (ts, i1, i2) such that |
| 255 | # i1 == self.indent_level(ts) >= other.indent_level(ts) == i2. |
| 256 | # Intended to be used after not self.less(other) is known, in which |
| 257 | # case it will return at least one witnessing tab size. |
| 258 | def not_less_witness(self, other): |
| 259 | n = max(self.longest_run_of_spaces(), |
| 260 | other.longest_run_of_spaces()) + 1 |
| 261 | a = [] |
| 262 | for ts in range(1, n+1): |
| 263 | if self.indent_level(ts) >= other.indent_level(ts): |
| 264 | a.append( (ts, |
| 265 | self.indent_level(ts), |
| 266 | other.indent_level(ts)) ) |
| 267 | return a |
| 268 | |
| 269 | def format_witnesses(w): |
Georg Brandl | cbd2ab1 | 2010-12-04 10:39:14 +0000 | [diff] [blame] | 270 | firsts = (str(tup[0]) for tup in w) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 271 | prefix = "at tab size" |
| 272 | if len(w) > 1: |
| 273 | prefix = prefix + "s" |
Neal Norwitz | 7ce734c | 2002-05-31 14:13:04 +0000 | [diff] [blame] | 274 | return prefix + " " + ', '.join(firsts) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 275 | |
Tim Peters | 4efb6e9 | 2001-06-29 23:51:08 +0000 | [diff] [blame] | 276 | def process_tokens(tokens): |
| 277 | INDENT = tokenize.INDENT |
| 278 | DEDENT = tokenize.DEDENT |
| 279 | NEWLINE = tokenize.NEWLINE |
| 280 | JUNK = tokenize.COMMENT, tokenize.NL |
Tim Peters | 5ca576e | 2001-06-18 22:08:13 +0000 | [diff] [blame] | 281 | indents = [Whitespace("")] |
| 282 | check_equal = 0 |
| 283 | |
| 284 | for (type, token, start, end, line) in tokens: |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 285 | if type == NEWLINE: |
| 286 | # a program statement, or ENDMARKER, will eventually follow, |
| 287 | # after some (possibly empty) run of tokens of the form |
| 288 | # (NL | COMMENT)* (INDENT | DEDENT+)? |
| 289 | # If an INDENT appears, setting check_equal is wrong, and will |
| 290 | # be undone when we see the INDENT. |
| 291 | check_equal = 1 |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 292 | |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 293 | elif type == INDENT: |
| 294 | check_equal = 0 |
| 295 | thisguy = Whitespace(token) |
| 296 | if not indents[-1].less(thisguy): |
| 297 | witness = indents[-1].not_less_witness(thisguy) |
| 298 | msg = "indent not greater e.g. " + format_witnesses(witness) |
| 299 | raise NannyNag(start[0], msg, line) |
| 300 | indents.append(thisguy) |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 301 | |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 302 | elif type == DEDENT: |
| 303 | # there's nothing we need to check here! what's important is |
| 304 | # that when the run of DEDENTs ends, the indentation of the |
| 305 | # program statement (or ENDMARKER) that triggered the run is |
| 306 | # equal to what's left at the top of the indents stack |
Guido van Rossum | aa2a7a4 | 1998-06-09 19:02:21 +0000 | [diff] [blame] | 307 | |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 308 | # Ouch! This assert triggers if the last line of the source |
| 309 | # is indented *and* lacks a newline -- then DEDENTs pop out |
| 310 | # of thin air. |
| 311 | # assert check_equal # else no earlier NEWLINE, or an earlier INDENT |
| 312 | check_equal = 1 |
Guido van Rossum | aa2a7a4 | 1998-06-09 19:02:21 +0000 | [diff] [blame] | 313 | |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 314 | del indents[-1] |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 315 | |
Tim Peters | b90f89a | 2001-01-15 03:26:36 +0000 | [diff] [blame] | 316 | elif check_equal and type not in JUNK: |
| 317 | # this is the first "real token" following a NEWLINE, so it |
| 318 | # must be the first token of the next program statement, or an |
| 319 | # ENDMARKER; the "line" argument exposes the leading whitespace |
| 320 | # for this statement; in the case of ENDMARKER, line is an empty |
| 321 | # string, so will properly match the empty string with which the |
| 322 | # "indents" stack was seeded |
| 323 | check_equal = 0 |
| 324 | thisguy = Whitespace(line) |
| 325 | if not indents[-1].equal(thisguy): |
| 326 | witness = indents[-1].not_equal_witness(thisguy) |
| 327 | msg = "indent not equal e.g. " + format_witnesses(witness) |
| 328 | raise NannyNag(start[0], msg, line) |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 329 | |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 330 | |
| 331 | if __name__ == '__main__': |
| 332 | main() |