From 56f1969b1d6fc4a89b53f0f34827953df37ef24d Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Mon, 2 Oct 2023 11:27:13 +0200 Subject: [PATCH 2/2] generate_enums.py: default ZERO and COUNT identifiers when prefix is given See osdn #48789 Signed-off-by: Alina Lenk --- gen_headers/enums/terrain_enums.def | 6 +++--- gen_headers/generate_enums.py | 28 ++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/gen_headers/enums/terrain_enums.def b/gen_headers/enums/terrain_enums.def index 38f596fcc5..bf55431e4b 100644 --- a/gen_headers/enums/terrain_enums.def +++ b/gen_headers/enums/terrain_enums.def @@ -8,7 +8,7 @@ /* Used in the network protocol. */ enum terrain_class prefix TC_ - count COUNT + count values /* TRANS: terrain class: used adjectivally */ LAND N_("Land") @@ -23,7 +23,7 @@ end * Used in the network protocol. */ enum terrain_alteration prefix TA_ - count COUNT + count values /* Can build irrigation without changing terrain */ /* TRANS: this and following strings may rarely be presented to the player @@ -82,7 +82,7 @@ end enum mapgen_terrain_property prefix MG_ - count COUNT + count values MOUNTAINOUS "mountainous" GREEN "green" diff --git a/gen_headers/generate_enums.py b/gen_headers/generate_enums.py index 793f16db2c..1ed9103c8a 100755 --- a/gen_headers/generate_enums.py +++ b/gen_headers/generate_enums.py @@ -55,8 +55,12 @@ The following are supported: - bitwise - zero (requires bitwise) + If 'prefix' is also given, the identifier is optional + and defaults to ZERO - count (cannot be used with bitwise) + If 'prefix' is also given, the identifier is optional + and defaults to COUNT - invalid - name-override - name-updater @@ -354,6 +358,14 @@ class Specenum: - the option name - (optional) the option's arguments""" + DEFAULT_ZERO = EnumValue("ZERO", None) + """Default SPECENUM_ZERO info when the 'zero' option is used without + any identifier, but in conjunction with a 'prefix' option""" + + DEFAULT_COUNT = EnumValue("COUNT", None) + """Default SPECENUM_COUNT info when the 'count' option is used without + any identifier, but in conjunction with a 'prefix' option""" + name: str """The SPECENUM_NAME of this enum""" @@ -417,14 +429,16 @@ class Specenum: if self.zero is not None: raise ValueError(f"duplicate option {option!r} for enum {self.name}") if arg is None: - raise ValueError(f"option {option!r} for enum {self.name} requires an argument") - self.zero = EnumValue.parse(arg) + self.zero = __class__.DEFAULT_ZERO + else: + self.zero = EnumValue.parse(arg) elif option == "count": if self.count is not None: raise ValueError(f"duplicate option {option!r} for enum {self.name}") if arg is None: - raise ValueError(f"option {option!r} for enum {self.name} requires an argument") - self.count = EnumValue.parse(arg) + self.count = __class__.DEFAULT_COUNT + else: + self.count = EnumValue.parse(arg) elif option == "invalid": if self.invalid is not None: raise ValueError(f"duplicate option {option!r} for enum {self.name}") @@ -460,6 +474,12 @@ class Specenum: if self.bitvector and self.bitwise: raise ValueError(f"option 'bitvector' conflicts with option 'bitwise' for enum {self.name}") + # check sanity + if self.zero is __class__.DEFAULT_ZERO and not self.prefix: + raise ValueError(f"option 'zero' for enum {self.name} requires an argument or option 'prefix'") + if self.count is __class__.DEFAULT_COUNT and not self.prefix: + raise ValueError(f"option 'count' for enum {self.name} requires an argument or option 'prefix'") + self.values = [EnumValue.parse(line) for line in lines] def code_parts(self) -> typing.Iterable[str]: -- 2.34.1