Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 3 | """The Tab Nanny despises ambiguous indentation. She knows no mercy.""" |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | ce73acf | 1998-04-10 19:14:59 +0000 | [diff] [blame] | 5 | # Released to the public domain, by Tim Peters, 6 April 1998. |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | ce73acf | 1998-04-10 19:14:59 +0000 | [diff] [blame] | 7 | __version__ = "4" |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 8 | |
| 9 | import os |
| 10 | import sys |
| 11 | import getopt |
| 12 | import tokenize |
| 13 | |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 14 | verbose = 0 |
| 15 | |
| 16 | def main(): |
| 17 | global verbose |
| 18 | try: |
| 19 | opts, args = getopt.getopt(sys.argv[1:], "v") |
| 20 | except getopt.error, msg: |
| 21 | print msg |
Guido van Rossum | 8053d89 | 1998-04-06 14:45:26 +0000 | [diff] [blame] | 22 | return |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 23 | for o, a in opts: |
| 24 | if o == '-v': |
| 25 | verbose = verbose + 1 |
Guido van Rossum | 8053d89 | 1998-04-06 14:45:26 +0000 | [diff] [blame] | 26 | if not args: |
Guido van Rossum | ce73acf | 1998-04-10 19:14:59 +0000 | [diff] [blame] | 27 | print "Usage:", sys.argv[0], "[-v] file_or_directory ..." |
Guido van Rossum | 8053d89 | 1998-04-06 14:45:26 +0000 | [diff] [blame] | 28 | return |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 29 | for arg in args: |
| 30 | check(arg) |
| 31 | |
| 32 | class NannyNag: |
| 33 | def __init__(self, lineno, msg, line): |
| 34 | self.lineno, self.msg, self.line = lineno, msg, line |
| 35 | def get_lineno(self): |
| 36 | return self.lineno |
| 37 | def get_msg(self): |
| 38 | return self.msg |
| 39 | def get_line(self): |
| 40 | return self.line |
| 41 | |
| 42 | def check(file): |
| 43 | if os.path.isdir(file) and not os.path.islink(file): |
| 44 | if verbose: |
| 45 | print "%s: listing directory" % `file` |
| 46 | names = os.listdir(file) |
| 47 | for name in names: |
| 48 | fullname = os.path.join(file, name) |
| 49 | if (os.path.isdir(fullname) and |
| 50 | not os.path.islink(fullname) or |
| 51 | os.path.normcase(name[-3:]) == ".py"): |
| 52 | check(fullname) |
| 53 | return |
| 54 | |
| 55 | try: |
| 56 | f = open(file) |
| 57 | except IOError, msg: |
| 58 | print "%s: I/O Error: %s" % (`file`, str(msg)) |
| 59 | return |
| 60 | |
| 61 | if verbose > 1: |
| 62 | print "checking", `file`, "..." |
| 63 | |
| 64 | reset_globals() |
| 65 | try: |
| 66 | tokenize.tokenize(f.readline, tokeneater) |
| 67 | |
| 68 | except tokenize.TokenError, msg: |
Guido van Rossum | 53117ae | 1998-04-28 16:01:13 +0000 | [diff] [blame] | 69 | print "%s: Token Error: %s" % (`file`, str(msg)) |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 70 | return |
| 71 | |
| 72 | except NannyNag, nag: |
| 73 | badline = nag.get_lineno() |
| 74 | line = nag.get_line() |
| 75 | if verbose: |
| 76 | print "%s: *** Line %d: trouble in tab city! ***" % ( |
| 77 | `file`, badline) |
| 78 | print "offending line:", `line` |
| 79 | print nag.get_msg() |
| 80 | else: |
| 81 | print file, badline, `line` |
| 82 | return |
| 83 | |
| 84 | if verbose: |
| 85 | print "%s: Clean bill of health." % `file` |
| 86 | |
| 87 | class Whitespace: |
| 88 | # the characters used for space and tab |
| 89 | S, T = ' \t' |
| 90 | |
| 91 | # members: |
| 92 | # raw |
| 93 | # the original string |
| 94 | # n |
| 95 | # the number of leading whitespace characters in raw |
| 96 | # nt |
| 97 | # the number of tabs in raw[:n] |
| 98 | # norm |
| 99 | # the normal form as a pair (count, trailing), where: |
| 100 | # count |
| 101 | # a tuple such that raw[:n] contains count[i] |
| 102 | # instances of S * i + T |
| 103 | # trailing |
| 104 | # the number of trailing spaces in raw[:n] |
| 105 | # It's A Theorem that m.indent_level(t) == |
| 106 | # n.indent_level(t) for all t >= 1 iff m.norm == n.norm. |
| 107 | # is_simple |
| 108 | # true iff raw[:n] is of the form (T*)(S*) |
| 109 | |
| 110 | def __init__(self, ws): |
| 111 | self.raw = ws |
| 112 | S, T = Whitespace.S, Whitespace.T |
| 113 | count = [] |
| 114 | b = n = nt = 0 |
| 115 | for ch in self.raw: |
| 116 | if ch == S: |
| 117 | n = n + 1 |
| 118 | b = b + 1 |
| 119 | elif ch == T: |
| 120 | n = n + 1 |
| 121 | nt = nt + 1 |
| 122 | if b >= len(count): |
| 123 | count = count + [0] * (b - len(count) + 1) |
| 124 | count[b] = count[b] + 1 |
| 125 | b = 0 |
| 126 | else: |
| 127 | break |
| 128 | self.n = n |
| 129 | self.nt = nt |
| 130 | self.norm = tuple(count), b |
| 131 | self.is_simple = len(count) <= 1 |
| 132 | |
| 133 | # return length of longest contiguous run of spaces (whether or not |
| 134 | # preceding a tab) |
| 135 | def longest_run_of_spaces(self): |
| 136 | count, trailing = self.norm |
| 137 | return max(len(count)-1, trailing) |
| 138 | |
| 139 | def indent_level(self, tabsize): |
| 140 | # count, il = self.norm |
| 141 | # for i in range(len(count)): |
| 142 | # if count[i]: |
| 143 | # il = il + (i/tabsize + 1)*tabsize * count[i] |
| 144 | # return il |
| 145 | |
| 146 | # quicker: |
| 147 | # il = trailing + sum (i/ts + 1)*ts*count[i] = |
| 148 | # trailing + ts * sum (i/ts + 1)*count[i] = |
| 149 | # trailing + ts * sum i/ts*count[i] + count[i] = |
| 150 | # trailing + ts * [(sum i/ts*count[i]) + (sum count[i])] = |
| 151 | # trailing + ts * [(sum i/ts*count[i]) + num_tabs] |
| 152 | # and note that i/ts*count[i] is 0 when i < ts |
| 153 | |
| 154 | count, trailing = self.norm |
| 155 | il = 0 |
| 156 | for i in range(tabsize, len(count)): |
| 157 | il = il + i/tabsize * count[i] |
| 158 | return trailing + tabsize * (il + self.nt) |
| 159 | |
| 160 | # return true iff self.indent_level(t) == other.indent_level(t) |
| 161 | # for all t >= 1 |
| 162 | def equal(self, other): |
| 163 | return self.norm == other.norm |
| 164 | |
| 165 | # return a list of tuples (ts, i1, i2) such that |
| 166 | # i1 == self.indent_level(ts) != other.indent_level(ts) == i2. |
| 167 | # Intended to be used after not self.equal(other) is known, in which |
| 168 | # case it will return at least one witnessing tab size. |
| 169 | def not_equal_witness(self, other): |
| 170 | n = max(self.longest_run_of_spaces(), |
| 171 | other.longest_run_of_spaces()) + 1 |
| 172 | a = [] |
| 173 | for ts in range(1, n+1): |
| 174 | if self.indent_level(ts) != other.indent_level(ts): |
| 175 | a.append( (ts, |
| 176 | self.indent_level(ts), |
| 177 | other.indent_level(ts)) ) |
| 178 | return a |
| 179 | |
| 180 | # Return true iff self.indent_level(t) < other.indent_level(t) |
| 181 | # for all t >= 1. |
| 182 | # The algorithm is due to Vincent Broman. |
| 183 | # Easy to prove it's correct. |
| 184 | # XXXpost that. |
| 185 | # Trivial to prove n is sharp (consider T vs ST). |
| 186 | # Unknown whether there's a faster general way. I suspected so at |
| 187 | # first, but no longer. |
| 188 | # For the special (but common!) case where M and N are both of the |
| 189 | # form (T*)(S*), M.less(N) iff M.len() < N.len() and |
| 190 | # M.num_tabs() <= N.num_tabs(). Proof is easy but kinda long-winded. |
| 191 | # XXXwrite that up. |
| 192 | # Note that M is of the form (T*)(S*) iff len(M.norm[0]) <= 1. |
| 193 | def less(self, other): |
| 194 | if self.n >= other.n: |
| 195 | return 0 |
| 196 | if self.is_simple and other.is_simple: |
| 197 | return self.nt <= other.nt |
| 198 | n = max(self.longest_run_of_spaces(), |
| 199 | other.longest_run_of_spaces()) + 1 |
| 200 | # the self.n >= other.n test already did it for ts=1 |
| 201 | for ts in range(2, n+1): |
| 202 | if self.indent_level(ts) >= other.indent_level(ts): |
| 203 | return 0 |
| 204 | return 1 |
| 205 | |
| 206 | # return a list of tuples (ts, i1, i2) such that |
| 207 | # i1 == self.indent_level(ts) >= other.indent_level(ts) == i2. |
| 208 | # Intended to be used after not self.less(other) is known, in which |
| 209 | # case it will return at least one witnessing tab size. |
| 210 | def not_less_witness(self, other): |
| 211 | n = max(self.longest_run_of_spaces(), |
| 212 | other.longest_run_of_spaces()) + 1 |
| 213 | a = [] |
| 214 | for ts in range(1, n+1): |
| 215 | if self.indent_level(ts) >= other.indent_level(ts): |
| 216 | a.append( (ts, |
| 217 | self.indent_level(ts), |
| 218 | other.indent_level(ts)) ) |
| 219 | return a |
| 220 | |
| 221 | def format_witnesses(w): |
| 222 | import string |
| 223 | firsts = map(lambda tup: str(tup[0]), w) |
| 224 | prefix = "at tab size" |
| 225 | if len(w) > 1: |
| 226 | prefix = prefix + "s" |
| 227 | return prefix + " " + string.join(firsts, ', ') |
| 228 | |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 229 | # The collection of globals, the reset_globals() function, and the |
| 230 | # tokeneater() function, depend on which version of tokenize is |
| 231 | # in use. |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 233 | if hasattr(tokenize, 'NL'): |
| 234 | # take advantage of Guido's patch! |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 236 | indents = [] |
| 237 | check_equal = 0 |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 239 | def reset_globals(): |
| 240 | global indents, check_equal |
| 241 | check_equal = 0 |
| 242 | indents = [Whitespace("")] |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 244 | def tokeneater(type, token, start, end, line, |
| 245 | INDENT=tokenize.INDENT, |
| 246 | DEDENT=tokenize.DEDENT, |
| 247 | NEWLINE=tokenize.NEWLINE, |
Guido van Rossum | ce73acf | 1998-04-10 19:14:59 +0000 | [diff] [blame] | 248 | JUNK=(tokenize.COMMENT, tokenize.NL) ): |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 249 | global indents, check_equal |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 250 | |
Guido van Rossum | ce73acf | 1998-04-10 19:14:59 +0000 | [diff] [blame] | 251 | if type == NEWLINE: |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 252 | # a program statement, or ENDMARKER, will eventually follow, |
| 253 | # after some (possibly empty) run of tokens of the form |
| 254 | # (NL | COMMENT)* (INDENT | DEDENT+)? |
| 255 | # If an INDENT appears, setting check_equal is wrong, and will |
| 256 | # be undone when we see the INDENT. |
| 257 | check_equal = 1 |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 259 | elif type == INDENT: |
| 260 | check_equal = 0 |
| 261 | thisguy = Whitespace(token) |
| 262 | if not indents[-1].less(thisguy): |
| 263 | witness = indents[-1].not_less_witness(thisguy) |
| 264 | msg = "indent not greater e.g. " + format_witnesses(witness) |
| 265 | raise NannyNag(start[0], msg, line) |
| 266 | indents.append(thisguy) |
| 267 | |
| 268 | elif type == DEDENT: |
| 269 | # there's nothing we need to check here! what's important is |
| 270 | # that when the run of DEDENTs ends, the indentation of the |
| 271 | # program statement (or ENDMARKER) that triggered the run is |
| 272 | # equal to what's left at the top of the indents stack |
| 273 | assert check_equal # else no earlier NEWLINE, or an earlier INDENT |
| 274 | del indents[-1] |
| 275 | |
Guido van Rossum | ce73acf | 1998-04-10 19:14:59 +0000 | [diff] [blame] | 276 | elif check_equal and type not in JUNK: |
Guido van Rossum | f4b44fa | 1998-04-06 14:41:20 +0000 | [diff] [blame] | 277 | # this is the first "real token" following a NEWLINE, so it |
| 278 | # must be the first token of the next program statement, or an |
| 279 | # ENDMARKER; the "line" argument exposes the leading whitespace |
| 280 | # for this statement; in the case of ENDMARKER, line is an empty |
| 281 | # string, so will properly match the empty string with which the |
| 282 | # "indents" stack was seeded |
| 283 | check_equal = 0 |
| 284 | thisguy = Whitespace(line) |
| 285 | if not indents[-1].equal(thisguy): |
| 286 | witness = indents[-1].not_equal_witness(thisguy) |
| 287 | msg = "indent not equal e.g. " + format_witnesses(witness) |
| 288 | raise NannyNag(start[0], msg, line) |
| 289 | |
| 290 | else: |
| 291 | # unpatched version of tokenize |
| 292 | |
| 293 | nesting_level = 0 |
| 294 | indents = [] |
| 295 | check_equal = 0 |
| 296 | |
| 297 | def reset_globals(): |
| 298 | global nesting_level, indents, check_equal |
| 299 | nesting_level = check_equal = 0 |
| 300 | indents = [Whitespace("")] |
| 301 | |
| 302 | def tokeneater(type, token, start, end, line, |
| 303 | INDENT=tokenize.INDENT, |
| 304 | DEDENT=tokenize.DEDENT, |
| 305 | NEWLINE=tokenize.NEWLINE, |
| 306 | COMMENT=tokenize.COMMENT, |
| 307 | OP=tokenize.OP): |
| 308 | global nesting_level, indents, check_equal |
| 309 | |
| 310 | if type == INDENT: |
| 311 | check_equal = 0 |
| 312 | thisguy = Whitespace(token) |
| 313 | if not indents[-1].less(thisguy): |
| 314 | witness = indents[-1].not_less_witness(thisguy) |
| 315 | msg = "indent not greater e.g. " + format_witnesses(witness) |
| 316 | raise NannyNag(start[0], msg, line) |
| 317 | indents.append(thisguy) |
| 318 | |
| 319 | elif type == DEDENT: |
| 320 | del indents[-1] |
| 321 | |
| 322 | elif type == NEWLINE: |
| 323 | if nesting_level == 0: |
| 324 | check_equal = 1 |
| 325 | |
| 326 | elif type == COMMENT: |
| 327 | pass |
| 328 | |
| 329 | elif check_equal: |
| 330 | check_equal = 0 |
| 331 | thisguy = Whitespace(line) |
| 332 | if not indents[-1].equal(thisguy): |
| 333 | witness = indents[-1].not_equal_witness(thisguy) |
| 334 | msg = "indent not equal e.g. " + format_witnesses(witness) |
| 335 | raise NannyNag(start[0], msg, line) |
| 336 | |
| 337 | if type == OP and token in ('{', '[', '('): |
| 338 | nesting_level = nesting_level + 1 |
| 339 | |
| 340 | elif type == OP and token in ('}', ']', ')'): |
| 341 | if nesting_level == 0: |
| 342 | raise NannyNag(start[0], |
| 343 | "unbalanced bracket '" + token + "'", |
| 344 | line) |
| 345 | nesting_level = nesting_level - 1 |
Guido van Rossum | 9ab75cb | 1998-03-31 14:31:39 +0000 | [diff] [blame] | 346 | |
| 347 | if __name__ == '__main__': |
| 348 | main() |
| 349 | |