pkgToDox.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. -- Generate dox from pkg file
  2. require "pkgFiles"
  3. enums = {}
  4. classes = {}
  5. globalConstants = {}
  6. globalFunctions = {}
  7. -- trim string.
  8. function trim(s)
  9. return (s:gsub("^%s*(.-)%s*$", "%1"))
  10. end
  11. -- check is embed code.
  12. inEmbed = false
  13. function isEmbedLine(line)
  14. if string.sub(line, 1, 2) == "$#" then
  15. return true
  16. end
  17. if not inEmbed then
  18. if string.find(line, "${") ~= nil or string.find(line, "$[[]") ~= nil then
  19. inEmbed = true
  20. return true
  21. end
  22. else
  23. if string.find(line, "$}") ~= nil or string.find(line, "$[]]") ~= nil then
  24. inEmbed = false
  25. return true
  26. end
  27. end
  28. return inEmbed
  29. end
  30. -- check is comment.
  31. inComment = false
  32. function isCommentLine(line)
  33. if line:find("//") ~= nil then
  34. return true
  35. end
  36. if inComment then
  37. if line:find("[*]/") ~= nil then
  38. inComment = false
  39. return true
  40. end
  41. else
  42. if line:find("/[*]") ~= nil then
  43. inComment = true
  44. return true
  45. end
  46. end
  47. return inComment
  48. end
  49. -- check is valid line.
  50. function isValidLine(line)
  51. if #line < 2 then
  52. return fasle
  53. end
  54. if isEmbedLine(line) then
  55. return false
  56. end
  57. if isCommentLine(line) then
  58. return false
  59. end
  60. -- Declaration line.
  61. if (string.find(line, "enum ") ~= nil or string.find(line, "class ") ~= nil or
  62. string.find(line, "struct ") ~= nil ) and string.find(line, "}") ~= nil then
  63. return false
  64. end
  65. return true
  66. end
  67. local ENUM = 1 -- line is enum code.
  68. local CLASS = 2 -- line is class code.
  69. local GLOBAL = 3 -- line is global code.
  70. local type = GLOBAL -- current line type.
  71. function handleLine(line)
  72. if type == ENUM then
  73. if line:find("};") ~= nil then -- end of enum.
  74. type = GLOBAL
  75. else
  76. table.insert(enums, line)
  77. end
  78. return
  79. end
  80. if type == CLASS then
  81. if line:find("};") ~= nil then -- end of class or struct.
  82. type = GLOBAL
  83. else
  84. table.insert(classes, line)
  85. end
  86. return
  87. end
  88. if type == GLOBAL then
  89. if line:find("enum ") ~= nil then
  90. type = ENUM
  91. return
  92. end
  93. if line:find("class ") ~= nil or line:find("struct ") ~= nil then -- begin of class or struct.
  94. type = CLASS
  95. table.insert(classes, line)
  96. return
  97. end
  98. if line:find(")") == nil then -- global function.
  99. table.insert(globalConstants, line)
  100. else
  101. table.insert(globalFunctions, line)
  102. end
  103. return
  104. end
  105. end
  106. function handlePkgFile(pkgFile)
  107. local file = io.open(pkgFile)
  108. if file == nil then
  109. return
  110. end
  111. type = GLOBAL
  112. local line = file:read()
  113. while line ~= nil do
  114. line = trim(line)
  115. if isValidLine(line) then
  116. handleLine(line)
  117. end
  118. line = file:read()
  119. end
  120. file:close()
  121. end
  122. for _, pkgFile in ipairs(pkgFiles) do
  123. handlePkgFile(pkgFile)
  124. end
  125. function writeGlobalFunctions(ofile)
  126. ofile:write("\\section LuaScriptAPI_GlobalFunctions Global functions\n")
  127. for _, line in ipairs(globalFunctions) do
  128. line = line:gsub("%a @ ", "")
  129. line = line:gsub(";", "")
  130. ofile:write("- " .. line .. "\n")
  131. end
  132. ofile:write("\n")
  133. end
  134. function writeGlobalConstants(ofile)
  135. ofile:write("\\section LuaScriptAPI_GlobalConstants Global constants\n")
  136. for _, line in ipairs(globalConstants) do
  137. line = line:gsub("static ", "")
  138. line = line:gsub("const ", "")
  139. line = line:gsub(";", "")
  140. ofile:write("- " .. line .. "\n")
  141. end
  142. for _, line in ipairs(enums) do
  143. line = line:gsub(",", "")
  144. line = line:gsub(" = .*", "")
  145. ofile:write("- int " .. line .. "\n")
  146. end
  147. ofile:write("\n")
  148. end
  149. function writeClasses(ofile)
  150. ofile:write("\\section LuaScriptAPI_Classes Classes\n")
  151. local firstProperty = true
  152. for _, line in ipairs(classes) do
  153. if line:find("class ") ~= nil or line:find("struct ") ~= nil then
  154. line = line:gsub("class ", "")
  155. line = line:gsub("struct ", "")
  156. line = line:gsub("public ", "")
  157. ofile:write("\n### " .. line .. "\n\nMethods:\n\n")
  158. firstProperty = true
  159. else
  160. line = line:gsub(";", "")
  161. if line:find(")") ~= nil then
  162. line = line:gsub(" %w+ @ ", " ")
  163. line = line:gsub("explicit ", "")
  164. line = line:gsub("tolua_outside ", "")
  165. ofile:write("- " .. line .. "\n")
  166. else
  167. if firstProperty then
  168. firstProperty = false
  169. ofile:write("\nProperties:\n\n")
  170. end
  171. line = line:gsub(" %w+_ @ ", " ")
  172. line = line:gsub("tolua_property__get_set ", "")
  173. line = line:gsub("tolua_property__is_set ", "")
  174. line = line:gsub("tolua_property__has_set ", "")
  175. line = line:gsub("tolua_property__no_prefix ", "")
  176. if line:find("tolua_readonly") == nil then
  177. ofile:write("- " .. line .. "\n")
  178. else
  179. line = line:gsub("tolua_readonly ", "")
  180. ofile:write("- " .. line .. " (readonly)\n")
  181. end
  182. end
  183. end
  184. end
  185. end
  186. ofile = io.open("LuaScriptAPI.dox", "w")
  187. ofile:write([[
  188. namespace Urho3D
  189. {
  190. /**
  191. \page LuaScriptAPI Lua Scripting API
  192. ]])
  193. ofile:write("\n")
  194. writeGlobalFunctions(ofile)
  195. writeGlobalConstants(ofile)
  196. writeClasses(ofile)
  197. ofile:write([[
  198. */
  199. }
  200. ]])