blob: cdeb5ffdc2265f7cca7fe321900ca01a76bfc317 [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:
Raymond Hettingerff41c482003-04-06 09:01:11 +000075 return 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]
Tim Peters669454e2003-03-07 17:30:48 +0000145
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
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000188 def decorator_name(self, nodelist):
189 listlen = len(nodelist)
190 assert listlen >= 1 and listlen % 2 == 1
191
192 item = self.atom_name(nodelist)
193 i = 1
194 while i < listlen:
195 assert nodelist[i][0] == token.DOT
196 assert nodelist[i + 1][0] == token.NAME
197 item = Getattr(item, nodelist[i + 1][1])
198 i += 2
199
200 return item
Tim Peters6db15d72004-08-04 02:36:18 +0000201
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000202 def decorator(self, nodelist):
203 # '@' dotted_name [ '(' [arglist] ')' ]
Michael W. Hudson0ccff072004-08-17 17:29:16 +0000204 assert len(nodelist) in (3, 5, 6)
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000205 assert nodelist[0][0] == token.AT
Michael W. Hudson0ccff072004-08-17 17:29:16 +0000206 assert nodelist[-1][0] == token.NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000207
208 assert nodelist[1][0] == symbol.dotted_name
209 funcname = self.decorator_name(nodelist[1][1:])
210
Michael W. Hudson0ccff072004-08-17 17:29:16 +0000211 if len(nodelist) > 3:
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000212 assert nodelist[2][0] == token.LPAR
213 expr = self.com_call_function(funcname, nodelist[3])
214 else:
215 expr = funcname
Tim Peters6db15d72004-08-04 02:36:18 +0000216
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000217 return expr
Tim Peters6db15d72004-08-04 02:36:18 +0000218
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000219 def decorators(self, nodelist):
220 # decorators: decorator ([NEWLINE] decorator)* NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000221 items = []
Michael W. Hudson0ccff072004-08-17 17:29:16 +0000222 for dec_nodelist in nodelist:
223 assert dec_nodelist[0] == symbol.decorator
224 items.append(self.decorator(dec_nodelist[1:]))
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000225 return Decorators(items)
Tim Peters6db15d72004-08-04 02:36:18 +0000226
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000227 def funcdef(self, nodelist):
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000228 # -6 -5 -4 -3 -2 -1
229 # funcdef: [decorators] 'def' NAME parameters ':' suite
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000230 # parameters: '(' [varargslist] ')'
231
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000232 if len(nodelist) == 6:
233 assert nodelist[0][0] == symbol.decorators
234 decorators = self.decorators(nodelist[0][1:])
235 else:
236 assert len(nodelist) == 5
237 decorators = None
Tim Peters6db15d72004-08-04 02:36:18 +0000238
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000239 lineno = nodelist[-4][2]
240 name = nodelist[-4][1]
241 args = nodelist[-3][2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000242
243 if args[0] == symbol.varargslist:
244 names, defaults, flags = self.com_arglist(args[1:])
245 else:
246 names = defaults = ()
247 flags = 0
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000248 doc = self.get_docstring(nodelist[-1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000249
250 # code for function
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000251 code = self.com_node(nodelist[-1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000252
Jeremy Hyltona59ac0a2000-11-06 03:33:52 +0000253 if doc is not None:
254 assert isinstance(code, Stmt)
255 assert isinstance(code.nodes[0], Discard)
256 del code.nodes[0]
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000257 n = Function(decorators, name, names, defaults, flags, doc, code)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000258 n.lineno = lineno
259 return n
260
261 def lambdef(self, nodelist):
262 # lambdef: 'lambda' [varargslist] ':' test
263 if nodelist[2][0] == symbol.varargslist:
264 names, defaults, flags = self.com_arglist(nodelist[2][1:])
265 else:
266 names = defaults = ()
267 flags = 0
268
269 # code for lambda
270 code = self.com_node(nodelist[-1])
271
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000272 n = Lambda(names, defaults, flags, code)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000273 n.lineno = nodelist[1][2]
274 return n
275
276 def classdef(self, nodelist):
277 # classdef: 'class' NAME ['(' testlist ')'] ':' suite
278
279 name = nodelist[1][1]
280 doc = self.get_docstring(nodelist[-1])
281 if nodelist[2][0] == token.COLON:
282 bases = []
283 else:
284 bases = self.com_bases(nodelist[3])
285
286 # code for class
287 code = self.com_node(nodelist[-1])
288
Jeremy Hyltonda8db8c2001-08-29 17:19:02 +0000289 if doc is not None:
290 assert isinstance(code, Stmt)
291 assert isinstance(code.nodes[0], Discard)
292 del code.nodes[0]
293
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000294 n = Class(name, bases, doc, code)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000295 n.lineno = nodelist[1][2]
296 return n
297
298 def stmt(self, nodelist):
299 return self.com_stmt(nodelist[0])
300
301 small_stmt = stmt
302 flow_stmt = stmt
303 compound_stmt = stmt
304
305 def simple_stmt(self, nodelist):
306 # small_stmt (';' small_stmt)* [';'] NEWLINE
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000307 stmts = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000308 for i in range(0, len(nodelist), 2):
309 self.com_append_stmt(stmts, nodelist[i])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000310 return Stmt(stmts)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000311
312 def parameters(self, nodelist):
313 raise error
314
315 def varargslist(self, nodelist):
316 raise error
317
318 def fpdef(self, nodelist):
319 raise error
320
321 def fplist(self, nodelist):
322 raise error
323
324 def dotted_name(self, nodelist):
325 raise error
326
327 def comp_op(self, nodelist):
328 raise error
329
330 def trailer(self, nodelist):
331 raise error
332
333 def sliceop(self, nodelist):
334 raise error
335
336 def argument(self, nodelist):
337 raise error
338
339 # --------------------------------------------------------------
340 #
341 # STATEMENT NODES (invoked by com_node())
342 #
343
344 def expr_stmt(self, nodelist):
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000345 # augassign testlist | testlist ('=' testlist)*
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +0000346 en = nodelist[-1]
347 exprNode = self.lookup_node(en)(en[1:])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000348 if len(nodelist) == 1:
Jeremy Hylton4c1f4272001-04-11 16:22:26 +0000349 n = Discard(exprNode)
350 n.lineno = exprNode.lineno
351 return n
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000352 if nodelist[1][0] == token.EQUAL:
Martin v. Löwis51e234a2002-02-28 17:48:48 +0000353 nodesl = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000354 for i in range(0, len(nodelist) - 2, 2):
Martin v. Löwis51e234a2002-02-28 17:48:48 +0000355 nodesl.append(self.com_assign(nodelist[i], OP_ASSIGN))
356 n = Assign(nodesl, exprNode)
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000357 n.lineno = nodelist[1][2]
358 else:
359 lval = self.com_augassign(nodelist[0])
360 op = self.com_augassign_op(nodelist[1])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000361 n = AugAssign(lval, op[1], exprNode)
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000362 n.lineno = op[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000363 return n
364
365 def print_stmt(self, nodelist):
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000366 # print ([ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000367 items = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000368 if len(nodelist) == 1:
369 start = 1
370 dest = None
371 elif nodelist[1][0] == token.RIGHTSHIFT:
372 assert len(nodelist) == 3 \
373 or nodelist[3][0] == token.COMMA
374 dest = self.com_node(nodelist[2])
375 start = 4
376 else:
377 dest = None
378 start = 1
379 for i in range(start, len(nodelist), 2):
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000380 items.append(self.com_node(nodelist[i]))
381 if nodelist[-1][0] == token.COMMA:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000382 n = Print(items, dest)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000383 n.lineno = nodelist[0][2]
384 return n
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000385 n = Printnl(items, dest)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000386 n.lineno = nodelist[0][2]
387 return n
388
389 def del_stmt(self, nodelist):
390 return self.com_assign(nodelist[1], OP_DELETE)
391
392 def pass_stmt(self, nodelist):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000393 n = Pass()
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000394 n.lineno = nodelist[0][2]
395 return n
396
397 def break_stmt(self, nodelist):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000398 n = Break()
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000399 n.lineno = nodelist[0][2]
400 return n
401
402 def continue_stmt(self, nodelist):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000403 n = Continue()
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000404 n.lineno = nodelist[0][2]
405 return n
406
407 def return_stmt(self, nodelist):
408 # return: [testlist]
409 if len(nodelist) < 2:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000410 n = Return(Const(None))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000411 n.lineno = nodelist[0][2]
412 return n
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000413 n = Return(self.com_node(nodelist[1]))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000414 n.lineno = nodelist[0][2]
415 return n
416
Jeremy Hyltonec927342001-08-18 00:04:31 +0000417 def yield_stmt(self, nodelist):
418 n = Yield(self.com_node(nodelist[1]))
419 n.lineno = nodelist[0][2]
420 return n
421
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000422 def raise_stmt(self, nodelist):
423 # raise: [test [',' test [',' test]]]
424 if len(nodelist) > 5:
425 expr3 = self.com_node(nodelist[5])
426 else:
427 expr3 = None
428 if len(nodelist) > 3:
429 expr2 = self.com_node(nodelist[3])
430 else:
431 expr2 = None
432 if len(nodelist) > 1:
433 expr1 = self.com_node(nodelist[1])
434 else:
435 expr1 = None
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000436 n = Raise(expr1, expr2, expr3)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000437 n.lineno = nodelist[0][2]
438 return n
439
440 def import_stmt(self, nodelist):
441 # import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
442 # from: 'from' dotted_name 'import'
443 # ('*' | import_as_name (',' import_as_name)*)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000444 if nodelist[0][1] == 'from':
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000445 names = []
446 if nodelist[3][0] == token.NAME:
447 for i in range(3, len(nodelist), 2):
448 names.append((nodelist[i][1], None))
449 else:
450 for i in range(3, len(nodelist), 2):
Jeremy Hylton42a08302001-04-09 04:27:12 +0000451 names.append(self.com_import_as_name(nodelist[i]))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000452 n = From(self.com_dotted_name(nodelist[1]), names)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000453 n.lineno = nodelist[0][2]
454 return n
455
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000456 if nodelist[1][0] == symbol.dotted_name:
457 names = [(self.com_dotted_name(nodelist[1][1:]), None)]
458 else:
459 names = []
460 for i in range(1, len(nodelist), 2):
461 names.append(self.com_dotted_as_name(nodelist[i]))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000462 n = Import(names)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000463 n.lineno = nodelist[0][2]
464 return n
465
466 def global_stmt(self, nodelist):
467 # global: NAME (',' NAME)*
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000468 names = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000469 for i in range(1, len(nodelist), 2):
470 names.append(nodelist[i][1])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000471 n = Global(names)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000472 n.lineno = nodelist[0][2]
473 return n
474
475 def exec_stmt(self, nodelist):
476 # exec_stmt: 'exec' expr ['in' expr [',' expr]]
477 expr1 = self.com_node(nodelist[1])
478 if len(nodelist) >= 4:
479 expr2 = self.com_node(nodelist[3])
480 if len(nodelist) >= 6:
481 expr3 = self.com_node(nodelist[5])
482 else:
483 expr3 = None
484 else:
485 expr2 = expr3 = None
486
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000487 n = Exec(expr1, expr2, expr3)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000488 n.lineno = nodelist[0][2]
489 return n
490
491 def assert_stmt(self, nodelist):
492 # 'assert': test, [',' test]
493 expr1 = self.com_node(nodelist[1])
494 if (len(nodelist) == 4):
495 expr2 = self.com_node(nodelist[3])
496 else:
Jeremy Hyltona59ac0a2000-11-06 03:33:52 +0000497 expr2 = None
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000498 n = Assert(expr1, expr2)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000499 n.lineno = nodelist[0][2]
500 return n
501
502 def if_stmt(self, nodelist):
503 # if: test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000504 tests = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000505 for i in range(0, len(nodelist) - 3, 4):
506 testNode = self.com_node(nodelist[i + 1])
507 suiteNode = self.com_node(nodelist[i + 3])
508 tests.append((testNode, suiteNode))
509
510 if len(nodelist) % 4 == 3:
511 elseNode = self.com_node(nodelist[-1])
512## elseNode.lineno = nodelist[-1][1][2]
513 else:
514 elseNode = None
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000515 n = If(tests, elseNode)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000516 n.lineno = nodelist[0][2]
517 return n
518
519 def while_stmt(self, nodelist):
520 # 'while' test ':' suite ['else' ':' suite]
521
522 testNode = self.com_node(nodelist[1])
523 bodyNode = self.com_node(nodelist[3])
524
525 if len(nodelist) > 4:
526 elseNode = self.com_node(nodelist[6])
527 else:
528 elseNode = None
529
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000530 n = While(testNode, bodyNode, elseNode)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000531 n.lineno = nodelist[0][2]
532 return n
533
534 def for_stmt(self, nodelist):
535 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
536
537 assignNode = self.com_assign(nodelist[1], OP_ASSIGN)
538 listNode = self.com_node(nodelist[3])
539 bodyNode = self.com_node(nodelist[5])
540
541 if len(nodelist) > 8:
542 elseNode = self.com_node(nodelist[8])
543 else:
544 elseNode = None
545
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000546 n = For(assignNode, listNode, bodyNode, elseNode)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000547 n.lineno = nodelist[0][2]
548 return n
549
550 def try_stmt(self, nodelist):
551 # 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
552 # | 'try' ':' suite 'finally' ':' suite
553 if nodelist[3][0] != symbol.except_clause:
554 return self.com_try_finally(nodelist)
555
556 return self.com_try_except(nodelist)
557
558 def suite(self, nodelist):
559 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
560 if len(nodelist) == 1:
561 return self.com_stmt(nodelist[0])
562
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000563 stmts = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000564 for node in nodelist:
565 if node[0] == symbol.stmt:
566 self.com_append_stmt(stmts, node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000567 return Stmt(stmts)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000568
569 # --------------------------------------------------------------
570 #
571 # EXPRESSION NODES (invoked by com_node())
572 #
573
574 def testlist(self, nodelist):
575 # testlist: expr (',' expr)* [',']
Jeremy Hyltond1142612001-10-17 13:32:52 +0000576 # testlist_safe: test [(',' test)+ [',']]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000577 # exprlist: expr (',' expr)* [',']
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000578 return self.com_binary(Tuple, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000579
Jeremy Hyltond1142612001-10-17 13:32:52 +0000580 testlist_safe = testlist # XXX
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000581 testlist1 = testlist
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000582 exprlist = testlist
583
Raymond Hettinger354433a2004-05-19 08:20:33 +0000584 def testlist_gexp(self, nodelist):
585 if len(nodelist) == 2 and nodelist[1][0] == symbol.gen_for:
586 test = self.com_node(nodelist[0])
587 return self.com_generator_expression(test, nodelist[1])
588 return self.testlist(nodelist)
589
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000590 def test(self, nodelist):
591 # and_test ('or' and_test)* | lambdef
592 if len(nodelist) == 1 and nodelist[0][0] == symbol.lambdef:
593 return self.lambdef(nodelist[0])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000594 return self.com_binary(Or, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000595
596 def and_test(self, nodelist):
597 # not_test ('and' not_test)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000598 return self.com_binary(And, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000599
600 def not_test(self, nodelist):
601 # 'not' not_test | comparison
602 result = self.com_node(nodelist[-1])
603 if len(nodelist) == 2:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000604 n = Not(result)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000605 n.lineno = nodelist[0][2]
606 return n
607 return result
608
609 def comparison(self, nodelist):
610 # comparison: expr (comp_op expr)*
611 node = self.com_node(nodelist[0])
612 if len(nodelist) == 1:
613 return node
614
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000615 results = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000616 for i in range(2, len(nodelist), 2):
617 nl = nodelist[i-1]
618
619 # comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
620 # | 'in' | 'not' 'in' | 'is' | 'is' 'not'
621 n = nl[1]
622 if n[0] == token.NAME:
623 type = n[1]
624 if len(nl) == 3:
625 if type == 'not':
626 type = 'not in'
627 else:
628 type = 'is not'
629 else:
630 type = _cmp_types[n[0]]
631
632 lineno = nl[1][2]
633 results.append((type, self.com_node(nodelist[i])))
634
635 # we need a special "compare" node so that we can distinguish
636 # 3 < x < 5 from (3 < x) < 5
637 # the two have very different semantics and results (note that the
638 # latter form is always true)
639
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000640 n = Compare(node, results)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000641 n.lineno = lineno
642 return n
643
644 def expr(self, nodelist):
645 # xor_expr ('|' xor_expr)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000646 return self.com_binary(Bitor, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000647
648 def xor_expr(self, nodelist):
649 # xor_expr ('^' xor_expr)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000650 return self.com_binary(Bitxor, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000651
652 def and_expr(self, nodelist):
653 # xor_expr ('&' xor_expr)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000654 return self.com_binary(Bitand, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000655
656 def shift_expr(self, nodelist):
657 # shift_expr ('<<'|'>>' shift_expr)*
658 node = self.com_node(nodelist[0])
659 for i in range(2, len(nodelist), 2):
660 right = self.com_node(nodelist[i])
661 if nodelist[i-1][0] == token.LEFTSHIFT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000662 node = LeftShift([node, right])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000663 node.lineno = nodelist[1][2]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000664 elif nodelist[i-1][0] == token.RIGHTSHIFT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000665 node = RightShift([node, right])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000666 node.lineno = nodelist[1][2]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000667 else:
668 raise ValueError, "unexpected token: %s" % nodelist[i-1][0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000669 return node
670
671 def arith_expr(self, nodelist):
672 node = self.com_node(nodelist[0])
673 for i in range(2, len(nodelist), 2):
674 right = self.com_node(nodelist[i])
675 if nodelist[i-1][0] == token.PLUS:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000676 node = Add([node, right])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000677 node.lineno = nodelist[1][2]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000678 elif nodelist[i-1][0] == token.MINUS:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000679 node = Sub([node, right])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000680 node.lineno = nodelist[1][2]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000681 else:
682 raise ValueError, "unexpected token: %s" % nodelist[i-1][0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000683 return node
684
685 def term(self, nodelist):
686 node = self.com_node(nodelist[0])
687 for i in range(2, len(nodelist), 2):
688 right = self.com_node(nodelist[i])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000689 t = nodelist[i-1][0]
690 if t == token.STAR:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000691 node = Mul([node, right])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000692 elif t == token.SLASH:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000693 node = Div([node, right])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000694 elif t == token.PERCENT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000695 node = Mod([node, right])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000696 elif t == token.DOUBLESLASH:
697 node = FloorDiv([node, right])
698 else:
699 raise ValueError, "unexpected token: %s" % t
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000700 node.lineno = nodelist[1][2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000701 return node
702
703 def factor(self, nodelist):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000704 elt = nodelist[0]
705 t = elt[0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000706 node = self.com_node(nodelist[-1])
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000707 # need to handle (unary op)constant here...
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000708 if t == token.PLUS:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000709 node = UnaryAdd(node)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000710 node.lineno = elt[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000711 elif t == token.MINUS:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000712 node = UnarySub(node)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000713 node.lineno = elt[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000714 elif t == token.TILDE:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000715 node = Invert(node)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000716 node.lineno = elt[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000717 return node
718
719 def power(self, nodelist):
720 # power: atom trailer* ('**' factor)*
721 node = self.com_node(nodelist[0])
722 for i in range(1, len(nodelist)):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000723 elt = nodelist[i]
724 if elt[0] == token.DOUBLESTAR:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000725 n = Power([node, self.com_node(nodelist[i+1])])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000726 n.lineno = elt[2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000727 return n
728
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000729 node = self.com_apply_trailer(node, elt)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000730
731 return node
732
733 def atom(self, nodelist):
Jeremy Hylton7845cf82001-08-29 20:56:30 +0000734 n = self._atom_dispatch[nodelist[0][0]](nodelist)
735 n.lineno = nodelist[0][2]
736 return n
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000737
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000738 def atom_lpar(self, nodelist):
739 if nodelist[1][0] == token.RPAR:
740 n = Tuple(())
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000741 n.lineno = nodelist[0][2]
742 return n
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000743 return self.com_node(nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000744
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000745 def atom_lsqb(self, nodelist):
746 if nodelist[1][0] == token.RSQB:
747 n = List(())
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000748 n.lineno = nodelist[0][2]
749 return n
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000750 return self.com_list_constructor(nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000751
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000752 def atom_lbrace(self, nodelist):
753 if nodelist[1][0] == token.RBRACE:
754 return Dict(())
755 return self.com_dictmaker(nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000756
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000757 def atom_backquote(self, nodelist):
758 n = Backquote(self.com_node(nodelist[1]))
759 n.lineno = nodelist[0][2]
760 return n
Tim Peterse0c446b2001-10-18 21:57:37 +0000761
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000762 def atom_number(self, nodelist):
763 ### need to verify this matches compile.c
764 k = eval(nodelist[0][1])
765 n = Const(k)
766 n.lineno = nodelist[0][2]
767 return n
Tim Peterse0c446b2001-10-18 21:57:37 +0000768
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000769 def decode_literal(self, lit):
770 if self.encoding:
771 # this is particularly fragile & a bit of a
772 # hack... changes in compile.c:parsestr and
773 # tokenizer.c must be reflected here.
774 if self.encoding not in ['utf-8', 'iso-8859-1']:
775 lit = unicode(lit, 'utf-8').encode(self.encoding)
776 return eval("# coding: %s\n%s" % (self.encoding, lit))
777 else:
778 return eval(lit)
779
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000780 def atom_string(self, nodelist):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000781 k = ''
782 for node in nodelist:
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000783 k += self.decode_literal(node[1])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000784 n = Const(k)
785 n.lineno = nodelist[0][2]
786 return n
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000787
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000788 def atom_name(self, nodelist):
789 ### any processing to do?
790 n = Name(nodelist[0][1])
791 n.lineno = nodelist[0][2]
792 return n
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000793
794 # --------------------------------------------------------------
795 #
796 # INTERNAL PARSING UTILITIES
797 #
798
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +0000799 # The use of com_node() introduces a lot of extra stack frames,
800 # enough to cause a stack overflow compiling test.test_parser with
801 # the standard interpreter recursionlimit. The com_node() is a
802 # convenience function that hides the dispatch details, but comes
803 # at a very high cost. It is more efficient to dispatch directly
804 # in the callers. In these cases, use lookup_node() and call the
805 # dispatched node directly.
806
807 def lookup_node(self, node):
808 return self._dispatch[node[0]]
809
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000810 def com_node(self, node):
811 # Note: compile.c has handling in com_node for del_stmt, pass_stmt,
812 # break_stmt, stmt, small_stmt, flow_stmt, simple_stmt,
813 # and compound_stmt.
814 # We'll just dispatch them.
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000815 return self._dispatch[node[0]](node[1:])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000816
Jeremy Hylton42a08302001-04-09 04:27:12 +0000817 def com_NEWLINE(self, *args):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000818 # A ';' at the end of a line can make a NEWLINE token appear
819 # here, Render it harmless. (genc discards ('discard',
820 # ('const', xxxx)) Nodes)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000821 return Discard(Const(None))
Tim Peterse0c446b2001-10-18 21:57:37 +0000822
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000823 def com_arglist(self, nodelist):
824 # varargslist:
Jeremy Hylton8d22b042002-04-19 22:56:37 +0000825 # (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
826 # | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000827 # fpdef: NAME | '(' fplist ')'
828 # fplist: fpdef (',' fpdef)* [',']
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000829 names = []
830 defaults = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000831 flags = 0
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000832
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000833 i = 0
834 while i < len(nodelist):
835 node = nodelist[i]
836 if node[0] == token.STAR or node[0] == token.DOUBLESTAR:
837 if node[0] == token.STAR:
838 node = nodelist[i+1]
839 if node[0] == token.NAME:
840 names.append(node[1])
841 flags = flags | CO_VARARGS
842 i = i + 3
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000843
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000844 if i < len(nodelist):
Jeremy Hylton8d22b042002-04-19 22:56:37 +0000845 # should be DOUBLESTAR
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000846 t = nodelist[i][0]
847 if t == token.DOUBLESTAR:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000848 node = nodelist[i+1]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000849 else:
850 raise ValueError, "unexpected token: %s" % t
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000851 names.append(node[1])
852 flags = flags | CO_VARKEYWORDS
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000853
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000854 break
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000855
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000856 # fpdef: NAME | '(' fplist ')'
857 names.append(self.com_fpdef(node))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000858
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000859 i = i + 1
860 if i >= len(nodelist):
861 break
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000862
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000863 if nodelist[i][0] == token.EQUAL:
864 defaults.append(self.com_node(nodelist[i + 1]))
865 i = i + 2
866 elif len(defaults):
867 # Treat "(a=1, b)" as "(a=1, b=None)"
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000868 defaults.append(Const(None))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000869
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000870 i = i + 1
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000871
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000872 return names, defaults, flags
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000873
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000874 def com_fpdef(self, node):
875 # fpdef: NAME | '(' fplist ')'
876 if node[1][0] == token.LPAR:
877 return self.com_fplist(node[2])
878 return node[1][1]
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000879
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000880 def com_fplist(self, node):
Tim Peterse0c446b2001-10-18 21:57:37 +0000881 # fplist: fpdef (',' fpdef)* [',']
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000882 if len(node) == 2:
883 return self.com_fpdef(node[1])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000884 list = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000885 for i in range(1, len(node), 2):
886 list.append(self.com_fpdef(node[i]))
887 return tuple(list)
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000888
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000889 def com_dotted_name(self, node):
890 # String together the dotted names and return the string
891 name = ""
892 for n in node:
893 if type(n) == type(()) and n[0] == 1:
894 name = name + n[1] + '.'
895 return name[:-1]
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000896
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000897 def com_dotted_as_name(self, node):
898 dot = self.com_dotted_name(node[1])
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000899 if len(node) <= 2:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000900 return dot, None
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000901 if node[0] == symbol.dotted_name:
902 pass
903 else:
904 assert node[2][1] == 'as'
905 assert node[3][0] == token.NAME
906 return dot, node[3][1]
Jeremy Hylton20516082000-09-01 20:33:26 +0000907
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000908 def com_import_as_name(self, node):
Jeremy Hylton42a08302001-04-09 04:27:12 +0000909 if node[0] == token.STAR:
Jeremy Hylton4e1be722000-10-12 20:23:23 +0000910 return '*', None
Jeremy Hylton42a08302001-04-09 04:27:12 +0000911 assert node[0] == symbol.import_as_name
912 node = node[1:]
913 if len(node) == 1:
914 assert node[0][0] == token.NAME
915 return node[0][1], None
916
917 assert node[1][1] == 'as', node
918 assert node[2][0] == token.NAME
919 return node[0][1], node[2][1]
Jeremy Hylton20516082000-09-01 20:33:26 +0000920
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000921 def com_bases(self, node):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000922 bases = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000923 for i in range(1, len(node), 2):
924 bases.append(self.com_node(node[i]))
925 return bases
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000926
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000927 def com_try_finally(self, nodelist):
928 # try_fin_stmt: "try" ":" suite "finally" ":" suite
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000929 n = TryFinally(self.com_node(nodelist[2]),
Tim Peterse0c446b2001-10-18 21:57:37 +0000930 self.com_node(nodelist[5]))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000931 n.lineno = nodelist[0][2]
932 return n
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000933
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000934 def com_try_except(self, nodelist):
935 # try_except: 'try' ':' suite (except_clause ':' suite)* ['else' suite]
936 #tryexcept: [TryNode, [except_clauses], elseNode)]
937 stmt = self.com_node(nodelist[2])
938 clauses = []
939 elseNode = None
940 for i in range(3, len(nodelist), 3):
941 node = nodelist[i]
942 if node[0] == symbol.except_clause:
943 # except_clause: 'except' [expr [',' expr]] */
944 if len(node) > 2:
945 expr1 = self.com_node(node[2])
946 if len(node) > 4:
947 expr2 = self.com_assign(node[4], OP_ASSIGN)
948 else:
949 expr2 = None
950 else:
951 expr1 = expr2 = None
952 clauses.append((expr1, expr2, self.com_node(nodelist[i+2])))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000953
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000954 if node[0] == token.NAME:
955 elseNode = self.com_node(nodelist[i+2])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000956 n = TryExcept(self.com_node(nodelist[2]), clauses, elseNode)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000957 n.lineno = nodelist[0][2]
958 return n
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000959
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000960 def com_augassign_op(self, node):
961 assert node[0] == symbol.augassign
962 return node[1]
963
964 def com_augassign(self, node):
965 """Return node suitable for lvalue of augmented assignment
966
967 Names, slices, and attributes are the only allowable nodes.
968 """
969 l = self.com_node(node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000970 if l.__class__ in (Name, Slice, Subscript, Getattr):
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000971 return l
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000972 raise SyntaxError, "can't assign to %s" % l.__class__.__name__
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000973
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000974 def com_assign(self, node, assigning):
975 # return a node suitable for use as an "lvalue"
976 # loop to avoid trivial recursion
977 while 1:
978 t = node[0]
Michael W. Hudson3f061892004-07-12 13:15:56 +0000979 if t == symbol.exprlist or t == symbol.testlist or t == symbol.testlist_gexp:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000980 if len(node) > 2:
981 return self.com_assign_tuple(node, assigning)
982 node = node[1]
983 elif t in _assign_types:
984 if len(node) > 2:
985 raise SyntaxError, "can't assign to operator"
986 node = node[1]
987 elif t == symbol.power:
988 if node[1][0] != symbol.atom:
989 raise SyntaxError, "can't assign to operator"
990 if len(node) > 2:
991 primary = self.com_node(node[1])
992 for i in range(2, len(node)-1):
993 ch = node[i]
994 if ch[0] == token.DOUBLESTAR:
995 raise SyntaxError, "can't assign to operator"
996 primary = self.com_apply_trailer(primary, ch)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000997 return self.com_assign_trailer(primary, node[-1],
Tim Peterse0c446b2001-10-18 21:57:37 +0000998 assigning)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000999 node = node[1]
1000 elif t == symbol.atom:
1001 t = node[1][0]
1002 if t == token.LPAR:
1003 node = node[2]
1004 if node[0] == token.RPAR:
1005 raise SyntaxError, "can't assign to ()"
1006 elif t == token.LSQB:
1007 node = node[2]
1008 if node[0] == token.RSQB:
1009 raise SyntaxError, "can't assign to []"
1010 return self.com_assign_list(node, assigning)
1011 elif t == token.NAME:
1012 return self.com_assign_name(node[1], assigning)
1013 else:
1014 raise SyntaxError, "can't assign to literal"
1015 else:
1016 raise SyntaxError, "bad assignment"
1017
1018 def com_assign_tuple(self, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001019 assigns = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001020 for i in range(1, len(node), 2):
1021 assigns.append(self.com_assign(node[i], assigning))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001022 return AssTuple(assigns)
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001023
1024 def com_assign_list(self, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001025 assigns = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001026 for i in range(1, len(node), 2):
Jeremy Hylton2e4cc7e2001-09-17 19:33:48 +00001027 if i + 1 < len(node):
1028 if node[i + 1][0] == symbol.list_for:
1029 raise SyntaxError, "can't assign to list comprehension"
1030 assert node[i + 1][0] == token.COMMA, node[i + 1]
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001031 assigns.append(self.com_assign(node[i], assigning))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001032 return AssList(assigns)
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001033
1034 def com_assign_name(self, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001035 n = AssName(node[1], assigning)
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001036 n.lineno = node[2]
1037 return n
1038
1039 def com_assign_trailer(self, primary, node, assigning):
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001040 t = node[1][0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001041 if t == token.DOT:
1042 return self.com_assign_attr(primary, node[2], assigning)
1043 if t == token.LSQB:
1044 return self.com_subscriptlist(primary, node[2], assigning)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001045 if t == token.LPAR:
1046 raise SyntaxError, "can't assign to function call"
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001047 raise SyntaxError, "unknown trailer type: %s" % t
1048
1049 def com_assign_attr(self, primary, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001050 return AssAttr(primary, node[1], assigning)
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001051
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001052 def com_binary(self, constructor, nodelist):
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001053 "Compile 'NODE (OP NODE)*' into (type, [ node1, ..., nodeN ])."
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001054 l = len(nodelist)
1055 if l == 1:
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001056 n = nodelist[0]
1057 return self.lookup_node(n)(n[1:])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001058 items = []
1059 for i in range(0, l, 2):
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001060 n = nodelist[i]
1061 items.append(self.lookup_node(n)(n[1:]))
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001062 return constructor(items)
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001063
1064 def com_stmt(self, node):
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001065 result = self.lookup_node(node)(node[1:])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001066 assert result is not None
1067 if isinstance(result, Stmt):
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001068 return result
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001069 return Stmt([result])
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001070
1071 def com_append_stmt(self, stmts, node):
1072 result = self.com_node(node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001073 assert result is not None
1074 if isinstance(result, Stmt):
1075 stmts.extend(result.nodes)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001076 else:
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001077 stmts.append(result)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001078
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001079 if hasattr(symbol, 'list_for'):
1080 def com_list_constructor(self, nodelist):
1081 # listmaker: test ( list_for | (',' test)* [','] )
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001082 values = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001083 for i in range(1, len(nodelist)):
1084 if nodelist[i][0] == symbol.list_for:
1085 assert len(nodelist[i:]) == 1
1086 return self.com_list_comprehension(values[0],
1087 nodelist[i])
1088 elif nodelist[i][0] == token.COMMA:
1089 continue
1090 values.append(self.com_node(nodelist[i]))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001091 return List(values)
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001092
1093 def com_list_comprehension(self, expr, node):
1094 # list_iter: list_for | list_if
1095 # list_for: 'for' exprlist 'in' testlist [list_iter]
1096 # list_if: 'if' test [list_iter]
Jeremy Hylton42a08302001-04-09 04:27:12 +00001097
1098 # XXX should raise SyntaxError for assignment
Tim Peterse0c446b2001-10-18 21:57:37 +00001099
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001100 lineno = node[1][2]
1101 fors = []
1102 while node:
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001103 t = node[1][1]
1104 if t == 'for':
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001105 assignNode = self.com_assign(node[2], OP_ASSIGN)
1106 listNode = self.com_node(node[4])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001107 newfor = ListCompFor(assignNode, listNode, [])
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001108 newfor.lineno = node[1][2]
1109 fors.append(newfor)
1110 if len(node) == 5:
1111 node = None
1112 else:
1113 node = self.com_list_iter(node[5])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001114 elif t == 'if':
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001115 test = self.com_node(node[2])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001116 newif = ListCompIf(test)
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001117 newif.lineno = node[1][2]
1118 newfor.ifs.append(newif)
1119 if len(node) == 3:
1120 node = None
1121 else:
1122 node = self.com_list_iter(node[3])
1123 else:
1124 raise SyntaxError, \
1125 ("unexpected list comprehension element: %s %d"
1126 % (node, lineno))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001127 n = ListComp(expr, fors)
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001128 n.lineno = lineno
1129 return n
1130
1131 def com_list_iter(self, node):
1132 assert node[0] == symbol.list_iter
1133 return node[1]
1134 else:
1135 def com_list_constructor(self, nodelist):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001136 values = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001137 for i in range(1, len(nodelist), 2):
1138 values.append(self.com_node(nodelist[i]))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001139 return List(values)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001140
Raymond Hettinger354433a2004-05-19 08:20:33 +00001141 if hasattr(symbol, 'gen_for'):
1142 def com_generator_expression(self, expr, node):
1143 # gen_iter: gen_for | gen_if
1144 # gen_for: 'for' exprlist 'in' test [gen_iter]
1145 # gen_if: 'if' test [gen_iter]
1146
1147 lineno = node[1][2]
1148 fors = []
1149 while node:
1150 t = node[1][1]
1151 if t == 'for':
1152 assignNode = self.com_assign(node[2], OP_ASSIGN)
1153 genNode = self.com_node(node[4])
1154 newfor = GenExprFor(assignNode, genNode, [])
1155 newfor.lineno = node[1][2]
1156 fors.append(newfor)
1157 if (len(node)) == 5:
1158 node = None
1159 else:
1160 node = self.com_gen_iter(node[5])
1161 elif t == 'if':
1162 test = self.com_node(node[2])
1163 newif = GenExprIf(test)
1164 newif.lineno = node[1][2]
1165 newfor.ifs.append(newif)
1166 if len(node) == 3:
1167 node = None
1168 else:
1169 node = self.com_gen_iter(node[3])
1170 else:
1171 raise SyntaxError, \
1172 ("unexpected generator expression element: %s %d"
1173 % (node, lineno))
1174 fors[0].is_outmost = True
1175 n = GenExpr(GenExprInner(expr, fors))
1176 n.lineno = lineno
1177 return n
1178
1179 def com_gen_iter(self, node):
1180 assert node[0] == symbol.gen_iter
1181 return node[1]
1182
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001183 def com_dictmaker(self, nodelist):
1184 # dictmaker: test ':' test (',' test ':' value)* [',']
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001185 items = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001186 for i in range(1, len(nodelist), 4):
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001187 items.append((self.com_node(nodelist[i]),
1188 self.com_node(nodelist[i+2])))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001189 return Dict(items)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001190
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001191 def com_apply_trailer(self, primaryNode, nodelist):
1192 t = nodelist[1][0]
1193 if t == token.LPAR:
1194 return self.com_call_function(primaryNode, nodelist[2])
1195 if t == token.DOT:
1196 return self.com_select_member(primaryNode, nodelist[2])
1197 if t == token.LSQB:
1198 return self.com_subscriptlist(primaryNode, nodelist[2], OP_APPLY)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001199
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001200 raise SyntaxError, 'unknown node type: %s' % t
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001201
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001202 def com_select_member(self, primaryNode, nodelist):
1203 if nodelist[0] != token.NAME:
1204 raise SyntaxError, "member must be a name"
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001205 n = Getattr(primaryNode, nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001206 n.lineno = nodelist[2]
1207 return n
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001208
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001209 def com_call_function(self, primaryNode, nodelist):
1210 if nodelist[0] == token.RPAR:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001211 return CallFunc(primaryNode, [])
1212 args = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001213 kw = 0
1214 len_nodelist = len(nodelist)
1215 for i in range(1, len_nodelist, 2):
1216 node = nodelist[i]
1217 if node[0] == token.STAR or node[0] == token.DOUBLESTAR:
Tim Peterse0c446b2001-10-18 21:57:37 +00001218 break
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001219 kw, result = self.com_argument(node, kw)
Raymond Hettinger354433a2004-05-19 08:20:33 +00001220 if len_nodelist != 2 and isinstance(result, GenExpr):
1221 raise SyntaxError, 'generator expression needs parenthesis'
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001222 args.append(result)
Jeremy Hyltonbe317e62000-05-02 22:32:59 +00001223 else:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001224 # No broken by star arg, so skip the last one we processed.
1225 i = i + 1
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001226 if i < len_nodelist and nodelist[i][0] == token.COMMA:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001227 # need to accept an application that looks like "f(a, b,)"
1228 i = i + 1
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001229 star_node = dstar_node = None
1230 while i < len_nodelist:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001231 tok = nodelist[i]
1232 ch = nodelist[i+1]
1233 i = i + 3
1234 if tok[0]==token.STAR:
1235 if star_node is not None:
1236 raise SyntaxError, 'already have the varargs indentifier'
1237 star_node = self.com_node(ch)
1238 elif tok[0]==token.DOUBLESTAR:
1239 if dstar_node is not None:
1240 raise SyntaxError, 'already have the kwargs indentifier'
1241 dstar_node = self.com_node(ch)
1242 else:
1243 raise SyntaxError, 'unknown node type: %s' % tok
Jeremy Hyltonbe317e62000-05-02 22:32:59 +00001244
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001245 return CallFunc(primaryNode, args, star_node, dstar_node)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001246
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001247 def com_argument(self, nodelist, kw):
Raymond Hettinger354433a2004-05-19 08:20:33 +00001248 if len(nodelist) == 3 and nodelist[2][0] == symbol.gen_for:
1249 test = self.com_node(nodelist[1])
1250 return 0, self.com_generator_expression(test, nodelist[2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001251 if len(nodelist) == 2:
1252 if kw:
1253 raise SyntaxError, "non-keyword arg after keyword arg"
1254 return 0, self.com_node(nodelist[1])
1255 result = self.com_node(nodelist[3])
1256 n = nodelist[1]
1257 while len(n) == 2 and n[0] != token.NAME:
1258 n = n[1]
1259 if n[0] != token.NAME:
1260 raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001261 node = Keyword(n[1], result)
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001262 node.lineno = n[2]
1263 return 1, node
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001264
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001265 def com_subscriptlist(self, primary, nodelist, assigning):
1266 # slicing: simple_slicing | extended_slicing
1267 # simple_slicing: primary "[" short_slice "]"
Tim Peterse0c446b2001-10-18 21:57:37 +00001268 # extended_slicing: primary "[" slice_list "]"
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001269 # slice_list: slice_item ("," slice_item)* [","]
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001270
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001271 # backwards compat slice for '[i:j]'
1272 if len(nodelist) == 2:
1273 sub = nodelist[1]
1274 if (sub[1][0] == token.COLON or \
1275 (len(sub) > 2 and sub[2][0] == token.COLON)) and \
1276 sub[-1][0] != symbol.sliceop:
1277 return self.com_slice(primary, sub, assigning)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001278
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001279 subscripts = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001280 for i in range(1, len(nodelist), 2):
1281 subscripts.append(self.com_subscript(nodelist[i]))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001282
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001283 return Subscript(primary, assigning, subscripts)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001284
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001285 def com_subscript(self, node):
1286 # slice_item: expression | proper_slice | ellipsis
1287 ch = node[1]
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001288 t = ch[0]
1289 if t == token.DOT and node[2][0] == token.DOT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001290 return Ellipsis()
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001291 if t == token.COLON or len(node) > 2:
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001292 return self.com_sliceobj(node)
1293 return self.com_node(ch)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001294
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001295 def com_sliceobj(self, node):
1296 # proper_slice: short_slice | long_slice
1297 # short_slice: [lower_bound] ":" [upper_bound]
1298 # long_slice: short_slice ":" [stride]
1299 # lower_bound: expression
1300 # upper_bound: expression
1301 # stride: expression
1302 #
1303 # Note: a stride may be further slicing...
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001304
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001305 items = []
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001306
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001307 if node[1][0] == token.COLON:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001308 items.append(Const(None))
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001309 i = 2
1310 else:
1311 items.append(self.com_node(node[1]))
1312 # i == 2 is a COLON
1313 i = 3
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001314
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001315 if i < len(node) and node[i][0] == symbol.test:
1316 items.append(self.com_node(node[i]))
1317 i = i + 1
1318 else:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001319 items.append(Const(None))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001320
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001321 # a short_slice has been built. look for long_slice now by looking
1322 # for strides...
1323 for j in range(i, len(node)):
1324 ch = node[j]
1325 if len(ch) == 2:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001326 items.append(Const(None))
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001327 else:
1328 items.append(self.com_node(ch[2]))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001329
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001330 return Sliceobj(items)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001331
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001332 def com_slice(self, primary, node, assigning):
1333 # short_slice: [lower_bound] ":" [upper_bound]
1334 lower = upper = None
1335 if len(node) == 3:
1336 if node[1][0] == token.COLON:
1337 upper = self.com_node(node[2])
1338 else:
1339 lower = self.com_node(node[1])
1340 elif len(node) == 4:
1341 lower = self.com_node(node[1])
1342 upper = self.com_node(node[3])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001343 return Slice(primary, assigning, lower, upper)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001344
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001345 def get_docstring(self, node, n=None):
1346 if n is None:
1347 n = node[0]
1348 node = node[1:]
1349 if n == symbol.suite:
1350 if len(node) == 1:
1351 return self.get_docstring(node[0])
1352 for sub in node:
1353 if sub[0] == symbol.stmt:
1354 return self.get_docstring(sub)
1355 return None
1356 if n == symbol.file_input:
1357 for sub in node:
1358 if sub[0] == symbol.stmt:
1359 return self.get_docstring(sub)
1360 return None
1361 if n == symbol.atom:
1362 if node[0][0] == token.STRING:
1363 s = ''
1364 for t in node:
1365 s = s + eval(t[1])
1366 return s
1367 return None
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001368 if n == symbol.stmt or n == symbol.simple_stmt \
1369 or n == symbol.small_stmt:
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001370 return self.get_docstring(node[0])
1371 if n in _doc_nodes and len(node) == 1:
1372 return self.get_docstring(node[0])
1373 return None
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001374
1375
1376_doc_nodes = [
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001377 symbol.expr_stmt,
1378 symbol.testlist,
Jeremy Hyltond1142612001-10-17 13:32:52 +00001379 symbol.testlist_safe,
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001380 symbol.test,
1381 symbol.and_test,
1382 symbol.not_test,
1383 symbol.comparison,
1384 symbol.expr,
1385 symbol.xor_expr,
1386 symbol.and_expr,
1387 symbol.shift_expr,
1388 symbol.arith_expr,
1389 symbol.term,
1390 symbol.factor,
1391 symbol.power,
1392 ]
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001393
1394# comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1395# | 'in' | 'not' 'in' | 'is' | 'is' 'not'
1396_cmp_types = {
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001397 token.LESS : '<',
1398 token.GREATER : '>',
1399 token.EQEQUAL : '==',
1400 token.EQUAL : '==',
1401 token.LESSEQUAL : '<=',
1402 token.GREATEREQUAL : '>=',
1403 token.NOTEQUAL : '!=',
1404 }
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001405
1406_legal_node_types = [
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001407 symbol.funcdef,
1408 symbol.classdef,
1409 symbol.stmt,
1410 symbol.small_stmt,
1411 symbol.flow_stmt,
1412 symbol.simple_stmt,
1413 symbol.compound_stmt,
1414 symbol.expr_stmt,
1415 symbol.print_stmt,
1416 symbol.del_stmt,
1417 symbol.pass_stmt,
1418 symbol.break_stmt,
1419 symbol.continue_stmt,
1420 symbol.return_stmt,
1421 symbol.raise_stmt,
1422 symbol.import_stmt,
1423 symbol.global_stmt,
1424 symbol.exec_stmt,
1425 symbol.assert_stmt,
1426 symbol.if_stmt,
1427 symbol.while_stmt,
1428 symbol.for_stmt,
1429 symbol.try_stmt,
1430 symbol.suite,
1431 symbol.testlist,
Jeremy Hyltond1142612001-10-17 13:32:52 +00001432 symbol.testlist_safe,
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001433 symbol.test,
1434 symbol.and_test,
1435 symbol.not_test,
1436 symbol.comparison,
1437 symbol.exprlist,
1438 symbol.expr,
1439 symbol.xor_expr,
1440 symbol.and_expr,
1441 symbol.shift_expr,
1442 symbol.arith_expr,
1443 symbol.term,
1444 symbol.factor,
1445 symbol.power,
1446 symbol.atom,
1447 ]
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001448
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001449if hasattr(symbol, 'yield_stmt'):
1450 _legal_node_types.append(symbol.yield_stmt)
1451
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001452_assign_types = [
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001453 symbol.test,
1454 symbol.and_test,
1455 symbol.not_test,
1456 symbol.comparison,
1457 symbol.expr,
1458 symbol.xor_expr,
1459 symbol.and_expr,
1460 symbol.shift_expr,
1461 symbol.arith_expr,
1462 symbol.term,
1463 symbol.factor,
1464 ]
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001465
1466import types
1467_names = {}
1468for k, v in symbol.sym_name.items():
1469 _names[k] = v
1470for k, v in token.tok_name.items():
1471 _names[k] = v
1472
1473def debug_tree(tree):
1474 l = []
1475 for elt in tree:
1476 if type(elt) == types.IntType:
1477 l.append(_names.get(elt, elt))
1478 elif type(elt) == types.StringType:
1479 l.append(elt)
1480 else:
1481 l.append(debug_tree(elt))
1482 return l