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 console_log, warning_log
20from text_tools import (
21    find_first_of_characters,
22    find_first_not_restricted_character,
23    smart_find_first_of_characters,
24    find_scope_borders,
25)
26
27
28def parse_enum_union(data: str) -> list:
29    equally_pos = data.find("=")
30    if equally_pos == -1:
31        raise RuntimeError("Can't find '='.")
32
33    if data.find("~") != -1 or data.find("&") != -1:
34        union = [] # NOTE(morlovsky): instead of using [data[equally_pos + 1:].strip(" \n")]
35    else:
36        union = [x for x in data[equally_pos + 1 :].split(" ") if x.strip(" \n") != "" and x.strip(" \n") != "|"]
37    return union
38
39
40def is_union_value(data: str) -> bool:
41    if data.find("=") == -1 or data.find("<<") != -1:
42        return False
43    if data.find("|") != -1:
44        return True
45
46    value = data[data.find("=") + 1 :].strip(" ")
47    if value[0].isdigit() or value[0] == "(" and value[1].isdigit():
48        return False
49
50    if find_first_of_characters("1234567890-+", value, 0) != len(value):
51        return False
52
53    return True
54
55
56def parse_enum_class_body(data: str) -> dict:
57    console_log("Parsing enum body...")
58
59    res: Dict[str, Any] = {}
60
61    value_start = 0
62    value_end = data.find(",", value_start)
63
64    if data.find("#define") != -1:
65        warning_log("Defines in enum not realized yet. Can't parse enum body with define:\n---\n" + data + "\n---\n")
66        return {}
67
68    if value_end == -1:
69        value_end = len(data)
70
71    while value_start != -1 and value_start < len(data):
72        value = data[value_start:value_end].strip(" \n")
73
74        if value != "":
75            if not is_union_value(value):
76                if "flags" not in res:
77                    res["flags"] = []
78                res["flags"].append(get_name_of_enum_value(value))
79            else:
80
81                union_flag: Dict[str, Any] = {}
82                union_flag["name"] = get_name_of_enum_value(value)
83                union_flag["flags"] = parse_enum_union(value)
84
85                if union_flag["flags"] != []:
86                    if "flag_unions" not in res:
87                        res["flag_unions"] = []
88
89                    res["flag_unions"].append(union_flag)
90
91        if value_end == len(data):
92            break
93
94        value_start = value_end + 1
95        value_end = data.find(",", value_start)
96
97        if value_end == -1:
98            value_end = len(data)
99
100    return res
101
102
103def parse_enum_class(data: str, start: int = 0) -> Tuple[int, Dict]:
104    res = {}
105
106    start_of_name = data.find("enum class", start)
107    start_of_name = find_first_not_restricted_character(" ", data, start_of_name + len("enum class"))
108    end_of_name = find_first_of_characters(" ;{\n", data, start_of_name)
109    enum_name = data[start_of_name:end_of_name]
110
111    if enum_name == "":
112        raise RuntimeError("Error! No name in enum\n")
113
114    res["name"] = enum_name
115    start_of_body = smart_find_first_of_characters("{", data, end_of_name)
116
117    if start_of_body == -1:
118        raise RuntimeError("Error! Empty body in enum class.")
119
120    start_of_body, end_of_body = find_scope_borders(data, start_of_body)
121
122    if data.find("<<", start_of_body, end_of_body) != -1:
123        res["kind"] = "flags"
124    else:
125        res["kind"] = "simple"
126
127    parsed_flags = parse_enum_class_body(data[start_of_body + 1 : end_of_body])
128
129    if "flags" in parsed_flags:
130        res["flags"] = parsed_flags["flags"]
131
132    if "flag_unions" in parsed_flags:
133        res["flag_unions"] = parsed_flags["flag_unions"]
134
135    console_log("parsed enum: '" + enum_name + "'")
136
137    end_of_body = data.find(";", end_of_body)
138
139    return end_of_body, res
140
141
142def get_name_of_enum_value(data: str) -> str:
143    equally_pos = data.find("=")
144
145    if equally_pos == -1:
146        return data
147
148    return data[:equally_pos].strip(" ")
149