From e8babd1da6e9fb41996639a667301b53a73a04b7 Mon Sep 17 00:00:00 2001 From: QuentinRodier <40471444+QuentinRodier@users.noreply.github.com> Date: Wed, 30 Mar 2022 23:57:12 +0200 Subject: [PATCH] Quentin 30/03/2022: add first try of fortran indentation parser --- mnh_expand/parser_fortran_python.py | 49 +++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 mnh_expand/parser_fortran_python.py diff --git a/mnh_expand/parser_fortran_python.py b/mnh_expand/parser_fortran_python.py new file mode 100644 index 000000000..c4bf4d66b --- /dev/null +++ b/mnh_expand/parser_fortran_python.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Mar 30 22:32:56 2022 + +@author: Quentin +""" + +def count_blank(text): + count=0 + for t in text: + if t == " ": + count=count+1 + else: + break + return count + +def check_indent(n,indent_score,worktext): + nblank=count_blank(worktext) + rawtext=worktext[nblank:] + correcttext=' '*((indent_score)*1) + rawtext + return correcttext + +filein = open('mode_tke_eps_sources.f90','r') +fileout = open('new.F90','w') +contentbyline = filein.readlines() + +ncurrline=0 +indent_score=0 +for i in contentbyline: + #Si la ligne est un commentaire (commence par !) : next (ne pas indenter) + # + # ligne de calcul sans ajout d'indentation (DO, ENDDDO, IF, ENDIF) + if indent_score > 0 and ("DO J" not in i and "ENDDO" not in i): + textwrite=check_indent(ncurrline,indent_score,i) + elif "DO J" in i: + textwrite=check_indent(ncurrline,indent_score,i) + indent_score = indent_score+1 + elif "ENDDO" in i: + indent_score = indent_score-1 + textwrite=check_indent(ncurrline,indent_score,i) + #Ajouter le case IF et ENDIF qui ajoute de lindentation + else: + textwrite=i + ncurrline=ncurrline+1 + fileout.writelines(textwrite) + +fileout.close() +filein.close() + \ No newline at end of file -- GitLab