fix(learnset_helpers): Harden regex-patterns for preproc directives (#9582)

This commit is contained in:
Rachel 2026-03-20 10:24:34 -07:00 committed by GitHub
parent ed30cf717e
commit 6034c13eab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 18 deletions

View File

@ -24,7 +24,6 @@ For a given species, a move is considered teachable to that species if:
from itertools import chain from itertools import chain
from textwrap import dedent from textwrap import dedent
import glob
import json import json
import pathlib import pathlib
import re import re
@ -32,9 +31,9 @@ import sys
import typing import typing
CONFIG_ENABLED_PAT = re.compile(r"#define P_LEARNSET_HELPER_TEACHABLE\s+(?P<cfg_val>[^ ]*)") CONFIG_ENABLED_PAT = re.compile(r"^#define P_LEARNSET_HELPER_TEACHABLE\s+(?P<cfg_val>[^ ]*)", flags=re.MULTILINE)
ALPHABETICAL_ORDER_ENABLED_PAT = re.compile(r"#define HGSS_SORT_TMS_BY_NUM\s+(?P<cfg_val>[^ ]*)") ALPHABETICAL_ORDER_ENABLED_PAT = re.compile(r"^#define HGSS_SORT_TMS_BY_NUM\s+(?P<cfg_val>[^ ]*)", flags=re.MULTILINE)
TM_LITTERACY_PAT = re.compile(r"#define P_TM_LITERACY\s+GEN_(?P<cfg_val>[^ ]*)") TM_LITERACY_PAT = re.compile(r"^#define P_TM_LITERACY\s+GEN_(?P<cfg_val>[^ ]*)", flags=re.MULTILINE)
TMHM_MACRO_PAT = re.compile(r"F\((\w+)\)") TMHM_MACRO_PAT = re.compile(r"F\((\w+)\)")
SNAKIFY_PAT = re.compile(r"(?!^)([A-Z]+)") SNAKIFY_PAT = re.compile(r"(?!^)([A-Z]+)")
@ -64,7 +63,7 @@ def extract_tm_litteracy_config() -> bool:
config = False config = False
with open("./include/config/pokemon.h", "r") as cfg_pokemon_fp: with open("./include/config/pokemon.h", "r") as cfg_pokemon_fp:
cfg_pokemon = cfg_pokemon_fp.read() cfg_pokemon = cfg_pokemon_fp.read()
cfg_defined = TM_LITTERACY_PAT.search(cfg_pokemon) cfg_defined = TM_LITERACY_PAT.search(cfg_pokemon)
if cfg_defined: if cfg_defined:
cfg_val = cfg_defined.group("cfg_val") cfg_val = cfg_defined.group("cfg_val")
if ((cfg_val == "LATEST") or (int(cfg_val) > 6)): if ((cfg_val == "LATEST") or (int(cfg_val) > 6)):
@ -193,14 +192,13 @@ def main():
print(__doc__, file=sys.stderr) print(__doc__, file=sys.stderr)
quit(1) quit(1)
if len(sys.argv) == 2: if len(sys.argv) == 3:
SOURCE_DIR = pathlib.Path(sys.argv[1])
elif len(sys.argv) == 3:
if sys.argv[1] != "--tutors": if sys.argv[1] != "--tutors":
print("Unknown make_teachables mode", file=sys.stderr) print("Unknown make_teachables mode", file=sys.stderr)
quit(1) quit(1)
tutor_mode = True tutor_mode = True
SOURCE_DIR = pathlib.Path(sys.argv[2])
SOURCE_DIR = pathlib.Path(sys.argv[-1])
with open("src/data/pokemon/special_movesets.json", "r") as file: with open("src/data/pokemon/special_movesets.json", "r") as file:
special_movesets = json.load(file) special_movesets = json.load(file)
@ -219,7 +217,6 @@ def main():
assert SOURCE_TEACHING_TYPES_JSON.is_file(), f"{SOURCE_TEACHING_TYPES_JSON=} is not a file" assert SOURCE_TEACHING_TYPES_JSON.is_file(), f"{SOURCE_TEACHING_TYPES_JSON=} is not a file"
repo_tms = list(extract_repo_tms()) repo_tms = list(extract_repo_tms())
order_alphabetically = False
with open("./include/config/pokedex_plus_hgss.h", "r") as cfg_pokemon_fp: with open("./include/config/pokedex_plus_hgss.h", "r") as cfg_pokemon_fp:
cfg_pokemon = cfg_pokemon_fp.read() cfg_pokemon = cfg_pokemon_fp.read()

View File

@ -1,16 +1,24 @@
#!/usr/bin/env python3
"""
Usage: python3 make_teaching_types.py OUTPUT_FILE
Build a primary store of "teaching-types" for each enabled species in the repository as an
input for make_teachables.py.
"""
import glob import glob
import json import json
import pathlib import pathlib
import re import re
import sys import sys
import typing
CONFIG_ENABLED_PAT = re.compile(r"#define P_LEARNSET_HELPER_TEACHABLE\s+(?P<cfg_val>[^ ]*)") CONFIG_ENABLED_PAT = re.compile(r"^#define P_LEARNSET_HELPER_TEACHABLE\s+(?P<cfg_val>[^ ]*)", flags=re.MULTILINE)
TEACHING_TYPE_PAT = re.compile(r"\s*\.teachingType\s*=\s*(?P<teaching_type>[A-Z_]+),") TEACHING_TYPE_PAT = re.compile(r"\s*\.teachingType\s*=\s*(?P<teaching_type>[A-Z_]+),")
LEARNSET_PAT = re.compile(r"\s*\.teachableLearnset\s*=\s*s(?P<name>\w+?)TeachableLearnset") LEARNSET_PAT = re.compile(r"\s*\.teachableLearnset\s*=\s*s(?P<name>\w+?)TeachableLearnset")
PREPROC_START_PAT = re.compile(r"#if(def)?\s+\w+") PREPROC_START_PAT = re.compile(r"^#if(def)?\s+\w+", flags=re.MULTILINE)
PREPROC_END_PAT = re.compile(r"#endif\s*(//\s*\w+)?") PREPROC_END_PAT = re.compile(r"^#endif\s*(//\s*\w+)?", flags=re.MULTILINE)
def enabled() -> bool: def enabled() -> bool:
""" """
@ -35,7 +43,7 @@ def extract_repo_species_data() -> list:
pokemon_list = [] pokemon_list = []
teaching_type = "DEFAULT_LEARNING" teaching_type = "DEFAULT_LEARNING"
file_list = sorted(glob.glob("src/data/pokemon/species_info/*_families.h")) file_list = sorted(glob.glob("src/data/pokemon/species_info/*_families.h"))
file_list.append(pathlib.Path("./src/data/pokemon/species_info.h")) file_list.append("./src/data/pokemon/species_info.h")
for families_fname in file_list: for families_fname in file_list:
with open(families_fname, "r") as family_fp: with open(families_fname, "r") as family_fp:
species_lines = family_fp.readlines() species_lines = family_fp.readlines()

View File

@ -1,5 +1,12 @@
#!/usr/bin/env python3
"""
Usage: python3 make_tutors.py OUTPUT_FILE
Build a primary store of move-tutors in the repository and what moves they teach.
"""
from itertools import chain from itertools import chain
from textwrap import dedent
import glob import glob
import json import json
@ -8,7 +15,7 @@ import re
import sys import sys
import typing import typing
CONFIG_ENABLED_PAT = re.compile(r"#define P_LEARNSET_HELPER_TEACHABLE\s+(?P<cfg_val>[^ ]*)") CONFIG_ENABLED_PAT = re.compile(r"^#define P_LEARNSET_HELPER_TEACHABLE\s+(?P<cfg_val>[^ ]*)", flags=re.MULTILINE)
INCFILE_HAS_TUTOR_PAT = re.compile(r"special ChooseMonForMoveTutor") INCFILE_HAS_TUTOR_PAT = re.compile(r"special ChooseMonForMoveTutor")
INCFILE_HAS_TUTOR_PAT2 = re.compile(r"chooseboxmon SELECT_PC_MON_MOVE_TUTOR") INCFILE_HAS_TUTOR_PAT2 = re.compile(r"chooseboxmon SELECT_PC_MON_MOVE_TUTOR")
INCFILE_MOVE_PAT = re.compile(r"setvar VAR_0x8005, (MOVE_[A-Z_]*)") INCFILE_MOVE_PAT = re.compile(r"setvar VAR_0x8005, (MOVE_[A-Z_]*)")