meta_gen.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!python
  2. import re
  3. import sys
  4. def eprint(*args, **kwargs):
  5. print(*args, file=sys.stderr, **kwargs)
  6. def readfile(filename):
  7. file = open(filename)
  8. content = file.read()
  9. return content
  10. def replace_all(template, **kwargs):
  11. for k, v in kwargs.items():
  12. template = template.replace("@@" + k + "@@", v)
  13. return template
  14. def meta_gen(content):
  15. namespace_pattern = re.compile(r"namespace(.*){")
  16. results = namespace_pattern.findall(content)
  17. assert(len(results) == 1)
  18. namespace = results[0].strip()
  19. struct_pattern = re.compile(r"struct (.*?){((.|\n)*?)^};", re.MULTILINE)
  20. results = struct_pattern.findall(content)
  21. body_pattern = re.compile(r"accept\((.*)Visitor ?& ?\) (.*?);")
  22. # print(results)
  23. # print(len(results[0]))
  24. root_base = None
  25. override_structs = []
  26. for (title, body, _) in results:
  27. pack = title.replace(' ', '').split(':')
  28. if len(pack) == 1:
  29. pack.append(None)
  30. body_res = body_pattern.findall(body)
  31. if len(body_res) != 1:
  32. continue
  33. eprint(struct_name)
  34. eprint(body_res)
  35. eprint(body)
  36. assert False
  37. struct_name, base_name = pack
  38. if not base_name:
  39. root_base = struct_name
  40. visitor_name, state = body_res[0]
  41. assert(visitor_name == root_base)
  42. if state.strip() == 'override':
  43. override_structs.append(struct_name)
  44. # print(body_res)
  45. return namespace, root_base, override_structs
  46. if __name__ == "__main__":
  47. assert(len(sys.argv) == 2)
  48. file = open(sys.argv[1])
  49. content = file.read()
  50. namespace, root_base, override_structs = meta_gen(content)
  51. eprint(namespace)
  52. eprint(root_base)
  53. eprint(override_structs)