idl.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. -- Copyright 2019 云风 https://github.com/cloudwu . All rights reserved.
  2. -- License (the same with bgfx) : https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  3. local idl = {}
  4. local all_types = {}
  5. local function typedef(_, typename)
  6. assert(all_types[typename] == nil, "Duplicate type")
  7. local t = {}
  8. all_types[typename] = t
  9. local function type_attrib(attrib)
  10. assert(type(attrib) == "table", "type attrib should be a table")
  11. for _, a in ipairs(attrib) do
  12. t[a] = true
  13. end
  14. end
  15. return function(cname)
  16. local typ = type(cname)
  17. if typ == "table" then
  18. type_attrib(cname)
  19. return
  20. end
  21. assert(typ == "string" , "type should be a string")
  22. t.cname = cname
  23. return type_attrib
  24. end
  25. end
  26. idl.typedef = setmetatable({} , { __index = typedef, __call = typedef })
  27. idl.types = all_types
  28. local all_funcs = {}
  29. local function duplicate_arg_name(name)
  30. error ("Duplicate arg name " .. name)
  31. end
  32. local function funcdef(_, funcname)
  33. local f = { name = funcname , args = {} }
  34. all_funcs[#all_funcs+1] = f
  35. local args
  36. local function args_desc(obj, args_name)
  37. obj[args_name] = duplicate_arg_name
  38. return function (fulltype)
  39. local arg = {
  40. name = "_" .. args_name,
  41. fulltype = fulltype,
  42. }
  43. f.args[#f.args+1] = arg
  44. local function arg_attrib(_, attrib )
  45. assert(type(attrib) == "table", "Arg attributes should be a table")
  46. for _, a in ipairs(attrib) do
  47. arg[a] = true
  48. end
  49. return args
  50. end
  51. return setmetatable( {} , {
  52. __index = function(_, name)
  53. return args_desc(obj, name)
  54. end
  55. , __call = arg_attrib } )
  56. end
  57. end
  58. args = setmetatable({}, { __index = args_desc })
  59. local function rettype(value)
  60. assert(type(value) == "string", "Need return type")
  61. f.ret = { fulltype = value }
  62. return args
  63. end
  64. local function funcdef(value)
  65. if type(value) == "table" then
  66. for k,v in pairs(value) do
  67. if type(k) == "number" then
  68. f[v] = true
  69. else
  70. f[k] = v
  71. end
  72. end
  73. return rettype
  74. end
  75. return rettype(value)
  76. end
  77. local function classfunc(_, methodname)
  78. f.class = f.name
  79. f.name = methodname
  80. return funcdef
  81. end
  82. return setmetatable({} , { __index = classfunc, __call = function(_, value) return funcdef(value) end })
  83. end
  84. idl.func = setmetatable({}, { __index = funcdef })
  85. idl.funcs = all_funcs
  86. idl.handle = "handle"
  87. idl.enum = "enum"
  88. idl.out = "out"
  89. idl.const = "const"
  90. return setmetatable(idl , { __index = function (_, keyword)
  91. error (tostring(keyword) .. " is invalid")
  92. end})