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