Skip to content

Commit a2178c1

Browse files
committed
Extract regex to separate file
1 parent 2af8e9b commit a2178c1

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

mkdocstrings_handlers/vba/regex.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import re
2+
3+
re_arg = re.compile(
4+
r"((?P<optional>Optional) +)?"
5+
r"((?P<modifier>ByVal|ByRef) +)?"
6+
r"(?P<name>[A-Z_][A-Z0-9_]*)"
7+
r"( +As +(?P<type>[A-Z_][A-Z0-9_]*))?"
8+
r"( *= *(?P<default>.*))?",
9+
re.IGNORECASE,
10+
)
11+
12+
re_signature = re.compile(
13+
r"((?P<visibility>Private|Public) +)?"
14+
r"(?P<type>Sub|Function|Property (Let|Get)) *"
15+
r"(?P<name>[A-Z_][A-Z0-9_]*)\( *(?P<args>[A-Z0-9_ ,=]*)\)"
16+
r"( +As +(?P<returnType>[A-Z_][A-Z0-9_]*))?",
17+
re.IGNORECASE,
18+
)
19+
20+
if __name__ == "__main__":
21+
print(re_arg.pattern)
22+
print(re_signature.pattern)

mkdocstrings_handlers/vba/util.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from griffe.dataclasses import Docstring
55
from griffe.docstrings import Parser
66

7+
from mkdocstrings_handlers.vba.regex import re_signature, re_arg
78
from mkdocstrings_handlers.vba.types import (
89
VbaArgumentInfo,
910
VbaSignatureInfo,
@@ -83,15 +84,7 @@ def parse_args(args: str) -> Generator[VbaArgumentInfo, None, None]:
8384
if not len(arg):
8485
continue
8586

86-
match = re.match(
87-
r"^((?P<optional>Optional) +)?"
88-
r"((?P<modifier>ByVal|ByRef) +)?"
89-
r"(?P<name>[A-Z_][A-Z0-9_]*)"
90-
r"( +As +(?P<type>[A-Z_][A-Z0-9_]*))?"
91-
r"( *= *(?P<default>.*))?$",
92-
arg,
93-
re.IGNORECASE,
94-
)
87+
match = re_arg.fullmatch(arg)
9588

9689
if match is None:
9790
raise RuntimeError(f"Failed to parse argument: {arg}")
@@ -112,15 +105,7 @@ def parse_signature(line: str) -> VbaSignatureInfo:
112105
"""
113106
line = re.sub(r"'.*$", "", line).strip() # Strip comment and whitespace.
114107

115-
# https://regex101.com/r/BclPPV/1
116-
match = re.match(
117-
r"^((?P<visibility>Private|Public) +)?"
118-
r"(?P<type>Sub|Function|Property (Let|Get)) *"
119-
r"(?P<name>[A-Z_][A-Z0-9_]*)\( *(?P<args>[A-Z0-9_ ,=]*)\)"
120-
r"( +As +(?P<returnType>[A-Z_][A-Z0-9_]*))?$",
121-
line,
122-
re.IGNORECASE,
123-
)
108+
match = re_signature.fullmatch(line)
124109

125110
if match is None:
126111
raise RuntimeError(f"Failed to parse signature: {line}")

test/util/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)