Salome HOME
reindent + copyright + merge manuel avec la V9_dev sauf repertoires metier
[tools/eficas.git] / Traducteur / visiteur.py
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2021   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 compiler import visitor
23
24 class MatchFinder:
25     """Visiteur de base : gestion des matches """
26     def reset(self,line):
27         self.matches=[]
28         self._matches = []
29         self.words = re.split("(\w+)", line) # every other one is a non word
30         self.positions = []
31         i = 0
32         for word in self.words:
33             self.positions.append(i)
34             i+=len(word)
35         self.index = 0
36
37     def popWordsUpTo(self, word):
38         if word == "*":
39             return        # won't be able to find this
40         posInWords = self.words.index(word)
41         idx = self.positions[posInWords]
42         self.words = self.words[posInWords+1:]
43         self.positions = self.positions[posInWords+1:]
44
45     def appendMatch(self,name):
46         idx = self.getNextIndexOfWord(name)
47         self._matches.append((idx, name))
48
49     def getNextIndexOfWord(self,name):
50         return self.positions[self.words.index(name)]
51
52
53 class KeywordFinder(MatchFinder):
54     """Visiteur pour les keywords d'une commande """
55
56     def visitKeyword(self,node):
57         idx = self.getNextIndexOfWord(node.name)
58         self.popWordsUpTo(node.name)
59         prevmatches=self._matches
60         self._matches = []
61         for child in node.getChildNodes():
62             self.visit(child)
63         prevmatches.append((idx, node.name,self._matches))
64         self._matches=prevmatches
65         #on ne garde que les matches du niveau Keyword le plus haut
66         self.matches=self._matches
67
68     def visitTuple(self,node):
69         matchlist=[]
70         for child in node.getChildNodes():
71             self._matches = []
72             self.visit(child)
73             if self._matches:
74                 # Pour eviter les tuples et listes ordinaires,
75                 # on ne garde que les visites fructueuses
76                 matchlist.append(self._matches)
77         self._matches=matchlist
78
79     visitList=visitTuple
80
81     def visitName(self,node):
82         self.popWordsUpTo(node.name)
83
84     def visitAssName(self,node):
85         self.popWordsUpTo(node.name)