error_class.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from output import *
  2. import sys
  3. error_list: dict = {}
  4. class Error(object):
  5. line: int = -1
  6. column: int = -1
  7. error_id: int = 1
  8. curr_token_list: list = []
  9. has_occur: bool = False
  10. def __init__(self, text: str, is_code_error: bool):
  11. self.text = text
  12. self.is_code_error = is_code_error
  13. self.id = Error.error_id
  14. Error.error_id += 1
  15. def call(self):
  16. Error.has_occur = True
  17. color_println('red', self.text)
  18. if self.is_code_error:
  19. color_println('red', f'line = {self.line}')
  20. for i in range(len(Error.curr_token_list)):
  21. if i == self.column:
  22. color_print('red', Error.curr_token_list[i], True)
  23. else:
  24. color_print('red', Error.curr_token_list[i])
  25. echo(' ', end='')
  26. echo()
  27. exit(self.id)
  28. def add_error(name: str, text: str, is_code_error: bool = False):
  29. error_list.update({name: Error(text, is_code_error)})
  30. def call_error(name: str):
  31. error_list[name].call()