code_generator.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import re
  2. import os
  3. import ymake
  4. pattern = re.compile(r"#include\s*[<\"](?P<INDUCED>[^>\"]+)[>\"]|(?:@|{@)\s*(?:import|include|from)\s*[\"'](?P<INCLUDE>[^\"']+)[\"']")
  5. class CodeGeneratorTemplateParser(object):
  6. def __init__(self, path, unit):
  7. self._path = path
  8. retargeted = os.path.join(unit.path(), os.path.relpath(path, unit.resolve(unit.path())))
  9. with open(path, 'rb') as f:
  10. includes, induced = CodeGeneratorTemplateParser.parse_includes(f.readlines())
  11. self._includes = unit.resolve_include([retargeted] + includes) if includes else []
  12. self._induced = unit.resolve_include([retargeted] + induced) if induced else []
  13. @staticmethod
  14. def parse_includes(lines):
  15. includes = []
  16. induced = []
  17. for line in lines:
  18. for match in pattern.finditer(line):
  19. type = match.lastgroup
  20. if type == 'INCLUDE':
  21. includes.append(match.group(type))
  22. elif type == 'INDUCED':
  23. induced.append(match.group(type))
  24. else:
  25. raise Exception("Unexpected match! Perhaps it is a result of an error in pattern.")
  26. return (includes, induced)
  27. def includes(self):
  28. return self._includes
  29. def induced_deps(self):
  30. return {
  31. 'h+cpp': self._induced
  32. }
  33. def init():
  34. ymake.addparser('markettemplate', CodeGeneratorTemplateParser)
  35. ymake.addparser('macro', CodeGeneratorTemplateParser)