]> SALOME platform Git repositories - tools/eficas.git/blob - Traducteur/visiteur.py
Salome HOME
merge de la branche BR_dev_mars_06 (tag V1_10b5) 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.words = re.split("(\w+)", line) # every other one is a non word
11         self.positions = []
12         i = 0
13         for word in self.words:
14             self.positions.append(i)
15             i+=len(word)
16         self.index = 0
17
18     def popWordsUpTo(self, word):
19         if word == "*":
20             return        # won't be able to find this
21         posInWords = self.words.index(word)
22         idx = self.positions[posInWords]
23         self.words = self.words[posInWords+1:]
24         self.positions = self.positions[posInWords+1:]
25
26     def appendMatch(self,name):
27         idx = self.getNextIndexOfWord(name)
28         self.matches.append((idx, name))
29
30     def getNextIndexOfWord(self,name):
31         return self.positions[self.words.index(name)]
32
33
34 class KeywordFinder(MatchFinder):
35     """Visiteur pour les keywords d'une commande """
36
37     def visitKeyword(self,node):
38         idx = self.getNextIndexOfWord(node.name)
39         #self.appendMatch(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
48     def visitTuple(self,node):
49         matchlist=[]
50         for child in node.getChildNodes():
51             self.matches = []
52             self.visit(child)
53             if self.matches:
54                 #Pour eviter les tuples et listes ordinaires, on ne garde que les visites fructueuses
55                 matchlist.append(self.matches)
56         self.matches=matchlist
57
58     visitList=visitTuple
59
60     def visitName(self,node):
61         self.popWordsUpTo(node.name)
62     def visitAssName(self,node):
63         self.popWordsUpTo(node.name)