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, Any
193af6ab5fSopenharmony_cifrom log_tools import warning_log
203af6ab5fSopenharmony_cifrom parse_arguments import parse_argument
213af6ab5fSopenharmony_cifrom parse_method import parse_method_or_constructor
223af6ab5fSopenharmony_cifrom text_tools import (
233af6ab5fSopenharmony_ci    smart_split_by,
243af6ab5fSopenharmony_ci    find_first_not_restricted_character,
253af6ab5fSopenharmony_ci    smart_find_first_of_characters,
263af6ab5fSopenharmony_ci    find_first_of_characters,
273af6ab5fSopenharmony_ci    find_scope_borders,
283af6ab5fSopenharmony_ci)
293af6ab5fSopenharmony_ci
303af6ab5fSopenharmony_ci
313af6ab5fSopenharmony_cidef parse_struct_body(data: str) -> list:
323af6ab5fSopenharmony_ci    res = []
333af6ab5fSopenharmony_ci    for x in smart_split_by(data, ";"):
343af6ab5fSopenharmony_ci        if x.find("(") != -1:
353af6ab5fSopenharmony_ci            res.append(parse_method_or_constructor(x, 0)[1])
363af6ab5fSopenharmony_ci        else:
373af6ab5fSopenharmony_ci            res.append(parse_argument(x))
383af6ab5fSopenharmony_ci    return [x for x in res if x]
393af6ab5fSopenharmony_ci
403af6ab5fSopenharmony_ci
413af6ab5fSopenharmony_cidef parse_struct(data: str, start: int = 0) -> Tuple[int, Dict]:
423af6ab5fSopenharmony_ci    res: Dict[str, Any] = {}
433af6ab5fSopenharmony_ci    check = data.find("struct ", start)
443af6ab5fSopenharmony_ci
453af6ab5fSopenharmony_ci    if check == -1:
463af6ab5fSopenharmony_ci        raise RuntimeError("Error! No need to parse struct.")
473af6ab5fSopenharmony_ci
483af6ab5fSopenharmony_ci    start_of_name = find_first_not_restricted_character(" ", data, check + len("struct "))
493af6ab5fSopenharmony_ci    end_of_name = find_first_of_characters(" ;{\n", data, start_of_name)
503af6ab5fSopenharmony_ci    struct_name = data[start_of_name:end_of_name]
513af6ab5fSopenharmony_ci
523af6ab5fSopenharmony_ci    if struct_name == "":
533af6ab5fSopenharmony_ci        raise RuntimeError("Error! No name in struct\n")
543af6ab5fSopenharmony_ci
553af6ab5fSopenharmony_ci    res["name"] = struct_name
563af6ab5fSopenharmony_ci    start_of_body = smart_find_first_of_characters("{", data, end_of_name)
573af6ab5fSopenharmony_ci    next_semicolon = smart_find_first_of_characters(";", data, end_of_name)
583af6ab5fSopenharmony_ci
593af6ab5fSopenharmony_ci    if start_of_body == len(data) or next_semicolon < start_of_body:
603af6ab5fSopenharmony_ci        warning_log("Empty body in struct '" + struct_name + "'")
613af6ab5fSopenharmony_ci        return end_of_name, res
623af6ab5fSopenharmony_ci
633af6ab5fSopenharmony_ci    start_of_body, end_of_body = find_scope_borders(data, start_of_body)
643af6ab5fSopenharmony_ci
653af6ab5fSopenharmony_ci    struct_members = parse_struct_body(data[start_of_body + 1 : end_of_body])
663af6ab5fSopenharmony_ci    res["members"] = struct_members
673af6ab5fSopenharmony_ci
683af6ab5fSopenharmony_ci    return end_of_body, res
69