astimprove.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:python-driver 作者: bblfsh 项目源码 文件源码
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))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号