Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
42 changes: 29 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,23 @@ 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.
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 6\n" in str(err.value)
assert ">>> if (.true.)\n" in str(err.value)
Loading