| 1 | n/a | """Parse tree transformation module. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Transforms Python source code into an abstract syntax tree (AST) |
|---|
| 4 | n/a | defined in the ast module. |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | The simplest ways to invoke this module are via parse and parseFile. |
|---|
| 7 | n/a | parse(buf) -> AST |
|---|
| 8 | n/a | parseFile(path) -> AST |
|---|
| 9 | 2 | """ |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | # Original version written by Greg Stein (gstein@lyra.org) |
|---|
| 12 | n/a | # and Bill Tutt (rassilon@lima.mudlib.org) |
|---|
| 13 | n/a | # February 1997. |
|---|
| 14 | n/a | # |
|---|
| 15 | n/a | # Modifications and improvements for Python 2.0 by Jeremy Hylton and |
|---|
| 16 | n/a | # Mark Hammond |
|---|
| 17 | n/a | # |
|---|
| 18 | n/a | # Some fixes to try to have correct line number on almost all nodes |
|---|
| 19 | n/a | # (except Module, Discard and Stmt) added by Sylvain Thenault |
|---|
| 20 | n/a | # |
|---|
| 21 | n/a | # Portions of this file are: |
|---|
| 22 | n/a | # Copyright (C) 1997-1998 Greg Stein. All Rights Reserved. |
|---|
| 23 | n/a | # |
|---|
| 24 | n/a | # This module is provided under a BSD-ish license. See |
|---|
| 25 | n/a | # http://www.opensource.org/licenses/bsd-license.html |
|---|
| 26 | n/a | # and replace OWNER, ORGANIZATION, and YEAR as appropriate. |
|---|
| 27 | n/a | |
|---|
| 28 | 2 | from compiler.ast import * |
|---|
| 29 | 2 | import parser |
|---|
| 30 | 2 | import symbol |
|---|
| 31 | 2 | import token |
|---|
| 32 | n/a | |
|---|
| 33 | 4 | class WalkerError(StandardError): |
|---|
| 34 | 2 | pass |
|---|
| 35 | n/a | |
|---|
| 36 | 2 | from compiler.consts import CO_VARARGS, CO_VARKEYWORDS |
|---|
| 37 | 2 | from compiler.consts import OP_ASSIGN, OP_DELETE, OP_APPLY |
|---|
| 38 | n/a | |
|---|
| 39 | 2 | def parseFile(path): |
|---|
| 40 | 1 | f = open(path, "U") |
|---|
| 41 | n/a | # XXX The parser API tolerates files without a trailing newline, |
|---|
| 42 | n/a | # but not strings without a trailing newline. Always add an extra |
|---|
| 43 | n/a | # newline to the file contents, since we're going through the string |
|---|
| 44 | n/a | # version of the API. |
|---|
| 45 | 1 | src = f.read() + "\n" |
|---|
| 46 | 1 | f.close() |
|---|
| 47 | 1 | return parse(src) |
|---|
| 48 | n/a | |
|---|
| 49 | 2 | def parse(buf, mode="exec"): |
|---|
| 50 | 38 | if mode == "exec" or mode == "single": |
|---|
| 51 | 29 | return Transformer().parsesuite(buf) |
|---|
| 52 | 9 | elif mode == "eval": |
|---|
| 53 | 9 | return Transformer().parseexpr(buf) |
|---|
| 54 | n/a | else: |
|---|
| 55 | 0 | raise ValueError("compile() arg 3 must be" |
|---|
| 56 | n/a | " 'exec' or 'eval' or 'single'") |
|---|
| 57 | n/a | |
|---|
| 58 | 2 | def asList(nodes): |
|---|
| 59 | 0 | l = [] |
|---|
| 60 | 0 | for item in nodes: |
|---|
| 61 | 0 | if hasattr(item, "asList"): |
|---|
| 62 | 0 | l.append(item.asList()) |
|---|
| 63 | n/a | else: |
|---|
| 64 | 0 | if type(item) is type( (None, None) ): |
|---|
| 65 | 0 | l.append(tuple(asList(item))) |
|---|
| 66 | 0 | elif type(item) is type( [] ): |
|---|
| 67 | 0 | l.append(asList(item)) |
|---|
| 68 | n/a | else: |
|---|
| 69 | 0 | l.append(item) |
|---|
| 70 | 0 | return l |
|---|
| 71 | n/a | |
|---|
| 72 | 2 | def extractLineNo(ast): |
|---|
| 73 | 48570 | if not isinstance(ast[1], tuple): |
|---|
| 74 | n/a | # get a terminal node |
|---|
| 75 | 3837 | return ast[2] |
|---|
| 76 | 44733 | for child in ast[1:]: |
|---|
| 77 | 44733 | if isinstance(child, tuple): |
|---|
| 78 | 44733 | lineno = extractLineNo(child) |
|---|
| 79 | 44733 | if lineno is not None: |
|---|
| 80 | 44733 | return lineno |
|---|
| 81 | n/a | |
|---|
| 82 | 2 | def Node(*args): |
|---|
| 83 | 0 | kind = args[0] |
|---|
| 84 | 0 | if kind in nodes: |
|---|
| 85 | 0 | try: |
|---|
| 86 | 0 | return nodes[kind](*args[1:]) |
|---|
| 87 | 0 | except TypeError: |
|---|
| 88 | 0 | print nodes[kind], len(args), args |
|---|
| 89 | 0 | raise |
|---|
| 90 | n/a | else: |
|---|
| 91 | 0 | raise WalkerError, "Can't find appropriate Node type: %s" % str(args) |
|---|
| 92 | n/a | #return apply(ast.Node, args) |
|---|
| 93 | n/a | |
|---|
| 94 | 4 | class Transformer: |
|---|
| 95 | n/a | """Utility object for transforming Python parse trees. |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | Exposes the following methods: |
|---|
| 98 | n/a | tree = transform(ast_tree) |
|---|
| 99 | n/a | tree = parsesuite(text) |
|---|
| 100 | n/a | tree = parseexpr(text) |
|---|
| 101 | n/a | tree = parsefile(fileob | filename) |
|---|
| 102 | 2 | """ |
|---|
| 103 | n/a | |
|---|
| 104 | 2 | def __init__(self): |
|---|
| 105 | 38 | self._dispatch = {} |
|---|
| 106 | 3306 | for value, name in symbol.sym_name.items(): |
|---|
| 107 | 3268 | if hasattr(self, name): |
|---|
| 108 | 2508 | self._dispatch[value] = getattr(self, name) |
|---|
| 109 | 38 | self._dispatch[token.NEWLINE] = self.com_NEWLINE |
|---|
| 110 | 38 | self._atom_dispatch = {token.LPAR: self.atom_lpar, |
|---|
| 111 | 38 | token.LSQB: self.atom_lsqb, |
|---|
| 112 | 38 | token.LBRACE: self.atom_lbrace, |
|---|
| 113 | 38 | token.BACKQUOTE: self.atom_backquote, |
|---|
| 114 | 38 | token.NUMBER: self.atom_number, |
|---|
| 115 | 38 | token.STRING: self.atom_string, |
|---|
| 116 | 38 | token.NAME: self.atom_name, |
|---|
| 117 | n/a | } |
|---|
| 118 | 38 | self.encoding = None |
|---|
| 119 | n/a | |
|---|
| 120 | 2 | def transform(self, tree): |
|---|
| 121 | n/a | """Transform an AST into a modified parse tree.""" |
|---|
| 122 | 38 | if not (isinstance(tree, tuple) or isinstance(tree, list)): |
|---|
| 123 | 38 | tree = parser.st2tuple(tree, line_info=1) |
|---|
| 124 | 38 | return self.compile_node(tree) |
|---|
| 125 | n/a | |
|---|
| 126 | 2 | def parsesuite(self, text): |
|---|
| 127 | n/a | """Return a modified parse tree for the given suite text.""" |
|---|
| 128 | 29 | return self.transform(parser.suite(text)) |
|---|
| 129 | n/a | |
|---|
| 130 | 2 | def parseexpr(self, text): |
|---|
| 131 | n/a | """Return a modified parse tree for the given expression text.""" |
|---|
| 132 | 9 | return self.transform(parser.expr(text)) |
|---|
| 133 | n/a | |
|---|
| 134 | 2 | def parsefile(self, file): |
|---|
| 135 | n/a | """Return a modified parse tree for the contents of the given file.""" |
|---|
| 136 | 0 | if type(file) == type(''): |
|---|
| 137 | 0 | file = open(file) |
|---|
| 138 | 0 | return self.parsesuite(file.read()) |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | # -------------------------------------------------------------- |
|---|
| 141 | n/a | # |
|---|
| 142 | n/a | # PRIVATE METHODS |
|---|
| 143 | n/a | # |
|---|
| 144 | n/a | |
|---|
| 145 | 2 | def compile_node(self, node): |
|---|
| 146 | n/a | ### emit a line-number node? |
|---|
| 147 | 38 | n = node[0] |
|---|
| 148 | n/a | |
|---|
| 149 | 38 | if n == symbol.encoding_decl: |
|---|
| 150 | 0 | self.encoding = node[2] |
|---|
| 151 | 0 | node = node[1] |
|---|
| 152 | 0 | n = node[0] |
|---|
| 153 | n/a | |
|---|
| 154 | 38 | if n == symbol.single_input: |
|---|
| 155 | 0 | return self.single_input(node[1:]) |
|---|
| 156 | 38 | if n == symbol.file_input: |
|---|
| 157 | 29 | return self.file_input(node[1:]) |
|---|
| 158 | 9 | if n == symbol.eval_input: |
|---|
| 159 | 9 | return self.eval_input(node[1:]) |
|---|
| 160 | 0 | if n == symbol.lambdef: |
|---|
| 161 | 0 | return self.lambdef(node[1:]) |
|---|
| 162 | 0 | if n == symbol.funcdef: |
|---|
| 163 | 0 | return self.funcdef(node[1:]) |
|---|
| 164 | 0 | if n == symbol.classdef: |
|---|
| 165 | 0 | return self.classdef(node[1:]) |
|---|
| 166 | n/a | |
|---|
| 167 | 0 | raise WalkerError, ('unexpected node type', n) |
|---|
| 168 | n/a | |
|---|
| 169 | 2 | def single_input(self, node): |
|---|
| 170 | n/a | ### do we want to do anything about being "interactive" ? |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | # NEWLINE | simple_stmt | compound_stmt NEWLINE |
|---|
| 173 | 0 | n = node[0][0] |
|---|
| 174 | 0 | if n != token.NEWLINE: |
|---|
| 175 | 0 | return self.com_stmt(node[0]) |
|---|
| 176 | n/a | |
|---|
| 177 | 0 | return Pass() |
|---|
| 178 | n/a | |
|---|
| 179 | 2 | def file_input(self, nodelist): |
|---|
| 180 | 29 | doc = self.get_docstring(nodelist, symbol.file_input) |
|---|
| 181 | 29 | if doc is not None: |
|---|
| 182 | 5 | i = 1 |
|---|
| 183 | n/a | else: |
|---|
| 184 | 24 | i = 0 |
|---|
| 185 | 29 | stmts = [] |
|---|
| 186 | 304 | for node in nodelist[i:]: |
|---|
| 187 | 278 | if node[0] != token.ENDMARKER and node[0] != token.NEWLINE: |
|---|
| 188 | 226 | self.com_append_stmt(stmts, node) |
|---|
| 189 | 26 | return Module(doc, Stmt(stmts)) |
|---|
| 190 | n/a | |
|---|
| 191 | 2 | def eval_input(self, nodelist): |
|---|
| 192 | n/a | # from the built-in function input() |
|---|
| 193 | n/a | ### is this sufficient? |
|---|
| 194 | 9 | return Expression(self.com_node(nodelist[0])) |
|---|
| 195 | n/a | |
|---|
| 196 | 2 | def decorator_name(self, nodelist): |
|---|
| 197 | 19 | listlen = len(nodelist) |
|---|
| 198 | 19 | assert listlen >= 1 and listlen % 2 == 1 |
|---|
| 199 | n/a | |
|---|
| 200 | 19 | item = self.atom_name(nodelist) |
|---|
| 201 | 19 | i = 1 |
|---|
| 202 | 37 | while i < listlen: |
|---|
| 203 | 18 | assert nodelist[i][0] == token.DOT |
|---|
| 204 | 18 | assert nodelist[i + 1][0] == token.NAME |
|---|
| 205 | 18 | item = Getattr(item, nodelist[i + 1][1]) |
|---|
| 206 | 18 | i += 2 |
|---|
| 207 | n/a | |
|---|
| 208 | 19 | return item |
|---|
| 209 | n/a | |
|---|
| 210 | 2 | def decorator(self, nodelist): |
|---|
| 211 | n/a | # '@' dotted_name [ '(' [arglist] ')' ] |
|---|
| 212 | 19 | assert len(nodelist) in (3, 5, 6) |
|---|
| 213 | 19 | assert nodelist[0][0] == token.AT |
|---|
| 214 | 19 | assert nodelist[-1][0] == token.NEWLINE |
|---|
| 215 | n/a | |
|---|
| 216 | 19 | assert nodelist[1][0] == symbol.dotted_name |
|---|
| 217 | 19 | funcname = self.decorator_name(nodelist[1][1:]) |
|---|
| 218 | n/a | |
|---|
| 219 | 19 | if len(nodelist) > 3: |
|---|
| 220 | 7 | assert nodelist[2][0] == token.LPAR |
|---|
| 221 | 7 | expr = self.com_call_function(funcname, nodelist[3]) |
|---|
| 222 | n/a | else: |
|---|
| 223 | 12 | expr = funcname |
|---|
| 224 | n/a | |
|---|
| 225 | 19 | return expr |
|---|
| 226 | n/a | |
|---|
| 227 | 2 | def decorators(self, nodelist): |
|---|
| 228 | n/a | # decorators: decorator ([NEWLINE] decorator)* NEWLINE |
|---|
| 229 | 19 | items = [] |
|---|
| 230 | 38 | for dec_nodelist in nodelist: |
|---|
| 231 | 19 | assert dec_nodelist[0] == symbol.decorator |
|---|
| 232 | 19 | items.append(self.decorator(dec_nodelist[1:])) |
|---|
| 233 | 19 | return Decorators(items) |
|---|
| 234 | n/a | |
|---|
| 235 | 2 | def decorated(self, nodelist): |
|---|
| 236 | 19 | assert nodelist[0][0] == symbol.decorators |
|---|
| 237 | 19 | if nodelist[1][0] == symbol.funcdef: |
|---|
| 238 | 19 | n = [nodelist[0]] + list(nodelist[1][1:]) |
|---|
| 239 | 19 | return self.funcdef(n) |
|---|
| 240 | 0 | elif nodelist[1][0] == symbol.classdef: |
|---|
| 241 | 0 | decorators = self.decorators(nodelist[0][1:]) |
|---|
| 242 | 0 | cls = self.classdef(nodelist[1][1:]) |
|---|
| 243 | 0 | cls.decorators = decorators |
|---|
| 244 | 0 | return cls |
|---|
| 245 | 0 | raise WalkerError() |
|---|
| 246 | n/a | |
|---|
| 247 | 2 | def funcdef(self, nodelist): |
|---|
| 248 | n/a | # -6 -5 -4 -3 -2 -1 |
|---|
| 249 | n/a | # funcdef: [decorators] 'def' NAME parameters ':' suite |
|---|
| 250 | n/a | # parameters: '(' [varargslist] ')' |
|---|
| 251 | n/a | |
|---|
| 252 | 649 | if len(nodelist) == 6: |
|---|
| 253 | 19 | assert nodelist[0][0] == symbol.decorators |
|---|
| 254 | 19 | decorators = self.decorators(nodelist[0][1:]) |
|---|
| 255 | n/a | else: |
|---|
| 256 | 630 | assert len(nodelist) == 5 |
|---|
| 257 | 630 | decorators = None |
|---|
| 258 | n/a | |
|---|
| 259 | 649 | lineno = nodelist[-4][2] |
|---|
| 260 | 649 | name = nodelist[-4][1] |
|---|
| 261 | 649 | args = nodelist[-3][2] |
|---|
| 262 | n/a | |
|---|
| 263 | 649 | if args[0] == symbol.varargslist: |
|---|
| 264 | 615 | names, defaults, flags = self.com_arglist(args[1:]) |
|---|
| 265 | n/a | else: |
|---|
| 266 | 34 | names = defaults = () |
|---|
| 267 | 34 | flags = 0 |
|---|
| 268 | 648 | doc = self.get_docstring(nodelist[-1]) |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | # code for function |
|---|
| 271 | 648 | code = self.com_node(nodelist[-1]) |
|---|
| 272 | n/a | |
|---|
| 273 | 648 | if doc is not None: |
|---|
| 274 | 21 | assert isinstance(code, Stmt) |
|---|
| 275 | 21 | assert isinstance(code.nodes[0], Discard) |
|---|
| 276 | 21 | del code.nodes[0] |
|---|
| 277 | 648 | return Function(decorators, name, names, defaults, flags, doc, code, |
|---|
| 278 | 648 | lineno=lineno) |
|---|
| 279 | n/a | |
|---|
| 280 | 2 | def lambdef(self, nodelist): |
|---|
| 281 | n/a | # lambdef: 'lambda' [varargslist] ':' test |
|---|
| 282 | 18 | if nodelist[2][0] == symbol.varargslist: |
|---|
| 283 | 17 | names, defaults, flags = self.com_arglist(nodelist[2][1:]) |
|---|
| 284 | n/a | else: |
|---|
| 285 | 1 | names = defaults = () |
|---|
| 286 | 1 | flags = 0 |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | # code for lambda |
|---|
| 289 | 18 | code = self.com_node(nodelist[-1]) |
|---|
| 290 | n/a | |
|---|
| 291 | 18 | return Lambda(names, defaults, flags, code, lineno=nodelist[1][2]) |
|---|
| 292 | 2 | old_lambdef = lambdef |
|---|
| 293 | n/a | |
|---|
| 294 | 2 | def classdef(self, nodelist): |
|---|
| 295 | n/a | # classdef: 'class' NAME ['(' [testlist] ')'] ':' suite |
|---|
| 296 | n/a | |
|---|
| 297 | 395 | name = nodelist[1][1] |
|---|
| 298 | 395 | doc = self.get_docstring(nodelist[-1]) |
|---|
| 299 | 395 | if nodelist[2][0] == token.COLON: |
|---|
| 300 | 35 | bases = [] |
|---|
| 301 | 360 | elif nodelist[3][0] == token.RPAR: |
|---|
| 302 | 1 | bases = [] |
|---|
| 303 | n/a | else: |
|---|
| 304 | 359 | bases = self.com_bases(nodelist[3]) |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | # code for class |
|---|
| 307 | 395 | code = self.com_node(nodelist[-1]) |
|---|
| 308 | n/a | |
|---|
| 309 | 395 | if doc is not None: |
|---|
| 310 | 8 | assert isinstance(code, Stmt) |
|---|
| 311 | 8 | assert isinstance(code.nodes[0], Discard) |
|---|
| 312 | 8 | del code.nodes[0] |
|---|
| 313 | n/a | |
|---|
| 314 | 395 | return Class(name, bases, doc, code, lineno=nodelist[1][2]) |
|---|
| 315 | n/a | |
|---|
| 316 | 2 | def stmt(self, nodelist): |
|---|
| 317 | 9851 | return self.com_stmt(nodelist[0]) |
|---|
| 318 | n/a | |
|---|
| 319 | 2 | small_stmt = stmt |
|---|
| 320 | 2 | flow_stmt = stmt |
|---|
| 321 | 2 | compound_stmt = stmt |
|---|
| 322 | n/a | |
|---|
| 323 | 2 | def simple_stmt(self, nodelist): |
|---|
| 324 | n/a | # small_stmt (';' small_stmt)* [';'] NEWLINE |
|---|
| 325 | 3395 | stmts = [] |
|---|
| 326 | 6793 | for i in range(0, len(nodelist), 2): |
|---|
| 327 | 3400 | self.com_append_stmt(stmts, nodelist[i]) |
|---|
| 328 | 3393 | return Stmt(stmts) |
|---|
| 329 | n/a | |
|---|
| 330 | 2 | def parameters(self, nodelist): |
|---|
| 331 | 0 | raise WalkerError |
|---|
| 332 | n/a | |
|---|
| 333 | 2 | def varargslist(self, nodelist): |
|---|
| 334 | 0 | raise WalkerError |
|---|
| 335 | n/a | |
|---|
| 336 | 2 | def fpdef(self, nodelist): |
|---|
| 337 | 0 | raise WalkerError |
|---|
| 338 | n/a | |
|---|
| 339 | 2 | def fplist(self, nodelist): |
|---|
| 340 | 0 | raise WalkerError |
|---|
| 341 | n/a | |
|---|
| 342 | 2 | def dotted_name(self, nodelist): |
|---|
| 343 | 0 | raise WalkerError |
|---|
| 344 | n/a | |
|---|
| 345 | 2 | def comp_op(self, nodelist): |
|---|
| 346 | 0 | raise WalkerError |
|---|
| 347 | n/a | |
|---|
| 348 | 2 | def trailer(self, nodelist): |
|---|
| 349 | 0 | raise WalkerError |
|---|
| 350 | n/a | |
|---|
| 351 | 2 | def sliceop(self, nodelist): |
|---|
| 352 | 0 | raise WalkerError |
|---|
| 353 | n/a | |
|---|
| 354 | 2 | def argument(self, nodelist): |
|---|
| 355 | 0 | raise WalkerError |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | # -------------------------------------------------------------- |
|---|
| 358 | n/a | # |
|---|
| 359 | n/a | # STATEMENT NODES (invoked by com_node()) |
|---|
| 360 | n/a | # |
|---|
| 361 | n/a | |
|---|
| 362 | 2 | def expr_stmt(self, nodelist): |
|---|
| 363 | n/a | # augassign testlist | testlist ('=' testlist)* |
|---|
| 364 | 2651 | en = nodelist[-1] |
|---|
| 365 | 2651 | exprNode = self.lookup_node(en)(en[1:]) |
|---|
| 366 | 2649 | if len(nodelist) == 1: |
|---|
| 367 | 1592 | return Discard(exprNode, lineno=exprNode.lineno) |
|---|
| 368 | 1057 | if nodelist[1][0] == token.EQUAL: |
|---|
| 369 | 1038 | nodesl = [] |
|---|
| 370 | 2080 | for i in range(0, len(nodelist) - 2, 2): |
|---|
| 371 | 1042 | nodesl.append(self.com_assign(nodelist[i], OP_ASSIGN)) |
|---|
| 372 | 1038 | return Assign(nodesl, exprNode, lineno=nodelist[1][2]) |
|---|
| 373 | n/a | else: |
|---|
| 374 | 19 | lval = self.com_augassign(nodelist[0]) |
|---|
| 375 | 19 | op = self.com_augassign_op(nodelist[1]) |
|---|
| 376 | 19 | return AugAssign(lval, op[1], exprNode, lineno=op[2]) |
|---|
| 377 | 0 | raise WalkerError, "can't get here" |
|---|
| 378 | n/a | |
|---|
| 379 | 2 | def print_stmt(self, nodelist): |
|---|
| 380 | n/a | # print ([ test (',' test)* [','] ] | '>>' test [ (',' test)+ [','] ]) |
|---|
| 381 | 12 | items = [] |
|---|
| 382 | 12 | if len(nodelist) == 1: |
|---|
| 383 | 0 | start = 1 |
|---|
| 384 | 0 | dest = None |
|---|
| 385 | 12 | elif nodelist[1][0] == token.RIGHTSHIFT: |
|---|
| 386 | 5 | assert len(nodelist) == 3 \ |
|---|
| 387 | 5 | or nodelist[3][0] == token.COMMA |
|---|
| 388 | 5 | dest = self.com_node(nodelist[2]) |
|---|
| 389 | 5 | start = 4 |
|---|
| 390 | n/a | else: |
|---|
| 391 | 7 | dest = None |
|---|
| 392 | 7 | start = 1 |
|---|
| 393 | 27 | for i in range(start, len(nodelist), 2): |
|---|
| 394 | 15 | items.append(self.com_node(nodelist[i])) |
|---|
| 395 | 12 | if nodelist[-1][0] == token.COMMA: |
|---|
| 396 | 0 | return Print(items, dest, lineno=nodelist[0][2]) |
|---|
| 397 | 12 | return Printnl(items, dest, lineno=nodelist[0][2]) |
|---|
| 398 | n/a | |
|---|
| 399 | 2 | def del_stmt(self, nodelist): |
|---|
| 400 | 55 | return self.com_assign(nodelist[1], OP_DELETE) |
|---|
| 401 | n/a | |
|---|
| 402 | 2 | def pass_stmt(self, nodelist): |
|---|
| 403 | 266 | return Pass(lineno=nodelist[0][2]) |
|---|
| 404 | n/a | |
|---|
| 405 | 2 | def break_stmt(self, nodelist): |
|---|
| 406 | 0 | return Break(lineno=nodelist[0][2]) |
|---|
| 407 | n/a | |
|---|
| 408 | 2 | def continue_stmt(self, nodelist): |
|---|
| 409 | 5 | return Continue(lineno=nodelist[0][2]) |
|---|
| 410 | n/a | |
|---|
| 411 | 2 | def return_stmt(self, nodelist): |
|---|
| 412 | n/a | # return: [testlist] |
|---|
| 413 | 281 | if len(nodelist) < 2: |
|---|
| 414 | 1 | return Return(Const(None), lineno=nodelist[0][2]) |
|---|
| 415 | 280 | return Return(self.com_node(nodelist[1]), lineno=nodelist[0][2]) |
|---|
| 416 | n/a | |
|---|
| 417 | 2 | def yield_stmt(self, nodelist): |
|---|
| 418 | 2 | expr = self.com_node(nodelist[0]) |
|---|
| 419 | 2 | return Discard(expr, lineno=expr.lineno) |
|---|
| 420 | n/a | |
|---|
| 421 | 2 | def yield_expr(self, nodelist): |
|---|
| 422 | 2 | if len(nodelist) > 1: |
|---|
| 423 | 1 | value = self.com_node(nodelist[1]) |
|---|
| 424 | n/a | else: |
|---|
| 425 | 1 | value = Const(None) |
|---|
| 426 | 2 | return Yield(value, lineno=nodelist[0][2]) |
|---|
| 427 | n/a | |
|---|
| 428 | 2 | def raise_stmt(self, nodelist): |
|---|
| 429 | n/a | # raise: [test [',' test [',' test]]] |
|---|
| 430 | 30 | if len(nodelist) > 5: |
|---|
| 431 | 0 | expr3 = self.com_node(nodelist[5]) |
|---|
| 432 | n/a | else: |
|---|
| 433 | 30 | expr3 = None |
|---|
| 434 | 30 | if len(nodelist) > 3: |
|---|
| 435 | 12 | expr2 = self.com_node(nodelist[3]) |
|---|
| 436 | n/a | else: |
|---|
| 437 | 18 | expr2 = None |
|---|
| 438 | 30 | if len(nodelist) > 1: |
|---|
| 439 | 27 | expr1 = self.com_node(nodelist[1]) |
|---|
| 440 | n/a | else: |
|---|
| 441 | 3 | expr1 = None |
|---|
| 442 | 30 | return Raise(expr1, expr2, expr3, lineno=nodelist[0][2]) |
|---|
| 443 | n/a | |
|---|
| 444 | 2 | def import_stmt(self, nodelist): |
|---|
| 445 | n/a | # import_stmt: import_name | import_from |
|---|
| 446 | 69 | assert len(nodelist) == 1 |
|---|
| 447 | 69 | return self.com_node(nodelist[0]) |
|---|
| 448 | n/a | |
|---|
| 449 | 2 | def import_name(self, nodelist): |
|---|
| 450 | n/a | # import_name: 'import' dotted_as_names |
|---|
| 451 | 50 | return Import(self.com_dotted_as_names(nodelist[1]), |
|---|
| 452 | 50 | lineno=nodelist[0][2]) |
|---|
| 453 | n/a | |
|---|
| 454 | 2 | def import_from(self, nodelist): |
|---|
| 455 | n/a | # import_from: 'from' ('.'* dotted_name | '.') 'import' ('*' | |
|---|
| 456 | n/a | # '(' import_as_names ')' | import_as_names) |
|---|
| 457 | 19 | assert nodelist[0][1] == 'from' |
|---|
| 458 | 19 | idx = 1 |
|---|
| 459 | 19 | while nodelist[idx][1] == '.': |
|---|
| 460 | 0 | idx += 1 |
|---|
| 461 | 19 | level = idx - 1 |
|---|
| 462 | 19 | if nodelist[idx][0] == symbol.dotted_name: |
|---|
| 463 | 19 | fromname = self.com_dotted_name(nodelist[idx]) |
|---|
| 464 | 19 | idx += 1 |
|---|
| 465 | n/a | else: |
|---|
| 466 | 0 | fromname = "" |
|---|
| 467 | 19 | assert nodelist[idx][1] == 'import' |
|---|
| 468 | 19 | if nodelist[idx + 1][0] == token.STAR: |
|---|
| 469 | 1 | return From(fromname, [('*', None)], level, |
|---|
| 470 | 1 | lineno=nodelist[0][2]) |
|---|
| 471 | n/a | else: |
|---|
| 472 | 18 | node = nodelist[idx + 1 + (nodelist[idx + 1][0] == token.LPAR)] |
|---|
| 473 | 18 | return From(fromname, self.com_import_as_names(node), level, |
|---|
| 474 | 18 | lineno=nodelist[0][2]) |
|---|
| 475 | n/a | |
|---|
| 476 | 2 | def global_stmt(self, nodelist): |
|---|
| 477 | n/a | # global: NAME (',' NAME)* |
|---|
| 478 | 8 | names = [] |
|---|
| 479 | 20 | for i in range(1, len(nodelist), 2): |
|---|
| 480 | 12 | names.append(nodelist[i][1]) |
|---|
| 481 | 8 | return Global(names, lineno=nodelist[0][2]) |
|---|
| 482 | n/a | |
|---|
| 483 | 2 | def exec_stmt(self, nodelist): |
|---|
| 484 | n/a | # exec_stmt: 'exec' expr ['in' expr [',' expr]] |
|---|
| 485 | 18 | expr1 = self.com_node(nodelist[1]) |
|---|
| 486 | 18 | if len(nodelist) >= 4: |
|---|
| 487 | 15 | expr2 = self.com_node(nodelist[3]) |
|---|
| 488 | 15 | if len(nodelist) >= 6: |
|---|
| 489 | 1 | expr3 = self.com_node(nodelist[5]) |
|---|
| 490 | n/a | else: |
|---|
| 491 | 14 | expr3 = None |
|---|
| 492 | n/a | else: |
|---|
| 493 | 3 | expr2 = expr3 = None |
|---|
| 494 | n/a | |
|---|
| 495 | 18 | return Exec(expr1, expr2, expr3, lineno=nodelist[0][2]) |
|---|
| 496 | n/a | |
|---|
| 497 | 2 | def assert_stmt(self, nodelist): |
|---|
| 498 | n/a | # 'assert': test, [',' test] |
|---|
| 499 | 3 | expr1 = self.com_node(nodelist[1]) |
|---|
| 500 | 3 | if (len(nodelist) == 4): |
|---|
| 501 | 1 | expr2 = self.com_node(nodelist[3]) |
|---|
| 502 | n/a | else: |
|---|
| 503 | 2 | expr2 = None |
|---|
| 504 | 3 | return Assert(expr1, expr2, lineno=nodelist[0][2]) |
|---|
| 505 | n/a | |
|---|
| 506 | 2 | def if_stmt(self, nodelist): |
|---|
| 507 | n/a | # if: test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
|---|
| 508 | 125 | tests = [] |
|---|
| 509 | 252 | for i in range(0, len(nodelist) - 3, 4): |
|---|
| 510 | 127 | testNode = self.com_node(nodelist[i + 1]) |
|---|
| 511 | 127 | suiteNode = self.com_node(nodelist[i + 3]) |
|---|
| 512 | 127 | tests.append((testNode, suiteNode)) |
|---|
| 513 | n/a | |
|---|
| 514 | 125 | if len(nodelist) % 4 == 3: |
|---|
| 515 | 28 | elseNode = self.com_node(nodelist[-1]) |
|---|
| 516 | n/a | ## elseNode.lineno = nodelist[-1][1][2] |
|---|
| 517 | n/a | else: |
|---|
| 518 | 97 | elseNode = None |
|---|
| 519 | 125 | return If(tests, elseNode, lineno=nodelist[0][2]) |
|---|
| 520 | n/a | |
|---|
| 521 | 2 | def while_stmt(self, nodelist): |
|---|
| 522 | n/a | # 'while' test ':' suite ['else' ':' suite] |
|---|
| 523 | n/a | |
|---|
| 524 | 12 | testNode = self.com_node(nodelist[1]) |
|---|
| 525 | 12 | bodyNode = self.com_node(nodelist[3]) |
|---|
| 526 | n/a | |
|---|
| 527 | 12 | if len(nodelist) > 4: |
|---|
| 528 | 0 | elseNode = self.com_node(nodelist[6]) |
|---|
| 529 | n/a | else: |
|---|
| 530 | 12 | elseNode = None |
|---|
| 531 | n/a | |
|---|
| 532 | 12 | return While(testNode, bodyNode, elseNode, lineno=nodelist[0][2]) |
|---|
| 533 | n/a | |
|---|
| 534 | 2 | def for_stmt(self, nodelist): |
|---|
| 535 | n/a | # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
|---|
| 536 | n/a | |
|---|
| 537 | 93 | assignNode = self.com_assign(nodelist[1], OP_ASSIGN) |
|---|
| 538 | 93 | listNode = self.com_node(nodelist[3]) |
|---|
| 539 | 93 | bodyNode = self.com_node(nodelist[5]) |
|---|
| 540 | n/a | |
|---|
| 541 | 93 | if len(nodelist) > 8: |
|---|
| 542 | 0 | elseNode = self.com_node(nodelist[8]) |
|---|
| 543 | n/a | else: |
|---|
| 544 | 93 | elseNode = None |
|---|
| 545 | n/a | |
|---|
| 546 | 93 | return For(assignNode, listNode, bodyNode, elseNode, |
|---|
| 547 | 93 | lineno=nodelist[0][2]) |
|---|
| 548 | n/a | |
|---|
| 549 | 2 | def try_stmt(self, nodelist): |
|---|
| 550 | 146 | return self.com_try_except_finally(nodelist) |
|---|
| 551 | n/a | |
|---|
| 552 | 2 | def with_stmt(self, nodelist): |
|---|
| 553 | 7 | return self.com_with(nodelist) |
|---|
| 554 | n/a | |
|---|
| 555 | 2 | def with_var(self, nodelist): |
|---|
| 556 | 0 | return self.com_with_var(nodelist) |
|---|
| 557 | n/a | |
|---|
| 558 | 2 | def suite(self, nodelist): |
|---|
| 559 | n/a | # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
|---|
| 560 | 1706 | if len(nodelist) == 1: |
|---|
| 561 | 116 | return self.com_stmt(nodelist[0]) |
|---|
| 562 | n/a | |
|---|
| 563 | 1590 | stmts = [] |
|---|
| 564 | 10840 | for node in nodelist: |
|---|
| 565 | 9250 | if node[0] == symbol.stmt: |
|---|
| 566 | 4480 | self.com_append_stmt(stmts, node) |
|---|
| 567 | 1590 | return Stmt(stmts) |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | # -------------------------------------------------------------- |
|---|
| 570 | n/a | # |
|---|
| 571 | n/a | # EXPRESSION NODES (invoked by com_node()) |
|---|
| 572 | n/a | # |
|---|
| 573 | n/a | |
|---|
| 574 | 2 | def testlist(self, nodelist): |
|---|
| 575 | n/a | # testlist: expr (',' expr)* [','] |
|---|
| 576 | n/a | # testlist_safe: test [(',' test)+ [',']] |
|---|
| 577 | n/a | # exprlist: expr (',' expr)* [','] |
|---|
| 578 | 3600 | return self.com_binary(Tuple, nodelist) |
|---|
| 579 | n/a | |
|---|
| 580 | 2 | testlist_safe = testlist # XXX |
|---|
| 581 | 2 | testlist1 = testlist |
|---|
| 582 | 2 | exprlist = testlist |
|---|
| 583 | n/a | |
|---|
| 584 | 2 | def testlist_comp(self, nodelist): |
|---|
| 585 | n/a | # test ( comp_for | (',' test)* [','] ) |
|---|
| 586 | 534 | assert nodelist[0][0] == symbol.test |
|---|
| 587 | 534 | if len(nodelist) == 2 and nodelist[1][0] == symbol.comp_for: |
|---|
| 588 | 1 | test = self.com_node(nodelist[0]) |
|---|
| 589 | 1 | return self.com_generator_expression(test, nodelist[1]) |
|---|
| 590 | 533 | return self.testlist(nodelist) |
|---|
| 591 | n/a | |
|---|
| 592 | 2 | def test(self, nodelist): |
|---|
| 593 | n/a | # or_test ['if' or_test 'else' test] | lambdef |
|---|
| 594 | 10618 | if len(nodelist) == 1 and nodelist[0][0] == symbol.lambdef: |
|---|
| 595 | 18 | return self.lambdef(nodelist[0]) |
|---|
| 596 | 10600 | then = self.com_node(nodelist[0]) |
|---|
| 597 | 10598 | if len(nodelist) > 1: |
|---|
| 598 | 0 | assert len(nodelist) == 5 |
|---|
| 599 | 0 | assert nodelist[1][1] == 'if' |
|---|
| 600 | 0 | assert nodelist[3][1] == 'else' |
|---|
| 601 | 0 | test = self.com_node(nodelist[2]) |
|---|
| 602 | 0 | else_ = self.com_node(nodelist[4]) |
|---|
| 603 | 0 | return IfExp(test, then, else_, lineno=nodelist[1][2]) |
|---|
| 604 | 10598 | return then |
|---|
| 605 | n/a | |
|---|
| 606 | 2 | def or_test(self, nodelist): |
|---|
| 607 | n/a | # and_test ('or' and_test)* | lambdef |
|---|
| 608 | 10659 | if len(nodelist) == 1 and nodelist[0][0] == symbol.lambdef: |
|---|
| 609 | 0 | return self.lambdef(nodelist[0]) |
|---|
| 610 | 10659 | return self.com_binary(Or, nodelist) |
|---|
| 611 | 2 | old_test = or_test |
|---|
| 612 | n/a | |
|---|
| 613 | 2 | def and_test(self, nodelist): |
|---|
| 614 | n/a | # not_test ('and' not_test)* |
|---|
| 615 | 10644 | return self.com_binary(And, nodelist) |
|---|
| 616 | n/a | |
|---|
| 617 | 2 | def not_test(self, nodelist): |
|---|
| 618 | n/a | # 'not' not_test | comparison |
|---|
| 619 | 10688 | result = self.com_node(nodelist[-1]) |
|---|
| 620 | 10686 | if len(nodelist) == 2: |
|---|
| 621 | 34 | return Not(result, lineno=nodelist[0][2]) |
|---|
| 622 | 10652 | return result |
|---|
| 623 | n/a | |
|---|
| 624 | 2 | def comparison(self, nodelist): |
|---|
| 625 | n/a | # comparison: expr (comp_op expr)* |
|---|
| 626 | 10654 | node = self.com_node(nodelist[0]) |
|---|
| 627 | 10652 | if len(nodelist) == 1: |
|---|
| 628 | 10413 | return node |
|---|
| 629 | n/a | |
|---|
| 630 | 239 | results = [] |
|---|
| 631 | 481 | for i in range(2, len(nodelist), 2): |
|---|
| 632 | 242 | nl = nodelist[i-1] |
|---|
| 633 | n/a | |
|---|
| 634 | n/a | # comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
|---|
| 635 | n/a | # | 'in' | 'not' 'in' | 'is' | 'is' 'not' |
|---|
| 636 | 242 | n = nl[1] |
|---|
| 637 | 242 | if n[0] == token.NAME: |
|---|
| 638 | 137 | type = n[1] |
|---|
| 639 | 137 | if len(nl) == 3: |
|---|
| 640 | 17 | if type == 'not': |
|---|
| 641 | 13 | type = 'not in' |
|---|
| 642 | n/a | else: |
|---|
| 643 | 4 | type = 'is not' |
|---|
| 644 | n/a | else: |
|---|
| 645 | 105 | type = _cmp_types[n[0]] |
|---|
| 646 | n/a | |
|---|
| 647 | 242 | lineno = nl[1][2] |
|---|
| 648 | 242 | results.append((type, self.com_node(nodelist[i]))) |
|---|
| 649 | n/a | |
|---|
| 650 | n/a | # we need a special "compare" node so that we can distinguish |
|---|
| 651 | n/a | # 3 < x < 5 from (3 < x) < 5 |
|---|
| 652 | n/a | # the two have very different semantics and results (note that the |
|---|
| 653 | n/a | # latter form is always true) |
|---|
| 654 | n/a | |
|---|
| 655 | 239 | return Compare(node, results, lineno=lineno) |
|---|
| 656 | n/a | |
|---|
| 657 | 2 | def expr(self, nodelist): |
|---|
| 658 | n/a | # xor_expr ('|' xor_expr)* |
|---|
| 659 | 10914 | return self.com_binary(Bitor, nodelist) |
|---|
| 660 | n/a | |
|---|
| 661 | 2 | def xor_expr(self, nodelist): |
|---|
| 662 | n/a | # xor_expr ('^' xor_expr)* |
|---|
| 663 | 10915 | return self.com_binary(Bitxor, nodelist) |
|---|
| 664 | n/a | |
|---|
| 665 | 2 | def and_expr(self, nodelist): |
|---|
| 666 | n/a | # xor_expr ('&' xor_expr)* |
|---|
| 667 | 10916 | return self.com_binary(Bitand, nodelist) |
|---|
| 668 | n/a | |
|---|
| 669 | 2 | def shift_expr(self, nodelist): |
|---|
| 670 | n/a | # shift_expr ('<<'|'>>' shift_expr)* |
|---|
| 671 | 10917 | node = self.com_node(nodelist[0]) |
|---|
| 672 | 10925 | for i in range(2, len(nodelist), 2): |
|---|
| 673 | 10 | right = self.com_node(nodelist[i]) |
|---|
| 674 | 10 | if nodelist[i-1][0] == token.LEFTSHIFT: |
|---|
| 675 | 6 | node = LeftShift([node, right], lineno=nodelist[1][2]) |
|---|
| 676 | 4 | elif nodelist[i-1][0] == token.RIGHTSHIFT: |
|---|
| 677 | 4 | node = RightShift([node, right], lineno=nodelist[1][2]) |
|---|
| 678 | n/a | else: |
|---|
| 679 | 0 | raise ValueError, "unexpected token: %s" % nodelist[i-1][0] |
|---|
| 680 | 10915 | return node |
|---|
| 681 | n/a | |
|---|
| 682 | 2 | def arith_expr(self, nodelist): |
|---|
| 683 | 10927 | node = self.com_node(nodelist[0]) |
|---|
| 684 | 11027 | for i in range(2, len(nodelist), 2): |
|---|
| 685 | 102 | right = self.com_node(nodelist[i]) |
|---|
| 686 | 102 | if nodelist[i-1][0] == token.PLUS: |
|---|
| 687 | 97 | node = Add([node, right], lineno=nodelist[1][2]) |
|---|
| 688 | 5 | elif nodelist[i-1][0] == token.MINUS: |
|---|
| 689 | 5 | node = Sub([node, right], lineno=nodelist[1][2]) |
|---|
| 690 | n/a | else: |
|---|
| 691 | 0 | raise ValueError, "unexpected token: %s" % nodelist[i-1][0] |
|---|
| 692 | 10925 | return node |
|---|
| 693 | n/a | |
|---|
| 694 | 2 | def term(self, nodelist): |
|---|
| 695 | 11029 | node = self.com_node(nodelist[0]) |
|---|
| 696 | 11172 | for i in range(2, len(nodelist), 2): |
|---|
| 697 | 145 | right = self.com_node(nodelist[i]) |
|---|
| 698 | 145 | t = nodelist[i-1][0] |
|---|
| 699 | 145 | if t == token.STAR: |
|---|
| 700 | 47 | node = Mul([node, right]) |
|---|
| 701 | 98 | elif t == token.SLASH: |
|---|
| 702 | 4 | node = Div([node, right]) |
|---|
| 703 | 94 | elif t == token.PERCENT: |
|---|
| 704 | 81 | node = Mod([node, right]) |
|---|
| 705 | 13 | elif t == token.DOUBLESLASH: |
|---|
| 706 | 13 | node = FloorDiv([node, right]) |
|---|
| 707 | n/a | else: |
|---|
| 708 | 0 | raise ValueError, "unexpected token: %s" % t |
|---|
| 709 | 145 | node.lineno = nodelist[1][2] |
|---|
| 710 | 11027 | return node |
|---|
| 711 | n/a | |
|---|
| 712 | 2 | def factor(self, nodelist): |
|---|
| 713 | 11239 | elt = nodelist[0] |
|---|
| 714 | 11239 | t = elt[0] |
|---|
| 715 | 11239 | node = self.lookup_node(nodelist[-1])(nodelist[-1][1:]) |
|---|
| 716 | n/a | # need to handle (unary op)constant here... |
|---|
| 717 | 11237 | if t == token.PLUS: |
|---|
| 718 | 4 | return UnaryAdd(node, lineno=elt[2]) |
|---|
| 719 | 11233 | elif t == token.MINUS: |
|---|
| 720 | 57 | return UnarySub(node, lineno=elt[2]) |
|---|
| 721 | 11176 | elif t == token.TILDE: |
|---|
| 722 | 0 | node = Invert(node, lineno=elt[2]) |
|---|
| 723 | 11176 | return node |
|---|
| 724 | n/a | |
|---|
| 725 | 2 | def power(self, nodelist): |
|---|
| 726 | n/a | # power: atom trailer* ('**' factor)* |
|---|
| 727 | 11178 | node = self.com_node(nodelist[0]) |
|---|
| 728 | 17001 | for i in range(1, len(nodelist)): |
|---|
| 729 | 5829 | elt = nodelist[i] |
|---|
| 730 | 5829 | if elt[0] == token.DOUBLESTAR: |
|---|
| 731 | 4 | return Power([node, self.com_node(nodelist[i+1])], |
|---|
| 732 | 4 | lineno=elt[2]) |
|---|
| 733 | n/a | |
|---|
| 734 | 5825 | node = self.com_apply_trailer(node, elt) |
|---|
| 735 | n/a | |
|---|
| 736 | 11172 | return node |
|---|
| 737 | n/a | |
|---|
| 738 | 2 | def atom(self, nodelist): |
|---|
| 739 | 11560 | return self._atom_dispatch[nodelist[0][0]](nodelist) |
|---|
| 740 | n/a | |
|---|
| 741 | 2 | def atom_lpar(self, nodelist): |
|---|
| 742 | 549 | if nodelist[1][0] == token.RPAR: |
|---|
| 743 | 15 | return Tuple((), lineno=nodelist[0][2]) |
|---|
| 744 | 534 | return self.com_node(nodelist[1]) |
|---|
| 745 | n/a | |
|---|
| 746 | 2 | def atom_lsqb(self, nodelist): |
|---|
| 747 | 337 | if nodelist[1][0] == token.RSQB: |
|---|
| 748 | 48 | return List((), lineno=nodelist[0][2]) |
|---|
| 749 | 289 | return self.com_list_constructor(nodelist[1]) |
|---|
| 750 | n/a | |
|---|
| 751 | 2 | def atom_lbrace(self, nodelist): |
|---|
| 752 | 132 | if nodelist[1][0] == token.RBRACE: |
|---|
| 753 | 44 | return Dict((), lineno=nodelist[0][2]) |
|---|
| 754 | 88 | return self.com_dictorsetmaker(nodelist[1]) |
|---|
| 755 | n/a | |
|---|
| 756 | 2 | def atom_backquote(self, nodelist): |
|---|
| 757 | 0 | return Backquote(self.com_node(nodelist[1])) |
|---|
| 758 | n/a | |
|---|
| 759 | 2 | def atom_number(self, nodelist): |
|---|
| 760 | n/a | ### need to verify this matches compile.c |
|---|
| 761 | 1799 | k = eval(nodelist[0][1]) |
|---|
| 762 | 1799 | return Const(k, lineno=nodelist[0][2]) |
|---|
| 763 | n/a | |
|---|
| 764 | 2 | def decode_literal(self, lit): |
|---|
| 765 | 1730 | if self.encoding: |
|---|
| 766 | n/a | # this is particularly fragile & a bit of a |
|---|
| 767 | n/a | # hack... changes in compile.c:parsestr and |
|---|
| 768 | n/a | # tokenizer.c must be reflected here. |
|---|
| 769 | 0 | if self.encoding not in ['utf-8', 'iso-8859-1']: |
|---|
| 770 | 0 | lit = unicode(lit, 'utf-8').encode(self.encoding) |
|---|
| 771 | 0 | return eval("# coding: %s\n%s" % (self.encoding, lit)) |
|---|
| 772 | n/a | else: |
|---|
| 773 | 1730 | return eval(lit) |
|---|
| 774 | n/a | |
|---|
| 775 | 2 | def atom_string(self, nodelist): |
|---|
| 776 | 1702 | k = '' |
|---|
| 777 | 3432 | for node in nodelist: |
|---|
| 778 | 1730 | k += self.decode_literal(node[1]) |
|---|
| 779 | 1702 | return Const(k, lineno=nodelist[0][2]) |
|---|
| 780 | n/a | |
|---|
| 781 | 2 | def atom_name(self, nodelist): |
|---|
| 782 | 7060 | return Name(nodelist[0][1], lineno=nodelist[0][2]) |
|---|
| 783 | n/a | |
|---|
| 784 | n/a | # -------------------------------------------------------------- |
|---|
| 785 | n/a | # |
|---|
| 786 | n/a | # INTERNAL PARSING UTILITIES |
|---|
| 787 | n/a | # |
|---|
| 788 | n/a | |
|---|
| 789 | n/a | # The use of com_node() introduces a lot of extra stack frames, |
|---|
| 790 | n/a | # enough to cause a stack overflow compiling test.test_parser with |
|---|
| 791 | n/a | # the standard interpreter recursionlimit. The com_node() is a |
|---|
| 792 | n/a | # convenience function that hides the dispatch details, but comes |
|---|
| 793 | n/a | # at a very high cost. It is more efficient to dispatch directly |
|---|
| 794 | n/a | # in the callers. In these cases, use lookup_node() and call the |
|---|
| 795 | n/a | # dispatched node directly. |
|---|
| 796 | n/a | |
|---|
| 797 | 2 | def lookup_node(self, node): |
|---|
| 798 | 90435 | return self._dispatch[node[0]] |
|---|
| 799 | n/a | |
|---|
| 800 | 2 | def com_node(self, node): |
|---|
| 801 | n/a | # Note: compile.c has handling in com_node for del_stmt, pass_stmt, |
|---|
| 802 | n/a | # break_stmt, stmt, small_stmt, flow_stmt, simple_stmt, |
|---|
| 803 | n/a | # and compound_stmt. |
|---|
| 804 | n/a | # We'll just dispatch them. |
|---|
| 805 | 85874 | return self._dispatch[node[0]](node[1:]) |
|---|
| 806 | n/a | |
|---|
| 807 | 2 | def com_NEWLINE(self, *args): |
|---|
| 808 | n/a | # A ';' at the end of a line can make a NEWLINE token appear |
|---|
| 809 | n/a | # here, Render it harmless. (genc discards ('discard', |
|---|
| 810 | n/a | # ('const', xxxx)) Nodes) |
|---|
| 811 | 0 | return Discard(Const(None)) |
|---|
| 812 | n/a | |
|---|
| 813 | 2 | def com_arglist(self, nodelist): |
|---|
| 814 | n/a | # varargslist: |
|---|
| 815 | n/a | # (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME) |
|---|
| 816 | n/a | # | fpdef ['=' test] (',' fpdef ['=' test])* [','] |
|---|
| 817 | n/a | # fpdef: NAME | '(' fplist ')' |
|---|
| 818 | n/a | # fplist: fpdef (',' fpdef)* [','] |
|---|
| 819 | 632 | names = [] |
|---|
| 820 | 632 | defaults = [] |
|---|
| 821 | 632 | flags = 0 |
|---|
| 822 | n/a | |
|---|
| 823 | 632 | i = 0 |
|---|
| 824 | 1583 | while i < len(nodelist): |
|---|
| 825 | 979 | node = nodelist[i] |
|---|
| 826 | 979 | if node[0] == token.STAR or node[0] == token.DOUBLESTAR: |
|---|
| 827 | 27 | if node[0] == token.STAR: |
|---|
| 828 | 27 | node = nodelist[i+1] |
|---|
| 829 | 27 | if node[0] == token.NAME: |
|---|
| 830 | 27 | names.append(node[1]) |
|---|
| 831 | 27 | flags = flags | CO_VARARGS |
|---|
| 832 | 27 | i = i + 3 |
|---|
| 833 | n/a | |
|---|
| 834 | 27 | if i < len(nodelist): |
|---|
| 835 | n/a | # should be DOUBLESTAR |
|---|
| 836 | 5 | t = nodelist[i][0] |
|---|
| 837 | 5 | if t == token.DOUBLESTAR: |
|---|
| 838 | 5 | node = nodelist[i+1] |
|---|
| 839 | n/a | else: |
|---|
| 840 | 0 | raise ValueError, "unexpected token: %s" % t |
|---|
| 841 | 5 | names.append(node[1]) |
|---|
| 842 | 5 | flags = flags | CO_VARKEYWORDS |
|---|
| 843 | n/a | |
|---|
| 844 | 27 | break |
|---|
| 845 | n/a | |
|---|
| 846 | n/a | # fpdef: NAME | '(' fplist ')' |
|---|
| 847 | 952 | names.append(self.com_fpdef(node)) |
|---|
| 848 | n/a | |
|---|
| 849 | 952 | i = i + 1 |
|---|
| 850 | 952 | if i < len(nodelist) and nodelist[i][0] == token.EQUAL: |
|---|
| 851 | 44 | defaults.append(self.com_node(nodelist[i + 1])) |
|---|
| 852 | 44 | i = i + 2 |
|---|
| 853 | 908 | elif len(defaults): |
|---|
| 854 | n/a | # we have already seen an argument with default, but here |
|---|
| 855 | n/a | # came one without |
|---|
| 856 | 1 | raise SyntaxError, "non-default argument follows default argument" |
|---|
| 857 | n/a | |
|---|
| 858 | n/a | # skip the comma |
|---|
| 859 | 951 | i = i + 1 |
|---|
| 860 | n/a | |
|---|
| 861 | 631 | return names, defaults, flags |
|---|
| 862 | n/a | |
|---|
| 863 | 2 | def com_fpdef(self, node): |
|---|
| 864 | n/a | # fpdef: NAME | '(' fplist ')' |
|---|
| 865 | 952 | if node[1][0] == token.LPAR: |
|---|
| 866 | 0 | return self.com_fplist(node[2]) |
|---|
| 867 | 952 | return node[1][1] |
|---|
| 868 | n/a | |
|---|
| 869 | 2 | def com_fplist(self, node): |
|---|
| 870 | n/a | # fplist: fpdef (',' fpdef)* [','] |
|---|
| 871 | 0 | if len(node) == 2: |
|---|
| 872 | 0 | return self.com_fpdef(node[1]) |
|---|
| 873 | 0 | list = [] |
|---|
| 874 | 0 | for i in range(1, len(node), 2): |
|---|
| 875 | 0 | list.append(self.com_fpdef(node[i])) |
|---|
| 876 | 0 | return tuple(list) |
|---|
| 877 | n/a | |
|---|
| 878 | 2 | def com_dotted_name(self, node): |
|---|
| 879 | n/a | # String together the dotted names and return the string |
|---|
| 880 | 77 | name = "" |
|---|
| 881 | 185 | for n in node: |
|---|
| 882 | 108 | if type(n) == type(()) and n[0] == 1: |
|---|
| 883 | 83 | name = name + n[1] + '.' |
|---|
| 884 | 77 | return name[:-1] |
|---|
| 885 | n/a | |
|---|
| 886 | 2 | def com_dotted_as_name(self, node): |
|---|
| 887 | 58 | assert node[0] == symbol.dotted_as_name |
|---|
| 888 | 58 | node = node[1:] |
|---|
| 889 | 58 | dot = self.com_dotted_name(node[0][1:]) |
|---|
| 890 | 58 | if len(node) == 1: |
|---|
| 891 | 52 | return dot, None |
|---|
| 892 | 6 | assert node[1][1] == 'as' |
|---|
| 893 | 6 | assert node[2][0] == token.NAME |
|---|
| 894 | 6 | return dot, node[2][1] |
|---|
| 895 | n/a | |
|---|
| 896 | 2 | def com_dotted_as_names(self, node): |
|---|
| 897 | 50 | assert node[0] == symbol.dotted_as_names |
|---|
| 898 | 50 | node = node[1:] |
|---|
| 899 | 50 | names = [self.com_dotted_as_name(node[0])] |
|---|
| 900 | 58 | for i in range(2, len(node), 2): |
|---|
| 901 | 8 | names.append(self.com_dotted_as_name(node[i])) |
|---|
| 902 | 50 | return names |
|---|
| 903 | n/a | |
|---|
| 904 | 2 | def com_import_as_name(self, node): |
|---|
| 905 | 20 | assert node[0] == symbol.import_as_name |
|---|
| 906 | 20 | node = node[1:] |
|---|
| 907 | 20 | assert node[0][0] == token.NAME |
|---|
| 908 | 20 | if len(node) == 1: |
|---|
| 909 | 19 | return node[0][1], None |
|---|
| 910 | 1 | assert node[1][1] == 'as', node |
|---|
| 911 | 1 | assert node[2][0] == token.NAME |
|---|
| 912 | 1 | return node[0][1], node[2][1] |
|---|
| 913 | n/a | |
|---|
| 914 | 2 | def com_import_as_names(self, node): |
|---|
| 915 | 18 | assert node[0] == symbol.import_as_names |
|---|
| 916 | 18 | node = node[1:] |
|---|
| 917 | 18 | names = [self.com_import_as_name(node[0])] |
|---|
| 918 | 20 | for i in range(2, len(node), 2): |
|---|
| 919 | 2 | names.append(self.com_import_as_name(node[i])) |
|---|
| 920 | 18 | return names |
|---|
| 921 | n/a | |
|---|
| 922 | 2 | def com_bases(self, node): |
|---|
| 923 | 359 | bases = [] |
|---|
| 924 | 775 | for i in range(1, len(node), 2): |
|---|
| 925 | 416 | bases.append(self.com_node(node[i])) |
|---|
| 926 | 359 | return bases |
|---|
| 927 | n/a | |
|---|
| 928 | 2 | def com_try_except_finally(self, nodelist): |
|---|
| 929 | n/a | # ('try' ':' suite |
|---|
| 930 | n/a | # ((except_clause ':' suite)+ ['else' ':' suite] ['finally' ':' suite] |
|---|
| 931 | n/a | # | 'finally' ':' suite)) |
|---|
| 932 | n/a | |
|---|
| 933 | 146 | if nodelist[3][0] == token.NAME: |
|---|
| 934 | n/a | # first clause is a finally clause: only try-finally |
|---|
| 935 | 10 | return TryFinally(self.com_node(nodelist[2]), |
|---|
| 936 | 10 | self.com_node(nodelist[5]), |
|---|
| 937 | 10 | lineno=nodelist[0][2]) |
|---|
| 938 | n/a | |
|---|
| 939 | n/a | #tryexcept: [TryNode, [except_clauses], elseNode)] |
|---|
| 940 | 136 | clauses = [] |
|---|
| 941 | 136 | elseNode = None |
|---|
| 942 | 136 | finallyNode = None |
|---|
| 943 | 376 | for i in range(3, len(nodelist), 3): |
|---|
| 944 | 240 | node = nodelist[i] |
|---|
| 945 | 240 | if node[0] == symbol.except_clause: |
|---|
| 946 | n/a | # except_clause: 'except' [expr [(',' | 'as') expr]] */ |
|---|
| 947 | 138 | if len(node) > 2: |
|---|
| 948 | 130 | expr1 = self.com_node(node[2]) |
|---|
| 949 | 130 | if len(node) > 4: |
|---|
| 950 | 21 | expr2 = self.com_assign(node[4], OP_ASSIGN) |
|---|
| 951 | n/a | else: |
|---|
| 952 | 109 | expr2 = None |
|---|
| 953 | n/a | else: |
|---|
| 954 | 8 | expr1 = expr2 = None |
|---|
| 955 | 138 | clauses.append((expr1, expr2, self.com_node(nodelist[i+2]))) |
|---|
| 956 | n/a | |
|---|
| 957 | 240 | if node[0] == token.NAME: |
|---|
| 958 | 102 | if node[1] == 'else': |
|---|
| 959 | 99 | elseNode = self.com_node(nodelist[i+2]) |
|---|
| 960 | 3 | elif node[1] == 'finally': |
|---|
| 961 | 3 | finallyNode = self.com_node(nodelist[i+2]) |
|---|
| 962 | 136 | try_except = TryExcept(self.com_node(nodelist[2]), clauses, elseNode, |
|---|
| 963 | 136 | lineno=nodelist[0][2]) |
|---|
| 964 | 136 | if finallyNode: |
|---|
| 965 | 3 | return TryFinally(try_except, finallyNode, lineno=nodelist[0][2]) |
|---|
| 966 | n/a | else: |
|---|
| 967 | 133 | return try_except |
|---|
| 968 | n/a | |
|---|
| 969 | 2 | def com_with(self, nodelist): |
|---|
| 970 | n/a | # with_stmt: 'with' with_item (',' with_item)* ':' suite |
|---|
| 971 | 7 | body = self.com_node(nodelist[-1]) |
|---|
| 972 | 8 | for i in range(len(nodelist) - 3, 0, -2): |
|---|
| 973 | 8 | ret = self.com_with_item(nodelist[i], body, nodelist[0][2]) |
|---|
| 974 | 8 | if i == 1: |
|---|
| 975 | 7 | return ret |
|---|
| 976 | 1 | body = ret |
|---|
| 977 | n/a | |
|---|
| 978 | 2 | def com_with_item(self, nodelist, body, lineno): |
|---|
| 979 | n/a | # with_item: test ['as' expr] |
|---|
| 980 | 8 | if len(nodelist) == 4: |
|---|
| 981 | 4 | var = self.com_assign(nodelist[3], OP_ASSIGN) |
|---|
| 982 | n/a | else: |
|---|
| 983 | 4 | var = None |
|---|
| 984 | 8 | expr = self.com_node(nodelist[1]) |
|---|
| 985 | 8 | return With(expr, var, body, lineno=lineno) |
|---|
| 986 | n/a | |
|---|
| 987 | 2 | def com_augassign_op(self, node): |
|---|
| 988 | 19 | assert node[0] == symbol.augassign |
|---|
| 989 | 19 | return node[1] |
|---|
| 990 | n/a | |
|---|
| 991 | 2 | def com_augassign(self, node): |
|---|
| 992 | n/a | """Return node suitable for lvalue of augmented assignment |
|---|
| 993 | n/a | |
|---|
| 994 | n/a | Names, slices, and attributes are the only allowable nodes. |
|---|
| 995 | n/a | """ |
|---|
| 996 | 19 | l = self.com_node(node) |
|---|
| 997 | 19 | if l.__class__ in (Name, Slice, Subscript, Getattr): |
|---|
| 998 | 19 | return l |
|---|
| 999 | 0 | raise SyntaxError, "can't assign to %s" % l.__class__.__name__ |
|---|
| 1000 | n/a | |
|---|
| 1001 | 2 | def com_assign(self, node, assigning): |
|---|
| 1002 | n/a | # return a node suitable for use as an "lvalue" |
|---|
| 1003 | n/a | # loop to avoid trivial recursion |
|---|
| 1004 | 1337 | while 1: |
|---|
| 1005 | 18099 | t = node[0] |
|---|
| 1006 | 18099 | if t in (symbol.exprlist, symbol.testlist, symbol.testlist_safe, symbol.testlist_comp): |
|---|
| 1007 | 1225 | if len(node) > 2: |
|---|
| 1008 | 43 | return self.com_assign_tuple(node, assigning) |
|---|
| 1009 | 1182 | node = node[1] |
|---|
| 1010 | 16874 | elif t in _assign_types: |
|---|
| 1011 | 14648 | if len(node) > 2: |
|---|
| 1012 | 0 | raise SyntaxError, "can't assign to operator" |
|---|
| 1013 | 14648 | node = node[1] |
|---|
| 1014 | 2226 | elif t == symbol.power: |
|---|
| 1015 | 1304 | if node[1][0] != symbol.atom: |
|---|
| 1016 | 0 | raise SyntaxError, "can't assign to operator" |
|---|
| 1017 | 1304 | if len(node) > 2: |
|---|
| 1018 | 382 | primary = self.com_node(node[1]) |
|---|
| 1019 | 403 | for i in range(2, len(node)-1): |
|---|
| 1020 | 21 | ch = node[i] |
|---|
| 1021 | 21 | if ch[0] == token.DOUBLESTAR: |
|---|
| 1022 | 0 | raise SyntaxError, "can't assign to operator" |
|---|
| 1023 | 21 | primary = self.com_apply_trailer(primary, ch) |
|---|
| 1024 | 382 | return self.com_assign_trailer(primary, node[-1], |
|---|
| 1025 | 382 | assigning) |
|---|
| 1026 | 922 | node = node[1] |
|---|
| 1027 | 922 | elif t == symbol.atom: |
|---|
| 1028 | 922 | t = node[1][0] |
|---|
| 1029 | 922 | if t == token.LPAR: |
|---|
| 1030 | 10 | node = node[2] |
|---|
| 1031 | 10 | if node[0] == token.RPAR: |
|---|
| 1032 | 0 | raise SyntaxError, "can't assign to ()" |
|---|
| 1033 | 912 | elif t == token.LSQB: |
|---|
| 1034 | 1 | node = node[2] |
|---|
| 1035 | 1 | if node[0] == token.RSQB: |
|---|
| 1036 | 0 | raise SyntaxError, "can't assign to []" |
|---|
| 1037 | 1 | return self.com_assign_list(node, assigning) |
|---|
| 1038 | 911 | elif t == token.NAME: |
|---|
| 1039 | 911 | return self.com_assign_name(node[1], assigning) |
|---|
| 1040 | n/a | else: |
|---|
| 1041 | 0 | raise SyntaxError, "can't assign to literal" |
|---|
| 1042 | n/a | else: |
|---|
| 1043 | 0 | raise SyntaxError, "bad assignment (%s)" % t |
|---|
| 1044 | n/a | |
|---|
| 1045 | 2 | def com_assign_tuple(self, node, assigning): |
|---|
| 1046 | 43 | assigns = [] |
|---|
| 1047 | 138 | for i in range(1, len(node), 2): |
|---|
| 1048 | 95 | assigns.append(self.com_assign(node[i], assigning)) |
|---|
| 1049 | 43 | return AssTuple(assigns, lineno=extractLineNo(node)) |
|---|
| 1050 | n/a | |
|---|
| 1051 | 2 | def com_assign_list(self, node, assigning): |
|---|
| 1052 | 1 | assigns = [] |
|---|
| 1053 | 3 | for i in range(1, len(node), 2): |
|---|
| 1054 | 2 | if i + 1 < len(node): |
|---|
| 1055 | 1 | if node[i + 1][0] == symbol.list_for: |
|---|
| 1056 | 0 | raise SyntaxError, "can't assign to list comprehension" |
|---|
| 1057 | 1 | assert node[i + 1][0] == token.COMMA, node[i + 1] |
|---|
| 1058 | 2 | assigns.append(self.com_assign(node[i], assigning)) |
|---|
| 1059 | 1 | return AssList(assigns, lineno=extractLineNo(node)) |
|---|
| 1060 | n/a | |
|---|
| 1061 | 2 | def com_assign_name(self, node, assigning): |
|---|
| 1062 | 911 | return AssName(node[1], assigning, lineno=node[2]) |
|---|
| 1063 | n/a | |
|---|
| 1064 | 2 | def com_assign_trailer(self, primary, node, assigning): |
|---|
| 1065 | 382 | t = node[1][0] |
|---|
| 1066 | 382 | if t == token.DOT: |
|---|
| 1067 | 339 | return self.com_assign_attr(primary, node[2], assigning) |
|---|
| 1068 | 43 | if t == token.LSQB: |
|---|
| 1069 | 43 | return self.com_subscriptlist(primary, node[2], assigning) |
|---|
| 1070 | 0 | if t == token.LPAR: |
|---|
| 1071 | 0 | raise SyntaxError, "can't assign to function call" |
|---|
| 1072 | 0 | raise SyntaxError, "unknown trailer type: %s" % t |
|---|
| 1073 | n/a | |
|---|
| 1074 | 2 | def com_assign_attr(self, primary, node, assigning): |
|---|
| 1075 | 339 | return AssAttr(primary, node[1], assigning, lineno=node[-1]) |
|---|
| 1076 | n/a | |
|---|
| 1077 | 2 | def com_binary(self, constructor, nodelist): |
|---|
| 1078 | n/a | "Compile 'NODE (OP NODE)*' into (type, [ node1, ..., nodeN ])." |
|---|
| 1079 | 57648 | l = len(nodelist) |
|---|
| 1080 | 57648 | if l == 1: |
|---|
| 1081 | 57109 | n = nodelist[0] |
|---|
| 1082 | 57109 | return self.lookup_node(n)(n[1:]) |
|---|
| 1083 | 539 | items = [] |
|---|
| 1084 | 1902 | for i in range(0, l, 2): |
|---|
| 1085 | 1363 | n = nodelist[i] |
|---|
| 1086 | 1363 | items.append(self.lookup_node(n)(n[1:])) |
|---|
| 1087 | 539 | return constructor(items, lineno=extractLineNo(nodelist)) |
|---|
| 1088 | n/a | |
|---|
| 1089 | 2 | def com_stmt(self, node): |
|---|
| 1090 | 9967 | result = self.lookup_node(node)(node[1:]) |
|---|
| 1091 | 9961 | assert result is not None |
|---|
| 1092 | 9961 | if isinstance(result, Stmt): |
|---|
| 1093 | 5137 | return result |
|---|
| 1094 | 4824 | return Stmt([result]) |
|---|
| 1095 | n/a | |
|---|
| 1096 | 2 | def com_append_stmt(self, stmts, node): |
|---|
| 1097 | 8106 | result = self.lookup_node(node)(node[1:]) |
|---|
| 1098 | 8101 | assert result is not None |
|---|
| 1099 | 8101 | if isinstance(result, Stmt): |
|---|
| 1100 | 8101 | stmts.extend(result.nodes) |
|---|
| 1101 | n/a | else: |
|---|
| 1102 | 0 | stmts.append(result) |
|---|
| 1103 | n/a | |
|---|
| 1104 | 2 | def com_list_constructor(self, nodelist): |
|---|
| 1105 | n/a | # listmaker: test ( list_for | (',' test)* [','] ) |
|---|
| 1106 | 289 | values = [] |
|---|
| 1107 | 1412 | for i in range(1, len(nodelist)): |
|---|
| 1108 | 1137 | if nodelist[i][0] == symbol.list_for: |
|---|
| 1109 | 14 | assert len(nodelist[i:]) == 1 |
|---|
| 1110 | 14 | return self.com_list_comprehension(values[0], |
|---|
| 1111 | 14 | nodelist[i]) |
|---|
| 1112 | 1123 | elif nodelist[i][0] == token.COMMA: |
|---|
| 1113 | 427 | continue |
|---|
| 1114 | 696 | values.append(self.com_node(nodelist[i])) |
|---|
| 1115 | 275 | return List(values, lineno=values[0].lineno) |
|---|
| 1116 | n/a | |
|---|
| 1117 | 2 | def com_list_comprehension(self, expr, node): |
|---|
| 1118 | 14 | return self.com_comprehension(expr, None, node, 'list') |
|---|
| 1119 | n/a | |
|---|
| 1120 | 2 | def com_comprehension(self, expr1, expr2, node, type): |
|---|
| 1121 | n/a | # list_iter: list_for | list_if |
|---|
| 1122 | n/a | # list_for: 'for' exprlist 'in' testlist [list_iter] |
|---|
| 1123 | n/a | # list_if: 'if' test [list_iter] |
|---|
| 1124 | n/a | |
|---|
| 1125 | n/a | # XXX should raise SyntaxError for assignment |
|---|
| 1126 | n/a | # XXX(avassalotti) Set and dict comprehensions should have generator |
|---|
| 1127 | n/a | # semantics. In other words, they shouldn't leak |
|---|
| 1128 | n/a | # variables outside of the comprehension's scope. |
|---|
| 1129 | n/a | |
|---|
| 1130 | 20 | lineno = node[1][2] |
|---|
| 1131 | 20 | fors = [] |
|---|
| 1132 | 50 | while node: |
|---|
| 1133 | 30 | t = node[1][1] |
|---|
| 1134 | 30 | if t == 'for': |
|---|
| 1135 | 22 | assignNode = self.com_assign(node[2], OP_ASSIGN) |
|---|
| 1136 | 22 | compNode = self.com_node(node[4]) |
|---|
| 1137 | 22 | newfor = ListCompFor(assignNode, compNode, []) |
|---|
| 1138 | 22 | newfor.lineno = node[1][2] |
|---|
| 1139 | 22 | fors.append(newfor) |
|---|
| 1140 | 22 | if len(node) == 5: |
|---|
| 1141 | 14 | node = None |
|---|
| 1142 | 8 | elif type == 'list': |
|---|
| 1143 | 4 | node = self.com_list_iter(node[5]) |
|---|
| 1144 | n/a | else: |
|---|
| 1145 | 4 | node = self.com_comp_iter(node[5]) |
|---|
| 1146 | 8 | elif t == 'if': |
|---|
| 1147 | 8 | test = self.com_node(node[2]) |
|---|
| 1148 | 8 | newif = ListCompIf(test, lineno=node[1][2]) |
|---|
| 1149 | 8 | newfor.ifs.append(newif) |
|---|
| 1150 | 8 | if len(node) == 3: |
|---|
| 1151 | 6 | node = None |
|---|
| 1152 | 2 | elif type == 'list': |
|---|
| 1153 | 0 | node = self.com_list_iter(node[3]) |
|---|
| 1154 | n/a | else: |
|---|
| 1155 | 2 | node = self.com_comp_iter(node[3]) |
|---|
| 1156 | n/a | else: |
|---|
| 1157 | 0 | raise SyntaxError, \ |
|---|
| 1158 | 0 | ("unexpected comprehension element: %s %d" |
|---|
| 1159 | 0 | % (node, lineno)) |
|---|
| 1160 | 20 | if type == 'list': |
|---|
| 1161 | 14 | return ListComp(expr1, fors, lineno=lineno) |
|---|
| 1162 | 6 | elif type == 'set': |
|---|
| 1163 | 3 | return SetComp(expr1, fors, lineno=lineno) |
|---|
| 1164 | 3 | elif type == 'dict': |
|---|
| 1165 | 3 | return DictComp(expr1, expr2, fors, lineno=lineno) |
|---|
| 1166 | n/a | else: |
|---|
| 1167 | 0 | raise ValueError("unexpected comprehension type: " + repr(type)) |
|---|
| 1168 | n/a | |
|---|
| 1169 | 2 | def com_list_iter(self, node): |
|---|
| 1170 | 4 | assert node[0] == symbol.list_iter |
|---|
| 1171 | 4 | return node[1] |
|---|
| 1172 | n/a | |
|---|
| 1173 | 2 | def com_comp_iter(self, node): |
|---|
| 1174 | 9 | assert node[0] == symbol.comp_iter |
|---|
| 1175 | 9 | return node[1] |
|---|
| 1176 | n/a | |
|---|
| 1177 | 2 | def com_generator_expression(self, expr, node): |
|---|
| 1178 | n/a | # comp_iter: comp_for | comp_if |
|---|
| 1179 | n/a | # comp_for: 'for' exprlist 'in' test [comp_iter] |
|---|
| 1180 | n/a | # comp_if: 'if' test [comp_iter] |
|---|
| 1181 | n/a | |
|---|
| 1182 | 2 | lineno = node[1][2] |
|---|
| 1183 | 2 | fors = [] |
|---|
| 1184 | 7 | while node: |
|---|
| 1185 | 5 | t = node[1][1] |
|---|
| 1186 | 5 | if t == 'for': |
|---|
| 1187 | 3 | assignNode = self.com_assign(node[2], OP_ASSIGN) |
|---|
| 1188 | 3 | genNode = self.com_node(node[4]) |
|---|
| 1189 | 3 | newfor = GenExprFor(assignNode, genNode, [], |
|---|
| 1190 | 3 | lineno=node[1][2]) |
|---|
| 1191 | 3 | fors.append(newfor) |
|---|
| 1192 | 3 | if (len(node)) == 5: |
|---|
| 1193 | 1 | node = None |
|---|
| 1194 | n/a | else: |
|---|
| 1195 | 2 | node = self.com_comp_iter(node[5]) |
|---|
| 1196 | 2 | elif t == 'if': |
|---|
| 1197 | 2 | test = self.com_node(node[2]) |
|---|
| 1198 | 2 | newif = GenExprIf(test, lineno=node[1][2]) |
|---|
| 1199 | 2 | newfor.ifs.append(newif) |
|---|
| 1200 | 2 | if len(node) == 3: |
|---|
| 1201 | 1 | node = None |
|---|
| 1202 | n/a | else: |
|---|
| 1203 | 1 | node = self.com_comp_iter(node[3]) |
|---|
| 1204 | n/a | else: |
|---|
| 1205 | 0 | raise SyntaxError, \ |
|---|
| 1206 | 0 | ("unexpected generator expression element: %s %d" |
|---|
| 1207 | 0 | % (node, lineno)) |
|---|
| 1208 | 2 | fors[0].is_outmost = True |
|---|
| 1209 | 2 | return GenExpr(GenExprInner(expr, fors), lineno=lineno) |
|---|
| 1210 | n/a | |
|---|
| 1211 | 2 | def com_dictorsetmaker(self, nodelist): |
|---|
| 1212 | n/a | # dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) | |
|---|
| 1213 | n/a | # (test (comp_for | (',' test)* [','])) ) |
|---|
| 1214 | 88 | assert nodelist[0] == symbol.dictorsetmaker |
|---|
| 1215 | 88 | nodelist = nodelist[1:] |
|---|
| 1216 | 88 | if len(nodelist) == 1 or nodelist[1][0] == token.COMMA: |
|---|
| 1217 | n/a | # set literal |
|---|
| 1218 | 7 | items = [] |
|---|
| 1219 | 28 | for i in range(0, len(nodelist), 2): |
|---|
| 1220 | 21 | items.append(self.com_node(nodelist[i])) |
|---|
| 1221 | 7 | return Set(items, lineno=items[0].lineno) |
|---|
| 1222 | 81 | elif nodelist[1][0] == symbol.comp_for: |
|---|
| 1223 | n/a | # set comprehension |
|---|
| 1224 | 3 | expr = self.com_node(nodelist[0]) |
|---|
| 1225 | 3 | return self.com_comprehension(expr, None, nodelist[1], 'set') |
|---|
| 1226 | 78 | elif len(nodelist) > 3 and nodelist[3][0] == symbol.comp_for: |
|---|
| 1227 | n/a | # dict comprehension |
|---|
| 1228 | 3 | assert nodelist[1][0] == token.COLON |
|---|
| 1229 | 3 | key = self.com_node(nodelist[0]) |
|---|
| 1230 | 3 | value = self.com_node(nodelist[2]) |
|---|
| 1231 | 3 | return self.com_comprehension(key, value, nodelist[3], 'dict') |
|---|
| 1232 | n/a | else: |
|---|
| 1233 | n/a | # dict literal |
|---|
| 1234 | 75 | items = [] |
|---|
| 1235 | 230 | for i in range(0, len(nodelist), 4): |
|---|
| 1236 | 155 | items.append((self.com_node(nodelist[i]), |
|---|
| 1237 | 155 | self.com_node(nodelist[i+2]))) |
|---|
| 1238 | 75 | return Dict(items, lineno=items[0][0].lineno) |
|---|
| 1239 | n/a | |
|---|
| 1240 | 2 | def com_apply_trailer(self, primaryNode, nodelist): |
|---|
| 1241 | 5846 | t = nodelist[1][0] |
|---|
| 1242 | 5846 | if t == token.LPAR: |
|---|
| 1243 | 3062 | return self.com_call_function(primaryNode, nodelist[2]) |
|---|
| 1244 | 2784 | if t == token.DOT: |
|---|
| 1245 | 2640 | return self.com_select_member(primaryNode, nodelist[2]) |
|---|
| 1246 | 144 | if t == token.LSQB: |
|---|
| 1247 | 144 | return self.com_subscriptlist(primaryNode, nodelist[2], OP_APPLY) |
|---|
| 1248 | n/a | |
|---|
| 1249 | 0 | raise SyntaxError, 'unknown node type: %s' % t |
|---|
| 1250 | n/a | |
|---|
| 1251 | 2 | def com_select_member(self, primaryNode, nodelist): |
|---|
| 1252 | 2640 | if nodelist[0] != token.NAME: |
|---|
| 1253 | 0 | raise SyntaxError, "member must be a name" |
|---|
| 1254 | 2640 | return Getattr(primaryNode, nodelist[1], lineno=nodelist[2]) |
|---|
| 1255 | n/a | |
|---|
| 1256 | 2 | def com_call_function(self, primaryNode, nodelist): |
|---|
| 1257 | 3069 | if nodelist[0] == token.RPAR: |
|---|
| 1258 | 487 | return CallFunc(primaryNode, [], lineno=extractLineNo(nodelist)) |
|---|
| 1259 | 2582 | args = [] |
|---|
| 1260 | 2582 | kw = 0 |
|---|
| 1261 | 2582 | star_node = dstar_node = None |
|---|
| 1262 | 2582 | len_nodelist = len(nodelist) |
|---|
| 1263 | 2582 | i = 1 |
|---|
| 1264 | 6749 | while i < len_nodelist: |
|---|
| 1265 | 4169 | node = nodelist[i] |
|---|
| 1266 | n/a | |
|---|
| 1267 | 4169 | if node[0]==token.STAR: |
|---|
| 1268 | 13 | if star_node is not None: |
|---|
| 1269 | 0 | raise SyntaxError, 'already have the varargs indentifier' |
|---|
| 1270 | 13 | star_node = self.com_node(nodelist[i+1]) |
|---|
| 1271 | 13 | i = i + 3 |
|---|
| 1272 | 13 | continue |
|---|
| 1273 | 4156 | elif node[0]==token.DOUBLESTAR: |
|---|
| 1274 | 10 | if dstar_node is not None: |
|---|
| 1275 | 0 | raise SyntaxError, 'already have the kwargs indentifier' |
|---|
| 1276 | 10 | dstar_node = self.com_node(nodelist[i+1]) |
|---|
| 1277 | 10 | i = i + 3 |
|---|
| 1278 | 10 | continue |
|---|
| 1279 | n/a | |
|---|
| 1280 | n/a | # positional or named parameters |
|---|
| 1281 | 4146 | kw, result = self.com_argument(node, kw, star_node) |
|---|
| 1282 | n/a | |
|---|
| 1283 | 4144 | if len_nodelist != 2 and isinstance(result, GenExpr) \ |
|---|
| 1284 | 0 | and len(node) == 3 and node[2][0] == symbol.comp_for: |
|---|
| 1285 | n/a | # allow f(x for x in y), but reject f(x for x in y, 1) |
|---|
| 1286 | n/a | # should use f((x for x in y), 1) instead of f(x for x in y, 1) |
|---|
| 1287 | 0 | raise SyntaxError, 'generator expression needs parenthesis' |
|---|
| 1288 | n/a | |
|---|
| 1289 | 4144 | args.append(result) |
|---|
| 1290 | 4144 | i = i + 2 |
|---|
| 1291 | n/a | |
|---|
| 1292 | 2580 | return CallFunc(primaryNode, args, star_node, dstar_node, |
|---|
| 1293 | 2580 | lineno=extractLineNo(nodelist)) |
|---|
| 1294 | n/a | |
|---|
| 1295 | 2 | def com_argument(self, nodelist, kw, star_node): |
|---|
| 1296 | 4146 | if len(nodelist) == 3 and nodelist[2][0] == symbol.comp_for: |
|---|
| 1297 | 1 | test = self.com_node(nodelist[1]) |
|---|
| 1298 | 1 | return 0, self.com_generator_expression(test, nodelist[2]) |
|---|
| 1299 | 4145 | if len(nodelist) == 2: |
|---|
| 1300 | 4104 | if kw: |
|---|
| 1301 | 1 | raise SyntaxError, "non-keyword arg after keyword arg" |
|---|
| 1302 | 4103 | if star_node: |
|---|
| 1303 | 1 | raise SyntaxError, "only named arguments may follow *expression" |
|---|
| 1304 | 4102 | return 0, self.com_node(nodelist[1]) |
|---|
| 1305 | 41 | result = self.com_node(nodelist[3]) |
|---|
| 1306 | 41 | n = nodelist[1] |
|---|
| 1307 | 615 | while len(n) == 2 and n[0] != token.NAME: |
|---|
| 1308 | 574 | n = n[1] |
|---|
| 1309 | 41 | if n[0] != token.NAME: |
|---|
| 1310 | 0 | raise SyntaxError, "keyword can't be an expression (%s)"%n[0] |
|---|
| 1311 | 41 | node = Keyword(n[1], result, lineno=n[2]) |
|---|
| 1312 | 41 | return 1, node |
|---|
| 1313 | n/a | |
|---|
| 1314 | 2 | def com_subscriptlist(self, primary, nodelist, assigning): |
|---|
| 1315 | n/a | # slicing: simple_slicing | extended_slicing |
|---|
| 1316 | n/a | # simple_slicing: primary "[" short_slice "]" |
|---|
| 1317 | n/a | # extended_slicing: primary "[" slice_list "]" |
|---|
| 1318 | n/a | # slice_list: slice_item ("," slice_item)* [","] |
|---|
| 1319 | n/a | |
|---|
| 1320 | n/a | # backwards compat slice for '[i:j]' |
|---|
| 1321 | 187 | if len(nodelist) == 2: |
|---|
| 1322 | 187 | sub = nodelist[1] |
|---|
| 1323 | 187 | if (sub[1][0] == token.COLON or \ |
|---|
| 1324 | 170 | (len(sub) > 2 and sub[2][0] == token.COLON)) and \ |
|---|
| 1325 | 36 | sub[-1][0] != symbol.sliceop: |
|---|
| 1326 | 36 | return self.com_slice(primary, sub, assigning) |
|---|
| 1327 | n/a | |
|---|
| 1328 | 151 | subscripts = [] |
|---|
| 1329 | 302 | for i in range(1, len(nodelist), 2): |
|---|
| 1330 | 151 | subscripts.append(self.com_subscript(nodelist[i])) |
|---|
| 1331 | 151 | return Subscript(primary, assigning, subscripts, |
|---|
| 1332 | 151 | lineno=extractLineNo(nodelist)) |
|---|
| 1333 | n/a | |
|---|
| 1334 | 2 | def com_subscript(self, node): |
|---|
| 1335 | n/a | # slice_item: expression | proper_slice | ellipsis |
|---|
| 1336 | 151 | ch = node[1] |
|---|
| 1337 | 151 | t = ch[0] |
|---|
| 1338 | 151 | if t == token.DOT and node[2][0] == token.DOT: |
|---|
| 1339 | 0 | return Ellipsis() |
|---|
| 1340 | 151 | if t == token.COLON or len(node) > 2: |
|---|
| 1341 | 0 | return self.com_sliceobj(node) |
|---|
| 1342 | 151 | return self.com_node(ch) |
|---|
| 1343 | n/a | |
|---|
| 1344 | 2 | def com_sliceobj(self, node): |
|---|
| 1345 | n/a | # proper_slice: short_slice | long_slice |
|---|
| 1346 | n/a | # short_slice: [lower_bound] ":" [upper_bound] |
|---|
| 1347 | n/a | # long_slice: short_slice ":" [stride] |
|---|
| 1348 | n/a | # lower_bound: expression |
|---|
| 1349 | n/a | # upper_bound: expression |
|---|
| 1350 | n/a | # stride: expression |
|---|
| 1351 | n/a | # |
|---|
| 1352 | n/a | # Note: a stride may be further slicing... |
|---|
| 1353 | n/a | |
|---|
| 1354 | 0 | items = [] |
|---|
| 1355 | n/a | |
|---|
| 1356 | 0 | if node[1][0] == token.COLON: |
|---|
| 1357 | 0 | items.append(Const(None)) |
|---|
| 1358 | 0 | i = 2 |
|---|
| 1359 | n/a | else: |
|---|
| 1360 | 0 | items.append(self.com_node(node[1])) |
|---|
| 1361 | n/a | # i == 2 is a COLON |
|---|
| 1362 | 0 | i = 3 |
|---|
| 1363 | n/a | |
|---|
| 1364 | 0 | if i < len(node) and node[i][0] == symbol.test: |
|---|
| 1365 | 0 | items.append(self.com_node(node[i])) |
|---|
| 1366 | 0 | i = i + 1 |
|---|
| 1367 | n/a | else: |
|---|
| 1368 | 0 | items.append(Const(None)) |
|---|
| 1369 | n/a | |
|---|
| 1370 | n/a | # a short_slice has been built. look for long_slice now by looking |
|---|
| 1371 | n/a | # for strides... |
|---|
| 1372 | 0 | for j in range(i, len(node)): |
|---|
| 1373 | 0 | ch = node[j] |
|---|
| 1374 | 0 | if len(ch) == 2: |
|---|
| 1375 | 0 | items.append(Const(None)) |
|---|
| 1376 | n/a | else: |
|---|
| 1377 | 0 | items.append(self.com_node(ch[2])) |
|---|
| 1378 | 0 | return Sliceobj(items, lineno=extractLineNo(node)) |
|---|
| 1379 | n/a | |
|---|
| 1380 | 2 | def com_slice(self, primary, node, assigning): |
|---|
| 1381 | n/a | # short_slice: [lower_bound] ":" [upper_bound] |
|---|
| 1382 | 36 | lower = upper = None |
|---|
| 1383 | 36 | if len(node) == 3: |
|---|
| 1384 | 19 | if node[1][0] == token.COLON: |
|---|
| 1385 | 11 | upper = self.com_node(node[2]) |
|---|
| 1386 | n/a | else: |
|---|
| 1387 | 8 | lower = self.com_node(node[1]) |
|---|
| 1388 | 17 | elif len(node) == 4: |
|---|
| 1389 | 11 | lower = self.com_node(node[1]) |
|---|
| 1390 | 11 | upper = self.com_node(node[3]) |
|---|
| 1391 | 36 | return Slice(primary, assigning, lower, upper, |
|---|
| 1392 | 36 | lineno=extractLineNo(node)) |
|---|
| 1393 | n/a | |
|---|
| 1394 | 2 | def get_docstring(self, node, n=None): |
|---|
| 1395 | 6878 | if n is None: |
|---|
| 1396 | 6849 | n = node[0] |
|---|
| 1397 | 6849 | node = node[1:] |
|---|
| 1398 | 6878 | if n == symbol.suite: |
|---|
| 1399 | 1043 | if len(node) == 1: |
|---|
| 1400 | 71 | return self.get_docstring(node[0]) |
|---|
| 1401 | 2916 | for sub in node: |
|---|
| 1402 | 2916 | if sub[0] == symbol.stmt: |
|---|
| 1403 | 972 | return self.get_docstring(sub) |
|---|
| 1404 | 0 | return None |
|---|
| 1405 | 5835 | if n == symbol.file_input: |
|---|
| 1406 | 29 | for sub in node: |
|---|
| 1407 | 29 | if sub[0] == symbol.stmt: |
|---|
| 1408 | 29 | return self.get_docstring(sub) |
|---|
| 1409 | 0 | return None |
|---|
| 1410 | 5806 | if n == symbol.atom: |
|---|
| 1411 | 35 | if node[0][0] == token.STRING: |
|---|
| 1412 | 34 | s = '' |
|---|
| 1413 | 68 | for t in node: |
|---|
| 1414 | 34 | s = s + eval(t[1]) |
|---|
| 1415 | 34 | return s |
|---|
| 1416 | 1 | return None |
|---|
| 1417 | 5771 | if n == symbol.stmt or n == symbol.simple_stmt \ |
|---|
| 1418 | 3999 | or n == symbol.small_stmt: |
|---|
| 1419 | 2543 | return self.get_docstring(node[0]) |
|---|
| 1420 | 3228 | if n in _doc_nodes and len(node) == 1: |
|---|
| 1421 | 2191 | return self.get_docstring(node[0]) |
|---|
| 1422 | 1037 | return None |
|---|
| 1423 | n/a | |
|---|
| 1424 | n/a | |
|---|
| 1425 | n/a | _doc_nodes = [ |
|---|
| 1426 | 2 | symbol.expr_stmt, |
|---|
| 1427 | 2 | symbol.testlist, |
|---|
| 1428 | 2 | symbol.testlist_safe, |
|---|
| 1429 | 2 | symbol.test, |
|---|
| 1430 | 2 | symbol.or_test, |
|---|
| 1431 | 2 | symbol.and_test, |
|---|
| 1432 | 2 | symbol.not_test, |
|---|
| 1433 | 2 | symbol.comparison, |
|---|
| 1434 | 2 | symbol.expr, |
|---|
| 1435 | 2 | symbol.xor_expr, |
|---|
| 1436 | 2 | symbol.and_expr, |
|---|
| 1437 | 2 | symbol.shift_expr, |
|---|
| 1438 | 2 | symbol.arith_expr, |
|---|
| 1439 | 2 | symbol.term, |
|---|
| 1440 | 2 | symbol.factor, |
|---|
| 1441 | 2 | symbol.power, |
|---|
| 1442 | n/a | ] |
|---|
| 1443 | n/a | |
|---|
| 1444 | n/a | # comp_op: '<' | '>' | '=' | '>=' | '<=' | '<>' | '!=' | '==' |
|---|
| 1445 | n/a | # | 'in' | 'not' 'in' | 'is' | 'is' 'not' |
|---|
| 1446 | 2 | _cmp_types = { |
|---|
| 1447 | 2 | token.LESS : '<', |
|---|
| 1448 | 2 | token.GREATER : '>', |
|---|
| 1449 | 2 | token.EQEQUAL : '==', |
|---|
| 1450 | 2 | token.EQUAL : '==', |
|---|
| 1451 | 2 | token.LESSEQUAL : '<=', |
|---|
| 1452 | 2 | token.GREATEREQUAL : '>=', |
|---|
| 1453 | 2 | token.NOTEQUAL : '!=', |
|---|
| 1454 | n/a | } |
|---|
| 1455 | n/a | |
|---|
| 1456 | n/a | _legal_node_types = [ |
|---|
| 1457 | 2 | symbol.funcdef, |
|---|
| 1458 | 2 | symbol.classdef, |
|---|
| 1459 | 2 | symbol.stmt, |
|---|
| 1460 | 2 | symbol.small_stmt, |
|---|
| 1461 | 2 | symbol.flow_stmt, |
|---|
| 1462 | 2 | symbol.simple_stmt, |
|---|
| 1463 | 2 | symbol.compound_stmt, |
|---|
| 1464 | 2 | symbol.expr_stmt, |
|---|
| 1465 | 2 | symbol.print_stmt, |
|---|
| 1466 | 2 | symbol.del_stmt, |
|---|
| 1467 | 2 | symbol.pass_stmt, |
|---|
| 1468 | 2 | symbol.break_stmt, |
|---|
| 1469 | 2 | symbol.continue_stmt, |
|---|
| 1470 | 2 | symbol.return_stmt, |
|---|
| 1471 | 2 | symbol.raise_stmt, |
|---|
| 1472 | 2 | symbol.import_stmt, |
|---|
| 1473 | 2 | symbol.global_stmt, |
|---|
| 1474 | 2 | symbol.exec_stmt, |
|---|
| 1475 | 2 | symbol.assert_stmt, |
|---|
| 1476 | 2 | symbol.if_stmt, |
|---|
| 1477 | 2 | symbol.while_stmt, |
|---|
| 1478 | 2 | symbol.for_stmt, |
|---|
| 1479 | 2 | symbol.try_stmt, |
|---|
| 1480 | 2 | symbol.with_stmt, |
|---|
| 1481 | 2 | symbol.suite, |
|---|
| 1482 | 2 | symbol.testlist, |
|---|
| 1483 | 2 | symbol.testlist_safe, |
|---|
| 1484 | 2 | symbol.test, |
|---|
| 1485 | 2 | symbol.and_test, |
|---|
| 1486 | 2 | symbol.not_test, |
|---|
| 1487 | 2 | symbol.comparison, |
|---|
| 1488 | 2 | symbol.exprlist, |
|---|
| 1489 | 2 | symbol.expr, |
|---|
| 1490 | 2 | symbol.xor_expr, |
|---|
| 1491 | 2 | symbol.and_expr, |
|---|
| 1492 | 2 | symbol.shift_expr, |
|---|
| 1493 | 2 | symbol.arith_expr, |
|---|
| 1494 | 2 | symbol.term, |
|---|
| 1495 | 2 | symbol.factor, |
|---|
| 1496 | 2 | symbol.power, |
|---|
| 1497 | 2 | symbol.atom, |
|---|
| 1498 | n/a | ] |
|---|
| 1499 | n/a | |
|---|
| 1500 | 2 | if hasattr(symbol, 'yield_stmt'): |
|---|
| 1501 | 2 | _legal_node_types.append(symbol.yield_stmt) |
|---|
| 1502 | 2 | if hasattr(symbol, 'yield_expr'): |
|---|
| 1503 | 2 | _legal_node_types.append(symbol.yield_expr) |
|---|
| 1504 | n/a | |
|---|
| 1505 | n/a | _assign_types = [ |
|---|
| 1506 | 2 | symbol.test, |
|---|
| 1507 | 2 | symbol.or_test, |
|---|
| 1508 | 2 | symbol.and_test, |
|---|
| 1509 | 2 | symbol.not_test, |
|---|
| 1510 | 2 | symbol.comparison, |
|---|
| 1511 | 2 | symbol.expr, |
|---|
| 1512 | 2 | symbol.xor_expr, |
|---|
| 1513 | 2 | symbol.and_expr, |
|---|
| 1514 | 2 | symbol.shift_expr, |
|---|
| 1515 | 2 | symbol.arith_expr, |
|---|
| 1516 | 2 | symbol.term, |
|---|
| 1517 | 2 | symbol.factor, |
|---|
| 1518 | n/a | ] |
|---|
| 1519 | n/a | |
|---|
| 1520 | 2 | _names = {} |
|---|
| 1521 | 174 | for k, v in symbol.sym_name.items(): |
|---|
| 1522 | 172 | _names[k] = v |
|---|
| 1523 | 114 | for k, v in token.tok_name.items(): |
|---|
| 1524 | 112 | _names[k] = v |
|---|
| 1525 | n/a | |
|---|
| 1526 | 2 | def debug_tree(tree): |
|---|
| 1527 | 0 | l = [] |
|---|
| 1528 | 0 | for elt in tree: |
|---|
| 1529 | 0 | if isinstance(elt, int): |
|---|
| 1530 | 0 | l.append(_names.get(elt, elt)) |
|---|
| 1531 | 0 | elif isinstance(elt, str): |
|---|
| 1532 | 0 | l.append(elt) |
|---|
| 1533 | n/a | else: |
|---|
| 1534 | 0 | l.append(debug_tree(elt)) |
|---|
| 1535 | 0 | return l |
|---|