13af6ab5fSopenharmony_ci#!/usr/bin/env python3
23af6ab5fSopenharmony_ci# coding=utf-8
33af6ab5fSopenharmony_ci#
43af6ab5fSopenharmony_ci# Copyright (c) 2024 Huawei Device Co., Ltd.
53af6ab5fSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
63af6ab5fSopenharmony_ci# you may not use this file except in compliance with the License.
73af6ab5fSopenharmony_ci# You may obtain a copy of the License at
83af6ab5fSopenharmony_ci#
93af6ab5fSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
103af6ab5fSopenharmony_ci#
113af6ab5fSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
123af6ab5fSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
133af6ab5fSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
143af6ab5fSopenharmony_ci# See the License for the specific language governing permissions and
153af6ab5fSopenharmony_ci# limitations under the License.
163af6ab5fSopenharmony_ci
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_cifrom typing import Tuple, Dict
193af6ab5fSopenharmony_cifrom text_tools import (
203af6ab5fSopenharmony_ci    smart_find_first_of_characters,
213af6ab5fSopenharmony_ci    find_scope_borders,
223af6ab5fSopenharmony_ci    find_first_of_characters,
233af6ab5fSopenharmony_ci    find_first_not_restricted_character,
243af6ab5fSopenharmony_ci)
253af6ab5fSopenharmony_ci
263af6ab5fSopenharmony_ci
273af6ab5fSopenharmony_cidef parse_friend_class(data: str, start: int) -> Tuple[int, str]:
283af6ab5fSopenharmony_ci    name_start = data.find("friend class ", start) + len("friend class ")
293af6ab5fSopenharmony_ci    name_end = data.find(";", name_start)
303af6ab5fSopenharmony_ci    friend_name = data[name_start:name_end].strip(" \n")
313af6ab5fSopenharmony_ci    return name_end, friend_name
323af6ab5fSopenharmony_ci
333af6ab5fSopenharmony_ci
343af6ab5fSopenharmony_cidef parse_class(data: str, start: int = 0, namespace: str = "", parent_class_name: str = "") -> Tuple[int, Dict]:
353af6ab5fSopenharmony_ci    # Extract class body
363af6ab5fSopenharmony_ci    start_of_body = smart_find_first_of_characters("{", data, start)
373af6ab5fSopenharmony_ci    start_of_body, end_of_body = find_scope_borders(data, start_of_body)
383af6ab5fSopenharmony_ci    class_body = data[start_of_body + 1 : end_of_body]
393af6ab5fSopenharmony_ci
403af6ab5fSopenharmony_ci    # Inheritance
413af6ab5fSopenharmony_ci    colon_pos = find_first_of_characters(":", data, start, start_of_body)
423af6ab5fSopenharmony_ci
433af6ab5fSopenharmony_ci    # Exctract class name
443af6ab5fSopenharmony_ci    start_of_name = data.find("class ", start) + len("class ")
453af6ab5fSopenharmony_ci    end_of_name = find_first_of_characters(" :{", data, start_of_name)
463af6ab5fSopenharmony_ci    class_name = data[start_of_name:end_of_name].strip(" \n")
473af6ab5fSopenharmony_ci
483af6ab5fSopenharmony_ci    from cpp_parser import CppParser  # pylint: disable=C0415
493af6ab5fSopenharmony_ci
503af6ab5fSopenharmony_ci    if parent_class_name != "":
513af6ab5fSopenharmony_ci        parsed_class = CppParser(class_body, namespace, parent_class_name + "::" + class_name).parse()
523af6ab5fSopenharmony_ci    else:
533af6ab5fSopenharmony_ci        parsed_class = CppParser(class_body, namespace, class_name).parse()
543af6ab5fSopenharmony_ci
553af6ab5fSopenharmony_ci    # Extract inheritance
563af6ab5fSopenharmony_ci    if colon_pos != len(data):
573af6ab5fSopenharmony_ci        extends_class = data[colon_pos + 1 : start_of_body].strip(" \n")
583af6ab5fSopenharmony_ci        parsed_class["extends"] = extends_class
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_ci    # Fill
613af6ab5fSopenharmony_ci    parsed_class["name"] = class_name
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_ci    return end_of_body, parsed_class
643af6ab5fSopenharmony_ci
653af6ab5fSopenharmony_ci
663af6ab5fSopenharmony_cidef parse_template_prefix(data: str, start: int) -> Tuple[int, str]:
673af6ab5fSopenharmony_ci    start = find_first_not_restricted_character(" ", data, start)
683af6ab5fSopenharmony_ci    if data[start : start + len("template")] != "template":
693af6ab5fSopenharmony_ci        raise RuntimeError("Not a template!")
703af6ab5fSopenharmony_ci
713af6ab5fSopenharmony_ci    start_of_template_args, end_of_template_args = find_scope_borders(data, start, "<")
723af6ab5fSopenharmony_ci    res = data[start_of_template_args + 1 : end_of_template_args]
733af6ab5fSopenharmony_ci
743af6ab5fSopenharmony_ci    return end_of_template_args, res
75