Coverage for lobster/errors.py: 71%

38 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-26 14:55 +0000

1#!/usr/bin/env python3 

2# 

3# LOBSTER - Lightweight Open BMW Software Traceability Evidence Report 

4# Copyright (C) 2022-2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) 

5# 

6# This program is free software: you can redistribute it and/or modify 

7# it under the terms of the GNU Affero General Public License as 

8# published by the Free Software Foundation, either version 3 of the 

9# License, or (at your option) any later version. 

10# 

11# This program is distributed in the hope that it will be useful, but 

12# WITHOUT ANY WARRANTY; without even the implied warranty of 

13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

14# Affero General Public License for more details. 

15# 

16# You should have received a copy of the GNU Affero General Public 

17# License along with this program. If not, see 

18# <https://www.gnu.org/licenses/>. 

19 

20from lobster.exceptions import LOBSTER_Exception 

21from lobster.location import Location 

22 

23 

24class LOBSTER_Error(LOBSTER_Exception): 

25 def __init__(self, location, message): 

26 super().__init__(message) 

27 assert isinstance(location, Location) 

28 assert isinstance(message, str) 

29 self.location = location 

30 self.message = message 

31 

32 def dump(self): 

33 print(self.location.to_string() + ": " + self.message) 

34 

35 

36class Message_Handler: 

37 def __init__(self): 

38 self.warnings = 0 

39 self.errors = 0 

40 

41 def emit(self, location, severity, message): 

42 assert isinstance(location, Location) 

43 assert severity in ("warning", "lex error", "error") 

44 assert isinstance(message, str) 

45 

46 if severity == "warning": 

47 self.warnings += 1 

48 else: 

49 self.errors += 1 

50 

51 print("%s: lobster %s: %s" % (location.to_string(), 

52 severity, 

53 message)) 

54 

55 def lex_error(self, location, message): 

56 assert isinstance(location, Location) 

57 assert isinstance(message, str) 

58 

59 self.emit(location, "lex error", message) 

60 raise LOBSTER_Error(location, message) 

61 

62 def error(self, location, message, fatal=True): 

63 assert isinstance(location, Location) 

64 assert isinstance(message, str) 

65 

66 self.emit(location, "error", message) 

67 if fatal: 67 ↛ 68line 67 didn't jump to line 68 because the condition on line 67 was never true

68 raise LOBSTER_Error(location, message) 

69 

70 def warning(self, location, message): 

71 assert isinstance(location, Location) 

72 assert isinstance(message, str) 

73 

74 self.emit(location, "warning", message)