Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 0 additions & 8 deletions example/test_files/make_public_correct.f90
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
MODULE a_mod

Comment thread
sergisiso marked this conversation as resolved.
! Access_Stmt private will be removed:

! Attr_Spec with 0 and 1 additional attribute, the protected will be remobed
REAL :: planet_radius = 123
REAL, PARAMETER :: planet_radius_constant = 123

LOGICAL :: public_protected = .FALSE.
LOGICAL :: only_protected = .FALSE.
LOGICAL :: private_protected = .FALSE.

! Access_stmt with public, this will be unmodified
PUBLIC :: public_protected
! Protected_Stmt - the whole statement will be removed
! Access_stmt with private - the whole statement will be removed

TYPE :: my_type
! Private_Components_Stmt in a type will be removed
INTEGER :: a, b
CONTAINS
! This private will also be removed.

END TYPE my_type

! Access_Spec - the `private` will be removed
TYPE(my_type), PUBLIC :: my_var

CONTAINS
SUBROUTINE sub_a
END SUBROUTINE sub_a
Expand Down
6 changes: 6 additions & 0 deletions src/fparser/two/Fortran2003.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ def __new__(cls, string, parent_cls=None):
from fparser.common import readfortran

if isinstance(string, readfortran.Comment):
# We can reach this with false readfortran.Comment nodes
# that represent empty lines in the original input. These
# have no syntactic relevance, and so are not kept by
# fparser.
if string.comment == "":
return
Comment thread
sergisiso marked this conversation as resolved.
Outdated
# We were after a comment and we got a comment. Construct
# one manually to avoid recursively calling this __new__
# method again...
Expand Down
66 changes: 53 additions & 13 deletions src/fparser/two/tests/test_comments_and_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import pytest
from fparser.two.Fortran2003 import Program, Comment, Directive, Subroutine_Subprogram
from fparser.two.utils import walk
from fparser.two.utils import walk, FortranSyntaxError
from fparser.api import get_reader

from fparser.two.parser import ParserFactory
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_simple_prog():
" ! A full line comment\n"
' PRINT *, "Hello"\n'
" ! This block gets executed\n"
"END PROGRAM a_prog\n"
"END PROGRAM a_prog"
)


Expand Down Expand Up @@ -120,7 +120,7 @@ def test_ifthen():
' PRINT *, "Hello"\n'
" ! Another full line comment\n"
" END IF\n"
"END PROGRAM a_prog\n"
"END PROGRAM a_prog"
)


Expand All @@ -147,7 +147,7 @@ def test_inline_ifthen():
" ! An in-line comment here\n"
" END IF\n"
" ! A comment after a block\n"
"END PROGRAM a_prog\n"
"END PROGRAM a_prog"
)


Expand Down Expand Up @@ -441,15 +441,11 @@ def test_directive_stmts():
old = reader.get_item()
assert old is not None

out = walk(program, Comment)
comments = 0
for comment in out:
if comment.items[0] != "":
comments = comments + 1
assert comments == 3
assert str(out[1]) == "!$dir inline"
assert str(out[3]) == "! A comment!"
assert str(out[4]) == "!!$ Another comment"
comments = walk(program, Comment)
assert len(comments) == 3
assert str(comments[0]) == "!$dir inline"
assert str(comments[1]) == "! A comment!"
assert str(comments[2]) == "!!$ Another comment"

# Check that passing something that isn't a comment into a Directive
# __new__ call doesn't create a Directive.
Expand Down Expand Up @@ -606,3 +602,47 @@ def test_inline_directive_is_comment():
program = Program(reader)
out = walk(program, Directive)
assert len(out) == 0


def test_syntax_error_with_comments():
"""Test that when we keep comments we still correctly give syntax errors
when the first line of the file is a blank line."""
source = """
Comment thread
sergisiso marked this conversation as resolved.


! This is module m


module m
integer :: x
contains
subroutine foo()
if (.true.)
x = 0
end if
end subroutine
end module"""
reader = get_reader(source, ignore_comments=False)
with pytest.raises(FortranSyntaxError) as err:
program = Program(reader)
assert "at line 11\n" in str(err.value)
assert ">>> if (.true.)\n" in str(err.value)


def test_base_to_fortran_empty_comment():
"""Test that if we have an empty comment we get the correct
to_fortran from the base class implementation (i.e. no tab)"""
source = """
!Comment
program test
end program
"""
reader = get_reader(source, ignore_comments=False)
program = Program(reader)
out = walk(program, Comment)
comment = out[0]
assert comment.tofortran(tab=" ") == " !Comment"
# Change the comment to be an empty comment.
comment.items = [""]
comment.item = ""
assert comment.tofortran(tab=" ") == ""
Loading