Skip to content

Commit ab39d9b

Browse files
committed
Changes for review
1 parent a77f658 commit ab39d9b

File tree

3 files changed

+38
-33
lines changed

3 files changed

+38
-33
lines changed

doc/source/fparser2.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,11 @@ file was found but would fail if the include file was not found::
498498
Compiler/OpenMP Directive support
499499
---------------------------------
500500
Most Fortran compilers support directives to enable compiler-specific
501-
functionality. Fparser will convert these into ``Directive`` nodes where
502-
possible.
501+
functionality. Fparser has an option to support converting these into
502+
``Directive`` nodes where possible. This option is ``process_directives``,
503+
and by default it is set to ``False``. For directives to be processed, the
504+
``ignore_comments`` must be also be ``False``, otherwise Fparser will ignore
505+
this option.
503506

504507
The supported directives are those recognized by flang, ifx, ifort (``!dir$``),
505508
and gcc (``!gcc$``), as well as OpenMP directives (such as ``!$omp``

src/fparser/common/readfortran.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,22 +554,27 @@ class FortranReaderBase:
554554
:param mode: a FortranFormat object as returned by \
555555
`sourceinfo.get_source_info()`
556556
:type mode: :py:class:`fparser.common.sourceinfo.Format`
557-
:param bool isstrict: whether we are strictly enforcing fixed format.
558-
:param bool ignore_comments: whether or not to discard comments. If
557+
:param isstrict: whether we are strictly enforcing fixed format.
558+
:param ignore_comments: whether or not to discard comments. If
559559
comments are not ignored, they will be added as special Comment node
560560
to the tree, and will therefore also be added to the output Fortran
561561
source code.
562-
:param Optional[bool] include_omp_conditional_lines: whether or not the
562+
:param include_omp_conditional_lines: whether or not the
563563
content of a line with an OMP sentinel is parsed or not. Default is
564564
False (in which case it is treated as a Comment).
565+
:param process_directives: whether or not to process directives as
566+
specialised Directive nodes. Default is False (in which case
567+
directives are left as comments).
565568
566569
The Fortran source is iterated by `get_single_line`,
567570
`get_next_line`, `put_single_line` methods.
568571
569572
"""
570573

571574
def __init__(
572-
self, source, mode, ignore_comments, include_omp_conditional_lines=False
575+
self, source, mode: bool, ignore_comments: bool,
576+
include_omp_conditional_lines: bool = False,
577+
process_directives: bool = False
573578
):
574579
self.source = source
575580
self._include_omp_conditional_lines = include_omp_conditional_lines
@@ -579,6 +584,10 @@ def __init__(
579584
# This value for ignore_comments can be overridden by using the
580585
# ignore_comments optional argument to e.g. get_single_line()
581586
self._ignore_comments = ignore_comments
587+
if self._ignore_comments:
588+
self.process_directives = False
589+
else:
590+
self.process_directives = process_directives
582591

583592
self.filo_line = [] # used for un-consuming lines.
584593
self.fifo_item = []

src/fparser/two/Fortran2003.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
import re
7474
import sys
7575

76+
from typing import Union
77+
7678
from fparser.common.splitline import string_replace_map
7779
from fparser.two import pattern_tools as pattern
7880
from fparser.common.readfortran import FortranReaderBase
@@ -136,21 +138,21 @@ class Directive(Base):
136138

137139
subclass_names = []
138140
_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
150152
]
151153

152154
@show_result
153-
def __new__(cls, string: str | FortranReaderBase, parent_cls=None):
155+
def __new__(cls, string: Union[str, FortranReaderBase], parent_cls=None):
154156
"""
155157
Create a new Directive instance.
156158
@@ -164,11 +166,12 @@ def __new__(cls, string: str | FortranReaderBase, parent_cls=None):
164166

165167
if isinstance(string, readfortran.Comment):
166168
# Directives must start with one of the specified directive
167-
# formats.
169+
# prefixes.
170+
lower = string.comment.lower()
168171
if not (
169172
any(
170173
[
171-
string.comment.lower().startswith(prefix)
174+
lower.startswith(prefix)
172175
for prefix in Directive._directive_formats
173176
]
174177
)
@@ -195,7 +198,6 @@ def __new__(cls, string: str | FortranReaderBase, parent_cls=None):
195198
return res
196199
# We didn't get a directive so put the item back in the FIFO
197200
reader.put_item(item)
198-
return
199201
# We didn't get a directive
200202
return
201203

@@ -277,17 +279,6 @@ def tostr(self):
277279
"""
278280
return str(self.items[0])
279281

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-
291282
def match_comment_or_include(reader):
292283
"""Creates a comment, directive, or include object from the current line.
293284
@@ -304,7 +295,9 @@ def match_comment_or_include(reader):
304295
or :py:class:`fparser.two.Fortran2003.Directive`
305296
306297
"""
307-
obj = Directive(reader)
298+
obj = None
299+
if(reader.process_directives):
300+
obj = Directive(reader)
308301
obj = Comment(reader) if not obj else obj
309302
obj = Include_Stmt(reader) if not obj else obj
310303
return obj

0 commit comments

Comments
 (0)