Coverage for lobster/tools/trlc/to_string_rules.py: 97%
22 statements
« prev ^ index » next coverage.py v7.10.7, created at 2026-09-22 04:43 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2026-09-22 04:43 +0000
1# LOBSTER - Lightweight Open BMW Software Traceability Evidence Report
2# Copyright (C) 2025 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as
6# published by the Free Software Foundation, either version 3 of the
7# License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12# Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public
15# License along with this program. If not, see
16# <https://www.gnu.org/licenses/>.
18from dataclasses import dataclass
19from typing import Dict, Iterable, List
20from trlc import ast
22from lobster.tools.trlc.instruction import Instruction
23from lobster.tools.trlc.text_generation import parse_instructions
26@dataclass
27class ToStringRules:
28 """A set of rules for converting TRLC tuples to strings."""
29 tuple_type_name: str
30 package_name: str
31 rules: List[List[Instruction]]
33 @staticmethod
34 def from_dict(data: dict) -> "ToStringRules":
35 return ToStringRules(
36 tuple_type_name=data["tuple-type"],
37 package_name=data["package"],
38 rules=[parse_instructions(to_string_line)
39 for to_string_line in data.get("to-string", [])],
40 )
43def build_tuple_type_to_ruleset_map(
44 symbol_table: ast.Symbol_Table,
45 to_string_rule_sets: Iterable[ToStringRules],
46) -> Dict[ast.Tuple_Type, ToStringRules]:
47 """Iterates over all tuple types in the symbol table and returns a mapping
48 from tuple types to their corresponding ToStringRules.
49 """
50 result = {}
51 for n_pkg in symbol_table.values(ast.Package):
52 for tuple_type in n_pkg.symbols.values(ast.Tuple_Type):
53 for rule_set in to_string_rule_sets:
54 if (tuple_type.name == rule_set.tuple_type_name) \
55 and (n_pkg.name == rule_set.package_name):
56 result[tuple_type] = rule_set
57 break
58 return result