def _pop_token(self, lineno: int, token_value: str) -> Token:
tokensline = self._lines[lineno - 1]
# Pop the first token with the same name in the same line
for t in tokensline:
if t.name != 'STRING':
line_value = t.value
else:
if t.value[0] == 'f' and t.value[1] in ('"', "'"):
# fstring: token identify as STRING but they parse into the AST as a
# collection of nodes so the token_value is different. To find the
# real token position we'll search inside the fstring token value.
tok_subpos = t.value.find(token_value)
if tok_subpos != -1:
# We don't remove the fstring token from the line in this case; other
# nodes could match different parts of it
newtok = deepcopy(t)
newtok.start.col = t.start.col + tok_subpos
return newtok
raise TokenNotFoundException("Could not find token '{}' inside f-string '{}'"
.format(token_value, t.value))
else:
# normal string; they include the single or double quotes so we liteval
line_value = literal_eval(t.value)
if str(line_value) == str(token_value):
tokensline.remove(t)
return t
raise TokenNotFoundException("Token named '{}' not found in line {}"
.format(token_value, lineno))
评论列表
文章目录