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