Salome HOME
bug
[tools/eficas.git] / Traducteur / removemocle.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017   EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 import logging
21 from Traducteur import regles
22 from Traducteur.parseur import FactNode
23 from Traducteur.dictErreurs import ecritErreur
24 from Traducteur.load import jdcSet
25
26 debug=0
27 #debug=1
28 #on n'a qu'un mocle par commande. On peut donc supprimer le mocle sans trop de précautions (a part iterer a l'envers sur les commandes)
29 #avant de supprimer un autre mocle, on remet à jour l'arbre syntaxique (lineno,colno,etc.)
30
31
32 #-----------------------------------------------------------------------
33 def removeMotCle(jdc,command,mocle,ensemble=regles.SansRegle,erreur = 0):
34 #-----------------------------------------------------------------------
35     #on itere sur les commandes a l'envers pour ne pas polluer les numeros de ligne avec les modifications
36     if command not in jdcSet : return
37     boolChange=0
38     commands= jdc.root.childNodes[:]
39     commands.reverse()
40     for c in commands:
41         if c.name != command:continue
42         for mc in c.childNodes:
43             if mc.name != mocle:continue
44             if ensemble.verif(c) == 0 : continue
45             if erreur : ecritErreur((command,mocle),c.lineno)
46             boolChange=1
47             removeMC(jdc,c,mc)
48
49     if boolChange : jdc.reset(jdc.getSource())
50
51 #-------------------------------------------------------
52 def removeMotCleSiRegle(jdc,command,mocle,liste_regles) :
53 #-------------------------------------------------------
54     if command not in jdcSet : return
55     mesRegles=regles.ensembleRegles(liste_regles)
56     removeMotCle(jdc,command,mocle,mesRegles,erreur=0)
57
58 #----------------------------------------------------------------
59 def removeMotCleSiRegleAvecErreur(jdc,command,mocle,liste_regles) :
60 #--------------------------------------------------------------
61     if command not in jdcSet : return
62     mesRegles=regles.ensembleRegles(liste_regles)
63     removeMotCle(jdc,command,mocle,mesRegles,erreur=1)
64
65 #----------------------------------------------------------------
66 def removeMotCleAvecErreur(jdc,command,mocle) :
67 #--------------------------------------------------------------
68     if command not in jdcSet : return
69     removeMotCle(jdc,command,mocle,erreur=1)
70       
71
72 #--------------------------------------------------------------------
73 def removeCommande(jdc,command,ensemble=regles.SansRegle,erreur=0):
74 #--------------------------------------------------------------------
75     if command not in jdcSet : return
76     boolChange=0
77     commands= jdc.root.childNodes[:]
78     commands.reverse()
79     for c in commands:
80         if c.name != command:continue
81         if ensemble.verif(c) == 0 : continue
82         boolChange=1
83         if erreur : ecritErreur((command,),c.lineno)
84         jdc.supLignes(c.lineno,c.endline)
85         logging.warning("Suppression de %s ligne %s",c.name,c.lineno)
86     if boolChange : jdc.reset(jdc.getSource())
87
88 #-------------------------------------------------------------
89 def removeCommandeSiRegle(jdc,command,liste_regles):
90 #-------------------------------------------------------------
91     if command not in jdcSet : return
92     mesRegles=regles.ensembleRegles(liste_regles)
93     removeCommande(jdc,command,mesRegles,0)
94
95 #-------------------------------------------------------------
96 def removeCommandeSiRegleAvecErreur(jdc,command,liste_regles):
97 #-------------------------------------------------------------
98     if command not in jdcSet : return
99     mesRegles=regles.ensembleRegles(liste_regles)
100     removeCommande(jdc,command,mesRegles,1)
101                 
102 #---------------------------------
103 def removeMC(jdc,c,mc):
104 #---------------------------------
105     if debug : print "Suppression de:",c.name,mc.name,mc.lineno,mc.colno,mc.endline,mc.endcol
106     logging.info("Suppression de %s dans %s ligne %d",mc.name,c.name,mc.lineno)
107
108     if mc.endline > mc.lineno:
109         if debug:print "mocle sur plusieurs lignes--%s--" % jdc.getLines()[mc.lineno-1][mc.colno:]
110         jdc.getLines()[mc.lineno-1]=jdc.getLines()[mc.lineno-1][:mc.colno]
111         jdc.getLines()[mc.endline-1]=jdc.getLines()[mc.endline-1][mc.endcol:]
112
113         #attention : supprimer les lignes à la fin
114         jdc.getLines()[mc.lineno:mc.endline-1]=[]
115     else:
116         if debug:print "mocle sur une ligne--%s--" % jdc.getLines()[mc.lineno-1][mc.colno:mc.endcol]
117         s=jdc.getLines()[mc.lineno-1]
118         jdc.getLines()[mc.lineno-1]=s[:mc.colno]+s[mc.endcol:]
119         fusionne(jdc,mc.lineno-1)
120
121 #---------------------------------------------------------------------------------
122 def removeMotCleInFact(jdc,command,fact,mocle,ensemble=regles.SansRegle,erreur=0):
123 #----------------------------------------------------------------------------------
124     # on itere sur les commandes a l'envers pour ne pas polluer 
125     # les numeros de ligne avec les modifications
126     if command not in jdcSet : return
127     commands= jdc.root.childNodes[:]
128     commands.reverse()
129     boolChange=0
130     for c in commands:
131         if c.name != command:continue
132         for mc in c.childNodes:
133             if mc.name != fact:continue
134             l=mc.childNodes[:]
135             l.reverse()
136             for ll in l:
137                 for n in ll.childNodes:
138                     if n.name != mocle:continue
139                     if ensemble.verif(c) == 0 : continue
140                     if erreur : ecritErreur((command,fact,mocle),c.lineno)
141                     boolChange=1
142                     removeMC(jdc,c,n)
143
144     if boolChange : jdc.reset(jdc.getSource())
145
146 #------------------------------------------------------------------
147 def removeMotCleInFactSiRegle(jdc,command,fact,mocle,liste_regles):
148 #------------------------------------------------------------------
149     if command not in jdcSet : return
150     erreur=0
151     mesRegles=regles.ensembleRegles(liste_regles)
152     removeMotCleInFact(jdc,command,fact,mocle,mesRegles,erreur)
153
154 #----------------------------------------------------------------------
155 def removeMotCleInFactSiRegleAvecErreur(jdc,command,fact,mocle,liste_regles):
156 #----------------------------------------------------------------------
157     if command not in jdcSet : return
158     erreur=1
159     mesRegles=regles.ensembleRegles(liste_regles)
160     removeMotCleInFact(jdc,command,fact,mocle,mesRegles,erreur)
161
162
163 #----------------------------------------------------------------------
164 def removeMotCleInFactCourantSiRegle(jdc,command,fact,mocle,liste_regles,erreur=0):
165 #----------------------------------------------------------------------
166     if command not in jdcSet : return
167     ensemble=regles.ensembleRegles(liste_regles)
168     commands= jdc.root.childNodes[:]
169     commands.reverse()
170     boolChange=0
171     for c in commands:
172         if c.name != command:continue
173         for mc in c.childNodes:
174             if mc.name != fact:continue
175             l=mc.childNodes[:]
176             l.reverse()
177             for ll in l:
178                 if ensemble.verif(ll) == 0 : continue
179                 for n in ll.childNodes:
180                     if n.name != mocle:continue
181                     if erreur : ecritErreur((command,fact,mocle),c.lineno)
182                     boolChange=1
183                     removeMC(jdc,c,n)
184
185     if boolChange : jdc.reset(jdc.getSource())
186     
187 #------------------------------------------
188 def fusionne(jdc,numLigne):
189 #------------------------------------------
190 #   fusionne la ligne numLigne et numLigne+1
191 #   si la ligne numLigne+1 ne contient que des parentheses
192 #   fermantes
193 #   et si la ligne  numLigne ne contient pas par un "#"
194 #   Attention a la difference de numerotation
195 #        jdc.getLines()[numLigne] donne la ligne numLigne + 1
196 #        alors que joinLineandNext(numLigne) travaille sur le tableau
197     index=0
198     texte=jdc.getLines()[numLigne]
199     fusion=1
200     while (index < len(texte)) :
201       if texte[index] not in (" ",",",")",";","\n") :
202          fusion=0
203          break
204       index=index+1
205        
206     if fusion == 0 : return;
207
208     texte=jdc.getLines()[numLigne -1]
209     if texte.find("#") < 0 :
210        fusion=1
211     else :
212        fusion=0
213  
214     if fusion : 
215        jdc.joinLineandNext(numLigne)