blob: 40b672630344c9c5d17d603444427dbc72ece10e [file] [log] [blame]
Guido van Rossum8113cdc1999-06-01 19:49:21 +00001import string
2import re
3import sys
4
5# Reason last stmt is continued (or C_NONE if it's not).
6C_NONE, C_BACKSLASH, C_STRING, C_BRACKET = range(4)
7
8if 0: # for throwaway debugging output
9 def dump(*stuff):
Guido van Rossum8113cdc1999-06-01 19:49:21 +000010 sys.__stdout__.write(string.join(map(str, stuff), " ") + "\n")
11
Guido van Rossumf4a15081999-06-03 14:32:16 +000012# Find what looks like the start of a popular stmt.
Guido van Rossumbbaba851999-06-01 19:55:34 +000013
Guido van Rossumf4a15081999-06-03 14:32:16 +000014_synchre = re.compile(r"""
Guido van Rossum8113cdc1999-06-01 19:49:21 +000015 ^
16 [ \t]*
Guido van Rossum729afc11999-06-07 14:28:14 +000017 (?: if
18 | for
19 | while
20 | else
21 | def
22 | return
23 | assert
24 | break
25 | class
26 | continue
27 | elif
28 | try
29 | except
30 | raise
31 | import
32 )
Guido van Rossumf4a15081999-06-03 14:32:16 +000033 \b
Guido van Rossum8113cdc1999-06-01 19:49:21 +000034""", re.VERBOSE | re.MULTILINE).search
35
Guido van Rossumbbaba851999-06-01 19:55:34 +000036# Match blank line or non-indenting comment line.
37
Guido van Rossum8113cdc1999-06-01 19:49:21 +000038_junkre = re.compile(r"""
39 [ \t]*
Guido van Rossumbbaba851999-06-01 19:55:34 +000040 (?: \# \S .* )?
Guido van Rossum8113cdc1999-06-01 19:49:21 +000041 \n
42""", re.VERBOSE).match
43
Guido van Rossumbbaba851999-06-01 19:55:34 +000044# Match any flavor of string; the terminating quote is optional
45# so that we're robust in the face of incomplete program text.
46
Guido van Rossum8113cdc1999-06-01 19:49:21 +000047_match_stringre = re.compile(r"""
48 \""" [^"\\]* (?:
49 (?: \\. | "(?!"") )
50 [^"\\]*
51 )*
52 (?: \""" )?
53
54| " [^"\\\n]* (?: \\. [^"\\\n]* )* "?
55
56| ''' [^'\\]* (?:
57 (?: \\. | '(?!'') )
58 [^'\\]*
59 )*
60 (?: ''' )?
61
62| ' [^'\\\n]* (?: \\. [^'\\\n]* )* '?
63""", re.VERBOSE | re.DOTALL).match
64
Guido van Rossumbbaba851999-06-01 19:55:34 +000065# Match a line that starts with something interesting;
66# used to find the first item of a bracket structure.
67
68_itemre = re.compile(r"""
Guido van Rossum8113cdc1999-06-01 19:49:21 +000069 [ \t]*
Guido van Rossumbbaba851999-06-01 19:55:34 +000070 [^\s#\\] # if we match, m.end()-1 is the interesting char
Guido van Rossum8113cdc1999-06-01 19:49:21 +000071""", re.VERBOSE).match
72
Guido van Rossumbbaba851999-06-01 19:55:34 +000073# Match start of stmts that should be followed by a dedent.
74
Guido van Rossum8113cdc1999-06-01 19:49:21 +000075_closere = re.compile(r"""
76 \s*
77 (?: return
78 | break
79 | continue
80 | raise
81 | pass
82 )
83 \b
84""", re.VERBOSE).match
85
Guido van Rossum9428fa62000-03-03 14:51:11 +000086# Chew up non-special chars as quickly as possible. If match is
87# successful, m.end() less 1 is the index of the last boring char
88# matched. If match is unsuccessful, the string starts with an
89# interesting char.
Guido van Rossumbbaba851999-06-01 19:55:34 +000090
91_chew_ordinaryre = re.compile(r"""
Guido van Rossum9428fa62000-03-03 14:51:11 +000092 [^[\](){}#'"\\]+
Guido van Rossumbbaba851999-06-01 19:55:34 +000093""", re.VERBOSE).match
94
Guido van Rossum8113cdc1999-06-01 19:49:21 +000095# Build translation table to map uninteresting chars to "x", open
96# brackets to "(", and close brackets to ")".
97
98_tran = ['x'] * 256
99for ch in "({[":
100 _tran[ord(ch)] = '('
101for ch in ")}]":
102 _tran[ord(ch)] = ')'
103for ch in "\"'\\\n#":
104 _tran[ord(ch)] = ch
105_tran = string.join(_tran, '')
106del ch
107
108class Parser:
109
110 def __init__(self, indentwidth, tabwidth):
111 self.indentwidth = indentwidth
112 self.tabwidth = tabwidth
113
114 def set_str(self, str):
115 assert len(str) == 0 or str[-1] == '\n'
116 self.str = str
117 self.study_level = 0
118
Guido van Rossumf4a15081999-06-03 14:32:16 +0000119 # Return index of a good place to begin parsing, as close to the
120 # end of the string as possible. This will be the start of some
Guido van Rossum729afc11999-06-07 14:28:14 +0000121 # popular stmt like "if" or "def". Return None if none found:
122 # the caller should pass more prior context then, if possible, or
123 # if not (the entire program text up until the point of interest
124 # has already been tried) pass 0 to set_lo.
Guido van Rossumf4a15081999-06-03 14:32:16 +0000125 #
126 # This will be reliable iff given a reliable is_char_in_string
Guido van Rossum729afc11999-06-07 14:28:14 +0000127 # function, meaning that when it says "no", it's absolutely
128 # guaranteed that the char is not in a string.
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000129 #
130 # Ack, hack: in the shell window this kills us, because there's
131 # no way to tell the differences between output, >>> etc and
132 # user input. Indeed, IDLE's first output line makes the rest
133 # look like it's in an unclosed paren!:
134 # Python 1.5.2 (#0, Apr 13 1999, ...
135
Guido van Rossum729afc11999-06-07 14:28:14 +0000136 def find_good_parse_start(self, use_ps1, is_char_in_string=None,
137 _rfind=string.rfind,
Guido van Rossumf4a15081999-06-03 14:32:16 +0000138 _synchre=_synchre):
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000139 str, pos = self.str, None
Guido van Rossumbbaba851999-06-01 19:55:34 +0000140 if use_ps1:
Guido van Rossum729afc11999-06-07 14:28:14 +0000141 # shell window
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000142 ps1 = '\n' + sys.ps1
Guido van Rossum729afc11999-06-07 14:28:14 +0000143 i = _rfind(str, ps1)
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000144 if i >= 0:
145 pos = i + len(ps1)
Guido van Rossum729afc11999-06-07 14:28:14 +0000146 # make it look like there's a newline instead
147 # of ps1 at the start -- hacking here once avoids
148 # repeated hackery later
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000149 self.str = str[:pos-1] + '\n' + str[pos:]
Guido van Rossum729afc11999-06-07 14:28:14 +0000150 return pos
151
152 # File window -- real work.
153 if not is_char_in_string:
154 # no clue -- make the caller pass everything
155 return None
156
157 # Peek back from the end for a good place to start,
158 # but don't try too often; pos will be left None, or
159 # bumped to a legitimate synch point.
160 limit = len(str)
161 for tries in range(5):
162 i = _rfind(str, ":\n", 0, limit)
163 if i < 0:
164 break
165 i = _rfind(str, '\n', 0, i) + 1 # start of colon line
166 m = _synchre(str, i, limit)
167 if m and not is_char_in_string(m.start()):
168 pos = m.start()
169 break
170 limit = i
171 if pos is None:
172 # Nothing looks like a block-opener, or stuff does
173 # but is_char_in_string keeps returning true; most likely
174 # we're in or near a giant string, the colorizer hasn't
175 # caught up enough to be helpful, or there simply *aren't*
176 # any interesting stmts. In any of these cases we're
177 # going to have to parse the whole thing to be sure, so
178 # give it one last try from the start, but stop wasting
179 # time here regardless of the outcome.
180 m = _synchre(str)
181 if m and not is_char_in_string(m.start()):
182 pos = m.start()
183 return pos
184
185 # Peeking back worked; look forward until _synchre no longer
186 # matches.
187 i = pos + 1
188 while 1:
189 m = _synchre(str, i)
190 if m:
191 s, i = m.span()
192 if not is_char_in_string(s):
193 pos = s
194 else:
195 break
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000196 return pos
197
198 # Throw away the start of the string. Intended to be called with
Guido van Rossumf4a15081999-06-03 14:32:16 +0000199 # find_good_parse_start's result.
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000200
201 def set_lo(self, lo):
202 assert lo == 0 or self.str[lo-1] == '\n'
203 if lo > 0:
204 self.str = self.str[lo:]
205
206 # As quickly as humanly possible <wink>, find the line numbers (0-
207 # based) of the non-continuation lines.
Guido van Rossumbbaba851999-06-01 19:55:34 +0000208 # Creates self.{goodlines, continuation}.
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000209
210 def _study1(self, _replace=string.replace, _find=string.find):
211 if self.study_level >= 1:
212 return
213 self.study_level = 1
214
215 # Map all uninteresting characters to "x", all open brackets
216 # to "(", all close brackets to ")", then collapse runs of
217 # uninteresting characters. This can cut the number of chars
218 # by a factor of 10-40, and so greatly speed the following loop.
219 str = self.str
220 str = string.translate(str, _tran)
221 str = _replace(str, 'xxxxxxxx', 'x')
222 str = _replace(str, 'xxxx', 'x')
223 str = _replace(str, 'xx', 'x')
224 str = _replace(str, 'xx', 'x')
225 str = _replace(str, '\nx', '\n')
226 # note that replacing x\n with \n would be incorrect, because
227 # x may be preceded by a backslash
228
229 # March over the squashed version of the program, accumulating
230 # the line numbers of non-continued stmts, and determining
231 # whether & why the last stmt is a continuation.
232 continuation = C_NONE
233 level = lno = 0 # level is nesting level; lno is line number
Guido van Rossumbbaba851999-06-01 19:55:34 +0000234 self.goodlines = goodlines = [0]
235 push_good = goodlines.append
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000236 i, n = 0, len(str)
237 while i < n:
238 ch = str[i]
Guido van Rossumbbaba851999-06-01 19:55:34 +0000239 i = i+1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000240
Guido van Rossumbbaba851999-06-01 19:55:34 +0000241 # cases are checked in decreasing order of frequency
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000242 if ch == 'x':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000243 continue
244
245 if ch == '\n':
246 lno = lno + 1
247 if level == 0:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000248 push_good(lno)
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000249 # else we're in an unclosed bracket structure
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000250 continue
251
252 if ch == '(':
253 level = level + 1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000254 continue
255
256 if ch == ')':
257 if level:
258 level = level - 1
259 # else the program is invalid, but we can't complain
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000260 continue
261
262 if ch == '"' or ch == "'":
263 # consume the string
264 quote = ch
Guido van Rossumbbaba851999-06-01 19:55:34 +0000265 if str[i-1:i+2] == quote * 3:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000266 quote = quote * 3
Guido van Rossumbbaba851999-06-01 19:55:34 +0000267 w = len(quote) - 1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000268 i = i+w
269 while i < n:
270 ch = str[i]
Guido van Rossumbbaba851999-06-01 19:55:34 +0000271 i = i+1
272
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000273 if ch == 'x':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000274 continue
275
Guido van Rossumbbaba851999-06-01 19:55:34 +0000276 if str[i-1:i+w] == quote:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000277 i = i+w
278 break
279
280 if ch == '\n':
281 lno = lno + 1
Guido van Rossumbbaba851999-06-01 19:55:34 +0000282 if w == 0:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000283 # unterminated single-quoted string
284 if level == 0:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000285 push_good(lno)
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000286 break
287 continue
288
289 if ch == '\\':
Guido van Rossumbbaba851999-06-01 19:55:34 +0000290 assert i < n
291 if str[i] == '\n':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000292 lno = lno + 1
Guido van Rossumbbaba851999-06-01 19:55:34 +0000293 i = i+1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000294 continue
295
296 # else comment char or paren inside string
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000297
298 else:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000299 # didn't break out of the loop, so we're still
300 # inside a string
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000301 continuation = C_STRING
Guido van Rossumbbaba851999-06-01 19:55:34 +0000302 continue # with outer loop
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000303
304 if ch == '#':
305 # consume the comment
306 i = _find(str, '\n', i)
307 assert i >= 0
308 continue
309
310 assert ch == '\\'
Guido van Rossumbbaba851999-06-01 19:55:34 +0000311 assert i < n
312 if str[i] == '\n':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000313 lno = lno + 1
Guido van Rossumbbaba851999-06-01 19:55:34 +0000314 if i+1 == n:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000315 continuation = C_BACKSLASH
Guido van Rossumbbaba851999-06-01 19:55:34 +0000316 i = i+1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000317
318 # The last stmt may be continued for all 3 reasons.
319 # String continuation takes precedence over bracket
320 # continuation, which beats backslash continuation.
321 if continuation != C_STRING and level > 0:
322 continuation = C_BRACKET
323 self.continuation = continuation
324
Guido van Rossumbbaba851999-06-01 19:55:34 +0000325 # Push the final line number as a sentinel value, regardless of
326 # whether it's continued.
327 assert (continuation == C_NONE) == (goodlines[-1] == lno)
328 if goodlines[-1] != lno:
329 push_good(lno)
330
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000331 def get_continuation_type(self):
332 self._study1()
333 return self.continuation
334
335 # study1 was sufficient to determine the continuation status,
336 # but doing more requires looking at every character. study2
337 # does this for the last interesting statement in the block.
338 # Creates:
339 # self.stmt_start, stmt_end
340 # slice indices of last interesting stmt
341 # self.lastch
342 # last non-whitespace character before optional trailing
343 # comment
344 # self.lastopenbracketpos
345 # if continuation is C_BRACKET, index of last open bracket
346
347 def _study2(self, _rfind=string.rfind, _find=string.find,
348 _ws=string.whitespace):
349 if self.study_level >= 2:
350 return
351 self._study1()
352 self.study_level = 2
353
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000354 # Set p and q to slice indices of last interesting stmt.
Guido van Rossumbbaba851999-06-01 19:55:34 +0000355 str, goodlines = self.str, self.goodlines
356 i = len(goodlines) - 1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000357 p = len(str) # index of newest line
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000358 while i:
359 assert p
Guido van Rossumbbaba851999-06-01 19:55:34 +0000360 # p is the index of the stmt at line number goodlines[i].
361 # Move p back to the stmt at line number goodlines[i-1].
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000362 q = p
Guido van Rossumbbaba851999-06-01 19:55:34 +0000363 for nothing in range(goodlines[i-1], goodlines[i]):
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000364 # tricky: sets p to 0 if no preceding newline
365 p = _rfind(str, '\n', 0, p-1) + 1
366 # The stmt str[p:q] isn't a continuation, but may be blank
367 # or a non-indenting comment line.
368 if _junkre(str, p):
369 i = i-1
370 else:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000371 break
Guido van Rossumbbaba851999-06-01 19:55:34 +0000372 if i == 0:
373 # nothing but junk!
374 assert p == 0
375 q = p
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000376 self.stmt_start, self.stmt_end = p, q
377
378 # Analyze this stmt, to find the last open bracket (if any)
379 # and last interesting character (if any).
Guido van Rossumbbaba851999-06-01 19:55:34 +0000380 lastch = ""
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000381 stack = [] # stack of open bracket indices
382 push_stack = stack.append
383 while p < q:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000384 # suck up all except ()[]{}'"#\\
385 m = _chew_ordinaryre(str, p, q)
386 if m:
Guido van Rossum9428fa62000-03-03 14:51:11 +0000387 # we skipped at least one boring char
Guido van Rossum7f1cd292000-03-13 14:50:24 +0000388 newp = m.end()
Guido van Rossum9428fa62000-03-03 14:51:11 +0000389 # back up over totally boring whitespace
Guido van Rossum7f1cd292000-03-13 14:50:24 +0000390 i = newp - 1 # index of last boring char
391 while i >= p and str[i] in " \t\n":
Guido van Rossum9428fa62000-03-03 14:51:11 +0000392 i = i-1
Guido van Rossum7f1cd292000-03-13 14:50:24 +0000393 if i >= p:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000394 lastch = str[i]
Guido van Rossum7f1cd292000-03-13 14:50:24 +0000395 p = newp
Guido van Rossumbbaba851999-06-01 19:55:34 +0000396 if p >= q:
397 break
398
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000399 ch = str[p]
Guido van Rossumbbaba851999-06-01 19:55:34 +0000400
401 if ch in "([{":
402 push_stack(p)
403 lastch = ch
404 p = p+1
405 continue
406
407 if ch in ")]}":
408 if stack:
409 del stack[-1]
410 lastch = ch
411 p = p+1
412 continue
413
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000414 if ch == '"' or ch == "'":
415 # consume string
416 # Note that study1 did this with a Python loop, but
417 # we use a regexp here; the reason is speed in both
418 # cases; the string may be huge, but study1 pre-squashed
419 # strings to a couple of characters per line. study1
420 # also needed to keep track of newlines, and we don't
421 # have to.
Guido van Rossumbbaba851999-06-01 19:55:34 +0000422 lastch = ch
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000423 p = _match_stringre(str, p, q).end()
424 continue
425
426 if ch == '#':
427 # consume comment and trailing newline
428 p = _find(str, '\n', p, q) + 1
429 assert p > 0
430 continue
431
Guido van Rossumbbaba851999-06-01 19:55:34 +0000432 assert ch == '\\'
433 p = p+1 # beyond backslash
434 assert p < q
435 if str[p] != '\n':
436 # the program is invalid, but can't complain
437 lastch = ch + str[p]
438 p = p+1 # beyond escaped char
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000439
440 # end while p < q:
441
Guido van Rossumbbaba851999-06-01 19:55:34 +0000442 self.lastch = lastch
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000443 if stack:
444 self.lastopenbracketpos = stack[-1]
445
446 # Assuming continuation is C_BRACKET, return the number
447 # of spaces the next line should be indented.
448
449 def compute_bracket_indent(self, _find=string.find):
450 self._study2()
451 assert self.continuation == C_BRACKET
452 j = self.lastopenbracketpos
453 str = self.str
454 n = len(str)
455 origi = i = string.rfind(str, '\n', 0, j) + 1
Guido van Rossumbbaba851999-06-01 19:55:34 +0000456 j = j+1 # one beyond open bracket
457 # find first list item; set i to start of its line
458 while j < n:
459 m = _itemre(str, j)
460 if m:
461 j = m.end() - 1 # index of first interesting char
462 extra = 0
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000463 break
Guido van Rossumbbaba851999-06-01 19:55:34 +0000464 else:
465 # this line is junk; advance to next line
466 i = j = _find(str, '\n', j) + 1
467 else:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000468 # nothing interesting follows the bracket;
469 # reproduce the bracket line's indentation + a level
470 j = i = origi
Guido van Rossumbbaba851999-06-01 19:55:34 +0000471 while str[j] in " \t":
472 j = j+1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000473 extra = self.indentwidth
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000474 return len(string.expandtabs(str[i:j],
475 self.tabwidth)) + extra
476
477 # Return number of physical lines in last stmt (whether or not
478 # it's an interesting stmt! this is intended to be called when
479 # continuation is C_BACKSLASH).
480
481 def get_num_lines_in_stmt(self):
482 self._study1()
Guido van Rossumbbaba851999-06-01 19:55:34 +0000483 goodlines = self.goodlines
484 return goodlines[-1] - goodlines[-2]
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000485
486 # Assuming continuation is C_BACKSLASH, return the number of spaces
487 # the next line should be indented. Also assuming the new line is
488 # the first one following the initial line of the stmt.
489
490 def compute_backslash_indent(self):
491 self._study2()
492 assert self.continuation == C_BACKSLASH
493 str = self.str
494 i = self.stmt_start
495 while str[i] in " \t":
496 i = i+1
497 startpos = i
Guido van Rossumbbaba851999-06-01 19:55:34 +0000498
499 # See whether the initial line starts an assignment stmt; i.e.,
500 # look for an = operator
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000501 endpos = string.find(str, '\n', startpos) + 1
502 found = level = 0
503 while i < endpos:
504 ch = str[i]
505 if ch in "([{":
506 level = level + 1
507 i = i+1
508 elif ch in ")]}":
509 if level:
510 level = level - 1
511 i = i+1
512 elif ch == '"' or ch == "'":
513 i = _match_stringre(str, i, endpos).end()
514 elif ch == '#':
515 break
516 elif level == 0 and ch == '=' and \
Guido van Rossumbbaba851999-06-01 19:55:34 +0000517 (i == 0 or str[i-1] not in "=<>!") and \
518 str[i+1] != '=':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000519 found = 1
520 break
521 else:
522 i = i+1
523
524 if found:
525 # found a legit =, but it may be the last interesting
526 # thing on the line
527 i = i+1 # move beyond the =
528 found = re.match(r"\s*\\", str[i:endpos]) is None
529
530 if not found:
531 # oh well ... settle for moving beyond the first chunk
532 # of non-whitespace chars
533 i = startpos
534 while str[i] not in " \t\n":
535 i = i+1
536
537 return len(string.expandtabs(str[self.stmt_start :
538 i],
539 self.tabwidth)) + 1
540
541 # Return the leading whitespace on the initial line of the last
542 # interesting stmt.
543
544 def get_base_indent_string(self):
545 self._study2()
546 i, n = self.stmt_start, self.stmt_end
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000547 j = i
548 str = self.str
549 while j < n and str[j] in " \t":
550 j = j + 1
551 return str[i:j]
552
553 # Did the last interesting stmt open a block?
554
555 def is_block_opener(self):
556 self._study2()
557 return self.lastch == ':'
558
559 # Did the last interesting stmt close a block?
560
561 def is_block_closer(self):
562 self._study2()
563 return _closere(self.str, self.stmt_start) is not None
Guido van Rossumf4a15081999-06-03 14:32:16 +0000564
565 # index of last open bracket ({[, or None if none
566 lastopenbracketpos = None
567
568 def get_last_open_bracket_pos(self):
569 self._study2()
570 return self.lastopenbracketpos