1
1
from __future__ import annotations
2
2
3
+ import glob
3
4
import json
4
5
import os
5
6
14
15
Variable ,
15
16
)
16
17
17
- none_ast = FortranAST ()
18
+ intrinsic_ast = FortranAST ()
18
19
lowercase_intrinsics = False
19
20
20
21
@@ -28,7 +29,7 @@ def __init__(
28
29
self ,
29
30
name : str ,
30
31
type : int ,
31
- doc_str : str | None = None ,
32
+ doc_str : str = "" ,
32
33
args : str = "" ,
33
34
parent = None ,
34
35
):
@@ -37,7 +38,7 @@ def __init__(
37
38
self .doc_str : str = doc_str
38
39
self .args : str = args .replace (" " , "" )
39
40
self .parent = parent
40
- self .file_ast : FortranAST = none_ast
41
+ self .file_ast : FortranAST = intrinsic_ast
41
42
if lowercase_intrinsics :
42
43
self .name = self .name .lower ()
43
44
self .args = self .args .lower ()
@@ -52,8 +53,7 @@ def get_desc(self):
52
53
return "KEYWORD"
53
54
elif self .type == 15 :
54
55
return "STATEMENT"
55
- else :
56
- return "INTRINSIC"
56
+ return "INTRINSIC"
57
57
58
58
def get_snippet (self , name_replace = None , drop_arg = - 1 ):
59
59
if self .args == "" :
@@ -90,12 +90,11 @@ def get_hover_md(self, long=False):
90
90
def is_callable (self ):
91
91
if self .type == 2 :
92
92
return True
93
- else :
94
- return False
93
+ return False
95
94
96
95
97
96
def load_intrinsics ():
98
- def create_int_object (name , json_obj , type ):
97
+ def create_int_object (name : str , json_obj : dict , type : int ):
99
98
args = json_obj .get ("args" , "" )
100
99
doc_str = json_obj .get ("doc" )
101
100
if lowercase_intrinsics :
@@ -104,41 +103,40 @@ def create_int_object(name, json_obj, type):
104
103
return Intrinsic (name , type , doc_str = doc_str , args = args )
105
104
106
105
def create_object (json_obj : dict , enc_obj = None ):
106
+ intrinsic_ast .enc_scope_name = None
107
107
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 = {}
111
111
if "mods" in json_obj :
112
112
keywords , keyword_info = map_keywords (json_obj ["mods" ])
113
- else :
114
- keywords = []
115
- keyword_info = {}
116
113
name = json_obj ["name" ]
117
114
args = json_obj .get ("args" , "" )
118
115
if lowercase_intrinsics :
119
116
name = name .lower ()
120
117
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 )
123
120
if "use" in json_obj :
124
121
mod_tmp .add_use (json_obj ["use" ], 0 )
125
122
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
129
126
return Function (
130
- none_ast ,
127
+ intrinsic_ast ,
131
128
0 ,
132
129
name ,
133
130
args = args ,
134
131
result_type = json_obj ["return" ],
135
132
keywords = keywords ,
136
- # keyword_info=keyword_info,
137
133
)
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 )
142
140
else :
143
141
raise ValueError
144
142
@@ -272,3 +270,28 @@ def get_intrinsic_keywords(statements, keywords, context=-1):
272
270
elif context == 3 :
273
271
return keywords ["var_def" ] + keywords ["type_mem" ] + keywords ["vis" ]
274
272
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