]> SALOME platform Git repositories - tools/eficas.git/blob - Traducteur/visiteur.py
Salome HOME
fusion avec la branche de dev pour le traducteur
[tools/eficas.git] / Traducteur / visiteur.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 re
22 from ast import NodeVisitor
23 debug=0
24
25 class MatchFinder (NodeVisitor):
26     """Visiteur de base : gestion des matches """
27     def reset(self,line):
28         self.matches=[]
29         self._matches = []
30         self.words = re.split("(\w+)", line) # every other one is a non word
31         self.positions = []
32         i = 0
33         for word in self.words:
34             self.positions.append(i)
35             i+=len(word)
36         self.index = 0
37         if debug : print ('fin reset', self.words)
38
39     def popWordsUpTo(self, word):
40         if 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:]
46
47     def appendMatch(self,name):
48         idx = self.getNextIndexOfWord(name)
49         self._matches.append((idx, name))
50
51     def getNextIndexOfWord(self,name):
52         return self.positions[self.words.index(name)]
53
54
55 class KeywordFinder(MatchFinder):
56     """Visiteur pour les keywords d'une commande """
57
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
63         self._matches = []
64         #for child in node.getChildNodes():
65         #    self.visit(child)
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
71
72     def visit_Tuple(self,node):
73         matchlist=[]
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():
79             self._matches = []
80             self.visit(child)
81             if self._matches:
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)
87
88     visit_List=visit_Tuple
89
90     def visit_Name(self,node):
91         if debug : print ('visit_Name', node.id)
92         self.popWordsUpTo(node.id)
93         self.generic_visit(node)
94
95     def visit_AssName(self,node):
96         if debug : print ('visit_AssName', node.id)
97         self.popWordsUpTo(node.id)
98         self.generic_visit(node)