How to use the asttokens.ASTTokens function in asttokens

To help you get started, we’ve selected a few asttokens examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gristlabs / asttokens / tests / test_mark_tokens.py View on Github external
def create_asttokens(source):
    return ASTTokens(source, parse=True)
github alexmojaki / executing / tests / samples / bird.py View on Github external
def compile(self, source, filename, flags=0):
        traced_file = super(BirdsEye, self).compile(source, filename, flags)
        traced_file.tokens = ASTTokens(source, tree=traced_file.root)
        return traced_file
github Parquery / icontract / icontract / _represent.py View on Github external
line = lines[i]

        if _DECORATOR_RE.match(line) or _DEF_CLASS_RE.match(line):
            decorator_end_lineno = i
            break

    if decorator_end_lineno is None:
        raise SyntaxError(("The next statement following the decorator corresponding to the line {} "
                           "could not be found in file {}: {!r}").format(lineno + 1, filename, lines[lineno]))

    decorator_lines = lines[decorator_lineno:decorator_end_lineno]

    # We need to dedent the decorator and add a dummy decoratee so that we can parse its text as valid source code.
    decorator_text = textwrap.dedent("".join(decorator_lines)) + "def dummy_{}(): pass".format(uuid.uuid4().hex)

    atok = asttokens.ASTTokens(decorator_text, parse=True)

    assert isinstance(atok.tree, ast.Module), "Expected the parsed decorator text to live in an AST module."

    module_node = atok.tree
    assert len(module_node.body) == 1, "Expected the module AST of the decorator text to have a single statement."
    assert isinstance(module_node.body[0], ast.FunctionDef), \
        "Expected the only statement in the AST module corresponding to the decorator text to be a function definition."

    func_def_node = module_node.body[0]

    assert len(func_def_node.decorator_list) == 1, \
        "Expected the function AST node corresponding to the decorator text to have a single decorator."

    assert isinstance(func_def_node.decorator_list[0], ast.Call), \
        "Expected the only decorator in the function definition AST node corresponding to the decorator text " \
        "to be a call node."
github spirali / nedoc / nedoc / parse.py View on Github external
def parse_path(fullpath):
    try:
        with open(fullpath) as f:
            code = f.read()
        atok = asttokens.ASTTokens(code, parse=True)
        return atok, code
    except Exception as e:
        message = "Error occured in file '{}': Error: {}".format(fullpath, e)
        logging.error(message)
        raise Exception("Parsing failed: {}".format(message))
github awslabs / aws-iot-analytics-notebook-containers / iota_notebook_containers / iota_run_nb.py View on Github external
def _sorted_assignments(source):
    tokens = asttokens.ASTTokens(source, parse=True)
    assignments = (_Assignment(n) for n in ast.walk(tokens.tree) if _is_var_assigment(n))
    return sorted(assignments, key = lambda a: a.val_startpos)
github crew102 / reprexpy / reprexpy / reprex.py View on Github external
def _get_input_chunks(code_str, si):
    tok = asttokens.ASTTokens(code_str, parse=True)

    ends = {statement.last_token.end[0] for statement in tok.tree.body}
    ends = list(sorted(ends))

    starts = [i + 1 for i in ends]
    starts.insert(0, 1)
    starts = starts[:-1]

    code_lines = code_str.splitlines()
    schunks = [code_lines[start - 1:end] for start, end in zip(starts, ends)]
    if si:
        schunks = schunks + [
            ['import reprexpy', 'print(reprexpy.SessionInfo())']
        ]
    return schunks
github vyperlang / vyper / vyper / ast_utils.py View on Github external
def parse_to_ast(source_code: str, source_id: int = 0) -> list:
    if '\x00' in source_code:
        raise ParserException('No null bytes (\\x00) allowed in the source code.')
    class_types, reformatted_code = pre_parse(source_code)
    try:
        py_ast = python_ast.parse(reformatted_code)
    except SyntaxError as e:
        # TODO: Ensure 1-to-1 match of source_code:reformatted_code SyntaxErrors
        raise PythonSyntaxException(e, source_code)
    annotate_ast(py_ast, source_code, class_types)
    asttokens.ASTTokens(source_code, tree=py_ast)
    # Convert to Vyper AST.
    vyper_ast = parse_python_ast(
        source_code=source_code,
        node=py_ast,
        source_id=source_id,
    )
    return vyper_ast.body  # type: ignore