diff --git a/loopy/frontend/fortran/__init__.py b/loopy/frontend/fortran/__init__.py index efdd07e8ab1f0ab32461f67c12920694b8eb6a26..ed228ebc137d8727c7f90b7a5ce618a9bc70fb6c 100644 --- a/loopy/frontend/fortran/__init__.py +++ b/loopy/frontend/fortran/__init__.py @@ -95,11 +95,17 @@ def _extract_loopy_lines(source): raise LoopyError("can't enter loopy block twice") in_loopy_code = True + # Preserves line numbers in loopy code, for debuggability + loopy_lines.append("# "+l) + elif cmt_stripped == "$loopy end": if not in_loopy_code: raise LoopyError("can't leave loopy block twice") in_loopy_code = False + # Preserves line numbers in loopy code, for debuggability + loopy_lines.append("# "+l) + elif in_loopy_code: loopy_lines.append(cmt) @@ -169,7 +175,9 @@ def parse_transformed_fortran(source, free_form=True, strict=True, from loopy.tools import remove_common_indentation transform_code = remove_common_indentation( transform_code, - require_leading_newline=False) + require_leading_newline=False, + ignore_lines_starting_with="#") + print(transform_code) if transform_code_context is None: proc_dict = {} diff --git a/loopy/tools.py b/loopy/tools.py index b3c610dcbcf5e23a1de5746db78d5fedc9d171ed..e734417d6095da768085fa8861c870114f071ec8 100644 --- a/loopy/tools.py +++ b/loopy/tools.py @@ -164,7 +164,8 @@ class PicklableDtype(object): # {{{ remove common indentation -def remove_common_indentation(code, require_leading_newline=True): +def remove_common_indentation(code, require_leading_newline=True, + ignore_lines_starting_with=None, strip_empty_lines=True): if "\n" not in code: return code @@ -175,21 +176,43 @@ def remove_common_indentation(code, require_leading_newline=True): return code lines = code.split("\n") - while lines[0].strip() == "": - lines.pop(0) - while lines[-1].strip() == "": - lines.pop(-1) - - if lines: - base_indent = 0 - while lines[0][base_indent] in " \t": + + if strip_empty_lines: + while lines[0].strip() == "": + lines.pop(0) + while lines[-1].strip() == "": + lines.pop(-1) + + test_line = None + if ignore_lines_starting_with: + for l in lines: + strip_l = l.lstrip() + if (strip_l + and not strip_l.startswith(ignore_lines_starting_with)): + test_line = l + break + + else: + test_line = lines[0] + + base_indent = 0 + if test_line: + while test_line[base_indent] in " \t": base_indent += 1 - for line in lines[1:]: - if line[:base_indent].strip(): - raise ValueError("inconsistent indentation") + new_lines = [] + for line in lines: + if (ignore_lines_starting_with + and line.lstrip().startswith(ignore_lines_starting_with)): + new_lines.append(line) + continue + + if line[:base_indent].strip(): + raise ValueError("inconsistent indentation: '%s'" % line) + + new_lines.append(line[base_indent:]) - return "\n".join(line[base_indent:] for line in lines) + return "\n".join(new_lines) # }}}