Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/fparser/two/Fortran2008/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
from fparser.two.Fortran2008.loop_control_r818 import Loop_Control
from fparser.two.Fortran2008.if_stmt_r837 import If_Stmt
from fparser.two.Fortran2008.error_stop_stmt_r856 import Error_Stop_Stmt
from fparser.two.Fortran2008.format_item_r1003 import Format_Item
from fparser.two.Fortran2008.stop_code_r857 import Stop_Code
from fparser.two.Fortran2008.specification_part_c1112 import Specification_Part_C1112
from fparser.two.Fortran2008.implicit_part_c1112 import Implicit_Part_C1112
Expand Down
105 changes: 105 additions & 0 deletions src/fparser/two/Fortran2008/format_item_r1003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -----------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2026, Science and Technology Facilities Council.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------

"""
Module containing Fortran 2008 Format_Item rule R1003
"""
from fparser.two.Fortran2003 import (
Format_Item as Format_Item_2003,
Format_Item_List,
)
from fparser.two.utils import NoMatchError


class Format_Item(Format_Item_2003): # R1003
"""Fortran 2008 rule R1003

format-item is [ r ] data-edit-desc
or control-edit-desc
or char-string-edit-desc
or [ r ] ( format-item-list )
or format-item-c1002
or hollerith-item
or * ( format-item-list )

Extends the Fortran 2003 rule R1003 with the additional unlimited
format repeat specifier ``*`` before a parenthesised format-item-list.

"""

# Inherit the parent's subclass_names so that Control_Edit_Desc,
# Hollerith_Item, etc. remain registered when the F2008 parser
# replaces F2003 Format_Item in the class hierarchy.
# Include "Format_Item" itself so that this F2008 class is
# registered in Base.subclasses["Format_Item"] and can be
# discovered when F2003 code (e.g. Format_Item_List.match)
# directly references the F2003 Format_Item class.
subclass_names = Format_Item_2003.subclass_names[:] + ["Format_Item"]
use_names = Format_Item_2003.use_names[:]

@staticmethod
def match(string):
"""Attempts to match the supplied text with this rule.

Calls the Fortran 2003 match first. If that fails, checks
for the Fortran 2008 unlimited format repeat: ``*(format-item-list)``.

:param str string: Fortran code to check for a match.

:returns: None if there is no match, a tuple of size 2 \
containing a repeat specifier (``"*"`` or an R instance) \
and the matched descriptor or format-item-list.
:rtype: Optional[Tuple]

"""
# Fortran 2003 matches all but unlimited repeat, so try it first.
# The F2003 match raises NoMatchError rather than returning None
# when it cannot parse the string (e.g. Data_Edit_Desc fails).
try:
result = Format_Item_2003.match(string)
except NoMatchError:
result = None
if result:
return result
# Try to match unlimited format repeat: *(format-item-list)
if not string:
return None
strip_string = string.strip()
if not strip_string:
return None
if strip_string[0] == "*" and len(strip_string) > 1:
my_string = strip_string[1:].lstrip()
if my_string[0] == "(" and my_string[-1] == ")":
return ("*", Format_Item_List(my_string[1:-1].lstrip()))
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Copyright (c) 2026 Science and Technology Facilities Council

# All rights reserved.

# Modifications made as part of the fparser project are distributed
# under the following license:

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:

# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.

# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.

# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Test Fortran 2008 rule R1003

format-item is [ r ] data-edit-desc
or control-edit-desc
or char-string-edit-desc
or [ r ] ( format-item-list )
or format-item-c1002
or hollerith-item
or * ( format-item-list )

Extends the Fortran 2003 rule R1003 with the additional unlimited
format repeat specifier ``*`` before a parenthesised format-item-list.

"""
from fparser.two.Fortran2008 import Format_Item
from fparser.two.parser import ParserFactory
from fparser.common.readfortran import FortranStringReader


def test_unlimited_repeat_basic():
"""Test basic unlimited format repeat: *(I5)."""
obj = Format_Item("*(I5)")
assert isinstance(obj, Format_Item)
assert str(obj) == "*(I5)"


def test_unlimited_repeat_multiple_items():
"""Test unlimited format repeat with multiple items: *(1X,A12)."""
obj = Format_Item("*(1X,A12)")
assert isinstance(obj, Format_Item)
assert str(obj) == "*(1X, A12)"


def test_unlimited_repeat_pe_descriptor():
"""Test unlimited format repeat with PE descriptor: *(1X,1PE12.5)."""
obj = Format_Item("*(1X,1PE12.5)")
assert isinstance(obj, Format_Item)
assert str(obj) == "*(1X, 1P, E12.5)"


def test_f2003_match():
"""Test that F2003 format items still match via the F2008 class."""
obj = Format_Item("3(I5)")
assert isinstance(obj, Format_Item)
assert str(obj) == "3(I5)"


def test_format_stmt_unlimited_repeat():
"""Test unlimited format repeat in a full FORMAT statement."""
f2008 = ParserFactory().create(std="f2008")

reader = FortranStringReader(
"subroutine t()\n 1 format(*(I5))\nend subroutine"
)
tree = f2008(reader)
assert tree is not None


def test_format_stmt_mixed_items():
"""Test FORMAT with regular items followed by unlimited repeat."""
f2008 = ParserFactory().create(std="f2008")

reader = FortranStringReader(
"subroutine t()\n 1 format('!',A12,*(1X,A12))\nend subroutine"
)
tree = f2008(reader)
assert tree is not None


def test_format_stmt_pe_descriptor():
"""Test FORMAT with unlimited repeat and PE edit descriptor."""
f2008 = ParserFactory().create(std="f2008")

reader = FortranStringReader(
"subroutine t()\n 2 format(*(1X,1PE12.5))\nend subroutine"
)
tree = f2008(reader)
assert tree is not None