Salome HOME
CCAR: rabattre la version V1_15a4 dans la branche principale
[tools/eficas.git] / Traducteur / mocles.py
1 # -*- coding: utf-8 -*-
2
3 import compiler
4 import types
5 from parseur  import Keyword, FactNode, lastparen, lastparen2,maskStringsAndComments
6 from visiteur import KeywordFinder, visitor
7 from utils    import indexToCoordinates
8 import traceback
9
10 debug=0
11
12 #------------------------
13 def parseFact(match,c,kw):
14 #------------------------
15     submatch=match[2]
16     lastpar=match[0]+lastparen(c.src[match[0]:])
17     if type(submatch[0][0]) ==types.IntType:
18         #mot cle facteur isolé
19         no=FactNode()
20         kw.addChild(no)
21         for ii in range(len(submatch)-1):
22             e=submatch[ii]
23             x,y=indexToCoordinates(c.src,e[0])
24             lineno=y+c.lineno
25             colno=x
26             x,y=indexToCoordinates(c.src,submatch[ii+1][0])
27             endline=y+c.lineno
28             endcol=x
29             no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
30         #last one
31         e=submatch[-1]
32         x,y=indexToCoordinates(c.src,e[0])
33         lineno=y+c.lineno
34         colno=x
35         x,y=indexToCoordinates(c.src,lastpar-1)
36         endline=y+c.lineno
37         endcol=x
38         no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
39     else:
40         #mot cle facteur multiple
41         ii=0
42         for l in submatch:
43             lastpar=l[0][0]+lastparen2(c.src[l[0][0]:])
44             ii=ii+1
45             no=FactNode()
46             kw.addChild(no)
47             for j in range(len(l)-1):
48                 e=l[j]
49                 x,y=indexToCoordinates(c.src,e[0])
50                 lineno=y+c.lineno
51                 colno=x
52                 x,y=indexToCoordinates(c.src,l[j+1][0])
53                 endline=y+c.lineno
54                 endcol=x
55                 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
56             #last one
57             e=l[-1]
58             x,y=indexToCoordinates(c.src,e[0])
59             lineno=y+c.lineno
60             colno=x
61             x,y=indexToCoordinates(c.src,lastpar-1)
62             endline=y+c.lineno
63             endcol=x
64             no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
65
66
67 #-----------------------
68 def parseKeywords(root):
69 #-----------------------
70     """A partir d'un arbre contenant des commandes, ajoute les noeuds 
71        fils correspondant aux mocles de la commande
72     """
73     #print "parseKeywords"
74     #traceback.print_stack(limit=5)
75
76     matchFinder=KeywordFinder()
77
78     for c in root.childNodes:
79         maskedsrc=maskStringsAndComments(c.src)
80         #on supprime seulement les blancs du debut pour pouvoir compiler
81         #meme si la commande est sur plusieurs lignes seul le debut compte
82         ast=compiler.parse(c.src.lstrip())
83         #print ast
84         #Ne pas supprimer les blancs du debut pour avoir les bons numeros de colonne
85         matchFinder.reset(maskedsrc)
86         visitor.walk(ast, matchFinder)
87         #print matchFinder.matches
88         if len(matchFinder.matches) > 1:
89             # plusieurs mocles trouvés : 
90             # un mocle commence au début du keyword (matchFinder.matches[i][0])
91             # et finit juste avant le keyword suivant 
92             # (matchFinder.matches[i+1][0]])
93             for i in range(len(matchFinder.matches)-1):
94                 if debug:print "texte:",c.src[matchFinder.matches[i][0]:matchFinder.matches[i+1][0]]
95                 x,y=indexToCoordinates(c.src,matchFinder.matches[i][0])
96                 lineno=y+c.lineno
97                 colno=x
98                 x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
99                 endline=y+c.lineno
100                 endcol=x
101                 if debug:print matchFinder.matches[i][0],matchFinder.matches[i][1],lineno,colno,endline,endcol
102                 kw=Keyword(matchFinder.matches[i][1],lineno,colno,endline,endcol)
103                 c.addChild(kw)
104                 submatch= matchFinder.matches[i][2]
105                 if submatch:
106                     parseFact(matchFinder.matches[i],c,kw)
107
108             # dernier mocle : 
109             #   il commence au debut du dernier keyword 
110             #   (matchFinder.matches[i+1][0]) et
111             #   finit avant la parenthese fermante de la commande (c.lastparen)
112
113             if debug:print "texte:",c.src[matchFinder.matches[i+1][0]:c.lastparen]
114             x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
115             lineno=y+c.lineno
116             colno=x
117             x,y=indexToCoordinates(c.src,c.lastparen)
118             endline=y+c.lineno
119             endcol=x
120             if debug:print matchFinder.matches[i+1][0],matchFinder.matches[i+1][1],lineno,colno,endline,endcol
121             kw=Keyword(matchFinder.matches[i+1][1],lineno,colno,endline,endcol)
122             c.addChild(kw)
123             submatch= matchFinder.matches[i+1][2]
124             if submatch:
125                 parseFact(matchFinder.matches[i+1],c,kw)
126
127         elif len(matchFinder.matches) == 1:
128             #un seul mocle trouve : 
129             # il commence au début du keyword (matchFinder.matches[0][0]) et 
130             # finit juste avant la parenthese fermante de la 
131             # commande (c.lastparen)
132             if debug:print "texte:",c.src[matchFinder.matches[0][0]:c.lastparen]
133             x,y=indexToCoordinates(c.src,matchFinder.matches[0][0])
134             lineno=y+c.lineno
135             colno=x
136             x,y=indexToCoordinates(c.src,c.lastparen)
137             endline=y+c.lineno
138             endcol=x
139             if debug:print matchFinder.matches[0][0],matchFinder.matches[0][1],lineno,colno,endline,endcol
140             kw=Keyword(matchFinder.matches[0][1],lineno,colno,endline,endcol)
141             c.addChild(kw)
142             submatch= matchFinder.matches[0][2]
143             if submatch:
144                 parseFact(matchFinder.matches[0],c,kw)
145         else:
146             pass
147