-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlua_reflection.py
More file actions
192 lines (155 loc) · 8.08 KB
/
lua_reflection.py
File metadata and controls
192 lines (155 loc) · 8.08 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
# AUI Framework - Declarative UI toolkit for modern C++20
# Copyright (C) 2020-2025 Alex2772 and Contributors
#
# SPDX-License-Identifier: MPL-2.0
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
import re
import sys
source = sys.argv[1]
destination = sys.argv[2]
RE_TEMPLATE = re.compile(r'^\s*(template<.+>)')
RE_CLASS_DEF = re.compile(r'^\s*(class|struct) (\w+)')
RE_METHOD_DEF = re.compile(r'^\s*(\S+) (\S+)\((.+ [a-zA-Z0-9]+)*\)( const)? override;')
RE_BLOCK_END = re.compile(r'^\s*}\s*;')
def parse_argument(argument):
RE_ARGUMENT = re.compile(r'(.*) ([a-zA-Z0-9]+)')
match = RE_ARGUMENT.match(argument)
return (match.groups()[0], match.groups()[1])
class Parser:
remaining = ""
def run(self, source, destination):
with open(source, 'r') as input:
with open(destination, 'w') as output:
self.output = output
output.write('// This file is generated with lua_reflection.py. Do not modify.\n')
self.templateDescription = None
def try_catch_wrapper(to_call):
log_tag = 'AClass<std::decay_t<decltype(*this)>>::name()'
output.write(" try {\n")
output.write(to_call)
output.write(" }\n")
output.write(" catch(const AException& e) {\n")
output.write(f' ALogger::err({log_tag}) << "Exception occurred after lua function call: " << e;\n')
output.write(' }\n')
output.write(" catch(const clg::lua_exception& e) {\n")
output.write(f' ALogger::warn({log_tag}) << "Exception occurred after lua function call: " << e.what();\n')
output.write(' }\n')
output.write(" catch(const std::exception& e) {\n")
output.write(f' ALogger::err({log_tag}) << "Exception occurred after lua function call: " << e.what();\n')
output.write(' }\n')
output.write(" catch(...) {\n")
output.write(f' ALogger::err({log_tag}) << "Exception occurred after lua function call: unknown exception";\n')
output.write(' }\n')
def visit_template(match):
self.templateDescription = match.groups()[0]
self.output.write(self.templateDescription)
self.output.write("\n")
self.process({
RE_CLASS_DEF: visit_class_def,
})
def visit_class_def(match):
className = match.groups()[1]
e = self.remaining.find("{")+1
self.output.write(f"class {className} {self.remaining[:e+1]}")
self.remaining = self.remaining[e:]
self.blockEnd = False
self.methods = []
def visit_block_end(match):
output.write("\nprivate:\n")
for method in self.methods:
output.write(f"bool m_{method}Flag = false;\n")
output.write("static const std::map<std::string_view, bool(LuaExposedView::*)>& ")
output.write(f"vtable()")
output.write(" {\n")
output.write(" static const std::map<std::string_view, bool(LuaExposedView::*)> v = {\n")
for method in self.methods:
output.write(" { ")
output.write("\"")
output.write(method)
output.write("\", ")
output.write(f"&LuaExposedView::m_{method}Flag")
output.write(" },\n")
output.write(" };\n")
output.write(" return v;\n")
output.write("}\n")
self.output.write("};\n")
self.blockEnd = True
def visit_method_def(match):
returnType = match.groups()[0]
isVoid = returnType == 'void'
name = match.groups()[1]
argsRaw = match.groups()[2]
if argsRaw is not None:
args = [i.strip() for i in match.groups()[2].split(",")]
else:
args = {}
argNames = ', '.join([parse_argument(i)[1] for i in args])
output.write('\n')
output.write(returnType)
output.write(' ')
output.write(name)
output.write('(')
output.write(", ".join(args))
output.write(') override {\n')
if name == "render":
output.write("performance::AUI_VIEW_RENDER += 1;\n")
def createSuperCall():
output.write(f'View::{name}')
output.write('(')
output.write(argNames)
output.write(');\n')
def createSuperCallWithResult():
output.write(f'auto superCallResult = View::{name}')
output.write('(')
output.write(argNames)
output.write(');\n')
output.write(' ')
if isVoid:
createSuperCall()
output.write(f' if (!m_{name}Flag) return;\n')
else:
createSuperCallWithResult()
output.write(f' if (!m_{name}Flag) return superCallResult;\n')
output.write(f' if (auto func = luaDataHolder()["{name}"].template is<clg::function>())\n')
output.write(' {\n')
if not argNames:
argsNamesWithComma = ""
else:
argsNamesWithComma = f', {argNames}'
if isVoid:
try_catch_wrapper(f' (*func)(aui::ptr::shared_from_this(this){argsNamesWithComma});\n')
else:
try_catch_wrapper(f' return (*func).template call<{returnType}>(aui::ptr::shared_from_this(this){argsNamesWithComma});\n')
output.write(' }\n')
if not isVoid:
output.write(f' return superCallResult;\n')
self.methods.append(name)
output.write('}\n')
while not self.blockEnd:
self.process({
RE_METHOD_DEF: visit_method_def,
RE_BLOCK_END: visit_block_end
})
self.remaining = "\n".join(input.readlines())
while len(self.remaining) > 0:
self.process({
RE_TEMPLATE: visit_template,
RE_CLASS_DEF: visit_class_def,
})
def process(self, table):
if len(self.remaining) == 0:
raise EOFError()
for pattern, callback in table.items():
match = pattern.match(self.remaining)
if match is not None:
self.remaining = self.remaining[match.end():]
callback(match)
return
self.output.write(self.remaining[:self.remaining.find("\n") + 1])
self.remaining = self.remaining[self.remaining.find("\n") + 2:]
p = Parser()
p.run(source, destination)