pkgToDox.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. -- Generate dox from pkg file
  2. if #arg == 0 then
  3. print("Usage: lua pkgToDox.lua <output file name> <input pkg file name> ...")
  4. os.exit(1)
  5. end
  6. pkgFiles = {}
  7. enums = {}
  8. classes = {}
  9. globalFunctions = {}
  10. globalProperties = {}
  11. globalConstants = {}
  12. renamings = {}
  13. -- trim string.
  14. function trim(s)
  15. return (s:gsub("^%s*(.-)%s*$", "%1"))
  16. end
  17. -- check is embed code.
  18. inEmbed = false
  19. function isEmbedLine(line)
  20. if string.sub(line, 1, 2) == "$#" then
  21. return true
  22. end
  23. if not inEmbed then
  24. if string.find(line, "${") ~= nil or string.find(line, "$[[]") ~= nil then
  25. inEmbed = true
  26. return true
  27. end
  28. else
  29. if string.find(line, "$}") ~= nil or string.find(line, "$[]]") ~= nil then
  30. inEmbed = false
  31. return true
  32. end
  33. end
  34. return inEmbed
  35. end
  36. -- check is comment.
  37. inComment = false
  38. function isCommentLine(line)
  39. if line:find("//") ~= nil then
  40. return true
  41. end
  42. if inComment then
  43. if line:find("[*]/") ~= nil then
  44. inComment = false
  45. return true
  46. end
  47. else
  48. if line:find("/[*]") ~= nil then
  49. inComment = true
  50. return true
  51. end
  52. end
  53. return inComment
  54. end
  55. -- check is valid line.
  56. function isValidLine(line)
  57. if #line < 2 then
  58. return fasle
  59. end
  60. if isEmbedLine(line) then
  61. return false
  62. end
  63. if isCommentLine(line) then
  64. return false
  65. end
  66. -- Declaration line.
  67. if (string.find(line, "enum ") ~= nil or string.find(line, "class ") ~= nil or
  68. string.find(line, "struct ") ~= nil ) and string.find(line, "}") ~= nil then
  69. return false
  70. end
  71. return true
  72. end
  73. local ENUM = 1 -- line is enum code.
  74. local CLASS = 2 -- line is class code.
  75. local GLOBAL = 3 -- line is global code.
  76. local type = GLOBAL -- current line type.
  77. function handleLine(line)
  78. if type == ENUM then
  79. if line:find("};") ~= nil then -- end of enum.
  80. type = GLOBAL
  81. else
  82. table.insert(enums, line)
  83. end
  84. return
  85. end
  86. if type == CLASS then
  87. if line:find("};") ~= nil then -- end of class or struct.
  88. type = GLOBAL
  89. else
  90. table.insert(classes, line)
  91. end
  92. return
  93. end
  94. if type == GLOBAL then
  95. if line:find("enum ") ~= nil then
  96. type = ENUM
  97. table.insert(enums, line)
  98. return
  99. end
  100. if line:find("class ") ~= nil or line:find("struct ") ~= nil then -- begin of class or struct.
  101. type = CLASS
  102. table.insert(classes, line)
  103. return
  104. end
  105. if line:find(")") ~= nil then -- global function.
  106. table.insert(globalFunctions, line)
  107. elseif line:find("tolua_property") ~= nil then -- global properties
  108. table.insert(globalProperties, line)
  109. else
  110. local i, _, oldName, newName = line:find("$renaming%s+(.-)%s*@%s*(.-)%s*$")
  111. if i ~= nill then -- renaming types.
  112. renamings[oldName] = newName
  113. else
  114. table.insert(globalConstants, line)
  115. end
  116. end
  117. return
  118. end
  119. end
  120. function gatherPkgFile(apiFile)
  121. local file = io.open(apiFile)
  122. if file == nil then
  123. return
  124. end
  125. local line = file:read()
  126. while line ~= nil do
  127. line = trim(line)
  128. local i, _, pkgFile = line:find("$pfile \"(.+)\"")
  129. if i ~= nil then -- begin of pkg file.
  130. table.insert(pkgFiles, pkgFile)
  131. end
  132. line = file:read()
  133. end
  134. file:close()
  135. end
  136. function handlePkgFile(pkgFile)
  137. local file = io.open(pkgFile)
  138. if file == nil then
  139. return
  140. end
  141. type = GLOBAL
  142. local line = file:read()
  143. while line ~= nil do
  144. line = trim(line)
  145. if isValidLine(line) then
  146. handleLine(line)
  147. end
  148. line = file:read()
  149. end
  150. file:close()
  151. end
  152. for i=2,#arg do
  153. gatherPkgFile(arg[i])
  154. end
  155. for _, pkgFile in ipairs(pkgFiles) do
  156. handlePkgFile(pkgFile)
  157. end
  158. function writeGlobalFunctions(ofile)
  159. ofile:write("\\section LuaScriptAPI_GlobalFunctions Global functions\n")
  160. for _, line in ipairs(globalFunctions) do
  161. line = line:gsub("%w+ @ ", "")
  162. line = line:gsub(";", "")
  163. ofile:write("- ", line, "\n")
  164. end
  165. ofile:write("\n")
  166. end
  167. function writeGlobalProperties(ofile)
  168. ofile:write("\\section LuaScriptAPI_GlobalProperties Global properties\n")
  169. for _, line in ipairs(globalProperties) do
  170. line = line:gsub("tolua_property__get_set ", "")
  171. line = line:gsub(";", "")
  172. if line:find("tolua_readonly") == nil then
  173. ofile:write("- ", line, "\n")
  174. else
  175. line = line:gsub("tolua_readonly ", "")
  176. ofile:write("- ", line, " (readonly)\n")
  177. end
  178. end
  179. ofile:write("\n")
  180. end
  181. function writeGlobalConstants(ofile)
  182. ofile:write("\\section LuaScriptAPI_GlobalConstants Global constants\n")
  183. for _, line in ipairs(globalConstants) do
  184. line = line:gsub("static ", "")
  185. line = line:gsub("const ", "")
  186. line = line:gsub(";", "")
  187. ofile:write("- ", line, "\n")
  188. end
  189. ofile:write("\n")
  190. end
  191. function writeEnums(ofile)
  192. ofile:write("\\section LuaScriptAPI_Enums Enumerations\n")
  193. for _, line in ipairs(enums) do
  194. if line:find("enum ") ~= nil or line:find("struct ") ~= nil then
  195. line = line:gsub("enum ", "")
  196. --line = line:gsub("struct ", "")
  197. --line = line:gsub("public ", "")
  198. ofile:write("\n\n### ", line, "\n\n")
  199. --firstProperty = true
  200. else
  201. line = line:gsub(",", "")
  202. line = line:gsub(" = .*", "")
  203. ofile:write("- int ", line, "\n")
  204. end
  205. end
  206. ofile:write("\n")
  207. end
  208. function writeClasses(ofile)
  209. ofile:write("\\section LuaScriptAPI_Classes Classes\n")
  210. local firstProperty = true
  211. for _, line in ipairs(classes) do
  212. if line:find("class ") ~= nil or line:find("struct ") ~= nil then
  213. line = line:gsub("class ", "")
  214. line = line:gsub("struct ", "")
  215. line = line:gsub("public ", "")
  216. ofile:write("\n### ", line, "\n\nMethods:\n\n")
  217. firstProperty = true
  218. else
  219. line = line:gsub(";", "")
  220. if line:find(")") ~= nil then
  221. line = line:gsub(" %w+ @ ", " ")
  222. line = line:gsub("explicit ", "")
  223. line = line:gsub("tolua_outside ", "")
  224. ofile:write("- ", line, "\n")
  225. else
  226. if firstProperty then
  227. firstProperty = false
  228. ofile:write("\nProperties:\n\n")
  229. end
  230. line = line:gsub(" %w+_ @ ", " ")
  231. line = line:gsub("tolua_property__get_set ", "")
  232. line = line:gsub("tolua_property__is_set ", "")
  233. line = line:gsub("tolua_property__has_set ", "")
  234. line = line:gsub("tolua_property__no_prefix ", "")
  235. if line:find("tolua_readonly") == nil then
  236. ofile:write("- ", line, "\n")
  237. else
  238. line = line:gsub("tolua_readonly ", "")
  239. ofile:write("- ", line, " (readonly)\n")
  240. end
  241. end
  242. end
  243. end
  244. ofile:write("\n")
  245. end
  246. function writeRenamings(ofile)
  247. ofile:write("\\section LuaScriptAPI_RenameTypes Rename types\n")
  248. for oldName, newName in pairs(renamings) do
  249. ofile:write("- ", oldName, " becomes ", newName, "\n")
  250. end
  251. ofile:write("\n")
  252. end
  253. ofile = io.open(arg[1], "w")
  254. ofile:write([[
  255. namespace Urho3D
  256. {
  257. /**
  258. \page LuaScriptAPI Lua Scripting API
  259. ]])
  260. ofile:write("\n")
  261. writeClasses(ofile)
  262. writeEnums(ofile)
  263. writeGlobalFunctions(ofile)
  264. writeGlobalProperties(ofile)
  265. writeGlobalConstants(ofile)
  266. writeRenamings(ofile)
  267. ofile:write([[
  268. */
  269. }
  270. ]])