-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_enum.py
More file actions
220 lines (160 loc) · 7.85 KB
/
auto_enum.py
File metadata and controls
220 lines (160 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from generator import Code
import generator
import sys
from typing import Optional, Any
TYPE_ENUM = "enum"
DETAILS_NAMESPACE: str = "details_namespace"
NAMESPACE: str = "namespace"
VALUES: str = "values"
DOCUMENTATION = "documentation"
class Enum:
def __init__(self, name: str, values: list[str], namespace: Optional[str] = None,
details_namespace: Optional[str] = None, documentation: Optional[str] = None):
self.name = name
self.namespace = namespace
self.details_namespace = details_namespace
self.values = values
self.documentation = documentation
def set_name(self, name: str):
if name is None or name.isspace():
raise ValueError("Enum provided with no name. Please check that all enums have an appropriate name.")
self.name = name
def set_values(self, values: list[str]):
if values is None or len(values) == 0:
raise AttributeError("No values provided for enum " + self.name)
self.values = values
def main2(input_file_path: str, directory: str):
try:
file_contents = generator.import_yaml_file(input_file_path)
enums: list[Enum] = []
for enum_contents in file_contents.items():
if generator.group_type(enum_contents) == TYPE_ENUM:
# In case of error, keep trying to parse the rest like a normal compiler
# The generator will refuse to produce files after we flag any error
# try:
# enums.append(parse_enum(enum_contents[0], enum_contents[1]))
# except Exception as exc:
# generator.error(exc)
enums.append(parse_enum(enum_contents[0], enum_contents[1]))
for enum in enums:
print("Producing files for enum " + enum.name + "...")
generator.write_to_file(generate_header_str(enum), enum.name + ".h", directory)
generator.write_to_file(generate_source_str(enum), enum.name + ".cpp", directory)
# catch fatal errors
except Exception as exc:
generator.error(exc)
def main(input_file_path: str, directory: str):
file_contents = generator.import_yaml_file(input_file_path)
enums: list[Enum] = []
for enum_contents in file_contents.items():
if generator.group_type(enum_contents) == TYPE_ENUM:
# In case of error, keep trying to parse the rest like a normal compiler
# The generator will refuse to produce files after we flag any error
# try:
# enums.append(parse_enum(enum_contents[0], enum_contents[1]))
# except Exception as exc:
# generator.error(exc)
enums.append(parse_enum(enum_contents[0], enum_contents[1]))
for enum in enums:
print("Producing files for enum " + enum.name + "...")
generator.write_to_file(generate_header_str(enum), enum.name + ".h", directory)
generator.write_to_file(generate_source_str(enum), enum.name + ".cpp", directory)
def parse_enum(enum_name: str, enum_descr: Any) -> Optional[Enum]:
def try_get_value(key: str) -> Optional[Any]:
try:
return e_dict[key]
except KeyError:
return None
name: str = enum_name
e_dict: dict[str] = enum_descr
if name is None or name.isspace():
raise ValueError("Enum provided with no name. Please check that all enums have an appropriate name.")
namespace: str = try_get_value(NAMESPACE)
details_namespace: str = try_get_value(DETAILS_NAMESPACE)
values: list[str] = try_get_value(VALUES)
documentation: str = try_get_value(DOCUMENTATION)
if namespace is None:
generator.warning("No namespace provided for enum {0}, assuming global namespace.".format(name))
if details_namespace is None:
generator.warning("No namespace provided for the details of enum {0}, assuming enum definition namespace."
.format(name))
return Enum(name, values, namespace, details_namespace, documentation)
def cpp_map_def(enum: Enum) -> str:
return "const std::unordered_map<{0}, std::string> {1}".format(enum.name, cpp_map_name(enum))
def cpp_map_name(enum: Enum) -> str:
return enum.name.upper() + "_MAP"
def cpp_qualified_name(enum: Enum) -> str:
return "{0}::{1}".format(enum.namespace if enum.namespace is not None else "", enum.name)
def cpp_access_func_def(enum: Enum) -> str:
return "std::string {0}ToString({1} {0})".format(enum.name.lower(), cpp_qualified_name(enum))
def cpp_iter_func_def(enum: Enum) -> str:
return "std::vector<{0}> {1}Values()".format(enum.name, enum.name.lower())
def generate_header_str(enum: Enum) -> str:
code = Code()
in_namespace: bool = enum.namespace is not None
uses_details_namespace: bool = enum.details_namespace is not None
has_doc: bool = enum.documentation is not None
code.add_statement("#pragma once")
code.add_statement(generator.HEADER_COMMENT)
code.add_statement("#include <unordered_map>")
code.add_statement()
if in_namespace:
code.start_block("namespace " + enum.namespace)
code.start_block("{0}enum class {1}".format(enum.documentation.replace("\n", "\n\t") if has_doc else "", enum.name))
for value in enum.values:
code.add_statement(value + ",")
code.end_block(True)
code.add_statement()
code.add_statement()
code.add_statement(cpp_access_func_def(enum) + ";")
code.add_statement()
code.add_statement()
code.add_statement(cpp_iter_func_def(enum) + ";")
code.add_statement()
if uses_details_namespace:
code.start_block("namespace " + enum.details_namespace)
code.add_statement("extern {0};".format(cpp_map_def(enum)))
return code.to_string()
def generate_source_str(enum: Enum) -> str:
# since the functions are declared outside the 'details' namespace, we may need to add the namespace prefix
def cpp_qualified_map_name():
map_namespace = (enum.details_namespace + "::") if enum.details_namespace is not None else ""
return map_namespace + cpp_map_name(enum)
code = Code()
in_namespace: bool = enum.namespace is not None
uses_details_namespace: bool = enum.details_namespace is not None
code.add_statement("#include \"{0}.h\"".format(enum.name))
code.add_statement("#include <string>")
code.add_statement("#include <utility>")
code.add_statement("#include <algorithm>")
code.add_statement()
if in_namespace:
code.start_block("namespace " + enum.namespace)
code.add_statement()
code.start_block(cpp_access_func_def(enum))
code.add_statement("return {0}.find({1})->second;".format(cpp_qualified_map_name(), enum.name.lower()))
code.end_block()
code.add_statement()
code.start_block(cpp_iter_func_def(enum))
code.add_statement("std::vector<{0}> v;".format(cpp_qualified_name(enum)))
code.start_block("for each (auto it in {0})".format(cpp_qualified_map_name()))
code.add_statement("v.push_back(it.first);")
code.end_block()
code.add_statement("return v;")
code.end_block()
code.add_statement()
if uses_details_namespace:
code.start_block("namespace " + enum.details_namespace)
code.add_statement(cpp_map_def(enum) + " = ")
code.start_block("")
for value in enum.values:
code.add_statement("std::make_pair({0}::{1}, \"{2}\"),".format(enum.name, value, value.capitalize()))
code.end_block(True)
return code.to_string()
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Usage: python <program> <input file> <output directory>")
sys.exit(-1)
main(sys.argv[1], sys.argv[2])
# set termination flag
sys.exit(-1 if generator.error_occurred() else 0)