def generate_sexpr()

in experimental/piranha_playground/rule_inference/utils/node_utils.py [0:0]


    def generate_sexpr(node: Node, depth: int = 0, prefix: str = "") -> str:
        """
        Creates a pretty s-expression representation of a given node.

        :param node: Node to generate the s-expression for.
        :param depth: Depth of the node in the AST.
        :param prefix: Prefix string to be appended at the start of the s-expression.
        :return: The generated s-expression.
        """
        indent = " " * depth
        cursor: TreeCursor = node.walk()
        s_exp = indent + f"{prefix}({node.type} "
        next_child = cursor.goto_first_child()

        while next_child:
            child_node: Node = cursor.node
            if child_node.is_named:
                s_exp += "\n"
                prefix = ""
                if cursor.current_field_name():
                    prefix = f"{cursor.current_field_name()}: "
                s_exp += NodeUtils.generate_sexpr(child_node, depth + 1, prefix)
            elif cursor.current_field_name():
                s_exp += "\n" + " " * (depth + 1)
                s_exp += f'{cursor.current_field_name()}: ("{child_node.type}")'
            next_child = cursor.goto_next_sibling()
        return s_exp + ")"