Coverage for lobster/errors.py: 71%
38 statements
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-06 09:51 +0000
« prev ^ index » next coverage.py v7.10.2, created at 2025-08-06 09:51 +0000
1#!/usr/bin/env python3
2#
3# LOBSTER - Lightweight Open BMW Software Traceability Evidence Report
4# Copyright (C) 2022-2023, 2025 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/>.
20from lobster.exceptions import LOBSTER_Exception
21from lobster.location import Location
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
32 def dump(self):
33 print(self.location.to_string() + ": " + self.message)
36class Message_Handler:
37 def __init__(self):
38 self.warnings = 0
39 self.errors = 0
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)
46 if severity == "warning":
47 self.warnings += 1
48 else:
49 self.errors += 1
51 print(f"{location.to_string()}: lobster {severity}: {message}")
53 def lex_error(self, location, message):
54 assert isinstance(location, Location)
55 assert isinstance(message, str)
57 self.emit(location, "lex error", message)
58 raise LOBSTER_Error(location, message)
60 def error(self, location, message, fatal=True):
61 assert isinstance(location, Location)
62 assert isinstance(message, str)
64 self.emit(location, "error", message)
65 if fatal: 65 ↛ 66line 65 didn't jump to line 66 because the condition on line 65 was never true
66 raise LOBSTER_Error(location, message)
68 def warning(self, location, message):
69 assert isinstance(location, Location)
70 assert isinstance(message, str)
72 self.emit(location, "warning", message)