-
Notifications
You must be signed in to change notification settings - Fork 13
/
sourceparser.py
executable file
·251 lines (186 loc) · 9.81 KB
/
sourceparser.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import sys
sys.path.insert(0, './pycparser/')
import pycparser
import pycparser.ply.cpp
from pycparser import c_ast, c_generator
import copy
import os
import re
import collections
# TODO: this should go into a utils.py
def generate_c(node):
return c_generator.CGenerator().visit(node)
def type_repr(arg):
ptrs = ''
while isinstance(arg, c_ast.PtrDecl):
ptrs += '*'
arg = arg.type
quals = ''.join('%s ' % qual for qual in sorted(arg.quals)) if hasattr(arg, 'quals') else ''
return f'{quals}{generate_c(arg)}{ptrs}'
def stripstart(s, start):
if not s.startswith(start):
raise ValueError(f'{s!r} does not start with {start!r}')
return s[len(start):]
ParseResult = collections.namedtuple('ParseResult', 'enums functions declarations typedefs structs objects global_functions defines')
LvglObject = collections.namedtuple('LvglObject', 'name methods ancestor')
def flatten_ast(node):
yield node
for name, child in node.children():
yield from flatten_ast(child)
class LvglSourceParser:
TYPEDEFS = {
'uint8_t' : 'unsigned int',
'int8_t' : 'int',
'uint16_t' : 'unsigned int',
'int16_t' :'int',
'uint32_t' : 'unsigned int',
'int32_t': 'int',
'bool': '_Bool'
}
def __init__(self):
self.lexer = pycparser.ply.lex.lex(module = pycparser.ply.cpp)
def parse_file(self, filename):
if os.name == 'nt':
args = ['-Ipycparser/utils/fake_libc_include']
cpp_path = r'C:\Program Files\LLVM\bin\clang.exe'
else:
args = ['-I../pycparser/utils/fake_libc_include']
cpp_path = 'gcc'
# TODO: preprocessor for Windows
return pycparser.parse_file(filename, use_cpp=True, cpp_path=cpp_path, cpp_args=['-E'] + args)
@staticmethod
def enum_to_dict(enum_node):
'''
Given a c enum ast node, determine:
- the name of the enum in python, using the common prefix of all enum items
- a dictionary with as keys the names as they should be in Python, and as values the C enum constant names
'''
items = [enumitem.name for enumitem in enum_node.values.enumerators if not enumitem.name.startswith('_')]
prefix = os.path.commonprefix(items)
prefix = "_".join(prefix.split("_")[:-1])
enumname = stripstart(prefix, 'LV_')
return enumname, {stripstart(item, prefix + '_'): item for item in items}
def parse_sources(self, path):
'''
Parse all lvgl sources which are required for generating the bindings
returns: ParseResult (collections.namedtuple) with the following fields:
enums: dict of enum name -> pycparser.c_ast.FuncDef object
functions: dict of enum name -> value
objects: dict of object name -> LvglObject object
defines: dict of name -> string representation of evaluated #define
'''
enums = collections.OrderedDict()
functions = collections.OrderedDict()
declarations = collections.OrderedDict()
structs = collections.OrderedDict()
typedefs = collections.OrderedDict()
ast = self.parse_file(os.path.join(path, 'lvgl.h'))
previous_item = None
# TODO: this whole filtering of items could be done in the bindings generator to allow for extending bindings generator without changing the sourceparser
for item in ast.ext:
if isinstance(item, c_ast.Decl) and isinstance(item.type, c_ast.FuncDecl):
# C function
if item.name not in functions:
# If it is already in there, it might be a FuncDef and we want to keep that one
functions[item.name] = c_ast.FuncDef(item, None, None)
elif isinstance(item, c_ast.FuncDef):
functions[item.decl.name] = item
elif isinstance(item, c_ast.Typedef) and not item.name in self.TYPEDEFS:
# Do not register typedefs which are defined in self.TYPEDEFS, these are
# to be treated as basic types by the bindings generators
typedefs[item.name] = item
if isinstance(item.type, c_ast.TypeDecl) and (isinstance(item.type.type, c_ast.Struct) or isinstance(item.type.type, c_ast.Union)):
# typedef struct { ... } lv_struct_name_t;
try:
structs[stripstart(item.type.declname,'lv_')] = item.type.type
except ValueError: # If name does not start with lv_
pass
elif (isinstance(item.type, c_ast.TypeDecl) and isinstance(item.type.type, c_ast.IdentifierType) and
isinstance(previous_item, c_ast.Decl) and isinstance(previous_item.type, c_ast.Enum)):
# typedef lv_enum_t ...; directly after an enum definition
# newer lvgl uses this to define enum variables as uint8_t
enumname, enum = self.enum_to_dict(previous_item.type)
enums[enumname] = enum
elif isinstance(item, c_ast.Decl) and isinstance(item.type, c_ast.TypeDecl):
declarations[stripstart(item.type.declname, 'lv_')] = item.type
previous_item = item
objects, global_functions = self.determine_objects(functions, typedefs)
# Find defines in color.h and symbol_def.h
defines = collections.OrderedDict() # There is no OrderedSet in Python, so let's use OrderedDict with None values
for filename in 'src/lv_misc/lv_color.h', 'src/lv_misc/lv_symbol_def.h':
with open(os.path.join(path, filename), 'rt', encoding='utf-8') as file:
code = file.read()
for define in re.findall('^\s*#define\s+(\w+)', code,flags = re.MULTILINE):
if not define.startswith('_'):
defines[define] = None
return ParseResult(enums, functions, declarations, typedefs, structs, objects, global_functions, defines)
@classmethod
def determine_objects(cls, functions, typedefs):
# Stage 1: find all objects and the name of their ancestor
# use the typedefs of lv_xxx_ext_t to determine which objects exist,
# and to determine their ancestors
objects = {}
lv_ext_pattern = re.compile('^lv_([^_]+)_ext_t')
for typedef in typedefs.values():
match = lv_ext_pattern.match(typedef.name)
if match:
try:
parent_ext_name = ' '.join(typedef.type.type.decls[0].type.type.names)
except AttributeError:
parent_ext_name = ''
parentmatch = lv_ext_pattern.match(parent_ext_name)
if parentmatch:
objects[match.group(1)] = parentmatch.group(1)
else:
objects[match.group(1)] = 'obj'
# Stage 2: sort objects, such that each object comes after its ancestors
sortedobjects = collections.OrderedDict(obj = None)
while objects:
remaining = list(objects.items())
reordered = False
for obj, anch in remaining:
if anch in sortedobjects:
sortedobjects[obj] = anch
objects.pop(obj)
reordered = True
if not reordered:
raise RuntimeError('No ancestor defined for:', remaining)
# Stage 3: Create LvglObject items
objects = collections.OrderedDict()
for name, ancestorname in sortedobjects.items():
ancestor = None if ancestorname is None else objects[ancestorname]
objects[name] = LvglObject(name, collections.OrderedDict(), ancestor)
# Stage 4: Assign methods to objects; those who do not belong to an
# object go into global_functions
global_functions = collections.OrderedDict()
for function_name, function in functions.items():
match = re.match(r'lv_([a-zA-Z0-9]+)_([a-zA-Z0-9_]+)$', function_name)
name, method = match.groups() if match else (None, None)
if name in objects and type_repr(function.decl.type.args.params[0].type) in ('lv_obj_t*', 'const lv_obj_t*'):
if method != 'create':
objects[name].methods[method] = function
else:
global_functions[function_name] = function
return objects, global_functions
if __name__ == '__main__':
result = LvglSourceParser().parse_sources('lvgl')
print ('##################################')
print ('# OBJECTS #')
print ('##################################')
for object in result.objects.values():
print (f'{object.name}({object.ancestor.name if object.ancestor else ""})')
for methodname, method in object.methods.items():
args = ','.join(
type_repr(param.type)+ ' ' + param.name
for param in method.decl.type.args.params)
print(f' {methodname}({args})')
print ('##################################')
print ('# ENUMS #')
print ('##################################')
for name, value in result.enums.items():
print (name, '=', value)
print ('##################################')
print ('# DEFINES #')
print ('##################################')
for name, value in result.defines.items():
print (name, '=', value)