blob: c8212b2143691bc7a910bbe89057ebc2e6753fc4 [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
Tim Peters82ac8d12001-06-19 00:28:47 +000032 | yield
Guido van Rossum729afc11999-06-07 14:28:14 +000033 )
Guido van Rossumf4a15081999-06-03 14:32:16 +000034 \b
Guido van Rossum8113cdc1999-06-01 19:49:21 +000035""", re.VERBOSE | re.MULTILINE).search
36
Guido van Rossumbbaba851999-06-01 19:55:34 +000037# Match blank line or non-indenting comment line.
38
Guido van Rossum8113cdc1999-06-01 19:49:21 +000039_junkre = re.compile(r"""
40 [ \t]*
Guido van Rossumbbaba851999-06-01 19:55:34 +000041 (?: \# \S .* )?
Guido van Rossum8113cdc1999-06-01 19:49:21 +000042 \n
43""", re.VERBOSE).match
44
Guido van Rossumbbaba851999-06-01 19:55:34 +000045# Match any flavor of string; the terminating quote is optional
46# so that we're robust in the face of incomplete program text.
47
Guido van Rossum8113cdc1999-06-01 19:49:21 +000048_match_stringre = re.compile(r"""
49 \""" [^"\\]* (?:
50 (?: \\. | "(?!"") )
51 [^"\\]*
52 )*
53 (?: \""" )?
54
55| " [^"\\\n]* (?: \\. [^"\\\n]* )* "?
56
57| ''' [^'\\]* (?:
58 (?: \\. | '(?!'') )
59 [^'\\]*
60 )*
61 (?: ''' )?
62
63| ' [^'\\\n]* (?: \\. [^'\\\n]* )* '?
64""", re.VERBOSE | re.DOTALL).match
65
Guido van Rossumbbaba851999-06-01 19:55:34 +000066# Match a line that starts with something interesting;
67# used to find the first item of a bracket structure.
68
69_itemre = re.compile(r"""
Guido van Rossum8113cdc1999-06-01 19:49:21 +000070 [ \t]*
Guido van Rossumbbaba851999-06-01 19:55:34 +000071 [^\s#\\] # if we match, m.end()-1 is the interesting char
Guido van Rossum8113cdc1999-06-01 19:49:21 +000072""", re.VERBOSE).match
73
Guido van Rossumbbaba851999-06-01 19:55:34 +000074# Match start of stmts that should be followed by a dedent.
75
Guido van Rossum8113cdc1999-06-01 19:49:21 +000076_closere = re.compile(r"""
77 \s*
78 (?: return
79 | break
80 | continue
81 | raise
82 | pass
83 )
84 \b
85""", re.VERBOSE).match
86
Guido van Rossum9428fa62000-03-03 14:51:11 +000087# Chew up non-special chars as quickly as possible. If match is
88# successful, m.end() less 1 is the index of the last boring char
89# matched. If match is unsuccessful, the string starts with an
90# interesting char.
Guido van Rossumbbaba851999-06-01 19:55:34 +000091
92_chew_ordinaryre = re.compile(r"""
Guido van Rossum9428fa62000-03-03 14:51:11 +000093 [^[\](){}#'"\\]+
Guido van Rossumbbaba851999-06-01 19:55:34 +000094""", re.VERBOSE).match
95
Guido van Rossum8113cdc1999-06-01 19:49:21 +000096# Build translation table to map uninteresting chars to "x", open
97# brackets to "(", and close brackets to ")".
98
99_tran = ['x'] * 256
100for ch in "({[":
101 _tran[ord(ch)] = '('
102for ch in ")}]":
103 _tran[ord(ch)] = ')'
104for ch in "\"'\\\n#":
105 _tran[ord(ch)] = ch
106_tran = string.join(_tran, '')
107del ch
108
Fred Drakef5d2fdf2001-02-16 22:13:48 +0000109try:
110 UnicodeType = type(unicode(""))
111except NameError:
112 UnicodeType = None
113
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000114class Parser:
115
116 def __init__(self, indentwidth, tabwidth):
117 self.indentwidth = indentwidth
118 self.tabwidth = tabwidth
119
120 def set_str(self, str):
121 assert len(str) == 0 or str[-1] == '\n'
Fred Drakef5d2fdf2001-02-16 22:13:48 +0000122 if type(str) is UnicodeType:
Tim Petersf2fba872000-10-06 23:09:00 +0000123 # The parse functions have no idea what to do with Unicode, so
124 # replace all Unicode characters with "x". This is "safe"
125 # so long as the only characters germane to parsing the structure
126 # of Python are 7-bit ASCII. It's *necessary* because Unicode
127 # strings don't have a .translate() method that supports
128 # deletechars.
129 uniphooey = str
130 str = []
131 push = str.append
132 for raw in map(ord, uniphooey):
133 push(raw < 127 and chr(raw) or "x")
134 str = "".join(str)
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000135 self.str = str
136 self.study_level = 0
137
Guido van Rossumf4a15081999-06-03 14:32:16 +0000138 # Return index of a good place to begin parsing, as close to the
139 # end of the string as possible. This will be the start of some
Guido van Rossum729afc11999-06-07 14:28:14 +0000140 # popular stmt like "if" or "def". Return None if none found:
141 # the caller should pass more prior context then, if possible, or
142 # if not (the entire program text up until the point of interest
143 # has already been tried) pass 0 to set_lo.
Guido van Rossumf4a15081999-06-03 14:32:16 +0000144 #
145 # This will be reliable iff given a reliable is_char_in_string
Guido van Rossum729afc11999-06-07 14:28:14 +0000146 # function, meaning that when it says "no", it's absolutely
147 # guaranteed that the char is not in a string.
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000148 #
149 # Ack, hack: in the shell window this kills us, because there's
150 # no way to tell the differences between output, >>> etc and
151 # user input. Indeed, IDLE's first output line makes the rest
152 # look like it's in an unclosed paren!:
153 # Python 1.5.2 (#0, Apr 13 1999, ...
154
Guido van Rossum729afc11999-06-07 14:28:14 +0000155 def find_good_parse_start(self, use_ps1, is_char_in_string=None,
156 _rfind=string.rfind,
Guido van Rossumf4a15081999-06-03 14:32:16 +0000157 _synchre=_synchre):
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000158 str, pos = self.str, None
Guido van Rossumbbaba851999-06-01 19:55:34 +0000159 if use_ps1:
Guido van Rossum729afc11999-06-07 14:28:14 +0000160 # shell window
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000161 ps1 = '\n' + sys.ps1
Guido van Rossum729afc11999-06-07 14:28:14 +0000162 i = _rfind(str, ps1)
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000163 if i >= 0:
164 pos = i + len(ps1)
Guido van Rossum729afc11999-06-07 14:28:14 +0000165 # make it look like there's a newline instead
166 # of ps1 at the start -- hacking here once avoids
167 # repeated hackery later
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000168 self.str = str[:pos-1] + '\n' + str[pos:]
Guido van Rossum729afc11999-06-07 14:28:14 +0000169 return pos
170
171 # File window -- real work.
172 if not is_char_in_string:
173 # no clue -- make the caller pass everything
174 return None
175
176 # Peek back from the end for a good place to start,
177 # but don't try too often; pos will be left None, or
178 # bumped to a legitimate synch point.
179 limit = len(str)
180 for tries in range(5):
181 i = _rfind(str, ":\n", 0, limit)
182 if i < 0:
183 break
184 i = _rfind(str, '\n', 0, i) + 1 # start of colon line
185 m = _synchre(str, i, limit)
186 if m and not is_char_in_string(m.start()):
187 pos = m.start()
188 break
189 limit = i
190 if pos is None:
191 # Nothing looks like a block-opener, or stuff does
192 # but is_char_in_string keeps returning true; most likely
193 # we're in or near a giant string, the colorizer hasn't
194 # caught up enough to be helpful, or there simply *aren't*
195 # any interesting stmts. In any of these cases we're
196 # going to have to parse the whole thing to be sure, so
197 # give it one last try from the start, but stop wasting
198 # time here regardless of the outcome.
199 m = _synchre(str)
200 if m and not is_char_in_string(m.start()):
201 pos = m.start()
202 return pos
203
204 # Peeking back worked; look forward until _synchre no longer
205 # matches.
206 i = pos + 1
207 while 1:
208 m = _synchre(str, i)
209 if m:
210 s, i = m.span()
211 if not is_char_in_string(s):
212 pos = s
213 else:
214 break
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000215 return pos
216
217 # Throw away the start of the string. Intended to be called with
Guido van Rossumf4a15081999-06-03 14:32:16 +0000218 # find_good_parse_start's result.
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000219
220 def set_lo(self, lo):
221 assert lo == 0 or self.str[lo-1] == '\n'
222 if lo > 0:
223 self.str = self.str[lo:]
224
225 # As quickly as humanly possible <wink>, find the line numbers (0-
226 # based) of the non-continuation lines.
Guido van Rossumbbaba851999-06-01 19:55:34 +0000227 # Creates self.{goodlines, continuation}.
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000228
229 def _study1(self, _replace=string.replace, _find=string.find):
230 if self.study_level >= 1:
231 return
232 self.study_level = 1
233
234 # Map all uninteresting characters to "x", all open brackets
235 # to "(", all close brackets to ")", then collapse runs of
236 # uninteresting characters. This can cut the number of chars
237 # by a factor of 10-40, and so greatly speed the following loop.
238 str = self.str
239 str = string.translate(str, _tran)
240 str = _replace(str, 'xxxxxxxx', 'x')
241 str = _replace(str, 'xxxx', 'x')
242 str = _replace(str, 'xx', 'x')
243 str = _replace(str, 'xx', 'x')
244 str = _replace(str, '\nx', '\n')
245 # note that replacing x\n with \n would be incorrect, because
246 # x may be preceded by a backslash
247
248 # March over the squashed version of the program, accumulating
249 # the line numbers of non-continued stmts, and determining
250 # whether & why the last stmt is a continuation.
251 continuation = C_NONE
252 level = lno = 0 # level is nesting level; lno is line number
Guido van Rossumbbaba851999-06-01 19:55:34 +0000253 self.goodlines = goodlines = [0]
254 push_good = goodlines.append
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000255 i, n = 0, len(str)
256 while i < n:
257 ch = str[i]
Guido van Rossumbbaba851999-06-01 19:55:34 +0000258 i = i+1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000259
Guido van Rossumbbaba851999-06-01 19:55:34 +0000260 # cases are checked in decreasing order of frequency
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000261 if ch == 'x':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000262 continue
263
264 if ch == '\n':
265 lno = lno + 1
266 if level == 0:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000267 push_good(lno)
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000268 # else we're in an unclosed bracket structure
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000269 continue
270
271 if ch == '(':
272 level = level + 1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000273 continue
274
275 if ch == ')':
276 if level:
277 level = level - 1
278 # else the program is invalid, but we can't complain
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000279 continue
280
281 if ch == '"' or ch == "'":
282 # consume the string
283 quote = ch
Guido van Rossumbbaba851999-06-01 19:55:34 +0000284 if str[i-1:i+2] == quote * 3:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000285 quote = quote * 3
Guido van Rossumbbaba851999-06-01 19:55:34 +0000286 w = len(quote) - 1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000287 i = i+w
288 while i < n:
289 ch = str[i]
Guido van Rossumbbaba851999-06-01 19:55:34 +0000290 i = i+1
291
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000292 if ch == 'x':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000293 continue
294
Guido van Rossumbbaba851999-06-01 19:55:34 +0000295 if str[i-1:i+w] == quote:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000296 i = i+w
297 break
298
299 if ch == '\n':
300 lno = lno + 1
Guido van Rossumbbaba851999-06-01 19:55:34 +0000301 if w == 0:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000302 # unterminated single-quoted string
303 if level == 0:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000304 push_good(lno)
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000305 break
306 continue
307
308 if ch == '\\':
Guido van Rossumbbaba851999-06-01 19:55:34 +0000309 assert i < n
310 if str[i] == '\n':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000311 lno = lno + 1
Guido van Rossumbbaba851999-06-01 19:55:34 +0000312 i = i+1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000313 continue
314
315 # else comment char or paren inside string
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000316
317 else:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000318 # didn't break out of the loop, so we're still
319 # inside a string
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000320 continuation = C_STRING
Guido van Rossumbbaba851999-06-01 19:55:34 +0000321 continue # with outer loop
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000322
323 if ch == '#':
324 # consume the comment
325 i = _find(str, '\n', i)
326 assert i >= 0
327 continue
328
329 assert ch == '\\'
Guido van Rossumbbaba851999-06-01 19:55:34 +0000330 assert i < n
331 if str[i] == '\n':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000332 lno = lno + 1
Guido van Rossumbbaba851999-06-01 19:55:34 +0000333 if i+1 == n:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000334 continuation = C_BACKSLASH
Guido van Rossumbbaba851999-06-01 19:55:34 +0000335 i = i+1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000336
337 # The last stmt may be continued for all 3 reasons.
338 # String continuation takes precedence over bracket
339 # continuation, which beats backslash continuation.
340 if continuation != C_STRING and level > 0:
341 continuation = C_BRACKET
342 self.continuation = continuation
343
Guido van Rossumbbaba851999-06-01 19:55:34 +0000344 # Push the final line number as a sentinel value, regardless of
345 # whether it's continued.
346 assert (continuation == C_NONE) == (goodlines[-1] == lno)
347 if goodlines[-1] != lno:
348 push_good(lno)
349
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000350 def get_continuation_type(self):
351 self._study1()
352 return self.continuation
353
354 # study1 was sufficient to determine the continuation status,
355 # but doing more requires looking at every character. study2
356 # does this for the last interesting statement in the block.
357 # Creates:
358 # self.stmt_start, stmt_end
359 # slice indices of last interesting stmt
360 # self.lastch
361 # last non-whitespace character before optional trailing
362 # comment
363 # self.lastopenbracketpos
364 # if continuation is C_BRACKET, index of last open bracket
365
366 def _study2(self, _rfind=string.rfind, _find=string.find,
367 _ws=string.whitespace):
368 if self.study_level >= 2:
369 return
370 self._study1()
371 self.study_level = 2
372
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000373 # Set p and q to slice indices of last interesting stmt.
Guido van Rossumbbaba851999-06-01 19:55:34 +0000374 str, goodlines = self.str, self.goodlines
375 i = len(goodlines) - 1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000376 p = len(str) # index of newest line
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000377 while i:
378 assert p
Guido van Rossumbbaba851999-06-01 19:55:34 +0000379 # p is the index of the stmt at line number goodlines[i].
380 # Move p back to the stmt at line number goodlines[i-1].
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000381 q = p
Guido van Rossumbbaba851999-06-01 19:55:34 +0000382 for nothing in range(goodlines[i-1], goodlines[i]):
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000383 # tricky: sets p to 0 if no preceding newline
384 p = _rfind(str, '\n', 0, p-1) + 1
385 # The stmt str[p:q] isn't a continuation, but may be blank
386 # or a non-indenting comment line.
387 if _junkre(str, p):
388 i = i-1
389 else:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000390 break
Guido van Rossumbbaba851999-06-01 19:55:34 +0000391 if i == 0:
392 # nothing but junk!
393 assert p == 0
394 q = p
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000395 self.stmt_start, self.stmt_end = p, q
396
397 # Analyze this stmt, to find the last open bracket (if any)
398 # and last interesting character (if any).
Guido van Rossumbbaba851999-06-01 19:55:34 +0000399 lastch = ""
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000400 stack = [] # stack of open bracket indices
401 push_stack = stack.append
402 while p < q:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000403 # suck up all except ()[]{}'"#\\
404 m = _chew_ordinaryre(str, p, q)
405 if m:
Guido van Rossum9428fa62000-03-03 14:51:11 +0000406 # we skipped at least one boring char
Guido van Rossum7f1cd292000-03-13 14:50:24 +0000407 newp = m.end()
Guido van Rossum9428fa62000-03-03 14:51:11 +0000408 # back up over totally boring whitespace
Guido van Rossum7f1cd292000-03-13 14:50:24 +0000409 i = newp - 1 # index of last boring char
410 while i >= p and str[i] in " \t\n":
Guido van Rossum9428fa62000-03-03 14:51:11 +0000411 i = i-1
Guido van Rossum7f1cd292000-03-13 14:50:24 +0000412 if i >= p:
Guido van Rossumbbaba851999-06-01 19:55:34 +0000413 lastch = str[i]
Guido van Rossum7f1cd292000-03-13 14:50:24 +0000414 p = newp
Guido van Rossumbbaba851999-06-01 19:55:34 +0000415 if p >= q:
416 break
417
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000418 ch = str[p]
Guido van Rossumbbaba851999-06-01 19:55:34 +0000419
420 if ch in "([{":
421 push_stack(p)
422 lastch = ch
423 p = p+1
424 continue
425
426 if ch in ")]}":
427 if stack:
428 del stack[-1]
429 lastch = ch
430 p = p+1
431 continue
432
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000433 if ch == '"' or ch == "'":
434 # consume string
435 # Note that study1 did this with a Python loop, but
436 # we use a regexp here; the reason is speed in both
437 # cases; the string may be huge, but study1 pre-squashed
438 # strings to a couple of characters per line. study1
439 # also needed to keep track of newlines, and we don't
440 # have to.
Guido van Rossumbbaba851999-06-01 19:55:34 +0000441 lastch = ch
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000442 p = _match_stringre(str, p, q).end()
443 continue
444
445 if ch == '#':
446 # consume comment and trailing newline
447 p = _find(str, '\n', p, q) + 1
448 assert p > 0
449 continue
450
Guido van Rossumbbaba851999-06-01 19:55:34 +0000451 assert ch == '\\'
452 p = p+1 # beyond backslash
453 assert p < q
454 if str[p] != '\n':
455 # the program is invalid, but can't complain
456 lastch = ch + str[p]
457 p = p+1 # beyond escaped char
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000458
459 # end while p < q:
460
Guido van Rossumbbaba851999-06-01 19:55:34 +0000461 self.lastch = lastch
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000462 if stack:
463 self.lastopenbracketpos = stack[-1]
464
465 # Assuming continuation is C_BRACKET, return the number
466 # of spaces the next line should be indented.
467
468 def compute_bracket_indent(self, _find=string.find):
469 self._study2()
470 assert self.continuation == C_BRACKET
471 j = self.lastopenbracketpos
472 str = self.str
473 n = len(str)
474 origi = i = string.rfind(str, '\n', 0, j) + 1
Guido van Rossumbbaba851999-06-01 19:55:34 +0000475 j = j+1 # one beyond open bracket
476 # find first list item; set i to start of its line
477 while j < n:
478 m = _itemre(str, j)
479 if m:
480 j = m.end() - 1 # index of first interesting char
481 extra = 0
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000482 break
Guido van Rossumbbaba851999-06-01 19:55:34 +0000483 else:
484 # this line is junk; advance to next line
485 i = j = _find(str, '\n', j) + 1
486 else:
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000487 # nothing interesting follows the bracket;
488 # reproduce the bracket line's indentation + a level
489 j = i = origi
Guido van Rossumbbaba851999-06-01 19:55:34 +0000490 while str[j] in " \t":
491 j = j+1
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000492 extra = self.indentwidth
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000493 return len(string.expandtabs(str[i:j],
494 self.tabwidth)) + extra
495
496 # Return number of physical lines in last stmt (whether or not
497 # it's an interesting stmt! this is intended to be called when
498 # continuation is C_BACKSLASH).
499
500 def get_num_lines_in_stmt(self):
501 self._study1()
Guido van Rossumbbaba851999-06-01 19:55:34 +0000502 goodlines = self.goodlines
503 return goodlines[-1] - goodlines[-2]
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000504
505 # Assuming continuation is C_BACKSLASH, return the number of spaces
506 # the next line should be indented. Also assuming the new line is
507 # the first one following the initial line of the stmt.
508
509 def compute_backslash_indent(self):
510 self._study2()
511 assert self.continuation == C_BACKSLASH
512 str = self.str
513 i = self.stmt_start
514 while str[i] in " \t":
515 i = i+1
516 startpos = i
Guido van Rossumbbaba851999-06-01 19:55:34 +0000517
518 # See whether the initial line starts an assignment stmt; i.e.,
519 # look for an = operator
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000520 endpos = string.find(str, '\n', startpos) + 1
521 found = level = 0
522 while i < endpos:
523 ch = str[i]
524 if ch in "([{":
525 level = level + 1
526 i = i+1
527 elif ch in ")]}":
528 if level:
529 level = level - 1
530 i = i+1
531 elif ch == '"' or ch == "'":
532 i = _match_stringre(str, i, endpos).end()
533 elif ch == '#':
534 break
535 elif level == 0 and ch == '=' and \
Guido van Rossumbbaba851999-06-01 19:55:34 +0000536 (i == 0 or str[i-1] not in "=<>!") and \
537 str[i+1] != '=':
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000538 found = 1
539 break
540 else:
541 i = i+1
542
543 if found:
544 # found a legit =, but it may be the last interesting
545 # thing on the line
546 i = i+1 # move beyond the =
547 found = re.match(r"\s*\\", str[i:endpos]) is None
548
549 if not found:
550 # oh well ... settle for moving beyond the first chunk
551 # of non-whitespace chars
552 i = startpos
553 while str[i] not in " \t\n":
554 i = i+1
555
556 return len(string.expandtabs(str[self.stmt_start :
557 i],
558 self.tabwidth)) + 1
559
560 # Return the leading whitespace on the initial line of the last
561 # interesting stmt.
562
563 def get_base_indent_string(self):
564 self._study2()
565 i, n = self.stmt_start, self.stmt_end
Guido van Rossum8113cdc1999-06-01 19:49:21 +0000566 j = i
567 str = self.str
568 while j < n and str[j] in " \t":
569 j = j + 1
570 return str[i:j]
571
572 # Did the last interesting stmt open a block?
573
574 def is_block_opener(self):
575 self._study2()
576 return self.lastch == ':'
577
578 # Did the last interesting stmt close a block?
579
580 def is_block_closer(self):
581 self._study2()
582 return _closere(self.str, self.stmt_start) is not None
Guido van Rossumf4a15081999-06-03 14:32:16 +0000583
584 # index of last open bracket ({[, or None if none
585 lastopenbracketpos = None
586
587 def get_last_open_bracket_pos(self):
588 self._study2()
589 return self.lastopenbracketpos