73
73
import re
74
74
import sys
75
75
76
+ from typing import Union
77
+
76
78
from fparser .common .splitline import string_replace_map
77
79
from fparser .two import pattern_tools as pattern
78
80
from fparser .common .readfortran import FortranReaderBase
@@ -136,21 +138,21 @@ class Directive(Base):
136
138
137
139
subclass_names = []
138
140
_directive_formats = [
139
- "!$dir" ,
140
- "!dir$" ,
141
- "cdir$" ,
142
- "!$omp" ,
143
- "c$omp" ,
144
- "*$omp" ,
145
- "!$omx" ,
146
- "c$omx" ,
147
- "*$omx" ,
148
- "!gcc$" ,
149
- "!$ompx" ,
141
+ "!$dir" , # Generic directive
142
+ "!dir$" , # flang, ifx, ifort directives.
143
+ "cdir$" , # flang, ifx, ifort fixed format directive.
144
+ "!$omp" , # OpenMP directive
145
+ "c$omp" , # OpenMP fixed format directive
146
+ "*$omp" , # OpenMP fixed format directive
147
+ "!$omx" , # OpenMP fixed format directive
148
+ "c$omx" , # OpenMP fixed format directive
149
+ "*$omx" , # OpenMP fixed format directive
150
+ "!gcc$" , # GCC compiler directive
151
+ "!$ompx" , # OpenMP extension directive
150
152
]
151
153
152
154
@show_result
153
- def __new__ (cls , string : str | FortranReaderBase , parent_cls = None ):
155
+ def __new__ (cls , string : Union [ str , FortranReaderBase ] , parent_cls = None ):
154
156
"""
155
157
Create a new Directive instance.
156
158
@@ -164,11 +166,12 @@ def __new__(cls, string: str | FortranReaderBase, parent_cls=None):
164
166
165
167
if isinstance (string , readfortran .Comment ):
166
168
# Directives must start with one of the specified directive
167
- # formats.
169
+ # prefixes.
170
+ lower = string .comment .lower ()
168
171
if not (
169
172
any (
170
173
[
171
- string . comment . lower () .startswith (prefix )
174
+ lower .startswith (prefix )
172
175
for prefix in Directive ._directive_formats
173
176
]
174
177
)
@@ -195,7 +198,6 @@ def __new__(cls, string: str | FortranReaderBase, parent_cls=None):
195
198
return res
196
199
# We didn't get a directive so put the item back in the FIFO
197
200
reader .put_item (item )
198
- return
199
201
# We didn't get a directive
200
202
return
201
203
@@ -277,17 +279,6 @@ def tostr(self):
277
279
"""
278
280
return str (self .items [0 ])
279
281
280
- def restore_reader (self , reader ):
281
- """
282
- Undo the read of this comment by putting its content back
283
- into the reader (which has a FIFO buffer)
284
-
285
- :param reader: the reader instance to return the comment to
286
- :type reader: :py:class:`fparser.readfortran.FortranReaderBase`
287
- """
288
- reader .put_item (self .item )
289
-
290
-
291
282
def match_comment_or_include (reader ):
292
283
"""Creates a comment, directive, or include object from the current line.
293
284
@@ -304,7 +295,9 @@ def match_comment_or_include(reader):
304
295
or :py:class:`fparser.two.Fortran2003.Directive`
305
296
306
297
"""
307
- obj = Directive (reader )
298
+ obj = None
299
+ if (reader .process_directives ):
300
+ obj = Directive (reader )
308
301
obj = Comment (reader ) if not obj else obj
309
302
obj = Include_Stmt (reader ) if not obj else obj
310
303
return obj
0 commit comments