-
Notifications
You must be signed in to change notification settings - Fork 69
/
__init__.py
1419 lines (1155 loc) · 52 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- 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/>.
###############################################################################
"""
Impose white space conventions and indentation based on scopes / subunits
normalization of white spaces supported for following operators:
- relational operators:
.EQ. .NE. .LT. .LE. .GT. .GE.
== /= < <= > >=
- logical operators:
.AND. .OR. .EQV. .NEQV.
.NOT.
- bracket delimiters
- commas and semicolons:
- arithmetic operators:
* / ** + -
- other operators:
% - (sign) = (function argument)
= (assignment) => (pointer assignment)
supported criteria for alignment / indentation:
Fortran lines:
- if, else, endif
- do, enddo
- select case, case, end select
- subroutine, end subroutine
- function, end function
- module, end module
- program, end program
- interface, end interface
- type, end type
Actual lines (parts of Fortran lines separated by linebreaks):
- bracket delimiters (.), (/./), and [.]
- assignments by value = and pointer =>.
LIMITATIONS
- assumes that all subunits are explicitly ended within same file,
no treatment of #include statements
- can not deal with f77 constructs (files are ignored)
FIXME's
- internal errors should not happen
- wrap regular expression parser. This allows to extend parser by constructs
that are not regular expressions (and support e.g. forall construct).
- strip whitespaces once and for all and then assume no trailing / leading
whitespaces
- open files only when needed
"""
from __future__ import (absolute_import, division,
print_function, unicode_literals)
import re
import sys
import logging
import os
import io
import argparse
# allow for unicode for stdin / stdout, it's a mess
try:
# python 3
sys.stdin = io.TextIOWrapper(
sys.stdin.detach(), encoding='UTF-8', line_buffering=True)
sys.stdout = io.TextIOWrapper(
sys.stdout.detach(), encoding='UTF-8', line_buffering=True)
except AttributeError:
# python 2
import codecs
utf8_reader = codecs.getreader('UTF-8')
utf8_writer = codecs.getwriter('UTF-8')
sys.stdin = utf8_reader(sys.stdin)
sys.stdout = utf8_writer(sys.stdout)
from .fparse_utils import (VAR_DECL_RE, OMP_RE, OMP_DIR_RE,
InputStream, CharFilter,
FprettifyException, FprettifyParseException, FprettifyInternalException,
CPP_RE, NOTFORTRAN_LINE_RE, FYPP_LINE_RE, RE_FLAGS, STR_OPEN_RE)
# recognize fortran files by extension
FORTRAN_EXTENSIONS = [".f", ".for", ".ftn",
".f90", ".f95", ".f03", ".fpp"]
FORTRAN_EXTENSIONS += [_.upper() for _ in FORTRAN_EXTENSIONS]
# constants, mostly regular expressions:
FORMATTER_ERROR_MESSAGE = (" Wrong usage of formatting-specific directives"
" '&', '!&', '!&<' or '!&>'.")
LINESPLIT_MESSAGE = ("auto indentation failed due to 132 chars limit, "
"line should be split")
EOL_STR = r"\s*;?\s*$" # end of fortran line
EOL_SC = r"\s*;\s*$" # whether line is ended with semicolon
SOL_STR = r"^\s*" # start of fortran line
STATEMENT_LABEL_RE = re.compile(r"^\s*(\d+)", RE_FLAGS)
# regular expressions for parsing statements that start, continue or end a
# subunit:
IF_RE = re.compile(
SOL_STR + r"(\w+\s*:)?\s*IF\s*\(.*\)\s*THEN" + EOL_STR, RE_FLAGS)
ELSE_RE = re.compile(
SOL_STR + r"ELSE(\s*IF\s*\(.*\)\s*THEN)?" + EOL_STR, RE_FLAGS)
ENDIF_RE = re.compile(SOL_STR + r"END\s*IF(\s+\w+)?" + EOL_STR, RE_FLAGS)
DO_RE = re.compile(SOL_STR + r"(\w+\s*:)?\s*DO(" + EOL_STR + r"|\s)", RE_FLAGS)
ENDDO_RE = re.compile(SOL_STR + r"END\s*DO(\s+\w+)?" + EOL_STR, RE_FLAGS)
SELCASE_RE = re.compile(
SOL_STR + r"SELECT\s*(CASE|TYPE)\s*\(.*\)" + EOL_STR, RE_FLAGS)
CASE_RE = re.compile(
SOL_STR + r"((CASE|TYPE\s+IS|CLASS\s+IS)\s*(\(.*\)|DEFAULT)|CLASS\s+DEFAULT)" + EOL_STR, RE_FLAGS)
ENDSEL_RE = re.compile(SOL_STR + r"END\s*SELECT" + EOL_STR, RE_FLAGS)
ASSOCIATE_RE = re.compile(SOL_STR + r"ASSOCIATE\s*\(.*\)" + EOL_STR, RE_FLAGS)
ENDASSOCIATE_RE = re.compile(SOL_STR + r"END\s*ASSOCIATE" + EOL_STR, RE_FLAGS)
BLK_RE = re.compile(SOL_STR + r"(\w+\s*:)?\s*BLOCK" + EOL_STR, RE_FLAGS)
ENDBLK_RE = re.compile(SOL_STR + r"END\s*BLOCK(\s+\w+)?" + EOL_STR, RE_FLAGS)
SUBR_RE = re.compile(
r"^([^\"']* )?SUBROUTINE\s+\w+\s*(\(.*\))?" + EOL_STR, RE_FLAGS)
ENDSUBR_RE = re.compile(
SOL_STR + r"END\s*SUBROUTINE(\s+\w+)?" + EOL_STR, RE_FLAGS)
FCT_RE = re.compile(
r"^([^\"']* )?FUNCTION\s+\w+\s*(\(.*\))?(\s*RESULT\s*\(\w+\))?" + EOL_STR,
RE_FLAGS)
ENDFCT_RE = re.compile(
SOL_STR + r"END\s*FUNCTION(\s+\w+)?" + EOL_STR, RE_FLAGS)
MOD_RE = re.compile(SOL_STR + r"MODULE\s+\w+" + EOL_STR, RE_FLAGS)
ENDMOD_RE = re.compile(SOL_STR + r"END\s*MODULE(\s+\w+)?" + EOL_STR, RE_FLAGS)
TYPE_RE = re.compile(
SOL_STR +
r"TYPE(\s*,\s*(BIND\s*\(\s*C\s*\)|EXTENDS\s*\(.*\)|ABSTRACT))?(\s*,\s*(PUBLIC|PRIVATE))?(\s*::\s*|\s+)\w+" + EOL_STR,
RE_FLAGS)
ENDTYPE_RE = re.compile(SOL_STR + r"END\s*TYPE(\s+\w+)?" + EOL_STR, RE_FLAGS)
PROG_RE = re.compile(SOL_STR + r"PROGRAM\s+\w+" + EOL_STR, RE_FLAGS)
ENDPROG_RE = re.compile(
SOL_STR + r"END\s*PROGRAM(\s+\w+)?" + EOL_STR, RE_FLAGS)
INTERFACE_RE = re.compile(
r"^([^\"']* )?INTERFACE(\s+\w+|\s+(OPERATOR|ASSIGNMENT)\s*\(.*\))?" + EOL_STR, RE_FLAGS)
ENDINTERFACE_RE = re.compile(
SOL_STR + r"END\s*INTERFACE(\s+\w+)?" + EOL_STR, RE_FLAGS)
CONTAINS_RE = re.compile(SOL_STR + r"CONTAINS" + EOL_STR, RE_FLAGS)
ENUM_RE = re.compile(
SOL_STR + r"ENUM(\s*,\s*(BIND\s*\(\s*C\s*\)))?(\s*::\s*|\s+)\w+" + EOL_STR,
RE_FLAGS)
ENDENUM_RE = re.compile(SOL_STR + r"END\s*ENUM(\s+\w+)?" + EOL_STR, RE_FLAGS)
ENDANY_RE = re.compile(SOL_STR + r"END" + EOL_STR, RE_FLAGS)
PRIVATE_RE = re.compile(SOL_STR + r"PRIVATE\s*::", RE_FLAGS)
PUBLIC_RE = re.compile(SOL_STR + r"PUBLIC\s*::", RE_FLAGS)
# intrinsic statements with parenthesis notation that are not functions
INTR_STMTS_PAR = (r"(ALLOCATE|DEALLOCATE|REWIND|BACKSPACE|INQUIRE|"
r"OPEN|CLOSE|READ|WRITE|"
r"FORALL|WHERE|ASSOCIATE|NULLIFY)")
# regular expressions for parsing linebreaks
LINEBREAK_STR = r"(&)[\s]*(?:!.*)?$"
# regular expressions for parsing operators
# Note: +/- in real literals and sign operator is ignored
PLUSMINUS_RE = re.compile(
r"(?<=[\w\)\]])(?<![\d\.][de])\s*(\+|-)\s*", RE_FLAGS)
# Note: ** or // (or any multiples of * or /) are ignored
MULTDIV_RE = re.compile(
r"(?<=[\w\)\]])\s*((?<!\*)\*(?!\*)|(?<!/)/(?!/))(?=[\s\w\(])", RE_FLAGS)
REL_OP_RE = re.compile(
r"(?<!\()\s*(\.(?:EQ|NE|LT|LE|GT|GE)\.|(?:==|\/=|<(?!=)|<=|(?<!=)>(?!=)|>=))\s*(?!\))",
RE_FLAGS)
LOG_OP_RE = re.compile(r"\s*(\.(?:AND|OR|EQV|NEQV)\.)\s*", RE_FLAGS)
PRINT_RE = re.compile(r"(?:(?<=\bPRINT)|(?<=\bREAD))\s*(\*,?)\s*", RE_FLAGS)
# regular expressions for parsing delimiters
DEL_OPEN_STR = r"(\(\/?|\[)"
DEL_OPEN_RE = re.compile(r"^" + DEL_OPEN_STR, RE_FLAGS)
DEL_CLOSE_STR = r"(\/?\)|\])"
DEL_CLOSE_RE = re.compile(r"^" + DEL_CLOSE_STR, RE_FLAGS)
# empty line regex
EMPTY_RE = re.compile(SOL_STR + r"$", RE_FLAGS)
# two-sided operators
LR_OPS_RE = [REL_OP_RE, LOG_OP_RE, PLUSMINUS_RE, MULTDIV_RE, PRINT_RE]
USE_RE = re.compile(
SOL_STR + "USE(\s+|(,.+?)?::\s*)\w+?((,.+?=>.+?)+|,\s*only\s*:.+?)?$" + EOL_STR, RE_FLAGS)
# markups to deactivate formatter
NO_ALIGN_RE = re.compile(SOL_STR + r"&\s*[^\s*]+")
# combine regex that define subunits
NEW_SCOPE_RE = [IF_RE, DO_RE, SELCASE_RE, SUBR_RE,
FCT_RE, MOD_RE, PROG_RE, INTERFACE_RE, TYPE_RE, ENUM_RE, ASSOCIATE_RE, None, BLK_RE]
CONTINUE_SCOPE_RE = [ELSE_RE, None, CASE_RE, CONTAINS_RE,
CONTAINS_RE, CONTAINS_RE, CONTAINS_RE, None, CONTAINS_RE, None, None, None, None]
END_SCOPE_RE = [ENDIF_RE, ENDDO_RE, ENDSEL_RE, ENDSUBR_RE,
ENDFCT_RE, ENDMOD_RE, ENDPROG_RE, ENDINTERFACE_RE, ENDTYPE_RE, ENDENUM_RE, ENDASSOCIATE_RE, ENDANY_RE, ENDBLK_RE]
# match namelist names
NML_RE = re.compile(r"(/\w+/)", RE_FLAGS)
# find namelists and data statements
NML_STMT_RE = re.compile(SOL_STR + r"NAMELIST.*/.*/", RE_FLAGS)
DATA_STMT_RE = re.compile(SOL_STR + r"DATA\s+\w", RE_FLAGS)
class F90Indenter(object):
"""
Parses encapsulation of subunits / scopes line by line
and updates the indentation.
"""
def __init__(self, first_indent, rel_indent, filename):
# scopes / subunits:
self._scope_storage = []
# indents for all fortran lines:
self._indent_storage = []
# indents of actual lines of current fortran line
self._line_indents = []
self._filename = filename
self._aligner = F90Aligner(filename)
# no lines have been processed yet:
self._initial = True
# implicit scopes: we define implicit scopes, as many as match
# first_indent and rel_indent. This allows for, e.g., a properly
# indented "END FUNCTION" without matching "FUNCTION" statement:
if rel_indent > 0:
for n_impl in range(first_indent % rel_indent, first_indent + 1, rel_indent):
self._indent_storage += [n_impl]
if not self._indent_storage:
self._indent_storage = [0]
def process_lines_of_fline(self, f_line, lines, rel_ind, rel_ind_con,
line_nr, manual_lines_indent=None):
"""
Process all lines that belong to a Fortran line `f_line`.
Impose a relative indent of `rel_ind` for current Fortran line,
and `rel_ind_con` for line continuation.
By default line continuations are auto-aligned by F90Aligner
:param f_line: fortran line
:param lines: actual lines belonging to f_line
:param rel_ind: relative scope indent size for this line
:rel_ind_con: relative continuation indent size for this line
:line_nr: line number
:manual_lines_indent: don't use F90Aligner but manually impose
indents for continuations
"""
self._line_indents = [0] * len(lines)
br_indent_list = [0] * len(lines)
# local variables to avoid self hassle:
line_indents = self._line_indents
scopes = self._scope_storage
indents = self._indent_storage
filename = self._filename
# check statements that start new scope
is_new = False
valid_new = False
f_filter = CharFilter(f_line)
f_line_filtered = f_filter.filter_all()
for new_n, newre in enumerate(NEW_SCOPE_RE):
if newre and newre.search(f_line_filtered) and \
not END_SCOPE_RE[new_n].search(f_line_filtered):
what_new = new_n
is_new = True
valid_new = True
scopes.append(what_new)
log_message("{}: {}".format(what_new, f_line),
"debug", filename, line_nr)
# check statements that continue scope
is_con = False
valid_con = False
for con_n, conre in enumerate(CONTINUE_SCOPE_RE):
if conre and conre.search(f_line_filtered):
what_con = con_n
is_con = True
if len(scopes) > 0:
what = scopes[-1]
if what == what_con:
valid_con = True
log_message("{}: {}".format(
what_con, f_line), "debug", filename, line_nr)
# check statements that end scope
is_end = False
valid_end = False
for end_n, endre in enumerate(END_SCOPE_RE):
if endre and endre.search(f_line_filtered):
what_end = end_n
is_end = True
if len(scopes) > 0:
what = scopes.pop()
if what == what_end:
valid_end = True
log_message("{}: {}".format(
what_end, f_line), "debug", filename, line_nr)
# deal with line breaks
if not manual_lines_indent:
self._aligner.process_lines_of_fline(
f_line, lines, rel_ind_con, line_nr)
br_indent_list = self._aligner.get_lines_indent()
else:
br_indent_list = manual_lines_indent
for pos in range(0, len(lines) - 1):
line_indents[pos + 1] = br_indent_list[pos + 1]
if is_new:
if not valid_new:
log_message('invalid scope opening statement',
"info", filename, line_nr)
line_indents = [ind + indents[-1] for ind in line_indents]
indents.append(rel_ind + indents[-1])
elif is_con or is_end:
valid = valid_con if is_con else valid_end
if not valid:
log_message('invalid scope closing statement',
"info", filename, line_nr)
try:
line_indents = [ind + indents[-2 + self._initial]
for ind in line_indents]
except IndexError:
assert not valid
if is_end:
if len(indents) > 1:
indents.pop()
else:
indents[-1] = 0
else:
line_indents = [ind + indents[-1] for ind in line_indents]
# we have processed first line:
self._initial = False
# reassigning self.* to the updated variables
self._line_indents = line_indents
self._scope_storage = scopes
self._indent_storage = indents
def get_fline_indent(self):
"""after processing, retrieve the indentation of the full Fortran line."""
return self._indent_storage[-1]
def get_lines_indent(self):
"""after processing, retrieve the indents of all line parts."""
return self._line_indents
class F90Aligner(object):
"""
Alignment of continuations of a broken line,
based on the following heuristics:
if line break in brackets
We are parsing the level of nesting
and align to most inner bracket delimiter.
else if line is an assignment
alignment to '=' or '=>'.
note: assignment operator recognized as any '=' that is not
part of another operator and that is not enclosed in bracket
else if line is a declaration
alignment to '::'
else
default indent
"""
def __init__(self, filename):
self._filename = filename
self.__init_line(0)
def __init_line(self, line_nr):
"""initialization before processing new line"""
self._line_nr = line_nr
self._line_indents = [0]
self._level = 0
self._br_indent_list = [0]
def process_lines_of_fline(self, f_line, lines, rel_ind, line_nr):
"""
process all lines that belong to a Fortran line `f_line`,
`rel_ind` is the relative indentation size.
"""
self.__init_line(line_nr)
is_decl = VAR_DECL_RE.search(f_line) or PUBLIC_RE.search(f_line) or PRIVATE_RE.match(f_line)
for pos, line in enumerate(lines):
self.__align_line_continuations(
line, is_decl, rel_ind, self._line_nr + pos)
if pos + 1 < len(lines):
self._line_indents.append(self._br_indent_list[-1])
if len(self._br_indent_list) > 2 or self._level:
log_message('unpaired bracket delimiters',
"info", self._filename, self._line_nr)
def get_lines_indent(self):
"""after processing, retrieve the indents of all line parts."""
return self._line_indents
def __align_line_continuations(self, line, is_decl, indent_size, line_nr):
"""align continuation lines."""
indent_list = self._br_indent_list
level = self._level
filename = self._filename
pos_eq = 0
pos_ldelim = []
pos_rdelim = []
ldelim = []
rdelim = []
# find delimiters that are not ended on this line.
# find proper alignment to most inner delimiter
# or alignment to assignment operator
rel_ind = indent_list[-1] # indentation of prev. line
end_of_delim = -1
for pos, char in CharFilter(line):
what_del_open = None
what_del_close = None
if pos > end_of_delim:
[what_del_open, what_del_close] = get_curr_delim(line, pos)
if what_del_open:
what_del_open = what_del_open.group()
end_of_delim = pos + len(what_del_open) - 1
level += 1
indent_list.append(pos + len(what_del_open) + rel_ind)
pos_ldelim.append(pos)
ldelim.append(what_del_open)
if what_del_close:
what_del_close = what_del_close.group()
end_of_delim = pos + len(what_del_close) - 1
if level > 0:
level += -1
indent_list.pop()
else:
log_message('unpaired bracket delimiters',
"info", filename, line_nr)
if pos_ldelim:
pos_ldelim.pop()
what_del_open = ldelim.pop()
valid = False
if what_del_open == r"(":
valid = what_del_close == r")"
if what_del_open == r"(/":
valid = what_del_close == r"/)"
if what_del_open == r"[":
valid = what_del_close == r"]"
if not valid:
log_message('unpaired bracket delimiters',
"info", filename, line_nr)
else:
pos_rdelim.append(pos)
rdelim.append(what_del_close)
if char == ',' and not level and pos_eq > 0:
# a top level comma removes previous alignment position.
# (see issue #11)
pos_eq = 0
indent_list.pop()
if not level and not is_decl and char == '=' and not REL_OP_RE.search(
line[max(0, pos - 1):min(pos + 2, len(line))]):
# should only have one assignment per line!
if pos_eq > 0:
raise FprettifyInternalException(
"found more than one assignment in the same Fortran line", filename, line_nr)
is_pointer = line[pos + 1] == '>'
pos_eq = pos + 1
# don't align if assignment operator directly before
# line break
if not re.search(r"=>?\s*" + LINEBREAK_STR, line,
RE_FLAGS):
indent_list.append(
pos_eq + 1 + is_pointer + indent_list[-1])
elif is_decl and line[pos:pos + 2] == '::' and not re.search(r"::\s*" + LINEBREAK_STR, line, RE_FLAGS):
indent_list.append(pos + 3 + indent_list[-1])
# Don't align if delimiter opening directly before line break
if level and re.search(DEL_OPEN_STR + r"\s*" + LINEBREAK_STR, line,
RE_FLAGS):
if len(indent_list) > 1:
indent_list[-1] = indent_list[-2]
else:
indent_list[-1] = 0
if not indent_list[-1]:
indent_list[-1] = indent_size
self._level = level
def inspect_ffile_format(infile, indent_size, strict_indent, orig_filename=None):
"""
Determine indentation by inspecting original Fortran file.
This is mainly for finding aligned blocks of DO/IF statements.
Also check if it has f77 constructs.
:param infile: open file
:param indent_size: the default indent size
:orig_filename: filename used for messages
:returns: [ target indent sizes for each line,
indent of first line (offset) ]
"""
if not orig_filename:
orig_filename = infile.name
num_labels = False
indents = []
stream = InputStream(infile, orig_filename)
prev_offset = 0
first_indent = -1
while 1:
f_line, _, lines = stream.next_fortran_line()
if not lines:
break
f_line, lines, label = preprocess_labels(f_line, lines)
offset = len(lines[0]) - len(lines[0].lstrip(' '))
if f_line.strip() and first_indent == -1:
first_indent = offset
indents.append(offset - prev_offset)
# don't impose indentation for blocked do/if constructs:
if (IF_RE.search(f_line) or DO_RE.search(f_line)):
if (prev_offset != offset or strict_indent):
indents[-1] = indent_size
else:
indents[-1] = indent_size
prev_offset = offset
return indents, first_indent
def format_single_fline(f_line, whitespace, linebreak_pos, ampersand_sep,
filename, line_nr, auto_format=True):
"""
format a single Fortran line - imposes white space formatting
and inserts linebreaks.
Takes a logical Fortran line `f_line` as input as well as the positions
of the linebreaks (`linebreak_pos`), and the number of
separating whitespace characters before ampersand (`ampersand_sep`).
`filename` and `line_nr` just for error messages.
The higher `whitespace`, the more white space characters inserted -
whitespace = 0, 1, 2, 3 are currently supported.
auto formatting can be turned off by setting `auto_format` to False.
"""
# define whether to put whitespaces around operators:
# 0: comma, semicolon
# 1: assignment operators
# 2: relational operators
# 3: logical operators
# 4: arithm. operators plus and minus
# 5: arithm. operators multiply and divide
# 6: print / read statements
# 7: select type components
if whitespace == 0:
spacey = [0, 0, 0, 0, 0, 0, 0, 0]
elif whitespace == 1:
spacey = [1, 1, 1, 1, 0, 0, 1, 0]
elif whitespace == 2:
spacey = [1, 1, 1, 1, 1, 0, 1, 0]
elif whitespace == 3:
spacey = [1, 1, 1, 1, 1, 1, 1, 0]
elif whitespace == 4:
spacey = [1, 1, 1, 1, 1, 1, 1, 1]
else:
raise NotImplementedError("unknown value for whitespace")
line = f_line
line_orig = line
if auto_format:
line = rm_extra_whitespace(line)
line = add_whitespace_charwise(line, spacey, filename, line_nr)
line = add_whitespace_context(line, spacey)
lines_out = split_reformatted_line(
line_orig, linebreak_pos, ampersand_sep, line, filename, line_nr)
return lines_out
def rm_extra_whitespace(line):
"""rm all unneeded whitespace chars, except for declarations"""
line_ftd = ''
pos_prev = -1
pos = -1
for pos, char in CharFilter(line):
is_decl = line[pos:].lstrip().startswith('::') or line[
:pos].rstrip().endswith('::')
if pos > pos_prev + 1: # skipped string
line_ftd = line_ftd + line[pos_prev + 1:pos]
if char == ' ':
# remove double spaces:
if line_ftd and (re.search(r'[\w]', line_ftd[-1]) or is_decl):
line_ftd = line_ftd + char
else:
if (line_ftd and line_ftd[-1] == ' ' and
(not re.search(r'[\w]', char) and not is_decl)):
line_ftd = line_ftd[:-1] # remove spaces except between words
line_ftd = line_ftd + char
pos_prev = pos
line_ftd = line_ftd + line[pos+1:]
return line_ftd
def add_whitespace_charwise(line, spacey, filename, line_nr):
"""add whitespace character wise (no need for context aware parsing)"""
line_ftd = line
pos_eq = []
end_of_delim = -1
level = 0
for pos, char in CharFilter(line):
# offset w.r.t. unformatted line
offset = len(line_ftd) - len(line)
# format delimiters
what_del_open = None
what_del_close = None
if pos > end_of_delim:
[what_del_open, what_del_close] = get_curr_delim(line, pos)
if what_del_open or what_del_close:
sep1 = 0
sep2 = 0
if what_del_open:
delim = what_del_open.group()
else:
delim = what_del_close.group()
lhs = line_ftd[:pos + offset]
rhs = line_ftd[pos + len(delim) + offset:]
# format opening delimiters
if what_del_open:
level += 1 # new scope
# add separating whitespace before opening delimiter
# with some exceptions:
# FIXME: duplication of regex, better to include them into
# INTR_STMTS_PAR
if ((not re.search((r"(" + DEL_OPEN_STR +
r"|[\w\*/=\+\-:])\s*$"),
line[:pos], RE_FLAGS) and
not EMPTY_RE.search(line[:pos])) or
re.search(SOL_STR + r"(\w+\s*:)?(ELSE)?\s*IF\s*$",
line[:pos], RE_FLAGS) or
re.search(SOL_STR + r"(\w+\s*:)?\s*DO\s+WHILE\s*$",
line[:pos], RE_FLAGS) or
re.search(SOL_STR + r"(SELECT)?\s*CASE\s*$",
line[:pos], RE_FLAGS) or
re.search(SOL_STR + r"SELECT\s*TYPE\s*$",
line[:pos], RE_FLAGS) or
re.search(SOL_STR + r"CLASS\s*DEFAULT\s*$",
line[:pos], RE_FLAGS) or
re.search(SOL_STR + r"(TYPE|CLASS)\s+IS\s*$",
line[:pos], RE_FLAGS) or
re.search(r"\b" + INTR_STMTS_PAR + r"\s*$",
line[:pos], RE_FLAGS)):
sep1 = 1
# format closing delimiters
else:
if level > 0:
level += -1 # close scope
else:
log_message('unpaired bracket delimiters',
"info", filename, line_nr)
# add separating whitespace after closing delimiter
# with some exceptions:
if not re.search(r"^\s*(" + DEL_CLOSE_STR + r"|[,%:/\*])",
line[pos + 1:], RE_FLAGS):
sep2 = 1
elif re.search(r"^\s*::", line[pos + 1:], RE_FLAGS):
sep2 = len(rhs) - len(rhs.lstrip(' '))
# where delimiter token ends
end_of_delim = pos + len(delim) - 1
line_ftd = lhs.rstrip(' ') + ' ' * sep1 + \
delim + ' ' * sep2 + rhs.lstrip(' ')
# format commas and semicolons
if char in [',', ';']:
lhs = line_ftd[:pos + offset]
rhs = line_ftd[pos + 1 + offset:]
line_ftd = lhs.rstrip(' ') + char + ' ' * \
spacey[0] + rhs.lstrip(' ')
line_ftd = line_ftd.rstrip(' ')
# format type selector %
if char == "%":
lhs = line_ftd[:pos + offset]
rhs = line_ftd[pos + 1 + offset:]
line_ftd = lhs.rstrip(' ') \
+ ' ' * spacey[7] \
+ char \
+ ' ' * spacey[7] \
+ rhs.lstrip(' ')
line_ftd = line_ftd.rstrip(' ')
# format .NOT.
if re.search(r"^\.NOT\.", line[pos:pos + 5], RE_FLAGS):
lhs = line_ftd[:pos + offset]
rhs = line_ftd[pos + 5 + offset:]
line_ftd = lhs.rstrip(
' ') + line[pos:pos + 5] + ' ' * spacey[3] + rhs.lstrip(' ')
# strip whitespaces from '=' and prepare assignment operator
# formatting:
if char == '=' and not REL_OP_RE.search(line[pos - 1:pos + 2]):
lhs = line_ftd[:pos + offset]
rhs = line_ftd[pos + 1 + offset:]
line_ftd = lhs.rstrip(' ') + '=' + rhs.lstrip(' ')
if not level: # remember position of assignment operator
pos_eq.append(len(lhs.rstrip(' ')))
line = line_ftd
for pos in pos_eq:
offset = len(line_ftd) - len(line)
is_pointer = line[pos + 1] == '>'
lhs = line_ftd[:pos + offset]
rhs = line_ftd[pos + 1 + is_pointer + offset:]
if is_pointer:
assign_op = '=>' # pointer assignment
else:
assign_op = '=' # assignment
line_ftd = (lhs.rstrip(' ') +
' ' * spacey[1] + assign_op +
' ' * spacey[1] + rhs.lstrip(' '))
# offset w.r.t. unformatted line
if level != 0:
log_message('unpaired bracket delimiters', "info", filename, line_nr)
return line_ftd
def add_whitespace_context(line, spacey):
"""
for context aware whitespace formatting we extract line parts that are
not comments or strings in order to be able to apply a context aware regex.
"""
pos_prev = -1
pos = -1
line_parts = ['']
for pos, char in CharFilter(line):
if pos > pos_prev + 1: # skipped string
line_parts.append(line[pos_prev + 1:pos].strip()) # append string
line_parts.append('')
line_parts[-1] += char
pos_prev = pos
if pos + 1 < len(line):
line_parts.append(line[pos + 1:])
# format namelists with spaces around /
if NML_STMT_RE.match(line):
for pos, part in enumerate(line_parts):
# exclude comments, strings:
if not STR_OPEN_RE.match(part):
partsplit = NML_RE.split(part)
line_parts[pos] = (' '.join(partsplit))
# Two-sided operators
for n_op, lr_re in enumerate(LR_OPS_RE):
for pos, part in enumerate(line_parts):
# exclude comments, strings:
if not STR_OPEN_RE.match(part):
# also exclude / if we see a namelist and data statement
if not ( NML_STMT_RE.match(line) or DATA_STMT_RE.match(line) ):
partsplit = lr_re.split(part)
line_parts[pos] = (' ' * spacey[n_op + 2]).join(partsplit)
line = ''.join(line_parts)
# format ':' for labels and use only statements
for newre in NEW_SCOPE_RE[0:2]:
if newre.search(line) and re.search(SOL_STR + r"\w+\s*:", line):
line = ': '.join(_.strip() for _ in line.split(':', 1))
if USE_RE.search(line):
line = re.sub(r'(only)\s*:\s*', r'\g<1>:' + ' ' *
spacey[0], line, flags=RE_FLAGS)
return line
def split_reformatted_line(line_orig, linebreak_pos_orig, ampersand_sep, line, filename, line_nr):
"""
Infer linebreak positions of formatted line from linebreak positions in
original line and split line.
"""
# shift line break positions from original to reformatted line
pos_new = 0
pos_old = 0
linebreak_pos_orig.sort(reverse=True)
linebreak_pos_ftd = []
while 1:
if pos_new == len(line) or pos_old == len(line_orig):
break
if line[pos_new] != line_orig[pos_old]:
raise FprettifyInternalException(
"failed at finding line break position", filename, line_nr)
if linebreak_pos_orig and pos_old > linebreak_pos_orig[-1]:
linebreak_pos_orig.pop()
linebreak_pos_ftd.append(pos_new)
continue
pos_new += 1
while pos_new < len(line) and line[pos_new] == ' ':
pos_new += 1
pos_old += 1
while pos_old < len(line_orig) and line_orig[pos_old] == ' ':
pos_old += 1
linebreak_pos_ftd.insert(0, 0)
# We split line into parts and we insert ampersands at line end, but not
# for empty lines and comment lines
lines_split = [(line[l:r].rstrip(' ') +
' ' * ampersand_sep[pos] + '&' * min(1, r - l))
for pos, (l, r) in enumerate(zip(linebreak_pos_ftd[0:-1],
linebreak_pos_ftd[1:]))]
lines_split.append(line[linebreak_pos_ftd[-1]:])
return lines_split
def reformat_inplace(filename, stdout=False, **kwargs): # pragma: no cover
"""reformat a file in place."""
if filename == '-':
infile = io.StringIO()
infile.write(sys.stdin.read())
else:
infile = io.open(filename, 'r', encoding='utf-8')
newfile = io.StringIO()
reformat_ffile(infile=infile, outfile=newfile,
orig_filename=filename, **kwargs)
if stdout:
sys.stdout.write(newfile.getvalue())
else:
outfile = io.open(filename, 'w', encoding='utf-8')
outfile.write(newfile.getvalue())
def reformat_ffile(infile, outfile, impose_indent=True, indent_size=3, strict_indent=False, impose_whitespace=True,
whitespace=2, strip_comments=False, orig_filename=None):
"""main method to be invoked for formatting a Fortran file."""
if not orig_filename:
orig_filename = infile.name
infile.seek(0)
req_indents, first_indent = inspect_ffile_format(
infile, indent_size, strict_indent, orig_filename)
infile.seek(0)
# initialization
# special cases for indentation:
# indent_special = 0: parse syntax and impose indent
# indent_special = 1: no indentation
# indent_special = 2: use indent from previous line
# indent_special = 3: take indent from input file (leave as is)
indent_special = 0
if impose_indent:
indenter = F90Indenter(first_indent, indent_size, orig_filename)
else:
indent_special = 3
nfl = 0 # fortran line counter
use_same_line = False
stream = InputStream(infile, orig_filename)
skip_blank = False
in_format_off_block = False
while 1:
f_line, comments, lines = stream.next_fortran_line()
if not lines:
break
nfl += 1
orig_lines = lines
if indent_special != 3:
indent = [0] * len(lines)
else:
indent = [len(l) - len((l.lstrip(' ')).lstrip('&')) for l in lines]
comment_lines = format_comments(lines, comments, strip_comments)
auto_align, auto_format, in_format_off_block = parse_fprettify_directives(
lines, comment_lines, in_format_off_block, orig_filename, stream.line_nr)
f_line, lines, is_omp, is_omp_conditional = preprocess_omp(
f_line, lines)
f_line, lines, label = preprocess_labels(f_line, lines)
lines, do_format, prev_indent, is_blank, is_fypp = preprocess_line(
f_line, lines, comments, orig_filename, stream.line_nr)
if is_fypp:
indent_special = 3
if prev_indent and indent_special == 0:
indent_special = 2
if is_blank and skip_blank:
continue
if (not do_format):
if indent_special == 2:
# inherit indent from previous line
indent[:] = [indenter.get_fline_indent()]*len(indent)
elif indent_special == 0:
indent_special = 1
else:
if not auto_align:
manual_lines_indent = get_manual_alignment(lines)
else:
manual_lines_indent = []
lines, pre_ampersand, ampersand_sep = remove_pre_ampersands(
lines, orig_filename, stream.line_nr)
linebreak_pos = get_linebreak_pos(lines)
f_line = f_line.strip(' ')