1 # -*- coding: utf-8 -*-
5 from parseur import Keyword, FactNode, lastparen, lastparen2,maskStringsAndComments
6 from visiteur import KeywordFinder, visitor
7 from utils import indexToCoordinates
12 #------------------------
13 def parseFact(match,c,kw):
14 #------------------------
16 lastpar=match[0]+lastparen(c.src[match[0]:])
17 if type(submatch[0][0]) ==types.IntType:
18 #mot cle facteur isolé
21 for ii in range(len(submatch)-1):
23 x,y=indexToCoordinates(c.src,e[0])
26 x,y=indexToCoordinates(c.src,submatch[ii+1][0])
29 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
32 x,y=indexToCoordinates(c.src,e[0])
35 x,y=indexToCoordinates(c.src,lastpar-1)
38 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
40 #mot cle facteur multiple
43 lastpar=l[0][0]+lastparen2(c.src[l[0][0]:])
47 for j in range(len(l)-1):
49 x,y=indexToCoordinates(c.src,e[0])
52 x,y=indexToCoordinates(c.src,l[j+1][0])
55 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
58 x,y=indexToCoordinates(c.src,e[0])
61 x,y=indexToCoordinates(c.src,lastpar-1)
64 no.addChild(Keyword(e[1],lineno,colno,endline,endcol))
67 #-----------------------
68 def parseKeywords(root):
69 #-----------------------
70 """A partir d'un arbre contenant des commandes, ajoute les noeuds
71 fils correspondant aux mocles de la commande
73 #print "parseKeywords"
74 #traceback.print_stack(limit=5)
76 matchFinder=KeywordFinder()
78 for c in root.childNodes:
79 maskedsrc=maskStringsAndComments(c.src)
80 #on supprime seulement les blancs du debut pour pouvoir compiler
81 #meme si la commande est sur plusieurs lignes seul le debut compte
82 ast=compiler.parse(c.src.lstrip())
84 #Ne pas supprimer les blancs du debut pour avoir les bons numeros de colonne
85 matchFinder.reset(maskedsrc)
86 visitor.walk(ast, matchFinder)
87 #print matchFinder.matches
88 if len(matchFinder.matches) > 1:
89 # plusieurs mocles trouvés :
90 # un mocle commence au début du keyword (matchFinder.matches[i][0])
91 # et finit juste avant le keyword suivant
92 # (matchFinder.matches[i+1][0]])
93 for i in range(len(matchFinder.matches)-1):
94 if debug:print "texte:",c.src[matchFinder.matches[i][0]:matchFinder.matches[i+1][0]]
95 x,y=indexToCoordinates(c.src,matchFinder.matches[i][0])
98 x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
101 if debug:print matchFinder.matches[i][0],matchFinder.matches[i][1],lineno,colno,endline,endcol
102 kw=Keyword(matchFinder.matches[i][1],lineno,colno,endline,endcol)
104 submatch= matchFinder.matches[i][2]
106 parseFact(matchFinder.matches[i],c,kw)
109 # il commence au debut du dernier keyword
110 # (matchFinder.matches[i+1][0]) et
111 # finit avant la parenthese fermante de la commande (c.lastparen)
113 if debug:print "texte:",c.src[matchFinder.matches[i+1][0]:c.lastparen]
114 x,y=indexToCoordinates(c.src,matchFinder.matches[i+1][0])
117 x,y=indexToCoordinates(c.src,c.lastparen)
120 if debug:print matchFinder.matches[i+1][0],matchFinder.matches[i+1][1],lineno,colno,endline,endcol
121 kw=Keyword(matchFinder.matches[i+1][1],lineno,colno,endline,endcol)
123 submatch= matchFinder.matches[i+1][2]
125 parseFact(matchFinder.matches[i+1],c,kw)
127 elif len(matchFinder.matches) == 1:
128 #un seul mocle trouve :
129 # il commence au début du keyword (matchFinder.matches[0][0]) et
130 # finit juste avant la parenthese fermante de la
131 # commande (c.lastparen)
132 if debug:print "texte:",c.src[matchFinder.matches[0][0]:c.lastparen]
133 x,y=indexToCoordinates(c.src,matchFinder.matches[0][0])
136 x,y=indexToCoordinates(c.src,c.lastparen)
139 if debug:print matchFinder.matches[0][0],matchFinder.matches[0][1],lineno,colno,endline,endcol
140 kw=Keyword(matchFinder.matches[0][1],lineno,colno,endline,endcol)
142 submatch= matchFinder.matches[0][2]
144 parseFact(matchFinder.matches[0],c,kw)