toolbox.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. local truckboris = require('truckboris')
  2. local lfs = require('lfs')
  3. local posix = require('posix')
  4. local function exec_out(cmd)
  5. local handle = io.popen(cmd)
  6. local result = string.gsub(handle:read("*a"), "\n", "")
  7. handle:close()
  8. return result
  9. end
  10. local function isdir(fn)
  11. return (posix.stat(fn, "type") == 'directory')
  12. end
  13. ---Split string in different parts which are returned in a table. The delimiter of each part is a pattern given in argument.
  14. --@param str the string to split
  15. --@param pat the pattern delimiter
  16. local function split(str, pat)
  17. local t = {} -- NOTE: use {n = 0} in Lua-5.0
  18. local fpat = "(.-)" .. pat
  19. local last_end = 1
  20. local s, e, cap = string.find(str,fpat, 1)
  21. while s do
  22. if s ~= 1 or cap ~= "" then
  23. table.insert(t,cap)
  24. end
  25. last_end = e+1
  26. s, e, cap = string.find(str,fpat, last_end)
  27. end
  28. if last_end <= #str then
  29. cap = string.sub(str,last_end)
  30. table.insert(t, cap)
  31. end
  32. return t
  33. end
  34. local function to_snake_case(a_string)
  35. if not a_string:match("[A-Z]") or not a_string:match("[a-z]") then
  36. return a_string
  37. else
  38. s = a_string:gsub("([a-z]+)([A-Z][a-z]%a*)","%1_%2")
  39. s = a_string:gsub("([A-Z]+)([A-Z][a-z]%a*)","%1_%2")
  40. s = s:gsub("([a-z]+)([A-Z]+)","%1_%2")
  41. return s
  42. end
  43. end
  44. local function to_screaming_snake_case(a_string)
  45. return to_snake_case(a_string):upper()
  46. end
  47. local Clangparser = {}
  48. function Clangparser:new (path)
  49. if not isdir(path) then
  50. return nil
  51. end
  52. local p = {}
  53. p.path = path
  54. p.source = p.path .. '/Index.h'
  55. local gcc_lib_base = '/usr/lib/gcc/' .. exec_out('llvm-config --host-target')
  56. local last = ''
  57. for dir in lfs.dir(gcc_lib_base) do
  58. if not dir:match('^..?$') then
  59. if dir > last then
  60. last = dir
  61. end
  62. end
  63. end
  64. p.headers_paths = {}
  65. p.headers_paths[1] = gcc_lib_base .. '/' .. last .. '/' .. 'include'
  66. p.headers_paths[2] = '/usr/include'
  67. p.headers_paths[3] = p.path
  68. setmetatable(p, self)
  69. self.__index = self
  70. return p
  71. end
  72. function Clangparser:parse(main_file)
  73. local main_file = main_file or false
  74. parser = truckboris.HeaderParser()
  75. parser:add_source_file(self.source)
  76. for _,h in ipairs(self.headers_paths) do
  77. parser:add_search_path(h)
  78. end
  79. self.parser = parser
  80. return parser:parse(main_file)
  81. end
  82. return {
  83. isdir = isdir,
  84. exec_out = exec_out,
  85. Clangparser = Clangparser,
  86. split = split,
  87. to_snake_case = to_snake_case,
  88. to_screaming_snake_case = to_screaming_snake_case,
  89. lfs = lfs,
  90. posix = posix,
  91. truckboris =truckboris,
  92. }