PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/tools/tolua++-1.0.93/src/bin/lua/basic.lua

https://bitbucket.org/dbindel/axfem
Lua | 414 lines | 277 code | 74 blank | 63 comment | 46 complexity | 5de8c48a23d6deec1b4eacd56e5b3be9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. -- tolua: basic utility functions
  2. -- Written by Waldemar Celes
  3. -- TeCGraf/PUC-Rio
  4. -- Jul 1998
  5. -- Last update: Apr 2003
  6. -- $Id: $
  7. -- This code is free software; you can redistribute it and/or modify it.
  8. -- The software provided hereunder is on an "as is" basis, and
  9. -- the author has no obligation to provide maintenance, support, updates,
  10. -- enhancements, or modifications.
  11. -- Basic C types and their corresponding Lua types
  12. -- All occurrences of "char*" will be replaced by "_cstring",
  13. -- and all occurrences of "void*" will be replaced by "_userdata"
  14. _basic = {
  15. ['void'] = '',
  16. ['char'] = 'number',
  17. ['int'] = 'number',
  18. ['short'] = 'number',
  19. ['long'] = 'number',
  20. ['unsigned'] = 'number',
  21. ['float'] = 'number',
  22. ['double'] = 'number',
  23. ['_cstring'] = 'string',
  24. ['_userdata'] = 'userdata',
  25. ['char*'] = 'string',
  26. ['void*'] = 'userdata',
  27. ['bool'] = 'boolean',
  28. ['lua_Object'] = 'value',
  29. ['LUA_VALUE'] = 'value', -- for compatibility with tolua 4.0
  30. ['lua_State*'] = 'state',
  31. ['_lstate'] = 'state',
  32. ['lua_Function'] = 'value',
  33. }
  34. _basic_ctype = {
  35. number = "lua_Number",
  36. string = "const char*",
  37. userdata = "void*",
  38. boolean = "bool",
  39. value = "int",
  40. state = "lua_State*",
  41. }
  42. -- functions the are used to do a 'raw push' of basic types
  43. _basic_raw_push = {}
  44. -- List of user defined types
  45. -- Each type corresponds to a variable name that stores its tag value.
  46. _usertype = {}
  47. -- List of types that have to be collected
  48. _collect = {}
  49. -- List of types
  50. _global_types = {n=0}
  51. _global_types_hash = {}
  52. -- list of classes
  53. _global_classes = {}
  54. -- List of enum constants
  55. _global_enums = {}
  56. -- List of auto renaming
  57. _renaming = {}
  58. function appendrenaming (s)
  59. local b,e,old,new = strfind(s,"%s*(.-)%s*@%s*(.-)%s*$")
  60. if not b then
  61. error("#Invalid renaming syntax; it should be of the form: pattern@pattern")
  62. end
  63. tinsert(_renaming,{old=old, new=new})
  64. end
  65. function applyrenaming (s)
  66. for i=1,getn(_renaming) do
  67. local m,n = gsub(s,_renaming[i].old,_renaming[i].new)
  68. if n ~= 0 then
  69. return m
  70. end
  71. end
  72. return nil
  73. end
  74. -- Error handler
  75. function tolua_error (s,f)
  76. if _curr_code then
  77. print("***curr code for error is "..tostring(_curr_code))
  78. print(debug.traceback())
  79. end
  80. local out = _OUTPUT
  81. _OUTPUT = _STDERR
  82. if strsub(s,1,1) == '#' then
  83. write("\n** tolua: "..strsub(s,2)..".\n\n")
  84. if _curr_code then
  85. local _,_,s = strfind(_curr_code,"^%s*(.-\n)") -- extract first line
  86. if s==nil then s = _curr_code end
  87. s = gsub(s,"_userdata","void*") -- return with 'void*'
  88. s = gsub(s,"_cstring","char*") -- return with 'char*'
  89. s = gsub(s,"_lstate","lua_State*") -- return with 'lua_State*'
  90. write("Code being processed:\n"..s.."\n")
  91. end
  92. else
  93. if not f then f = "(f is nil)" end
  94. print("\n** tolua internal error: "..f..s..".\n\n")
  95. return
  96. end
  97. _OUTPUT = out
  98. end
  99. function warning (msg)
  100. if flags.q then return end
  101. local out = _OUTPUT
  102. _OUTPUT = _STDERR
  103. write("\n** tolua warning: "..msg..".\n\n")
  104. _OUTPUT = out
  105. end
  106. -- register an user defined type: returns full type
  107. function regtype (t)
  108. --if isbasic(t) then
  109. -- return t
  110. --end
  111. local ft = findtype(t)
  112. if not _usertype[ft] then
  113. return appendusertype(t)
  114. end
  115. return ft
  116. end
  117. -- return type name: returns full type
  118. function typevar(type)
  119. if type == '' or type == 'void' then
  120. return type
  121. else
  122. local ft = findtype(type)
  123. if ft then
  124. return ft
  125. end
  126. _usertype[type] = type
  127. return type
  128. end
  129. end
  130. -- check if basic type
  131. function isbasic (type)
  132. local t = gsub(type,'const ','')
  133. local m,t = applytypedef('', t)
  134. local b = _basic[t]
  135. if b then
  136. return b,_basic_ctype[b]
  137. end
  138. return nil
  139. end
  140. -- split string using a token
  141. function split (s,t)
  142. local l = {n=0}
  143. local f = function (s)
  144. l.n = l.n + 1
  145. l[l.n] = s
  146. return ""
  147. end
  148. local p = "%s*(.-)%s*"..t.."%s*"
  149. s = gsub(s,"^%s+","")
  150. s = gsub(s,"%s+$","")
  151. s = gsub(s,p,f)
  152. l.n = l.n + 1
  153. l[l.n] = gsub(s,"(%s%s*)$","")
  154. return l
  155. end
  156. -- splits a string using a pattern, considering the spacial cases of C code (templates, function parameters, etc)
  157. -- pattern can't contain the '^' (as used to identify the begining of the line)
  158. -- also strips whitespace
  159. function split_c_tokens(s, pat)
  160. s = string.gsub(s, "^%s*", "")
  161. s = string.gsub(s, "%s*$", "")
  162. local token_begin = 1
  163. local token_end = 1
  164. local ofs = 1
  165. local ret = {n=0}
  166. function add_token(ofs)
  167. local t = string.sub(s, token_begin, ofs)
  168. t = string.gsub(t, "^%s*", "")
  169. t = string.gsub(t, "%s*$", "")
  170. ret.n = ret.n + 1
  171. ret[ret.n] = t
  172. end
  173. while ofs <= string.len(s) do
  174. local sub = string.sub(s, ofs, -1)
  175. local b,e = string.find(sub, "^"..pat)
  176. if b then
  177. add_token(ofs-1)
  178. ofs = ofs+e
  179. token_begin = ofs
  180. else
  181. local char = string.sub(s, ofs, ofs)
  182. if char == "(" or char == "<" then
  183. local block
  184. if char == "(" then block = "^%b()" end
  185. if char == "<" then block = "^%b<>" end
  186. b,e = string.find(sub, block)
  187. if not b then
  188. -- unterminated block?
  189. ofs = ofs+1
  190. else
  191. ofs = ofs + e
  192. end
  193. else
  194. ofs = ofs+1
  195. end
  196. end
  197. end
  198. add_token(ofs)
  199. --if ret.n == 0 then
  200. -- ret.n=1
  201. -- ret[1] = ""
  202. --end
  203. return ret
  204. end
  205. -- concatenate strings of a table
  206. function concat (t,f,l,jstr)
  207. jstr = jstr or " "
  208. local s = ''
  209. local i=f
  210. while i<=l do
  211. s = s..t[i]
  212. i = i+1
  213. if i <= l then s = s..jstr end
  214. end
  215. return s
  216. end
  217. -- concatenate all parameters, following output rules
  218. function concatparam (line, ...)
  219. local i=1
  220. while i<=arg.n do
  221. if _cont and not strfind(_cont,'[%(,"]') and
  222. strfind(arg[i],"^[%a_~]") then
  223. line = line .. ' '
  224. end
  225. line = line .. arg[i]
  226. if arg[i] ~= '' then
  227. _cont = strsub(arg[i],-1,-1)
  228. end
  229. i = i+1
  230. end
  231. if strfind(arg[arg.n],"[%/%)%;%{%}]$") then
  232. _cont=nil line = line .. '\n'
  233. end
  234. return line
  235. end
  236. -- output line
  237. function output (...)
  238. local i=1
  239. while i<=arg.n do
  240. if _cont and not strfind(_cont,'[%(,"]') and
  241. strfind(arg[i],"^[%a_~]") then
  242. write(' ')
  243. end
  244. write(arg[i])
  245. if arg[i] ~= '' then
  246. _cont = strsub(arg[i],-1,-1)
  247. end
  248. i = i+1
  249. end
  250. if strfind(arg[arg.n],"[%/%)%;%{%}]$") then
  251. _cont=nil write('\n')
  252. end
  253. end
  254. function get_property_methods(ptype, name)
  255. if get_property_methods_hook and get_property_methods_hook(ptype,name) then
  256. return get_property_methods_hook(ptype, name)
  257. end
  258. if ptype == "default" then -- get_name, set_name
  259. return "get_"..name, "set_"..name
  260. end
  261. if ptype == "qt" then -- name, setName
  262. return name, "set"..string.upper(string.sub(name, 1, 1))..string.sub(name, 2, -1)
  263. end
  264. if ptype == "overload" then -- name, name
  265. return name,name
  266. end
  267. return nil
  268. end
  269. -------------- the hooks
  270. -- called right after processing the $[ichl]file directives,
  271. -- right before processing anything else
  272. -- takes the package object as the parameter
  273. function preprocess_hook(p)
  274. -- p.code has all the input code from the pkg
  275. end
  276. -- called for every $ifile directive
  277. -- takes a table with a string called 'code' inside, the filename, and any extra arguments
  278. -- passed to $ifile. no return value
  279. function include_file_hook(t, filename, ...)
  280. end
  281. -- called after processing anything that's not code (like '$renaming', comments, etc)
  282. -- and right before parsing the actual code.
  283. -- takes the Package object with all the code on the 'code' key. no return value
  284. function preparse_hook(package)
  285. end
  286. -- called before starting output
  287. function pre_output_hook(package)
  288. end
  289. -- called after writing all the output.
  290. -- takes the Package object
  291. function post_output_hook(package)
  292. end
  293. -- called from 'get_property_methods' to get the methods to retrieve a property
  294. -- according to its type
  295. function get_property_methods_hook(property_type, name)
  296. end
  297. -- called from ClassContainer:doparse with the string being parsed
  298. -- return nil, or a substring
  299. function parser_hook(s)
  300. return nil
  301. end
  302. -- called from classFunction:supcode, before the call to the function is output
  303. function pre_call_hook(f)
  304. end
  305. -- called from classFunction:supcode, after the call to the function is output
  306. function post_call_hook(f)
  307. end
  308. -- called before the register code is output
  309. function pre_register_hook(package)
  310. end
  311. -- called to output an error message
  312. function output_error_hook(...)
  313. return string.format(...)
  314. end
  315. -- custom pushers
  316. _push_functions = {}
  317. _is_functions = {}
  318. _to_functions = {}
  319. _base_push_functions = {}
  320. _base_is_functions = {}
  321. _base_to_functions = {}
  322. local function search_base(t, funcs)
  323. local class = _global_classes[t]
  324. while class do
  325. if funcs[class.type] then
  326. return funcs[class.type]
  327. end
  328. class = _global_classes[class.btype]
  329. end
  330. return nil
  331. end
  332. function get_push_function(t)
  333. return _push_functions[t] or search_base(t, _base_push_functions) or "tolua_pushusertype"
  334. end
  335. function get_to_function(t)
  336. return _to_functions[t] or search_base(t, _base_to_functions) or "tolua_tousertype"
  337. end
  338. function get_is_function(t)
  339. return _is_functions[t] or search_base(t, _base_is_functions) or "tolua_isusertype"
  340. end