pkgToDox.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. return
  97. end
  98. if line:find("class ") ~= nil or line:find("struct ") ~= nil then -- begin of class or struct.
  99. type = CLASS
  100. table.insert(classes, line)
  101. return
  102. end
  103. if line:find(")") ~= nil then -- global function.
  104. table.insert(globalFunctions, line)
  105. else
  106. local i, _, oldName, newName = line:find("$renaming%s+(.-)%s*@%s*(.-)%s*$")
  107. if i ~= nill then -- renaming types.
  108. renamings[oldName] = newName
  109. else
  110. table.insert(globalConstants, line)
  111. end
  112. end
  113. return
  114. end
  115. end
  116. function gatherPkgFile(apiFile)
  117. local file = io.open(apiFile)
  118. if file == nil then
  119. return
  120. end
  121. local line = file:read()
  122. while line ~= nil do
  123. line = trim(line)
  124. local i, _, pkgFile = line:find("$pfile \"(.+)\"")
  125. if i ~= nil then -- begin of pkg file.
  126. table.insert(pkgFiles, pkgFile)
  127. end
  128. line = file:read()
  129. end
  130. file:close()
  131. end
  132. function handlePkgFile(pkgFile)
  133. local file = io.open(pkgFile)
  134. if file == nil then
  135. return
  136. end
  137. type = GLOBAL
  138. local line = file:read()
  139. while line ~= nil do
  140. line = trim(line)
  141. if isValidLine(line) then
  142. handleLine(line)
  143. end
  144. line = file:read()
  145. end
  146. file:close()
  147. end
  148. for i=2,#arg do
  149. gatherPkgFile(arg[i])
  150. end
  151. for _, pkgFile in ipairs(pkgFiles) do
  152. handlePkgFile(pkgFile)
  153. end
  154. function writeGlobalFunctions(ofile)
  155. ofile:write("\\section LuaScriptAPI_GlobalFunctions Global functions\n")
  156. for _, line in ipairs(globalFunctions) do
  157. line = line:gsub("%w+ @ ", "")
  158. line = line:gsub(";", "")
  159. ofile:write("- ", line, "\n")
  160. end
  161. ofile:write("\n")
  162. end
  163. function writeGlobalConstants(ofile)
  164. ofile:write("\\section LuaScriptAPI_GlobalConstants Global constants\n")
  165. for _, line in ipairs(globalConstants) do
  166. line = line:gsub("static ", "")
  167. line = line:gsub("const ", "")
  168. line = line:gsub(";", "")
  169. ofile:write("- ", line, "\n")
  170. end
  171. for _, line in ipairs(enums) do
  172. line = line:gsub(",", "")
  173. line = line:gsub(" = .*", "")
  174. ofile:write("- int ", line, "\n")
  175. end
  176. ofile:write("\n")
  177. end
  178. function writeClasses(ofile)
  179. ofile:write("\\section LuaScriptAPI_Classes Classes\n")
  180. local firstProperty = true
  181. for _, line in ipairs(classes) do
  182. if line:find("class ") ~= nil or line:find("struct ") ~= nil then
  183. line = line:gsub("class ", "")
  184. line = line:gsub("struct ", "")
  185. line = line:gsub("public ", "")
  186. ofile:write("\n### ", line, "\n\nMethods:\n\n")
  187. firstProperty = true
  188. else
  189. line = line:gsub(";", "")
  190. if line:find(")") ~= nil then
  191. line = line:gsub(" %w+ @ ", " ")
  192. line = line:gsub("explicit ", "")
  193. line = line:gsub("tolua_outside ", "")
  194. ofile:write("- ", line, "\n")
  195. else
  196. if firstProperty then
  197. firstProperty = false
  198. ofile:write("\nProperties:\n\n")
  199. end
  200. line = line:gsub(" %w+_ @ ", " ")
  201. line = line:gsub("tolua_property__get_set ", "")
  202. line = line:gsub("tolua_property__is_set ", "")
  203. line = line:gsub("tolua_property__has_set ", "")
  204. line = line:gsub("tolua_property__no_prefix ", "")
  205. if line:find("tolua_readonly") == nil then
  206. ofile:write("- ", line, "\n")
  207. else
  208. line = line:gsub("tolua_readonly ", "")
  209. ofile:write("- ", line, " (readonly)\n")
  210. end
  211. end
  212. end
  213. end
  214. ofile:write("\n")
  215. end
  216. function writeRenamings(ofile)
  217. ofile:write("\\section LuaScriptAPI_RenameTypes Rename types\n")
  218. for oldName, newName in pairs(renamings) do
  219. ofile:write("- ", oldName, " becomes ", newName, "\n")
  220. end
  221. ofile:write("\n")
  222. end
  223. ofile = io.open(arg[1], "w")
  224. ofile:write([[
  225. namespace Urho3D
  226. {
  227. /**
  228. \page LuaScriptAPI Lua Scripting API
  229. ]])
  230. ofile:write("\n")
  231. writeClasses(ofile)
  232. writeGlobalFunctions(ofile)
  233. writeGlobalConstants(ofile)
  234. writeRenamings(ofile)
  235. ofile:write([[
  236. */
  237. }
  238. ]])