• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 debug_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    res: Dict[str, Any] = {}
58
59    value_start = 0
60    value_end = data.find(",", value_start)
61
62    if data.find("#define") != -1:
63        debug_log(f"Defines in enum not realized yet. Can't parse enum body with define:\n---\n{data}\n---\n")
64        return {}
65
66    if value_end == -1:
67        value_end = len(data)
68
69    while value_start != -1 and value_start < len(data):
70        value = data[value_start:value_end].strip(" \n")
71
72        if value != "" and (is_union_value(value) == False):
73            if "flags" not in res:
74                res["flags"] = []
75            res["flags"].append(get_name_of_enum_value(value))
76
77        elif value != "" and (is_union_value(value) == True):
78
79            union_flag: Dict[str, Any] = {}
80            union_flag["name"] = get_name_of_enum_value(value)
81            union_flag["flags"] = parse_enum_union(value)
82
83            if (union_flag["flags"] != []) and ("flag_unions" not in res):
84                res["flag_unions"] = []
85                res["flag_unions"].append(union_flag)
86            elif (union_flag["flags"] != []) and ("flag_unions" not in res) == False:
87                res["flag_unions"].append(union_flag)
88
89        if value_end == len(data):
90            break
91
92        value_start = value_end + 1
93        value_end = data.find(",", value_start)
94
95        if value_end == -1:
96            value_end = len(data)
97
98    return res
99
100
101def parse_enum_class(data: str, start: int = 0) -> Tuple[int, Dict]:
102    res = {}
103
104    start_of_name = data.find("enum ", start)
105
106    if data.find("enum class", start_of_name) == start_of_name:
107        start_of_name = find_first_not_restricted_character(" ", data, start_of_name + len("enum class"))
108    else:
109        start_of_name = find_first_not_restricted_character(" ", data, start_of_name + len("enum"))
110
111    end_of_name = find_first_of_characters(" ;{\n", data, start_of_name)
112    enum_name = data[start_of_name:end_of_name]
113
114    if enum_name == "":
115        raise RuntimeError("Error! No name in enum\n")
116
117    res["name"] = enum_name
118    start_of_body = smart_find_first_of_characters("{", data, end_of_name)
119
120    if start_of_body == len(data):
121        return find_first_of_characters(";", data, end_of_name), res
122
123    start_of_body, end_of_body = find_scope_borders(data, start_of_body)
124
125    if data.find("<<", start_of_body, end_of_body) != -1:
126        res["kind"] = "flags"
127    else:
128        res["kind"] = "simple"
129
130    parsed_flags = parse_enum_class_body(data[start_of_body + 1 : end_of_body])
131
132    if "flags" in parsed_flags:
133        res["flags"] = parsed_flags["flags"]
134
135    if "flag_unions" in parsed_flags:
136        res["flag_unions"] = parsed_flags["flag_unions"]
137
138    end_of_body = data.find(";", end_of_body)
139
140    return end_of_body, res
141
142
143def get_name_of_enum_value(data: str) -> str:
144    equally_pos = data.find("=")
145
146    if equally_pos == -1:
147        return data
148
149    return data[:equally_pos].strip(" ")
150