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

Loading…
Cancel
Save