Browse Source

enums implemented

master
heck 5 years ago
parent
commit
7bdba88b7b
  1. 70
      gen/extract.py

70
gen/extract.py

@ -31,8 +31,8 @@ def read_file(path):
def write_json(header, key): def write_json(header, key):
header["outpath"] += "." + key + ".json" outpath = header["outpath"] + "." + key + ".json"
with open(header.get("outpath"), "w+") as f: with open(outpath, "w+") as f:
json.dump(header[key], f, indent=4) json.dump(header[key], f, indent=4)
@ -43,41 +43,41 @@ def prepare_header(header, out_dir):
return header return header
def create_simple_AST(ast): def create_simple_ast(ast):
elems = {"functions": "", elems = {"functions": "",
"structs": "", "structs": "",
"enums": ""} "enums": ""}
elems["functions"] = extract_functions_from_ast(ast) elems["functions"] = extract_functions_from_ast(ast)
elems["enums"] = extract_enums_from_ast(ast)
return elems return elems
# extracts top level functions only (is there anything else in C?) # extracts top level functions only (is there anything else in C?)
def extract_functions_from_ast(ast): def extract_functions_from_ast(ast):
functions = [] functions = []
children = ast["children"] for child in ast["children"]:
for child in children:
if child["kind"] == "CursorKind.FUNCTION_DECL": if child["kind"] == "CursorKind.FUNCTION_DECL":
functions.append(simple_AST_functions(child)) functions.append(simple_ast_functions(child))
return functions return functions
def simple_AST_functions(func_AST): def simple_ast_functions(func_ast):
simple_func = {"name": "", simple_func = {"name": "",
"return_type": "", "return_type": "",
"arguments": []} "arguments": []}
simple_func["name"] = func_AST["name"] simple_func["name"] = func_ast["name"]
simple_func["return_type"] = func_AST["result_type"] simple_func["return_type"] = func_ast["result_type"]
arguments = [] arguments = []
# check if func has args # check if func has args
if "children" in func_AST: if "children" in func_ast:
for arg_AST in func_AST["children"]: for arg_ast in func_ast["children"]:
arg_simple = None arg_simple = None
if arg_AST["kind"] == "CursorKind.PARM_DECL": if arg_ast["kind"] == "CursorKind.PARM_DECL":
arg_simple = {"name": "", arg_simple = {"name": "",
"type": ""} "type": ""}
arg_simple["name"] = arg_AST["name"] arg_simple["name"] = arg_ast["name"]
arg_simple["type"] = arg_AST["type"] arg_simple["type"] = arg_ast["type"]
if arg_simple: if arg_simple:
arguments.append(arg_simple) arguments.append(arg_simple)
@ -86,6 +86,40 @@ def simple_AST_functions(func_AST):
return simple_func return simple_func
# only typedef enums
def extract_enums_from_ast(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(simple_ast_enums(enum, typename))
return enums
def simple_ast_enums(enum_ast, typename):
simple_enum = {"name": typename,
"items": []}
items = []
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
def main(): def main():
input() input()
parser = AST_Parser("/opt/local/libexec/llvm-9.0/lib/libclang.dylib") parser = AST_Parser("/opt/local/libexec/llvm-9.0/lib/libclang.dylib")
@ -105,11 +139,11 @@ def main():
for header in headers: for header in headers:
header = prepare_header(header, out_dir) header = prepare_header(header, out_dir)
print("processing path: " + header.get("path") + "...") print("processing path: " + header.get("path") + "...")
header["AST"] = parser.parse(header["path"], header["sourcecode"]) header["ast"] = parser.parse(header["path"], header["sourcecode"])
write_json(header, "AST") write_json(header, "ast")
header["simple_AST"] = create_simple_AST(header["AST"]) header["simple_ast"] = create_simple_ast(header["ast"])
write_json(header, "simple_AST") write_json(header, "simple_ast")
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save