]> SALOME platform Git repositories - tools/eficas.git/blob - Traducteur/mocles.py
Salome HOME
f9429ad8157ee0ec5450f66f34f5f54927fd8138
[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
6 from visiteur import KeywordFinder,visitor
7 from utils import indexToCoordinates
8
9 debug=0
10
11 def parseFact(match,c,kw):
12     submatch=match[2]
13     lastpar=match[0]+lastparen(c.src[match[0]:])
14     if type(submatch[0][0]) ==types.IntType:
15         #mot cle facteur isolé
16         no=FactNode()
17         kw.addChild(no)
18         for ii in range(len(submatch)-1):
19             e=submatch[ii]
20             x,y=indexToCoordinates(c.src,e[0])
21             lineno=y+c.lineno
22             colno=x
23             x,y=indexToCoordinates(c.src,submatch[ii+1][0])
24             endline=y+c.lineno
25             endcol=x
26             no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
27         #last one
28         e=submatch[-1]
29         x,y=indexToCoordinates(c.src,e[0])
30         lineno=y+c.lineno
31         colno=x
32         x,y=indexToCoordinates(c.src,lastpar-1)
33         endline=y+c.lineno
34         endcol=x
35         no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
36     else:
37         #mot cle facteur multiple
38         ii=0
39         for l in submatch:
40             lastpar=l[0][0]+lastparen2(c.src[l[0][0]:])
41             ii=ii+1
42             no=FactNode()
43             kw.addChild(no)
44             for j in range(len(l)-1):
45                 e=l[j]
46                 x,y=indexToCoordinates(c.src,e[0])
47                 lineno=y+c.lineno
48                 colno=x
49                 x,y=indexToCoordinates(c.src,l[j+1][0])
50                 endline=y+c.lineno
51                 endcol=x
52                 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
53             #last one
54             e=l[-1]
55             x,y=indexToCoordinates(c.src,e[0])
56             lineno=y+c.lineno
57             colno=x
58             x,y=indexToCoordinates(c.src,lastpar-1)
59             endline=y+c.lineno
60             endcol=x
61             no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
62
63
64 def parseKeywords(root):
65     """A partir d'un arbre contenant des commandes, ajoute les noeuds fils correspondant aux mocles
66        de la commande
67     """
68     matchFinder=KeywordFinder()
69
70     for c in root.childNodes:
71         ast=compiler.parse(c.src)
72         #print ast
73         matchFinder.reset(c.src)
74         visitor.walk(ast, matchFinder)
75         #print matchFinder.matches
76         if len(matchFinder.matches) > 1:
77             #plusieurs mocles trouvés : un mocle commence au début du keyword (matchFinder.matches[i][0])
78             # et finit juste avant le keyword suivant (matchFinder.matches[i+1][0]])
79             for i in range(len(matchFinder.matches)-1):
80                 if debug:print "texte:",c.src[matchFinder.matches[i][0]:matchFinder.matches[i+1][0]]
81                 x,y=indexToCoordinates(c.src,matchFinder.matches[i][0])
82                 lineno=y+c.lineno
83                 colno=x
84                 x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
85                 endline=y+c.lineno
86                 endcol=x
87                 if debug:print matchFinder.matches[i][0],matchFinder.matches[i][1],lineno,colno,endline,endcol
88                 kw=Keyword(matchFinder.matches[i][1],lineno,colno,endline,endcol)
89                 c.addChild(kw)
90                 submatch= matchFinder.matches[i][2]
91                 if submatch:
92                     parseFact(matchFinder.matches[i],c,kw)
93             #dernier mocle : il commence au debut du dernier keyword (matchFinder.matches[i+1][0]) et
94             #finit avant la parenthese fermante de la commande (c.lastparen)
95             if debug:print "texte:",c.src[matchFinder.matches[i+1][0]:c.lastparen]
96             x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
97             lineno=y+c.lineno
98             colno=x
99             x,y=indexToCoordinates(c.src,c.lastparen)
100             endline=y+c.lineno
101             endcol=x
102             if debug:print matchFinder.matches[i+1][0],matchFinder.matches[i+1][1],lineno,colno,endline,endcol
103             kw=Keyword(matchFinder.matches[i+1][1],lineno,colno,endline,endcol)
104             c.addChild(kw)
105             submatch= matchFinder.matches[i+1][2]
106             if submatch:
107                 parseFact(matchFinder.matches[i+1],c,kw)
108
109         elif len(matchFinder.matches) == 1:
110             #un seul mocle trouve : il commence au début du keyword (matchFinder.matches[0][0]) et 
111             #finit juste avant la parenthese fermante de la commande (c.lastparen)
112             if debug:print "texte:",c.src[matchFinder.matches[0][0]:c.lastparen]
113             x,y=indexToCoordinates(c.src,matchFinder.matches[0][0])
114             lineno=y+c.lineno
115             colno=x
116             x,y=indexToCoordinates(c.src,c.lastparen)
117             endline=y+c.lineno
118             endcol=x
119             if debug:print matchFinder.matches[0][0],matchFinder.matches[0][1],lineno,colno,endline,endcol
120             kw=Keyword(matchFinder.matches[0][1],lineno,colno,endline,endcol)
121             c.addChild(kw)
122             submatch= matchFinder.matches[0][2]
123             if submatch:
124                 parseFact(matchFinder.matches[0],c,kw)
125         else:
126             pass
127