source_map.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """Defines source maps: dictionaries that map lines in generated source to debug information."""
  2. class SourceMap(object):
  3. """A source map, which converts generated source lines to debug information."""
  4. def __init__(self):
  5. self.lines = {}
  6. def map_line(self, line_number, debug_info):
  7. """Assigns the given debug information to the given line."""
  8. self.lines[line_number] = debug_info
  9. def get_debug_info(self, line_number):
  10. """Gets the debug information for the given line number, or None if no debug info was
  11. found."""
  12. if line_number in self.lines:
  13. return self.lines[line_number]
  14. else:
  15. return None
  16. def __str__(self):
  17. return '\n'.join(
  18. ['%d: %s' % pair
  19. for pair in sorted(self.lines.items(), key=lambda (key, _): key)])
  20. class SourceMapBuilder(object):
  21. """A type of object that makes it easy to build source maps for hierarchical instructions."""
  22. def __init__(self, initial_line_number=0):
  23. self.source_map = SourceMap()
  24. self.debug_info_stack = []
  25. self.line_number = initial_line_number - 1
  26. def push_debug_info(self, debug_info):
  27. """Informs the source map that subsequent lines of code will have the given debug
  28. information associated with them."""
  29. self.debug_info_stack.append(debug_info)
  30. def pop_debug_info(self):
  31. """Informs the source map that the debug information that was pushed last should
  32. be discarded in favor of the debug information that was pushed onto the stack
  33. just prior to it."""
  34. return self.debug_info_stack.pop()
  35. def append_line(self):
  36. """Has the source map builder increment its line number counter, and assigns the debug
  37. information that is at the top of the debug information stack to that line."""
  38. self.line_number += 1
  39. if len(self.debug_info_stack) > 0:
  40. self.source_map.map_line(self.line_number, self.debug_info_stack[-1])
  41. def __str__(self):
  42. return str(self.source_map)
  43. class ManualSourceMap(object):
  44. """A source map whose debug information can be set and reset. Line numbers are ignored."""
  45. def __init__(self):
  46. self.debug_information = None
  47. def get_debug_info(self, _):
  48. """Gets the debug information for the given line number, or None if no debug info was
  49. found."""
  50. return self.debug_information
  51. def __str__(self):
  52. return str(self.debug_information)