]> SALOME platform Git repositories - tools/eficas.git/blob - Traducteur/visiteur.py
Salome HOME
CCAR: merge de la version de developpement V1_12a2 dans la branche principale
[tools/eficas.git] / Traducteur / visiteur.py
1 # -*- coding: utf-8 -*-
2
3 import re
4 from compiler import visitor
5
6 class MatchFinder:
7     """Visiteur de base : gestion des matches """
8     def reset(self,line):
9         self.matches=[]
10         self._matches = []
11         self.words = re.split("(\w+)", line) # every other one is a non word
12         self.positions = []
13         i = 0
14         for word in self.words:
15             self.positions.append(i)
16             i+=len(word)
17         self.index = 0
18
19     def popWordsUpTo(self, word):
20         if word == "*":
21             return        # won't be able to find this
22         posInWords = self.words.index(word)
23         idx = self.positions[posInWords]
24         self.words = self.words[posInWords+1:]
25         self.positions = self.positions[posInWords+1:]
26
27     def appendMatch(self,name):
28         idx = self.getNextIndexOfWord(name)
29         self._matches.append((idx, name))
30
31     def getNextIndexOfWord(self,name):
32         return self.positions[self.words.index(name)]
33
34
35 class KeywordFinder(MatchFinder):
36     """Visiteur pour les keywords d'une commande """
37
38     def visitKeyword(self,node):
39         idx = self.getNextIndexOfWord(node.name)
40         self.popWordsUpTo(node.name)
41         prevmatches=self._matches
42         self._matches = []
43         for child in node.getChildNodes():
44             self.visit(child)
45         prevmatches.append((idx, node.name,self._matches))
46         self._matches=prevmatches
47         #on ne garde que les matches du niveau Keyword le plus haut
48         self.matches=self._matches
49
50     def visitTuple(self,node):
51         matchlist=[]
52         for child in node.getChildNodes():
53             self._matches = []
54             self.visit(child)
55             if self._matches:
56                 # Pour eviter les tuples et listes ordinaires, 
57                 # on ne garde que les visites fructueuses
58                 matchlist.append(self._matches)
59         self._matches=matchlist
60
61     visitList=visitTuple
62
63     def visitName(self,node):
64         self.popWordsUpTo(node.name)
65
66     def visitAssName(self,node):
67         self.popWordsUpTo(node.name)