blob: c9900abe3a7f4e0830be10d391635adade4efd05 [file] [log] [blame]
Guido van Rossum1d28e171994-05-15 18:14:33 +00001#! /ufs/guido/bin/sgi/python
2#! /usr/local/bin/python
3
4# This file contains a class and a main program that perform two
5# related (though complimentary) formatting operations on Python
6# programs. When called as "pindend -c", it takes a valid Python
7# program as input and outputs a version augmented with block-closing
8# comments. When called as "pindent -r" it assumes its input is a
9# Python program with block-closing comments but with its indentation
10# messed up, and outputs a properly indented version.
11
12# A "block-closing comment" is a comment of the form '# end <keyword>'
13# where <keyword> is the keyword that opened the block. If the
14# opening keyword is 'def' or 'class', the function or class name may
15# be repeated in the block-closing comment as well. Here is an
16# example of a program fully augmented with block-closing comments:
17
18# def foobar(a, b):
19# if a == b:
20# a = a+1
21# elif a < b:
22# b = b-1
23# if b > a: a = a-1
24# # end if
25# else:
26# print 'oops!'
27# # end if
28# # end def foobar
29
30# Note that only the last part of an if...elif...else... block needs a
31# block-closing comment; the same is true for other compound
32# statements (e.g. try...except). Also note that "short-form" blocks
33# like the second 'if' in the example must be closed as well;
34# otherwise the 'else' in the example would be ambiguous (remember
35# that indentation is not significant when interpreting block-closing
36# comments).
37
38# Both operations are idempotent (i.e. applied to their own output
39# they yield an identical result). Running first "pindent -c" and
40# then "pindent -r" on a valid Python program produces a program that
41# is semantically identical to the input (though its indentation may
42# be different).
43
44# Other options:
45# -s stepsize: set the indentation step size (default 8)
46# -t tabsize : set the number of spaces a tab character is worth (default 8)
47# file ... : input file(s) (default standard input)
48# The results always go to standard output
49
50# Caveats:
51# - comments ending in a backslash will be mistaken for continued lines
52# - continuations using backslash are always left unchanged
53# - continuations inside parentheses are not extra indented by -r
54# but must be indented for -c to work correctly (this breaks
55# idempotency!)
56# - continued lines inside triple-quoted strings are totally garbled
57
58# Secret feature:
59# - On input, a block may also be closed with an "end statement" --
60# this is a block-closing comment without the '#' sign.
61
62# Possible improvements:
63# - check syntax based on transitions in 'next' table
64# - better error reporting
65# - better error recovery
66# - check identifier after class/def
67
68# The following wishes need a more complete tokenization of the source:
69# - Don't get fooled by comments ending in backslash
70# - reindent continuation lines indicated by backslash
71# - handle continuation lines inside parentheses/braces/brackets
72# - handle triple quoted strings spanning lines
73# - realign comments
74# - optionally do much more thorough reformatting, a la C indent
75
76import os
77import regex
78import string
79import sys
80
81next = {}
82next['if'] = next['elif'] = 'elif', 'else', 'end'
83next['while'] = next['for'] = 'else', 'end'
84next['try'] = 'except', 'finally'
85next['except'] = 'except', 'else', 'end'
86next['else'] = next['finally'] = next['def'] = next['class'] = 'end'
87next['end'] = ()
88start = 'if', 'while', 'for', 'try', 'def', 'class'
89
90class PythonIndenter:
91
92 def __init__(self, fpi = sys.stdin, fpo = sys.stdout,
93 indentsize = 8, tabsize = 8):
94 self.fpi = fpi
95 self.fpo = fpo
96 self.indentsize = indentsize
97 self.tabsize = tabsize
98 self.lineno = 0
99 self.write = fpo.write
100 self.kwprog = regex.symcomp(
101 '^[ \t]*\(<kw>[a-z]+\)'
102 '\([ \t]+\(<id>[a-zA-Z_][a-zA-Z0-9_]*\)\)?'
103 '[^a-zA-Z0-9_]')
104 self.endprog = regex.symcomp(
105 '^[ \t]*#?[ \t]*end[ \t]+\(<kw>[a-z]+\)'
106 '\([ \t]+\(<id>[a-zA-Z_][a-zA-Z0-9_]*\)\)?'
107 '[^a-zA-Z0-9_]')
108 self.wsprog = regex.compile('^[ \t]*')
109 # end def __init__
110
111 def readline(self):
112 line = self.fpi.readline()
113 if line: self.lineno = self.lineno + 1
114 # end if
115 return line
116 # end def readline
117
118 def error(self, fmt, *args):
119 if args: fmt = fmt % args
120 # end if
121 sys.stderr.write('Error at line %d: %s\n' % (self.lineno, fmt))
122 self.write('### %s ###\n' % fmt)
123 # end def error
124
125 def getline(self):
126 line = self.readline()
127 while line[-2:] == '\\\n':
128 line2 = self.readline()
129 if not line2: break
130 # end if
131 line = line + line2
132 # end while
133 return line
134 # end def getline
135
136 def putline(self, line, indent = None):
137 if indent is None:
138 self.write(line)
139 return
140 # end if
141 tabs, spaces = divmod(indent*self.indentsize, self.tabsize)
142 i = max(0, self.wsprog.match(line))
143 self.write('\t'*tabs + ' '*spaces + line[i:])
144 # end def putline
145
146 def reformat(self):
147 stack = []
148 while 1:
149 line = self.getline()
150 if not line: break # EOF
151 # end if
152 if self.endprog.match(line) >= 0:
153 kw = 'end'
154 kw2 = self.endprog.group('kw')
155 if not stack:
156 self.error('unexpected end')
157 elif stack[-1][0] != kw2:
158 self.error('unmatched end')
159 # end if
160 del stack[-1:]
161 self.putline(line, len(stack))
162 continue
163 # end if
164 if self.kwprog.match(line) >= 0:
165 kw = self.kwprog.group('kw')
166 if kw in start:
167 self.putline(line, len(stack))
168 stack.append((kw, kw))
169 continue
170 # end if
171 if next.has_key(kw) and stack:
172 self.putline(line, len(stack)-1)
173 kwa, kwb = stack[-1]
174 stack[-1] = kwa, kw
175 continue
176 # end if
177 # end if
178 self.putline(line, len(stack))
179 # end while
180 if stack:
181 self.error('unterminated keywords')
182 for kwa, kwb in stack:
183 self.write('\t%s\n' % kwa)
184 # end for
185 # end if
186 # end def reformat
187
188 def complete(self):
189 self.indentsize = 1
190 stack = []
191 todo = []
192 current, firstkw, lastkw, topid = 0, '', '', ''
193 while 1:
194 line = self.getline()
195 i = max(0, self.wsprog.match(line))
196 if self.endprog.match(line) >= 0:
197 thiskw = 'end'
198 endkw = self.endprog.group('kw')
199 thisid = self.endprog.group('id')
200 elif self.kwprog.match(line) >= 0:
201 thiskw = self.kwprog.group('kw')
202 if not next.has_key(thiskw):
203 thiskw = ''
204 # end if
205 if thiskw in ('def', 'class'):
206 thisid = self.kwprog.group('id')
207 else:
208 thisid = ''
209 # end if
210 elif line[i:i+1] in ('\n', '#'):
211 todo.append(line)
212 continue
213 else:
214 thiskw = ''
215 # end if
216 indent = len(string.expandtabs(line[:i], self.tabsize))
217 while indent < current:
218 if firstkw:
219 if topid:
220 s = '# end %s %s\n' % (
221 firstkw, topid)
222 else:
223 s = '# end %s\n' % firstkw
224 # end if
225 self.putline(s, current)
226 firstkw = lastkw = ''
227 # end if
228 current, firstkw, lastkw, topid = stack[-1]
229 del stack[-1]
230 # end while
231 if indent == current and firstkw:
232 if thiskw == 'end':
233 if endkw != firstkw:
234 self.error('mismatched end')
235 # end if
236 firstkw = lastkw = ''
237 elif not thiskw or thiskw in start:
238 if topid:
239 s = '# end %s %s\n' % (
240 firstkw, topid)
241 else:
242 s = '# end %s\n' % firstkw
243 # end if
244 self.putline(s, current)
245 firstkw = lastkw = topid = ''
246 # end if
247 # end if
248 if indent > current:
249 stack.append(current, firstkw, lastkw, topid)
250 if thiskw and thiskw not in start:
251 # error
252 thiskw = ''
253 # end if
254 current, firstkw, lastkw, topid = \
255 indent, thiskw, thiskw, thisid
256 # end if
257 if thiskw:
258 if thiskw in start:
259 firstkw = lastkw = thiskw
260 topid = thisid
261 else:
262 lastkw = thiskw
263 # end if
264 # end if
265 for l in todo: self.write(l)
266 # end for
267 todo = []
268 if not line: break
269 # end if
270 self.write(line)
271 # end while
272 # end def complete
273
274# end class PythonIndenter
275
276def test():
277 import getopt
278 opts, args = getopt.getopt(sys.argv[1:], 'crs:t:')
279 action = None
280 stepsize = 8
281 tabsize = 8
282 for o, a in opts:
283 if o == '-c':
284 action = PythonIndenter.complete
285 elif o == '-r':
286 action = PythonIndenter.reformat
Guido van Rossum3962fdb1994-05-27 14:13:46 +0000287 elif o == '-s':
Guido van Rossum1d28e171994-05-15 18:14:33 +0000288 stepsize = string.atoi(a)
Guido van Rossum3962fdb1994-05-27 14:13:46 +0000289 elif o == '-t':
Guido van Rossum1d28e171994-05-15 18:14:33 +0000290 tabsize = string.atoi(a)
291 # end if
292 # end for
293 if not action:
294 print 'You must specify -c(omplete) or -r(eformat)'
295 sys.exit(2)
296 # end if
297 if not args: args = ['-']
298 # end if
299 for file in args:
300 if file == '-':
301 fp = sys.stdin
302 else:
303 fp = open(file, 'r')
304 # end if
305 pi = PythonIndenter(fp, sys.stdout, stepsize, tabsize)
306 action(pi)
307 if fp != sys.stdin: fp.close()
308 # end if
309 # end for
310# end def test
311
312if __name__ == '__main__':
313 test()
314# end if