def parsed_to_ast(parsed: Parsed, errors: Errors, component_type: str = ""):
    """Convert parsed data struct to AST dictionary

    Args:
        parsed:
        errors:
        component_type: Empty string or 'subject' or 'object' to indicate that we
            are parsing the subject or object field input
    """

    ast = {}
    sorted_keys = sorted(parsed.keys())

    # Setup top-level tree
    for key in sorted_keys:
        if parsed[key]["type"] == "Nested":
            nested_component_stack = ["subject", "object"]

    if component_type:
        component_stack = [component_type]
    else:
        component_stack = ["subject", "object"]

    for key in sorted_keys:
        if parsed[key]["type"] == "Function" and parsed[key]["function_level"] == "top":
            ast[component_stack.pop(0)] = parsed_function_to_ast(parsed, key)
        elif parsed[key]["type"] == "Relation" and "relation" not in ast:
            ast["relation"] = {
                "name": parsed[key]["name"],
                "type": "Relation",
                "span": key,
            }
        elif parsed[key]["type"] == "Nested":
            ast["nested"] = {}
            for nested_key in sorted_keys:
                if nested_key <= key:
                    continue

                if (
                    parsed[nested_key]["type"] == "Function"
                    and parsed[nested_key]["function_level"] == "top"
                ):
                    ast["nested"][
                        nested_component_stack.pop(0)
                    ] = parsed_function_to_ast(parsed, nested_key)
                elif (
                    parsed[nested_key]["type"] == "Relation"
                    and "relation" not in ast["nested"]
                ):
                    ast["nested"]["relation"] = {
                        "name": parsed[nested_key]["name"],
                        "type": "Relation",
                        "span": parsed[nested_key]["span"],
                    }

            return ast, errors

    return ast, errors
Convert parsed data struct to AST dictionary

    Args:
        parsed:
        errors:
        component_type: Empty string or 'subject' or 'object' to indicate that we
            are parsing the subject or object field input