pkgToDox.lua 7.5 KB

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