-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcparser.lua
More file actions
182 lines (169 loc) · 7.17 KB
/
cparser.lua
File metadata and controls
182 lines (169 loc) · 7.17 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
local luaclang = require "luaclang"
--module table
local cparser = {}
function obtain_type(cursor_type, cursor_map)
local type_kind = cursor_type:getTypeKind()
if type_kind == "ConstantArray" then
local element_type = cursor_type:getArrayElementType()
return {
tag = 'array',
n = cursor_type:getArraySize(),
type = obtain_type(element_type, cursor_map)
}
elseif type_kind == "Pointer" then
local pointee_type = cursor_type:getPointeeType()
if pointee_type:getTypeKind() ~= "FunctionProto" then
return {
tag = 'pointer',
type = obtain_type(pointee_type, cursor_map)
}
else
return obtain_type(pointee_type, cursor_map)
end
elseif type_kind == "FunctionProto" then
local return_type = cursor_type:getResultType()
local func_pointer_table = {
tag = 'function-pointer',
ret = obtain_type(return_type, cursor_map),
fields = {}
}
local num_params = cursor_type:getNumArgTypes()
for i=1, num_params do
local arg_type = cursor_type:getArgType(i)
table.insert(func_pointer_table.fields, obtain_type(arg_type, cursor_map))
end
return func_pointer_table
elseif type_kind == "Elaborated" or type_kind == "Typedef" then
local cursor = cursor_type:getTypeDeclaration()
for i, c in ipairs(cursor_map) do
if c:equals(cursor) then
return {
tag = 'decl',
decl = i
}
end
end
end
return cursor_type:getSpelling()
end
function add_declaration(cursor, decl, declarations, cursor_map)
table.insert(declarations, decl)
table.insert(cursor_map, cursor)
end
local function enum_handler(cursor, declarations, cursor_map)
local fields = {}
local decl = {
tag = 'enum',
name = cursor:getSpelling(),
fields = fields
}
add_declaration(cursor, decl, declarations, cursor_map)
cursor:visitChildren(function (cursor)
local kind = cursor:getKind()
local enum_const
if kind == "EnumConstantDecl" then
enum_const = {
name = cursor:getSpelling(),
value = cursor:getEnumValue()
}
end
table.insert(fields, enum_const)
return "continue"
end)
end
local function union_struct_handler(cursor, declarations, cursor_map)
local fields = {}
local decl = {
name = cursor:getSpelling(),
tag = cursor:getKind() == "StructDecl" and 'struct' or 'union',
fields = fields
}
add_declaration(cursor, decl, declarations, cursor_map)
cursor:visitChildren(function (cursor)
local kind = cursor:getKind()
if kind == "FieldDecl" then
local type = cursor:getType()
local field = {
name = cursor:getSpelling(),
type = obtain_type(type, cursor_map),
}
if cursor:isBitField() then
field.bit_field = "true"
field.field_width = cursor:getBitFieldWidth()
end
table.insert(fields, field)
elseif kind == "StructDecl" or kind == "UnionDecl" then
union_struct_handler(cursor, declarations, cursor_map)
elseif kind == "EnumDecl" then
enum_handler(cursor, declarations, cursor_map)
end
return "continue"
end)
end
local function var_handler(cursor, declarations, cursor_map)
local type = cursor:getType()
local decl = {
tag = 'variable',
name = cursor:getSpelling(),
type = obtain_type(type, cursor_map),
storage_specifier = cursor:getStorageClass(),
}
add_declaration(cursor, decl, declarations, cursor_map)
end
local function function_handler(cursor, declarations, cursor_map)
local func_params = {}
local type = cursor:getType()
local return_type = type:getResultType()
local decl = {
tag = 'function',
name = cursor:getSpelling(),
inline = cursor:isFunctionInlined(),
storage_specifier = cursor:getStorageClass(),
ret = obtain_type(return_type, cursor_map),
params = func_params
}
add_declaration(cursor, decl, declarations, cursor_map)
local num_params = cursor:getNumArgs()
for i=1, num_params do
local param_cursor = cursor:getArgCursor(i)
param_type = param_cursor:getType()
local param = {
name = param_cursor:getSpelling(),
type = obtain_type(param_type, cursor_map)
}
table.insert(func_params, param)
end
end
--global visitor for spotting all declarations in the file
local function toplevel_visitor(cursor, _, declarations, cursor_map)
local kind = cursor:getKind()
if kind == "EnumDecl" then
enum_handler(cursor, declarations, cursor_map)
elseif kind == "FunctionDecl" then
function_handler(cursor, declarations, cursor_map)
elseif kind == "TypedefDecl" then
local underlying_type = cursor:getTypedefUnderlyingType()
local type = cursor:getType()
local decl = {
tag = 'typedef',
underlying_type = obtain_type(underlying_type, cursor_map),
type = type:getSpelling()
}
add_declaration(cursor, decl, declarations, cursor_map)
elseif kind == "StructDecl" or kind == "UnionDecl" then
union_struct_handler(cursor, declarations, cursor_map)
elseif kind == "VarDecl" then
var_handler(cursor, declarations, cursor_map)
end
return "continue"
end
function cparser.parse(file_name)
local parser = luaclang.newParser(file_name)
local declarations = {}
local cursor_map = {} --auxiliary table for referencing cursors
local cur = parser:getCursor()
cur:visitChildren(toplevel_visitor, declarations, cursor_map)
parser:dispose()
return declarations
end
return cparser