Mercurial > cpython-lru_cache-weakref
annotate Lib/idlelib/PyParse.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | ea9884f14545 |
| children |
| rev | line source |
|---|---|
| 13975 | 1 import re |
| 2 import sys | |
| 3 | |
| 4 # Reason last stmt is continued (or C_NONE if it's not). | |
|
35876
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
5 (C_NONE, C_BACKSLASH, C_STRING_FIRST_LINE, |
|
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
6 C_STRING_NEXT_LINES, C_BRACKET) = range(5) |
| 13975 | 7 |
| 8 if 0: # for throwaway debugging output | |
| 9 def dump(*stuff): | |
| 25301 | 10 sys.__stdout__.write(" ".join(map(str, stuff)) + "\n") |
| 13975 | 11 |
| 12 # Find what looks like the start of a popular stmt. | |
| 13 | |
| 14 _synchre = re.compile(r""" | |
| 15 ^ | |
| 16 [ \t]* | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
17 (?: while |
| 13975 | 18 | else |
| 19 | def | |
| 20 | return | |
| 21 | assert | |
| 22 | break | |
| 23 | class | |
| 24 | continue | |
| 25 | elif | |
| 26 | try | |
| 27 | except | |
| 28 | raise | |
| 29 | import | |
|
18585
4247b4b2c48e
py-cvs-2001_07_13 (Rel 1.9) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
18555
diff
changeset
|
30 | yield |
| 13975 | 31 ) |
| 32 \b | |
| 33 """, re.VERBOSE | re.MULTILINE).search | |
| 34 | |
| 35 # Match blank line or non-indenting comment line. | |
| 36 | |
| 37 _junkre = re.compile(r""" | |
| 38 [ \t]* | |
| 39 (?: \# \S .* )? | |
| 40 \n | |
| 41 """, re.VERBOSE).match | |
| 42 | |
| 43 # Match any flavor of string; the terminating quote is optional | |
| 44 # so that we're robust in the face of incomplete program text. | |
| 45 | |
| 46 _match_stringre = re.compile(r""" | |
| 47 \""" [^"\\]* (?: | |
| 48 (?: \\. | "(?!"") ) | |
| 49 [^"\\]* | |
| 50 )* | |
| 51 (?: \""" )? | |
| 52 | |
| 53 | " [^"\\\n]* (?: \\. [^"\\\n]* )* "? | |
| 54 | |
| 55 | ''' [^'\\]* (?: | |
| 56 (?: \\. | '(?!'') ) | |
| 57 [^'\\]* | |
| 58 )* | |
| 59 (?: ''' )? | |
| 60 | |
| 61 | ' [^'\\\n]* (?: \\. [^'\\\n]* )* '? | |
| 62 """, re.VERBOSE | re.DOTALL).match | |
| 63 | |
| 64 # Match a line that starts with something interesting; | |
| 65 # used to find the first item of a bracket structure. | |
| 66 | |
| 67 _itemre = re.compile(r""" | |
| 68 [ \t]* | |
| 69 [^\s#\\] # if we match, m.end()-1 is the interesting char | |
| 70 """, re.VERBOSE).match | |
| 71 | |
| 72 # Match start of stmts that should be followed by a dedent. | |
| 73 | |
| 74 _closere = re.compile(r""" | |
| 75 \s* | |
| 76 (?: return | |
| 77 | break | |
| 78 | continue | |
| 79 | raise | |
| 80 | pass | |
| 81 ) | |
| 82 \b | |
| 83 """, re.VERBOSE).match | |
| 84 | |
| 85 # Chew up non-special chars as quickly as possible. If match is | |
| 86 # successful, m.end() less 1 is the index of the last boring char | |
| 87 # matched. If match is unsuccessful, the string starts with an | |
| 88 # interesting char. | |
| 89 | |
| 90 _chew_ordinaryre = re.compile(r""" | |
| 91 [^[\](){}#'"\\]+ | |
| 92 """, re.VERBOSE).match | |
| 93 | |
| 94 # Build translation table to map uninteresting chars to "x", open | |
| 95 # brackets to "(", and close brackets to ")". | |
| 96 | |
|
43949
ea9884f14545
Fix an issue with str.translate() in IDLE -- str.translate() only accepts
Guido van Rossum <guido@python.org>
parents:
42569
diff
changeset
|
97 _tran = {} |
|
ea9884f14545
Fix an issue with str.translate() in IDLE -- str.translate() only accepts
Guido van Rossum <guido@python.org>
parents:
42569
diff
changeset
|
98 for i in range(256): |
|
ea9884f14545
Fix an issue with str.translate() in IDLE -- str.translate() only accepts
Guido van Rossum <guido@python.org>
parents:
42569
diff
changeset
|
99 _tran[i] = 'x' |
| 13975 | 100 for ch in "({[": |
| 101 _tran[ord(ch)] = '(' | |
| 102 for ch in ")}]": | |
| 103 _tran[ord(ch)] = ')' | |
| 104 for ch in "\"'\\\n#": | |
| 105 _tran[ord(ch)] = ch | |
|
43949
ea9884f14545
Fix an issue with str.translate() in IDLE -- str.translate() only accepts
Guido van Rossum <guido@python.org>
parents:
42569
diff
changeset
|
106 del i, ch |
| 13975 | 107 |
| 108 class Parser: | |
| 109 | |
| 110 def __init__(self, indentwidth, tabwidth): | |
| 111 self.indentwidth = indentwidth | |
| 112 self.tabwidth = tabwidth | |
| 113 | |
|
42050
ca693f4fd05d
Simplify various spots where: str() is called on something
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
114 def set_str(self, s): |
|
ca693f4fd05d
Simplify various spots where: str() is called on something
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
115 assert len(s) == 0 or s[-1] == '\n' |
|
ca693f4fd05d
Simplify various spots where: str() is called on something
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
116 if isinstance(s, str): |
|
18555
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
117 # The parse functions have no idea what to do with Unicode, so |
|
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
118 # replace all Unicode characters with "x". This is "safe" |
|
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
119 # so long as the only characters germane to parsing the structure |
|
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
120 # of Python are 7-bit ASCII. It's *necessary* because Unicode |
|
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
121 # strings don't have a .translate() method that supports |
|
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
122 # deletechars. |
|
42050
ca693f4fd05d
Simplify various spots where: str() is called on something
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
123 uniphooey = s |
| 42569 | 124 s = [] |
|
42050
ca693f4fd05d
Simplify various spots where: str() is called on something
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
125 push = s.append |
|
18555
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
126 for raw in map(ord, uniphooey): |
|
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
127 push(raw < 127 and chr(raw) or "x") |
|
42050
ca693f4fd05d
Simplify various spots where: str() is called on something
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
128 s = "".join(s) |
|
ca693f4fd05d
Simplify various spots where: str() is called on something
Walter Dörwald <walter@livinglogic.de>
parents:
41575
diff
changeset
|
129 self.str = s |
| 13975 | 130 self.study_level = 0 |
| 131 | |
| 132 # Return index of a good place to begin parsing, as close to the | |
| 133 # end of the string as possible. This will be the start of some | |
| 134 # popular stmt like "if" or "def". Return None if none found: | |
| 135 # the caller should pass more prior context then, if possible, or | |
| 136 # if not (the entire program text up until the point of interest | |
| 137 # has already been tried) pass 0 to set_lo. | |
| 138 # | |
| 139 # This will be reliable iff given a reliable is_char_in_string | |
| 140 # function, meaning that when it says "no", it's absolutely | |
| 141 # guaranteed that the char is not in a string. | |
| 142 | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
143 def find_good_parse_start(self, is_char_in_string=None, |
| 13975 | 144 _synchre=_synchre): |
| 145 str, pos = self.str, None | |
| 146 | |
| 147 if not is_char_in_string: | |
| 148 # no clue -- make the caller pass everything | |
| 149 return None | |
| 150 | |
| 151 # Peek back from the end for a good place to start, | |
| 152 # but don't try too often; pos will be left None, or | |
| 153 # bumped to a legitimate synch point. | |
| 154 limit = len(str) | |
| 155 for tries in range(5): | |
| 25301 | 156 i = str.rfind(":\n", 0, limit) |
| 13975 | 157 if i < 0: |
| 158 break | |
| 25301 | 159 i = str.rfind('\n', 0, i) + 1 # start of colon line |
| 13975 | 160 m = _synchre(str, i, limit) |
| 161 if m and not is_char_in_string(m.start()): | |
| 162 pos = m.start() | |
| 163 break | |
| 164 limit = i | |
| 165 if pos is None: | |
| 166 # Nothing looks like a block-opener, or stuff does | |
| 167 # but is_char_in_string keeps returning true; most likely | |
| 168 # we're in or near a giant string, the colorizer hasn't | |
| 169 # caught up enough to be helpful, or there simply *aren't* | |
| 170 # any interesting stmts. In any of these cases we're | |
| 171 # going to have to parse the whole thing to be sure, so | |
| 172 # give it one last try from the start, but stop wasting | |
| 173 # time here regardless of the outcome. | |
| 174 m = _synchre(str) | |
| 175 if m and not is_char_in_string(m.start()): | |
| 176 pos = m.start() | |
| 177 return pos | |
| 178 | |
| 179 # Peeking back worked; look forward until _synchre no longer | |
| 180 # matches. | |
| 181 i = pos + 1 | |
| 182 while 1: | |
| 183 m = _synchre(str, i) | |
| 184 if m: | |
| 185 s, i = m.span() | |
| 186 if not is_char_in_string(s): | |
| 187 pos = s | |
| 188 else: | |
| 189 break | |
| 190 return pos | |
| 191 | |
| 192 # Throw away the start of the string. Intended to be called with | |
| 193 # find_good_parse_start's result. | |
| 194 | |
| 195 def set_lo(self, lo): | |
| 196 assert lo == 0 or self.str[lo-1] == '\n' | |
| 197 if lo > 0: | |
| 198 self.str = self.str[lo:] | |
| 199 | |
| 200 # As quickly as humanly possible <wink>, find the line numbers (0- | |
| 201 # based) of the non-continuation lines. | |
| 202 # Creates self.{goodlines, continuation}. | |
| 203 | |
| 25301 | 204 def _study1(self): |
| 13975 | 205 if self.study_level >= 1: |
| 206 return | |
| 207 self.study_level = 1 | |
| 208 | |
| 209 # Map all uninteresting characters to "x", all open brackets | |
| 210 # to "(", all close brackets to ")", then collapse runs of | |
| 211 # uninteresting characters. This can cut the number of chars | |
| 212 # by a factor of 10-40, and so greatly speed the following loop. | |
| 213 str = self.str | |
| 25301 | 214 str = str.translate(_tran) |
| 215 str = str.replace('xxxxxxxx', 'x') | |
| 216 str = str.replace('xxxx', 'x') | |
| 217 str = str.replace('xx', 'x') | |
| 218 str = str.replace('xx', 'x') | |
| 219 str = str.replace('\nx', '\n') | |
| 13975 | 220 # note that replacing x\n with \n would be incorrect, because |
| 221 # x may be preceded by a backslash | |
| 222 | |
| 223 # March over the squashed version of the program, accumulating | |
| 224 # the line numbers of non-continued stmts, and determining | |
| 225 # whether & why the last stmt is a continuation. | |
| 226 continuation = C_NONE | |
| 227 level = lno = 0 # level is nesting level; lno is line number | |
| 228 self.goodlines = goodlines = [0] | |
| 229 push_good = goodlines.append | |
| 230 i, n = 0, len(str) | |
| 231 while i < n: | |
| 232 ch = str[i] | |
| 233 i = i+1 | |
| 234 | |
| 235 # cases are checked in decreasing order of frequency | |
| 236 if ch == 'x': | |
| 237 continue | |
| 238 | |
| 239 if ch == '\n': | |
| 240 lno = lno + 1 | |
| 241 if level == 0: | |
| 242 push_good(lno) | |
| 243 # else we're in an unclosed bracket structure | |
| 244 continue | |
| 245 | |
| 246 if ch == '(': | |
| 247 level = level + 1 | |
| 248 continue | |
| 249 | |
| 250 if ch == ')': | |
| 251 if level: | |
| 252 level = level - 1 | |
| 253 # else the program is invalid, but we can't complain | |
| 254 continue | |
| 255 | |
| 256 if ch == '"' or ch == "'": | |
| 257 # consume the string | |
| 258 quote = ch | |
| 259 if str[i-1:i+2] == quote * 3: | |
| 260 quote = quote * 3 | |
|
35876
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
261 firstlno = lno |
| 13975 | 262 w = len(quote) - 1 |
| 263 i = i+w | |
| 264 while i < n: | |
| 265 ch = str[i] | |
| 266 i = i+1 | |
| 267 | |
| 268 if ch == 'x': | |
| 269 continue | |
| 270 | |
| 271 if str[i-1:i+w] == quote: | |
| 272 i = i+w | |
| 273 break | |
| 274 | |
| 275 if ch == '\n': | |
| 276 lno = lno + 1 | |
| 277 if w == 0: | |
| 278 # unterminated single-quoted string | |
| 279 if level == 0: | |
| 280 push_good(lno) | |
| 281 break | |
| 282 continue | |
| 283 | |
| 284 if ch == '\\': | |
| 285 assert i < n | |
| 286 if str[i] == '\n': | |
| 287 lno = lno + 1 | |
| 288 i = i+1 | |
| 289 continue | |
| 290 | |
| 291 # else comment char or paren inside string | |
| 292 | |
| 293 else: | |
| 294 # didn't break out of the loop, so we're still | |
| 295 # inside a string | |
|
35876
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
296 if (lno - 1) == firstlno: |
|
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
297 # before the previous \n in str, we were in the first |
|
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
298 # line of the string |
|
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
299 continuation = C_STRING_FIRST_LINE |
|
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
300 else: |
|
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
301 continuation = C_STRING_NEXT_LINES |
| 13975 | 302 continue # with outer loop |
| 303 | |
| 304 if ch == '#': | |
| 305 # consume the comment | |
| 25301 | 306 i = str.find('\n', i) |
| 13975 | 307 assert i >= 0 |
| 308 continue | |
| 309 | |
| 310 assert ch == '\\' | |
| 311 assert i < n | |
| 312 if str[i] == '\n': | |
| 313 lno = lno + 1 | |
| 314 if i+1 == n: | |
| 315 continuation = C_BACKSLASH | |
| 316 i = i+1 | |
| 317 | |
| 318 # The last stmt may be continued for all 3 reasons. | |
| 319 # String continuation takes precedence over bracket | |
| 320 # continuation, which beats backslash continuation. | |
|
35876
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
321 if (continuation != C_STRING_FIRST_LINE |
|
f334efe76f6a
Better indentation after first line of string continuation.
Kurt B. Kaiser <kbk@shore.net>
parents:
25301
diff
changeset
|
322 and continuation != C_STRING_NEXT_LINES and level > 0): |
| 13975 | 323 continuation = C_BRACKET |
| 324 self.continuation = continuation | |
| 325 | |
| 326 # Push the final line number as a sentinel value, regardless of | |
| 327 # whether it's continued. | |
| 328 assert (continuation == C_NONE) == (goodlines[-1] == lno) | |
| 329 if goodlines[-1] != lno: | |
| 330 push_good(lno) | |
| 331 | |
| 332 def get_continuation_type(self): | |
| 333 self._study1() | |
| 334 return self.continuation | |
| 335 | |
| 336 # study1 was sufficient to determine the continuation status, | |
| 337 # but doing more requires looking at every character. study2 | |
| 338 # does this for the last interesting statement in the block. | |
| 339 # Creates: | |
| 340 # self.stmt_start, stmt_end | |
| 341 # slice indices of last interesting stmt | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
342 # self.stmt_bracketing |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
343 # the bracketing structure of the last interesting stmt; |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
344 # for example, for the statement "say(boo) or die", stmt_bracketing |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
345 # will be [(0, 0), (3, 1), (8, 0)]. Strings and comments are |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
346 # treated as brackets, for the matter. |
| 13975 | 347 # self.lastch |
| 348 # last non-whitespace character before optional trailing | |
| 349 # comment | |
| 350 # self.lastopenbracketpos | |
| 351 # if continuation is C_BRACKET, index of last open bracket | |
| 352 | |
| 25301 | 353 def _study2(self): |
| 13975 | 354 if self.study_level >= 2: |
| 355 return | |
| 356 self._study1() | |
| 357 self.study_level = 2 | |
| 358 | |
| 359 # Set p and q to slice indices of last interesting stmt. | |
| 360 str, goodlines = self.str, self.goodlines | |
| 361 i = len(goodlines) - 1 | |
| 362 p = len(str) # index of newest line | |
| 363 while i: | |
| 364 assert p | |
| 365 # p is the index of the stmt at line number goodlines[i]. | |
| 366 # Move p back to the stmt at line number goodlines[i-1]. | |
| 367 q = p | |
| 368 for nothing in range(goodlines[i-1], goodlines[i]): | |
| 369 # tricky: sets p to 0 if no preceding newline | |
| 25301 | 370 p = str.rfind('\n', 0, p-1) + 1 |
| 13975 | 371 # The stmt str[p:q] isn't a continuation, but may be blank |
| 372 # or a non-indenting comment line. | |
| 373 if _junkre(str, p): | |
| 374 i = i-1 | |
| 375 else: | |
| 376 break | |
| 377 if i == 0: | |
| 378 # nothing but junk! | |
| 379 assert p == 0 | |
| 380 q = p | |
| 381 self.stmt_start, self.stmt_end = p, q | |
| 382 | |
| 383 # Analyze this stmt, to find the last open bracket (if any) | |
| 384 # and last interesting character (if any). | |
| 385 lastch = "" | |
| 386 stack = [] # stack of open bracket indices | |
| 387 push_stack = stack.append | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
388 bracketing = [(p, 0)] |
| 13975 | 389 while p < q: |
| 390 # suck up all except ()[]{}'"#\\ | |
| 391 m = _chew_ordinaryre(str, p, q) | |
| 392 if m: | |
| 393 # we skipped at least one boring char | |
|
18555
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
394 newp = m.end() |
| 13975 | 395 # back up over totally boring whitespace |
|
18555
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
396 i = newp - 1 # index of last boring char |
|
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
397 while i >= p and str[i] in " \t\n": |
| 13975 | 398 i = i-1 |
|
18555
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
399 if i >= p: |
| 13975 | 400 lastch = str[i] |
|
18555
a06831b35407
py-cvs-rel2_1 (Rev 1.6 - 1.8) merge
Kurt B. Kaiser <kbk@shore.net>
parents:
13975
diff
changeset
|
401 p = newp |
| 13975 | 402 if p >= q: |
| 403 break | |
| 404 | |
| 405 ch = str[p] | |
| 406 | |
| 407 if ch in "([{": | |
| 408 push_stack(p) | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
409 bracketing.append((p, len(stack))) |
| 13975 | 410 lastch = ch |
| 411 p = p+1 | |
| 412 continue | |
| 413 | |
| 414 if ch in ")]}": | |
| 415 if stack: | |
| 416 del stack[-1] | |
| 417 lastch = ch | |
| 418 p = p+1 | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
419 bracketing.append((p, len(stack))) |
| 13975 | 420 continue |
| 421 | |
| 422 if ch == '"' or ch == "'": | |
| 423 # consume string | |
| 424 # Note that study1 did this with a Python loop, but | |
| 425 # we use a regexp here; the reason is speed in both | |
| 426 # cases; the string may be huge, but study1 pre-squashed | |
| 427 # strings to a couple of characters per line. study1 | |
| 428 # also needed to keep track of newlines, and we don't | |
| 429 # have to. | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
430 bracketing.append((p, len(stack)+1)) |
| 13975 | 431 lastch = ch |
| 432 p = _match_stringre(str, p, q).end() | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
433 bracketing.append((p, len(stack))) |
| 13975 | 434 continue |
| 435 | |
| 436 if ch == '#': | |
| 437 # consume comment and trailing newline | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
438 bracketing.append((p, len(stack)+1)) |
| 25301 | 439 p = str.find('\n', p, q) + 1 |
| 13975 | 440 assert p > 0 |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
441 bracketing.append((p, len(stack))) |
| 13975 | 442 continue |
| 443 | |
| 444 assert ch == '\\' | |
| 445 p = p+1 # beyond backslash | |
| 446 assert p < q | |
| 447 if str[p] != '\n': | |
| 448 # the program is invalid, but can't complain | |
| 449 lastch = ch + str[p] | |
| 450 p = p+1 # beyond escaped char | |
| 451 | |
| 452 # end while p < q: | |
| 453 | |
| 454 self.lastch = lastch | |
| 455 if stack: | |
| 456 self.lastopenbracketpos = stack[-1] | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
457 self.stmt_bracketing = tuple(bracketing) |
| 13975 | 458 |
| 459 # Assuming continuation is C_BRACKET, return the number | |
| 460 # of spaces the next line should be indented. | |
| 461 | |
| 25301 | 462 def compute_bracket_indent(self): |
| 13975 | 463 self._study2() |
| 464 assert self.continuation == C_BRACKET | |
| 465 j = self.lastopenbracketpos | |
| 466 str = self.str | |
| 467 n = len(str) | |
| 25301 | 468 origi = i = str.rfind('\n', 0, j) + 1 |
| 13975 | 469 j = j+1 # one beyond open bracket |
| 470 # find first list item; set i to start of its line | |
| 471 while j < n: | |
| 472 m = _itemre(str, j) | |
| 473 if m: | |
| 474 j = m.end() - 1 # index of first interesting char | |
| 475 extra = 0 | |
| 476 break | |
| 477 else: | |
| 478 # this line is junk; advance to next line | |
| 25301 | 479 i = j = str.find('\n', j) + 1 |
| 13975 | 480 else: |
| 481 # nothing interesting follows the bracket; | |
| 482 # reproduce the bracket line's indentation + a level | |
| 483 j = i = origi | |
| 484 while str[j] in " \t": | |
| 485 j = j+1 | |
| 486 extra = self.indentwidth | |
| 25301 | 487 return len(str[i:j].expandtabs(self.tabwidth)) + extra |
| 13975 | 488 |
| 489 # Return number of physical lines in last stmt (whether or not | |
| 490 # it's an interesting stmt! this is intended to be called when | |
| 491 # continuation is C_BACKSLASH). | |
| 492 | |
| 493 def get_num_lines_in_stmt(self): | |
| 494 self._study1() | |
| 495 goodlines = self.goodlines | |
| 496 return goodlines[-1] - goodlines[-2] | |
| 497 | |
| 498 # Assuming continuation is C_BACKSLASH, return the number of spaces | |
| 499 # the next line should be indented. Also assuming the new line is | |
| 500 # the first one following the initial line of the stmt. | |
| 501 | |
| 502 def compute_backslash_indent(self): | |
| 503 self._study2() | |
| 504 assert self.continuation == C_BACKSLASH | |
| 505 str = self.str | |
| 506 i = self.stmt_start | |
| 507 while str[i] in " \t": | |
| 508 i = i+1 | |
| 509 startpos = i | |
| 510 | |
| 511 # See whether the initial line starts an assignment stmt; i.e., | |
| 512 # look for an = operator | |
| 25301 | 513 endpos = str.find('\n', startpos) + 1 |
| 13975 | 514 found = level = 0 |
| 515 while i < endpos: | |
| 516 ch = str[i] | |
| 517 if ch in "([{": | |
| 518 level = level + 1 | |
| 519 i = i+1 | |
| 520 elif ch in ")]}": | |
| 521 if level: | |
| 522 level = level - 1 | |
| 523 i = i+1 | |
| 524 elif ch == '"' or ch == "'": | |
| 525 i = _match_stringre(str, i, endpos).end() | |
| 526 elif ch == '#': | |
| 527 break | |
| 528 elif level == 0 and ch == '=' and \ | |
| 529 (i == 0 or str[i-1] not in "=<>!") and \ | |
| 530 str[i+1] != '=': | |
| 531 found = 1 | |
| 532 break | |
| 533 else: | |
| 534 i = i+1 | |
| 535 | |
| 536 if found: | |
| 537 # found a legit =, but it may be the last interesting | |
| 538 # thing on the line | |
| 539 i = i+1 # move beyond the = | |
| 540 found = re.match(r"\s*\\", str[i:endpos]) is None | |
| 541 | |
| 542 if not found: | |
| 543 # oh well ... settle for moving beyond the first chunk | |
| 544 # of non-whitespace chars | |
| 545 i = startpos | |
| 546 while str[i] not in " \t\n": | |
| 547 i = i+1 | |
| 548 | |
| 25301 | 549 return len(str[self.stmt_start:i].expandtabs(\ |
| 13975 | 550 self.tabwidth)) + 1 |
| 551 | |
| 552 # Return the leading whitespace on the initial line of the last | |
| 553 # interesting stmt. | |
| 554 | |
| 555 def get_base_indent_string(self): | |
| 556 self._study2() | |
| 557 i, n = self.stmt_start, self.stmt_end | |
| 558 j = i | |
| 559 str = self.str | |
| 560 while j < n and str[j] in " \t": | |
| 561 j = j + 1 | |
| 562 return str[i:j] | |
| 563 | |
| 564 # Did the last interesting stmt open a block? | |
| 565 | |
| 566 def is_block_opener(self): | |
| 567 self._study2() | |
| 568 return self.lastch == ':' | |
| 569 | |
| 570 # Did the last interesting stmt close a block? | |
| 571 | |
| 572 def is_block_closer(self): | |
| 573 self._study2() | |
| 574 return _closere(self.str, self.stmt_start) is not None | |
| 575 | |
| 576 # index of last open bracket ({[, or None if none | |
| 577 lastopenbracketpos = None | |
| 578 | |
| 579 def get_last_open_bracket_pos(self): | |
| 580 self._study2() | |
| 581 return self.lastopenbracketpos | |
|
35891
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
582 |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
583 # the structure of the bracketing of the last interesting statement, |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
584 # in the format defined in _study2, or None if the text didn't contain |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
585 # anything |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
586 stmt_bracketing = None |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
587 |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
588 def get_last_stmt_bracketing(self): |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
589 self._study2() |
|
8fd46b660e66
Merge IDLE-syntax-branch r39668:41449 into trunk
Kurt B. Kaiser <kbk@shore.net>
parents:
35876
diff
changeset
|
590 return self.stmt_bracketing |
