Skip to content

Commit df2f436

Browse files
committed
refactor: add typings in intrinsics & update def
M_intrinsics can now be automatically updated by python3 -m fortls.intrinsics
1 parent 936ad19 commit df2f436

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

.coveragerc

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ exclude_lines =
1010
log.debug
1111
except:
1212
if not PY3K:
13+
def update_m_intrinsics
14+
update_m_intrinsics()
1315

1416
[html]
1517
show_contexts = True

fortls/intrinsics.py

+48-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import glob
34
import json
45
import os
56

@@ -14,7 +15,7 @@
1415
Variable,
1516
)
1617

17-
none_ast = FortranAST()
18+
intrinsic_ast = FortranAST()
1819
lowercase_intrinsics = False
1920

2021

@@ -28,7 +29,7 @@ def __init__(
2829
self,
2930
name: str,
3031
type: int,
31-
doc_str: str | None = None,
32+
doc_str: str = "",
3233
args: str = "",
3334
parent=None,
3435
):
@@ -37,7 +38,7 @@ def __init__(
3738
self.doc_str: str = doc_str
3839
self.args: str = args.replace(" ", "")
3940
self.parent = parent
40-
self.file_ast: FortranAST = none_ast
41+
self.file_ast: FortranAST = intrinsic_ast
4142
if lowercase_intrinsics:
4243
self.name = self.name.lower()
4344
self.args = self.args.lower()
@@ -52,8 +53,7 @@ def get_desc(self):
5253
return "KEYWORD"
5354
elif self.type == 15:
5455
return "STATEMENT"
55-
else:
56-
return "INTRINSIC"
56+
return "INTRINSIC"
5757

5858
def get_snippet(self, name_replace=None, drop_arg=-1):
5959
if self.args == "":
@@ -90,12 +90,11 @@ def get_hover_md(self, long=False):
9090
def is_callable(self):
9191
if self.type == 2:
9292
return True
93-
else:
94-
return False
93+
return False
9594

9695

9796
def load_intrinsics():
98-
def create_int_object(name, json_obj, type):
97+
def create_int_object(name: str, json_obj: dict, type: int):
9998
args = json_obj.get("args", "")
10099
doc_str = json_obj.get("doc")
101100
if lowercase_intrinsics:
@@ -104,41 +103,40 @@ def create_int_object(name, json_obj, type):
104103
return Intrinsic(name, type, doc_str=doc_str, args=args)
105104

106105
def create_object(json_obj: dict, enc_obj=None):
106+
intrinsic_ast.enc_scope_name = None
107107
if enc_obj is not None:
108-
none_ast.enc_scope_name = enc_obj.FQSN
109-
else:
110-
none_ast.enc_scope_name = None
108+
intrinsic_ast.enc_scope_name = enc_obj.FQSN
109+
keywords = []
110+
keyword_info = {}
111111
if "mods" in json_obj:
112112
keywords, keyword_info = map_keywords(json_obj["mods"])
113-
else:
114-
keywords = []
115-
keyword_info = {}
116113
name = json_obj["name"]
117114
args = json_obj.get("args", "")
118115
if lowercase_intrinsics:
119116
name = name.lower()
120117
args = args.lower()
121-
if json_obj["type"] == 0:
122-
mod_tmp = Module(none_ast, 0, name)
118+
if json_obj["type"] == 0: # module, match "type": in JSON files
119+
mod_tmp = Module(intrinsic_ast, 0, name)
123120
if "use" in json_obj:
124121
mod_tmp.add_use(json_obj["use"], 0)
125122
return mod_tmp
126-
elif json_obj["type"] == 1:
127-
return Subroutine(none_ast, 0, name, args=args)
128-
elif json_obj["type"] == 2:
123+
elif json_obj["type"] == 1: # subroutine, match "type": in JSON files
124+
return Subroutine(intrinsic_ast, 0, name, args=args)
125+
elif json_obj["type"] == 2: # function, match "type": in JSON files
129126
return Function(
130-
none_ast,
127+
intrinsic_ast,
131128
0,
132129
name,
133130
args=args,
134131
result_type=json_obj["return"],
135132
keywords=keywords,
136-
# keyword_info=keyword_info,
137133
)
138-
elif json_obj["type"] == 3:
139-
return Variable(none_ast, 0, name, json_obj["desc"], keywords, keyword_info)
140-
elif json_obj["type"] == 4:
141-
return Type(none_ast, 0, name, keywords)
134+
elif json_obj["type"] == 3: # variable, match "type": in JSON files
135+
return Variable(
136+
intrinsic_ast, 0, name, json_obj["desc"], keywords, keyword_info
137+
)
138+
elif json_obj["type"] == 4: # derived type, match "type": in JSON files
139+
return Type(intrinsic_ast, 0, name, keywords)
142140
else:
143141
raise ValueError
144142

@@ -272,3 +270,28 @@ def get_intrinsic_keywords(statements, keywords, context=-1):
272270
elif context == 3:
273271
return keywords["var_def"] + keywords["type_mem"] + keywords["vis"]
274272
return keywords["var_def"] + keywords["param"]
273+
274+
275+
def update_m_intrinsics():
276+
try:
277+
files = glob.glob("M_intrinsics/md/*.md")
278+
markdown_intrinsics = {}
279+
for f in files:
280+
key = f.replace("M_intrinsics/md/", "")
281+
key = key.replace(".md", "").upper() # remove md extension
282+
with open(f) as md_f:
283+
val = md_f.read()
284+
# remove manpage tag
285+
val = val.replace(f"**{key.lower()}**(3)", f"**{key.lower()}**")
286+
val = val.replace(f"**{key.upper()}**(3)", f"**{key.upper()}**")
287+
markdown_intrinsics[key] = val
288+
289+
with open("fortls/intrinsic.procedures.markdown.json", "w") as f:
290+
json.dump(markdown_intrinsics, f, indent=2)
291+
f.write("\n") # add newline at end of file
292+
except Exception as e:
293+
print(e)
294+
295+
296+
if __name__ == "__main__":
297+
update_m_intrinsics()

0 commit comments

Comments
 (0)