Jeremy Hylton | fa974a9 | 2000-03-06 18:50:48 +0000 | [diff] [blame] | 1 | """Parse tree transformation module. |
| 2 | |
| 3 | Transforms Python source code into an abstract syntax tree (AST) |
| 4 | defined in the ast module. |
| 5 | |
| 6 | The simplest ways to invoke this module are via parse and parseFile. |
| 7 | parse(buf) -> AST |
| 8 | parseFile(path) -> AST |
| 9 | """ |
| 10 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 11 | # Original version written by Greg Stein (gstein@lyra.org) |
| 12 | # and Bill Tutt (rassilon@lima.mudlib.org) |
| 13 | # February 1997. |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 14 | # |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 15 | # 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 25 | from ast import * |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 26 | import parser |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 27 | # 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 29 | import symbol |
| 30 | import token |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 31 | import sys |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 32 | |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 33 | error = 'walker.error' |
| 34 | |
Jeremy Hylton | 9605c11 | 2000-02-08 18:57:32 +0000 | [diff] [blame] | 35 | from consts import CO_VARARGS, CO_VARKEYWORDS |
| 36 | from consts import OP_ASSIGN, OP_DELETE, OP_APPLY |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 37 | |
Jeremy Hylton | fa974a9 | 2000-03-06 18:50:48 +0000 | [diff] [blame] | 38 | def parseFile(path): |
| 39 | f = open(path) |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 40 | # 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 Hylton | fa974a9 | 2000-03-06 18:50:48 +0000 | [diff] [blame] | 45 | f.close() |
| 46 | return parse(src) |
| 47 | |
Jeremy Hylton | 9dca364 | 2001-09-17 21:02:51 +0000 | [diff] [blame] | 48 | def 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 Hylton | fa974a9 | 2000-03-06 18:50:48 +0000 | [diff] [blame] | 56 | |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 57 | def asList(nodes): |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 58 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 70 | |
| 71 | def Node(*args): |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 72 | kind = args[0] |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 73 | if nodes.has_key(kind): |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 74 | try: |
Raymond Hettinger | ff41c48 | 2003-04-06 09:01:11 +0000 | [diff] [blame] | 75 | return nodes[kind](*args[1:]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 76 | except TypeError: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 77 | print nodes[kind], len(args), args |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 78 | raise |
| 79 | else: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 80 | raise error, "Can't find appropriate Node type: %s" % str(args) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 81 | #return apply(ast.Node, args) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 82 | |
| 83 | class Transformer: |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 84 | """Utility object for transforming Python parse trees. |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 85 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 86 | Exposes the following methods: |
| 87 | tree = transform(ast_tree) |
| 88 | tree = parsesuite(text) |
| 89 | tree = parseexpr(text) |
| 90 | tree = parsefile(fileob | filename) |
| 91 | """ |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 92 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 93 | def __init__(self): |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 94 | self._dispatch = {} |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 95 | for value, name in symbol.sym_name.items(): |
| 96 | if hasattr(self, name): |
| 97 | self._dispatch[value] = getattr(self, name) |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 98 | 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 Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 107 | self.encoding = None |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 108 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 109 | def transform(self, tree): |
| 110 | """Transform an AST into a modified parse tree.""" |
| 111 | if type(tree) != type(()) and type(tree) != type([]): |
Jeremy Hylton | 7845cf8 | 2001-08-29 20:56:30 +0000 | [diff] [blame] | 112 | tree = parser.ast2tuple(tree, line_info=1) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 113 | return self.compile_node(tree) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 114 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 115 | 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 Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 118 | # this can go now we have universal newlines? |
Neal Norwitz | a312c3a | 2002-06-06 18:30:10 +0000 | [diff] [blame] | 119 | text = text.replace('\x0d', '') |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 120 | return self.transform(parser.suite(text)) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 121 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 122 | def parseexpr(self, text): |
| 123 | """Return a modified parse tree for the given expression text.""" |
| 124 | return self.transform(parser.expr(text)) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 125 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 126 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 131 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 132 | # -------------------------------------------------------------- |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 133 | # |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 134 | # PRIVATE METHODS |
| 135 | # |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 136 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 137 | def compile_node(self, node): |
| 138 | ### emit a line-number node? |
| 139 | n = node[0] |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 140 | |
| 141 | if n == symbol.encoding_decl: |
| 142 | self.encoding = node[2] |
| 143 | node = node[1] |
| 144 | n = node[0] |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 145 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 146 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 169 | return Pass() |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 170 | |
| 171 | def file_input(self, nodelist): |
| 172 | doc = self.get_docstring(nodelist, symbol.file_input) |
Jeremy Hylton | ec92734 | 2001-08-18 00:04:31 +0000 | [diff] [blame] | 173 | if doc is not None: |
| 174 | i = 1 |
| 175 | else: |
| 176 | i = 0 |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 177 | stmts = [] |
Jeremy Hylton | ec92734 | 2001-08-18 00:04:31 +0000 | [diff] [blame] | 178 | for node in nodelist[i:]: |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 179 | if node[0] != token.ENDMARKER and node[0] != token.NEWLINE: |
| 180 | self.com_append_stmt(stmts, node) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 181 | return Module(doc, Stmt(stmts)) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 182 | |
| 183 | def eval_input(self, nodelist): |
| 184 | # from the built-in function input() |
| 185 | ### is this sufficient? |
Barry Warsaw | 52acb49 | 2001-12-21 20:04:22 +0000 | [diff] [blame] | 186 | return Expression(self.com_node(nodelist[0])) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 187 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 188 | 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 Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 201 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 202 | def decorator(self, nodelist): |
| 203 | # '@' dotted_name [ '(' [arglist] ')' ] |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 204 | assert len(nodelist) in (3, 5, 6) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 205 | assert nodelist[0][0] == token.AT |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 206 | assert nodelist[-1][0] == token.NEWLINE |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 207 | |
| 208 | assert nodelist[1][0] == symbol.dotted_name |
| 209 | funcname = self.decorator_name(nodelist[1][1:]) |
| 210 | |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 211 | if len(nodelist) > 3: |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 212 | assert nodelist[2][0] == token.LPAR |
| 213 | expr = self.com_call_function(funcname, nodelist[3]) |
| 214 | else: |
| 215 | expr = funcname |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 216 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 217 | return expr |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 218 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 219 | def decorators(self, nodelist): |
| 220 | # decorators: decorator ([NEWLINE] decorator)* NEWLINE |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 221 | items = [] |
Michael W. Hudson | 0ccff07 | 2004-08-17 17:29:16 +0000 | [diff] [blame] | 222 | for dec_nodelist in nodelist: |
| 223 | assert dec_nodelist[0] == symbol.decorator |
| 224 | items.append(self.decorator(dec_nodelist[1:])) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 225 | return Decorators(items) |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 226 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 227 | def funcdef(self, nodelist): |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 228 | # -6 -5 -4 -3 -2 -1 |
| 229 | # funcdef: [decorators] 'def' NAME parameters ':' suite |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 230 | # parameters: '(' [varargslist] ')' |
| 231 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 232 | 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 Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 238 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 239 | lineno = nodelist[-4][2] |
| 240 | name = nodelist[-4][1] |
| 241 | args = nodelist[-3][2] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 242 | |
| 243 | if args[0] == symbol.varargslist: |
| 244 | names, defaults, flags = self.com_arglist(args[1:]) |
| 245 | else: |
| 246 | names = defaults = () |
| 247 | flags = 0 |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 248 | doc = self.get_docstring(nodelist[-1]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 249 | |
| 250 | # code for function |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 251 | code = self.com_node(nodelist[-1]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 252 | |
Jeremy Hylton | a59ac0a | 2000-11-06 03:33:52 +0000 | [diff] [blame] | 253 | if doc is not None: |
| 254 | assert isinstance(code, Stmt) |
| 255 | assert isinstance(code.nodes[0], Discard) |
| 256 | del code.nodes[0] |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 257 | n = Function(decorators, name, names, defaults, flags, doc, code) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 258 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 272 | n = Lambda(names, defaults, flags, code) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 273 | 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 Hylton | da8db8c | 2001-08-29 17:19:02 +0000 | [diff] [blame] | 289 | if doc is not None: |
| 290 | assert isinstance(code, Stmt) |
| 291 | assert isinstance(code.nodes[0], Discard) |
| 292 | del code.nodes[0] |
| 293 | |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 294 | n = Class(name, bases, doc, code) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 295 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 307 | stmts = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 308 | for i in range(0, len(nodelist), 2): |
| 309 | self.com_append_stmt(stmts, nodelist[i]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 310 | return Stmt(stmts) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 311 | |
| 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 Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 345 | # augassign testlist | testlist ('=' testlist)* |
Jeremy Hylton | 7e30c9b | 2001-08-27 21:02:51 +0000 | [diff] [blame] | 346 | en = nodelist[-1] |
| 347 | exprNode = self.lookup_node(en)(en[1:]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 348 | if len(nodelist) == 1: |
Jeremy Hylton | 4c1f427 | 2001-04-11 16:22:26 +0000 | [diff] [blame] | 349 | n = Discard(exprNode) |
| 350 | n.lineno = exprNode.lineno |
| 351 | return n |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 352 | if nodelist[1][0] == token.EQUAL: |
Martin v. Löwis | 51e234a | 2002-02-28 17:48:48 +0000 | [diff] [blame] | 353 | nodesl = [] |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 354 | for i in range(0, len(nodelist) - 2, 2): |
Martin v. Löwis | 51e234a | 2002-02-28 17:48:48 +0000 | [diff] [blame] | 355 | nodesl.append(self.com_assign(nodelist[i], OP_ASSIGN)) |
| 356 | n = Assign(nodesl, exprNode) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 357 | n.lineno = nodelist[1][2] |
| 358 | else: |
| 359 | lval = self.com_augassign(nodelist[0]) |
| 360 | op = self.com_augassign_op(nodelist[1]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 361 | n = AugAssign(lval, op[1], exprNode) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 362 | n.lineno = op[2] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 363 | return n |
| 364 | |
| 365 | def print_stmt(self, nodelist): |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 366 | # print ([ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 367 | items = [] |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 368 | 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 Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 380 | items.append(self.com_node(nodelist[i])) |
| 381 | if nodelist[-1][0] == token.COMMA: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 382 | n = Print(items, dest) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 383 | n.lineno = nodelist[0][2] |
| 384 | return n |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 385 | n = Printnl(items, dest) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 386 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 393 | n = Pass() |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 394 | n.lineno = nodelist[0][2] |
| 395 | return n |
| 396 | |
| 397 | def break_stmt(self, nodelist): |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 398 | n = Break() |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 399 | n.lineno = nodelist[0][2] |
| 400 | return n |
| 401 | |
| 402 | def continue_stmt(self, nodelist): |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 403 | n = Continue() |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 404 | n.lineno = nodelist[0][2] |
| 405 | return n |
| 406 | |
| 407 | def return_stmt(self, nodelist): |
| 408 | # return: [testlist] |
| 409 | if len(nodelist) < 2: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 410 | n = Return(Const(None)) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 411 | n.lineno = nodelist[0][2] |
| 412 | return n |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 413 | n = Return(self.com_node(nodelist[1])) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 414 | n.lineno = nodelist[0][2] |
| 415 | return n |
| 416 | |
Jeremy Hylton | ec92734 | 2001-08-18 00:04:31 +0000 | [diff] [blame] | 417 | def yield_stmt(self, nodelist): |
| 418 | n = Yield(self.com_node(nodelist[1])) |
| 419 | n.lineno = nodelist[0][2] |
| 420 | return n |
| 421 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 422 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 436 | n = Raise(expr1, expr2, expr3) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 437 | 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 Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 444 | if nodelist[0][1] == 'from': |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 445 | 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 Hylton | 42a0830 | 2001-04-09 04:27:12 +0000 | [diff] [blame] | 451 | names.append(self.com_import_as_name(nodelist[i])) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 452 | n = From(self.com_dotted_name(nodelist[1]), names) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 453 | n.lineno = nodelist[0][2] |
| 454 | return n |
| 455 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 456 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 462 | n = Import(names) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 463 | n.lineno = nodelist[0][2] |
| 464 | return n |
| 465 | |
| 466 | def global_stmt(self, nodelist): |
| 467 | # global: NAME (',' NAME)* |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 468 | names = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 469 | for i in range(1, len(nodelist), 2): |
| 470 | names.append(nodelist[i][1]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 471 | n = Global(names) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 472 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 487 | n = Exec(expr1, expr2, expr3) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 488 | 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 Hylton | a59ac0a | 2000-11-06 03:33:52 +0000 | [diff] [blame] | 497 | expr2 = None |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 498 | n = Assert(expr1, expr2) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 499 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 504 | tests = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 505 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 515 | n = If(tests, elseNode) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 516 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 530 | n = While(testNode, bodyNode, elseNode) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 531 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 546 | n = For(assignNode, listNode, bodyNode, elseNode) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 547 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 563 | stmts = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 564 | for node in nodelist: |
| 565 | if node[0] == symbol.stmt: |
| 566 | self.com_append_stmt(stmts, node) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 567 | return Stmt(stmts) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 568 | |
| 569 | # -------------------------------------------------------------- |
| 570 | # |
| 571 | # EXPRESSION NODES (invoked by com_node()) |
| 572 | # |
| 573 | |
| 574 | def testlist(self, nodelist): |
| 575 | # testlist: expr (',' expr)* [','] |
Jeremy Hylton | d114261 | 2001-10-17 13:32:52 +0000 | [diff] [blame] | 576 | # testlist_safe: test [(',' test)+ [',']] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 577 | # exprlist: expr (',' expr)* [','] |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 578 | return self.com_binary(Tuple, nodelist) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 579 | |
Jeremy Hylton | d114261 | 2001-10-17 13:32:52 +0000 | [diff] [blame] | 580 | testlist_safe = testlist # XXX |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 581 | testlist1 = testlist |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 582 | exprlist = testlist |
| 583 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 584 | 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 Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 590 | 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 Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 594 | return self.com_binary(Or, nodelist) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 595 | |
| 596 | def and_test(self, nodelist): |
| 597 | # not_test ('and' not_test)* |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 598 | return self.com_binary(And, nodelist) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 599 | |
| 600 | def not_test(self, nodelist): |
| 601 | # 'not' not_test | comparison |
| 602 | result = self.com_node(nodelist[-1]) |
| 603 | if len(nodelist) == 2: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 604 | n = Not(result) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 605 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 615 | results = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 616 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 640 | n = Compare(node, results) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 641 | n.lineno = lineno |
| 642 | return n |
| 643 | |
| 644 | def expr(self, nodelist): |
| 645 | # xor_expr ('|' xor_expr)* |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 646 | return self.com_binary(Bitor, nodelist) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 647 | |
| 648 | def xor_expr(self, nodelist): |
| 649 | # xor_expr ('^' xor_expr)* |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 650 | return self.com_binary(Bitxor, nodelist) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 651 | |
| 652 | def and_expr(self, nodelist): |
| 653 | # xor_expr ('&' xor_expr)* |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 654 | return self.com_binary(Bitand, nodelist) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 655 | |
| 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 662 | node = LeftShift([node, right]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 663 | node.lineno = nodelist[1][2] |
Jeremy Hylton | 7abf520 | 2001-08-29 18:12:30 +0000 | [diff] [blame] | 664 | elif nodelist[i-1][0] == token.RIGHTSHIFT: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 665 | node = RightShift([node, right]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 666 | node.lineno = nodelist[1][2] |
Jeremy Hylton | 7abf520 | 2001-08-29 18:12:30 +0000 | [diff] [blame] | 667 | else: |
| 668 | raise ValueError, "unexpected token: %s" % nodelist[i-1][0] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 669 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 676 | node = Add([node, right]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 677 | node.lineno = nodelist[1][2] |
Jeremy Hylton | 7abf520 | 2001-08-29 18:12:30 +0000 | [diff] [blame] | 678 | elif nodelist[i-1][0] == token.MINUS: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 679 | node = Sub([node, right]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 680 | node.lineno = nodelist[1][2] |
Jeremy Hylton | 7abf520 | 2001-08-29 18:12:30 +0000 | [diff] [blame] | 681 | else: |
| 682 | raise ValueError, "unexpected token: %s" % nodelist[i-1][0] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 683 | 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 Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 689 | t = nodelist[i-1][0] |
| 690 | if t == token.STAR: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 691 | node = Mul([node, right]) |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 692 | elif t == token.SLASH: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 693 | node = Div([node, right]) |
Jeremy Hylton | 7abf520 | 2001-08-29 18:12:30 +0000 | [diff] [blame] | 694 | elif t == token.PERCENT: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 695 | node = Mod([node, right]) |
Jeremy Hylton | 7abf520 | 2001-08-29 18:12:30 +0000 | [diff] [blame] | 696 | elif t == token.DOUBLESLASH: |
| 697 | node = FloorDiv([node, right]) |
| 698 | else: |
| 699 | raise ValueError, "unexpected token: %s" % t |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 700 | node.lineno = nodelist[1][2] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 701 | return node |
| 702 | |
| 703 | def factor(self, nodelist): |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 704 | elt = nodelist[0] |
| 705 | t = elt[0] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 706 | node = self.com_node(nodelist[-1]) |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 707 | # need to handle (unary op)constant here... |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 708 | if t == token.PLUS: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 709 | node = UnaryAdd(node) |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 710 | node.lineno = elt[2] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 711 | elif t == token.MINUS: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 712 | node = UnarySub(node) |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 713 | node.lineno = elt[2] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 714 | elif t == token.TILDE: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 715 | node = Invert(node) |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 716 | node.lineno = elt[2] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 717 | 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 Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 723 | elt = nodelist[i] |
| 724 | if elt[0] == token.DOUBLESTAR: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 725 | n = Power([node, self.com_node(nodelist[i+1])]) |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 726 | n.lineno = elt[2] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 727 | return n |
| 728 | |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 729 | node = self.com_apply_trailer(node, elt) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 730 | |
| 731 | return node |
| 732 | |
| 733 | def atom(self, nodelist): |
Jeremy Hylton | 7845cf8 | 2001-08-29 20:56:30 +0000 | [diff] [blame] | 734 | n = self._atom_dispatch[nodelist[0][0]](nodelist) |
| 735 | n.lineno = nodelist[0][2] |
| 736 | return n |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 737 | |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 738 | def atom_lpar(self, nodelist): |
| 739 | if nodelist[1][0] == token.RPAR: |
| 740 | n = Tuple(()) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 741 | n.lineno = nodelist[0][2] |
| 742 | return n |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 743 | return self.com_node(nodelist[1]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 744 | |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 745 | def atom_lsqb(self, nodelist): |
| 746 | if nodelist[1][0] == token.RSQB: |
| 747 | n = List(()) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 748 | n.lineno = nodelist[0][2] |
| 749 | return n |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 750 | return self.com_list_constructor(nodelist[1]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 751 | |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 752 | def atom_lbrace(self, nodelist): |
| 753 | if nodelist[1][0] == token.RBRACE: |
| 754 | return Dict(()) |
| 755 | return self.com_dictmaker(nodelist[1]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 756 | |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 757 | def atom_backquote(self, nodelist): |
| 758 | n = Backquote(self.com_node(nodelist[1])) |
| 759 | n.lineno = nodelist[0][2] |
| 760 | return n |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 761 | |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 762 | 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 Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 768 | |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 769 | 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 Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 780 | def atom_string(self, nodelist): |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 781 | k = '' |
| 782 | for node in nodelist: |
Jeremy Hylton | accb62b | 2002-12-31 18:17:44 +0000 | [diff] [blame] | 783 | k += self.decode_literal(node[1]) |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 784 | n = Const(k) |
| 785 | n.lineno = nodelist[0][2] |
| 786 | return n |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 787 | |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 788 | 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 Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 793 | |
| 794 | # -------------------------------------------------------------- |
| 795 | # |
| 796 | # INTERNAL PARSING UTILITIES |
| 797 | # |
| 798 | |
Jeremy Hylton | 7e30c9b | 2001-08-27 21:02:51 +0000 | [diff] [blame] | 799 | # 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 Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 810 | 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 Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 815 | return self._dispatch[node[0]](node[1:]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 816 | |
Jeremy Hylton | 42a0830 | 2001-04-09 04:27:12 +0000 | [diff] [blame] | 817 | def com_NEWLINE(self, *args): |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 818 | # 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 Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 821 | return Discard(Const(None)) |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 822 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 823 | def com_arglist(self, nodelist): |
| 824 | # varargslist: |
Jeremy Hylton | 8d22b04 | 2002-04-19 22:56:37 +0000 | [diff] [blame] | 825 | # (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
| 826 | # | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 827 | # fpdef: NAME | '(' fplist ')' |
| 828 | # fplist: fpdef (',' fpdef)* [','] |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 829 | names = [] |
| 830 | defaults = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 831 | flags = 0 |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 832 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 833 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 843 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 844 | if i < len(nodelist): |
Jeremy Hylton | 8d22b04 | 2002-04-19 22:56:37 +0000 | [diff] [blame] | 845 | # should be DOUBLESTAR |
Jeremy Hylton | 7abf520 | 2001-08-29 18:12:30 +0000 | [diff] [blame] | 846 | t = nodelist[i][0] |
| 847 | if t == token.DOUBLESTAR: |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 848 | node = nodelist[i+1] |
Jeremy Hylton | 7abf520 | 2001-08-29 18:12:30 +0000 | [diff] [blame] | 849 | else: |
| 850 | raise ValueError, "unexpected token: %s" % t |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 851 | names.append(node[1]) |
| 852 | flags = flags | CO_VARKEYWORDS |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 853 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 854 | break |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 855 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 856 | # fpdef: NAME | '(' fplist ')' |
| 857 | names.append(self.com_fpdef(node)) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 858 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 859 | i = i + 1 |
| 860 | if i >= len(nodelist): |
| 861 | break |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 862 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 863 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 868 | defaults.append(Const(None)) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 869 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 870 | i = i + 1 |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 871 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 872 | return names, defaults, flags |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 873 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 874 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 879 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 880 | def com_fplist(self, node): |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 881 | # fplist: fpdef (',' fpdef)* [','] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 882 | if len(node) == 2: |
| 883 | return self.com_fpdef(node[1]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 884 | list = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 885 | for i in range(1, len(node), 2): |
| 886 | list.append(self.com_fpdef(node[i])) |
| 887 | return tuple(list) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 888 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 889 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 896 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 897 | def com_dotted_as_name(self, node): |
| 898 | dot = self.com_dotted_name(node[1]) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 899 | if len(node) <= 2: |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 900 | return dot, None |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 901 | 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 Hylton | 2051608 | 2000-09-01 20:33:26 +0000 | [diff] [blame] | 907 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 908 | def com_import_as_name(self, node): |
Jeremy Hylton | 42a0830 | 2001-04-09 04:27:12 +0000 | [diff] [blame] | 909 | if node[0] == token.STAR: |
Jeremy Hylton | 4e1be72 | 2000-10-12 20:23:23 +0000 | [diff] [blame] | 910 | return '*', None |
Jeremy Hylton | 42a0830 | 2001-04-09 04:27:12 +0000 | [diff] [blame] | 911 | 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 Hylton | 2051608 | 2000-09-01 20:33:26 +0000 | [diff] [blame] | 920 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 921 | def com_bases(self, node): |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 922 | bases = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 923 | for i in range(1, len(node), 2): |
| 924 | bases.append(self.com_node(node[i])) |
| 925 | return bases |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 926 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 927 | def com_try_finally(self, nodelist): |
| 928 | # try_fin_stmt: "try" ":" suite "finally" ":" suite |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 929 | n = TryFinally(self.com_node(nodelist[2]), |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 930 | self.com_node(nodelist[5])) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 931 | n.lineno = nodelist[0][2] |
| 932 | return n |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 933 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 934 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 953 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 954 | if node[0] == token.NAME: |
| 955 | elseNode = self.com_node(nodelist[i+2]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 956 | n = TryExcept(self.com_node(nodelist[2]), clauses, elseNode) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 957 | n.lineno = nodelist[0][2] |
| 958 | return n |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 959 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 960 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 970 | if l.__class__ in (Name, Slice, Subscript, Getattr): |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 971 | return l |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 972 | raise SyntaxError, "can't assign to %s" % l.__class__.__name__ |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 973 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 974 | 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. Hudson | 3f06189 | 2004-07-12 13:15:56 +0000 | [diff] [blame] | 979 | if t == symbol.exprlist or t == symbol.testlist or t == symbol.testlist_gexp: |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 980 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 997 | return self.com_assign_trailer(primary, node[-1], |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 998 | assigning) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 999 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1019 | assigns = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1020 | for i in range(1, len(node), 2): |
| 1021 | assigns.append(self.com_assign(node[i], assigning)) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1022 | return AssTuple(assigns) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1023 | |
| 1024 | def com_assign_list(self, node, assigning): |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1025 | assigns = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1026 | for i in range(1, len(node), 2): |
Jeremy Hylton | 2e4cc7e | 2001-09-17 19:33:48 +0000 | [diff] [blame] | 1027 | 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 Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1031 | assigns.append(self.com_assign(node[i], assigning)) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1032 | return AssList(assigns) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1033 | |
| 1034 | def com_assign_name(self, node, assigning): |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1035 | n = AssName(node[1], assigning) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1036 | n.lineno = node[2] |
| 1037 | return n |
| 1038 | |
| 1039 | def com_assign_trailer(self, primary, node, assigning): |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1040 | t = node[1][0] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1041 | 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 Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 1045 | if t == token.LPAR: |
| 1046 | raise SyntaxError, "can't assign to function call" |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1047 | raise SyntaxError, "unknown trailer type: %s" % t |
| 1048 | |
| 1049 | def com_assign_attr(self, primary, node, assigning): |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1050 | return AssAttr(primary, node[1], assigning) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1051 | |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 1052 | def com_binary(self, constructor, nodelist): |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1053 | "Compile 'NODE (OP NODE)*' into (type, [ node1, ..., nodeN ])." |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1054 | l = len(nodelist) |
| 1055 | if l == 1: |
Jeremy Hylton | 7e30c9b | 2001-08-27 21:02:51 +0000 | [diff] [blame] | 1056 | n = nodelist[0] |
| 1057 | return self.lookup_node(n)(n[1:]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1058 | items = [] |
| 1059 | for i in range(0, l, 2): |
Jeremy Hylton | 7e30c9b | 2001-08-27 21:02:51 +0000 | [diff] [blame] | 1060 | n = nodelist[i] |
| 1061 | items.append(self.lookup_node(n)(n[1:])) |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 1062 | return constructor(items) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1063 | |
| 1064 | def com_stmt(self, node): |
Jeremy Hylton | 7e30c9b | 2001-08-27 21:02:51 +0000 | [diff] [blame] | 1065 | result = self.lookup_node(node)(node[1:]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1066 | assert result is not None |
| 1067 | if isinstance(result, Stmt): |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1068 | return result |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1069 | return Stmt([result]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1070 | |
| 1071 | def com_append_stmt(self, stmts, node): |
| 1072 | result = self.com_node(node) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1073 | assert result is not None |
| 1074 | if isinstance(result, Stmt): |
| 1075 | stmts.extend(result.nodes) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1076 | else: |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1077 | stmts.append(result) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1078 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1079 | if hasattr(symbol, 'list_for'): |
| 1080 | def com_list_constructor(self, nodelist): |
| 1081 | # listmaker: test ( list_for | (',' test)* [','] ) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1082 | values = [] |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1083 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1091 | return List(values) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1092 | |
| 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 Hylton | 42a0830 | 2001-04-09 04:27:12 +0000 | [diff] [blame] | 1097 | |
| 1098 | # XXX should raise SyntaxError for assignment |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 1099 | |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1100 | lineno = node[1][2] |
| 1101 | fors = [] |
| 1102 | while node: |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 1103 | t = node[1][1] |
| 1104 | if t == 'for': |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1105 | assignNode = self.com_assign(node[2], OP_ASSIGN) |
| 1106 | listNode = self.com_node(node[4]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1107 | newfor = ListCompFor(assignNode, listNode, []) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1108 | 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 Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 1114 | elif t == 'if': |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1115 | test = self.com_node(node[2]) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1116 | newif = ListCompIf(test) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1117 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1127 | n = ListComp(expr, fors) |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1128 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1136 | values = [] |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1137 | for i in range(1, len(nodelist), 2): |
| 1138 | values.append(self.com_node(nodelist[i])) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1139 | return List(values) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1140 | |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1141 | 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 Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1183 | def com_dictmaker(self, nodelist): |
| 1184 | # dictmaker: test ':' test (',' test ':' value)* [','] |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1185 | items = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1186 | for i in range(1, len(nodelist), 4): |
Jeremy Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1187 | items.append((self.com_node(nodelist[i]), |
| 1188 | self.com_node(nodelist[i+2]))) |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1189 | return Dict(items) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1190 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1191 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1199 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1200 | raise SyntaxError, 'unknown node type: %s' % t |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1201 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1202 | def com_select_member(self, primaryNode, nodelist): |
| 1203 | if nodelist[0] != token.NAME: |
| 1204 | raise SyntaxError, "member must be a name" |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1205 | n = Getattr(primaryNode, nodelist[1]) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1206 | n.lineno = nodelist[2] |
| 1207 | return n |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1208 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1209 | def com_call_function(self, primaryNode, nodelist): |
| 1210 | if nodelist[0] == token.RPAR: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1211 | return CallFunc(primaryNode, []) |
| 1212 | args = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1213 | 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 Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 1218 | break |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1219 | kw, result = self.com_argument(node, kw) |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1220 | if len_nodelist != 2 and isinstance(result, GenExpr): |
| 1221 | raise SyntaxError, 'generator expression needs parenthesis' |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1222 | args.append(result) |
Jeremy Hylton | be317e6 | 2000-05-02 22:32:59 +0000 | [diff] [blame] | 1223 | else: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1224 | # No broken by star arg, so skip the last one we processed. |
| 1225 | i = i + 1 |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1226 | if i < len_nodelist and nodelist[i][0] == token.COMMA: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1227 | # need to accept an application that looks like "f(a, b,)" |
| 1228 | i = i + 1 |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1229 | star_node = dstar_node = None |
| 1230 | while i < len_nodelist: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1231 | 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 Hylton | be317e6 | 2000-05-02 22:32:59 +0000 | [diff] [blame] | 1244 | |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1245 | return CallFunc(primaryNode, args, star_node, dstar_node) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1246 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1247 | def com_argument(self, nodelist, kw): |
Raymond Hettinger | 354433a | 2004-05-19 08:20:33 +0000 | [diff] [blame] | 1248 | 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 Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1251 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1261 | node = Keyword(n[1], result) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1262 | node.lineno = n[2] |
| 1263 | return 1, node |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1264 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1265 | def com_subscriptlist(self, primary, nodelist, assigning): |
| 1266 | # slicing: simple_slicing | extended_slicing |
| 1267 | # simple_slicing: primary "[" short_slice "]" |
Tim Peters | e0c446b | 2001-10-18 21:57:37 +0000 | [diff] [blame] | 1268 | # extended_slicing: primary "[" slice_list "]" |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1269 | # slice_list: slice_item ("," slice_item)* [","] |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1270 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1271 | # 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1278 | |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1279 | subscripts = [] |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1280 | for i in range(1, len(nodelist), 2): |
| 1281 | subscripts.append(self.com_subscript(nodelist[i])) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1282 | |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1283 | return Subscript(primary, assigning, subscripts) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1284 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1285 | def com_subscript(self, node): |
| 1286 | # slice_item: expression | proper_slice | ellipsis |
| 1287 | ch = node[1] |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 1288 | t = ch[0] |
| 1289 | if t == token.DOT and node[2][0] == token.DOT: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1290 | return Ellipsis() |
Jeremy Hylton | 4ebf3be | 2000-11-06 16:03:52 +0000 | [diff] [blame] | 1291 | if t == token.COLON or len(node) > 2: |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1292 | return self.com_sliceobj(node) |
| 1293 | return self.com_node(ch) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1294 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1295 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1304 | |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1305 | items = [] |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1306 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1307 | if node[1][0] == token.COLON: |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1308 | items.append(Const(None)) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1309 | i = 2 |
| 1310 | else: |
| 1311 | items.append(self.com_node(node[1])) |
| 1312 | # i == 2 is a COLON |
| 1313 | i = 3 |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1314 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1315 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1319 | items.append(Const(None)) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1320 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1321 | # 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1326 | items.append(Const(None)) |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1327 | else: |
| 1328 | items.append(self.com_node(ch[2])) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1329 | |
Jeremy Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1330 | return Sliceobj(items) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1331 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1332 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1343 | return Slice(primary, assigning, lower, upper) |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1344 | |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1345 | 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 Hylton | 7cff7fe | 2000-10-25 18:10:32 +0000 | [diff] [blame] | 1368 | if n == symbol.stmt or n == symbol.simple_stmt \ |
| 1369 | or n == symbol.small_stmt: |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1370 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1374 | |
| 1375 | |
| 1376 | _doc_nodes = [ |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1377 | symbol.expr_stmt, |
| 1378 | symbol.testlist, |
Jeremy Hylton | d114261 | 2001-10-17 13:32:52 +0000 | [diff] [blame] | 1379 | symbol.testlist_safe, |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1380 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1393 | |
| 1394 | # comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
| 1395 | # | 'in' | 'not' 'in' | 'is' | 'is' 'not' |
| 1396 | _cmp_types = { |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1397 | token.LESS : '<', |
| 1398 | token.GREATER : '>', |
| 1399 | token.EQEQUAL : '==', |
| 1400 | token.EQUAL : '==', |
| 1401 | token.LESSEQUAL : '<=', |
| 1402 | token.GREATEREQUAL : '>=', |
| 1403 | token.NOTEQUAL : '!=', |
| 1404 | } |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1405 | |
| 1406 | _legal_node_types = [ |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1407 | 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 Hylton | d114261 | 2001-10-17 13:32:52 +0000 | [diff] [blame] | 1432 | symbol.testlist_safe, |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1433 | 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 Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1448 | |
Jeremy Hylton | 7e30c9b | 2001-08-27 21:02:51 +0000 | [diff] [blame] | 1449 | if hasattr(symbol, 'yield_stmt'): |
| 1450 | _legal_node_types.append(symbol.yield_stmt) |
| 1451 | |
Jeremy Hylton | f968e85 | 2000-02-04 00:25:23 +0000 | [diff] [blame] | 1452 | _assign_types = [ |
Jeremy Hylton | 572bdce | 2000-09-20 02:47:28 +0000 | [diff] [blame] | 1453 | 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 Hylton | 9c048f9 | 2000-10-13 21:58:13 +0000 | [diff] [blame] | 1465 | |
| 1466 | import types |
| 1467 | _names = {} |
| 1468 | for k, v in symbol.sym_name.items(): |
| 1469 | _names[k] = v |
| 1470 | for k, v in token.tok_name.items(): |
| 1471 | _names[k] = v |
| 1472 | |
| 1473 | def 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 |