1#!/usr/bin/env python3 2# coding=utf-8 3# 4# Copyright (c) 2024 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17 18from typing import Tuple, Dict, Any 19from log_tools import debug_log 20from parse_arguments import parse_argument 21from parse_define import is_known_macros 22from parse_method import parse_method_or_constructor 23from text_tools import ( 24 smart_split_by, 25 find_first_not_restricted_character, 26 smart_find_first_of_characters, 27 find_first_of_characters, 28 find_scope_borders, 29) 30 31 32def parse_struct_body(data: str) -> list: 33 res = [] 34 for x in smart_split_by(data, ";"): 35 if x.find("(") != -1: 36 if not is_known_macros(x): 37 res.append(parse_method_or_constructor(x, 0)[1]) 38 else: 39 res.append(parse_argument(x)) 40 return [x for x in res if x] 41 42 43def parse_struct(data: str, start: int = 0) -> Tuple[int, Dict]: 44 res: Dict[str, Any] = {} 45 check = data.find("struct ", start) 46 47 if check == -1: 48 raise RuntimeError("Error! No need to parse struct.") 49 50 start_of_name = find_first_not_restricted_character(" ", data, check + len("struct ")) 51 end_of_name = find_first_of_characters(" ;{\n", data, start_of_name) 52 struct_name = data[start_of_name:end_of_name] 53 54 if struct_name == "": 55 raise RuntimeError("Error! No name in struct\n") 56 57 res["name"] = struct_name 58 start_of_body = smart_find_first_of_characters("{", data, end_of_name) 59 next_semicolon = smart_find_first_of_characters(";", data, end_of_name) 60 61 if start_of_body == len(data) or next_semicolon < start_of_body: 62 debug_log(f"Empty body in struct '{struct_name}'") 63 return end_of_name, res 64 65 start_of_body, end_of_body = find_scope_borders(data, start_of_body) 66 67 struct_members = parse_struct_body(data[start_of_body + 1 : end_of_body]) 68 res["members"] = struct_members 69 70 return end_of_body, res 71