enums.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env lua
  2. local toolbox = require('toolbox')
  3. local gcc_lib_base = '/usr/lib/gcc/' .. toolbox.exec_out('llvm-config --host-target')
  4. local last = ''
  5. for dir in toolbox.lfs.dir(gcc_lib_base) do
  6. if not dir:match('^..?$') then
  7. if dir > last then
  8. last = dir
  9. end
  10. end
  11. end
  12. headers_paths = {}
  13. headers_paths[1] = gcc_lib_base .. '/' .. last .. '/' .. 'include'
  14. headers_paths[2] = '/usr/include'
  15. headers_paths[3] = '/usr/include/clang-c'
  16. clangc_source = '/usr/include/clang-c/Index.h'
  17. local parser = toolbox.truckboris.HeaderParser()
  18. parser:add_source_file(clangc_source)
  19. for _,h in ipairs(headers_paths) do
  20. parser:add_search_path(h)
  21. end
  22. has_worked = parser:parse()
  23. if has_worked == true then
  24. print("Parsing has worked")
  25. end
  26. local enums = {}
  27. for i = 1, parser:enums_num(), 1 do
  28. enums[i] = parser:get_nth_enum(i - 1)
  29. end
  30. local function rename_module_table(enum)
  31. local name = enum:name() == "" and enum:typedef_name() or enum:name()
  32. return name:gsub('^CX_?','')
  33. end
  34. local function rename_enum_constant(constant_name)
  35. local name = constant_name:gsub("^([^_]+_)(.*)$","%2")
  36. return toolbox.to_screaming_snake_case(name)
  37. end
  38. local function create_module_constant(enum)
  39. local name = rename_module_table(enum)
  40. print('-->' .. name)
  41. for i = 1, enum:constants_num(), 1 do
  42. local enum_constant_name = enum:get_nth_enum_constant(i - 1):name()
  43. print('\t-' .. rename_enum_constant(enum_constant_name) .. ' -- ' .. enum_constant_name)
  44. end
  45. end
  46. for _,e in ipairs(enums) do
  47. create_module_constant(e)
  48. end