Skip to content

Commit c44b6a7

Browse files
committed
further improvements on error handling
1 parent 268ccac commit c44b6a7

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

fparse_utils.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@
99
flags=re.IGNORECASE)
1010

1111
# FIXME bad ass regex!
12-
VAR_DECL_RE = re.compile(r" *(?P<type>integer(?: *\* *[0-9]+)?|logical|character(?: *\* *[0-9]+)?|real(?: *\* *[0-9]+)?|complex(?: *\* *[0-9]+)?|type) *(?P<parameters>\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))? *(?P<attributes>(?: *, *[a-zA-Z_0-9]+(?: *\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))?)+)? *(?P<dpnt>::)?(?P<vars>[^\n]+)\n?", re.IGNORECASE)
12+
VAR_DECL_RE = re.compile(
13+
r" *(?P<type>integer(?: *\* *[0-9]+)?|logical|character(?: *\* *[0-9]+)?|real(?: *\* *[0-9]+)?|complex(?: *\* *[0-9]+)?|type) *(?P<parameters>\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))? *(?P<attributes>(?: *, *[a-zA-Z_0-9]+(?: *\((?:[^()]+|\((?:[^()]+|\([^()]*\))*\))*\))?)+)? *(?P<dpnt>::)?(?P<vars>[^\n]+)\n?", re.IGNORECASE)
1314

1415
OMP_DIR_RE = re.compile(r"^\s*(!\$omp)", re.IGNORECASE)
1516
OMP_RE = re.compile(r"^\s*(!\$)", re.IGNORECASE)
1617

1718

19+
class FPrettifyParseError(Exception):
20+
"""Exception for unparseable Fortran code"""
21+
22+
def __init__(self, filename, line_nr,
23+
msg=("Parser error - "
24+
"possibly due to incorrect Fortran syntax.")):
25+
super(FPrettifyParseError, self).__init__('{}:{}:{}'.format(
26+
filename, line_nr, msg))
27+
28+
1829
class CharFilter(object):
1930
"""
2031
An iterator to wrap the iterator returned by `enumerate`
@@ -70,10 +81,13 @@ class InputStream(object):
7081
Class to read logical Fortran lines from a Fortran file.
7182
"""
7283

73-
def __init__(self, infile):
84+
def __init__(self, infile, orig_filename=None):
85+
if not orig_filename:
86+
orig_filename = infile.name
7487
self.line_buffer = deque([])
7588
self.infile = infile
7689
self.line_nr = 0
90+
self.filename = orig_filename
7791

7892
def next_fortran_line(self):
7993
"""Reads a group of connected lines (connected with &, separated by newline or semicolon)
@@ -126,11 +140,12 @@ def next_fortran_line(self):
126140
# FIXME: does not handle line continuation of
127141
# omp conditional fortran statements
128142
# starting with an ampersand.
129-
raise SyntaxError("unexpected line format:" + repr(line))
143+
raise FPrettifyParseError(
144+
self.filename, self.line_nr, "unexpected line format:")
130145
if match.group("preprocessor"):
131146
if len(lines) > 1:
132-
raise SyntaxError(
133-
"continuation to a preprocessor line not supported " + repr(line))
147+
raise FPrettifyParseError(self.filename, self.line_nr,
148+
"continuation to a preprocessor line not supported")
134149
comments.append(line)
135150
break
136151
core_att = match.group("core")
@@ -151,5 +166,3 @@ def next_fortran_line(self):
151166
if not continuation:
152167
break
153168
return (joined_line, comments, lines)
154-
155-

fprettify.py

+18-26
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
import os
4444
import tempfile
4545
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)
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-
172162
class 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

Comments
 (0)