source_map.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. return self.lines[line_number]
  13. class SourceMapBuilder(object):
  14. """A type of object that makes it easy to build source maps for hierarchical instructions."""
  15. def __init__(self, initial_line_number=0):
  16. self.source_map = SourceMap()
  17. self.debug_info_stack = []
  18. self.line_number = initial_line_number - 1
  19. def push_debug_info(self, debug_info):
  20. """Informs the source map that subsequent lines of code will have the given debug
  21. information associated with them."""
  22. self.debug_info_stack.append(debug_info)
  23. def pop_debug_info(self):
  24. """Informs the source map that the debug information that was pushed last should
  25. be discarded in favor of the debug information that was pushed onto the stack
  26. just prior to it."""
  27. return self.debug_info_stack.pop()
  28. def append_line(self):
  29. """Has the source map builder increment its line number counter, and assigns the debug
  30. information that is at the top of the debug information stack to that line."""
  31. self.line_number += 1
  32. if len(self.debug_info_stack) > 0:
  33. self.source_map.map_line(self.line_number, self.debug_info_stack[-1])