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
61 stars 29 forks source link

Improve fparser2 error report for parsing an empty file #119

Closed TeranIvy closed 5 years ago

TeranIvy commented 5 years ago

When parsing an empty file fparser2 returns an unhelpful error

{{{
  File ".local/PSycloneTest/lib/python2.7/site-packages/fparser/two/Fortran2003.py", line 209, in __new__
    return Base.__new__(cls, string)
  File ".local/PSycloneTest/lib/python2.7/site-packages/fparser/two/utils.py", line 256, in __new__
    string.source_lines[string.linecount-1])
IndexError: list index out of range

Apparently, just compiling an empty file is tolerated by Fortran compilers (e.g. Intel 17.0.1 and Gnu 6.1.0) so fparser2 should not fail because of this. However, a warning to report parsing an empty file would be very useful.

rupertford commented 5 years ago

This can be fixed by checking for an empty file within the new method of Program. Add the following code at line 209 on master of file Fortran2003.py

        if string.linecount == 0:
            raise FortranSyntaxError("Empty file")

Obviously some tidying of the SyntaxError would need to be done. There have been improvements to the output of errors in PR @121 (e.g. it modifies the script so it does not raise an exception if there is no code in the buffer - which is what happens in this case) so I'll wait until that is on master before adding this code.

rupertford commented 5 years ago

I don't think the above solution is correct as it is returning 0 even if the number of lines in the file is not 0. We might need to actually read a line, check it, then put it back if it is not empty, or perhaps modify the reader to give us the information we want.

rupertford commented 5 years ago

For anything other than a completely empty file we get a sensible syntax error. So we just need to handle this particular case.

rupertford commented 5 years ago

It's pretty simple to raise a SyntaxError when there is an empty file but I don't think that it what @TeranIvy wants. She has asked for there to be no error if the input is empty. I think I can do this by using sys.exit("message").

rupertford commented 5 years ago

Created branch empty_input

rupertford commented 5 years ago

raising SystemExit works but it kills the interpreter when running interactively (unless it is trapped) so I'm not happy with this as a solution.

rupertford commented 5 years ago

Now I'm just returning if there is no content, rather than raising an exception when there are no matches and this seems to work.

rupertford commented 5 years ago

pr #134 has been merged to master. Closing this issue.