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 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 # Exctract class name 44 start_of_name = data.find("class ", start) + len("class ") 45 end_of_name = find_first_of_characters(" :{", data, start_of_name) 46 class_name = data[start_of_name:end_of_name].strip(" \n") 47 48 from cpp_parser import CppParser # pylint: disable=C0415 49 50 if parent_class_name != "": 51 parsed_class = CppParser(class_body, namespace, parent_class_name + "::" + class_name).parse() 52 else: 53 parsed_class = CppParser(class_body, namespace, class_name).parse() 54 55 # Extract inheritance 56 if colon_pos != len(data): 57 extends_class = data[colon_pos + 1 : start_of_body].strip(" \n") 58 parsed_class["extends"] = extends_class 59 60 # Fill 61 parsed_class["name"] = class_name 62 63 return end_of_body, parsed_class 64 65 66def parse_template_prefix(data: str, start: int) -> Tuple[int, str]: 67 start = find_first_not_restricted_character(" ", data, start) 68 if data[start : start + len("template")] != "template": 69 raise RuntimeError("Not a template!") 70 71 start_of_template_args, end_of_template_args = find_scope_borders(data, start, "<") 72 res = data[start_of_template_args + 1 : end_of_template_args] 73 74 return end_of_template_args, res 75