1 # -*- coding: utf-8 -*-
5 from parseur import Keyword,FactNode,lastparen,lastparen2
6 from visiteur import KeywordFinder,visitor
7 from utils import indexToCoordinates
11 def parseFact(match,c,kw):
13 lastpar=match[0]+lastparen(c.src[match[0]:])
14 if type(submatch[0][0]) ==types.IntType:
15 #mot cle facteur isolé
18 for ii in range(len(submatch)-1):
20 x,y=indexToCoordinates(c.src,e[0])
23 x,y=indexToCoordinates(c.src,submatch[ii+1][0])
26 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
29 x,y=indexToCoordinates(c.src,e[0])
32 x,y=indexToCoordinates(c.src,lastpar-1)
35 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
37 #mot cle facteur multiple
40 lastpar=l[0][0]+lastparen2(c.src[l[0][0]:])
44 for j in range(len(l)-1):
46 x,y=indexToCoordinates(c.src,e[0])
49 x,y=indexToCoordinates(c.src,l[j+1][0])
52 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
55 x,y=indexToCoordinates(c.src,e[0])
58 x,y=indexToCoordinates(c.src,lastpar-1)
61 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
64 def parseKeywords(root):
65 """A partir d'un arbre contenant des commandes, ajoute les noeuds fils correspondant aux mocles
68 matchFinder=KeywordFinder()
70 for c in root.childNodes:
71 ast=compiler.parse(c.src)
73 matchFinder.reset(c.src)
74 visitor.walk(ast, matchFinder)
75 #print matchFinder.matches
76 if len(matchFinder.matches) > 1:
77 #plusieurs mocles trouvés : un mocle commence au début du keyword (matchFinder.matches[i][0])
78 # et finit juste avant le keyword suivant (matchFinder.matches[i+1][0]])
79 for i in range(len(matchFinder.matches)-1):
80 if debug:print "texte:",c.src[matchFinder.matches[i][0]:matchFinder.matches[i+1][0]]
81 x,y=indexToCoordinates(c.src,matchFinder.matches[i][0])
84 x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
87 if debug:print matchFinder.matches[i][0],matchFinder.matches[i][1],lineno,colno,endline,endcol
88 kw=Keyword(matchFinder.matches[i][1],lineno,colno,endline,endcol)
90 submatch= matchFinder.matches[i][2]
92 parseFact(matchFinder.matches[i],c,kw)
93 #dernier mocle : il commence au debut du dernier keyword (matchFinder.matches[i+1][0]) et
94 #finit avant la parenthese fermante de la commande (c.lastparen)
95 if debug:print "texte:",c.src[matchFinder.matches[i+1][0]:c.lastparen]
96 x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
99 x,y=indexToCoordinates(c.src,c.lastparen)
102 if debug:print matchFinder.matches[i+1][0],matchFinder.matches[i+1][1],lineno,colno,endline,endcol
103 kw=Keyword(matchFinder.matches[i+1][1],lineno,colno,endline,endcol)
105 submatch= matchFinder.matches[i+1][2]
107 parseFact(matchFinder.matches[i+1],c,kw)
109 elif len(matchFinder.matches) == 1:
110 #un seul mocle trouve : il commence au début du keyword (matchFinder.matches[0][0]) et
111 #finit juste avant la parenthese fermante de la commande (c.lastparen)
112 if debug:print "texte:",c.src[matchFinder.matches[0][0]:c.lastparen]
113 x,y=indexToCoordinates(c.src,matchFinder.matches[0][0])
116 x,y=indexToCoordinates(c.src,c.lastparen)
119 if debug:print matchFinder.matches[0][0],matchFinder.matches[0][1],lineno,colno,endline,endcol
120 kw=Keyword(matchFinder.matches[0][1],lineno,colno,endline,endcol)
122 submatch= matchFinder.matches[0][2]
124 parseFact(matchFinder.matches[0],c,kw)