Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 1 | import string |
| 2 | import re |
| 3 | import sys |
| 4 | |
| 5 | # Reason last stmt is continued (or C_NONE if it's not). |
| 6 | C_NONE, C_BACKSLASH, C_STRING, C_BRACKET = range(4) |
| 7 | |
| 8 | if 0: # for throwaway debugging output |
| 9 | def dump(*stuff): |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 10 | sys.__stdout__.write(string.join(map(str, stuff), " ") + "\n") |
| 11 | |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 12 | # Find what looks like the start of a popular stmt. |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 14 | _synchre = re.compile(r""" |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 15 | ^ |
| 16 | [ \t]* |
Guido van Rossum | 729afc1 | 1999-06-07 14:28:14 +0000 | [diff] [blame^] | 17 | (?: 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 Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 33 | \b |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 34 | """, re.VERBOSE | re.MULTILINE).search |
| 35 | |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 36 | # Match blank line or non-indenting comment line. |
| 37 | |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 38 | _junkre = re.compile(r""" |
| 39 | [ \t]* |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 40 | (?: \# \S .* )? |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 41 | \n |
| 42 | """, re.VERBOSE).match |
| 43 | |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 44 | # 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 Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 47 | _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 Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 65 | # 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 Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 69 | [ \t]* |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 70 | [^\s#\\] # if we match, m.end()-1 is the interesting char |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 71 | """, re.VERBOSE).match |
| 72 | |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 73 | # Match start of stmts that should be followed by a dedent. |
| 74 | |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 75 | _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 Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 86 | # Chew up non-special chars as quickly as possible, but retaining |
| 87 | # enough info to determine the last non-ws char seen; if match is |
| 88 | # successful, and m.group(1) isn't None, m.end(1) less 1 is the |
| 89 | # index of the last non-ws char matched. |
| 90 | |
| 91 | _chew_ordinaryre = re.compile(r""" |
| 92 | (?: \s+ |
| 93 | | ( [^\s[\](){}#'"\\]+ ) |
| 94 | )+ |
| 95 | """, re.VERBOSE).match |
| 96 | |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 97 | # Build translation table to map uninteresting chars to "x", open |
| 98 | # brackets to "(", and close brackets to ")". |
| 99 | |
| 100 | _tran = ['x'] * 256 |
| 101 | for ch in "({[": |
| 102 | _tran[ord(ch)] = '(' |
| 103 | for ch in ")}]": |
| 104 | _tran[ord(ch)] = ')' |
| 105 | for ch in "\"'\\\n#": |
| 106 | _tran[ord(ch)] = ch |
| 107 | _tran = string.join(_tran, '') |
| 108 | del ch |
| 109 | |
| 110 | class Parser: |
| 111 | |
| 112 | def __init__(self, indentwidth, tabwidth): |
| 113 | self.indentwidth = indentwidth |
| 114 | self.tabwidth = tabwidth |
| 115 | |
| 116 | def set_str(self, str): |
| 117 | assert len(str) == 0 or str[-1] == '\n' |
| 118 | self.str = str |
| 119 | self.study_level = 0 |
| 120 | |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 121 | # Return index of a good place to begin parsing, as close to the |
| 122 | # end of the string as possible. This will be the start of some |
Guido van Rossum | 729afc1 | 1999-06-07 14:28:14 +0000 | [diff] [blame^] | 123 | # popular stmt like "if" or "def". Return None if none found: |
| 124 | # the caller should pass more prior context then, if possible, or |
| 125 | # if not (the entire program text up until the point of interest |
| 126 | # has already been tried) pass 0 to set_lo. |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 127 | # |
| 128 | # This will be reliable iff given a reliable is_char_in_string |
Guido van Rossum | 729afc1 | 1999-06-07 14:28:14 +0000 | [diff] [blame^] | 129 | # function, meaning that when it says "no", it's absolutely |
| 130 | # guaranteed that the char is not in a string. |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 131 | # |
| 132 | # Ack, hack: in the shell window this kills us, because there's |
| 133 | # no way to tell the differences between output, >>> etc and |
| 134 | # user input. Indeed, IDLE's first output line makes the rest |
| 135 | # look like it's in an unclosed paren!: |
| 136 | # Python 1.5.2 (#0, Apr 13 1999, ... |
| 137 | |
Guido van Rossum | 729afc1 | 1999-06-07 14:28:14 +0000 | [diff] [blame^] | 138 | def find_good_parse_start(self, use_ps1, is_char_in_string=None, |
| 139 | _rfind=string.rfind, |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 140 | _synchre=_synchre): |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 141 | str, pos = self.str, None |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 142 | if use_ps1: |
Guido van Rossum | 729afc1 | 1999-06-07 14:28:14 +0000 | [diff] [blame^] | 143 | # shell window |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 144 | ps1 = '\n' + sys.ps1 |
Guido van Rossum | 729afc1 | 1999-06-07 14:28:14 +0000 | [diff] [blame^] | 145 | i = _rfind(str, ps1) |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 146 | if i >= 0: |
| 147 | pos = i + len(ps1) |
Guido van Rossum | 729afc1 | 1999-06-07 14:28:14 +0000 | [diff] [blame^] | 148 | # make it look like there's a newline instead |
| 149 | # of ps1 at the start -- hacking here once avoids |
| 150 | # repeated hackery later |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 151 | self.str = str[:pos-1] + '\n' + str[pos:] |
Guido van Rossum | 729afc1 | 1999-06-07 14:28:14 +0000 | [diff] [blame^] | 152 | return pos |
| 153 | |
| 154 | # File window -- real work. |
| 155 | if not is_char_in_string: |
| 156 | # no clue -- make the caller pass everything |
| 157 | return None |
| 158 | |
| 159 | # Peek back from the end for a good place to start, |
| 160 | # but don't try too often; pos will be left None, or |
| 161 | # bumped to a legitimate synch point. |
| 162 | limit = len(str) |
| 163 | for tries in range(5): |
| 164 | i = _rfind(str, ":\n", 0, limit) |
| 165 | if i < 0: |
| 166 | break |
| 167 | i = _rfind(str, '\n', 0, i) + 1 # start of colon line |
| 168 | m = _synchre(str, i, limit) |
| 169 | if m and not is_char_in_string(m.start()): |
| 170 | pos = m.start() |
| 171 | break |
| 172 | limit = i |
| 173 | if pos is None: |
| 174 | # Nothing looks like a block-opener, or stuff does |
| 175 | # but is_char_in_string keeps returning true; most likely |
| 176 | # we're in or near a giant string, the colorizer hasn't |
| 177 | # caught up enough to be helpful, or there simply *aren't* |
| 178 | # any interesting stmts. In any of these cases we're |
| 179 | # going to have to parse the whole thing to be sure, so |
| 180 | # give it one last try from the start, but stop wasting |
| 181 | # time here regardless of the outcome. |
| 182 | m = _synchre(str) |
| 183 | if m and not is_char_in_string(m.start()): |
| 184 | pos = m.start() |
| 185 | return pos |
| 186 | |
| 187 | # Peeking back worked; look forward until _synchre no longer |
| 188 | # matches. |
| 189 | i = pos + 1 |
| 190 | while 1: |
| 191 | m = _synchre(str, i) |
| 192 | if m: |
| 193 | s, i = m.span() |
| 194 | if not is_char_in_string(s): |
| 195 | pos = s |
| 196 | else: |
| 197 | break |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 198 | return pos |
| 199 | |
| 200 | # Throw away the start of the string. Intended to be called with |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 201 | # find_good_parse_start's result. |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 202 | |
| 203 | def set_lo(self, lo): |
| 204 | assert lo == 0 or self.str[lo-1] == '\n' |
| 205 | if lo > 0: |
| 206 | self.str = self.str[lo:] |
| 207 | |
| 208 | # As quickly as humanly possible <wink>, find the line numbers (0- |
| 209 | # based) of the non-continuation lines. |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 210 | # Creates self.{goodlines, continuation}. |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 211 | |
| 212 | def _study1(self, _replace=string.replace, _find=string.find): |
| 213 | if self.study_level >= 1: |
| 214 | return |
| 215 | self.study_level = 1 |
| 216 | |
| 217 | # Map all uninteresting characters to "x", all open brackets |
| 218 | # to "(", all close brackets to ")", then collapse runs of |
| 219 | # uninteresting characters. This can cut the number of chars |
| 220 | # by a factor of 10-40, and so greatly speed the following loop. |
| 221 | str = self.str |
| 222 | str = string.translate(str, _tran) |
| 223 | str = _replace(str, 'xxxxxxxx', 'x') |
| 224 | str = _replace(str, 'xxxx', 'x') |
| 225 | str = _replace(str, 'xx', 'x') |
| 226 | str = _replace(str, 'xx', 'x') |
| 227 | str = _replace(str, '\nx', '\n') |
| 228 | # note that replacing x\n with \n would be incorrect, because |
| 229 | # x may be preceded by a backslash |
| 230 | |
| 231 | # March over the squashed version of the program, accumulating |
| 232 | # the line numbers of non-continued stmts, and determining |
| 233 | # whether & why the last stmt is a continuation. |
| 234 | continuation = C_NONE |
| 235 | level = lno = 0 # level is nesting level; lno is line number |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 236 | self.goodlines = goodlines = [0] |
| 237 | push_good = goodlines.append |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 238 | i, n = 0, len(str) |
| 239 | while i < n: |
| 240 | ch = str[i] |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 241 | i = i+1 |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 242 | |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 243 | # cases are checked in decreasing order of frequency |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 244 | if ch == 'x': |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 245 | continue |
| 246 | |
| 247 | if ch == '\n': |
| 248 | lno = lno + 1 |
| 249 | if level == 0: |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 250 | push_good(lno) |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 251 | # else we're in an unclosed bracket structure |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 252 | continue |
| 253 | |
| 254 | if ch == '(': |
| 255 | level = level + 1 |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 256 | continue |
| 257 | |
| 258 | if ch == ')': |
| 259 | if level: |
| 260 | level = level - 1 |
| 261 | # else the program is invalid, but we can't complain |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 262 | continue |
| 263 | |
| 264 | if ch == '"' or ch == "'": |
| 265 | # consume the string |
| 266 | quote = ch |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 267 | if str[i-1:i+2] == quote * 3: |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 268 | quote = quote * 3 |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 269 | w = len(quote) - 1 |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 270 | i = i+w |
| 271 | while i < n: |
| 272 | ch = str[i] |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 273 | i = i+1 |
| 274 | |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 275 | if ch == 'x': |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 276 | continue |
| 277 | |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 278 | if str[i-1:i+w] == quote: |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 279 | i = i+w |
| 280 | break |
| 281 | |
| 282 | if ch == '\n': |
| 283 | lno = lno + 1 |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 284 | if w == 0: |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 285 | # unterminated single-quoted string |
| 286 | if level == 0: |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 287 | push_good(lno) |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 288 | break |
| 289 | continue |
| 290 | |
| 291 | if ch == '\\': |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 292 | assert i < n |
| 293 | if str[i] == '\n': |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 294 | lno = lno + 1 |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 295 | i = i+1 |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 296 | continue |
| 297 | |
| 298 | # else comment char or paren inside string |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 299 | |
| 300 | else: |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 301 | # didn't break out of the loop, so we're still |
| 302 | # inside a string |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 303 | continuation = C_STRING |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 304 | continue # with outer loop |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 305 | |
| 306 | if ch == '#': |
| 307 | # consume the comment |
| 308 | i = _find(str, '\n', i) |
| 309 | assert i >= 0 |
| 310 | continue |
| 311 | |
| 312 | assert ch == '\\' |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 313 | assert i < n |
| 314 | if str[i] == '\n': |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 315 | lno = lno + 1 |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 316 | if i+1 == n: |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 317 | continuation = C_BACKSLASH |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 318 | i = i+1 |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 319 | |
| 320 | # The last stmt may be continued for all 3 reasons. |
| 321 | # String continuation takes precedence over bracket |
| 322 | # continuation, which beats backslash continuation. |
| 323 | if continuation != C_STRING and level > 0: |
| 324 | continuation = C_BRACKET |
| 325 | self.continuation = continuation |
| 326 | |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 327 | # Push the final line number as a sentinel value, regardless of |
| 328 | # whether it's continued. |
| 329 | assert (continuation == C_NONE) == (goodlines[-1] == lno) |
| 330 | if goodlines[-1] != lno: |
| 331 | push_good(lno) |
| 332 | |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 333 | def get_continuation_type(self): |
| 334 | self._study1() |
| 335 | return self.continuation |
| 336 | |
| 337 | # study1 was sufficient to determine the continuation status, |
| 338 | # but doing more requires looking at every character. study2 |
| 339 | # does this for the last interesting statement in the block. |
| 340 | # Creates: |
| 341 | # self.stmt_start, stmt_end |
| 342 | # slice indices of last interesting stmt |
| 343 | # self.lastch |
| 344 | # last non-whitespace character before optional trailing |
| 345 | # comment |
| 346 | # self.lastopenbracketpos |
| 347 | # if continuation is C_BRACKET, index of last open bracket |
| 348 | |
| 349 | def _study2(self, _rfind=string.rfind, _find=string.find, |
| 350 | _ws=string.whitespace): |
| 351 | if self.study_level >= 2: |
| 352 | return |
| 353 | self._study1() |
| 354 | self.study_level = 2 |
| 355 | |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 356 | # Set p and q to slice indices of last interesting stmt. |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 357 | str, goodlines = self.str, self.goodlines |
| 358 | i = len(goodlines) - 1 |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 359 | p = len(str) # index of newest line |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 360 | while i: |
| 361 | assert p |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 362 | # p is the index of the stmt at line number goodlines[i]. |
| 363 | # Move p back to the stmt at line number goodlines[i-1]. |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 364 | q = p |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 365 | for nothing in range(goodlines[i-1], goodlines[i]): |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 366 | # tricky: sets p to 0 if no preceding newline |
| 367 | p = _rfind(str, '\n', 0, p-1) + 1 |
| 368 | # The stmt str[p:q] isn't a continuation, but may be blank |
| 369 | # or a non-indenting comment line. |
| 370 | if _junkre(str, p): |
| 371 | i = i-1 |
| 372 | else: |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 373 | break |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 374 | if i == 0: |
| 375 | # nothing but junk! |
| 376 | assert p == 0 |
| 377 | q = p |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 378 | self.stmt_start, self.stmt_end = p, q |
| 379 | |
| 380 | # Analyze this stmt, to find the last open bracket (if any) |
| 381 | # and last interesting character (if any). |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 382 | lastch = "" |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 383 | stack = [] # stack of open bracket indices |
| 384 | push_stack = stack.append |
| 385 | while p < q: |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 386 | # suck up all except ()[]{}'"#\\ |
| 387 | m = _chew_ordinaryre(str, p, q) |
| 388 | if m: |
| 389 | i = m.end(1) - 1 # last non-ws (if any) |
| 390 | if i >= 0: |
| 391 | lastch = str[i] |
| 392 | p = m.end() |
| 393 | if p >= q: |
| 394 | break |
| 395 | |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 396 | ch = str[p] |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 397 | |
| 398 | if ch in "([{": |
| 399 | push_stack(p) |
| 400 | lastch = ch |
| 401 | p = p+1 |
| 402 | continue |
| 403 | |
| 404 | if ch in ")]}": |
| 405 | if stack: |
| 406 | del stack[-1] |
| 407 | lastch = ch |
| 408 | p = p+1 |
| 409 | continue |
| 410 | |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 411 | if ch == '"' or ch == "'": |
| 412 | # consume string |
| 413 | # Note that study1 did this with a Python loop, but |
| 414 | # we use a regexp here; the reason is speed in both |
| 415 | # cases; the string may be huge, but study1 pre-squashed |
| 416 | # strings to a couple of characters per line. study1 |
| 417 | # also needed to keep track of newlines, and we don't |
| 418 | # have to. |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 419 | lastch = ch |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 420 | p = _match_stringre(str, p, q).end() |
| 421 | continue |
| 422 | |
| 423 | if ch == '#': |
| 424 | # consume comment and trailing newline |
| 425 | p = _find(str, '\n', p, q) + 1 |
| 426 | assert p > 0 |
| 427 | continue |
| 428 | |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 429 | assert ch == '\\' |
| 430 | p = p+1 # beyond backslash |
| 431 | assert p < q |
| 432 | if str[p] != '\n': |
| 433 | # the program is invalid, but can't complain |
| 434 | lastch = ch + str[p] |
| 435 | p = p+1 # beyond escaped char |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 436 | |
| 437 | # end while p < q: |
| 438 | |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 439 | self.lastch = lastch |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 440 | if stack: |
| 441 | self.lastopenbracketpos = stack[-1] |
| 442 | |
| 443 | # Assuming continuation is C_BRACKET, return the number |
| 444 | # of spaces the next line should be indented. |
| 445 | |
| 446 | def compute_bracket_indent(self, _find=string.find): |
| 447 | self._study2() |
| 448 | assert self.continuation == C_BRACKET |
| 449 | j = self.lastopenbracketpos |
| 450 | str = self.str |
| 451 | n = len(str) |
| 452 | origi = i = string.rfind(str, '\n', 0, j) + 1 |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 453 | j = j+1 # one beyond open bracket |
| 454 | # find first list item; set i to start of its line |
| 455 | while j < n: |
| 456 | m = _itemre(str, j) |
| 457 | if m: |
| 458 | j = m.end() - 1 # index of first interesting char |
| 459 | extra = 0 |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 460 | break |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 461 | else: |
| 462 | # this line is junk; advance to next line |
| 463 | i = j = _find(str, '\n', j) + 1 |
| 464 | else: |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 465 | # nothing interesting follows the bracket; |
| 466 | # reproduce the bracket line's indentation + a level |
| 467 | j = i = origi |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 468 | while str[j] in " \t": |
| 469 | j = j+1 |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 470 | extra = self.indentwidth |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 471 | return len(string.expandtabs(str[i:j], |
| 472 | self.tabwidth)) + extra |
| 473 | |
| 474 | # Return number of physical lines in last stmt (whether or not |
| 475 | # it's an interesting stmt! this is intended to be called when |
| 476 | # continuation is C_BACKSLASH). |
| 477 | |
| 478 | def get_num_lines_in_stmt(self): |
| 479 | self._study1() |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 480 | goodlines = self.goodlines |
| 481 | return goodlines[-1] - goodlines[-2] |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 482 | |
| 483 | # Assuming continuation is C_BACKSLASH, return the number of spaces |
| 484 | # the next line should be indented. Also assuming the new line is |
| 485 | # the first one following the initial line of the stmt. |
| 486 | |
| 487 | def compute_backslash_indent(self): |
| 488 | self._study2() |
| 489 | assert self.continuation == C_BACKSLASH |
| 490 | str = self.str |
| 491 | i = self.stmt_start |
| 492 | while str[i] in " \t": |
| 493 | i = i+1 |
| 494 | startpos = i |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 495 | |
| 496 | # See whether the initial line starts an assignment stmt; i.e., |
| 497 | # look for an = operator |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 498 | endpos = string.find(str, '\n', startpos) + 1 |
| 499 | found = level = 0 |
| 500 | while i < endpos: |
| 501 | ch = str[i] |
| 502 | if ch in "([{": |
| 503 | level = level + 1 |
| 504 | i = i+1 |
| 505 | elif ch in ")]}": |
| 506 | if level: |
| 507 | level = level - 1 |
| 508 | i = i+1 |
| 509 | elif ch == '"' or ch == "'": |
| 510 | i = _match_stringre(str, i, endpos).end() |
| 511 | elif ch == '#': |
| 512 | break |
| 513 | elif level == 0 and ch == '=' and \ |
Guido van Rossum | bbaba85 | 1999-06-01 19:55:34 +0000 | [diff] [blame] | 514 | (i == 0 or str[i-1] not in "=<>!") and \ |
| 515 | str[i+1] != '=': |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 516 | found = 1 |
| 517 | break |
| 518 | else: |
| 519 | i = i+1 |
| 520 | |
| 521 | if found: |
| 522 | # found a legit =, but it may be the last interesting |
| 523 | # thing on the line |
| 524 | i = i+1 # move beyond the = |
| 525 | found = re.match(r"\s*\\", str[i:endpos]) is None |
| 526 | |
| 527 | if not found: |
| 528 | # oh well ... settle for moving beyond the first chunk |
| 529 | # of non-whitespace chars |
| 530 | i = startpos |
| 531 | while str[i] not in " \t\n": |
| 532 | i = i+1 |
| 533 | |
| 534 | return len(string.expandtabs(str[self.stmt_start : |
| 535 | i], |
| 536 | self.tabwidth)) + 1 |
| 537 | |
| 538 | # Return the leading whitespace on the initial line of the last |
| 539 | # interesting stmt. |
| 540 | |
| 541 | def get_base_indent_string(self): |
| 542 | self._study2() |
| 543 | i, n = self.stmt_start, self.stmt_end |
Guido van Rossum | 8113cdc | 1999-06-01 19:49:21 +0000 | [diff] [blame] | 544 | j = i |
| 545 | str = self.str |
| 546 | while j < n and str[j] in " \t": |
| 547 | j = j + 1 |
| 548 | return str[i:j] |
| 549 | |
| 550 | # Did the last interesting stmt open a block? |
| 551 | |
| 552 | def is_block_opener(self): |
| 553 | self._study2() |
| 554 | return self.lastch == ':' |
| 555 | |
| 556 | # Did the last interesting stmt close a block? |
| 557 | |
| 558 | def is_block_closer(self): |
| 559 | self._study2() |
| 560 | return _closere(self.str, self.stmt_start) is not None |
Guido van Rossum | f4a1508 | 1999-06-03 14:32:16 +0000 | [diff] [blame] | 561 | |
| 562 | # index of last open bracket ({[, or None if none |
| 563 | lastopenbracketpos = None |
| 564 | |
| 565 | def get_last_open_bracket_pos(self): |
| 566 | self._study2() |
| 567 | return self.lastopenbracketpos |