Given a string and pos, return the number of chars in the identifier which ends at pos, or 0 if there is no such one. This ignores non-identifier eywords are not identifiers.
(cls, str, limit, pos)
| 161 | |
| 162 | @classmethod |
| 163 | def _eat_identifier(cls, str, limit, pos): |
| 164 | """Given a string and pos, return the number of chars in the |
| 165 | identifier which ends at pos, or 0 if there is no such one. |
| 166 | |
| 167 | This ignores non-identifier eywords are not identifiers. |
| 168 | """ |
| 169 | is_ascii_id_char = _IS_ASCII_ID_CHAR |
| 170 | |
| 171 | # Start at the end (pos) and work backwards. |
| 172 | i = pos |
| 173 | |
| 174 | # Go backwards as long as the characters are valid ASCII |
| 175 | # identifier characters. This is an optimization, since it |
| 176 | # is faster in the common case where most of the characters |
| 177 | # are ASCII. |
| 178 | while i > limit and ( |
| 179 | ord(str[i - 1]) < 128 and |
| 180 | is_ascii_id_char[ord(str[i - 1])] |
| 181 | ): |
| 182 | i -= 1 |
| 183 | |
| 184 | # If the above loop ended due to reaching a non-ASCII |
| 185 | # character, continue going backwards using the most generic |
| 186 | # test for whether a string contains only valid identifier |
| 187 | # characters. |
| 188 | if i > limit and ord(str[i - 1]) >= 128: |
| 189 | while i - 4 >= limit and ('a' + str[i - 4:pos]).isidentifier(): |
| 190 | i -= 4 |
| 191 | if i - 2 >= limit and ('a' + str[i - 2:pos]).isidentifier(): |
| 192 | i -= 2 |
| 193 | if i - 1 >= limit and ('a' + str[i - 1:pos]).isidentifier(): |
| 194 | i -= 1 |
| 195 | |
| 196 | # The identifier candidate starts here. If it isn't a valid |
| 197 | # identifier, don't eat anything. At this point that is only |
| 198 | # possible if the first character isn't a valid first |
| 199 | # character for an identifier. |
| 200 | if not str[i:pos].isidentifier(): |
| 201 | return 0 |
| 202 | elif i < pos: |
| 203 | # All characters in str[i:pos] are valid ASCII identifier |
| 204 | # characters, so it is enough to check that the first is |
| 205 | # valid as the first character of an identifier. |
| 206 | if not _IS_ASCII_ID_FIRST_CHAR[ord(str[i])]: |
| 207 | return 0 |
| 208 | |
| 209 | # All keywords are valid identifiers, but should not be |
| 210 | # considered identifiers here, except for True, False and None. |
| 211 | if i < pos and ( |
| 212 | iskeyword(str[i:pos]) and |
| 213 | str[i:pos] not in cls._ID_KEYWORDS |
| 214 | ): |
| 215 | return 0 |
| 216 | |
| 217 | return pos - i |
| 218 | |
| 219 | # This string includes all chars that may be in a white space |
| 220 | _whitespace_chars = " \t\n\\" |