40 |
40 |
41 class Function(ClbrBaseClasses.Function, VisibilityMixin): |
41 class Function(ClbrBaseClasses.Function, VisibilityMixin): |
42 """ |
42 """ |
43 Class to represent a Python function. |
43 Class to represent a Python function. |
44 """ |
44 """ |
45 def __init__(self, module, name, file, lineno, signature='', separator=','): |
45 def __init__(self, module, name, file, lineno, signature='', |
|
46 separator=','): |
46 """ |
47 """ |
47 Constructor |
48 Constructor |
48 |
49 |
49 @param module name of the module containing this function |
50 @param module name of the module containing this function |
50 @param name name of this function |
51 @param name name of this function |
111 |
112 |
112 return self.__dict |
113 return self.__dict |
113 |
114 |
114 def __visit(self, root): |
115 def __visit(self, root): |
115 """ |
116 """ |
116 Private method implementing the visit logic delegating to interesting methods. |
117 Private method implementing the visit logic delegating to interesting |
117 """ |
118 methods. |
118 call = lambda n: getattr(self, "visit_{0}".format(n.type), self.visit_noop)(n) |
119 |
|
120 @param root root node to visit |
|
121 """ |
|
122 call = lambda n: getattr(self, "visit_{0}".format(n.type), |
|
123 self.visit_noop)(n) |
119 call(root) |
124 call(root) |
120 for node in root: |
125 for node in root: |
121 self.__visit(node) |
126 self.__visit(node) |
122 |
127 |
123 def visit_noop(self, node): |
128 def visit_noop(self, node): |
137 if node.type == "function" and \ |
142 if node.type == "function" and \ |
138 getattr(node, "name", None) and \ |
143 getattr(node, "name", None) and \ |
139 node.functionForm == "declared_form": |
144 node.functionForm == "declared_form": |
140 if self.__stack and self.__stack[-1].endlineno < node.line: |
145 if self.__stack and self.__stack[-1].endlineno < node.line: |
141 del self.__stack[-1] |
146 del self.__stack[-1] |
142 endline = node.line + self.__source.count('\n', node.start, node.end) |
147 endline = node.line + self.__source.count( |
|
148 '\n', node.start, node.end) |
143 if getattr(node, "params", None): |
149 if getattr(node, "params", None): |
144 func_sig = ", ".join([p.value for p in node.params]) |
150 func_sig = ", ".join([p.value for p in node.params]) |
145 else: |
151 else: |
146 func_sig = "" |
152 func_sig = "" |
147 if self.__stack: |
153 if self.__stack: |
172 @param node reference to the node (jasy.js.parse.Node.Node) |
178 @param node reference to the node (jasy.js.parse.Node.Node) |
173 """ |
179 """ |
174 if node.type == "property_init" and node[1].type == "function": |
180 if node.type == "property_init" and node[1].type == "function": |
175 if self.__stack and self.__stack[-1].endlineno < node[0].line: |
181 if self.__stack and self.__stack[-1].endlineno < node[0].line: |
176 del self.__stack[-1] |
182 del self.__stack[-1] |
177 endline = node[0].line + self.__source.count('\n', node.start, node[1].end) |
183 endline = node[0].line + self.__source.count( |
|
184 '\n', node.start, node[1].end) |
178 if getattr(node[1], "params", None): |
185 if getattr(node[1], "params", None): |
179 func_sig = ", ".join([p.value for p in node[1].params]) |
186 func_sig = ", ".join([p.value for p in node[1].params]) |
180 else: |
187 else: |
181 func_sig = "" |
188 func_sig = "" |
182 if self.__stack: |
189 if self.__stack: |
212 if self.__stack and self.__stack[-1].endlineno < node[0].line: |
219 if self.__stack and self.__stack[-1].endlineno < node[0].line: |
213 del self.__stack[-1] |
220 del self.__stack[-1] |
214 if self.__stack: |
221 if self.__stack: |
215 # function variables |
222 # function variables |
216 for var in node: |
223 for var in node: |
217 attr = Attribute(self.__module, var.name, self.__file, var.line) |
224 attr = Attribute( |
|
225 self.__module, var.name, self.__file, var.line) |
218 self.__stack[-1]._addattribute(attr) |
226 self.__stack[-1]._addattribute(attr) |
219 else: |
227 else: |
220 # global variable |
228 # global variable |
221 if "@@Globals@@" not in self.__dict: |
229 if "@@Globals@@" not in self.__dict: |
222 self.__dict["@@Globals@@"] = \ |
230 self.__dict["@@Globals@@"] = ClbrBaseClasses.ClbrBase( |
223 ClbrBaseClasses.ClbrBase(self.__module, "Globals", self.__file, 0) |
231 self.__module, "Globals", self.__file, 0) |
224 for var in node: |
232 for var in node: |
225 self.__dict["@@Globals@@"]._addglobal( |
233 self.__dict["@@Globals@@"]._addglobal(Attribute( |
226 Attribute(self.__module, var.name, self.__file, var.line)) |
234 self.__module, var.name, self.__file, var.line)) |
227 |
235 |
228 def visit_const(self, node): |
236 def visit_const(self, node): |
229 """ |
237 """ |
230 Public method to treat a constant node. |
238 Public method to treat a constant node. |
231 |
239 |
243 self.__file, var.line) |
251 self.__file, var.line) |
244 self.__stack[-1]._addattribute(attr) |
252 self.__stack[-1]._addattribute(attr) |
245 else: |
253 else: |
246 # global variable |
254 # global variable |
247 if "@@Globals@@" not in self.__dict: |
255 if "@@Globals@@" not in self.__dict: |
248 self.__dict["@@Globals@@"] = \ |
256 self.__dict["@@Globals@@"] = ClbrBaseClasses.ClbrBase( |
249 ClbrBaseClasses.ClbrBase(self.__module, "Globals", self.__file, 0) |
257 self.__module, "Globals", self.__file, 0) |
250 for var in node: |
258 for var in node: |
251 self.__dict["@@Globals@@"]._addglobal( |
259 self.__dict["@@Globals@@"]._addglobal( |
252 Attribute(self.__module, "const " + var.name, |
260 Attribute(self.__module, "const " + var.name, |
253 self.__file, var.line)) |
261 self.__file, var.line)) |
254 |
262 |
255 |
263 |
256 def readmodule_ex(module, path=[]): |
264 def readmodule_ex(module, path=[]): |
257 ''' |
265 """ |
258 Read a JavaScript file and return a dictionary of functions and variables. |
266 Read a JavaScript file and return a dictionary of functions and variables. |
259 |
267 |
260 @param module name of the JavaScript file (string) |
268 @param module name of the JavaScript file (string) |
261 @param path path the file should be searched in (list of strings) |
269 @param path path the file should be searched in (list of strings) |
262 @return the resulting dictionary |
270 @return the resulting dictionary |
263 ''' |
271 """ |
264 global _modules |
272 global _modules |
265 |
273 |
266 dict = {} |
274 dict = {} |
267 |
275 |
268 if module in _modules: |
276 if module in _modules: |