@@ -27,6 +27,37 @@ def _fortran_fpp_files(src_dir: Path):
2727 yield from sorted (src_dir .rglob ("*.fpp" ))
2828
2929
30+ def _check_single_fypp_list (full_line : str , rel : Path , start_line : int ) -> list [str ]:
31+ """Parse one Fypp ``#:for ... in [...]`` line and return errors for duplicates."""
32+ errors : list [str ] = []
33+
34+ bracket_start = full_line .find ("[" )
35+ bracket_end = full_line .rfind ("]" )
36+ if not 0 <= bracket_start < bracket_end :
37+ return errors
38+
39+ list_content = full_line [bracket_start + 1 :bracket_end ]
40+ list_content = list_content .replace ("&" , "" )
41+
42+ # Extract single- or double-quoted entries
43+ entries = re .findall (r"['\"]([^'\"]*)['\"]" , list_content )
44+
45+ seen : dict [str , int ] = {}
46+ for pos , entry in enumerate (entries , 1 ):
47+ if entry in seen :
48+ errors .append (
49+ f" { rel } :{ start_line } Fypp list has duplicate"
50+ f" entry '{ entry } ' (positions { seen [entry ]} "
51+ f" and { pos } )."
52+ " Fix: one copy is likely a typo for a"
53+ " different variable"
54+ )
55+ else :
56+ seen [entry ] = pos
57+
58+ return errors
59+
60+
3061def check_fypp_list_duplicates (repo_root : Path ) -> list [str ]:
3162 """Check for duplicate entries in Fypp ``#:for VAR in [...]`` lists.
3263
@@ -52,27 +83,7 @@ def check_fypp_list_duplicates(repo_root: Path) -> list[str]:
5283 i += 1
5384 full += " " + lines [i ].strip ()
5485
55- bracket_start = full .find ("[" )
56- bracket_end = full .rfind ("]" )
57- if bracket_start >= 0 and bracket_end > bracket_start :
58- list_content = full [bracket_start + 1 :bracket_end ]
59- list_content = list_content .replace ("&" , "" )
60-
61- # Extract single- or double-quoted entries
62- entries = re .findall (r"['\"]([^'\"]*)['\"]" , list_content )
63-
64- seen : dict [str , int ] = {}
65- for pos , entry in enumerate (entries , 1 ):
66- if entry in seen :
67- errors .append (
68- f" { rel } :{ start_line } Fypp list has duplicate"
69- f" entry '{ entry } ' (positions { seen [entry ]} "
70- f" and { pos } )."
71- " Fix: one copy is likely a typo for a"
72- " different variable"
73- )
74- else :
75- seen [entry ] = pos
86+ errors .extend (_check_single_fypp_list (full , rel , start_line ))
7687 i += 1
7788
7889 return errors
0 commit comments