forked from duckdb/duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_enums.py
More file actions
161 lines (127 loc) · 5.13 KB
/
Copy pathgenerate_enums.py
File metadata and controls
161 lines (127 loc) · 5.13 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
import os
import json
import re
targets = [{'source': 'extension/json/include/', 'target': 'extension/json'}]
file_list = []
for target in targets:
source_base = os.path.sep.join(target['source'].split('/'))
target_base = os.path.sep.join(target['target'].split('/'))
for fname in os.listdir(source_base):
if '_enums.json' not in fname:
continue
file_list.append(
{
'source': os.path.join(source_base, fname),
'include_path': fname.replace('.json', '.hpp'),
'target_hpp': os.path.join(source_base, fname.replace('.json', '.hpp')),
'target_cpp': os.path.join(target_base, fname.replace('.json', '.cpp')),
}
)
header = '''//===----------------------------------------------------------------------===//
// This file is automatically generated by scripts/generate_enums.py
// Do not edit this file manually, your changes will be overwritten
//===----------------------------------------------------------------------===//
${INCLUDE_LIST}
namespace duckdb {
'''
footer = '''
} // namespace duckdb
'''
include_base = '#include "${FILENAME}"\n'
enum_header = '\nenum class ${ENUM_NAME} : ${ENUM_TYPE} {\n'
enum_footer = '};'
enum_value = '\t${ENUM_MEMBER} = ${ENUM_VALUE},\n'
enum_util_header = '''
template<>
const char* EnumUtil::ToChars<${ENUM_NAME}>(${ENUM_NAME} value);
template<>
${ENUM_NAME} EnumUtil::FromString<${ENUM_NAME}>(const char *value);
'''
enum_util_conversion_begin = '''
template<>
const char* EnumUtil::ToChars<${ENUM_NAME}>(${ENUM_NAME} value) {
switch(value) {
'''
enum_util_switch = '\tcase ${ENUM_NAME}::${ENUM_MEMBER}:\n\t\treturn "${ENUM_MEMBER}";\n'
enum_util_conversion_end = ''' default:
throw NotImplementedException(StringUtil::Format("Enum value of type ${ENUM_NAME}: '%d' not implemented", value));
}
}
'''
from_string_begin = '''
template<>
${ENUM_NAME} EnumUtil::FromString<${ENUM_NAME}>(const char *value) {
'''
from_string_comparison = ''' if (StringUtil::Equals(value, "${ENUM_MEMBER}")) {
return ${ENUM_NAME}::${ENUM_MEMBER};
}
'''
from_string_end = ''' throw NotImplementedException(StringUtil::Format("Enum value of type ${ENUM_NAME}: '%s' not implemented", value));
}
'''
class EnumMember:
def __init__(self, entry, index):
self.comment = None
self.index = index
if type(entry) == str:
self.name = entry
else:
self.name = entry['name']
if 'comment' in entry:
self.comment = entry['comment']
if 'index' in entry:
self.index = int(entry['index'])
class EnumClass:
def __init__(self, entry):
self.name = entry['name']
self.type = 'uint8_t'
self.values = []
index = 0
for value_entry in entry['values']:
self.values.append(EnumMember(value_entry, index))
index += 1
for entry in file_list:
source_path = entry['source']
target_header = entry['target_hpp']
target_source = entry['target_cpp']
include_path = entry['include_path']
with open(source_path, 'r') as f:
json_data = json.load(f)
include_list = ['duckdb/common/constants.hpp', 'duckdb/common/enum_util.hpp']
enums = []
for entry in json_data:
if 'includes' in entry:
include_list += entry['includes']
enums.append(EnumClass(entry))
with open(target_header, 'w+') as f:
include_text = '#pragma once\n\n'
include_text += ''.join([include_base.replace('${FILENAME}', x) for x in include_list])
f.write(header.replace('${INCLUDE_LIST}', include_text))
for enum in enums:
f.write(enum_header.replace('${ENUM_NAME}', enum.name).replace('${ENUM_TYPE}', enum.type))
for value in enum.values:
if value.comment is not None:
f.write('\t//! ' + value.comment + '\n')
f.write(enum_value.replace('${ENUM_MEMBER}', value.name).replace('${ENUM_VALUE}', str(value.index)))
f.write(enum_footer)
f.write('\n')
for enum in enums:
f.write(enum_util_header.replace('${ENUM_NAME}', enum.name))
f.write(footer)
with open(target_source, 'w+') as f:
source_include_list = [include_path, 'duckdb/common/string_util.hpp']
f.write(
header.replace(
'${INCLUDE_LIST}', ''.join([include_base.replace('${FILENAME}', x) for x in source_include_list])
)
)
for enum in enums:
f.write(enum_util_conversion_begin.replace('${ENUM_NAME}', enum.name))
for value in enum.values:
f.write(enum_util_switch.replace('${ENUM_MEMBER}', value.name).replace('${ENUM_NAME}', enum.name))
f.write(enum_util_conversion_end.replace('${ENUM_NAME}', enum.name))
f.write(from_string_begin.replace('${ENUM_NAME}', enum.name))
for value in enum.values:
f.write(from_string_comparison.replace('${ENUM_MEMBER}', value.name).replace('${ENUM_NAME}', enum.name))
f.write(from_string_end.replace('${ENUM_NAME}', enum.name))
f.write(footer)