Coverage for lobster/tools/trlc/errors.py: 100%

28 statements  

« prev     ^ index     » next       coverage.py v7.10.5, created at 2025-08-27 13:02 +0000

1from typing import Iterable 

2from trlc import ast 

3 

4 

5class TrlcFailure(Exception): 

6 """Exception if a TRLC API function indicated an error through its return value. 

7 In that case the TRLC message handler should have printed details to stdout, but 

8 these details are not provided through the API return value. 

9 """ 

10 

11 

12class InvalidConversionRuleError(Exception): 

13 """Exception raised when a conversion rule is invalid, e.g., if it does not match 

14 any record type in the TRLC symbol table. 

15 """ 

16 

17 

18# The errors below are usually raised when TRLC data could not be converted to a 

19# LOBSTER item 

20 

21 

22class RecordObjectComponentError(Exception): 

23 """Exception raised when a required component is missing in a RecordObject.""" 

24 

25 def __init__(self, component_name: str, record_object: ast.Record_Object): 

26 super().__init__( 

27 f"Component '{component_name}' not found in TRLC Record_Object " 

28 f"'{record_object.fully_qualified_name()}'!" 

29 ) 

30 self.component_name = component_name 

31 self.record_object = record_object 

32 

33 

34class TupleError(Exception): 

35 """Exception raised for errors related to tuple aggregates.""" 

36 

37 def __init__(self, tuple_aggregate: ast.Tuple_Aggregate, message: str): 

38 super().__init__(message) 

39 self.tuple_aggregate = tuple_aggregate 

40 

41 @staticmethod 

42 def tuple_details(tuple_aggregate: ast.Tuple_Aggregate) -> str: 

43 return f"tuple '{tuple_aggregate.typ.name}' " \ 

44 f"with value '{tuple_aggregate.to_python_object()}', " \ 

45 f"used at {tuple_aggregate.location.to_string()}, " \ 

46 f"defined at {tuple_aggregate.typ.location.to_string()}" 

47 

48 

49class TupleComponentError(TupleError): 

50 """Exception raised when a to-string instruction cannot be applied to a tuple 

51 aggregate because a required component is missing. 

52 

53 This can either be the case if the corresponding Tuple_Type does not have 

54 the required component at all, or if the component is not present (because) 

55 it is optional). 

56 """ 

57 

58 def __init__(self, component_name: str, tuple_aggregate: ast.Tuple_Aggregate): 

59 super().__init__( 

60 tuple_aggregate, 

61 f"Required tuple component " 

62 f"'{component_name}' is missing for " 

63 f"{self.tuple_details(tuple_aggregate)}!", 

64 ) 

65 self.component_name = component_name 

66 

67 

68class TupleToStringMissingError(TupleError): 

69 """Exception raised when a to-string function is missing for a tuple aggregate.""" 

70 

71 def __init__(self, tuple_aggregate: ast.Tuple_Aggregate): 

72 super().__init__( 

73 tuple_aggregate, 

74 f"No 'to-string' function defined for " 

75 f"{self.tuple_details(tuple_aggregate)}!" 

76 ) 

77 

78 

79class TupleToStringFailedError(TupleError): 

80 """Exception raised when all to-string conversion instructions failed to be 

81 applied to a tuple.""" 

82 

83 def __init__( 

84 self, 

85 tuple_aggregate: ast.Tuple_Aggregate, 

86 earlier_errors: Iterable[Exception], 

87 ): 

88 earlier_errors_text = '\n'.join(str(e) for e in earlier_errors) 

89 super().__init__( 

90 tuple_aggregate, 

91 f"All 'to_string' conversion functions failed for " 

92 f"{self.tuple_details(tuple_aggregate)}!\n" 

93 f"The following errors occurred when applying the given functions:\n" 

94 f"{earlier_errors_text}" 

95 ) 

96 self.earlier_errors = earlier_errors