code_generator.py 1.5 KB

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