Skip to content

Commit 921a96b

Browse files
committed
Moves regular expressions to a dataclass
1 parent 914ff47 commit 921a96b

File tree

6 files changed

+270
-342
lines changed

6 files changed

+270
-342
lines changed

fortls/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import sys
55
from dataclasses import dataclass, field
66

7+
from fortls.regex_patterns import FortranRegularExpressions
8+
79
PY3K = sys.version_info >= (3, 0)
810

911
log = logging.getLogger(__name__)
@@ -81,3 +83,6 @@ class FUN_sig:
8183
def __post_init__(self):
8284
if not self.result.name:
8385
self.result.name = self.name
86+
87+
88+
FRegex = FortranRegularExpressions()

fortls/helper_functions.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,7 @@
33
import os
44
from pathlib import Path
55

6-
from fortls.constants import KEYWORD_ID_DICT, KEYWORD_LIST, log, sort_keywords
7-
from fortls.regex_patterns import (
8-
DQ_STRING_REGEX,
9-
FIXED_COMMENT_LINE_MATCH,
10-
FREE_FORMAT_TEST,
11-
LINE_LABEL_REGEX,
12-
LOGICAL_REGEX,
13-
NAT_VAR_REGEX,
14-
NUMBER_REGEX,
15-
OBJBREAK_REGEX,
16-
SQ_STRING_REGEX,
17-
WORD_REGEX,
18-
)
6+
from fortls.constants import KEYWORD_ID_DICT, KEYWORD_LIST, FRegex, log, sort_keywords
197

208

219
def expand_name(line: str, char_poss: int) -> str:
@@ -35,7 +23,13 @@ def expand_name(line: str, char_poss: int) -> str:
3523
"""
3624
# The order here is important.
3725
# WORD will capture substrings in logical and strings
38-
regexs = [LOGICAL_REGEX, SQ_STRING_REGEX, DQ_STRING_REGEX, WORD_REGEX, NUMBER_REGEX]
26+
regexs = [
27+
FRegex.LOGICAL,
28+
FRegex.SQ_STRING,
29+
FRegex.DQ_STRING,
30+
FRegex.WORD,
31+
FRegex.NUMBER,
32+
]
3933
for r in regexs:
4034
for num_match in r.finditer(line):
4135
if num_match.start(0) <= char_poss <= num_match.end(0):
@@ -59,13 +53,13 @@ def detect_fixed_format(file_lines: list[str]) -> bool:
5953
True if file_lines are of Fixed Fortran style
6054
"""
6155
for line in file_lines:
62-
if FREE_FORMAT_TEST.match(line):
56+
if FRegex.FREE_FORMAT_TEST.match(line):
6357
return False
64-
tmp_match = NAT_VAR_REGEX.match(line)
58+
tmp_match = FRegex.VAR.match(line)
6559
if tmp_match and tmp_match.start(1) < 6:
6660
return False
6761
# Trailing ampersand indicates free or intersection format
68-
if not FIXED_COMMENT_LINE_MATCH.match(line):
62+
if not FRegex.FIXED_COMMENT.match(line):
6963
line_end = line.split("!")[0].strip()
7064
if len(line_end) > 0 and line_end[-1] == "&":
7165
return False
@@ -86,7 +80,7 @@ def strip_line_label(line: str) -> tuple[str, str | None]:
8680
Output string, Line label returns None if no line label present
8781
"""
8882

89-
match = LINE_LABEL_REGEX.match(line)
83+
match = FRegex.LINE_LABEL.match(line)
9084
if match is None:
9185
return line, None
9286
else:
@@ -118,11 +112,11 @@ def repl_dq(m):
118112
return '"{0}"'.format(" " * (len(m.group()) - 2))
119113

120114
if maintain_len:
121-
out_line = SQ_STRING_REGEX.sub(repl_sq, in_line)
122-
out_line = DQ_STRING_REGEX.sub(repl_dq, out_line)
115+
out_line = FRegex.SQ_STRING.sub(repl_sq, in_line)
116+
out_line = FRegex.DQ_STRING.sub(repl_dq, out_line)
123117
else:
124-
out_line = SQ_STRING_REGEX.sub("", in_line)
125-
out_line = DQ_STRING_REGEX.sub("", out_line)
118+
out_line = FRegex.SQ_STRING.sub("", in_line)
119+
out_line = FRegex.DQ_STRING.sub("", out_line)
126120
return out_line
127121

128122

@@ -183,7 +177,7 @@ def find_word_in_line(line: str, word: str) -> tuple[int, int]:
183177
start and end positions (indices) of the word if not found it returns
184178
-1, len(word) -1"""
185179
i0 = -1
186-
for poss_name in WORD_REGEX.finditer(line):
180+
for poss_name in FRegex.WORD.finditer(line):
187181
if poss_name.group() == word:
188182
i0 = poss_name.start()
189183
break
@@ -417,7 +411,7 @@ def get_var_stack(line):
417411
final_var += line[section[0] : section[1]]
418412
#
419413
if final_var is not None:
420-
final_op_split = OBJBREAK_REGEX.split(final_var)
414+
final_op_split = FRegex.OBJBREAK.split(final_var)
421415
return final_op_split[-1].split("%")
422416
else:
423417
return None

fortls/langserver.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
SELECT_TYPE_ID,
2222
SUBROUTINE_TYPE_ID,
2323
VAR_TYPE_ID,
24+
FRegex,
2425
log,
2526
)
2627
from fortls.helper_functions import (
@@ -47,14 +48,7 @@
4748
get_use_tree,
4849
)
4950
from fortls.parse_fortran import fortran_file, get_line_context, process_file
50-
from fortls.regex_patterns import (
51-
DQ_STRING_REGEX,
52-
INT_STMNT_REGEX,
53-
LOGICAL_REGEX,
54-
NUMBER_REGEX,
55-
SQ_STRING_REGEX,
56-
src_file_exts,
57-
)
51+
from fortls.regex_patterns import src_file_exts
5852
from fortls.version import __version__
5953

6054
# Global regexes
@@ -540,6 +534,7 @@ def build_comp(
540534
if self.autocomplete_no_prefix:
541535
var_prefix = ""
542536
# Suggestions for user-defined type members
537+
scope_list = []
543538
if is_member:
544539
curr_scope = file_obj.ast.get_inner_scope(ac_line + 1)
545540
type_scope = climb_type_tree(var_stack, curr_scope, self.obj_tree)
@@ -758,14 +753,16 @@ def get_definition(
758753
# Return a dummy object with the correct type & position in the doc
759754
if hover_req and curr_scope:
760755
var_type = None
761-
if NUMBER_REGEX.match(def_name):
756+
if FRegex.NUMBER.match(def_name):
762757
if any(s in def_name for s in [".", "e", "d"]):
763758
var_type = f"{FORTRAN_LITERAL}REAL"
764759
else:
765760
var_type = f"{FORTRAN_LITERAL}INTEGER"
766-
elif LOGICAL_REGEX.match(def_name):
761+
elif FRegex.LOGICAL.match(def_name):
767762
var_type = f"{FORTRAN_LITERAL}LOGICAL"
768-
elif SQ_STRING_REGEX.match(def_name) or DQ_STRING_REGEX.match(def_name):
763+
elif FRegex.SQ_STRING.match(def_name) or FRegex.DQ_STRING.match(
764+
def_name
765+
):
769766
var_type = f"{FORTRAN_LITERAL}STRING"
770767
if var_type:
771768
return fortran_var(
@@ -853,7 +850,7 @@ def check_optional(arg, params):
853850
break
854851
# Check keywords
855852
if (var_obj is None) and (
856-
INT_STMNT_REGEX.match(line_prefix[:sub_end]) is not None
853+
FRegex.INT_STMNT.match(line_prefix[:sub_end]) is not None
857854
):
858855
key = sub_name.lower()
859856
for candidate in get_intrinsic_keywords(self.statements, self.keywords, 0):

fortls/objects.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
SUBROUTINE_TYPE_ID,
2525
VAR_TYPE_ID,
2626
WHERE_TYPE_ID,
27+
FRegex,
2728
)
2829
from fortls.helper_functions import get_keywords, get_paren_substring, get_var_stack
2930
from fortls.jsonrpc import path_to_uri
30-
from fortls.regex_patterns import CLASS_VAR_REGEX, DEF_KIND_REGEX
3131

3232
# Helper types
3333
VAR_info = NamedTuple(
@@ -1595,7 +1595,7 @@ def __init__(
15951595
self.desc: str = var_desc
15961596
self.keywords: list = keywords
15971597
self.keyword_info: dict = keyword_info
1598-
self.callable: bool = CLASS_VAR_REGEX.match(var_desc) is not None
1598+
self.callable: bool = FRegex.CLASS_VAR.match(var_desc) is not None
15991599
self.children: list = []
16001600
self.use: list[USE_line] = []
16011601
self.link_obj = None
@@ -1718,7 +1718,7 @@ def set_external_attr(self):
17181718

17191719
def check_definition(self, obj_tree, known_types={}, interface=False):
17201720
# Check for type definition in scope
1721-
type_match = DEF_KIND_REGEX.match(self.desc)
1721+
type_match = FRegex.DEF_KIND.match(self.desc)
17221722
if type_match is not None:
17231723
var_type = type_match.group(1).strip().lower()
17241724
if var_type == "procedure":

0 commit comments

Comments
 (0)