OLD | NEW |
1 """Parse tree transformation module. | 1 """Parse tree transformation module. |
2 | 2 |
3 Transforms Python source code into an abstract syntax tree (AST) | 3 Transforms Python source code into an abstract syntax tree (AST) |
4 defined in the ast module. | 4 defined in the ast module. |
5 | 5 |
6 The simplest ways to invoke this module are via parse and parseFile. | 6 The simplest ways to invoke this module are via parse and parseFile. |
7 parse(buf) -> AST | 7 parse(buf) -> AST |
8 parseFile(path) -> AST | 8 parseFile(path) -> AST |
9 """ | 9 """ |
10 | 10 |
(...skipping 947 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
958 elif node[1] == 'finally': | 958 elif node[1] == 'finally': |
959 finallyNode = self.com_node(nodelist[i+2]) | 959 finallyNode = self.com_node(nodelist[i+2]) |
960 try_except = TryExcept(self.com_node(nodelist[2]), clauses, elseNode, | 960 try_except = TryExcept(self.com_node(nodelist[2]), clauses, elseNode, |
961 lineno=nodelist[0][2]) | 961 lineno=nodelist[0][2]) |
962 if finallyNode: | 962 if finallyNode: |
963 return TryFinally(try_except, finallyNode, lineno=nodelist[0][2]) | 963 return TryFinally(try_except, finallyNode, lineno=nodelist[0][2]) |
964 else: | 964 else: |
965 return try_except | 965 return try_except |
966 | 966 |
967 def com_with(self, nodelist): | 967 def com_with(self, nodelist): |
968 # with_stmt: 'with' expr [with_var] ':' suite | 968 # with_stmt: 'with' with_item (',' with_item)* ':' suite |
| 969 body = self.com_node(nodelist[-1]) |
| 970 for i in range(len(nodelist) - 3, 0, -2): |
| 971 ret = self.com_with_item(nodelist[i], body, nodelist[0][2]) |
| 972 if i == 1: |
| 973 return ret |
| 974 body = ret |
| 975 |
| 976 def com_with_item(self, nodelist, body, lineno): |
| 977 # with_item: test ['as' expr] |
| 978 if len(nodelist) == 4: |
| 979 var = self.com_assign(nodelist[3], OP_ASSIGN) |
| 980 else: |
| 981 var = None |
969 expr = self.com_node(nodelist[1]) | 982 expr = self.com_node(nodelist[1]) |
970 body = self.com_node(nodelist[-1]) | 983 return With(expr, var, body, lineno=lineno) |
971 if nodelist[2][0] == token.COLON: | |
972 var = None | |
973 else: | |
974 var = self.com_assign(nodelist[2][2], OP_ASSIGN) | |
975 return With(expr, var, body, lineno=nodelist[0][2]) | |
976 | |
977 def com_with_var(self, nodelist): | |
978 # with_var: 'as' expr | |
979 return self.com_node(nodelist[1]) | |
980 | 984 |
981 def com_augassign_op(self, node): | 985 def com_augassign_op(self, node): |
982 assert node[0] == symbol.augassign | 986 assert node[0] == symbol.augassign |
983 return node[1] | 987 return node[1] |
984 | 988 |
985 def com_augassign(self, node): | 989 def com_augassign(self, node): |
986 """Return node suitable for lvalue of augmented assignment | 990 """Return node suitable for lvalue of augmented assignment |
987 | 991 |
988 Names, slices, and attributes are the only allowable nodes. | 992 Names, slices, and attributes are the only allowable nodes. |
989 """ | 993 """ |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1490 def debug_tree(tree): | 1494 def debug_tree(tree): |
1491 l = [] | 1495 l = [] |
1492 for elt in tree: | 1496 for elt in tree: |
1493 if isinstance(elt, int): | 1497 if isinstance(elt, int): |
1494 l.append(_names.get(elt, elt)) | 1498 l.append(_names.get(elt, elt)) |
1495 elif isinstance(elt, str): | 1499 elif isinstance(elt, str): |
1496 l.append(elt) | 1500 l.append(elt) |
1497 else: | 1501 else: |
1498 l.append(debug_tree(elt)) | 1502 l.append(debug_tree(elt)) |
1499 return l | 1503 return l |
OLD | NEW |