5from pathlib
import Path
9reg_type_spec = (
r"(?:INTEGER|REAL|DOUBLE\s*PRECISION|COMPLEX|CHARACTER|LOGICAL|TYPE|CLASS)"
10 r"\s*(?:\((?:[^()]|\([^()]*\))*\)|\*\s*\w+)?")
11reg_proc_prefix = (
r"(?:(?:IMPURE|PURE|ELEMENTAL|RECURSIVE|NON_RECURSIVE)\s+|"
12 + reg_type_spec +
r"\s*)*")
13reg_proc_name = re.compile(
r"^\s*" + reg_proc_prefix +
r"(SUBROUTINE|FUNCTION)\s+(\w+)",
16reg_proc_end = re.compile(
r"^\s*END\s*((SUBROUTINE|FUNCTION)\b|$)", re.IGNORECASE)
17reg_proc_args = re.compile(
r"\(([\w\s,]+)\)", re.IGNORECASE)
18reg_func_result = re.compile(
r"RESULT\s*\(([\w\s]+)\)", re.IGNORECASE)
19reg_use_module_only = re.compile(
r"(?:^\s*use\s+\w+\s*,\s*only\s*:)(.*)", re.IGNORECASE)
20reg_use_module_name = re.compile(
r"^\s*use\s+(\w+)", re.IGNORECASE)
21reg_variable_name_simple = re.compile(
r"::\s*(\w+)", re.IGNORECASE)
22reg_string = re.compile(
r"'[^']*'|\"[^\"]*\"")
23reg_component = re.compile(
r"%\s*\w+")
24reg_identifier = re.compile(
r"[A-Z_][A-Z0-9_]*")
26reg_acdc = re.compile(
r"!\$ACDC\b", re.IGNORECASE)
29reg_case_is_important = re.compile(
r"\s*#|\s*INCLUDE", re.IGNORECASE)
31reg_all_decl_variable = [
32 r"^\s*INTEGER\s*[\(*,:\s]",
33 r"^\s*REAL\s*[\(*,:\s]",
34 r"^\s*DOUBLE PRECISION\s*[\(*,:\s]",
35 r"^\s*CHARACTER\s*[\(*,:\s]",
36 r"^\s*LOGICAL\s*[\(*,:\s]",
43 reg_proc_name.pattern,
49 *reg_all_decl_variable,
52reg_is_still_decl = re.compile(
"|".join(x
for x
in reg_all_decl), re.IGNORECASE)
53reg_is_variable_decl = re.compile(
54 "|".join(x
for x
in reg_all_decl_variable), re.IGNORECASE
59 """Source file whose interface module cannot be generated"""
65 if line.startswith(
"MODULE "):
77 if reg_acdc.match(line):
79 lines.append(line +
"\n")
81 if line.startswith(
"!"):
84 line = line.replace(
" ",
" ")
85 if not reg_case_is_important.match(line):
87 if "!" in line
and not line.startswith(
"#"):
90 line = line.split(
"!")[0].strip()
91 if line.endswith(
"&"):
92 buf += line.replace(
"&",
"")
94 lines.append(buf + line.replace(
"&",
"") +
"\n")
103 for idx, line
in enumerate(lines):
104 if reg_proc_name.match(line):
108 if reg_proc_end.match(line):
110 for idx
in reversed(to_rm):
116 for idx, line
in enumerate(lines):
117 if line.startswith(
"#if"):
119 if line.startswith(
"#endif"):
121 for idx
in reversed(ifdef):
133 for idx, line
in enumerate(lines):
134 if line.startswith(
"USE "):
135 matches = reg_use_module_only.match(line)
139 use_only[idx] = [v.strip()
for v
in matches.group(1).split(
",")
if v.strip()]
146 if reg_is_variable_decl.match(line):
147 line = reg_component.sub(
"", reg_string.sub(
"", line))
148 used.update(reg_identifier.findall(line))
152 for idx, values
in use_only.items():
156 local = val.split(
"=>")[0].strip()
158 if local.startswith((
"OPERATOR",
"ASSIGNMENT"))
or local
in used:
161 module = reg_use_module_name.match(lines[idx]).group(1)
162 lines[idx] = f
"USE {module}, ONLY: {', '.join(kept)}\n"
166 for idx
in reversed(sorted(to_rm)):
172 matches = reg_proc_name.match(line)
173 proc_type = matches.group(1)
174 proc_name = matches.group(2)
177 end_name = matches.end()
179 matches = reg_proc_args.search(line, end_name)
181 proc_args = [arg.strip()
for arg
in matches.group(1).split(
",")]
183 if proc_type ==
"FUNCTION":
184 matches = reg_func_result.search(line, end_name)
186 proc_args.append(matches.group(1).strip())
188 return proc_type, proc_name, proc_args
195 while len(line) > 80
and not line.lstrip().startswith(
"!"):
196 break_idx_space = line[0:80].rfind(
" ")
197 break_idx_comma = line[0:80].rfind(
",")
200 if break_idx_comma > break_idx_space:
201 break_idx = break_idx_comma
203 break_idx = break_idx_space
209 newlines.append(line[:break_idx] +
"&\n")
210 line = line[break_idx:]
211 newlines.append(line)
219 names = [reg_proc_name.match(line).group(2)
for line
in lines
220 if reg_proc_name.match(line)]
222 raise GenModiError(f
"{f90}: no SUBROUTINE or FUNCTION found")
224 raise GenModiError(f
"{f90}: several procedures found outside a module "
225 f
"({', '.join(names)}), only one is allowed")
226 if names[0].upper() != f90.stem.upper():
227 raise GenModiError(f
"{f90}: the procedure name ({names[0]}) must be the "
228 f
"file name ({f90.stem})")
232 modi_path = Path(modi_dir / Path(
"modi_" + str(f90.name)))
234 modi_path.unlink(missing_ok=
True)
235 with open(f90,
"r")
as src:
250 if reg_proc_name.match(line):
252 modi_lines.append(f
"MODULE MODI_{proc_name}\nINTERFACE\n")
254 matches = reg_is_still_decl.match(line)
255 if not matches
and proc_name:
256 modi_lines.append(f
"END {proc_type} {proc_name}\n")
257 modi_lines.append(f
"END INTERFACE\n")
258 modi_lines.append(f
"END MODULE MODI_{proc_name}\n")
262 if proc_name
and reg_is_variable_decl.match(line)
and "INTENT" not in line:
266 matches = reg_variable_name_simple.search(line)
268 var_name = matches.group(1)
269 if var_name
not in proc_args
and var_name != proc_name:
273 modi_lines.append(line)
278 with open(modi_path,
"w")
as modi:
279 modi.write(
"".join(modi_lines))
282modi_dir = Path(
"modi")
285 idx = args.index(
"-o")
289 modi_dir = Path(args[idx + 1])
293for arg
in sys.argv[1:]:
297 except GenModiError
as err:
298 print(f
"gen_modi.py: error: {err}", file=sys.stderr)
remove_unsed_modules(lines)
check_procedures(f90, lines)
remove_dangling_ifdef(lines)
remove_contained_procedures(lines)