Coverage for lobster/tools/trlc/converter.py: 95%

87 statements  

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

1from typing import Iterable, List, Optional 

2from trlc import ast 

3 

4from lobster.common.items import Item, Requirement, Tracing_Tag 

5from lobster.common.location import File_Reference 

6from lobster.tools.trlc.conversion_rule import ConversionRule 

7from lobster.tools.trlc.conversion_rule_lookup import ( 

8 build_record_type_to_conversion_rule_lookup, 

9 get_record_types, 

10) 

11from lobster.tools.trlc.errors import ( 

12 InvalidConversionRuleError, 

13 TupleComponentError, 

14 TupleToStringFailedError, 

15 TupleToStringMissingError, 

16) 

17from lobster.tools.trlc.hierarchy_tree import build_children_lookup 

18from lobster.tools.trlc.item_wrapper import ItemWrapper 

19from lobster.tools.trlc.text_generation import build_text_from_instructions 

20from lobster.tools.trlc.to_string_rules import ( 

21 ToStringRules, build_tuple_type_to_ruleset_map, 

22) 

23 

24 

25class Converter: 

26 def __init__( 

27 self, 

28 conversion_rules: Iterable[ConversionRule], 

29 to_string_rules: Iterable[ToStringRules], 

30 symbol_table: ast.Symbol_Table, 

31 ) -> None: 

32 self._conversion_rule_lookup = build_record_type_to_conversion_rule_lookup( 

33 conversion_rules=conversion_rules, 

34 children_lookup=build_children_lookup(symbol_table), 

35 symbol_table=symbol_table, 

36 ) 

37 # check if any rule is left-over and could not be allocated to a record type 

38 orphan_rules = set(conversion_rules) - \ 

39 set(self._conversion_rule_lookup.values()) 

40 

41 if orphan_rules: 

42 raise self._build_orphan_rules_exception(symbol_table, orphan_rules) 

43 self._to_string_rules = build_tuple_type_to_ruleset_map( 

44 symbol_table=symbol_table, 

45 to_string_rule_sets=to_string_rules, 

46 ) 

47 

48 def _build_orphan_rules_exception( 

49 self, 

50 symbol_table: ast.Symbol_Table, 

51 orphan_rules: Iterable[ConversionRule], 

52 ) -> InvalidConversionRuleError: 

53 orphan_rule_names = ", ".join( 

54 f"{rule.package_name}.{rule.type_name}" for rule in orphan_rules 

55 ) 

56 available_type_names = ', '.join( 

57 record_type.fully_qualified_name() 

58 for record_type in get_record_types(symbol_table) 

59 ) 

60 if available_type_names: 

61 available_types_message = ( 

62 f"Detected record types are '{available_type_names}'." 

63 ) 

64 else: 

65 available_types_message = ( 

66 "The TRLC symbol table contains no record types at all." 

67 ) 

68 if self._conversion_rule_lookup: 

69 successfully_mapped_types = ', '.join( 

70 f"{rule.package_name}.{rule.type_name}" 

71 for rule in set(self._conversion_rule_lookup.values()) 

72 ) 

73 else: 

74 successfully_mapped_types = "none" 

75 

76 return InvalidConversionRuleError( 

77 f"The following conversion rules do not match any record type in " 

78 f"the TRLC symbol table: {orphan_rule_names}. {available_types_message} " 

79 f"The following conversion rules were successfully mapped to TRLC types: " 

80 f"{successfully_mapped_types}." 

81 ) 

82 

83 def generate_lobster_object(self, n_obj: ast.Record_Object) -> Optional[Item]: 

84 rule = self._conversion_rule_lookup.get(n_obj.n_typ) 

85 if not rule: 

86 return None 

87 

88 item_tag = Tracing_Tag( 

89 namespace=rule.lobster_namespace, 

90 tag=n_obj.fully_qualified_name(), 

91 version=None, 

92 ) 

93 

94 item_loc = File_Reference( 

95 filename=n_obj.location.file_name, 

96 line=n_obj.location.line_no, 

97 column=n_obj.location.col_no 

98 ) 

99 

100 item_wrapper = ItemWrapper(n_obj) 

101 item_text = self._get_description(item_wrapper, rule.description_fields) 

102 

103 if rule.lobster_namespace != "req": 103 ↛ 104line 103 didn't jump to line 104 because the condition on line 103 was never true

104 raise NotImplementedError( 

105 f"Conversion for namespace '{rule.lobster_namespace}' not implemented." 

106 ) 

107 rv = Requirement( 

108 tag=item_tag, 

109 location=item_loc, 

110 framework="TRLC", 

111 kind=n_obj.n_typ.name, 

112 name=n_obj.fully_qualified_name(), 

113 text=item_text 

114 ) 

115 

116 for tag_entry in rule.tags: 

117 for field_str_value in self._generate_text(item_wrapper, tag_entry.field): 

118 tag = Tracing_Tag.from_text(tag_entry.namespace, field_str_value) 

119 rv.add_tracing_target(tag) 

120 for value_list, fields in ( 

121 (rv.just_up, rule.justification_up_fields), 

122 (rv.just_down, rule.justification_down_fields), 

123 (rv.just_global, rule.justification_global_fields), 

124 ): 

125 for just_field in fields: 125 ↛ 126line 125 didn't jump to line 126 because the loop on line 125 never started

126 value_list.extend(self._generate_text(item_wrapper, just_field)) 

127 return rv 

128 

129 def _generate_text(self, item_wrapper: ItemWrapper, field_name: str) -> List[str]: 

130 """Generates a list of texts for the values in the given field 

131 

132 The function always returns a list of strings, even if the field is a 

133 single-value field. 

134 """ 

135 field_value = item_wrapper.get_field(field_name) 

136 if field_value is None: 

137 return [] 

138 raw_field = item_wrapper.get_field_raw(field_name) 

139 if isinstance(raw_field.typ, ast.Array_Type): 

140 texts = [] 

141 for element in raw_field.value: 

142 if isinstance(element, ast.Tuple_Aggregate): 142 ↛ 143line 142 didn't jump to line 143 because the condition on line 142 was never true

143 texts.append(self._tuple_value_as_string(element)) 

144 elif isinstance(element, ast.Record_Reference): 

145 texts.append(element.target.fully_qualified_name()) 

146 else: 

147 texts.append(str(element.to_python_object())) 

148 return texts 

149 elif isinstance(raw_field, ast.Tuple_Aggregate): 

150 return [self._tuple_value_as_string(raw_field)] 

151 return [str(field_value)] 

152 

153 def _tuple_value_as_string(self, tuple_aggregate: ast.Tuple_Aggregate): 

154 to_string_rules = self._to_string_rules.get(tuple_aggregate.typ) 

155 if not to_string_rules: 

156 raise TupleToStringMissingError(tuple_aggregate) 

157 

158 # We have functions, so we attempt to apply until we get 

159 # one that works, in order. 

160 earlier_errors = [] 

161 for instruction_list in to_string_rules.rules: 

162 try: 

163 return build_text_from_instructions(instruction_list, tuple_aggregate) 

164 except TupleComponentError as e: 

165 # If the instruction set is invalid, we skip to the next one. 

166 earlier_errors.append(e) 

167 continue 

168 # If we reach here, it means no instruction worked. 

169 # We raise an error to indicate that no valid instruction set was found. 

170 raise TupleToStringFailedError(tuple_aggregate, earlier_errors) 

171 

172 def _get_description( 

173 self, 

174 item_wrapper: ItemWrapper, 

175 description_fields: List[str], 

176 ) -> str: 

177 """Generates a description text for the LOBSTER item. 

178 

179 The text of a LOBSTER item is always a single string, not a list of strings, 

180 even if there are multiple description fields to consider. 

181 

182 This string uses a different format depending on the number of description 

183 fields: 

184 - If there is only one description field, it returns the text of that field. 

185 - If there are multiple description fields, it formats them as "field: text" 

186 pairs and joins them with two newlines. 

187 

188 If a field is a Array_Type, then all individual values are joined 

189 with a comma. 

190 

191 If a field is a Tuple_Aggregate, it is converted to a string using the 

192 `self._tuple_value_as_string` method. 

193 """ 

194 def join_field_str_values(texts: Iterable[str]) -> str: 

195 return ', '.join(texts) 

196 

197 # if there is only one description field, then return it directly 

198 if len(description_fields) == 1: 

199 return join_field_str_values( 

200 self._generate_text(item_wrapper, description_fields[0]), 

201 ) 

202 

203 # if there are multiple description fields (or zero), then format them 

204 field_text_map = {} 

205 for field in description_fields: 

206 text = join_field_str_values(self._generate_text(item_wrapper, field)) 

207 if text: 

208 field_text_map[field] = text 

209 

210 return "\n\n".join( 

211 f"{field}: {text}" 

212 for field, text in field_text_map.items() 

213 )