Mercurial > cpython-lru_cache-weakref
annotate Lib/tabnanny.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 14ec9cfd9760 |
| children | 6b18a095c2e7 |
| rev | line source |
|---|---|
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
3 """The Tab Nanny despises ambiguous indentation. She knows no mercy. |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
4 |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
5 tabnanny -- Detection of ambiguous indentation |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
6 |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
7 For the time being this module is intended to be called as a script. |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
8 However it is possible to import it into an IDE and use the function |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
9 check() described below. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
10 |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
11 Warning: The API provided by this module is likely to change in future |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
12 releases; such changes may not be backward compatible. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
13 """ |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 |
|
8473
a9d88bfa24af
From: "Tim Peters" <tim_one@msn.com>
Guido van Rossum <guido@python.org>
parents:
8173
diff
changeset
|
15 # Released to the public domain, by Tim Peters, 15 April 1998. |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
11832
d229bb792659
Added tabnanny.py, by Tim Peters, formerly from Tools/scripts, to the
Guido van Rossum <guido@python.org>
parents:
11158
diff
changeset
|
17 # XXX Note: this is now a standard library module. |
|
d229bb792659
Added tabnanny.py, by Tim Peters, formerly from Tools/scripts, to the
Guido van Rossum <guido@python.org>
parents:
11158
diff
changeset
|
18 # XXX The API needs to undergo changes however; the current code is too |
|
d229bb792659
Added tabnanny.py, by Tim Peters, formerly from Tools/scripts, to the
Guido van Rossum <guido@python.org>
parents:
11158
diff
changeset
|
19 # XXX script-like. This will be addressed later. |
|
d229bb792659
Added tabnanny.py, by Tim Peters, formerly from Tools/scripts, to the
Guido van Rossum <guido@python.org>
parents:
11158
diff
changeset
|
20 |
| 11158 | 21 __version__ = "6" |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 import os |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 import sys |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 import getopt |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 import tokenize |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
27 if not hasattr(tokenize, 'NL'): |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
28 raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
19039
f66ccf2c63eb
Add the NannyNag exception class and the process_tokens() function to
Guido van Rossum <guido@python.org>
parents:
18351
diff
changeset
|
30 __all__ = ["check", "NannyNag", "process_tokens"] |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
15873
diff
changeset
|
31 |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 verbose = 0 |
|
9648
c66b0d39a736
Added a -q ('quiet') option to tabnanny, which causes only the names of
Andrew M. Kuchling <amk@amk.ca>
parents:
9107
diff
changeset
|
33 filename_only = 0 |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 |
|
9107
24cad4e0db8d
Print serious errors to stderr instead of stdout.
Guido van Rossum <guido@python.org>
parents:
8473
diff
changeset
|
35 def errprint(*args): |
|
24cad4e0db8d
Print serious errors to stderr instead of stdout.
Guido van Rossum <guido@python.org>
parents:
8473
diff
changeset
|
36 sep = "" |
|
24cad4e0db8d
Print serious errors to stderr instead of stdout.
Guido van Rossum <guido@python.org>
parents:
8473
diff
changeset
|
37 for arg in args: |
|
24cad4e0db8d
Print serious errors to stderr instead of stdout.
Guido van Rossum <guido@python.org>
parents:
8473
diff
changeset
|
38 sys.stderr.write(sep + str(arg)) |
|
24cad4e0db8d
Print serious errors to stderr instead of stdout.
Guido van Rossum <guido@python.org>
parents:
8473
diff
changeset
|
39 sep = " " |
|
24cad4e0db8d
Print serious errors to stderr instead of stdout.
Guido van Rossum <guido@python.org>
parents:
8473
diff
changeset
|
40 sys.stderr.write("\n") |
|
24cad4e0db8d
Print serious errors to stderr instead of stdout.
Guido van Rossum <guido@python.org>
parents:
8473
diff
changeset
|
41 |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 def main(): |
|
9648
c66b0d39a736
Added a -q ('quiet') option to tabnanny, which causes only the names of
Andrew M. Kuchling <amk@amk.ca>
parents:
9107
diff
changeset
|
43 global verbose, filename_only |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 try: |
|
9648
c66b0d39a736
Added a -q ('quiet') option to tabnanny, which causes only the names of
Andrew M. Kuchling <amk@amk.ca>
parents:
9107
diff
changeset
|
45 opts, args = getopt.getopt(sys.argv[1:], "qv") |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 except getopt.error, msg: |
|
9107
24cad4e0db8d
Print serious errors to stderr instead of stdout.
Guido van Rossum <guido@python.org>
parents:
8473
diff
changeset
|
47 errprint(msg) |
|
7890
8a389c8fe6ef
Add usage message when no arguments.
Guido van Rossum <guido@python.org>
parents:
7889
diff
changeset
|
48 return |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 for o, a in opts: |
|
9648
c66b0d39a736
Added a -q ('quiet') option to tabnanny, which causes only the names of
Andrew M. Kuchling <amk@amk.ca>
parents:
9107
diff
changeset
|
50 if o == '-q': |
|
c66b0d39a736
Added a -q ('quiet') option to tabnanny, which causes only the names of
Andrew M. Kuchling <amk@amk.ca>
parents:
9107
diff
changeset
|
51 filename_only = filename_only + 1 |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 if o == '-v': |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 verbose = verbose + 1 |
|
7890
8a389c8fe6ef
Add usage message when no arguments.
Guido van Rossum <guido@python.org>
parents:
7889
diff
changeset
|
54 if not args: |
|
9107
24cad4e0db8d
Print serious errors to stderr instead of stdout.
Guido van Rossum <guido@python.org>
parents:
8473
diff
changeset
|
55 errprint("Usage:", sys.argv[0], "[-v] file_or_directory ...") |
|
7890
8a389c8fe6ef
Add usage message when no arguments.
Guido van Rossum <guido@python.org>
parents:
7889
diff
changeset
|
56 return |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 for arg in args: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 check(arg) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 |
|
22703
e0ee009417df
Derive exception classes from Exception
Neal Norwitz <nnorwitz@gmail.com>
parents:
19039
diff
changeset
|
60 class NannyNag(Exception): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
61 """ |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
62 Raised by tokeneater() if detecting an ambiguous indent. |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
63 Captured and handled in check(). |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
64 """ |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 def __init__(self, lineno, msg, line): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 self.lineno, self.msg, self.line = lineno, msg, line |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 def get_lineno(self): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 return self.lineno |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 def get_msg(self): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 return self.msg |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 def get_line(self): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 return self.line |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 def check(file): |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
75 """check(file_or_dir) |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
76 |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
77 If file_or_dir is a directory and not a symbolic link, then recursively |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
78 descend the directory tree named by file_or_dir, checking all .py files |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
79 along the way. If file_or_dir is an ordinary Python source file, it is |
|
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
80 checked for whitespace related problems. The diagnostic messages are |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
81 written to standard output using the print statement. |
|
23455
68c776ba5ea5
Added docstrings excerpted from Python Library Reference.
Raymond Hettinger <python@rcn.com>
parents:
22830
diff
changeset
|
82 """ |
|
23521
a90faa5f9417
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
23455
diff
changeset
|
83 |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 if os.path.isdir(file) and not os.path.islink(file): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 if verbose: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
23590
diff
changeset
|
86 print "%r: listing directory" % (file,) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 names = os.listdir(file) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 for name in names: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 fullname = os.path.join(file, name) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 if (os.path.isdir(fullname) and |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 not os.path.islink(fullname) or |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 os.path.normcase(name[-3:]) == ".py"): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 check(fullname) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 return |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 try: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 f = open(file) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 except IOError, msg: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
23590
diff
changeset
|
99 errprint("%r: I/O Error: %s" % (file, msg)) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 return |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 if verbose > 1: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
23590
diff
changeset
|
103 print "checking %r ..." % file |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 try: |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17338
diff
changeset
|
106 process_tokens(tokenize.generate_tokens(f.readline)) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 except tokenize.TokenError, msg: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
23590
diff
changeset
|
109 errprint("%r: Token Error: %s" % (file, msg)) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 return |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 |
|
39495
14ec9cfd9760
Make tabnanny recognize IndentationErrors raised by tokenize.
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
112 except IndentationError, msg: |
|
14ec9cfd9760
Make tabnanny recognize IndentationErrors raised by tokenize.
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
113 errprint("%r: Indentation Error: %s" % (file, msg)) |
|
14ec9cfd9760
Make tabnanny recognize IndentationErrors raised by tokenize.
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
114 return |
|
14ec9cfd9760
Make tabnanny recognize IndentationErrors raised by tokenize.
Georg Brandl <georg@python.org>
parents:
31515
diff
changeset
|
115 |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 except NannyNag, nag: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 badline = nag.get_lineno() |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 line = nag.get_line() |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 if verbose: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
23590
diff
changeset
|
120 print "%r: *** Line %d: trouble in tab city! ***" % (file, badline) |
|
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
23590
diff
changeset
|
121 print "offending line: %r" % (line,) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 print nag.get_msg() |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 else: |
| 11158 | 124 if ' ' in file: file = '"' + file + '"' |
|
9648
c66b0d39a736
Added a -q ('quiet') option to tabnanny, which causes only the names of
Andrew M. Kuchling <amk@amk.ca>
parents:
9107
diff
changeset
|
125 if filename_only: print file |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
23590
diff
changeset
|
126 else: print file, badline, repr(line) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 return |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 if verbose: |
|
31515
71a294bd7950
Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents:
23590
diff
changeset
|
130 print "%r: Clean bill of health." % (file,) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 class Whitespace: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 # the characters used for space and tab |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 S, T = ' \t' |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 # members: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 # raw |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 # the original string |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 # n |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 # the number of leading whitespace characters in raw |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 # nt |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 # the number of tabs in raw[:n] |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 # norm |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 # the normal form as a pair (count, trailing), where: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 # count |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 # a tuple such that raw[:n] contains count[i] |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 # instances of S * i + T |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 # trailing |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 # the number of trailing spaces in raw[:n] |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 # It's A Theorem that m.indent_level(t) == |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 # n.indent_level(t) for all t >= 1 iff m.norm == n.norm. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 # is_simple |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 # true iff raw[:n] is of the form (T*)(S*) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 def __init__(self, ws): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 self.raw = ws |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 S, T = Whitespace.S, Whitespace.T |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 count = [] |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 b = n = nt = 0 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 for ch in self.raw: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 if ch == S: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 n = n + 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 b = b + 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 elif ch == T: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 n = n + 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 nt = nt + 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 if b >= len(count): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 count = count + [0] * (b - len(count) + 1) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 count[b] = count[b] + 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 b = 0 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 else: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 break |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 self.n = n |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 self.nt = nt |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 self.norm = tuple(count), b |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 self.is_simple = len(count) <= 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 # return length of longest contiguous run of spaces (whether or not |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 # preceding a tab) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 def longest_run_of_spaces(self): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 count, trailing = self.norm |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 return max(len(count)-1, trailing) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 def indent_level(self, tabsize): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 # count, il = self.norm |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 # for i in range(len(count)): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 # if count[i]: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 # il = il + (i/tabsize + 1)*tabsize * count[i] |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 # return il |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 # quicker: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 # il = trailing + sum (i/ts + 1)*ts*count[i] = |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 # trailing + ts * sum (i/ts + 1)*count[i] = |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 # trailing + ts * sum i/ts*count[i] + count[i] = |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 # trailing + ts * [(sum i/ts*count[i]) + (sum count[i])] = |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 # trailing + ts * [(sum i/ts*count[i]) + num_tabs] |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 # and note that i/ts*count[i] is 0 when i < ts |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 count, trailing = self.norm |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 il = 0 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 for i in range(tabsize, len(count)): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 il = il + i/tabsize * count[i] |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 return trailing + tabsize * (il + self.nt) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 # return true iff self.indent_level(t) == other.indent_level(t) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 # for all t >= 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 def equal(self, other): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 return self.norm == other.norm |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 # return a list of tuples (ts, i1, i2) such that |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 # i1 == self.indent_level(ts) != other.indent_level(ts) == i2. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 # Intended to be used after not self.equal(other) is known, in which |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 # case it will return at least one witnessing tab size. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 def not_equal_witness(self, other): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 n = max(self.longest_run_of_spaces(), |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 other.longest_run_of_spaces()) + 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 a = [] |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 for ts in range(1, n+1): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 if self.indent_level(ts) != other.indent_level(ts): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 a.append( (ts, |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 self.indent_level(ts), |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 other.indent_level(ts)) ) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 return a |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22703
diff
changeset
|
225 # Return True iff self.indent_level(t) < other.indent_level(t) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 # for all t >= 1. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 # The algorithm is due to Vincent Broman. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 # Easy to prove it's correct. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 # XXXpost that. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 # Trivial to prove n is sharp (consider T vs ST). |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 # Unknown whether there's a faster general way. I suspected so at |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 # first, but no longer. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 # For the special (but common!) case where M and N are both of the |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 # form (T*)(S*), M.less(N) iff M.len() < N.len() and |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 # M.num_tabs() <= N.num_tabs(). Proof is easy but kinda long-winded. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 # XXXwrite that up. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 # Note that M is of the form (T*)(S*) iff len(M.norm[0]) <= 1. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 def less(self, other): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 if self.n >= other.n: |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22703
diff
changeset
|
240 return False |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 if self.is_simple and other.is_simple: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 return self.nt <= other.nt |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 n = max(self.longest_run_of_spaces(), |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 other.longest_run_of_spaces()) + 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 # the self.n >= other.n test already did it for ts=1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 for ts in range(2, n+1): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 if self.indent_level(ts) >= other.indent_level(ts): |
|
22830
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22703
diff
changeset
|
248 return False |
|
a9dd5e6d6805
Convert a pile of obvious "yes/no" functions to return bool.
Tim Peters <tim.peters@gmail.com>
parents:
22703
diff
changeset
|
249 return True |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 # return a list of tuples (ts, i1, i2) such that |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 # i1 == self.indent_level(ts) >= other.indent_level(ts) == i2. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 # Intended to be used after not self.less(other) is known, in which |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 # case it will return at least one witnessing tab size. |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 def not_less_witness(self, other): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 n = max(self.longest_run_of_spaces(), |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 other.longest_run_of_spaces()) + 1 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 a = [] |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 for ts in range(1, n+1): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 if self.indent_level(ts) >= other.indent_level(ts): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 a.append( (ts, |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 self.indent_level(ts), |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 other.indent_level(ts)) ) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 return a |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 def format_witnesses(w): |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 firsts = map(lambda tup: str(tup[0]), w) |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 prefix = "at tab size" |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 if len(w) > 1: |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 prefix = prefix + "s" |
|
23590
a4bb18212b48
Use string methods where possible, and remove import string
Neal Norwitz <nnorwitz@gmail.com>
parents:
23521
diff
changeset
|
271 return prefix + " " + ', '.join(firsts) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 |
|
18351
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
273 def process_tokens(tokens): |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
274 INDENT = tokenize.INDENT |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
275 DEDENT = tokenize.DEDENT |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
276 NEWLINE = tokenize.NEWLINE |
|
44a87c300f2b
Turns out Neil didn't intend for *all* of his gen-branch work to get
Tim Peters <tim.peters@gmail.com>
parents:
18148
diff
changeset
|
277 JUNK = tokenize.COMMENT, tokenize.NL |
|
18148
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17338
diff
changeset
|
278 indents = [Whitespace("")] |
|
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17338
diff
changeset
|
279 check_equal = 0 |
|
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17338
diff
changeset
|
280 |
|
7bc473b2c654
Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents:
17338
diff
changeset
|
281 for (type, token, start, end, line) in tokens: |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
282 if type == NEWLINE: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
283 # a program statement, or ENDMARKER, will eventually follow, |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
284 # after some (possibly empty) run of tokens of the form |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
285 # (NL | COMMENT)* (INDENT | DEDENT+)? |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
286 # If an INDENT appears, setting check_equal is wrong, and will |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
287 # be undone when we see the INDENT. |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
288 check_equal = 1 |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
290 elif type == INDENT: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
291 check_equal = 0 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
292 thisguy = Whitespace(token) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
293 if not indents[-1].less(thisguy): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
294 witness = indents[-1].not_less_witness(thisguy) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
295 msg = "indent not greater e.g. " + format_witnesses(witness) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
296 raise NannyNag(start[0], msg, line) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
297 indents.append(thisguy) |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
299 elif type == DEDENT: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
300 # there's nothing we need to check here! what's important is |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
301 # that when the run of DEDENTs ends, the indentation of the |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
302 # program statement (or ENDMARKER) that triggered the run is |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
303 # equal to what's left at the top of the indents stack |
|
8473
a9d88bfa24af
From: "Tim Peters" <tim_one@msn.com>
Guido van Rossum <guido@python.org>
parents:
8173
diff
changeset
|
304 |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
305 # Ouch! This assert triggers if the last line of the source |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
306 # is indented *and* lacks a newline -- then DEDENTs pop out |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
307 # of thin air. |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
308 # assert check_equal # else no earlier NEWLINE, or an earlier INDENT |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
309 check_equal = 1 |
|
8473
a9d88bfa24af
From: "Tim Peters" <tim_one@msn.com>
Guido van Rossum <guido@python.org>
parents:
8173
diff
changeset
|
310 |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
311 del indents[-1] |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 |
|
15873
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
313 elif check_equal and type not in JUNK: |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
314 # this is the first "real token" following a NEWLINE, so it |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
315 # must be the first token of the next program statement, or an |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
316 # ENDMARKER; the "line" argument exposes the leading whitespace |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
317 # for this statement; in the case of ENDMARKER, line is an empty |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
318 # string, so will properly match the empty string with which the |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
319 # "indents" stack was seeded |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
320 check_equal = 0 |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
321 thisguy = Whitespace(line) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
322 if not indents[-1].equal(thisguy): |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
323 witness = indents[-1].not_equal_witness(thisguy) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
324 msg = "indent not equal e.g. " + format_witnesses(witness) |
|
3dd217fbdd59
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
11832
diff
changeset
|
325 raise NannyNag(start[0], msg, line) |
|
7889
41c7d5243be9
Tim's latest version (supports old and new tokenize modules)
Guido van Rossum <guido@python.org>
parents:
7773
diff
changeset
|
326 |
|
7773
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 if __name__ == '__main__': |
|
8a567412b21b
Tim Peters' program for checking whether a program's indentation is
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 main() |
