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