Skip to content

Commit dfc1bd6

Browse files
committed
refactor: edit incoming commits from stale PR
1 parent fa34adb commit dfc1bd6

File tree

5 files changed

+39
-73
lines changed

5 files changed

+39
-73
lines changed

fortls/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,7 @@ def locate_config(root: str) -> str | None:
501501
error_exit(f"Reading file failed: {err_str}")
502502
print(f" Detected format: {'fixed' if file_obj.fixed else 'free'}")
503503
print("\n=========\nParser Output\n=========\n")
504-
file_ast = file_obj.parse(
505-
debug=True,
506-
pp_defs=pp_defs,
507-
include_dirs=include_dirs,
508-
)
504+
file_ast = file_obj.parse(debug=True, pp_defs=pp_defs, include_dirs=include_dirs)
509505
print("\n=========\nObject Tree\n=========\n")
510506
for obj in file_ast.get_scopes():
511507
print("{}: {}".format(obj.get_type(), obj.FQSN))

fortls/langserver.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,9 +1323,7 @@ def serve_onChange(self, request: dict):
13231323
# Update inheritance (currently file only)
13241324
# tmp_file.ast.resolve_links(self.obj_tree, self.link_version)
13251325
elif file_obj.preproc:
1326-
file_obj.preprocess(
1327-
pp_defs=self.pp_defs,
1328-
)
1326+
file_obj.preprocess(pp_defs=self.pp_defs)
13291327
self.pp_defs = {**self.pp_defs, **file_obj.pp_defs}
13301328

13311329
def serve_onOpen(self, request: dict):
@@ -1398,8 +1396,7 @@ def update_workspace_file(
13981396
if not file_changed:
13991397
return False, None
14001398
ast_new = file_obj.parse(
1401-
pp_defs=self.pp_defs,
1402-
include_dirs=self.include_dirs,
1399+
pp_defs=self.pp_defs, include_dirs=self.include_dirs
14031400
)
14041401
# Add the included read in pp_defs from to the ones specified in the
14051402
# configuration file
@@ -1463,10 +1460,7 @@ def file_init(
14631460
# This is a bypass.
14641461
# For more see on SO: shorturl.at/hwAG1
14651462
set_keyword_ordering(sort)
1466-
file_ast = file_obj.parse(
1467-
pp_defs=pp_defs,
1468-
include_dirs=include_dirs,
1469-
)
1463+
file_ast = file_obj.parse(pp_defs=pp_defs, include_dirs=include_dirs)
14701464
except:
14711465
log.error("Error while parsing file %s", filepath, exc_info=True)
14721466
return "Error during parsing"

fortls/parsers/internal/parser.py

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,10 +1176,7 @@ def find_word_in_code_line(
11761176
return line_no, word_range
11771177

11781178
def preprocess(
1179-
self,
1180-
pp_defs: dict = None,
1181-
include_dirs: set = None,
1182-
debug: bool = False,
1179+
self, pp_defs: dict = None, include_dirs: set = None, debug: bool = False
11831180
) -> tuple[list, list]:
11841181
if pp_defs is None:
11851182
pp_defs = {}
@@ -1268,9 +1265,7 @@ def parse(
12681265
if self.preproc:
12691266
log.debug("=== PreProc Pass ===\n")
12701267
pp_skips, pp_defines = self.preprocess(
1271-
pp_defs=pp_defs,
1272-
include_dirs=include_dirs,
1273-
debug=debug,
1268+
pp_defs=pp_defs, include_dirs=include_dirs, debug=debug
12741269
)
12751270
for pp_reg in pp_skips:
12761271
file_ast.start_ppif(pp_reg[0])
@@ -2043,7 +2038,6 @@ def replace_ops(expr: str):
20432038
expr = expr.replace("!=", " <> ")
20442039
expr = expr.replace("!", " not ")
20452040
expr = expr.replace(" <> ", " != ")
2046-
20472041
return expr
20482042

20492043
def replace_defined(line: str):
@@ -2076,9 +2070,7 @@ def replace_vars(line: str):
20762070

20772071
if defs is None:
20782072
defs = {}
2079-
2080-
out_line = text
2081-
out_line = replace_defined(out_line)
2073+
out_line = replace_defined(text)
20822074
out_line = replace_vars(out_line)
20832075
try:
20842076
line_res = eval(replace_ops(out_line))
@@ -2106,27 +2098,30 @@ def replace_vars(line: str):
21062098
if def_cont_name is not None:
21072099
output_file.append("")
21082100
if line.rstrip()[-1] != "\\":
2109-
append_multiline_macro(defs_tmp, def_cont_name, line.strip())
2101+
defs_tmp[def_cont_name] = append_multiline_macro(
2102+
defs_tmp[def_cont_name], line.strip()
2103+
)
21102104
def_cont_name = None
21112105
else:
2112-
append_multiline_macro(defs_tmp, def_cont_name, line[0:-1].strip())
2113-
2106+
defs_tmp[def_cont_name] = append_multiline_macro(
2107+
defs_tmp[def_cont_name], line[0:-1].strip()
2108+
)
21142109
continue
21152110
# Handle conditional statements
21162111
match = FRegex.PP_REGEX.match(line)
2117-
if match and check_pp_prefix(match.group(1)):
2112+
if match:
21182113
output_file.append(line)
21192114
def_name = None
21202115
if_start = False
21212116
# Opening conditional statements
2122-
if match.group(2).lower() == "if ":
2123-
is_path = eval_pp_if(line[match.end(2) :], defs_tmp)
2117+
if match.group(1).lower() == "if ":
2118+
is_path = eval_pp_if(line[match.end(1) :], defs_tmp)
21242119
if_start = True
2125-
elif match.group(2).lower() == "ifdef":
2120+
elif match.group(1).lower() == "ifdef":
21262121
if_start = True
21272122
def_name = line[match.end(0) :].strip()
21282123
is_path = def_name in defs_tmp
2129-
elif match.group(2).lower() == "ifndef":
2124+
elif match.group(1).lower() == "ifndef":
21302125
if_start = True
21312126
def_name = line[match.end(0) :].strip()
21322127
is_path = not (def_name in defs_tmp)
@@ -2144,7 +2139,7 @@ def replace_vars(line: str):
21442139
inc_start = False
21452140
exc_start = False
21462141
exc_continue = False
2147-
if match.group(2).lower() == "elif":
2142+
if match.group(1).lower() == "elif":
21482143
if (not pp_stack_group) or (pp_stack_group[-1][0] != len(pp_stack)):
21492144
# First elif statement for this elif group
21502145
if pp_stack[-1][0] < 0:
@@ -2156,15 +2151,15 @@ def replace_vars(line: str):
21562151
exc_continue = True
21572152
if pp_stack[-1][0] < 0:
21582153
pp_stack[-1][0] = i + 1
2159-
elif eval_pp_if(line[match.end(2) :], defs_tmp):
2154+
elif eval_pp_if(line[match.end(1) :], defs_tmp):
21602155
pp_stack[-1][1] = i + 1
21612156
pp_skips.append(pp_stack.pop())
21622157
pp_stack_group[-1][1] = True
21632158
pp_stack.append([-1, -1])
21642159
inc_start = True
21652160
else:
21662161
exc_start = True
2167-
elif match.group(2).lower() == "else":
2162+
elif match.group(1).lower() == "else":
21682163
if pp_stack[-1][0] < 0:
21692164
pp_stack[-1][0] = i + 1
21702165
exc_start = True
@@ -2180,7 +2175,7 @@ def replace_vars(line: str):
21802175
pp_skips.append(pp_stack.pop())
21812176
pp_stack.append([-1, -1])
21822177
inc_start = True
2183-
elif match.group(2).lower() == "endif":
2178+
elif match.group(1).lower() == "endif":
21842179
if pp_stack_group and (pp_stack_group[-1][0] == len(pp_stack)):
21852180
pp_stack_group.pop()
21862181
if pp_stack[-1][0] < 0:
@@ -2201,12 +2196,10 @@ def replace_vars(line: str):
22012196
continue
22022197
# Handle variable/macro definitions files
22032198
match = FRegex.PP_DEF.match(line)
2204-
if (match is not None and check_pp_prefix(match.group(1))) and (
2205-
(len(pp_stack) == 0) or (pp_stack[-1][0] < 0)
2206-
):
2199+
if (match is not None) and ((len(pp_stack) == 0) or (pp_stack[-1][0] < 0)):
22072200
output_file.append(line)
22082201
pp_defines.append(i + 1)
2209-
def_name = match.group(3)
2202+
def_name = match.group(2)
22102203
# If this is an argument list of a function add them to the name
22112204
# get_definition will only return the function name upon hover
22122205
# hence if the argument list is appended in the def_name then
@@ -2215,11 +2208,8 @@ def replace_vars(line: str):
22152208
# This also does not allow for multiline argument list definitions.
22162209
# if match.group(3):
22172210
# def_name += match.group(3)
2218-
if (match.group(2) == "define") and (def_name not in defs_tmp):
2211+
if (match.group(1) == "define") and (def_name not in defs_tmp):
22192212
eq_ind = line[match.end(0) :].find(" ")
2220-
if eq_ind < 0:
2221-
eq_ind = line[match.end(0) :].find("\t")
2222-
22232213
if eq_ind >= 0:
22242214
# Handle multiline macros
22252215
if line.rstrip()[-1] == "\\":
@@ -2231,11 +2221,11 @@ def replace_vars(line: str):
22312221
def_value = "True"
22322222

22332223
# are there arguments to parse?
2234-
if match.group(4):
2235-
def_value = (match.group(5), def_value)
2224+
if match.group(3):
2225+
def_value = (match.group(4), def_value)
22362226

22372227
defs_tmp[def_name] = def_value
2238-
elif (match.group(2) == "undef") and (def_name in defs_tmp):
2228+
elif (match.group(1) == "undef") and (def_name in defs_tmp):
22392229
defs_tmp.pop(def_name, None)
22402230
log.debug(f"{line.strip()} !!! Define statement({i + 1})")
22412231
continue
@@ -2286,10 +2276,9 @@ def replace_vars(line: str):
22862276
def_regex = def_regexes.get(def_tmp)
22872277
if def_regex is None:
22882278
if isinstance(value, tuple):
2289-
def_regex = expand_def_func_macro(def_tmp, value)
2279+
def_regex = expand_func_macro(def_tmp, value)
22902280
else:
22912281
def_regex = re.compile(rf"\b{def_tmp}\b")
2292-
22932282
def_regexes[def_tmp] = def_regex
22942283

22952284
if isinstance(def_regex, tuple):
@@ -2305,7 +2294,7 @@ def replace_vars(line: str):
23052294
return output_file, pp_skips, pp_defines, defs_tmp
23062295

23072296

2308-
def expand_def_func_macro(def_name: str, def_value: tuple[str, str]):
2297+
def expand_func_macro(def_name: str, def_value: tuple[str, str]):
23092298
def_args, sub = def_value
23102299
def_args = def_args.split(",")
23112300
regex = re.compile(rf"\b{def_name}\s*\({','.join(['(.*)']*len(def_args))}\)")
@@ -2317,19 +2306,9 @@ def expand_def_func_macro(def_name: str, def_value: tuple[str, str]):
23172306
return regex, sub
23182307

23192308

2320-
def append_multiline_macro(pp_defs: dict, def_name: str, line: str):
2321-
def_value = pp_defs[def_name]
2322-
def_args = None
2309+
def append_multiline_macro(def_value: str | tuple, line: str):
23232310
if isinstance(def_value, tuple):
23242311
def_args, def_value = def_value
2325-
2326-
def_value += line
2327-
2328-
if def_args is not None:
2329-
def_value = (def_args, def_value)
2330-
2331-
pp_defs[def_name] = def_value
2332-
2333-
2334-
def check_pp_prefix(prefix: str):
2335-
return prefix == "#"
2312+
def_value += line
2313+
return (def_args, def_value)
2314+
return def_value + line

fortls/regex_patterns.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,14 @@ class FortranRegularExpressions:
124124
FREE_FORMAT_TEST: Pattern = compile(r"[ ]{1,4}[a-z]", I)
125125
# Preprocessor matching rules
126126
DEFINED: Pattern = compile(r"defined[ ]*\(?[ ]*([a-z_]\w*)[ ]*\)?", I)
127-
PP_REGEX: Pattern = compile(
128-
r"[ ]*(#)[ ]*(if |ifdef|ifndef|else|elif|endif)",
129-
I,
130-
)
127+
PP_REGEX: Pattern = compile(r"[ ]*#[ ]*(if |ifdef|ifndef|else|elif|endif)", I)
131128
PP_DEF: Pattern = compile(
132-
r"[ ]*(#)[ ]*(define|undef|undefined)" r"[ ]+(\w+)(\([ ]*([ \w,]*?)[ ]*\))?",
129+
r"[ ]*#[ ]*(define|undef|undefined)[ ]*(\w+)(\([ ]*([ \w,]*?)[ ]*\))?",
133130
I,
134131
)
135132
PP_DEF_TEST: Pattern = compile(r"(![ ]*)?defined[ ]*\([ ]*(\w*)[ ]*\)$", I)
136-
PP_INCLUDE: Pattern = compile(r"[ ]*#[ ]*include[ ]+([\"\w\.]*)", I)
137-
PP_ANY: Pattern = compile(r"(^[ ]*(?:#)[ ]*\w*:?\w+)")
133+
PP_INCLUDE: Pattern = compile(r"[ ]*#[ ]*include[ ]*([\"\w\.]*)", I)
134+
PP_ANY: Pattern = compile(r"^[ ]*#:?[ ]*(\w+)")
138135
# Context matching rules
139136
CALL: Pattern = compile(r"[ ]*CALL[ ]+[\w%]*$", I)
140137
INT_STMNT: Pattern = compile(r"^[ ]*[a-z]*$", I)

test/test_source/pp/.pp_conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"pp_suffixes": [".h", ".F90"],
88
"incl_suffixes": [".h"],
99
"include_dirs": ["include"],
10-
"pp_defs": { "HAVE_CONTIGUOUS": "" },
10+
"pp_defs": { "HAVE_CONTIGUOUS": "" }
1111
}

0 commit comments

Comments
 (0)