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