4343import os
4444import tempfile
4545import logging
46- from fparse_utils import (USE_PARSE_RE , VAR_DECL_RE , InputStream ,
47- CharFilter , OMP_RE , OMP_DIR_RE )
46+ from fparse_utils import (USE_PARSE_RE , VAR_DECL_RE , OMP_RE , OMP_DIR_RE ,
47+ InputStream , CharFilter , FPrettifyParseError )
4848
4949
5050# PY2/PY3 compat wrappers:
@@ -159,16 +159,6 @@ def any(iterable):
159159 ENDFCT_RE , ENDMOD_RE , ENDPROG_RE , ENDINTERFACE_RE , ENDTYPE_RE ]
160160
161161
162- class FortranSyntaxError (Exception ):
163- """Exception for unparseable Fortran code"""
164-
165- def __init__ (self , filename , line_nr ,
166- msg = ("Syntax error - "
167- "this formatter can not handle invalid Fortran files." )):
168- super (FortranSyntaxError , self ).__init__ ('{}:{}:{}' .format (
169- filename , line_nr , msg ))
170-
171-
172162class F90Indenter (object ):
173163 """
174164 Parses encapsulation of subunits / scopes line by line
@@ -251,7 +241,7 @@ def process_lines_of_fline(self, f_line, lines, rel_ind, rel_ind_con,
251241
252242 if is_new :
253243 if not valid_new :
254- raise FortranSyntaxError (filename , line_nr )
244+ raise FPrettifyParseError (filename , line_nr )
255245 else :
256246 line_indents = [ind + indents [- 1 ] for ind in line_indents ]
257247 old_ind = indents [- 1 ]
@@ -263,13 +253,13 @@ def process_lines_of_fline(self, f_line, lines, rel_ind, rel_ind_con,
263253
264254 elif is_con :
265255 if not valid_con :
266- raise FortranSyntaxError (filename , line_nr )
256+ raise FPrettifyParseError (filename , line_nr )
267257 else :
268258 line_indents = [ind + indents [- 2 ] for ind in line_indents ]
269259
270260 elif is_end :
271261 if not valid_end :
272- raise FortranSyntaxError (filename , line_nr )
262+ raise FPrettifyParseError (filename , line_nr )
273263 else :
274264 line_indents = [ind + indents [- 2 ] for ind in line_indents ]
275265 indents .pop ()
@@ -337,7 +327,7 @@ def process_lines_of_fline(self, f_line, lines, rel_ind, line_nr):
337327 self ._line_indents .append (self ._br_indent_list [- 1 ])
338328
339329 if len (self ._br_indent_list ) > 2 or self ._level :
340- raise SyntaxError (self ._filename , self ._line_nr )
330+ raise FPrettifyParseError (self ._filename , self ._line_nr )
341331
342332 def get_lines_indent (self ):
343333 """
@@ -386,7 +376,7 @@ def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
386376 level += - 1
387377 indent_list .pop ()
388378 if level < 0 :
389- raise FortranSyntaxError (filename , line_nr )
379+ raise FPrettifyParseError (filename , line_nr )
390380
391381 if pos_ldelim :
392382 pos_ldelim .pop ()
@@ -399,7 +389,7 @@ def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
399389 if what_del_open == r"[" :
400390 valid = what_del_close == r"]"
401391 if not valid :
402- raise FortranSyntaxError (filename , line_nr )
392+ raise FPrettifyParseError (filename , line_nr )
403393 else :
404394 pos_rdelim .append (pos )
405395 rdelim .append (what_del_close )
@@ -408,7 +398,7 @@ def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
408398 if not REL_OP_RE .match (
409399 line [max (0 , pos - 1 ):min (pos + 2 , len (line ))]):
410400 if pos_eq > 0 :
411- raise FortranSyntaxError (filename , line_nr )
401+ raise FPrettifyParseError (filename , line_nr )
412402 is_pointer = line [pos + 1 ] == '>'
413403 pos_eq = pos + 1
414404 # don't align if assignment operator directly before
@@ -435,18 +425,20 @@ def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
435425 self ._level = level
436426
437427
438- def inspect_ffile_format (infile , indent_size ):
428+ def inspect_ffile_format (infile , indent_size , orig_filename = None ):
439429 """
440430 Determine indentation by inspecting original Fortran file
441431 (mainly for finding aligned blocks of DO/IF statements).
442432 Also check if it has f77 constructs.
443433 """
434+ if not orig_filename :
435+ orig_filename = infile .name
444436
445437 adopt = indent_size <= 0
446438
447439 is_f90 = True
448440 indents = []
449- stream = InputStream (infile )
441+ stream = InputStream (infile , orig_filename )
450442 prev_offset = 0
451443 first_indent = - 1
452444 while 1 :
@@ -704,7 +696,7 @@ def format_single_fline(f_line, whitespace, linebreak_pos, ampersand_sep,
704696 lines_out .append (line [linebreak_pos_ftd [- 1 ]:])
705697
706698 if level != 0 :
707- raise FortranSyntaxError (filename , line_nr )
699+ raise FPrettifyParseError (filename , line_nr )
708700
709701 return lines_out
710702
@@ -750,7 +742,7 @@ def reformat_ffile(infile, outfile, indent_size=3, whitespace=2,
750742
751743 infile .seek (0 )
752744 req_indents , first_indent , is_f90 = inspect_ffile_format (
753- infile , indent_size )
745+ infile , indent_size , orig_filename )
754746 infile .seek (0 )
755747
756748 if not is_f90 :
@@ -763,7 +755,7 @@ def reformat_ffile(infile, outfile, indent_size=3, whitespace=2,
763755
764756 do_indent = True
765757 use_same_line = False
766- stream = InputStream (infile )
758+ stream = InputStream (infile , orig_filename )
767759 skip_blank = False
768760 in_manual_block = False
769761
@@ -800,8 +792,8 @@ def reformat_ffile(infile, outfile, indent_size=3, whitespace=2,
800792 else :
801793 in_manual_block = False
802794 if not valid_directive :
803- raise FortranSyntaxError (orig_filename , stream .line_nr ,
804- FORMATTER_ERROR_MESSAGE )
795+ raise FPrettifyParseError (orig_filename , stream .line_nr ,
796+ FORMATTER_ERROR_MESSAGE )
805797
806798 indent = [0 ] * len (lines )
807799
0 commit comments