Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't store sub-groups where not needed in regex and other processing optimisations #669

Merged
merged 6 commits into from
Nov 20, 2024
Merged
12 changes: 9 additions & 3 deletions ford/sourceform.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ class FortranBase:

POINTS_TO_RE = re.compile(r"\s*=>\s*", re.IGNORECASE)
SPLIT_RE = re.compile(r"\s*,\s*", re.IGNORECASE)
SRC_CAPTURE_STR = r"^[ \t]*([\w(),*: \t]+?[ \t]+)?{0}([\w(),*: \t]+?)?[ \t]+{1}[ \t\n,(].*?end[ \t]*{0}[ \t]+{1}[ \t]*?(!.*?)?$"

pretty_obj = {
"proc": "procedures",
Expand Down Expand Up @@ -443,8 +442,15 @@ def markdown(self, md: MetaMarkdown):
if self.obj in ["proc", "type", "program"] and self.meta.source:
obj = getattr(self, "proctype", self.obj).lower()
regex = re.compile(
self.SRC_CAPTURE_STR.format(obj, self.name),
re.IGNORECASE | re.DOTALL | re.MULTILINE,
fr"""
^(?:[\w(),*: \t]*?)? # Attributes, function type
\b{obj}\b # Subroutine/function
(?:[\w(),*: \t]+?)?[ \t]+ # Interstitial nonsense
\b{self.name}\b[ \t\n,(].*? # Entity name
^[ \t]*\bend[ \t]*{obj}\b[ \t]+\b{self.name}\b[ \t]*? # End statement
(?:!.*?)?$
""",
re.IGNORECASE | re.DOTALL | re.MULTILINE | re.VERBOSE,
)
if match := regex.search(self.source_file.raw_src):
self.src = highlight(
Expand Down
Loading