blob: 840d79f380fafd12fcc21327194936a143757eb5 [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
Jeremy Hylton566d9342004-09-07 15:28:01 +000017#
18# Some fixes to try to have correct line number on almost all nodes
19# (except Module, Discard and Stmt) added by Sylvain Thenault
20#
Jeremy Hylton9c048f92000-10-13 21:58:13 +000021# Portions of this file are:
22# Copyright (C) 1997-1998 Greg Stein. All Rights Reserved.
23#
24# This module is provided under a BSD-ish license. See
25# http://www.opensource.org/licenses/bsd-license.html
26# and replace OWNER, ORGANIZATION, and YEAR as appropriate.
27
Jeremy Hylton566d9342004-09-07 15:28:01 +000028from compiler.ast import *
Jeremy Hyltonf968e852000-02-04 00:25:23 +000029import parser
30import symbol
31import token
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +000032import sys
Jeremy Hyltonf968e852000-02-04 00:25:23 +000033
Jeremy Hylton566d9342004-09-07 15:28:01 +000034class WalkerError(StandardError):
35 pass
Jeremy Hyltonf968e852000-02-04 00:25:23 +000036
Jeremy Hylton9605c112000-02-08 18:57:32 +000037from consts import CO_VARARGS, CO_VARKEYWORDS
38from consts import OP_ASSIGN, OP_DELETE, OP_APPLY
Jeremy Hyltonf968e852000-02-04 00:25:23 +000039
Jeremy Hyltonfa974a92000-03-06 18:50:48 +000040def parseFile(path):
Jeremy Hylton566d9342004-09-07 15:28:01 +000041 f = open(path, "U")
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +000042 # XXX The parser API tolerates files without a trailing newline,
43 # but not strings without a trailing newline. Always add an extra
44 # newline to the file contents, since we're going through the string
45 # version of the API.
46 src = f.read() + "\n"
Jeremy Hyltonfa974a92000-03-06 18:50:48 +000047 f.close()
48 return parse(src)
49
Jeremy Hylton9dca3642001-09-17 21:02:51 +000050def parse(buf, mode="exec"):
51 if mode == "exec" or mode == "single":
52 return Transformer().parsesuite(buf)
53 elif mode == "eval":
54 return Transformer().parseexpr(buf)
55 else:
56 raise ValueError("compile() arg 3 must be"
57 " 'exec' or 'eval' or 'single'")
Jeremy Hyltonfa974a92000-03-06 18:50:48 +000058
Jeremy Hyltonf968e852000-02-04 00:25:23 +000059def asList(nodes):
Jeremy Hylton572bdce2000-09-20 02:47:28 +000060 l = []
61 for item in nodes:
62 if hasattr(item, "asList"):
63 l.append(item.asList())
64 else:
65 if type(item) is type( (None, None) ):
66 l.append(tuple(asList(item)))
67 elif type(item) is type( [] ):
68 l.append(asList(item))
69 else:
70 l.append(item)
71 return l
Jeremy Hyltonf968e852000-02-04 00:25:23 +000072
Jeremy Hylton566d9342004-09-07 15:28:01 +000073def extractLineNo(ast):
74 if not isinstance(ast[1], tuple):
75 # get a terminal node
76 return ast[2]
77 for child in ast[1:]:
78 if isinstance(child, tuple):
79 lineno = extractLineNo(child)
80 if lineno is not None:
81 return lineno
Tim Peters0e9980f2004-09-12 03:49:31 +000082
Jeremy Hyltonf968e852000-02-04 00:25:23 +000083def Node(*args):
Jeremy Hylton572bdce2000-09-20 02:47:28 +000084 kind = args[0]
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +000085 if nodes.has_key(kind):
Jeremy Hylton572bdce2000-09-20 02:47:28 +000086 try:
Raymond Hettingerff41c482003-04-06 09:01:11 +000087 return nodes[kind](*args[1:])
Jeremy Hylton572bdce2000-09-20 02:47:28 +000088 except TypeError:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +000089 print nodes[kind], len(args), args
Jeremy Hylton572bdce2000-09-20 02:47:28 +000090 raise
91 else:
Jeremy Hylton566d9342004-09-07 15:28:01 +000092 raise WalkerEror, "Can't find appropriate Node type: %s" % str(args)
Jeremy Hylton572bdce2000-09-20 02:47:28 +000093 #return apply(ast.Node, args)
Jeremy Hyltonf968e852000-02-04 00:25:23 +000094
95class Transformer:
Jeremy Hylton572bdce2000-09-20 02:47:28 +000096 """Utility object for transforming Python parse trees.
Jeremy Hyltonf968e852000-02-04 00:25:23 +000097
Jeremy Hylton572bdce2000-09-20 02:47:28 +000098 Exposes the following methods:
99 tree = transform(ast_tree)
100 tree = parsesuite(text)
101 tree = parseexpr(text)
102 tree = parsefile(fileob | filename)
103 """
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000104
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000105 def __init__(self):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000106 self._dispatch = {}
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000107 for value, name in symbol.sym_name.items():
108 if hasattr(self, name):
109 self._dispatch[value] = getattr(self, name)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000110 self._dispatch[token.NEWLINE] = self.com_NEWLINE
111 self._atom_dispatch = {token.LPAR: self.atom_lpar,
112 token.LSQB: self.atom_lsqb,
113 token.LBRACE: self.atom_lbrace,
114 token.BACKQUOTE: self.atom_backquote,
115 token.NUMBER: self.atom_number,
116 token.STRING: self.atom_string,
117 token.NAME: self.atom_name,
118 }
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000119 self.encoding = None
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000120
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000121 def transform(self, tree):
122 """Transform an AST into a modified parse tree."""
Jeremy Hylton566d9342004-09-07 15:28:01 +0000123 if not (isinstance(tree, tuple) or isinstance(tree, list)):
Jeremy Hylton7845cf82001-08-29 20:56:30 +0000124 tree = parser.ast2tuple(tree, line_info=1)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000125 return self.compile_node(tree)
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000126
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000127 def parsesuite(self, text):
128 """Return a modified parse tree for the given suite text."""
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000129 return self.transform(parser.suite(text))
Tim Peters0e9980f2004-09-12 03:49:31 +0000130
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000131 def parseexpr(self, text):
132 """Return a modified parse tree for the given expression text."""
133 return self.transform(parser.expr(text))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000134
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000135 def parsefile(self, file):
136 """Return a modified parse tree for the contents of the given file."""
137 if type(file) == type(''):
138 file = open(file)
139 return self.parsesuite(file.read())
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000140
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000141 # --------------------------------------------------------------
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000142 #
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000143 # PRIVATE METHODS
144 #
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000145
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000146 def compile_node(self, node):
147 ### emit a line-number node?
148 n = node[0]
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000149
150 if n == symbol.encoding_decl:
151 self.encoding = node[2]
152 node = node[1]
153 n = node[0]
Tim Peters669454e2003-03-07 17:30:48 +0000154
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000155 if n == symbol.single_input:
156 return self.single_input(node[1:])
157 if n == symbol.file_input:
158 return self.file_input(node[1:])
159 if n == symbol.eval_input:
160 return self.eval_input(node[1:])
161 if n == symbol.lambdef:
162 return self.lambdef(node[1:])
163 if n == symbol.funcdef:
164 return self.funcdef(node[1:])
165 if n == symbol.classdef:
166 return self.classdef(node[1:])
167
Jeremy Hylton566d9342004-09-07 15:28:01 +0000168 raise WalkerEror, ('unexpected node type', n)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000169
170 def single_input(self, node):
171 ### do we want to do anything about being "interactive" ?
172
173 # NEWLINE | simple_stmt | compound_stmt NEWLINE
174 n = node[0][0]
175 if n != token.NEWLINE:
176 return self.com_stmt(node[0])
177
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000178 return Pass()
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000179
180 def file_input(self, nodelist):
181 doc = self.get_docstring(nodelist, symbol.file_input)
Jeremy Hyltonec927342001-08-18 00:04:31 +0000182 if doc is not None:
183 i = 1
184 else:
185 i = 0
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000186 stmts = []
Jeremy Hyltonec927342001-08-18 00:04:31 +0000187 for node in nodelist[i:]:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000188 if node[0] != token.ENDMARKER and node[0] != token.NEWLINE:
189 self.com_append_stmt(stmts, node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000190 return Module(doc, Stmt(stmts))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000191
192 def eval_input(self, nodelist):
193 # from the built-in function input()
194 ### is this sufficient?
Barry Warsaw52acb492001-12-21 20:04:22 +0000195 return Expression(self.com_node(nodelist[0]))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000196
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000197 def decorator_name(self, nodelist):
198 listlen = len(nodelist)
199 assert listlen >= 1 and listlen % 2 == 1
200
201 item = self.atom_name(nodelist)
202 i = 1
203 while i < listlen:
204 assert nodelist[i][0] == token.DOT
205 assert nodelist[i + 1][0] == token.NAME
206 item = Getattr(item, nodelist[i + 1][1])
207 i += 2
208
209 return item
Tim Peters6db15d72004-08-04 02:36:18 +0000210
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000211 def decorator(self, nodelist):
212 # '@' dotted_name [ '(' [arglist] ')' ]
Michael W. Hudson0ccff072004-08-17 17:29:16 +0000213 assert len(nodelist) in (3, 5, 6)
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000214 assert nodelist[0][0] == token.AT
Michael W. Hudson0ccff072004-08-17 17:29:16 +0000215 assert nodelist[-1][0] == token.NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000216
217 assert nodelist[1][0] == symbol.dotted_name
218 funcname = self.decorator_name(nodelist[1][1:])
219
Michael W. Hudson0ccff072004-08-17 17:29:16 +0000220 if len(nodelist) > 3:
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000221 assert nodelist[2][0] == token.LPAR
222 expr = self.com_call_function(funcname, nodelist[3])
223 else:
224 expr = funcname
Tim Peters6db15d72004-08-04 02:36:18 +0000225
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000226 return expr
Tim Peters6db15d72004-08-04 02:36:18 +0000227
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000228 def decorators(self, nodelist):
229 # decorators: decorator ([NEWLINE] decorator)* NEWLINE
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000230 items = []
Michael W. Hudson0ccff072004-08-17 17:29:16 +0000231 for dec_nodelist in nodelist:
232 assert dec_nodelist[0] == symbol.decorator
233 items.append(self.decorator(dec_nodelist[1:]))
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000234 return Decorators(items)
Tim Peters6db15d72004-08-04 02:36:18 +0000235
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000236 def funcdef(self, nodelist):
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000237 # -6 -5 -4 -3 -2 -1
238 # funcdef: [decorators] 'def' NAME parameters ':' suite
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000239 # parameters: '(' [varargslist] ')'
240
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000241 if len(nodelist) == 6:
242 assert nodelist[0][0] == symbol.decorators
243 decorators = self.decorators(nodelist[0][1:])
244 else:
245 assert len(nodelist) == 5
246 decorators = None
Tim Peters6db15d72004-08-04 02:36:18 +0000247
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000248 lineno = nodelist[-4][2]
249 name = nodelist[-4][1]
250 args = nodelist[-3][2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000251
252 if args[0] == symbol.varargslist:
253 names, defaults, flags = self.com_arglist(args[1:])
254 else:
255 names = defaults = ()
256 flags = 0
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000257 doc = self.get_docstring(nodelist[-1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000258
259 # code for function
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000260 code = self.com_node(nodelist[-1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000261
Jeremy Hyltona59ac0a2000-11-06 03:33:52 +0000262 if doc is not None:
263 assert isinstance(code, Stmt)
264 assert isinstance(code.nodes[0], Discard)
265 del code.nodes[0]
Jeremy Hylton566d9342004-09-07 15:28:01 +0000266 return Function(decorators, name, names, defaults, flags, doc, code,
267 lineno=lineno)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000268
269 def lambdef(self, nodelist):
270 # lambdef: 'lambda' [varargslist] ':' test
271 if nodelist[2][0] == symbol.varargslist:
272 names, defaults, flags = self.com_arglist(nodelist[2][1:])
273 else:
274 names = defaults = ()
275 flags = 0
276
277 # code for lambda
278 code = self.com_node(nodelist[-1])
279
Jeremy Hylton566d9342004-09-07 15:28:01 +0000280 return Lambda(names, defaults, flags, code, lineno=nodelist[1][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000281
282 def classdef(self, nodelist):
283 # classdef: 'class' NAME ['(' testlist ')'] ':' suite
284
285 name = nodelist[1][1]
286 doc = self.get_docstring(nodelist[-1])
287 if nodelist[2][0] == token.COLON:
288 bases = []
289 else:
290 bases = self.com_bases(nodelist[3])
291
292 # code for class
293 code = self.com_node(nodelist[-1])
294
Jeremy Hyltonda8db8c2001-08-29 17:19:02 +0000295 if doc is not None:
296 assert isinstance(code, Stmt)
297 assert isinstance(code.nodes[0], Discard)
298 del code.nodes[0]
299
Jeremy Hylton566d9342004-09-07 15:28:01 +0000300 return Class(name, bases, doc, code, lineno=nodelist[1][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000301
302 def stmt(self, nodelist):
303 return self.com_stmt(nodelist[0])
304
305 small_stmt = stmt
306 flow_stmt = stmt
307 compound_stmt = stmt
308
309 def simple_stmt(self, nodelist):
310 # small_stmt (';' small_stmt)* [';'] NEWLINE
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000311 stmts = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000312 for i in range(0, len(nodelist), 2):
313 self.com_append_stmt(stmts, nodelist[i])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000314 return Stmt(stmts)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000315
316 def parameters(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000317 raise WalkerEror
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000318
319 def varargslist(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000320 raise WalkerEror
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000321
322 def fpdef(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000323 raise WalkerEror
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000324
325 def fplist(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000326 raise WalkerEror
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000327
328 def dotted_name(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000329 raise WalkerEror
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000330
331 def comp_op(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000332 raise WalkerEror
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000333
334 def trailer(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000335 raise WalkerEror
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000336
337 def sliceop(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000338 raise WalkerEror
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000339
340 def argument(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000341 raise WalkerEror
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000342
343 # --------------------------------------------------------------
344 #
345 # STATEMENT NODES (invoked by com_node())
346 #
347
348 def expr_stmt(self, nodelist):
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000349 # augassign testlist | testlist ('=' testlist)*
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +0000350 en = nodelist[-1]
351 exprNode = self.lookup_node(en)(en[1:])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000352 if len(nodelist) == 1:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000353 return Discard(exprNode, lineno=exprNode.lineno)
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000354 if nodelist[1][0] == token.EQUAL:
Martin v. Löwis51e234a2002-02-28 17:48:48 +0000355 nodesl = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000356 for i in range(0, len(nodelist) - 2, 2):
Martin v. Löwis51e234a2002-02-28 17:48:48 +0000357 nodesl.append(self.com_assign(nodelist[i], OP_ASSIGN))
Jeremy Hylton566d9342004-09-07 15:28:01 +0000358 return Assign(nodesl, exprNode, lineno=nodelist[1][2])
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000359 else:
360 lval = self.com_augassign(nodelist[0])
361 op = self.com_augassign_op(nodelist[1])
Jeremy Hylton566d9342004-09-07 15:28:01 +0000362 return AugAssign(lval, op[1], exprNode, lineno=op[2])
363 raise WalkerError, "can't get here"
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000364
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 Hylton566d9342004-09-07 15:28:01 +0000382 return Print(items, dest, lineno=nodelist[0][2])
383 return Printnl(items, dest, lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000384
385 def del_stmt(self, nodelist):
386 return self.com_assign(nodelist[1], OP_DELETE)
387
388 def pass_stmt(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000389 return Pass(lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000390
391 def break_stmt(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000392 return Break(lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000393
394 def continue_stmt(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000395 return Continue(lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000396
397 def return_stmt(self, nodelist):
398 # return: [testlist]
399 if len(nodelist) < 2:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000400 return Return(Const(None), lineno=nodelist[0][2])
401 return Return(self.com_node(nodelist[1]), lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000402
Jeremy Hyltonec927342001-08-18 00:04:31 +0000403 def yield_stmt(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000404 return Yield(self.com_node(nodelist[1]), lineno=nodelist[0][2])
Jeremy Hyltonec927342001-08-18 00:04:31 +0000405
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000406 def raise_stmt(self, nodelist):
407 # raise: [test [',' test [',' test]]]
408 if len(nodelist) > 5:
409 expr3 = self.com_node(nodelist[5])
410 else:
411 expr3 = None
412 if len(nodelist) > 3:
413 expr2 = self.com_node(nodelist[3])
414 else:
415 expr2 = None
416 if len(nodelist) > 1:
417 expr1 = self.com_node(nodelist[1])
418 else:
419 expr1 = None
Jeremy Hylton566d9342004-09-07 15:28:01 +0000420 return Raise(expr1, expr2, expr3, lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000421
422 def import_stmt(self, nodelist):
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000423 # import_stmt: import_name | import_from
424 assert len(nodelist) == 1
425 return self.com_node(nodelist[0])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000426
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000427 def import_name(self, nodelist):
428 # import_name: 'import' dotted_as_names
Tim Peters0e9980f2004-09-12 03:49:31 +0000429 return Import(self.com_dotted_as_names(nodelist[1]),
Jeremy Hylton566d9342004-09-07 15:28:01 +0000430 lineno=nodelist[0][2])
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000431
432 def import_from(self, nodelist):
433 # import_from: 'from' dotted_name 'import' ('*' |
434 # '(' import_as_names ')' | import_as_names)
435 assert nodelist[0][1] == 'from'
436 assert nodelist[1][0] == symbol.dotted_name
437 assert nodelist[2][1] == 'import'
438 fromname = self.com_dotted_name(nodelist[1])
439 if nodelist[3][0] == token.STAR:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000440 # TODO(jhylton): where is the lineno?
441 return From(fromname, [('*', None)])
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000442 else:
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000443 node = nodelist[3 + (nodelist[3][0] == token.LPAR)]
Jeremy Hylton566d9342004-09-07 15:28:01 +0000444 return From(fromname, self.com_import_as_names(node),
445 lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000446
447 def global_stmt(self, nodelist):
448 # global: NAME (',' NAME)*
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000449 names = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000450 for i in range(1, len(nodelist), 2):
451 names.append(nodelist[i][1])
Jeremy Hylton566d9342004-09-07 15:28:01 +0000452 return Global(names, lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000453
454 def exec_stmt(self, nodelist):
455 # exec_stmt: 'exec' expr ['in' expr [',' expr]]
456 expr1 = self.com_node(nodelist[1])
457 if len(nodelist) >= 4:
458 expr2 = self.com_node(nodelist[3])
459 if len(nodelist) >= 6:
460 expr3 = self.com_node(nodelist[5])
461 else:
462 expr3 = None
463 else:
464 expr2 = expr3 = None
465
Jeremy Hylton566d9342004-09-07 15:28:01 +0000466 return Exec(expr1, expr2, expr3, lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000467
468 def assert_stmt(self, nodelist):
469 # 'assert': test, [',' test]
470 expr1 = self.com_node(nodelist[1])
471 if (len(nodelist) == 4):
472 expr2 = self.com_node(nodelist[3])
473 else:
Jeremy Hyltona59ac0a2000-11-06 03:33:52 +0000474 expr2 = None
Jeremy Hylton566d9342004-09-07 15:28:01 +0000475 return Assert(expr1, expr2, lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000476
477 def if_stmt(self, nodelist):
478 # if: test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000479 tests = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000480 for i in range(0, len(nodelist) - 3, 4):
481 testNode = self.com_node(nodelist[i + 1])
482 suiteNode = self.com_node(nodelist[i + 3])
483 tests.append((testNode, suiteNode))
484
485 if len(nodelist) % 4 == 3:
486 elseNode = self.com_node(nodelist[-1])
487## elseNode.lineno = nodelist[-1][1][2]
488 else:
489 elseNode = None
Jeremy Hylton566d9342004-09-07 15:28:01 +0000490 return If(tests, elseNode, lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000491
492 def while_stmt(self, nodelist):
493 # 'while' test ':' suite ['else' ':' suite]
494
495 testNode = self.com_node(nodelist[1])
496 bodyNode = self.com_node(nodelist[3])
497
498 if len(nodelist) > 4:
499 elseNode = self.com_node(nodelist[6])
500 else:
501 elseNode = None
502
Jeremy Hylton566d9342004-09-07 15:28:01 +0000503 return While(testNode, bodyNode, elseNode, lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000504
505 def for_stmt(self, nodelist):
506 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
507
508 assignNode = self.com_assign(nodelist[1], OP_ASSIGN)
509 listNode = self.com_node(nodelist[3])
510 bodyNode = self.com_node(nodelist[5])
511
512 if len(nodelist) > 8:
513 elseNode = self.com_node(nodelist[8])
514 else:
515 elseNode = None
516
Tim Peters0e9980f2004-09-12 03:49:31 +0000517 return For(assignNode, listNode, bodyNode, elseNode,
Jeremy Hylton566d9342004-09-07 15:28:01 +0000518 lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000519
520 def try_stmt(self, nodelist):
521 # 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
522 # | 'try' ':' suite 'finally' ':' suite
523 if nodelist[3][0] != symbol.except_clause:
524 return self.com_try_finally(nodelist)
525
526 return self.com_try_except(nodelist)
527
528 def suite(self, nodelist):
529 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
530 if len(nodelist) == 1:
531 return self.com_stmt(nodelist[0])
532
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000533 stmts = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000534 for node in nodelist:
535 if node[0] == symbol.stmt:
536 self.com_append_stmt(stmts, node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000537 return Stmt(stmts)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000538
539 # --------------------------------------------------------------
540 #
541 # EXPRESSION NODES (invoked by com_node())
542 #
543
544 def testlist(self, nodelist):
545 # testlist: expr (',' expr)* [',']
Jeremy Hyltond1142612001-10-17 13:32:52 +0000546 # testlist_safe: test [(',' test)+ [',']]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000547 # exprlist: expr (',' expr)* [',']
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000548 return self.com_binary(Tuple, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000549
Jeremy Hyltond1142612001-10-17 13:32:52 +0000550 testlist_safe = testlist # XXX
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000551 testlist1 = testlist
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000552 exprlist = testlist
553
Raymond Hettinger354433a2004-05-19 08:20:33 +0000554 def testlist_gexp(self, nodelist):
555 if len(nodelist) == 2 and nodelist[1][0] == symbol.gen_for:
556 test = self.com_node(nodelist[0])
557 return self.com_generator_expression(test, nodelist[1])
558 return self.testlist(nodelist)
559
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000560 def test(self, nodelist):
561 # and_test ('or' and_test)* | lambdef
562 if len(nodelist) == 1 and nodelist[0][0] == symbol.lambdef:
563 return self.lambdef(nodelist[0])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000564 return self.com_binary(Or, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000565
566 def and_test(self, nodelist):
567 # not_test ('and' not_test)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000568 return self.com_binary(And, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000569
570 def not_test(self, nodelist):
571 # 'not' not_test | comparison
572 result = self.com_node(nodelist[-1])
573 if len(nodelist) == 2:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000574 return Not(result, lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000575 return result
576
577 def comparison(self, nodelist):
578 # comparison: expr (comp_op expr)*
579 node = self.com_node(nodelist[0])
580 if len(nodelist) == 1:
581 return node
582
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000583 results = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000584 for i in range(2, len(nodelist), 2):
585 nl = nodelist[i-1]
586
587 # comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
588 # | 'in' | 'not' 'in' | 'is' | 'is' 'not'
589 n = nl[1]
590 if n[0] == token.NAME:
591 type = n[1]
592 if len(nl) == 3:
593 if type == 'not':
594 type = 'not in'
595 else:
596 type = 'is not'
597 else:
598 type = _cmp_types[n[0]]
599
600 lineno = nl[1][2]
601 results.append((type, self.com_node(nodelist[i])))
602
603 # we need a special "compare" node so that we can distinguish
604 # 3 < x < 5 from (3 < x) < 5
605 # the two have very different semantics and results (note that the
606 # latter form is always true)
607
Jeremy Hylton566d9342004-09-07 15:28:01 +0000608 return Compare(node, results, lineno=lineno)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000609
610 def expr(self, nodelist):
611 # xor_expr ('|' xor_expr)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000612 return self.com_binary(Bitor, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000613
614 def xor_expr(self, nodelist):
615 # xor_expr ('^' xor_expr)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000616 return self.com_binary(Bitxor, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000617
618 def and_expr(self, nodelist):
619 # xor_expr ('&' xor_expr)*
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000620 return self.com_binary(Bitand, nodelist)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000621
622 def shift_expr(self, nodelist):
623 # shift_expr ('<<'|'>>' shift_expr)*
624 node = self.com_node(nodelist[0])
625 for i in range(2, len(nodelist), 2):
626 right = self.com_node(nodelist[i])
627 if nodelist[i-1][0] == token.LEFTSHIFT:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000628 node = LeftShift([node, right], lineno=nodelist[1][2])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000629 elif nodelist[i-1][0] == token.RIGHTSHIFT:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000630 node = RightShift([node, right], lineno=nodelist[1][2])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000631 else:
632 raise ValueError, "unexpected token: %s" % nodelist[i-1][0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000633 return node
634
635 def arith_expr(self, nodelist):
636 node = self.com_node(nodelist[0])
637 for i in range(2, len(nodelist), 2):
638 right = self.com_node(nodelist[i])
639 if nodelist[i-1][0] == token.PLUS:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000640 node = Add([node, right], lineno=nodelist[1][2])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000641 elif nodelist[i-1][0] == token.MINUS:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000642 node = Sub([node, right], lineno=nodelist[1][2])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000643 else:
644 raise ValueError, "unexpected token: %s" % nodelist[i-1][0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000645 return node
646
647 def term(self, nodelist):
648 node = self.com_node(nodelist[0])
649 for i in range(2, len(nodelist), 2):
650 right = self.com_node(nodelist[i])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000651 t = nodelist[i-1][0]
652 if t == token.STAR:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000653 node = Mul([node, right])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000654 elif t == token.SLASH:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000655 node = Div([node, right])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000656 elif t == token.PERCENT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000657 node = Mod([node, right])
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000658 elif t == token.DOUBLESLASH:
659 node = FloorDiv([node, right])
660 else:
661 raise ValueError, "unexpected token: %s" % t
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000662 node.lineno = nodelist[1][2]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000663 return node
664
665 def factor(self, nodelist):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000666 elt = nodelist[0]
667 t = elt[0]
Jeremy Hylton566d9342004-09-07 15:28:01 +0000668 node = self.lookup_node(nodelist[-1])(nodelist[-1][1:])
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000669 # need to handle (unary op)constant here...
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000670 if t == token.PLUS:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000671 return UnaryAdd(node, lineno=elt[2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000672 elif t == token.MINUS:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000673 return UnarySub(node, lineno=elt[2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000674 elif t == token.TILDE:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000675 node = Invert(node, lineno=elt[2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000676 return node
677
678 def power(self, nodelist):
679 # power: atom trailer* ('**' factor)*
680 node = self.com_node(nodelist[0])
681 for i in range(1, len(nodelist)):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000682 elt = nodelist[i]
683 if elt[0] == token.DOUBLESTAR:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000684 return Power([node, self.com_node(nodelist[i+1])],
685 lineno=elt[2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000686
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000687 node = self.com_apply_trailer(node, elt)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000688
689 return node
690
691 def atom(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000692 return self._atom_dispatch[nodelist[0][0]](nodelist)
Jeremy Hylton7845cf82001-08-29 20:56:30 +0000693 n.lineno = nodelist[0][2]
694 return n
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000695
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000696 def atom_lpar(self, nodelist):
697 if nodelist[1][0] == token.RPAR:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000698 return Tuple(())
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000699 return self.com_node(nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000700
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000701 def atom_lsqb(self, nodelist):
702 if nodelist[1][0] == token.RSQB:
Jeremy Hylton566d9342004-09-07 15:28:01 +0000703 return List(())
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000704 return self.com_list_constructor(nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000705
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000706 def atom_lbrace(self, nodelist):
707 if nodelist[1][0] == token.RBRACE:
708 return Dict(())
709 return self.com_dictmaker(nodelist[1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000710
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000711 def atom_backquote(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000712 return Backquote(self.com_node(nodelist[1]))
Tim Peterse0c446b2001-10-18 21:57:37 +0000713
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000714 def atom_number(self, nodelist):
715 ### need to verify this matches compile.c
716 k = eval(nodelist[0][1])
Jeremy Hylton566d9342004-09-07 15:28:01 +0000717 return Const(k, lineno=nodelist[0][2])
Tim Peterse0c446b2001-10-18 21:57:37 +0000718
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000719 def decode_literal(self, lit):
720 if self.encoding:
721 # this is particularly fragile & a bit of a
722 # hack... changes in compile.c:parsestr and
723 # tokenizer.c must be reflected here.
724 if self.encoding not in ['utf-8', 'iso-8859-1']:
725 lit = unicode(lit, 'utf-8').encode(self.encoding)
726 return eval("# coding: %s\n%s" % (self.encoding, lit))
727 else:
728 return eval(lit)
729
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000730 def atom_string(self, nodelist):
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000731 k = ''
732 for node in nodelist:
Jeremy Hyltonaccb62b2002-12-31 18:17:44 +0000733 k += self.decode_literal(node[1])
Jeremy Hylton566d9342004-09-07 15:28:01 +0000734 return Const(k, lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000735
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000736 def atom_name(self, nodelist):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000737 return Name(nodelist[0][1], lineno=nodelist[0][2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000738
739 # --------------------------------------------------------------
740 #
741 # INTERNAL PARSING UTILITIES
742 #
743
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +0000744 # The use of com_node() introduces a lot of extra stack frames,
745 # enough to cause a stack overflow compiling test.test_parser with
746 # the standard interpreter recursionlimit. The com_node() is a
747 # convenience function that hides the dispatch details, but comes
748 # at a very high cost. It is more efficient to dispatch directly
749 # in the callers. In these cases, use lookup_node() and call the
750 # dispatched node directly.
751
752 def lookup_node(self, node):
753 return self._dispatch[node[0]]
754
Jeremy Hylton566d9342004-09-07 15:28:01 +0000755 _callers = {}
756
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000757 def com_node(self, node):
758 # Note: compile.c has handling in com_node for del_stmt, pass_stmt,
759 # break_stmt, stmt, small_stmt, flow_stmt, simple_stmt,
760 # and compound_stmt.
761 # We'll just dispatch them.
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000762 return self._dispatch[node[0]](node[1:])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000763
Jeremy Hylton42a08302001-04-09 04:27:12 +0000764 def com_NEWLINE(self, *args):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000765 # A ';' at the end of a line can make a NEWLINE token appear
766 # here, Render it harmless. (genc discards ('discard',
767 # ('const', xxxx)) Nodes)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +0000768 return Discard(Const(None))
Tim Peterse0c446b2001-10-18 21:57:37 +0000769
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000770 def com_arglist(self, nodelist):
771 # varargslist:
Jeremy Hylton8d22b042002-04-19 22:56:37 +0000772 # (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
773 # | fpdef ['=' test] (',' fpdef ['=' test])* [',']
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000774 # fpdef: NAME | '(' fplist ')'
775 # fplist: fpdef (',' fpdef)* [',']
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000776 names = []
777 defaults = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000778 flags = 0
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000779
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000780 i = 0
781 while i < len(nodelist):
782 node = nodelist[i]
783 if node[0] == token.STAR or node[0] == token.DOUBLESTAR:
784 if node[0] == token.STAR:
785 node = nodelist[i+1]
786 if node[0] == token.NAME:
787 names.append(node[1])
788 flags = flags | CO_VARARGS
789 i = i + 3
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000790
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000791 if i < len(nodelist):
Jeremy Hylton8d22b042002-04-19 22:56:37 +0000792 # should be DOUBLESTAR
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000793 t = nodelist[i][0]
794 if t == token.DOUBLESTAR:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000795 node = nodelist[i+1]
Jeremy Hylton7abf5202001-08-29 18:12:30 +0000796 else:
797 raise ValueError, "unexpected token: %s" % t
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000798 names.append(node[1])
799 flags = flags | CO_VARKEYWORDS
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000800
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000801 break
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000802
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000803 # fpdef: NAME | '(' fplist ')'
804 names.append(self.com_fpdef(node))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000805
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000806 i = i + 1
807 if i >= len(nodelist):
808 break
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000809
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000810 if nodelist[i][0] == token.EQUAL:
811 defaults.append(self.com_node(nodelist[i + 1]))
812 i = i + 2
813 elif len(defaults):
Jeremy Hylton8f00a242004-09-07 15:36:48 +0000814 # XXX This should be a syntax error.
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000815 # Treat "(a=1, b)" as "(a=1, b=None)"
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000816 defaults.append(Const(None))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000817
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000818 i = i + 1
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000819
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000820 return names, defaults, flags
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000821
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000822 def com_fpdef(self, node):
823 # fpdef: NAME | '(' fplist ')'
824 if node[1][0] == token.LPAR:
825 return self.com_fplist(node[2])
826 return node[1][1]
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000827
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000828 def com_fplist(self, node):
Tim Peterse0c446b2001-10-18 21:57:37 +0000829 # fplist: fpdef (',' fpdef)* [',']
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000830 if len(node) == 2:
831 return self.com_fpdef(node[1])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000832 list = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000833 for i in range(1, len(node), 2):
834 list.append(self.com_fpdef(node[i]))
835 return tuple(list)
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000836
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000837 def com_dotted_name(self, node):
838 # String together the dotted names and return the string
839 name = ""
840 for n in node:
841 if type(n) == type(()) and n[0] == 1:
842 name = name + n[1] + '.'
843 return name[:-1]
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000844
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000845 def com_dotted_as_name(self, node):
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000846 assert node[0] == symbol.dotted_as_name
847 node = node[1:]
848 dot = self.com_dotted_name(node[0][1:])
849 if len(node) == 1:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000850 return dot, None
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000851 assert node[1][1] == 'as'
852 assert node[2][0] == token.NAME
853 return dot, node[2][1]
854
855 def com_dotted_as_names(self, node):
856 assert node[0] == symbol.dotted_as_names
857 node = node[1:]
858 names = [self.com_dotted_as_name(node[0])]
859 for i in range(2, len(node), 2):
860 names.append(self.com_dotted_as_name(node[i]))
861 return names
Jeremy Hylton20516082000-09-01 20:33:26 +0000862
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000863 def com_import_as_name(self, node):
Jeremy Hylton42a08302001-04-09 04:27:12 +0000864 assert node[0] == symbol.import_as_name
865 node = node[1:]
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000866 assert node[0][0] == token.NAME
Jeremy Hylton42a08302001-04-09 04:27:12 +0000867 if len(node) == 1:
Jeremy Hylton42a08302001-04-09 04:27:12 +0000868 return node[0][1], None
Jeremy Hylton42a08302001-04-09 04:27:12 +0000869 assert node[1][1] == 'as', node
870 assert node[2][0] == token.NAME
871 return node[0][1], node[2][1]
Jeremy Hylton20516082000-09-01 20:33:26 +0000872
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000873 def com_import_as_names(self, node):
874 assert node[0] == symbol.import_as_names
875 node = node[1:]
876 names = [self.com_import_as_name(node[0])]
877 for i in range(2, len(node), 2):
878 names.append(self.com_import_as_name(node[i]))
879 return names
880
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000881 def com_bases(self, node):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000882 bases = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000883 for i in range(1, len(node), 2):
884 bases.append(self.com_node(node[i]))
885 return bases
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000886
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000887 def com_try_finally(self, nodelist):
888 # try_fin_stmt: "try" ":" suite "finally" ":" suite
Jeremy Hylton566d9342004-09-07 15:28:01 +0000889 return TryFinally(self.com_node(nodelist[2]),
Tim Peters0e9980f2004-09-12 03:49:31 +0000890 self.com_node(nodelist[5]),
Jeremy Hylton566d9342004-09-07 15:28:01 +0000891 lineno=nodelist[0][2])
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000892
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000893 def com_try_except(self, nodelist):
894 # try_except: 'try' ':' suite (except_clause ':' suite)* ['else' suite]
895 #tryexcept: [TryNode, [except_clauses], elseNode)]
896 stmt = self.com_node(nodelist[2])
897 clauses = []
898 elseNode = None
899 for i in range(3, len(nodelist), 3):
900 node = nodelist[i]
901 if node[0] == symbol.except_clause:
902 # except_clause: 'except' [expr [',' expr]] */
903 if len(node) > 2:
904 expr1 = self.com_node(node[2])
905 if len(node) > 4:
906 expr2 = self.com_assign(node[4], OP_ASSIGN)
907 else:
908 expr2 = None
909 else:
910 expr1 = expr2 = None
911 clauses.append((expr1, expr2, self.com_node(nodelist[i+2])))
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000912
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000913 if node[0] == token.NAME:
914 elseNode = self.com_node(nodelist[i+2])
Jeremy Hylton566d9342004-09-07 15:28:01 +0000915 return TryExcept(self.com_node(nodelist[2]), clauses, elseNode,
916 lineno=nodelist[0][2])
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000917
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000918 def com_augassign_op(self, node):
919 assert node[0] == symbol.augassign
920 return node[1]
921
922 def com_augassign(self, node):
923 """Return node suitable for lvalue of augmented assignment
924
925 Names, slices, and attributes are the only allowable nodes.
926 """
927 l = self.com_node(node)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000928 if l.__class__ in (Name, Slice, Subscript, Getattr):
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000929 return l
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000930 raise SyntaxError, "can't assign to %s" % l.__class__.__name__
Jeremy Hylton9c048f92000-10-13 21:58:13 +0000931
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000932 def com_assign(self, node, assigning):
933 # return a node suitable for use as an "lvalue"
934 # loop to avoid trivial recursion
935 while 1:
936 t = node[0]
Michael W. Hudson3f061892004-07-12 13:15:56 +0000937 if t == symbol.exprlist or t == symbol.testlist or t == symbol.testlist_gexp:
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000938 if len(node) > 2:
939 return self.com_assign_tuple(node, assigning)
940 node = node[1]
941 elif t in _assign_types:
942 if len(node) > 2:
943 raise SyntaxError, "can't assign to operator"
944 node = node[1]
945 elif t == symbol.power:
946 if node[1][0] != symbol.atom:
947 raise SyntaxError, "can't assign to operator"
948 if len(node) > 2:
949 primary = self.com_node(node[1])
950 for i in range(2, len(node)-1):
951 ch = node[i]
952 if ch[0] == token.DOUBLESTAR:
953 raise SyntaxError, "can't assign to operator"
954 primary = self.com_apply_trailer(primary, ch)
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000955 return self.com_assign_trailer(primary, node[-1],
Tim Peterse0c446b2001-10-18 21:57:37 +0000956 assigning)
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000957 node = node[1]
958 elif t == symbol.atom:
959 t = node[1][0]
960 if t == token.LPAR:
961 node = node[2]
962 if node[0] == token.RPAR:
963 raise SyntaxError, "can't assign to ()"
964 elif t == token.LSQB:
965 node = node[2]
966 if node[0] == token.RSQB:
967 raise SyntaxError, "can't assign to []"
968 return self.com_assign_list(node, assigning)
969 elif t == token.NAME:
970 return self.com_assign_name(node[1], assigning)
971 else:
972 raise SyntaxError, "can't assign to literal"
973 else:
974 raise SyntaxError, "bad assignment"
975
976 def com_assign_tuple(self, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000977 assigns = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000978 for i in range(1, len(node), 2):
979 assigns.append(self.com_assign(node[i], assigning))
Jeremy Hylton566d9342004-09-07 15:28:01 +0000980 return AssTuple(assigns, lineno=extractLineNo(node))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000981
982 def com_assign_list(self, node, assigning):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +0000983 assigns = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000984 for i in range(1, len(node), 2):
Jeremy Hylton2e4cc7e2001-09-17 19:33:48 +0000985 if i + 1 < len(node):
986 if node[i + 1][0] == symbol.list_for:
987 raise SyntaxError, "can't assign to list comprehension"
988 assert node[i + 1][0] == token.COMMA, node[i + 1]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000989 assigns.append(self.com_assign(node[i], assigning))
Jeremy Hylton566d9342004-09-07 15:28:01 +0000990 return AssList(assigns, lineno=extractLineNo(node))
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000991
992 def com_assign_name(self, node, assigning):
Jeremy Hylton566d9342004-09-07 15:28:01 +0000993 return AssName(node[1], assigning, lineno=node[2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000994
995 def com_assign_trailer(self, primary, node, assigning):
Jeremy Hyltonf968e852000-02-04 00:25:23 +0000996 t = node[1][0]
Jeremy Hylton572bdce2000-09-20 02:47:28 +0000997 if t == token.DOT:
998 return self.com_assign_attr(primary, node[2], assigning)
999 if t == token.LSQB:
1000 return self.com_subscriptlist(primary, node[2], assigning)
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001001 if t == token.LPAR:
1002 raise SyntaxError, "can't assign to function call"
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001003 raise SyntaxError, "unknown trailer type: %s" % t
1004
1005 def com_assign_attr(self, primary, node, assigning):
Jeremy Hylton566d9342004-09-07 15:28:01 +00001006 return AssAttr(primary, node[1], assigning, lineno=node[-1])
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001007
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001008 def com_binary(self, constructor, nodelist):
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001009 "Compile 'NODE (OP NODE)*' into (type, [ node1, ..., nodeN ])."
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001010 l = len(nodelist)
1011 if l == 1:
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001012 n = nodelist[0]
1013 return self.lookup_node(n)(n[1:])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001014 items = []
1015 for i in range(0, l, 2):
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001016 n = nodelist[i]
1017 items.append(self.lookup_node(n)(n[1:]))
Jeremy Hylton566d9342004-09-07 15:28:01 +00001018 return constructor(items, lineno=extractLineNo(nodelist))
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001019
1020 def com_stmt(self, node):
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001021 result = self.lookup_node(node)(node[1:])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001022 assert result is not None
1023 if isinstance(result, Stmt):
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001024 return result
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001025 return Stmt([result])
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001026
1027 def com_append_stmt(self, stmts, node):
Jeremy Hylton566d9342004-09-07 15:28:01 +00001028 result = self.lookup_node(node)(node[1:])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001029 assert result is not None
1030 if isinstance(result, Stmt):
1031 stmts.extend(result.nodes)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001032 else:
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001033 stmts.append(result)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001034
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001035 if hasattr(symbol, 'list_for'):
1036 def com_list_constructor(self, nodelist):
1037 # listmaker: test ( list_for | (',' test)* [','] )
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001038 values = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001039 for i in range(1, len(nodelist)):
1040 if nodelist[i][0] == symbol.list_for:
1041 assert len(nodelist[i:]) == 1
1042 return self.com_list_comprehension(values[0],
1043 nodelist[i])
1044 elif nodelist[i][0] == token.COMMA:
1045 continue
1046 values.append(self.com_node(nodelist[i]))
Jeremy Hylton566d9342004-09-07 15:28:01 +00001047 return List(values, lineno=values[0].lineno)
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001048
1049 def com_list_comprehension(self, expr, node):
1050 # list_iter: list_for | list_if
1051 # list_for: 'for' exprlist 'in' testlist [list_iter]
1052 # list_if: 'if' test [list_iter]
Jeremy Hylton42a08302001-04-09 04:27:12 +00001053
1054 # XXX should raise SyntaxError for assignment
Tim Peterse0c446b2001-10-18 21:57:37 +00001055
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001056 lineno = node[1][2]
1057 fors = []
1058 while node:
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001059 t = node[1][1]
1060 if t == 'for':
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001061 assignNode = self.com_assign(node[2], OP_ASSIGN)
1062 listNode = self.com_node(node[4])
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001063 newfor = ListCompFor(assignNode, listNode, [])
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001064 newfor.lineno = node[1][2]
1065 fors.append(newfor)
1066 if len(node) == 5:
1067 node = None
1068 else:
1069 node = self.com_list_iter(node[5])
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001070 elif t == 'if':
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001071 test = self.com_node(node[2])
Jeremy Hylton566d9342004-09-07 15:28:01 +00001072 newif = ListCompIf(test, lineno=node[1][2])
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001073 newfor.ifs.append(newif)
1074 if len(node) == 3:
1075 node = None
1076 else:
1077 node = self.com_list_iter(node[3])
1078 else:
1079 raise SyntaxError, \
1080 ("unexpected list comprehension element: %s %d"
1081 % (node, lineno))
Jeremy Hylton566d9342004-09-07 15:28:01 +00001082 return ListComp(expr, fors, lineno=lineno)
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001083
1084 def com_list_iter(self, node):
1085 assert node[0] == symbol.list_iter
1086 return node[1]
1087 else:
1088 def com_list_constructor(self, nodelist):
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001089 values = []
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001090 for i in range(1, len(nodelist), 2):
1091 values.append(self.com_node(nodelist[i]))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001092 return List(values)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001093
Raymond Hettinger354433a2004-05-19 08:20:33 +00001094 if hasattr(symbol, 'gen_for'):
1095 def com_generator_expression(self, expr, node):
1096 # gen_iter: gen_for | gen_if
1097 # gen_for: 'for' exprlist 'in' test [gen_iter]
1098 # gen_if: 'if' test [gen_iter]
1099
1100 lineno = node[1][2]
1101 fors = []
1102 while node:
1103 t = node[1][1]
1104 if t == 'for':
1105 assignNode = self.com_assign(node[2], OP_ASSIGN)
1106 genNode = self.com_node(node[4])
Jeremy Hylton566d9342004-09-07 15:28:01 +00001107 newfor = GenExprFor(assignNode, genNode, [],
1108 lineno=node[1][2])
Raymond Hettinger354433a2004-05-19 08:20:33 +00001109 fors.append(newfor)
1110 if (len(node)) == 5:
1111 node = None
1112 else:
1113 node = self.com_gen_iter(node[5])
1114 elif t == 'if':
1115 test = self.com_node(node[2])
Jeremy Hylton566d9342004-09-07 15:28:01 +00001116 newif = GenExprIf(test, lineno=node[1][2])
Raymond Hettinger354433a2004-05-19 08:20:33 +00001117 newfor.ifs.append(newif)
1118 if len(node) == 3:
1119 node = None
1120 else:
1121 node = self.com_gen_iter(node[3])
1122 else:
1123 raise SyntaxError, \
1124 ("unexpected generator expression element: %s %d"
1125 % (node, lineno))
1126 fors[0].is_outmost = True
Jeremy Hylton566d9342004-09-07 15:28:01 +00001127 return GenExpr(GenExprInner(expr, fors), lineno=lineno)
Raymond Hettinger354433a2004-05-19 08:20:33 +00001128
1129 def com_gen_iter(self, node):
1130 assert node[0] == symbol.gen_iter
1131 return node[1]
1132
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001133 def com_dictmaker(self, nodelist):
1134 # dictmaker: test ':' test (',' test ':' value)* [',']
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001135 items = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001136 for i in range(1, len(nodelist), 4):
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001137 items.append((self.com_node(nodelist[i]),
1138 self.com_node(nodelist[i+2])))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001139 return Dict(items)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001140
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001141 def com_apply_trailer(self, primaryNode, nodelist):
1142 t = nodelist[1][0]
1143 if t == token.LPAR:
1144 return self.com_call_function(primaryNode, nodelist[2])
1145 if t == token.DOT:
1146 return self.com_select_member(primaryNode, nodelist[2])
1147 if t == token.LSQB:
1148 return self.com_subscriptlist(primaryNode, nodelist[2], OP_APPLY)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001149
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001150 raise SyntaxError, 'unknown node type: %s' % t
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001151
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001152 def com_select_member(self, primaryNode, nodelist):
1153 if nodelist[0] != token.NAME:
1154 raise SyntaxError, "member must be a name"
Jeremy Hylton566d9342004-09-07 15:28:01 +00001155 return Getattr(primaryNode, nodelist[1], lineno=nodelist[2])
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001156
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001157 def com_call_function(self, primaryNode, nodelist):
1158 if nodelist[0] == token.RPAR:
Jeremy Hylton566d9342004-09-07 15:28:01 +00001159 return CallFunc(primaryNode, [], lineno=extractLineNo(nodelist))
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001160 args = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001161 kw = 0
1162 len_nodelist = len(nodelist)
1163 for i in range(1, len_nodelist, 2):
1164 node = nodelist[i]
1165 if node[0] == token.STAR or node[0] == token.DOUBLESTAR:
Tim Peterse0c446b2001-10-18 21:57:37 +00001166 break
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001167 kw, result = self.com_argument(node, kw)
Michael W. Hudsonc054a8b2004-10-11 15:35:53 +00001168
1169 if len_nodelist != 2 and isinstance(result, GenExpr) \
1170 and len(node) == 3 and node[2][0] == symbol.gen_for:
1171 # allow f(x for x in y), but reject f(x for x in y, 1)
1172 # should use f((x for x in y), 1) instead of f(x for x in y, 1)
Raymond Hettinger354433a2004-05-19 08:20:33 +00001173 raise SyntaxError, 'generator expression needs parenthesis'
Michael W. Hudsonc054a8b2004-10-11 15:35:53 +00001174
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001175 args.append(result)
Jeremy Hyltonbe317e62000-05-02 22:32:59 +00001176 else:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001177 # No broken by star arg, so skip the last one we processed.
1178 i = i + 1
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001179 if i < len_nodelist and nodelist[i][0] == token.COMMA:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001180 # need to accept an application that looks like "f(a, b,)"
1181 i = i + 1
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001182 star_node = dstar_node = None
1183 while i < len_nodelist:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001184 tok = nodelist[i]
1185 ch = nodelist[i+1]
1186 i = i + 3
1187 if tok[0]==token.STAR:
1188 if star_node is not None:
1189 raise SyntaxError, 'already have the varargs indentifier'
1190 star_node = self.com_node(ch)
1191 elif tok[0]==token.DOUBLESTAR:
1192 if dstar_node is not None:
1193 raise SyntaxError, 'already have the kwargs indentifier'
1194 dstar_node = self.com_node(ch)
1195 else:
1196 raise SyntaxError, 'unknown node type: %s' % tok
Jeremy Hylton566d9342004-09-07 15:28:01 +00001197 return CallFunc(primaryNode, args, star_node, dstar_node,
1198 lineno=extractLineNo(nodelist))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001199
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001200 def com_argument(self, nodelist, kw):
Raymond Hettinger354433a2004-05-19 08:20:33 +00001201 if len(nodelist) == 3 and nodelist[2][0] == symbol.gen_for:
1202 test = self.com_node(nodelist[1])
1203 return 0, self.com_generator_expression(test, nodelist[2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001204 if len(nodelist) == 2:
1205 if kw:
1206 raise SyntaxError, "non-keyword arg after keyword arg"
1207 return 0, self.com_node(nodelist[1])
1208 result = self.com_node(nodelist[3])
1209 n = nodelist[1]
1210 while len(n) == 2 and n[0] != token.NAME:
1211 n = n[1]
1212 if n[0] != token.NAME:
1213 raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
Jeremy Hylton566d9342004-09-07 15:28:01 +00001214 node = Keyword(n[1], result, lineno=n[2])
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001215 return 1, node
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001216
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001217 def com_subscriptlist(self, primary, nodelist, assigning):
1218 # slicing: simple_slicing | extended_slicing
1219 # simple_slicing: primary "[" short_slice "]"
Tim Peterse0c446b2001-10-18 21:57:37 +00001220 # extended_slicing: primary "[" slice_list "]"
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001221 # slice_list: slice_item ("," slice_item)* [","]
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001222
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001223 # backwards compat slice for '[i:j]'
1224 if len(nodelist) == 2:
1225 sub = nodelist[1]
1226 if (sub[1][0] == token.COLON or \
1227 (len(sub) > 2 and sub[2][0] == token.COLON)) and \
1228 sub[-1][0] != symbol.sliceop:
1229 return self.com_slice(primary, sub, assigning)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001230
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001231 subscripts = []
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001232 for i in range(1, len(nodelist), 2):
1233 subscripts.append(self.com_subscript(nodelist[i]))
Tim Peters0e9980f2004-09-12 03:49:31 +00001234 return Subscript(primary, assigning, subscripts,
Jeremy Hylton566d9342004-09-07 15:28:01 +00001235 lineno=extractLineNo(nodelist))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001236
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001237 def com_subscript(self, node):
1238 # slice_item: expression | proper_slice | ellipsis
1239 ch = node[1]
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001240 t = ch[0]
1241 if t == token.DOT and node[2][0] == token.DOT:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001242 return Ellipsis()
Jeremy Hylton4ebf3be2000-11-06 16:03:52 +00001243 if t == token.COLON or len(node) > 2:
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001244 return self.com_sliceobj(node)
1245 return self.com_node(ch)
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001246
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001247 def com_sliceobj(self, node):
1248 # proper_slice: short_slice | long_slice
1249 # short_slice: [lower_bound] ":" [upper_bound]
1250 # long_slice: short_slice ":" [stride]
1251 # lower_bound: expression
1252 # upper_bound: expression
1253 # stride: expression
1254 #
1255 # Note: a stride may be further slicing...
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001256
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001257 items = []
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001258
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001259 if node[1][0] == token.COLON:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001260 items.append(Const(None))
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001261 i = 2
1262 else:
1263 items.append(self.com_node(node[1]))
1264 # i == 2 is a COLON
1265 i = 3
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001266
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001267 if i < len(node) and node[i][0] == symbol.test:
1268 items.append(self.com_node(node[i]))
1269 i = i + 1
1270 else:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001271 items.append(Const(None))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001272
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001273 # a short_slice has been built. look for long_slice now by looking
1274 # for strides...
1275 for j in range(i, len(node)):
1276 ch = node[j]
1277 if len(ch) == 2:
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001278 items.append(Const(None))
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001279 else:
1280 items.append(self.com_node(ch[2]))
Jeremy Hylton566d9342004-09-07 15:28:01 +00001281 return Sliceobj(items, lineno=extractLineNo(node))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001282
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001283 def com_slice(self, primary, node, assigning):
1284 # short_slice: [lower_bound] ":" [upper_bound]
1285 lower = upper = None
1286 if len(node) == 3:
1287 if node[1][0] == token.COLON:
1288 upper = self.com_node(node[2])
1289 else:
1290 lower = self.com_node(node[1])
1291 elif len(node) == 4:
1292 lower = self.com_node(node[1])
1293 upper = self.com_node(node[3])
Jeremy Hylton566d9342004-09-07 15:28:01 +00001294 return Slice(primary, assigning, lower, upper,
1295 lineno=extractLineNo(node))
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001296
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001297 def get_docstring(self, node, n=None):
1298 if n is None:
1299 n = node[0]
1300 node = node[1:]
1301 if n == symbol.suite:
1302 if len(node) == 1:
1303 return self.get_docstring(node[0])
1304 for sub in node:
1305 if sub[0] == symbol.stmt:
1306 return self.get_docstring(sub)
1307 return None
1308 if n == symbol.file_input:
1309 for sub in node:
1310 if sub[0] == symbol.stmt:
1311 return self.get_docstring(sub)
1312 return None
1313 if n == symbol.atom:
1314 if node[0][0] == token.STRING:
1315 s = ''
1316 for t in node:
1317 s = s + eval(t[1])
1318 return s
1319 return None
Jeremy Hylton7cff7fe2000-10-25 18:10:32 +00001320 if n == symbol.stmt or n == symbol.simple_stmt \
1321 or n == symbol.small_stmt:
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001322 return self.get_docstring(node[0])
1323 if n in _doc_nodes and len(node) == 1:
1324 return self.get_docstring(node[0])
1325 return None
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001326
1327
1328_doc_nodes = [
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001329 symbol.expr_stmt,
1330 symbol.testlist,
Jeremy Hyltond1142612001-10-17 13:32:52 +00001331 symbol.testlist_safe,
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001332 symbol.test,
1333 symbol.and_test,
1334 symbol.not_test,
1335 symbol.comparison,
1336 symbol.expr,
1337 symbol.xor_expr,
1338 symbol.and_expr,
1339 symbol.shift_expr,
1340 symbol.arith_expr,
1341 symbol.term,
1342 symbol.factor,
1343 symbol.power,
1344 ]
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001345
1346# comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '=='
1347# | 'in' | 'not' 'in' | 'is' | 'is' 'not'
1348_cmp_types = {
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001349 token.LESS : '<',
1350 token.GREATER : '>',
1351 token.EQEQUAL : '==',
1352 token.EQUAL : '==',
1353 token.LESSEQUAL : '<=',
1354 token.GREATEREQUAL : '>=',
1355 token.NOTEQUAL : '!=',
1356 }
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001357
1358_legal_node_types = [
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001359 symbol.funcdef,
1360 symbol.classdef,
1361 symbol.stmt,
1362 symbol.small_stmt,
1363 symbol.flow_stmt,
1364 symbol.simple_stmt,
1365 symbol.compound_stmt,
1366 symbol.expr_stmt,
1367 symbol.print_stmt,
1368 symbol.del_stmt,
1369 symbol.pass_stmt,
1370 symbol.break_stmt,
1371 symbol.continue_stmt,
1372 symbol.return_stmt,
1373 symbol.raise_stmt,
1374 symbol.import_stmt,
1375 symbol.global_stmt,
1376 symbol.exec_stmt,
1377 symbol.assert_stmt,
1378 symbol.if_stmt,
1379 symbol.while_stmt,
1380 symbol.for_stmt,
1381 symbol.try_stmt,
1382 symbol.suite,
1383 symbol.testlist,
Jeremy Hyltond1142612001-10-17 13:32:52 +00001384 symbol.testlist_safe,
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001385 symbol.test,
1386 symbol.and_test,
1387 symbol.not_test,
1388 symbol.comparison,
1389 symbol.exprlist,
1390 symbol.expr,
1391 symbol.xor_expr,
1392 symbol.and_expr,
1393 symbol.shift_expr,
1394 symbol.arith_expr,
1395 symbol.term,
1396 symbol.factor,
1397 symbol.power,
1398 symbol.atom,
1399 ]
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001400
Jeremy Hylton7e30c9b2001-08-27 21:02:51 +00001401if hasattr(symbol, 'yield_stmt'):
1402 _legal_node_types.append(symbol.yield_stmt)
1403
Jeremy Hyltonf968e852000-02-04 00:25:23 +00001404_assign_types = [
Jeremy Hylton572bdce2000-09-20 02:47:28 +00001405 symbol.test,
1406 symbol.and_test,
1407 symbol.not_test,
1408 symbol.comparison,
1409 symbol.expr,
1410 symbol.xor_expr,
1411 symbol.and_expr,
1412 symbol.shift_expr,
1413 symbol.arith_expr,
1414 symbol.term,
1415 symbol.factor,
1416 ]
Jeremy Hylton9c048f92000-10-13 21:58:13 +00001417
1418import types
1419_names = {}
1420for k, v in symbol.sym_name.items():
1421 _names[k] = v
1422for k, v in token.tok_name.items():
1423 _names[k] = v
1424
1425def debug_tree(tree):
1426 l = []
1427 for elt in tree:
1428 if type(elt) == types.IntType:
1429 l.append(_names.get(elt, elt))
1430 elif type(elt) == types.StringType:
1431 l.append(elt)
1432 else:
1433 l.append(debug_tree(elt))
1434 return l