1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2017 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
22 from ast import NodeVisitor
25 class MatchFinder (NodeVisitor):
26 """Visiteur de base : gestion des matches """
30 self.words = re.split("(\w+)", line) # every other one is a non word
33 for word in self.words:
34 self.positions.append(i)
37 if debug : print ('fin reset', self.words)
39 def popWordsUpTo(self, word):
41 return # won't be able to find this
42 posInWords = self.words.index(word)
43 idx = self.positions[posInWords]
44 self.words = self.words[posInWords+1:]
45 self.positions = self.positions[posInWords+1:]
47 def appendMatch(self,name):
48 idx = self.getNextIndexOfWord(name)
49 self._matches.append((idx, name))
51 def getNextIndexOfWord(self,name):
52 return self.positions[self.words.index(name)]
55 class KeywordFinder(MatchFinder):
56 """Visiteur pour les keywords d'une commande """
58 def visit_keyword(self,node):
59 if debug : print (' visit_keyword', node.arg)
60 idx = self.getNextIndexOfWord(node.arg)
61 self.popWordsUpTo(node.arg)
62 prevmatches=self._matches
64 #for child in node.getChildNodes():
66 self.generic_visit(node)
67 prevmatches.append((idx, node.arg,self._matches))
68 self._matches=prevmatches
69 #on ne garde que les matches du niveau Keyword le plus haut
70 self.matches=self._matches
72 def visit_Tuple(self,node):
74 # Pour eviter les tuples et listes ordinaires,
75 if not hasattr(node,'getChildNodes') : return
76 print ('*********************************************************************')
77 print ("_____________ visit_Tuple", node)
78 for child in node.getChildNodes():
82 # Pour eviter les tuples et listes ordinaires,
83 # on ne garde que les visites fructueuses
84 matchlist.append(self._matches)
85 self._matches=matchlist
86 #self.generic_visit(node)
88 visit_List=visit_Tuple
90 def visit_Name(self,node):
91 if debug : print ('visit_Name', node.id)
92 self.popWordsUpTo(node.id)
93 self.generic_visit(node)
95 def visit_AssName(self,node):
96 if debug : print ('visit_AssName', node.id)
97 self.popWordsUpTo(node.id)
98 self.generic_visit(node)