1 # -*- coding: utf-8 -*-
3 from parseur import FactNode
4 from dictErreurs import jdcSet
5 from dictErreurs import EcritErreur
11 #-----------------------------------
12 def insereMotCle(jdc,recepteur,texte):
13 #-----------------------------------
14 # appelle la methode selon la classe
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)
24 #--------------------------------------------
25 def insereMotCleDansCommande(jdc,command,texte):
26 #---------------------------------------------
27 # insere le texte comme 1er mot cle
29 if command.name not in jdcSet : return
30 if debug : print "insereMotCle ", texte , " dans ", command.name
31 numcol=chercheDebut1Mot(jdc,command)
33 jdc.splitLine(command.lineno,numcol)
35 while texte[indice] == " " :
37 if texte[indice] != "," : texte=texte+","
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)
45 #---------------------------------------------
46 def insereMotCleDansFacteur(jdc,facteur,texte):
47 #-------------------------------------------------
48 if debug : print "insereMotCle ", texte , " dans ", facteur.name
50 if texte[-1] == "\n" : texte=texte[0:-1]
51 ancien=jdc.getLine(facteur.lineno)
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 ","
59 while texte[indice] == " " :
61 if texte[indice] != "," :
63 if (texte.find("#") > -1) and (texte.find("#") < texte.find(",")) :
69 ligneaCouper=facteur.lineno
73 while ligneaCouper < facteur.endline + 1 :
75 while ancien.find("_F") > 0 :
77 indice=ancien.find("_F")
79 # pour ne pas tenir compte des autres noms
80 # Attention si 2 MCF sur la meme ligne (la 1ere)
82 if ((ligneaCouper!=facteur.lineno) or ((ancien.find(facteur.name) < indice ) or (ancien.find(facteur.name) < 0))) :
84 indiceParcours=indice + 2
85 # attention pour regler DEFI_FONCTION ..
87 indiceDeCoupe=indiceDeCoupe+indice+2
88 ancien=ancien[indice +2:]
92 indiceDeCoupe=indiceDeCoupe+indice
93 # print "indice de Parcours" ,indiceParcours
94 # print ancien[indiceParcours]
95 # print ancien[indiceParcours+1]
96 # print ancien[indiceParcours+2]
97 while indiceParcours < longueur :
98 if ancien[indiceParcours] == "(" :
102 if ancien[indiceParcours] != " " :
106 indiceParcours = indiceParcours+1
107 trouve = trouveP * trouveF
109 ancien=ancien[indice+1:]
111 trouve = trouveP * trouveF
113 ligneaCouper=ligneaCouper+1
114 ancien=jdc.getLine(ligneaCouper)
117 debut=indiceDeCoupe + 3
118 jdc.splitLine(ligneaCouper,debut)
120 print "Le traducteur ne sait pas faire"
123 # enleve les blancs en debut de texte
125 while i < len(texte) :
126 if texte[i] != " " : break
129 jdc.addLine(texte,ligneaCouper)
130 jdc.joinLineandNext(ligneaCouper)
131 logging.info("Insertion de %s ligne %d", texteinfo,ligneaCouper)
132 # Gestion du cas particulier du mot clef facteur vide
133 if facteur.childNodes == []:
134 jdc.joinLineandNext(facteur.lineno)
137 #-----------------------------------
138 def chercheDebut1Mot(jdc,command):
139 #-----------------------------------
140 # Retourne le numero de colonne si le 1er mot clef est
141 # sur la meme ligne que le mot clef facteur
143 assert (command.childNodes != [])
145 node1=command.childNodes[0]
146 if hasattr(node1,"lineno"):
147 if node1.lineno == command.lineno :
150 debut=chercheDebutFacteur(jdc,command)
151 if debut == -1 and debug : print "attention!!! pb pour trouver le debut dans ", command
154 #-----------------------------------
155 def chercheDebutFacteur(jdc,facteur):
156 #-----------------------------------
158 ligne=jdc.getLines()[facteur.lineno]
159 debut=ligne.find("_F")
160 if debut > -1 : debut=debut + 3
164 #-----------------------------------
165 def chercheAlignement(jdc,command):
166 #-----------------------------------
167 # Retourne le nb de blanc
168 # pour aligner sur le 1er mot clef fils
169 assert (command.childNodes != [])
170 node1=command.childNodes[0]
174 #---------------------------------------------------------------------------------------------------------
175 def chercheOperInsereFacteur(jdc,nomcommande,nouveau,ensemble=regles.SansRegle, estunFacteur=1, erreur=0):
176 #--------------------------------------------------------------------------------------------------------
179 # appelle insereMotCle pour ajouter le texte
183 texte=nouveau+"=_F(),"
186 if nomcommande not in jdcSet : return
187 commands= jdc.root.childNodes[:]
190 if c.name != nomcommande:continue
191 if ensemble.verif(c) == 0 : continue
192 if erreur : EcritErreur((nomcommande,nouveau),c.lineno)
194 insereMotCle(jdc,c,texte)
195 if boolChange : jdc.reset(jdc.getSource())
197 #----------------------------------------------------------------------------------------
198 def chercheOperInsereFacteurSiRegle(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
199 #----------------------------------------------------------------------------------------
202 # appelle insereMotCle pour ajouter le texte
204 if nomcommande not in jdcSet : return
205 mesRegles=regles.ensembleRegles(liste_regles)
206 chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur)
208 #---------------------------------------------------------------------------------------------------------
209 def chercheOperInsereFacteurSiRegleAvecAvertissement(jdc,nomcommande,nouveau,liste_regles, estunFacteur=1):
210 #---------------------------------------------------------------------------------------------------------
211 if nomcommande not in jdcSet : return
212 mesRegles=regles.ensembleRegles(liste_regles)
213 chercheOperInsereFacteur(jdc,nomcommande,nouveau,mesRegles,estunFacteur,erreur=1)
215 #-------------------------------------------------------------------------------------------------
216 def AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,ensemble=regles.SansRegle, estunFacteur=0):
217 #-------------------------------------------------------------------------------------------------
218 # Cherche la commande
221 # appelle insereMotCle pour ajouter le texte
223 if commande not in jdcSet : return
225 texte=nouveau+"=_F(),"
228 commands= jdc.root.childNodes[:]
232 if c.name != commande : continue
233 for mcF in c.childNodes:
234 if mcF.name != fact : continue
235 if ensemble.verif(c) == 0 : continue
239 insereMotCleDansFacteur(jdc,mcF,texte)
240 if boolChange : jdc.reset(jdc.getSource())
242 #-------------------------------------------------------------------------------------------
243 def AjouteMotClefDansFacteurSiRegle(jdc,commande,fact,nouveau,liste_regles,estunFacteur=0):
244 #-------------------------------------------------------------------------------------------
246 if commande not in jdcSet : return
247 mesRegles=regles.ensembleRegles(liste_regles)
248 AjouteMotClefDansFacteur(jdc,commande,fact,nouveau,mesRegles,estunFacteur)