blob: d1001bdc3761dd3f5715f6385077dfba8a214ecd [file] [log] [blame]
Jeremy Hyltonfa974a92000-03-06 18:50:48 +00001"""Parse tree transformation module.
2
3Transforms Python source code into an abstract syntax tree (AST)
4defined in the ast module.
5
6The simplest ways to invoke this module are via parse and parseFile.
7parse(buf) -> AST
8parseFile(path) -> AST
9"""
10
Jeremy Hylton9c048f92000-10-13 21:58:13 +000011# Original version written by Greg Stein (gstein@lyra.org)
12# and Bill Tutt (rassilon@lima.mudlib.org)
13# February 1997.
Jeremy Hyltonf968e852000-02-04 00:25:23 +000014#
Jeremy Hylton9c048f92000-10-13 21:58:13 +000015# Modifications and improvements for Python 2.0 by Jeremy Hylton and
16# Mark Hammond
17
18# Portions of this file are:
19# Copyright (C) 1997-1998 Greg Stein. All Rights Reserved.
20#
21# This module is provided under a BSD-ish license. See
22# http://www.opensource.org/licenses/bsd-license.html
23# and replace OWNER, ORGANIZATION, and YEAR as appropriate.
24
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +000025from ast import *
Jeremy Hyltonf968e852000-02-04 00:25:23 +000026import parser
Jeremy Hylton9c048f92000-10-13 21:58:13 +000027# Care must be taken to use only symbols and tokens defined in Python
28# 1.5.2 for code branches executed in 1.5.2
Jeremy Hyltonf968e852000-02-04 00:25:23 +000029import symbol
30import token
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +000031import sys
Jeremy Hyltonf968e852000-02-04 00:25:23 +000032
Jeremy Hyltonf968e852000-02-04 00:25:23 +000033error = 'walker.error'
34
Jeremy Hylton9605c112000-02-08 18:57:32 +000035from consts import CO_VARARGS, CO_VARKEYWORDS
36from consts import OP_ASSIGN, OP_DELETE, OP_APPLY
Jeremy Hyltonf968e852000-02-04 00:25:23 +000037
Jeremy Hyltonfa974a92000-03-06 18:50:48 +000038def parseFile(path):
39 f = open(path)
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000040 # XXX The parser API tolerates files without a trailing newline,
41 # but not strings without a trailing newline. Always add an extra
42 # newline to the file contents, since we're going through the string
43 # version of the API.
44 src = f.read() + "\n"
Jeremy Hyltonfa974a92000-03-06 18:50:48 +000045 f.close()
46 return parse(src)
47
Jeremy Hylton9dca3642001-09-17 21:02:51 +000048def parse(buf, mode="exec"):
49 if mode == "exec" or mode == "single":
50 return Transformer().parsesuite(buf)
51 elif mode == "eval":
52 return Transformer().parseexpr(buf)
53 else:
54 raise ValueError("compile() arg 3 must be"
55 " 'exec' or 'eval' or 'single'")
Jeremy Hyltonfa974a92000-03-06 18:50:48 +000056
Jeremy Hyltonf968e852000-02-04 00:25:23 +000057def asList(nodes):
Jeremy Hylton572bdce2000-09-20 02:47:28 +000058 l = []
59 for item in nodes:
60 if hasattr(item, "asList"):
61 l.append(item.asList())
62 else:
63 if type(item) is type( (None, None) ):
64 l.append(tuple(asList(item)))
65 elif type(item) is type( [] ):
66 l.append(asList(item))
67 else:
68 l.append(item)
69 return l
Jeremy Hyltonf968e852000-02-04 00:25:23 +000070
71def Node(*args):
Jeremy Hylton572bdce2000-09-20 02:47:28 +000072 kind = args[0]
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +000073 if nodes.has_key(kind):
Jeremy Hylton572bdce2000-09-20 02:47:28 +000074 try:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +000075 return apply(nodes[kind], args[1:])
Jeremy Hylton572bdce2000-09-20 02:47:28 +000076 except TypeError:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +000077 print nodes[kind], len(args), args
Jeremy Hylton572bdce2000-09-20 02:47:28 +000078 raise
79 else:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +000080 raise error, "Can't find appropriate Node type: %s" % str(args)
Jeremy Hylton572bdce2000-09-20 02:47:28 +000081 #return apply(ast.Node, args)
Jeremy Hyltonf968e852000-02-04 00:25:23 +000082
83class Transformer:
Jeremy Hylton572bdce2000-09-20 02:47:28 +000084 """Utility object for transforming Python parse trees.
Jeremy Hyltonf968e852000-02-04 00:25:23 +000085
Jeremy Hylton572bdce2000-09-20 02:47:28 +000086 Exposes the following methods:
87 tree = transform(ast_tree)
88 tree = parsesuite(text)
89 tree = parseexpr(text)
90 tree = parsefile(fileob | filename)
91 """
Jeremy Hyltonf968e852000-02-04 00:25:23 +000092
Jeremy Hylton572bdce2000-09-20 02:47:28 +000093 def __init__(self):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +000094 self._dispatch = {}
Jeremy Hylton572bdce2000-09-20 02:47:28 +000095 for value, name in symbol.sym_name.items():
96 if hasattr(self, name):
97 self._dispatch[value] = getattr(self, name)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +000098 self._dispatch[token.NEWLINE] = self.com_NEWLINE
99 self._atom_dispatch = {token.LPAR: self.atom_lpar,
100 token.LSQB: self.atom_lsqb,
101 token.LBRACE: self.atom_lbrace,
102 token.BACKQUOTE: self.atom_backquote,
103 token.NUMBER: self.atom_number,
104 token.STRING: self.atom_string,
105 token.NAME: self.atom_name,
106 }
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000107 self.encoding = None
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000108
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000109 def transform(self, tree):
110 """Transform an AST into a modified parse tree."""
111 if type(tree) != type(()) and type(tree) != type([]):
Jeremy Hylton7845cf82001-08-29 20:56:30 +0000112 tree = parser.ast2tuple(tree, line_info=1)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000113 return self.compile_node(tree)
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000114
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000115 def parsesuite(self, text):
116 """Return a modified parse tree for the given suite text."""
117 # Hack for handling non-native line endings on non-DOS like OSs.
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000118 # this can go now we have universal newlines?
Neal Norwitza312c3a2002-06-06 18:30:10 +0000119 text = text.replace('\x0d', '')
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000120 return self.transform(parser.suite(text))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000121
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000122 def parseexpr(self, text):
123 """Return a modified parse tree for the given expression text."""
124 return self.transform(parser.expr(text))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000125
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000126 def parsefile(self, file):
127 """Return a modified parse tree for the contents of the given file."""
128 if type(file) == type(''):
129 file = open(file)
130 return self.parsesuite(file.read())
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000131
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000132 # --------------------------------------------------------------
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000133 #
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000134 # PRIVATE METHODS
135 #
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000136
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000137 def compile_node(self, node):
138 ### emit a line-number node?
139 n = node[0]
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000140
141 if n == symbol.encoding_decl:
142 self.encoding = node[2]
143 node = node[1]
144 n = node[0]
145
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000146 if n == symbol.single_input:
147 return self.single_input(node[1:])
148 if n == symbol.file_input:
149 return self.file_input(node[1:])
150 if n == symbol.eval_input:
151 return self.eval_input(node[1:])
152 if n == symbol.lambdef:
153 return self.lambdef(node[1:])
154 if n == symbol.funcdef:
155 return self.funcdef(node[1:])
156 if n == symbol.classdef:
157 return self.classdef(node[1:])
158
159 raise error, ('unexpected node type', n)
160
161 def single_input(self, node):
162 ### do we want to do anything about being "interactive" ?
163
164 # NEWLINE | simple_stmt | compound_stmt NEWLINE
165 n = node[0][0]
166 if n != token.NEWLINE:
167 return self.com_stmt(node[0])
168
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000169 return Pass()
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000170
171 def file_input(self, nodelist):
172 doc = self.get_docstring(nodelist, symbol.file_input)
Jeremy Hyltonec927342001-08-18 00:04:31 +0000173 if doc is not None:
174 i = 1
175 else:
176 i = 0
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000177 stmts = []
Jeremy Hyltonec927342001-08-18 00:04:31 +0000178 for node in nodelist[i:]:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000179 if node[0] != token.ENDMARKER and node[0] != token.NEWLINE:
180 self.com_append_stmt(stmts, node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000181 return Module(doc, Stmt(stmts))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000182
183 def eval_input(self, nodelist):
184 # from the built-in function input()
185 ### is this sufficient?
Barry Warsaw52acb492001-12-21 20:04:22 +0000186 return Expression(self.com_node(nodelist[0]))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000187
188 def funcdef(self, nodelist):
189 # funcdef: 'def' NAME parameters ':' suite
190 # parameters: '(' [varargslist] ')'
191
192 lineno = nodelist[1][2]
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000193 name = nodelist[1][1]
194 args = nodelist[2][2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000195
196 if args[0] == symbol.varargslist:
197 names, defaults, flags = self.com_arglist(args[1:])
198 else:
199 names = defaults = ()
200 flags = 0
201 doc = self.get_docstring(nodelist[4])
202
203 # code for function
204 code = self.com_node(nodelist[4])
205
Jeremy Hyltona59ac0a2000-11-06 03:33:52 +0000206 if doc is not None:
207 assert isinstance(code, Stmt)
208 assert isinstance(code.nodes[0], Discard)
209 del code.nodes[0]
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000210 n = Function(name, names, defaults, flags, doc, code)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000211 n.lineno = lineno
212 return n
213
214 def lambdef(self, nodelist):
215 # lambdef: 'lambda' [varargslist] ':' test
216 if nodelist[2][0] == symbol.varargslist:
217 names, defaults, flags = self.com_arglist(nodelist[2][1:])
218 else:
219 names = defaults = ()
220 flags = 0
221
222 # code for lambda
223 code = self.com_node(nodelist[-1])
224
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000225 n = Lambda(names, defaults, flags, code)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000226 n.lineno = nodelist[1][2]
227 return n
228
229 def classdef(self, nodelist):
230 # classdef: 'class' NAME ['(' testlist ')'] ':' suite
231
232 name = nodelist[1][1]
233 doc = self.get_docstring(nodelist[-1])
234 if nodelist[2][0] == token.COLON:
235 bases = []
236 else:
237 bases = self.com_bases(nodelist[3])
238
239 # code for class
240 code = self.com_node(nodelist[-1])
241
Jeremy Hyltonda8db8c2001-08-29 17:19:02 +0000242 if doc is not None:
243 assert isinstance(code, Stmt)
244 assert isinstance(code.nodes[0], Discard)
245 del code.nodes[0]
246
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000247 n = Class(name, bases, doc, code)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000248 n.lineno = nodelist[1][2]
249 return n
250
251 def stmt(self, nodelist):
252 return self.com_stmt(nodelist[0])
253
254 small_stmt = stmt
255 flow_stmt = stmt
256 compound_stmt = stmt
257
258 def simple_stmt(self, nodelist):
259 # small_stmt (';' small_stmt)* [';'] NEWLINE
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000260 stmts = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000261 for i in range(0, len(nodelist), 2):
262 self.com_append_stmt(stmts, nodelist[i])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000263 return Stmt(stmts)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000264
265 def parameters(self, nodelist):
266 raise error
267
268 def varargslist(self, nodelist):
269 raise error
270
271 def fpdef(self, nodelist):
272 raise error
273
274 def fplist(self, nodelist):
275 raise error
276
277 def dotted_name(self, nodelist):
278 raise error
279
280 def comp_op(self, nodelist):
281 raise error
282
283 def trailer(self, nodelist):
284 raise error
285
286 def sliceop(self, nodelist):
287 raise error
288
289 def argument(self, nodelist):
290 raise error
291
292 # --------------------------------------------------------------
293 #
294 # STATEMENT NODES (invoked by com_node())
295 #
296
297 def expr_stmt(self, nodelist):
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000298 # augassign testlist | testlist ('=' testlist)*
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +0000299 en = nodelist[-1]
300 exprNode = self.lookup_node(en)(en[1:])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000301 if len(nodelist) == 1:
Jeremy Hylton4c1f4272001-04-11 16:22:26 +0000302 n = Discard(exprNode)
303 n.lineno = exprNode.lineno
304 return n
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000305 if nodelist[1][0] == token.EQUAL:
Martin v. Löwis51e234a2002-02-28 17:48:48 +0000306 nodesl = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000307 for i in range(0, len(nodelist) - 2, 2):
Martin v. Löwis51e234a2002-02-28 17:48:48 +0000308 nodesl.append(self.com_assign(nodelist[i], OP_ASSIGN))
309 n = Assign(nodesl, exprNode)
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000310 n.lineno = nodelist[1][2]
311 else:
312 lval = self.com_augassign(nodelist[0])
313 op = self.com_augassign_op(nodelist[1])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000314 n = AugAssign(lval, op[1], exprNode)
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000315 n.lineno = op[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000316 return n
317
318 def print_stmt(self, nodelist):
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000319 # print ([ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000320 items = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000321 if len(nodelist) == 1:
322 start = 1
323 dest = None
324 elif nodelist[1][0] == token.RIGHTSHIFT:
325 assert len(nodelist) == 3 \
326 or nodelist[3][0] == token.COMMA
327 dest = self.com_node(nodelist[2])
328 start = 4
329 else:
330 dest = None
331 start = 1
332 for i in range(start, len(nodelist), 2):
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000333 items.append(self.com_node(nodelist[i]))
334 if nodelist[-1][0] == token.COMMA:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000335 n = Print(items, dest)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000336 n.lineno = nodelist[0][2]
337 return n
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000338 n = Printnl(items, dest)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000339 n.lineno = nodelist[0][2]
340 return n
341
342 def del_stmt(self, nodelist):
343 return self.com_assign(nodelist[1], OP_DELETE)
344
345 def pass_stmt(self, nodelist):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000346 n = Pass()
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000347 n.lineno = nodelist[0][2]
348 return n
349
350 def break_stmt(self, nodelist):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000351 n = Break()
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000352 n.lineno = nodelist[0][2]
353 return n
354
355 def continue_stmt(self, nodelist):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000356 n = Continue()
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000357 n.lineno = nodelist[0][2]
358 return n
359
360 def return_stmt(self, nodelist):
361 # return: [testlist]
362 if len(nodelist) < 2:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000363 n = Return(Const(None))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000364 n.lineno = nodelist[0][2]
365 return n
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000366 n = Return(self.com_node(nodelist[1]))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000367 n.lineno = nodelist[0][2]
368 return n
369
Jeremy Hyltonec927342001-08-18 00:04:31 +0000370 def yield_stmt(self, nodelist):
371 n = Yield(self.com_node(nodelist[1]))
372 n.lineno = nodelist[0][2]
373 return n
374
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000375 def raise_stmt(self, nodelist):
376 # raise: [test [',' test [',' test]]]
377 if len(nodelist) > 5:
378 expr3 = self.com_node(nodelist[5])
379 else:
380 expr3 = None
381 if len(nodelist) > 3:
382 expr2 = self.com_node(nodelist[3])
383 else:
384 expr2 = None
385 if len(nodelist) > 1:
386 expr1 = self.com_node(nodelist[1])
387 else:
388 expr1 = None
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000389 n = Raise(expr1, expr2, expr3)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000390 n.lineno = nodelist[0][2]
391 return n
392
393 def import_stmt(self, nodelist):
394 # import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
395 # from: 'from' dotted_name 'import'
396 # ('*' | import_as_name (',' import_as_name)*)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000397 if nodelist[0][1] == 'from':
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000398 names = []
399 if nodelist[3][0] == token.NAME:
400 for i in range(3, len(nodelist), 2):
401 names.append((nodelist[i][1], None))
402 else:
403 for i in range(3, len(nodelist), 2):
Jeremy Hylton42a08302001-04-09 04:27:12 +0000404 names.append(self.com_import_as_name(nodelist[i]))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000405 n = From(self.com_dotted_name(nodelist[1]), names)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000406 n.lineno = nodelist[0][2]
407 return n
408
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000409 if nodelist[1][0] == symbol.dotted_name:
410 names = [(self.com_dotted_name(nodelist[1][1:]), None)]
411 else:
412 names = []
413 for i in range(1, len(nodelist), 2):
414 names.append(self.com_dotted_as_name(nodelist[i]))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000415 n = Import(names)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000416 n.lineno = nodelist[0][2]
417 return n
418
419 def global_stmt(self, nodelist):
420 # global: NAME (',' NAME)*
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000421 names = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000422 for i in range(1, len(nodelist), 2):
423 names.append(nodelist[i][1])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000424 n = Global(names)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000425 n.lineno = nodelist[0][2]
426 return n
427
428 def exec_stmt(self, nodelist):
429 # exec_stmt: 'exec' expr ['in' expr [',' expr]]
430 expr1 = self.com_node(nodelist[1])
431 if len(nodelist) >= 4:
432 expr2 = self.com_node(nodelist[3])
433 if len(nodelist) >= 6:
434 expr3 = self.com_node(nodelist[5])
435 else:
436 expr3 = None
437 else:
438 expr2 = expr3 = None
439
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000440 n = Exec(expr1, expr2, expr3)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000441 n.lineno = nodelist[0][2]
442 return n
443
444 def assert_stmt(self, nodelist):
445 # 'assert': test, [',' test]
446 expr1 = self.com_node(nodelist[1])
447 if (len(nodelist) == 4):
448 expr2 = self.com_node(nodelist[3])
449 else:
Jeremy Hyltona59ac0a2000-11-06 03:33:52 +0000450 expr2 = None
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000451 n = Assert(expr1, expr2)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000452 n.lineno = nodelist[0][2]
453 return n
454
455 def if_stmt(self, nodelist):
456 # if: test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000457 tests = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000458 for i in range(0, len(nodelist) - 3, 4):
459 testNode = self.com_node(nodelist[i + 1])
460 suiteNode = self.com_node(nodelist[i + 3])
461 tests.append((testNode, suiteNode))
462
463 if len(nodelist) % 4 == 3:
464 elseNode = self.com_node(nodelist[-1])
465## elseNode.lineno = nodelist[-1][1][2]
466 else:
467 elseNode = None
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000468 n = If(tests, elseNode)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000469 n.lineno = nodelist[0][2]
470 return n
471
472 def while_stmt(self, nodelist):
473 # 'while' test ':' suite ['else' ':' suite]
474
475 testNode = self.com_node(nodelist[1])
476 bodyNode = self.com_node(nodelist[3])
477
478 if len(nodelist) > 4:
479 elseNode = self.com_node(nodelist[6])
480 else:
481 elseNode = None
482
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000483 n = While(testNode, bodyNode, elseNode)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000484 n.lineno = nodelist[0][2]
485 return n
486
487 def for_stmt(self, nodelist):
488 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
489
490 assignNode = self.com_assign(nodelist[1], OP_ASSIGN)
491 listNode = self.com_node(nodelist[3])
492 bodyNode = self.com_node(nodelist[5])
493
494 if len(nodelist) > 8:
495 elseNode = self.com_node(nodelist[8])
496 else:
497 elseNode = None
498
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000499 n = For(assignNode, listNode, bodyNode, elseNode)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000500 n.lineno = nodelist[0][2]
501 return n
502
503 def try_stmt(self, nodelist):
504 # 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
505 # | 'try' ':' suite 'finally' ':' suite
506 if nodelist[3][0] != symbol.except_clause:
507 return self.com_try_finally(nodelist)
508
509 return self.com_try_except(nodelist)
510
511 def suite(self, nodelist):
512 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
513 if len(nodelist) == 1:
514 return self.com_stmt(nodelist[0])
515
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000516 stmts = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000517 for node in nodelist:
518 if node[0] == symbol.stmt:
519 self.com_append_stmt(stmts, node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000520 return Stmt(stmts)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000521
522 # --------------------------------------------------------------
523 #
524 # EXPRESSION NODES (invoked by com_node())
525 #
526
527 def testlist(self, nodelist):
528 # testlist: expr (',' expr)* [',']
Jeremy Hyltond1142612001-10-17 13:32:52 +0000529 # testlist_safe: test [(',' test)+ [',']]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000530 # exprlist: expr (',' expr)* [',']
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000531 return self.com_binary(Tuple, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000532
Jeremy Hyltond1142612001-10-17 13:32:52 +0000533 testlist_safe = testlist # XXX
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000534 testlist1 = testlist
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000535 exprlist = testlist
536
537 def test(self, nodelist):
538 # and_test ('or' and_test)* | lambdef
539 if len(nodelist) == 1 and nodelist[0][0] == symbol.lambdef:
540 return self.lambdef(nodelist[0])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000541 return self.com_binary(Or, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000542
543 def and_test(self, nodelist):
544 # not_test ('and' not_test)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000545 return self.com_binary(And, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000546
547 def not_test(self, nodelist):
548 # 'not' not_test | comparison
549 result = self.com_node(nodelist[-1])
550 if len(nodelist) == 2:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000551 n = Not(result)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000552 n.lineno = nodelist[0][2]
553 return n
554 return result
555
556 def comparison(self, nodelist):
557 # comparison: expr (comp_op expr)*
558 node = self.com_node(nodelist[0])
559 if len(nodelist) == 1:
560 return node
561
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000562 results = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000563 for i in range(2, len(nodelist), 2):
564 nl = nodelist[i-1]
565
566 # comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
567 # | 'in' | 'not' 'in' | 'is' | 'is' 'not'
568 n = nl[1]
569 if n[0] == token.NAME:
570 type = n[1]
571 if len(nl) == 3:
572 if type == 'not':
573 type = 'not in'
574 else:
575 type = 'is not'
576 else:
577 type = _cmp_types[n[0]]
578
579 lineno = nl[1][2]
580 results.append((type, self.com_node(nodelist[i])))
581
582 # we need a special "compare" node so that we can distinguish
583 # 3 < x < 5 from (3 < x) < 5
584 # the two have very different semantics and results (note that the
585 # latter form is always true)
586
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000587 n = Compare(node, results)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000588 n.lineno = lineno
589 return n
590
591 def expr(self, nodelist):
592 # xor_expr ('|' xor_expr)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000593 return self.com_binary(Bitor, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000594
595 def xor_expr(self, nodelist):
596 # xor_expr ('^' xor_expr)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000597 return self.com_binary(Bitxor, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000598
599 def and_expr(self, nodelist):
600 # xor_expr ('&' xor_expr)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000601 return self.com_binary(Bitand, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000602
603 def shift_expr(self, nodelist):
604 # shift_expr ('<<'|'>>' shift_expr)*
605 node = self.com_node(nodelist[0])
606 for i in range(2, len(nodelist), 2):
607 right = self.com_node(nodelist[i])
608 if nodelist[i-1][0] == token.LEFTSHIFT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000609 node = LeftShift([node, right])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000610 node.lineno = nodelist[1][2]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000611 elif nodelist[i-1][0] == token.RIGHTSHIFT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000612 node = RightShift([node, right])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000613 node.lineno = nodelist[1][2]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000614 else:
615 raise ValueError, "unexpected token: %s" % nodelist[i-1][0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000616 return node
617
618 def arith_expr(self, nodelist):
619 node = self.com_node(nodelist[0])
620 for i in range(2, len(nodelist), 2):
621 right = self.com_node(nodelist[i])
622 if nodelist[i-1][0] == token.PLUS:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000623 node = Add([node, right])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000624 node.lineno = nodelist[1][2]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000625 elif nodelist[i-1][0] == token.MINUS:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000626 node = Sub([node, right])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000627 node.lineno = nodelist[1][2]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000628 else:
629 raise ValueError, "unexpected token: %s" % nodelist[i-1][0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000630 return node
631
632 def term(self, nodelist):
633 node = self.com_node(nodelist[0])
634 for i in range(2, len(nodelist), 2):
635 right = self.com_node(nodelist[i])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000636 t = nodelist[i-1][0]
637 if t == token.STAR:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000638 node = Mul([node, right])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000639 elif t == token.SLASH:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000640 node = Div([node, right])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000641 elif t == token.PERCENT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000642 node = Mod([node, right])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000643 elif t == token.DOUBLESLASH:
644 node = FloorDiv([node, right])
645 else:
646 raise ValueError, "unexpected token: %s" % t
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000647 node.lineno = nodelist[1][2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000648 return node
649
650 def factor(self, nodelist):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000651 elt = nodelist[0]
652 t = elt[0]
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000653 print "source", nodelist[-1]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000654 node = self.com_node(nodelist[-1])
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000655 # need to handle (unary op)constant here...
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000656 if t == token.PLUS:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000657 node = UnaryAdd(node)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000658 node.lineno = elt[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000659 elif t == token.MINUS:
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000660 print node
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000661 node = UnarySub(node)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000662 node.lineno = elt[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000663 elif t == token.TILDE:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000664 node = Invert(node)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000665 node.lineno = elt[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000666 return node
667
668 def power(self, nodelist):
669 # power: atom trailer* ('**' factor)*
670 node = self.com_node(nodelist[0])
671 for i in range(1, len(nodelist)):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000672 elt = nodelist[i]
673 if elt[0] == token.DOUBLESTAR:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000674 n = Power([node, self.com_node(nodelist[i+1])])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000675 n.lineno = elt[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000676 return n
677
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000678 node = self.com_apply_trailer(node, elt)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000679
680 return node
681
682 def atom(self, nodelist):
Jeremy Hylton7845cf82001-08-29 20:56:30 +0000683 n = self._atom_dispatch[nodelist[0][0]](nodelist)
684 n.lineno = nodelist[0][2]
685 return n
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000686
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000687 def atom_lpar(self, nodelist):
688 if nodelist[1][0] == token.RPAR:
689 n = Tuple(())
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000690 n.lineno = nodelist[0][2]
691 return n
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000692 return self.com_node(nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000693
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000694 def atom_lsqb(self, nodelist):
695 if nodelist[1][0] == token.RSQB:
696 n = List(())
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000697 n.lineno = nodelist[0][2]
698 return n
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000699 return self.com_list_constructor(nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000700
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000701 def atom_lbrace(self, nodelist):
702 if nodelist[1][0] == token.RBRACE:
703 return Dict(())
704 return self.com_dictmaker(nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000705
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000706 def atom_backquote(self, nodelist):
707 n = Backquote(self.com_node(nodelist[1]))
708 n.lineno = nodelist[0][2]
709 return n
Tim Peterse0c446b2001-10-18 21:57:37 +0000710
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000711 def atom_number(self, nodelist):
712 ### need to verify this matches compile.c
713 k = eval(nodelist[0][1])
714 n = Const(k)
715 n.lineno = nodelist[0][2]
716 return n
Tim Peterse0c446b2001-10-18 21:57:37 +0000717
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000718 def decode_literal(self, lit):
719 if self.encoding:
720 # this is particularly fragile & a bit of a
721 # hack... changes in compile.c:parsestr and
722 # tokenizer.c must be reflected here.
723 if self.encoding not in ['utf-8', 'iso-8859-1']:
724 lit = unicode(lit, 'utf-8').encode(self.encoding)
725 return eval("# coding: %s\n%s" % (self.encoding, lit))
726 else:
727 return eval(lit)
728
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000729 def atom_string(self, nodelist):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000730 k = ''
731 for node in nodelist:
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000732 k += self.decode_literal(node[1])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000733 n = Const(k)
734 n.lineno = nodelist[0][2]
735 return n
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000736
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000737 def atom_name(self, nodelist):
738 ### any processing to do?
739 n = Name(nodelist[0][1])
740 n.lineno = nodelist[0][2]
741 return n
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000742
743 # --------------------------------------------------------------
744 #
745 # INTERNAL PARSING UTILITIES
746 #
747
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +0000748 # The use of com_node() introduces a lot of extra stack frames,
749 # enough to cause a stack overflow compiling test.test_parser with
750 # the standard interpreter recursionlimit. The com_node() is a
751 # convenience function that hides the dispatch details, but comes
752 # at a very high cost. It is more efficient to dispatch directly
753 # in the callers. In these cases, use lookup_node() and call the
754 # dispatched node directly.
755
756 def lookup_node(self, node):
757 return self._dispatch[node[0]]
758
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000759 def com_node(self, node):
760 # Note: compile.c has handling in com_node for del_stmt, pass_stmt,
761 # break_stmt, stmt, small_stmt, flow_stmt, simple_stmt,
762 # and compound_stmt.
763 # We'll just dispatch them.
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000764 return self._dispatch[node[0]](node[1:])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000765
Jeremy Hylton42a08302001-04-09 04:27:12 +0000766 def com_NEWLINE(self, *args):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000767 # A ';' at the end of a line can make a NEWLINE token appear
768 # here, Render it harmless. (genc discards ('discard',
769 # ('const', xxxx)) Nodes)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000770 return Discard(Const(None))
Tim Peterse0c446b2001-10-18 21:57:37 +0000771
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000772 def com_arglist(self, nodelist):
773 # varargslist:
Jeremy Hylton8d22b042002-04-19 22:56:37 +0000774 # (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
775 # | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000776 # fpdef: NAME | '(' fplist ')'
777 # fplist: fpdef (',' fpdef)* [',']
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000778 names = []
779 defaults = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000780 flags = 0
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000781
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000782 i = 0
783 while i < len(nodelist):
784 node = nodelist[i]
785 if node[0] == token.STAR or node[0] == token.DOUBLESTAR:
786 if node[0] == token.STAR:
787 node = nodelist[i+1]
788 if node[0] == token.NAME:
789 names.append(node[1])
790 flags = flags | CO_VARARGS
791 i = i + 3
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000792
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000793 if i < len(nodelist):
Jeremy Hylton8d22b042002-04-19 22:56:37 +0000794 # should be DOUBLESTAR
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000795 t = nodelist[i][0]
796 if t == token.DOUBLESTAR:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000797 node = nodelist[i+1]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000798 else:
799 raise ValueError, "unexpected token: %s" % t
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000800 names.append(node[1])
801 flags = flags | CO_VARKEYWORDS
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000802
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000803 break
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000804
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000805 # fpdef: NAME | '(' fplist ')'
806 names.append(self.com_fpdef(node))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000807
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000808 i = i + 1
809 if i >= len(nodelist):
810 break
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000811
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000812 if nodelist[i][0] == token.EQUAL:
813 defaults.append(self.com_node(nodelist[i + 1]))
814 i = i + 2
815 elif len(defaults):
816 # Treat "(a=1, b)" as "(a=1, b=None)"
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000817 defaults.append(Const(None))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000818
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000819 i = i + 1
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000820
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000821 return names, defaults, flags
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000822
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000823 def com_fpdef(self, node):
824 # fpdef: NAME | '(' fplist ')'
825 if node[1][0] == token.LPAR:
826 return self.com_fplist(node[2])
827 return node[1][1]
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000828
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000829 def com_fplist(self, node):
Tim Peterse0c446b2001-10-18 21:57:37 +0000830 # fplist: fpdef (',' fpdef)* [',']
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000831 if len(node) == 2:
832 return self.com_fpdef(node[1])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000833 list = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000834 for i in range(1, len(node), 2):
835 list.append(self.com_fpdef(node[i]))
836 return tuple(list)
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000837
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000838 def com_dotted_name(self, node):
839 # String together the dotted names and return the string
840 name = ""
841 for n in node:
842 if type(n) == type(()) and n[0] == 1:
843 name = name + n[1] + '.'
844 return name[:-1]
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000845
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000846 def com_dotted_as_name(self, node):
847 dot = self.com_dotted_name(node[1])
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000848 if len(node) <= 2:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000849 return dot, None
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000850 if node[0] == symbol.dotted_name:
851 pass
852 else:
853 assert node[2][1] == 'as'
854 assert node[3][0] == token.NAME
855 return dot, node[3][1]
Jeremy Hylton20516082000-09-01 20:33:26 +0000856
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000857 def com_import_as_name(self, node):
Jeremy Hylton42a08302001-04-09 04:27:12 +0000858 if node[0] == token.STAR:
Jeremy Hylton4e1be722000-10-12 20:23:23 +0000859 return '*', None
Jeremy Hylton42a08302001-04-09 04:27:12 +0000860 assert node[0] == symbol.import_as_name
861 node = node[1:]
862 if len(node) == 1:
863 assert node[0][0] == token.NAME
864 return node[0][1], None
865
866 assert node[1][1] == 'as', node
867 assert node[2][0] == token.NAME
868 return node[0][1], node[2][1]
Jeremy Hylton20516082000-09-01 20:33:26 +0000869
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000870 def com_bases(self, node):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000871 bases = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000872 for i in range(1, len(node), 2):
873 bases.append(self.com_node(node[i]))
874 return bases
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000875
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000876 def com_try_finally(self, nodelist):
877 # try_fin_stmt: "try" ":" suite "finally" ":" suite
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000878 n = TryFinally(self.com_node(nodelist[2]),
Tim Peterse0c446b2001-10-18 21:57:37 +0000879 self.com_node(nodelist[5]))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000880 n.lineno = nodelist[0][2]
881 return n
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000882
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000883 def com_try_except(self, nodelist):
884 # try_except: 'try' ':' suite (except_clause ':' suite)* ['else' suite]
885 #tryexcept: [TryNode, [except_clauses], elseNode)]
886 stmt = self.com_node(nodelist[2])
887 clauses = []
888 elseNode = None
889 for i in range(3, len(nodelist), 3):
890 node = nodelist[i]
891 if node[0] == symbol.except_clause:
892 # except_clause: 'except' [expr [',' expr]] */
893 if len(node) > 2:
894 expr1 = self.com_node(node[2])
895 if len(node) > 4:
896 expr2 = self.com_assign(node[4], OP_ASSIGN)
897 else:
898 expr2 = None
899 else:
900 expr1 = expr2 = None
901 clauses.append((expr1, expr2, self.com_node(nodelist[i+2])))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000902
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000903 if node[0] == token.NAME:
904 elseNode = self.com_node(nodelist[i+2])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000905 n = TryExcept(self.com_node(nodelist[2]), clauses, elseNode)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000906 n.lineno = nodelist[0][2]
907 return n
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000908
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000909 def com_augassign_op(self, node):
910 assert node[0] == symbol.augassign
911 return node[1]
912
913 def com_augassign(self, node):
914 """Return node suitable for lvalue of augmented assignment
915
916 Names, slices, and attributes are the only allowable nodes.
917 """
918 l = self.com_node(node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000919 if l.__class__ in (Name, Slice, Subscript, Getattr):
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000920 return l
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000921 raise SyntaxError, "can't assign to %s" % l.__class__.__name__
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000922
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000923 def com_assign(self, node, assigning):
924 # return a node suitable for use as an "lvalue"
925 # loop to avoid trivial recursion
926 while 1:
927 t = node[0]
928 if t == symbol.exprlist or t == symbol.testlist:
929 if len(node) > 2:
930 return self.com_assign_tuple(node, assigning)
931 node = node[1]
932 elif t in _assign_types:
933 if len(node) > 2:
934 raise SyntaxError, "can't assign to operator"
935 node = node[1]
936 elif t == symbol.power:
937 if node[1][0] != symbol.atom:
938 raise SyntaxError, "can't assign to operator"
939 if len(node) > 2:
940 primary = self.com_node(node[1])
941 for i in range(2, len(node)-1):
942 ch = node[i]
943 if ch[0] == token.DOUBLESTAR:
944 raise SyntaxError, "can't assign to operator"
945 primary = self.com_apply_trailer(primary, ch)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000946 return self.com_assign_trailer(primary, node[-1],
Tim Peterse0c446b2001-10-18 21:57:37 +0000947 assigning)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000948 node = node[1]
949 elif t == symbol.atom:
950 t = node[1][0]
951 if t == token.LPAR:
952 node = node[2]
953 if node[0] == token.RPAR:
954 raise SyntaxError, "can't assign to ()"
955 elif t == token.LSQB:
956 node = node[2]
957 if node[0] == token.RSQB:
958 raise SyntaxError, "can't assign to []"
959 return self.com_assign_list(node, assigning)
960 elif t == token.NAME:
961 return self.com_assign_name(node[1], assigning)
962 else:
963 raise SyntaxError, "can't assign to literal"
964 else:
965 raise SyntaxError, "bad assignment"
966
967 def com_assign_tuple(self, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000968 assigns = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000969 for i in range(1, len(node), 2):
970 assigns.append(self.com_assign(node[i], assigning))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000971 return AssTuple(assigns)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000972
973 def com_assign_list(self, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000974 assigns = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000975 for i in range(1, len(node), 2):
Jeremy Hylton2e4cc7e2001-09-17 19:33:48 +0000976 if i + 1 < len(node):
977 if node[i + 1][0] == symbol.list_for:
978 raise SyntaxError, "can't assign to list comprehension"
979 assert node[i + 1][0] == token.COMMA, node[i + 1]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000980 assigns.append(self.com_assign(node[i], assigning))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000981 return AssList(assigns)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000982
983 def com_assign_name(self, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000984 n = AssName(node[1], assigning)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000985 n.lineno = node[2]
986 return n
987
988 def com_assign_trailer(self, primary, node, assigning):
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000989 t = node[1][0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000990 if t == token.DOT:
991 return self.com_assign_attr(primary, node[2], assigning)
992 if t == token.LSQB:
993 return self.com_subscriptlist(primary, node[2], assigning)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000994 if t == token.LPAR:
995 raise SyntaxError, "can't assign to function call"
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000996 raise SyntaxError, "unknown trailer type: %s" % t
997
998 def com_assign_attr(self, primary, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000999 return AssAttr(primary, node[1], assigning)
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001000
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001001 def com_binary(self, constructor, nodelist):
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001002 "Compile 'NODE (OP NODE)*' into (type, [ node1, ..., nodeN ])."
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001003 l = len(nodelist)
1004 if l == 1:
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001005 n = nodelist[0]
1006 return self.lookup_node(n)(n[1:])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001007 items = []
1008 for i in range(0, l, 2):
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001009 n = nodelist[i]
1010 items.append(self.lookup_node(n)(n[1:]))
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001011 return constructor(items)
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001012
1013 def com_stmt(self, node):
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001014 result = self.lookup_node(node)(node[1:])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001015 assert result is not None
1016 if isinstance(result, Stmt):
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001017 return result
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001018 return Stmt([result])
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001019
1020 def com_append_stmt(self, stmts, node):
1021 result = self.com_node(node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001022 assert result is not None
1023 if isinstance(result, Stmt):
1024 stmts.extend(result.nodes)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001025 else:
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001026 stmts.append(result)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001027
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001028 if hasattr(symbol, 'list_for'):
1029 def com_list_constructor(self, nodelist):
1030 # listmaker: test ( list_for | (',' test)* [','] )
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001031 values = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001032 for i in range(1, len(nodelist)):
1033 if nodelist[i][0] == symbol.list_for:
1034 assert len(nodelist[i:]) == 1
1035 return self.com_list_comprehension(values[0],
1036 nodelist[i])
1037 elif nodelist[i][0] == token.COMMA:
1038 continue
1039 values.append(self.com_node(nodelist[i]))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001040 return List(values)
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001041
1042 def com_list_comprehension(self, expr, node):
1043 # list_iter: list_for | list_if
1044 # list_for: 'for' exprlist 'in' testlist [list_iter]
1045 # list_if: 'if' test [list_iter]
Jeremy Hylton42a08302001-04-09 04:27:12 +00001046
1047 # XXX should raise SyntaxError for assignment
Tim Peterse0c446b2001-10-18 21:57:37 +00001048
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001049 lineno = node[1][2]
1050 fors = []
1051 while node:
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001052 t = node[1][1]
1053 if t == 'for':
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001054 assignNode = self.com_assign(node[2], OP_ASSIGN)
1055 listNode = self.com_node(node[4])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001056 newfor = ListCompFor(assignNode, listNode, [])
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001057 newfor.lineno = node[1][2]
1058 fors.append(newfor)
1059 if len(node) == 5:
1060 node = None
1061 else:
1062 node = self.com_list_iter(node[5])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001063 elif t == 'if':
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001064 test = self.com_node(node[2])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001065 newif = ListCompIf(test)
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001066 newif.lineno = node[1][2]
1067 newfor.ifs.append(newif)
1068 if len(node) == 3:
1069 node = None
1070 else:
1071 node = self.com_list_iter(node[3])
1072 else:
1073 raise SyntaxError, \
1074 ("unexpected list comprehension element: %s %d"
1075 % (node, lineno))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001076 n = ListComp(expr, fors)
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001077 n.lineno = lineno
1078 return n
1079
1080 def com_list_iter(self, node):
1081 assert node[0] == symbol.list_iter
1082 return node[1]
1083 else:
1084 def com_list_constructor(self, nodelist):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001085 values = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001086 for i in range(1, len(nodelist), 2):
1087 values.append(self.com_node(nodelist[i]))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001088 return List(values)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001089
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001090 def com_dictmaker(self, nodelist):
1091 # dictmaker: test ':' test (',' test ':' value)* [',']
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001092 items = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001093 for i in range(1, len(nodelist), 4):
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001094 items.append((self.com_node(nodelist[i]),
1095 self.com_node(nodelist[i+2])))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001096 return Dict(items)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001097
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001098 def com_apply_trailer(self, primaryNode, nodelist):
1099 t = nodelist[1][0]
1100 if t == token.LPAR:
1101 return self.com_call_function(primaryNode, nodelist[2])
1102 if t == token.DOT:
1103 return self.com_select_member(primaryNode, nodelist[2])
1104 if t == token.LSQB:
1105 return self.com_subscriptlist(primaryNode, nodelist[2], OP_APPLY)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001106
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001107 raise SyntaxError, 'unknown node type: %s' % t
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001108
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001109 def com_select_member(self, primaryNode, nodelist):
1110 if nodelist[0] != token.NAME:
1111 raise SyntaxError, "member must be a name"
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001112 n = Getattr(primaryNode, nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001113 n.lineno = nodelist[2]
1114 return n
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001115
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001116 def com_call_function(self, primaryNode, nodelist):
1117 if nodelist[0] == token.RPAR:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001118 return CallFunc(primaryNode, [])
1119 args = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001120 kw = 0
1121 len_nodelist = len(nodelist)
1122 for i in range(1, len_nodelist, 2):
1123 node = nodelist[i]
1124 if node[0] == token.STAR or node[0] == token.DOUBLESTAR:
Tim Peterse0c446b2001-10-18 21:57:37 +00001125 break
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001126 kw, result = self.com_argument(node, kw)
1127 args.append(result)
Jeremy Hyltonbe317e62000-05-02 22:32:59 +00001128 else:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001129 # No broken by star arg, so skip the last one we processed.
1130 i = i + 1
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001131 if i < len_nodelist and nodelist[i][0] == token.COMMA:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001132 # need to accept an application that looks like "f(a, b,)"
1133 i = i + 1
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001134 star_node = dstar_node = None
1135 while i < len_nodelist:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001136 tok = nodelist[i]
1137 ch = nodelist[i+1]
1138 i = i + 3
1139 if tok[0]==token.STAR:
1140 if star_node is not None:
1141 raise SyntaxError, 'already have the varargs indentifier'
1142 star_node = self.com_node(ch)
1143 elif tok[0]==token.DOUBLESTAR:
1144 if dstar_node is not None:
1145 raise SyntaxError, 'already have the kwargs indentifier'
1146 dstar_node = self.com_node(ch)
1147 else:
1148 raise SyntaxError, 'unknown node type: %s' % tok
Jeremy Hyltonbe317e62000-05-02 22:32:59 +00001149
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001150 return CallFunc(primaryNode, args, star_node, dstar_node)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001151
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001152 def com_argument(self, nodelist, kw):
1153 if len(nodelist) == 2:
1154 if kw:
1155 raise SyntaxError, "non-keyword arg after keyword arg"
1156 return 0, self.com_node(nodelist[1])
1157 result = self.com_node(nodelist[3])
1158 n = nodelist[1]
1159 while len(n) == 2 and n[0] != token.NAME:
1160 n = n[1]
1161 if n[0] != token.NAME:
1162 raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001163 node = Keyword(n[1], result)
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001164 node.lineno = n[2]
1165 return 1, node
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001166
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001167 def com_subscriptlist(self, primary, nodelist, assigning):
1168 # slicing: simple_slicing | extended_slicing
1169 # simple_slicing: primary "[" short_slice "]"
Tim Peterse0c446b2001-10-18 21:57:37 +00001170 # extended_slicing: primary "[" slice_list "]"
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001171 # slice_list: slice_item ("," slice_item)* [","]
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001172
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001173 # backwards compat slice for '[i:j]'
1174 if len(nodelist) == 2:
1175 sub = nodelist[1]
1176 if (sub[1][0] == token.COLON or \
1177 (len(sub) > 2 and sub[2][0] == token.COLON)) and \
1178 sub[-1][0] != symbol.sliceop:
1179 return self.com_slice(primary, sub, assigning)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001180
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001181 subscripts = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001182 for i in range(1, len(nodelist), 2):
1183 subscripts.append(self.com_subscript(nodelist[i]))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001184
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001185 return Subscript(primary, assigning, subscripts)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001186
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001187 def com_subscript(self, node):
1188 # slice_item: expression | proper_slice | ellipsis
1189 ch = node[1]
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001190 t = ch[0]
1191 if t == token.DOT and node[2][0] == token.DOT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001192 return Ellipsis()
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001193 if t == token.COLON or len(node) > 2:
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001194 return self.com_sliceobj(node)
1195 return self.com_node(ch)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001196
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001197 def com_sliceobj(self, node):
1198 # proper_slice: short_slice | long_slice
1199 # short_slice: [lower_bound] ":" [upper_bound]
1200 # long_slice: short_slice ":" [stride]
1201 # lower_bound: expression
1202 # upper_bound: expression
1203 # stride: expression
1204 #
1205 # Note: a stride may be further slicing...
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001206
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001207 items = []
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001208
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001209 if node[1][0] == token.COLON:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001210 items.append(Const(None))
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001211 i = 2
1212 else:
1213 items.append(self.com_node(node[1]))
1214 # i == 2 is a COLON
1215 i = 3
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001216
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001217 if i < len(node) and node[i][0] == symbol.test:
1218 items.append(self.com_node(node[i]))
1219 i = i + 1
1220 else:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001221 items.append(Const(None))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001222
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001223 # a short_slice has been built. look for long_slice now by looking
1224 # for strides...
1225 for j in range(i, len(node)):
1226 ch = node[j]
1227 if len(ch) == 2:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001228 items.append(Const(None))
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001229 else:
1230 items.append(self.com_node(ch[2]))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001231
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001232 return Sliceobj(items)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001233
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001234 def com_slice(self, primary, node, assigning):
1235 # short_slice: [lower_bound] ":" [upper_bound]
1236 lower = upper = None
1237 if len(node) == 3:
1238 if node[1][0] == token.COLON:
1239 upper = self.com_node(node[2])
1240 else:
1241 lower = self.com_node(node[1])
1242 elif len(node) == 4:
1243 lower = self.com_node(node[1])
1244 upper = self.com_node(node[3])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001245 return Slice(primary, assigning, lower, upper)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001246
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001247 def get_docstring(self, node, n=None):
1248 if n is None:
1249 n = node[0]
1250 node = node[1:]
1251 if n == symbol.suite:
1252 if len(node) == 1:
1253 return self.get_docstring(node[0])
1254 for sub in node:
1255 if sub[0] == symbol.stmt:
1256 return self.get_docstring(sub)
1257 return None
1258 if n == symbol.file_input:
1259 for sub in node:
1260 if sub[0] == symbol.stmt:
1261 return self.get_docstring(sub)
1262 return None
1263 if n == symbol.atom:
1264 if node[0][0] == token.STRING:
1265 s = ''
1266 for t in node:
1267 s = s + eval(t[1])
1268 return s
1269 return None
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001270 if n == symbol.stmt or n == symbol.simple_stmt \
1271 or n == symbol.small_stmt:
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001272 return self.get_docstring(node[0])
1273 if n in _doc_nodes and len(node) == 1:
1274 return self.get_docstring(node[0])
1275 return None
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001276
1277
1278_doc_nodes = [
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001279 symbol.expr_stmt,
1280 symbol.testlist,
Jeremy Hyltond1142612001-10-17 13:32:52 +00001281 symbol.testlist_safe,
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001282 symbol.test,
1283 symbol.and_test,
1284 symbol.not_test,
1285 symbol.comparison,
1286 symbol.expr,
1287 symbol.xor_expr,
1288 symbol.and_expr,
1289 symbol.shift_expr,
1290 symbol.arith_expr,
1291 symbol.term,
1292 symbol.factor,
1293 symbol.power,
1294 ]
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001295
1296# comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1297# | 'in' | 'not' 'in' | 'is' | 'is' 'not'
1298_cmp_types = {
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001299 token.LESS : '<',
1300 token.GREATER : '>',
1301 token.EQEQUAL : '==',
1302 token.EQUAL : '==',
1303 token.LESSEQUAL : '<=',
1304 token.GREATEREQUAL : '>=',
1305 token.NOTEQUAL : '!=',
1306 }
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001307
1308_legal_node_types = [
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001309 symbol.funcdef,
1310 symbol.classdef,
1311 symbol.stmt,
1312 symbol.small_stmt,
1313 symbol.flow_stmt,
1314 symbol.simple_stmt,
1315 symbol.compound_stmt,
1316 symbol.expr_stmt,
1317 symbol.print_stmt,
1318 symbol.del_stmt,
1319 symbol.pass_stmt,
1320 symbol.break_stmt,
1321 symbol.continue_stmt,
1322 symbol.return_stmt,
1323 symbol.raise_stmt,
1324 symbol.import_stmt,
1325 symbol.global_stmt,
1326 symbol.exec_stmt,
1327 symbol.assert_stmt,
1328 symbol.if_stmt,
1329 symbol.while_stmt,
1330 symbol.for_stmt,
1331 symbol.try_stmt,
1332 symbol.suite,
1333 symbol.testlist,
Jeremy Hyltond1142612001-10-17 13:32:52 +00001334 symbol.testlist_safe,
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001335 symbol.test,
1336 symbol.and_test,
1337 symbol.not_test,
1338 symbol.comparison,
1339 symbol.exprlist,
1340 symbol.expr,
1341 symbol.xor_expr,
1342 symbol.and_expr,
1343 symbol.shift_expr,
1344 symbol.arith_expr,
1345 symbol.term,
1346 symbol.factor,
1347 symbol.power,
1348 symbol.atom,
1349 ]
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001350
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001351if hasattr(symbol, 'yield_stmt'):
1352 _legal_node_types.append(symbol.yield_stmt)
1353
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001354_assign_types = [
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001355 symbol.test,
1356 symbol.and_test,
1357 symbol.not_test,
1358 symbol.comparison,
1359 symbol.expr,
1360 symbol.xor_expr,
1361 symbol.and_expr,
1362 symbol.shift_expr,
1363 symbol.arith_expr,
1364 symbol.term,
1365 symbol.factor,
1366 ]
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001367
1368import types
1369_names = {}
1370for k, v in symbol.sym_name.items():
1371 _names[k] = v
1372for k, v in token.tok_name.items():
1373 _names[k] = v
1374
1375def debug_tree(tree):
1376 l = []
1377 for elt in tree:
1378 if type(elt) == types.IntType:
1379 l.append(_names.get(elt, elt))
1380 elif type(elt) == types.StringType:
1381 l.append(elt)
1382 else:
1383 l.append(debug_tree(elt))
1384 return l