1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2013 EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 from parseur import Keyword, FactNode, lastparen, lastparen2,maskStringsAndComments
24 from visiteur import KeywordFinder, visitor
25 from utils import indexToCoordinates, lineToDict, DictToLine
29 #------------------------
30 def parseFact(match,c,kw):
31 #------------------------
33 lastpar=match[0]+lastparen(c.src[match[0]:])
34 if type(submatch[0][0]) ==types.IntType:
35 #mot cle facteur isolé
38 for ii in range(len(submatch)-1):
40 x,y=indexToCoordinates(c.src,e[0])
43 x,y=indexToCoordinates(c.src,submatch[ii+1][0])
46 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
49 x,y=indexToCoordinates(c.src,e[0])
52 x,y=indexToCoordinates(c.src,lastpar-1)
55 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
57 #mot cle facteur multiple
60 lastpar=l[0][0]+lastparen2(c.src[l[0][0]:])
64 for j in range(len(l)-1):
66 x,y=indexToCoordinates(c.src,e[0])
69 x,y=indexToCoordinates(c.src,l[j+1][0])
72 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
75 x,y=indexToCoordinates(c.src,e[0])
78 x,y=indexToCoordinates(c.src,lastpar-1)
81 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
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
90 #print "parseKeywords"
91 #traceback.print_stack(limit=5)
93 matchFinder=KeywordFinder()
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())
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 trouvés :
107 # un mocle commence au début 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])
115 x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
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)
121 submatch= matchFinder.matches[i][2]
123 parseFact(matchFinder.matches[i],c,kw)
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)
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])
134 x,y=indexToCoordinates(c.src,c.lastparen)
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)
140 submatch= matchFinder.matches[i+1][2]
142 parseFact(matchFinder.matches[i+1],c,kw)
144 elif len(matchFinder.matches) == 1:
145 #un seul mocle trouve :
146 # il commence au début 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])
153 x,y=indexToCoordinates(c.src,c.lastparen)
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)
159 submatch= matchFinder.matches[0][2]
161 parseFact(matchFinder.matches[0],c,kw)