Return a string with the Python expression which ends at the given index, which is empty if there is no real one.
(self)
| 220 | _whitespace_chars = " \t\n\\" |
| 221 | |
| 222 | def get_expression(self): |
| 223 | """Return a string with the Python expression which ends at the |
| 224 | given index, which is empty if there is no real one. |
| 225 | """ |
| 226 | if not self.is_in_code(): |
| 227 | raise ValueError("get_expression should only be called " |
| 228 | "if index is inside a code.") |
| 229 | |
| 230 | rawtext = self.rawtext |
| 231 | bracketing = self.bracketing |
| 232 | |
| 233 | brck_index = self.indexbracket |
| 234 | brck_limit = bracketing[brck_index][0] |
| 235 | pos = self.indexinrawtext |
| 236 | |
| 237 | last_identifier_pos = pos |
| 238 | postdot_phase = True |
| 239 | |
| 240 | while True: |
| 241 | # Eat whitespaces, comments, and if postdot_phase is False - a dot |
| 242 | while True: |
| 243 | if pos>brck_limit and rawtext[pos-1] in self._whitespace_chars: |
| 244 | # Eat a whitespace |
| 245 | pos -= 1 |
| 246 | elif (not postdot_phase and |
| 247 | pos > brck_limit and rawtext[pos-1] == '.'): |
| 248 | # Eat a dot |
| 249 | pos -= 1 |
| 250 | postdot_phase = True |
| 251 | # The next line will fail if we are *inside* a comment, |
| 252 | # but we shouldn't be. |
| 253 | elif (pos == brck_limit and brck_index > 0 and |
| 254 | rawtext[bracketing[brck_index-1][0]] == '#'): |
| 255 | # Eat a comment |
| 256 | brck_index -= 2 |
| 257 | brck_limit = bracketing[brck_index][0] |
| 258 | pos = bracketing[brck_index+1][0] |
| 259 | else: |
| 260 | # If we didn't eat anything, quit. |
| 261 | break |
| 262 | |
| 263 | if not postdot_phase: |
| 264 | # We didn't find a dot, so the expression end at the |
| 265 | # last identifier pos. |
| 266 | break |
| 267 | |
| 268 | ret = self._eat_identifier(rawtext, brck_limit, pos) |
| 269 | if ret: |
| 270 | # There is an identifier to eat |
| 271 | pos = pos - ret |
| 272 | last_identifier_pos = pos |
| 273 | # Now, to continue the search, we must find a dot. |
| 274 | postdot_phase = False |
| 275 | # (the loop continues now) |
| 276 | |
| 277 | elif pos == brck_limit: |
| 278 | # We are at a bracketing limit. If it is a closing |
| 279 | # bracket, eat the bracket, otherwise, stop the search. |