fortran-lang / fortls

fortls - Fortran Language Server
https://fortls.fortran-lang.org/
MIT License
258 stars 41 forks source link

Semicolon in fixed-form comment makes part of the comment being parsed as code #411

Open albertziegenhagel opened 5 months ago

albertziegenhagel commented 5 months ago

Describe the bug Given a fixed-form Fortran file that has a comment line which includes a semicolon, all text after the semicolon in the comment line is parsed as regular code. This can confuse the parser if the comment includes text that makes up Fortran keywords.

To Reproduce Given the following code in a file called invalid_comment.f:

c ; do not treat this as code
      subroutine foo
      end subroutine foo

Then, calling fortls --debug_filepath invalid_comment.f --debug_parser gives

=== Parsing Pass ===

 do not treat this as code !!! DO - Ln:1
      subroutine foo !!! SUBROUTINE - Ln:2
      end subroutine foo !!! END SUBROUTINE Scope - Ln:3

which shows that the do in the first comment line is parsed as a do-statement. This can lead to many follow up errors, e.g. in the code above we will get an "Invalid parent for "SUBROUTINE" declaration" diagnostic being generated in line 2, since for the parser it seems that the subroutine is defined within a "do" loop.

Expected behavior Comments should never be parsed as code.

Screenshots & Animations image

Setup information (please complete the following information):

gnikit commented 5 months ago

Thanks @albertziegenhagel I will look into it.

gnikit commented 5 months ago

I think I have located the problem, however it interferes with the spaghetti code for parsing Doxygen-like and FORD-like docstrings. I might have to rewrite those 2 parsers first, before I fix this. I