Esempio n. 1
0
def parse_to_ast(source_code: str, source_id: int = 0) -> vy_ast.Module:
    """
    Parses a vyper source string and generates basic vyper AST nodes.

    Parameters
    ----------
    source_code : str
        The vyper source code to parse.
    source_id : int, optional
        Source id to use in the .src member of each node.

    Returns
    -------
    list
        Untyped, unoptimized vyper AST nodes.
    """
    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) from e
    annotate_python_ast(py_ast, source_code, class_types, source_id)

    # Convert to Vyper AST.
    return vy_ast.get_node(py_ast)  # type: ignore
Esempio n. 2
0
def parse_to_ast(source_code: str,
                 source_id: int = 0,
                 contract_name: Optional[str] = None) -> vy_ast.Module:
    """
    Parses a Vyper source string and generates basic Vyper AST nodes.

    Parameters
    ----------
    source_code : str
        The Vyper source code to parse.
    source_id : int, optional
        Source id to use in the `src` member of each node.

    Returns
    -------
    list
        Untyped, unoptimized Vyper AST nodes.
    """
    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 SyntaxException(str(e), source_code, e.lineno, e.offset) from e
    annotate_python_ast(py_ast, source_code, class_types, source_id,
                        contract_name)

    # Convert to Vyper AST.
    return vy_ast.get_node(py_ast)  # type: ignore
Esempio n. 3
0
def get_contract_info(source_code):
    class_types, reformatted_code = pre_parse(source_code)
    py_ast = python_ast.parse(reformatted_code)

    annotate_python_ast(py_ast, reformatted_code, class_types)

    return py_ast, reformatted_code
Esempio n. 4
0
def parse_to_ast(
    source_code: str,
    source_id: int = 0,
    contract_name: Optional[str] = None,
    add_fn_node: Optional[str] = None,
) -> vy_ast.Module:
    """
    Parses a Vyper source string and generates basic Vyper AST nodes.

    Parameters
    ----------
    source_code : str
        The Vyper source code to parse.
    source_id : int, optional
        Source id to use in the `src` member of each node.
    contract_name: str, optional
        Name of contract.
    add_fn_node: str, optional
        If not None, adds a dummy Python AST FunctionDef wrapper node.

    Returns
    -------
    list
        Untyped, unoptimized Vyper AST nodes.
    """
    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 SyntaxException(str(e), source_code, e.lineno, e.offset) from e

    # Add dummy function node to ensure local variables are treated as `AnnAssign`
    # instead of state variables (`VariableDecl`)
    if add_fn_node:
        fn_node = python_ast.FunctionDef(add_fn_node, py_ast.body, [], [])
        fn_node.body = py_ast.body
        fn_node.args = python_ast.arguments(defaults=[])
        py_ast.body = [fn_node]
    annotate_python_ast(py_ast, source_code, class_types, source_id,
                        contract_name)

    # Convert to Vyper AST.
    return vy_ast.get_node(py_ast)  # type: ignore