Salome HOME
debug intempestif
[tools/eficas.git] / Traducteur2 / mocles.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017   EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import compiler
22 import types
23 from Traducteur.parseur  import Keyword, FactNode, lastParen, lastParen2,maskStringsAndComments
24 from Traducteur.visiteur import KeywordFinder, visitor
25 from Traducteur.utils import indexToCoordinates, lineToDict, dictToLine
26
27 debug=0
28
29 #------------------------
30 def parseFact(match,c,kw):
31 #------------------------
32     submatch=match[2]
33     lastpar=match[0]+lastParen(c.src[match[0]:])
34     if type(submatch[0][0]) ==types.IntType:
35         #mot cle facteur isole
36         no=FactNode()
37         kw.addChild(no)
38         for ii in range(len(submatch)-1):
39             e=submatch[ii]
40             x,y=indexToCoordinates(c.src,e[0])
41             lineno=y+c.lineno
42             colno=x
43             x,y=indexToCoordinates(c.src,submatch[ii+1][0])
44             endline=y+c.lineno
45             endcol=x
46             no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
47         #last one
48         e=submatch[-1]
49         x,y=indexToCoordinates(c.src,e[0])
50         lineno=y+c.lineno
51         colno=x
52         x,y=indexToCoordinates(c.src,lastpar-1)
53         endline=y+c.lineno
54         endcol=x
55         no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
56     else:
57         #mot cle facteur multiple
58         ii=0
59         for l in submatch:
60             lastpar=l[0][0]+lastParen2(c.src[l[0][0]:])
61             ii=ii+1
62             no=FactNode()
63             kw.addChild(no)
64             for j in range(len(l)-1):
65                 e=l[j]
66                 x,y=indexToCoordinates(c.src,e[0])
67                 lineno=y+c.lineno
68                 colno=x
69                 x,y=indexToCoordinates(c.src,l[j+1][0])
70                 endline=y+c.lineno
71                 endcol=x
72                 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
73             #last one
74             e=l[-1]
75             x,y=indexToCoordinates(c.src,e[0])
76             lineno=y+c.lineno
77             colno=x
78             x,y=indexToCoordinates(c.src,lastpar-1)
79             endline=y+c.lineno
80             endcol=x
81             no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
82
83
84 #-----------------------
85 def parseKeywords(root):
86 #-----------------------
87     """A partir d'un arbre contenant des commandes, ajoute les noeuds
88        fils correspondant aux mocles de la commande
89     """
90     #print "parseKeywords"
91     #traceback.print_stack(limit=5)
92
93     matchFinder=KeywordFinder()
94
95     for c in root.childNodes:
96         maskedsrc=maskStringsAndComments(c.src)
97         #on supprime seulement les blancs du debut pour pouvoir compiler
98         #meme si la commande est sur plusieurs lignes seul le debut compte
99         ast=compiler.parse(c.src.lstrip())
100         #print ast
101         #Ne pas supprimer les blancs du debut pour avoir les bons numeros de colonne
102         matchFinder.reset(maskedsrc)
103         visitor.walk(ast, matchFinder)
104         #print matchFinder.matches
105         if len(matchFinder.matches) > 1:
106             # plusieurs mocles trouves :
107             # un mocle commence au debut du keyword (matchFinder.matches[i][0])
108             # et finit juste avant le keyword suivant
109             # (matchFinder.matches[i+1][0]])
110             for i in range(len(matchFinder.matches)-1):
111                 if debug:print "texte:",c.src[matchFinder.matches[i][0]:matchFinder.matches[i+1][0]]
112                 x,y=indexToCoordinates(c.src,matchFinder.matches[i][0])
113                 lineno=y+c.lineno
114                 colno=x
115                 x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
116                 endline=y+c.lineno
117                 endcol=x
118                 if debug:print matchFinder.matches[i][0],matchFinder.matches[i][1],lineno,colno,endline,endcol
119                 kw=Keyword(matchFinder.matches[i][1],lineno,colno,endline,endcol)
120                 c.addChild(kw)
121                 submatch= matchFinder.matches[i][2]
122                 if submatch:
123                     parseFact(matchFinder.matches[i],c,kw)
124
125             # dernier mocle :
126             #   il commence au debut du dernier keyword
127             #   (matchFinder.matches[i+1][0]) et
128             #   finit avant la parenthese fermante de la commande (c.lastParen)
129
130             if debug:print "texte:",c.src[matchFinder.matches[i+1][0]:c.lastParen]
131             x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
132             lineno=y+c.lineno
133             colno=x
134             x,y=indexToCoordinates(c.src,c.lastParen)
135             endline=y+c.lineno
136             endcol=x
137             if debug:print matchFinder.matches[i+1][0],matchFinder.matches[i+1][1],lineno,colno,endline,endcol
138             kw=Keyword(matchFinder.matches[i+1][1],lineno,colno,endline,endcol)
139             c.addChild(kw)
140             submatch= matchFinder.matches[i+1][2]
141             if submatch:
142                 parseFact(matchFinder.matches[i+1],c,kw)
143
144         elif len(matchFinder.matches) == 1:
145             #un seul mocle trouve :
146             # il commence au debut du keyword (matchFinder.matches[0][0]) et
147             # finit juste avant la parenthese fermante de la
148             # commande (c.lastParen)
149             if debug:print "texte:",c.src[matchFinder.matches[0][0]:c.lastParen]
150             x,y=indexToCoordinates(c.src,matchFinder.matches[0][0])
151             lineno=y+c.lineno
152             colno=x
153             x,y=indexToCoordinates(c.src,c.lastParen)
154             endline=y+c.lineno
155             endcol=x
156             if debug:print matchFinder.matches[0][0],matchFinder.matches[0][1],lineno,colno,endline,endcol
157             kw=Keyword(matchFinder.matches[0][1],lineno,colno,endline,endcol)
158             c.addChild(kw)
159             submatch= matchFinder.matches[0][2]
160             if submatch:
161                 parseFact(matchFinder.matches[0],c,kw)
162         else:
163             pass