43
43
import os
44
44
import tempfile
45
45
import 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 )
48
48
49
49
50
50
# PY2/PY3 compat wrappers:
@@ -159,16 +159,6 @@ def any(iterable):
159
159
ENDFCT_RE , ENDMOD_RE , ENDPROG_RE , ENDINTERFACE_RE , ENDTYPE_RE ]
160
160
161
161
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
-
172
162
class F90Indenter (object ):
173
163
"""
174
164
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,
251
241
252
242
if is_new :
253
243
if not valid_new :
254
- raise FortranSyntaxError (filename , line_nr )
244
+ raise FPrettifyParseError (filename , line_nr )
255
245
else :
256
246
line_indents = [ind + indents [- 1 ] for ind in line_indents ]
257
247
old_ind = indents [- 1 ]
@@ -263,13 +253,13 @@ def process_lines_of_fline(self, f_line, lines, rel_ind, rel_ind_con,
263
253
264
254
elif is_con :
265
255
if not valid_con :
266
- raise FortranSyntaxError (filename , line_nr )
256
+ raise FPrettifyParseError (filename , line_nr )
267
257
else :
268
258
line_indents = [ind + indents [- 2 ] for ind in line_indents ]
269
259
270
260
elif is_end :
271
261
if not valid_end :
272
- raise FortranSyntaxError (filename , line_nr )
262
+ raise FPrettifyParseError (filename , line_nr )
273
263
else :
274
264
line_indents = [ind + indents [- 2 ] for ind in line_indents ]
275
265
indents .pop ()
@@ -337,7 +327,7 @@ def process_lines_of_fline(self, f_line, lines, rel_ind, line_nr):
337
327
self ._line_indents .append (self ._br_indent_list [- 1 ])
338
328
339
329
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 )
341
331
342
332
def get_lines_indent (self ):
343
333
"""
@@ -386,7 +376,7 @@ def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
386
376
level += - 1
387
377
indent_list .pop ()
388
378
if level < 0 :
389
- raise FortranSyntaxError (filename , line_nr )
379
+ raise FPrettifyParseError (filename , line_nr )
390
380
391
381
if pos_ldelim :
392
382
pos_ldelim .pop ()
@@ -399,7 +389,7 @@ def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
399
389
if what_del_open == r"[" :
400
390
valid = what_del_close == r"]"
401
391
if not valid :
402
- raise FortranSyntaxError (filename , line_nr )
392
+ raise FPrettifyParseError (filename , line_nr )
403
393
else :
404
394
pos_rdelim .append (pos )
405
395
rdelim .append (what_del_close )
@@ -408,7 +398,7 @@ def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
408
398
if not REL_OP_RE .match (
409
399
line [max (0 , pos - 1 ):min (pos + 2 , len (line ))]):
410
400
if pos_eq > 0 :
411
- raise FortranSyntaxError (filename , line_nr )
401
+ raise FPrettifyParseError (filename , line_nr )
412
402
is_pointer = line [pos + 1 ] == '>'
413
403
pos_eq = pos + 1
414
404
# don't align if assignment operator directly before
@@ -435,18 +425,20 @@ def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
435
425
self ._level = level
436
426
437
427
438
- def inspect_ffile_format (infile , indent_size ):
428
+ def inspect_ffile_format (infile , indent_size , orig_filename = None ):
439
429
"""
440
430
Determine indentation by inspecting original Fortran file
441
431
(mainly for finding aligned blocks of DO/IF statements).
442
432
Also check if it has f77 constructs.
443
433
"""
434
+ if not orig_filename :
435
+ orig_filename = infile .name
444
436
445
437
adopt = indent_size <= 0
446
438
447
439
is_f90 = True
448
440
indents = []
449
- stream = InputStream (infile )
441
+ stream = InputStream (infile , orig_filename )
450
442
prev_offset = 0
451
443
first_indent = - 1
452
444
while 1 :
@@ -704,7 +696,7 @@ def format_single_fline(f_line, whitespace, linebreak_pos, ampersand_sep,
704
696
lines_out .append (line [linebreak_pos_ftd [- 1 ]:])
705
697
706
698
if level != 0 :
707
- raise FortranSyntaxError (filename , line_nr )
699
+ raise FPrettifyParseError (filename , line_nr )
708
700
709
701
return lines_out
710
702
@@ -750,7 +742,7 @@ def reformat_ffile(infile, outfile, indent_size=3, whitespace=2,
750
742
751
743
infile .seek (0 )
752
744
req_indents , first_indent , is_f90 = inspect_ffile_format (
753
- infile , indent_size )
745
+ infile , indent_size , orig_filename )
754
746
infile .seek (0 )
755
747
756
748
if not is_f90 :
@@ -763,7 +755,7 @@ def reformat_ffile(infile, outfile, indent_size=3, whitespace=2,
763
755
764
756
do_indent = True
765
757
use_same_line = False
766
- stream = InputStream (infile )
758
+ stream = InputStream (infile , orig_filename )
767
759
skip_blank = False
768
760
in_manual_block = False
769
761
@@ -800,8 +792,8 @@ def reformat_ffile(infile, outfile, indent_size=3, whitespace=2,
800
792
else :
801
793
in_manual_block = False
802
794
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 )
805
797
806
798
indent = [0 ] * len (lines )
807
799
0 commit comments