prettify: update to fprettify v0.3.5

This commit is contained in:
Tiziano Müller 2019-09-18 15:35:25 +02:00 committed by Tiziano Müller
parent e1a59529db
commit 28c3246259
6 changed files with 32 additions and 1554 deletions

4
.gitmodules vendored
View file

@ -2,3 +2,7 @@
path = exts/dbcsr
url = https://github.com/cp2k/dbcsr
branch = master
[submodule "tools/prettify/fprettify"]
path = tools/prettify/fprettify
url = https://github.com/pseewald/fprettify.git
branch = master

@ -0,0 +1 @@
Subproject commit 2b2801bf91dd651c417c232bb891d407bf81085c

File diff suppressed because it is too large Load diff

View file

@ -1,197 +0,0 @@
# -*- coding: utf-8 -*-
###############################################################################
# This file is part of fprettify.
# Copyright (C) 2016-2018 Patrick Seewald, CP2K developers group
#
# fprettify is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# fprettify is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fprettify. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
"""This is a collection of Fortran parsing utilities."""
from __future__ import (absolute_import, division,
print_function, unicode_literals)
import re
from collections import deque
RE_FLAGS = re.IGNORECASE | re.UNICODE
# FIXME bad ass regex!
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_FLAGS)
# FIXME: unify omp regular expressions
OMP_DIR_RE = re.compile(r"^\s*(!\$omp)", RE_FLAGS)
OMP_RE = re.compile(r"^\s*(!\$)", RE_FLAGS)
OMP_SUBS_RE = re.compile(r"^\s*(!\$(omp)?)", RE_FLAGS)
class FprettifyException(Exception):
"""Base class for all custom exceptions"""
def __init__(self, msg, filename, line_nr):
super(FprettifyException, self).__init__(msg)
self.filename = filename
self.line_nr = line_nr
class FprettifyParseException(FprettifyException):
"""Exception for unparseable Fortran code (user's fault)."""
pass
class FprettifyInternalException(FprettifyException):
"""Exception for potential internal errors (fixme's)."""
pass
class CharFilter(object):
"""
An iterator to wrap the iterator returned by `enumerate`
and ignore comments and characters inside strings
"""
def __init__(self, it):
self._it = it
self._instring = ''
def __iter__(self):
return self
def next(self):
""" Python 2 compatibility """
return self.__next__()
def __next__(self):
pos, char = next(self._it)
if not self._instring and char == '!':
raise StopIteration
# detect start/end of a string
if char in ['"', "'"]:
if self._instring == char:
self._instring = ''
elif not self._instring:
self._instring = char
if self._instring:
return self.__next__()
return (pos, char)
class InputStream(object):
"""Class to read logical Fortran lines from a Fortran file."""
def __init__(self, infile, orig_filename=None):
if not orig_filename:
orig_filename = infile.name
self.line_buffer = deque([])
self.infile = infile
self.line_nr = 0
self.filename = orig_filename
self.endpos = deque([])
self.what_omp = deque([])
def next_fortran_line(self):
"""Reads a group of connected lines (connected with &, separated by newline or semicolon)
returns a touple with the joined line, and a list with the original lines.
Doesn't support multiline character constants!
"""
joined_line = ""
comments = []
lines = []
continuation = 0
instring = ''
while 1:
if not self.line_buffer:
line = self.infile.readline().replace("\t", 8 * " ")
self.line_nr += 1
# convert OMP-conditional fortran statements into normal fortran statements
# but remember to convert them back
what_omp = ''
if OMP_SUBS_RE.search(line):
what_omp = OMP_SUBS_RE.search(line).group(1)
line = line.replace(what_omp, '', 1)
line_start = 0
for pos, char in enumerate(line):
if not instring and char in ['!', '#']:
self.endpos.append(pos - 1 - line_start)
self.line_buffer.append(line[line_start:])
self.what_omp.append(what_omp)
break
if char in ['"', "'"]:
if instring == char:
instring = ''
elif not instring:
instring = char
if not instring:
if char == ';' or pos + 1 == len(line):
self.endpos.append(pos - line_start)
self.line_buffer.append(line[line_start:pos + 1])
self.what_omp.append(what_omp)
what_omp = ''
line_start = pos + 1
if instring:
raise FprettifyInternalException(
"multline strings not supported", self.filename, self.line_nr)
if self.line_buffer:
line = self.line_buffer.popleft()
endpos = self.endpos.popleft()
what_omp = self.what_omp.popleft()
if not line:
break
lines.append(what_omp + line)
line_core = line[:endpos + 1]
try:
if line[endpos + 1] in ['!', '#']:
line_comments = line[endpos + 1:]
else:
line_comments = ''
except IndexError:
line_comments = ''
if line_core:
newline = (line_core[-1] == '\n')
else:
newline = False
line_core = line_core.strip()
if line_core:
continuation = 0
if line_core.endswith('&'):
continuation = 1
line_core = line_core.strip('&')
comments.append(line_comments.rstrip('\n'))
if joined_line.strip():
joined_line = joined_line.rstrip(
'\n') + line_core + '\n' * newline
else:
joined_line = what_omp + line_core + '\n' * newline
if not continuation:
break
return (joined_line, comments, lines)

View file

@ -19,6 +19,8 @@ except ImportError:
from prettify_cp2k import normalizeFortranFile
from prettify_cp2k import replacer
sys.path.append(path.join(path.dirname(path.abspath(__file__)), "fprettify"))
from fprettify import reformat_ffile, fparse_utils, log_exception
@ -357,7 +359,7 @@ def main(argv):
)
parser.add_argument("--indent", type=int, default=3)
parser.add_argument("--whitespace", type=int, default=1, choices=range(3))
parser.add_argument("--whitespace", type=int, default=2, choices=range(0, 5))
parser.add_argument("--decl-linelength", type=int, default=100)
parser.add_argument("--decl-offset", type=int, default=50)
parser.add_argument("--backup-dir", type=abspath, default=abspath("preprettify"))

View file

@ -27,7 +27,7 @@ CONTAINS
INTEGER, INTENT(IN) :: r, i, j, k
INTEGER :: l
l = r+i+j+k
l = r + i + j + k
END FUNCTION
FUNCTION &
str_function(a) RESULT(l)
@ -65,7 +65,7 @@ PROGRAM example_prog
! example 1.1
r = 1; i = -2; j = 3; k = 4; l = 5
r2 = 0.0_dp; r3 = 1.0_dp; r4 = 2.0_dp; r5 = 3.0_dp; r6 = 4.0_dp
r1 = -(r2**i*(r3+r5*(-r4)-r6))-2.e+2
r1 = -(r2**i*(r3 + r5*(-r4) - r6)) - 2.e+2
IF (r .EQ. 2 .AND. r <= 5) i = 3
WRITE (*, *) (MERGE(3, 1, i <= 2))
WRITE (*, *) test_function(r, i, j, k)
@ -117,15 +117,15 @@ PROGRAM example_prog
IF (i <= 2) THEN
m = 0
DO WHILE (m < 4)
m = m+1
m = m + 1
DO k = 1, 3
IF (k == 1) l = l+1
IF (k == 1) l = l + 1
END DO
ENDDO
ENDIF
ENDDO do_label
CASE (2)
l = i+j+k
l = i + j + k
END SELECT
ENDDO
@ -138,7 +138,7 @@ PROGRAM example_prog
DO i = 4, 5
DO my_integer = 1, 1
DO j = 1, 2
WRITE (*, *) test_function(m, r, k, l)+i
WRITE (*, *) test_function(m, r, k, l) + i
ENDDO
ENDDO
ENDDO
@ -151,33 +151,33 @@ PROGRAM example_prog
!************************************!
! example 3.1
l = test_function(1, 2, test_function(1, 2, 3, 4), 4)+3*(2+1)
l = test_function(1, 2, test_function(1, 2, 3, 4), 4) + 3*(2 + 1)
l = test_function(1, 2, test_function(1, 2, 3, 4), 4)+ &
3*(2+1)
l = test_function(1, 2, test_function(1, 2, 3, 4), 4) + &
3*(2 + 1)
l = test_function(1, 2, &
test_function(1, 2, 3, 4), 4)+ &
3*(2+1)
test_function(1, 2, 3, 4), 4) + &
3*(2 + 1)
l = test_function(1, 2, &
test_function(1, 2, 3, &
4), 4)+ &
3*(2+1)
4), 4) + &
3*(2 + 1)
! example 3.2
arr = [1, (/3, 4, 5/), 6]+[1, 2, 3, 4, 5]
arr = [1, (/3, 4, 5/), 6] + [1, 2, 3, 4, 5]
arr = [1, (/3, 4, 5/), &
6]+[1, 2, 3, 4, 5]
6] + [1, 2, 3, 4, 5]
arr = [1, (/3, 4, 5/), &
6]+ &
6] + &
[1, 2, 3, 4, 5]
arr = [1, (/3, 4, &
5/), &
6]+ &
6] + &
[1, 2, 3, 4, 5]
! example 3.3
@ -203,7 +203,7 @@ PROGRAM example_prog
DO i = 1, 100; IF (i <= 2) THEN ! comment
DO j = 1, 5
DO k = 1, 3
l = l+1
l = l + 1
! unindented comment
! indented comment
END DO; ENDDO
@ -214,7 +214,7 @@ PROGRAM example_prog
ENDIF
ENDDO
CASE (2)
l = i+j+k
l = i + j + k
END SELECT
ENDDO
@ -227,12 +227,12 @@ PROGRAM example_prog
IF (k == 1) &
l = test_function(1, &
test_function(r=4, i=5, &
j=6, k=test_function(1, 2*(3*(1+1)), str_function(")a!(b['(;=dfe"), &
9)+ &
j=6, k=test_function(1, 2*(3*(1 + 1)), str_function(")a!(b['(;=dfe"), &
9) + &
test_function(1, 2, 3, 4)), 9, 10) &
! test_function(1,2,3,4)),9,10) &
! +13*str_function('') + str_function('"')
+13*str_function('')+str_function('"')
! + 13*str_function('') + str_function('"')
+ 13*str_function('') + str_function('"')
END & ! comment
! comment
DO
@ -241,7 +241,7 @@ PROGRAM example_prog
! example 4.3
arr = [1, (/3, 4, &
5/), &
6]+ &
6] + &
[1, 2, 3, 4, 5]; arr = [1, 2, &
3, 4, 5]