1#!/usr/bin/env python3 2# coding=utf-8 3# 4# Copyright (c) 2024-2025 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 19from text_tools import ( 20 smart_find_first_of_characters, 21 find_scope_borders, 22 find_first_of_characters, 23 find_first_not_restricted_character, 24) 25 26 27def parse_friend_class(data: str, start: int) -> Tuple[int, str]: 28 name_start = data.find("friend class ", start) + len("friend class ") 29 name_end = data.find(";", name_start) 30 friend_name = data[name_start:name_end].strip(" \n") 31 return name_end, friend_name 32 33 34def parse_class(data: str, start: int = 0, namespace: str = "", parent_class_name: str = "") -> Tuple[int, Dict]: 35 # Extract class body 36 start_of_body = smart_find_first_of_characters("{", data, start) 37 start_of_body, end_of_body = find_scope_borders(data, start_of_body) 38 class_body = data[start_of_body + 1 : end_of_body] 39 40 # Inheritance 41 colon_pos = find_first_of_characters(":", data, start, start_of_body) 42 43 class_name = extract_class_name(data, data.find("class ", start) + len("class ")) 44 45 from cpp_parser import CppParser # pylint: disable=C0415 46 47 if parent_class_name != "": 48 parsed_class = CppParser(class_body, namespace, parent_class_name + "::" + class_name).parse() 49 else: 50 parsed_class = CppParser(class_body, namespace, class_name).parse() 51 52 # Extract inheritance 53 if colon_pos != len(data): 54 extends_class = data[colon_pos + 1 : start_of_body].strip(" \n") 55 parsed_class["extends"] = extends_class 56 57 # Fill 58 parsed_class["name"] = class_name 59 60 return end_of_body, parsed_class 61 62 63def extract_class_name(data: str, pos: int) -> str: 64 name = "" 65 while not name or name.isupper(): 66 start_of_name = find_first_not_restricted_character(" :{\n", data, pos) 67 pos = find_first_of_characters(" :{\n", data, start_of_name) 68 69 if start_of_name == len(data) or pos == len(data): 70 raise RuntimeError("Error while extracting class name") 71 72 name = data[start_of_name:pos] 73 74 return data[start_of_name:pos] 75 76 77def parse_template_prefix(data: str, start: int) -> Tuple[int, str]: 78 start = find_first_not_restricted_character(" ", data, start) 79 if data[start : start + len("template")] != "template": 80 raise RuntimeError("Not a template!") 81 82 start_of_template_args, end_of_template_args = find_scope_borders(data, start, "<") 83 res = data[start_of_template_args + 1 : end_of_template_args] 84 85 return end_of_template_args, res 86