stfc / fparser

This project maintains and develops a Fortran parser called fparser2 written purely in Python which supports Fortran 2003 and some Fortran 2008. A legacy parser fparser1 is also available but is not supported. The parsers were originally part of the f2py project by Pearu Peterson.
https://fparser.readthedocs.io
Other
64 stars 29 forks source link

Issue with Parsing Comments in Fixed-form Fortran w/ Include #407

Closed mjklemm closed 1 year ago

mjklemm commented 1 year ago

The parser throws an error for this code snippet:

      subroutine foobar (a,b,c)
c     Some comment
#include "some.inc"
      end subroutine foobar

Error reported by the parser:

Traceback (most recent call last):
  File "C:\Users\micha\PycharmProjects\fortran_offload\fparser\two\Fortran2003.py", line 266, in __new__
    return Base.__new__(cls, string)
  File "C:\Users\micha\PycharmProjects\fortran_offload\fparser\two\utils.py", line 487, in __new__
    raise NoMatchError(errmsg)
fparser.two.utils.NoMatchError: at line 2
>>>c     Some comment

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\micha\PycharmProjects\fortran_offload\main.py", line 51, in <module>
    parse_tree = f08_parser(reader)
  File "C:\Users\micha\PycharmProjects\fortran_offload\fparser\two\Fortran2003.py", line 270, in __new__
    raise FortranSyntaxError(string, "")
fparser.two.utils.FortranSyntaxError: at line 2
>>>c     Some comment

The error goes away when the #include is removed.

arporter commented 1 year ago

I think the problem here is the #include - fparser has only very limited support for pre-processor macros (since they aren't Fortran, see e.g. #333). You could try using the Fortran include instead?

mjklemm commented 1 year ago

Ok, that seems to work better. I think I can manage to run the preprocessor first and provide "clean" Fortran to fparser.

mjklemm commented 1 year ago

PS: Closing this as "user error" :-)