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