osmo-iuh/asn1/utils/asn1_restructure_sequence_o...

58 lines
1.9 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright 2022 sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
"""
Avoid compiler warnings like "struct HNBAP_CriticalityDiagnostics_IE_List__Member
declared inside parameter list will not be visible outside of this definition
or declaration", which break compiling the sources with -Werror and can't be
ignored with diagnostics as they don't have their own type (unlike e.g.
-Wuninitialized).
This problem is explained further in:
https://github.com/vlm/asn1c/issues/430
To avoid this, convert "SQUENCE … OF SEQUENCE" like here:
CriticalityDiagnostics-IE-List ::= SEQUENCE (SIZE (1..maxNrOfErrors)) OF
SEQUENCE {
to "SEQUENCE … OF …-Value" and below that "…-Value ::= SEQUENCE":
CriticalityDiagnostics-IE-List ::= SEQUENCE (SIZE (1..maxNrOfErrors)) OF CriticalityDiagnostics-IE-List-Value
CriticalityDiagnostics-IE-List-Value ::=
SEQUENCE {
"""
import glob
import re
re_seq_of = re.compile("([A-Za-z0-0-]*)\s*::=\s*SEQUENCE .* OF$")
re_seq_of2 = re.compile("([A-Za-z0-0-]*)\s*::=\s*SEQUENCE .* OF SEQUENCE\s*{$")
for file in glob.glob("asn1/*/*.asn"):
print(f"{file}:")
with open(file) as handle:
lines = handle.readlines()
for i, line in enumerate(lines):
match = re_seq_of.search(line)
if match and "SEQUENCE" in lines[i + 1]:
name_value = f"{match.group(1)}-Value"
print(f" {name_value}")
lines[i] = f"{line.rstrip()} {name_value}\n{name_value} ::=\n"
continue
match = re_seq_of2.search(line)
if match:
name_value = f"{match.group(1)}-Value"
print(f" {name_value}")
line = line.split(" OF SEQUENCE")[0]
lines[i] = f"{line} OF {name_value}\n{name_value} ::= SEQUENCE {{\n"
continue
with open(file, "w") as handle:
handle.writelines(lines)
print("Done")