Salome HOME
merge avec les devloppts de l ete
[tools/eficas.git] / Traducteur / inseremocle.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013   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.parseur import FactNode
22 from Traducteur.load import jdcSet 
23 from Traducteur.dictErreurs import EcritErreur
24 from Traducteur import regles
25 debug=0
26
27
28 #-----------------------------------
29 def insereMotCle(jdc,recepteur,texte):
30 #-----------------------------------
31 # appelle la methode selon la classe 
32 # du recepteur
33
34     if recepteur.name  not in jdcSet : return
35     if recepteur.__class__.__name__ == "Command" :
36        if debug : print " Ajout de ", texte, "dans la commande : " ,recepteur.name 
37        insereMotCleDansCommande(jdc,recepteur,texte)
38        return
39
40
41 #--------------------------------------------
42 def insereMotCleDansCommande(jdc,command,texte):
43 #---------------------------------------------
44 # insere le texte comme 1er mot cle
45 # de la commande
46     if command.name  not in jdcSet : return
47     if debug : print "insereMotCle ", texte , " dans ", command.name
48     numcol=chercheDebut1Mot(jdc,command)
49     if numcol > 0 :
50        jdc.splitLine(command.lineno,numcol)
51     indice = -1
52     while texte[indice] == " " or texte[indice] == "\n": 
53        indice = indice -1
54     if texte[indice] != "," : texte=texte+","
55     texteinfo=texte
56     texte=texte+'\n'
57     jdc.addLine(texte,command.lineno) 
58     logging.info("Insertion de : %s ligne %d", texteinfo,command.lineno)
59     if numcol > 0 :             # Les mots clefs etaient sur la même ligne
60         jdc.joinLineandNext(command.lineno)
61
62 #-------------------------------------------------------------
63 def insereMotCleDansFacteur(jdc,facteur,texte,plusieursFois=True):
64 #----------------------------------------------------------------
65     if debug : print "insereMotCle ", texte , " dans ", facteur.name
66
67     if texte[-1] == "\n" : texte=texte[0:-1] 
68     ancien=jdc.getLine(facteur.lineno)
69
70     # On va chercher la derniere ) pour ajouter avant
71     # on va verifier s il il y a un , avant
72     # si le texte ne finit pas par une ","
73     # on en met une
74
75     indice = -1
76     while texte[indice] == " " : 
77        indice = indice -1
78     if texte[indice] != "," : 
79        texte=texte+","
80     if (texte.find("#") > -1) and (texte.find("#") < texte.find(",")) :
81           texte=texte+"\n,"
82           
83     texteinfo=texte
84     texte=texte+"\n"
85   
86     ligneaCouper=facteur.lineno
87     while ligneaCouper < facteur.endline + 1 :
88         trouve=0
89         trouveF=0
90         trouveP=0
91         indiceDeCoupe=0
92         while  ancien.find("_F") > 0 :
93             longueur=len(ancien)
94             indice=ancien.find("_F")
95             indiceParcours=0
96             # pour ne pas tenir compte des autres noms 
97             # Attention si 2 MCF sur la meme ligne (la 1ere)
98             if trouveF == 0 :
99                 if ((ligneaCouper!=facteur.lineno) or ((ancien.find(facteur.name) < indice ) or (ancien.find(facteur.name) < 0))) :
100                    trouveF=1
101                    indiceParcours=indice + 2
102             # attention pour regler DEFI_FONCTION .. 
103                 else :
104                    indiceDeCoupe=indiceDeCoupe+indice+2
105                    ancien=ancien[indice +2:]
106                    continue
107             if trouveF == 1 :
108                 indiceDeCoupe=indiceDeCoupe+indice
109     #            print "indice de Parcours" ,indiceParcours
110     #            print ancien[indiceParcours] 
111     #            print ancien[indiceParcours+1] 
112     #            print ancien[indiceParcours+2] 
113                 while  indiceParcours < longueur :
114                     if ancien[indiceParcours] == "(" :
115                         trouveP=1
116     #                    print "trouve"
117                         break
118                     if ancien[indiceParcours] != " " :
119                         trouveP=0
120     #                    print "mouv"
121                         break
122                     indiceParcours = indiceParcours+1
123             trouve = trouveP * trouveF
124             if trouve : break
125             ancien=ancien[indice+1:]
126         if trouve :
127             debut=indiceDeCoupe + 3
128             if(jdc.getLine(ligneaCouper)[debut:]!="\n"):
129                 jdc.splitLine(ligneaCouper,debut)
130             jdc.addLine(texte,ligneaCouper)
131             jdc.joinLineandNext(ligneaCouper)
132             logging.info("Insertion de %s ligne %d", texteinfo,ligneaCouper)
133
134             # Gestion du cas particulier du mot clef facteur vide
135             if facteur.childNodes == []:
136                 jdc.joinLineandNext(facteur.lineno)
137
138         ligneaCouper=ligneaCouper+1
139         ancien=jdc.getLine(ligneaCouper)
140         if not plusieursFois and trouve : break
141
142
143 #-----------------------------------
144 def chercheDebut1Mot(jdc,command):
145 #-----------------------------------
146 # Retourne le numero de colonne si le 1er mot clef est 
147 # sur la meme ligne que le mot clef facteur
148 # -1 sinon
149     assert (command.childNodes != [])
150     debut=-1
151     node1=command.childNodes[0]
152     if hasattr(node1,"lineno"):
153        if node1.lineno == command.lineno :
154           debut=node1.colno
155     else:
156        debut=chercheDebutFacteur(jdc,command) 
157     if debut == -1 and debug : print "attention!!! pb pour trouver le debut dans ", command
158     return debut
159
160 #-----------------------------------
161 def chercheDebutFacteur(jdc,facteur):
162 #-----------------------------------
163     debut=-1
164     ligne=jdc.getLines()[facteur.lineno]
165     debut=ligne.find("_F")
166     if debut >  -1 : debut=debut + 3
167     return debut
168     
169
170 #-----------------------------------
171 def chercheAlignement(jdc,command):
172 #-----------------------------------
173 # Retourne le nb de blanc
174 # pour aligner sur le 1er mot clef fils
175     assert (command.childNodes != []) 
176     node1=command.childNodes[0]
177     nbBlanc=node1.colno
178     return " "*nbBlanc
179
180 #---------------------------------------------------------------------------------------------------------
181 def chercheOperInsereFacteur(jdc,nomcommande,nouveau,ensemble=regles.SansRegle, estunFacteur=1, erreur=0):
182 #--------------------------------------------------------------------------------------------------------
183 # Cherche l oper
184 # cree le texte
185 # appelle insereMotCle pour ajouter le texte
186 #
187     boolChange=0
188     if estunFacteur : 
189       texte=nouveau+"=_F(),"
190     else :
191       texte=nouveau
192     if nomcommande  not in jdcSet : return
193     commands= jdc.root.childNodes[:]
194     commands.reverse()
195     for c in commands:
196         if c.name != nomcommande:continue
197         if ensemble.verif(c) == 0 : continue
198         if erreur : EcritErreur((nomcommande,nouveau),c.lineno)
199         boolChange=1
200         insereMotCle(jdc,c,texte)
201     if boolChange : jdc.reset(jdc.getSource())
202
203 #----------------------------------------------------------------------------------------
204 def chercheOperInsereFacteurSiRegle(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
205 #----------------------------------------------------------------------------------------
206 # Cherche l oper
207 # cree le texte
208 # appelle insereMotCle pour ajouter le texte
209 #
210     if nomcommande  not in jdcSet : return
211     mesRegles=regles.ensembleRegles(liste_regles)
212     chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur)
213
214 #----------------------------------------------------------------------------------------
215 def chercheOperInsereMotCleSiRegle(jdc,nomcommande,nouveau,liste_regles, estunFacteur=0):
216 #----------------------------------------------------------------------------------------
217     if nomcommande  not in jdcSet : return
218     mesRegles=regles.ensembleRegles(liste_regles)
219     chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur)
220
221     
222 #---------------------------------------------------------------------------------------------------------
223 def chercheOperInsereFacteurSiRegleAvecAvertissement(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
224 #---------------------------------------------------------------------------------------------------------
225     if nomcommande  not in jdcSet : return
226     mesRegles=regles.ensembleRegles(liste_regles)
227     chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur,erreur=1)
228
229 #-------------------------------------------------------------------------------------------------
230 def AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,ensemble=regles.SansRegle, estunFacteur=0):
231 #-------------------------------------------------------------------------------------------------
232 # Cherche la commande
233 # Cherche le MCF
234 # cree le texte
235 # appelle insereMotCle pour ajouter le texte
236 #
237     if commande  not in jdcSet : return
238     if estunFacteur : 
239       texte=nouveau+"=_F(),"
240     else :
241       texte=nouveau
242     commands= jdc.root.childNodes[:]
243     commands.reverse()
244     boolChange=0
245     for c in commands:
246         if c.name != commande : continue
247         for mcF in c.childNodes:
248             if mcF.name != fact : continue
249             if ensemble.verif(c) == 0 : continue
250             boolChange=1
251             insereMotCleDansFacteur(jdc,mcF,texte)
252     if boolChange : jdc.reset(jdc.getSource())
253
254 #-------------------------------------------------------------------------------------------
255 def AjouteMotClefDansFacteurSiRegle(jdc,commande,fact,nouveau,liste_regles,estunFacteur=0):
256 #-------------------------------------------------------------------------------------------
257 #
258     if commande  not in jdcSet : return
259     mesRegles=regles.ensembleRegles(liste_regles)
260     AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,mesRegles,estunFacteur)
261
262 #-------------------------------------------------------------------------------------------
263 def AjouteMotClefDansFacteurCourantSiRegle(jdc,commande,fact,nouveau,liste_regles):
264 #-------------------------------------------------------------------------------------------
265 #
266     if commande  not in jdcSet : return
267     ensemble=regles.ensembleRegles(liste_regles)
268     commands= jdc.root.childNodes[:]
269     commands.reverse()
270     boolChange=0
271     for c in commands:
272         if c.name != commande : continue
273         for mcF in c.childNodes:
274           if mcF.name != fact : continue
275           l=mcF.childNodes[:]
276           l.reverse()
277           for ll in l:
278              if ensemble.verif(ll) == 0 : continue
279              boolChange=1
280              n=ll.childNodes[0]
281              ligneaCouper=n.lineno-1
282              numcol=n.colno
283              jdc.splitLine(ligneaCouper+1,numcol)
284              texte=nouveau+",\n"
285              jdc.addLine(texte,ligneaCouper+1)
286              logging.info("Insertion de %s dans %s : ligne %d", nouveau,c.name,ligneaCouper+1)
287              if numcol > 0 :    
288                  jdc.joinLineandNext(ligneaCouper+1)
289     if boolChange : jdc.reset(jdc.getSource())