# -*- coding: utf-8 -*- class SimpleAST: def __init__(self): pass def create_simple_ast(self,ast): elems = {"functions": "", "typedefs": "", "structs": "", "enums": ""} elems["functions"] = self.extract_functions_from_ast(ast) elems["typedefs"] = self.extract_typedefs_from_ast(ast) elems["structs"] = self.extract_structs_from_ast(ast) elems["enums"] = self.extract_enums_from_ast(ast) return elems # extracts top level functions only (is there anything else in C?) def extract_functions_from_ast(self,ast): functions = [] for child in ast["children"]: if child["kind"] == "CursorKind.FUNCTION_DECL": functions.append(self._simple_ast_functions(child)) return functions def _simple_ast_functions(self, func_ast): simple_func = {"name": "", "return_type": "", "arguments": []} simple_func["name"] = func_ast["name"] simple_func["return_type"] = func_ast["result_type"] arguments = [] # check if func has args if "children" in func_ast: for arg_ast in func_ast["children"]: arg_simple = None if arg_ast["kind"] == "CursorKind.PARM_DECL": arg_simple = {"name": "", "type": ""} arg_simple["name"] = arg_ast["name"] arg_simple["type"] = arg_ast["type"] if arg_simple: arguments.append(arg_simple) if arguments: simple_func["arguments"] = arguments return simple_func # only typedef enums def extract_enums_from_ast(self, ast): enums = [] for typedef in ast["children"]: if typedef["kind"] == "CursorKind.TYPEDEF_DECL": typename = typedef["type"] if "children" in typedef: for enum in typedef["children"]: if enum["kind"] == "CursorKind.ENUM_DECL": enums.append(self._simple_ast_enums(enum, typename)) return enums def _simple_ast_enums(self, enum_ast, typename): simple_enum = {"name": typename, "items": []} items = [] if "children" in enum_ast: for item in enum_ast["children"]: if item["kind"] == "CursorKind.ENUM_CONSTANT_DECL": simple_item = {"name": "", "value": ""} simple_item["name"] = item["name"] simple_item["value"] = item["value"] items.append(simple_item) if items: simple_enum["items"] = items return simple_enum # only typedef structs def extract_structs_from_ast(self, ast): structs = [] for typedef in ast["children"]: if typedef["kind"] == "CursorKind.TYPEDEF_DECL": typename = typedef["type"] if "children" in typedef: for struct in typedef["children"]: if struct["kind"] == "CursorKind.STRUCT_DECL": structs.append(self._simple_ast_structs(struct, typename)) return structs def _simple_ast_structs(self, struct_ast, typename): simple_struct = {"name": typename, "fields": []} fields = [] if "children" in struct_ast: for field in struct_ast["children"]: if field["kind"] == "CursorKind.FIELD_DECL": simple_field = {"name": "", "type": ""} simple_field["name"] = field["name"] simple_field["type"] = field["type"] fields.append(simple_field) if fields: simple_struct["fields"] = fields return simple_struct def extract_typedefs_from_ast(self, ast): typdefs = [] for typedef in ast["children"]: if typedef["kind"] == "CursorKind.TYPEDEF_DECL": typename = typedef["type"] if "children" in typedef: children = typedef["children"] if children: ref = children[0] if ref["kind"] == "CursorKind.TYPE_REF": typdefs.append(self._simple_ast_typedefs(ref, typename)) return typdefs def _simple_ast_typedefs(self, typedef_ast, typename): simple_typedef = {"name": typename, "kind" : "", "value": ""} simple_typedef["kind"] = typedef_ast["kind"] simple_typedef["value"] = typedef_ast["name"] return simple_typedef