From 1836376a35610d01e90a963f9e311effc4b73d68 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 10 Dec 2020 02:13:45 +0100 Subject: [PATCH] move class "AST_Parser" into own module. --- gen/__init__.py | 0 gen/ast_parser.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ gen/extract.py | 56 +--------------------------------------------- 3 files changed, 58 insertions(+), 55 deletions(-) create mode 100644 gen/__init__.py create mode 100644 gen/ast_parser.py diff --git a/gen/__init__.py b/gen/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gen/ast_parser.py b/gen/ast_parser.py new file mode 100644 index 0000000..6e60316 --- /dev/null +++ b/gen/ast_parser.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +import clang.cindex +from clang.cindex import CursorKind + + +class AST_Parser: + def __init__(self,library_file=None): + if not clang.cindex.Config.loaded: + print("Using libclang from: %s", library_file) + clang.cindex.Config.set_library_file(library_file) + + def parse(self, filename, content): + index = clang.cindex.Index.create() + arguments = ["-x", "c"] + options = clang.cindex.TranslationUnit.PARSE_SKIP_FUNCTION_BODIES + content = [(filename, content)] + translation_unit = index.parse(filename, unsaved_files=content, args=arguments, options=options) + ret = self._parse(translation_unit.cursor, filename) + return ret + + def _get_children_filelocal(self, cursor, path): + return [c for c in cursor.get_children() if c.location.file and c.location.file.name == path] + + def _parse(self, cursor, path): + item = {} + excluded_cursortypes = [CursorKind.INTEGER_LITERAL] + if not cursor.kind in excluded_cursortypes: + if not str(cursor.kind) == "": + item["kind"] = str(cursor.kind) + + if not cursor.spelling == "": + item["name"] = cursor.spelling + + if not cursor.displayname == "": + item["displayname"] = cursor.displayname + + if not cursor.type.spelling == "": + item["type"] = cursor.type.spelling + + if not cursor.result_type.spelling == "": + item["result_type"] = cursor.result_type.spelling + + if cursor.kind == CursorKind.ENUM_CONSTANT_DECL: + item["value"] = cursor.enum_value + + child_cursors = self._get_children_filelocal(cursor, path) + if len(child_cursors) > 0: + child_arr = [] + for child_cursor in child_cursors: + child_result = self._parse(child_cursor, path) + if child_result: + child_arr.append(child_result) + + if child_arr: + item["children"] = child_arr + + return item diff --git a/gen/extract.py b/gen/extract.py index eccda2d..b141994 100755 --- a/gen/extract.py +++ b/gen/extract.py @@ -2,63 +2,9 @@ # -*- coding: utf-8 -*- import os import json -import clang.cindex -from clang.cindex import CursorKind -class AST_Parser: - def __init__(self,library_file=None): - if not clang.cindex.Config.loaded: - print("Using libclang from: %s", library_file) - clang.cindex.Config.set_library_file(library_file) - - def parse(self, filename, content): - index = clang.cindex.Index.create() - arguments = ["-x", "c"] - options = clang.cindex.TranslationUnit.PARSE_SKIP_FUNCTION_BODIES - content = [(filename, content)] - translation_unit = index.parse(filename, unsaved_files=content, args=arguments, options=options) - ret = self._parse(translation_unit.cursor, filename) - return ret - - def _get_children_filelocal(self, cursor, path): - return [c for c in cursor.get_children() if c.location.file and c.location.file.name == path] - - def _parse(self, cursor, path): - item = {} - excluded_cursortypes = [CursorKind.INTEGER_LITERAL] - if not cursor.kind in excluded_cursortypes: - if not str(cursor.kind) == "": - item["kind"] = str(cursor.kind) - - if not cursor.spelling == "": - item["name"] = cursor.spelling - - if not cursor.displayname == "": - item["displayname"] = cursor.displayname - - if not cursor.type.spelling == "": - item["type"] = cursor.type.spelling - - if not cursor.result_type.spelling == "": - item["result_type"] = cursor.result_type.spelling - - if cursor.kind == CursorKind.ENUM_CONSTANT_DECL: - item["value"] = cursor.enum_value - - child_cursors = self._get_children_filelocal(cursor, path) - if len(child_cursors) > 0: - child_arr = [] - for child_cursor in child_cursors: - child_result = self._parse(child_cursor, path) - if child_result: - child_arr.append(child_result) - - if child_arr: - item["children"] = child_arr - - return item - +from ast_parser import AST_Parser def create_paths_list(dirname, filenames): paths = []