Salome HOME
merge de la branche BR_dev_mars_06 (tag V1_10b5) dans la branche principale
[tools/eficas.git] / Traducteur / inseremocle.py
1 # -*- coding: utf-8 -*-
2 import logging
3 from parseur import FactNode
4 import string
5 debug=1
6
7
8 #-----------------------------------
9 def inseremotcle(jdc,recepteur,texte):
10 #-----------------------------------
11 # appelle la methode selon la classe 
12 # du recepteur
13
14     if recepteur.__class__.__name__ == "Command" :
15        if debug : print " Ajout de ", texte, "dans la commande : " ,recepteur.name 
16        inseremotcleincommand(jdc,recepteur,texte)
17        return
18
19
20 #--------------------------------------------
21 def inseremotcleincommand(jdc,command,texte):
22 #---------------------------------------------
23 # insere le texte comme 1er mot cle
24 # de la commande
25     if debug : print "inseremotcle ", texte , " dans ", command.name
26     numcol=chercheDebut1mot(jdc,command)
27     if numcol > 0 :
28        jdc.splitLine(command.lineno,numcol)
29     debut=chercheAlignement(jdc,command)
30     texte=debut+texte+"\n"
31     jdc.addLine(texte,command.lineno) 
32     if numcol > 0 :             # Les mots clefs etaient sur la même ligne
33         jdc.joinLineandNext(command.lineno)
34
35 #---------------------------------------------
36 def inseremotcleinfacteur(jdc,facteur,texte):
37 #-------------------------------------------------
38     if debug : print "inseremotcle ", texte , " dans ", facteur.name
39     ancien=jdc.getLine(facteur.lineno )
40     # On va chercher la dernier ) pour ajouter avant
41     # on va verifier s il il y a un , avant
42     ligne,col,boolvirgule=chercheDerniereParenthese(jdc,facteur)
43     if col > 0 :
44        jdc.splitLine(ligne,col)
45     if boolvirgule == 0 :
46        jdc.addLine(",\n",ligne)
47        jdc.joinLineandNext(ligne)
48     debut=ancien.find("_F") + 3
49     aligne=debut*" "
50     # enleve les blancs en debut de texte
51     i = 0
52     while i < len(texte) :
53       if texte[i] != " " : break
54       i = i +1
55     texte=aligne+texte+"\n"
56     jdc.addLine(texte,ligne)
57     jdc.joinLineandNext(ligne+1)
58
59 #---------------------------------------
60 def chercheDerniereParenthese(jdc,facteur):
61 #---------------------------------------
62     ligne=facteur.endline-1
63     col=-1
64     boolvirgule=0
65     trouveParent=0
66     while ( trouveParent == 0) :
67        texte=jdc.getLine(ligne)
68        col=texte.rfind(")")
69        if col < 0 :
70           ligne=ligne-1
71        else :
72           trouveParent=1
73     indice=col -1
74     while ( indice > -1 and texte[indice] == " " ):
75           indice = indice -1
76     if texte[indice]=="," :
77        boolvirgule = 1
78     return (ligne,col,boolvirgule)
79
80 #-----------------------------------
81 def chercheDebut1mot(jdc,command):
82 #-----------------------------------
83 # Retourne le numero de colonne si le 1er mot clef est 
84 # sur la meme ligne que le mot clef facteur
85 # -1 sinon
86     assert (command.childNodes != [])
87     debut=-1
88     node1=command.childNodes[0]
89     if hasattr(node1,"lineno"):
90        if node1.lineno == command.lineno :
91           debut=node1.colno
92     else:
93        debut=chercheDebutfacteur(jdc,command) 
94     if debut == -1 and debug : print "attention!!! pb pour trouver le debut dans ", command
95     return debut
96
97 #-----------------------------------
98 def chercheDebutfacteur(jdc,facteur):
99 #-----------------------------------
100     debut=-1
101     ligne=jdc.getLines()[facteur.lineno]
102     debut=ligne.find("_F")
103     if debut >  -1 : debut=debut + 3
104     return debut
105     
106
107
108 #-----------------------------------
109 def chercheAlignement(jdc,command):
110 #-----------------------------------
111 # Retourne le nb de blanc
112 # pour aligner sur le 1er mot clef fils
113     assert (command.childNodes != []) 
114     node1=command.childNodes[0]
115     nbBlanc=node1.colno
116     return " "*nbBlanc
117
118