-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpp2uml.lua
executable file
·80 lines (72 loc) · 2.22 KB
/
cpp2uml.lua
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
#!/usr/bin/lua
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '\n['..k..'] = ' .. dump(v) .. ','
end
return s .. '}\n '
else
return tostring(o)
end
end
function toUML(classTable)
local plantUMLString = "class \""..classTable.baseClass.."\"\n"
for k,v in ipairs(classTable.motherClass) do
plantUMLString = plantUMLString..(v.." <|-- \""..classTable.baseClass).."\"\n"
end
for k,v in ipairs(classTable.attribute) do
plantUMLString = plantUMLString..('"'..classTable.baseClass.."\" : "..v).."\n"
end
for k,v in ipairs(classTable.method) do
plantUMLString = plantUMLString..('"'..classTable.baseClass.."\" : "..v).."\n"
end
return plantUMLString
end
print("@startuml")
print("skinparam classAttributeIconSize 0")
for headerFile in io.input():lines() do
if headerFile:match(".h$") then
local file_ = io.open(headerFile)
local file = file_:read("*a")
file_:close()
local classIt = file:gmatch("(.-):")
file = file:gsub("::","_"):gsub("#endif",":"):gsub("enum.-;","")
local class = {}
class.motherClass = {}
class.method = {}
class.attribute = {}
file = file:gsub("};",":")
local classPattern = "class%s+([%w%s:_,]-)%s*{"
local classHead = file:match(classPattern)
file = file:gsub("class "..classHead,"",1)
local baseClass, motherClass = classHead:match("(%w+)%s*:%s*([%s,%w]+)")
if motherClass then
class.baseClass = baseClass
motherClass = motherClass:gmatch("(public%s+%w+)")
class.motherClass = {}
for mother in motherClass do
local motherClassName = mother:gsub("public%s+","")
table.insert(class.motherClass,motherClassName)
end
else
class.baseClass = classHead
end
local classIt = file:gmatch("(.-):")
for chunk in classIt do
chunk = chunk:gsub("//.-\n%s",""):gsub("/\*.*\*/",""):gsub("^#.*$","")
for member in chunk:gmatch("%s(.-)[;|{]}?%s") do
local trimedMember = member:gsub("^%s*",""):gsub("\n%s*"," ")
if member:match("%(") then
table.insert(class.method,trimedMember)
elseif trimedMember ~= "" then
table.insert(class.attribute,trimedMember)
end
end
end
-- print(dump(class))
print(toUML(class))
end
end
print("@enduml")