예제 #1
0
def gen_free_name(tree: ast.AST,
                  env: SymbolTable,
                  prefix: tp.Optional[str] = None) -> str:
    names = used_names(tree) | env.keys()
    if prefix is not None and prefix not in names:
        return prefix
    elif prefix is None:
        prefix = '__auto_name_'

    f_str = prefix + '{}'
    c = 0
    name = f_str.format(c)
    while name in names:
        c += 1
        name = f_str.format(c)

    return name
예제 #2
0
def gen_free_prefix(tree: ast.AST,
                    env: SymbolTable,
                    preprefix: tp.Optional[str] = None) -> str:
    def check_prefix(prefix: str, used_names: tp.AbstractSet[str]) -> bool:
        return not any(name.startswith(prefix) for name in used_names)

    names = used_names(tree) | env.keys()

    if preprefix is not None and check_prefix(preprefix, names):
        return preprefix
    elif preprefix is None:
        preprefix = '__auto_prefix_'

    f_str = preprefix + '{}'
    c = 0
    prefix = f_str.format(c)
    while not check_prefix(prefix, names):
        c += 1
        prefix = f_str.format(c)

    return prefix
예제 #3
0
def is_free_prefix(tree: ast.AST, env: SymbolTable, prefix: str):
    names = used_names(tree)
    return not any(
        name.startswith(prefix) for name in itertools.chain(names, env.keys()))