ToDoxHook.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. --
  2. -- Copyright (c) 2008-2014 the Urho3D project.
  3. --
  4. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  5. -- of this software and associated documentation files (the "Software"), to deal
  6. -- in the Software without restriction, including without limitation the rights
  7. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. -- copies of the Software, and to permit persons to whom the Software is
  9. -- furnished to do so, subject to the following conditions:
  10. --
  11. -- The above copyright notice and this permission notice shall be included in
  12. -- all copies or substantial portions of the Software.
  13. --
  14. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. -- THE SOFTWARE.
  21. --
  22. classes = {}
  23. enumerates = {}
  24. globalConstants = {}
  25. globalFunctions = {}
  26. globalProperties = {}
  27. currentClass = nil
  28. currentFunction = nil
  29. function classClass:print(ident,close)
  30. local class = {}
  31. class.name = self.name
  32. class.base = self.base
  33. class.lname = self.lname
  34. class.type = self.type
  35. class.btype = self.btype
  36. class.ctype = self.ctype
  37. currentClass = class
  38. local i = 1
  39. while self[i] do
  40. self[i]:print(ident.." ",",")
  41. i = i + 1
  42. end
  43. currentClass = nil
  44. table.insert(classes, class)
  45. end
  46. function classCode:print(ident,close)
  47. end
  48. function classDeclaration:print(ident,close)
  49. local declaration = {}
  50. declaration.mod = self.mod
  51. declaration.type = self.type
  52. declaration.ptr = self.ptr
  53. declaration.name = self.name
  54. declaration.dim = self.dim
  55. declaration.def = self.def
  56. declaration.ret = self.ret
  57. if currentFunction ~= nil then
  58. if currentFunction.declarations == nil then
  59. currentFunction.declarations = { declaration }
  60. else
  61. table.insert(currentFunction.declarations, declaration)
  62. end
  63. end
  64. end
  65. function classEnumerate:print(ident,close)
  66. local enumerate = {}
  67. enumerate.name = self.name
  68. local i = 1
  69. while self[i] do
  70. if self[i] ~= "" then
  71. if enumerate.values == nil then
  72. enumerate.values = { self[i] }
  73. else
  74. table.insert(enumerate.values, self[i])
  75. end
  76. end
  77. i = i + 1
  78. end
  79. if enumerate.values ~= nil then
  80. table.insert(enumerates, enumerate)
  81. end
  82. end
  83. function deepCopy(t)
  84. if type(t) ~= "table" then
  85. return t
  86. end
  87. local mt = getmetatable(t)
  88. local ret = {}
  89. for k, v in pairs(t) do
  90. if type(v) == "table" then
  91. v = deepCopy(v)
  92. end
  93. ret[k] = v
  94. end
  95. setmetatable(ret, mt)
  96. return ret
  97. end
  98. function printFunction(self,ident,close,isfunc)
  99. local func = {}
  100. func.mod = self.mod
  101. func.type = self.type
  102. func.ptr = self.ptr
  103. func.name = self.name
  104. func.lname = self.lname
  105. func.const = self.const
  106. func.cname = self.cname
  107. func.lname = self.lname
  108. if isfunc then
  109. func.name = func.lname
  110. end
  111. currentFunction = func
  112. local i = 1
  113. while self.args[i] do
  114. self.args[i]:print(ident.." ",",")
  115. i = i + 1
  116. end
  117. currentFunction = nil
  118. if currentClass == nil then
  119. table.insert(globalFunctions, func)
  120. else
  121. if func.name == "new" then
  122. -- add construct function
  123. local ctor = deepCopy(func)
  124. ctor.type = ""
  125. ctor.ptr = ""
  126. ctor.name = currentClass.name
  127. ctor.lname = currentClass.name
  128. ctor.const = "(GC)"
  129. ctor.cname = currentClass.name
  130. ctor.lname = currentClass.name
  131. if currentClass.functions == nil then
  132. currentClass.functions = { ctor }
  133. else
  134. table.insert(currentClass.functions, ctor)
  135. end
  136. end
  137. if func.name == "delete" then
  138. func.type = "void"
  139. end
  140. if currentClass.functions == nil then
  141. currentClass.functions = { func }
  142. else
  143. table.insert(currentClass.functions, func)
  144. end
  145. end
  146. end
  147. function classFunction:print(ident,close)
  148. printFunction(self,ident,close, true)
  149. end
  150. function classOperator:print(ident,close)
  151. printFunction(self,ident,close, false)
  152. end
  153. function classVariable:print(ident,close)
  154. local property = {}
  155. property.mod = self.mod
  156. property.type = self.type
  157. property.ptr = self.ptr
  158. property.name = self.lname
  159. property.def = self.def
  160. property.ret = self.ret
  161. if currentClass == nil then
  162. if property.mod:find("tolua_property__") == nil then
  163. table.insert(globalConstants, property)
  164. else
  165. table.insert(globalProperties, property)
  166. end
  167. else
  168. if currentClass.properties == nil then
  169. currentClass.properties = { property }
  170. else
  171. table.insert(currentClass.properties, property)
  172. end
  173. end
  174. end
  175. function classVerbatim:print(ident,close)
  176. end
  177. function sortByName(t)
  178. table.sort(t, function(a, b) return a.name < b.name end)
  179. end
  180. function writeClass(file, class)
  181. if class.base == "" then
  182. file:write("### " .. class.name .. "\n\n")
  183. else
  184. file:write("### " .. class.name .. " : " .. class.base .. "\n")
  185. end
  186. if class.functions ~= nil then
  187. file:write("\nMethods:\n\n")
  188. for i, func in ipairs(class.functions) do
  189. writeFunction(file, func)
  190. end
  191. end
  192. if class.properties ~= nil then
  193. file:write("\nProperties:\n\n")
  194. for i, property in ipairs(class.properties) do
  195. writeProperty(file, property)
  196. end
  197. end
  198. file:write("\n")
  199. end
  200. function writeClasses(file)
  201. sortByName(classes)
  202. file:write("\n\\section LuaScriptAPI_Classes Classes\n\n")
  203. for i, class in ipairs(classes) do
  204. writeClass(file, class)
  205. end
  206. end
  207. function writeEnumerates(file)
  208. sortByName(enumerates)
  209. file:write("\\section LuaScriptAPI_Enums Enumerations\n\n")
  210. for i, enumerate in ipairs(enumerates) do
  211. file:write("### " .. enumerate.name .. "\n\n")
  212. for i, value in ipairs(enumerate.values) do
  213. file:write("- int " .. value .. "\n")
  214. end
  215. file:write("\n")
  216. end
  217. end
  218. function writeFunction(file, func)
  219. local line = "- "
  220. -- construct function
  221. if func.type == "" and func.ptr == "" then
  222. line = line .. func.name .. "("
  223. else
  224. line = line .. func.type .. func.ptr .. " " .. func.name .. "("
  225. end
  226. -- write parameters
  227. if func.declarations ~= nil then
  228. local count = table.maxn(func.declarations)
  229. for i = 1, count do
  230. local declaration = func.declarations[i]
  231. if declaration.type ~= "void" then
  232. line = line .. declaration.type .. declaration.ptr .. " " .. declaration.name
  233. -- add paramter default value
  234. if declaration.def ~= "" then
  235. line = line .. " = " .. declaration.def
  236. end
  237. end
  238. if i ~= count then
  239. line = line .. ", "
  240. end
  241. end
  242. end
  243. line = line .. ")"
  244. -- add const
  245. if func.const ~= "" then
  246. line = line .. " " .. func.const
  247. end
  248. file:write(line .. "\n")
  249. end
  250. function writeGlobalConstants(file)
  251. sortByName(globalConstants)
  252. file:write("\n\\section LuaScriptAPI_GlobalConstants Global constants\n")
  253. for i, constant in ipairs(globalConstants) do
  254. local line = "- " .. constant.type:gsub("const ", "") .. constant.ptr .. " " .. constant.name
  255. file:write(line .. "\n")
  256. end
  257. file:write("\n")
  258. end
  259. function writeGlobalFunctions(file)
  260. sortByName(globalFunctions)
  261. file:write("\n\\section LuaScriptAPI_GlobalFunctions Global functions\n")
  262. for i, func in ipairs(globalFunctions) do
  263. writeFunction(file, func)
  264. end
  265. file:write("\n")
  266. end
  267. function writeGlobalProperties(file)
  268. sortByName(globalProperties)
  269. file:write("\n\\section LuaScriptAPI_GlobalProperties Global properties\n")
  270. for i, property in ipairs(globalProperties) do
  271. writeProperty(file, property)
  272. end
  273. end
  274. function writeProperty(file, property)
  275. file:write("- " .. property.type .. property.ptr .. " " .. property.name)
  276. if property.mod:find("tolua_readonly") == nil then
  277. file:write("\n")
  278. else
  279. file:write(" (readonly)\n")
  280. end
  281. end
  282. function classPackage:print()
  283. if flags.o == nil then
  284. print("Invalid output filename");
  285. return
  286. end
  287. local filename = flags.o
  288. local file = io.open(filename, "wt")
  289. file:write("namespace Urho3D\n")
  290. file:write("{\n")
  291. file:write("\n")
  292. file:write("/**\n")
  293. file:write("\\page LuaScriptAPI Lua Scripting API\n")
  294. local i = 1
  295. while self[i] do
  296. self[i]:print("","")
  297. i = i + 1
  298. end
  299. writeClasses(file)
  300. writeEnumerates(file)
  301. writeGlobalFunctions(file)
  302. writeGlobalProperties(file)
  303. writeGlobalConstants(file)
  304. file:write("*/\n")
  305. file:write("\n")
  306. file:write("}\n")
  307. file:close()
  308. end