Coverage for lobster/errors.py: 88%
38 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-26 14:55 +0000
« 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/>.
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("%s: lobster %s: %s" % (location.to_string(),
52 severity,
53 message))
55 def lex_error(self, location, message):
56 assert isinstance(location, Location)
57 assert isinstance(message, str)
59 self.emit(location, "lex error", message)
60 raise LOBSTER_Error(location, message)
62 def error(self, location, message, fatal=True):
63 assert isinstance(location, Location)
64 assert isinstance(message, str)
66 self.emit(location, "error", message)
67 if fatal:
68 raise LOBSTER_Error(location, message)
70 def warning(self, location, message):
71 assert isinstance(location, Location)
72 assert isinstance(message, str)
74 self.emit(location, "warning", message)