| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This file includes functions to transform a concrete syntax tree (CST) to | 2 * This file includes functions to transform a concrete syntax tree (CST) to |
| 3 * an abstract syntax tree (AST). The main function is PyAST_FromNode(). | 3 * an abstract syntax tree (AST). The main function is PyAST_FromNode(). |
| 4 * | 4 * |
| 5 */ | 5 */ |
| 6 #include "Python.h" | 6 #include "Python.h" |
| 7 #include "Python-ast.h" | 7 #include "Python-ast.h" |
| 8 #include "grammar.h" | 8 #include "grammar.h" |
| 9 #include "node.h" | 9 #include "node.h" |
| 10 #include "pyarena.h" | 10 #include "pyarena.h" |
| (...skipping 2991 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3002 if (body == NULL) | 3002 if (body == NULL) |
| 3003 return NULL; | 3003 return NULL; |
| 3004 asdl_seq_SET(body, 0, except_st); | 3004 asdl_seq_SET(body, 0, except_st); |
| 3005 } | 3005 } |
| 3006 | 3006 |
| 3007 /* must be a try ... finally (except clauses are in body, if any exist) */ | 3007 /* must be a try ... finally (except clauses are in body, if any exist) */ |
| 3008 assert(finally != NULL); | 3008 assert(finally != NULL); |
| 3009 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena); | 3009 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena); |
| 3010 } | 3010 } |
| 3011 | 3011 |
| 3012 static expr_ty | 3012 /* with_item: test ['as' expr] */ |
| 3013 ast_for_with_var(struct compiling *c, const node *n) | |
| 3014 { | |
| 3015 REQ(n, with_var); | |
| 3016 return ast_for_expr(c, CHILD(n, 1)); | |
| 3017 } | |
| 3018 | |
| 3019 /* with_stmt: 'with' test [ with_var ] ':' suite */ | |
| 3020 static stmt_ty | 3013 static stmt_ty |
| 3021 ast_for_with_stmt(struct compiling *c, const node *n) | 3014 ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content) |
| 3022 { | 3015 { |
| 3023 expr_ty context_expr, optional_vars = NULL; | 3016 expr_ty context_expr, optional_vars = NULL; |
| 3024 int suite_index = 3; /* skip 'with', test, and ':' */ | |
| 3025 asdl_seq *suite_seq; | |
| 3026 | 3017 |
| 3027 assert(TYPE(n) == with_stmt); | 3018 REQ(n, with_item); |
| 3028 context_expr = ast_for_expr(c, CHILD(n, 1)); | 3019 context_expr = ast_for_expr(c, CHILD(n, 0)); |
| 3029 if (!context_expr) | 3020 if (!context_expr) |
| 3030 return NULL; | 3021 return NULL; |
| 3031 if (TYPE(CHILD(n, 2)) == with_var) { | 3022 if (NCH(n) == 3) { |
| 3032 optional_vars = ast_for_with_var(c, CHILD(n, 2)); | 3023 optional_vars = ast_for_expr(c, CHILD(n, 2)); |
| 3033 | 3024 |
| 3034 if (!optional_vars) { | 3025 if (!optional_vars) { |
| 3035 return NULL; | 3026 return NULL; |
| 3036 } | 3027 } |
| 3037 if (!set_context(c, optional_vars, Store, n)) { | 3028 if (!set_context(c, optional_vars, Store, n)) { |
| 3038 return NULL; | 3029 return NULL; |
| 3039 } | 3030 } |
| 3040 suite_index = 4; | |
| 3041 } | 3031 } |
| 3042 | 3032 |
| 3043 suite_seq = ast_for_suite(c, CHILD(n, suite_index)); | 3033 return With(context_expr, optional_vars, content, LINENO(n), |
| 3044 if (!suite_seq) { | |
| 3045 return NULL; | |
| 3046 } | |
| 3047 return With(context_expr, optional_vars, suite_seq, LINENO(n),· | |
| 3048 n->n_col_offset, c->c_arena); | 3034 n->n_col_offset, c->c_arena); |
| 3049 } | 3035 } |
| 3050 | 3036 |
| 3037 /* with_stmt: 'with' with_item (',' with_item)* ':' suite */ | |
| 3051 static stmt_ty | 3038 static stmt_ty |
| 3039 ast_for_with_stmt(struct compiling *c, const node *n) | |
| 3040 { | |
| 3041 int i; | |
| 3042 stmt_ty ret; | |
| 3043 asdl_seq *inner; | |
| 3044 | |
| 3045 assert(TYPE(n) == with_stmt); | |
|
Benjamin
2009/05/02 18:22:06
REQ() is better here.
Georg
2009/05/02 18:48:00
It was already there, but Done.
| |
| 3046 | |
| 3047 /* process the with items inside-out */ | |
| 3048 i = NCH(n) - 1; | |
| 3049 /* the suite of the innermost with item is the suite of the with stmt */ | |
| 3050 inner = ast_for_suite(c, CHILD(n, i)); | |
| 3051 if (!inner) | |
| 3052 return NULL; | |
| 3053 | |
| 3054 for (;;) { | |
| 3055 i -= 2; | |
| 3056 ret = ast_for_with_item(c, CHILD(n, i), inner); | |
| 3057 if (!ret) | |
| 3058 return NULL; | |
| 3059 /* was this the last item? */ | |
| 3060 if (i == 1) | |
| 3061 break; | |
| 3062 /* if not, wrap the result so far in a new sequence */ | |
| 3063 inner = asdl_seq_new(1, c->c_arena); | |
| 3064 if (!inner) | |
| 3065 return NULL; | |
| 3066 asdl_seq_SET(inner, 0, ret); | |
| 3067 } | |
| 3068 | |
| 3069 return ret; | |
| 3070 } | |
| 3071 | |
| 3072 static stmt_ty | |
| 3052 ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) | 3073 ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 3053 { | 3074 { |
| 3054 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ | 3075 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ |
| 3055 PyObject *classname; | 3076 PyObject *classname; |
| 3056 asdl_seq *bases, *s; | 3077 asdl_seq *bases, *s; |
| 3057 ···· | 3078 ···· |
| 3058 REQ(n, classdef); | 3079 REQ(n, classdef); |
| 3059 | 3080 |
| 3060 if (!forbidden_check(c, n, STR(CHILD(n, 1)))) | 3081 if (!forbidden_check(c, n, STR(CHILD(n, 1)))) |
| 3061 return NULL; | 3082 return NULL; |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3421 } | 3442 } |
| 3422 #endif | 3443 #endif |
| 3423 } | 3444 } |
| 3424 } | 3445 } |
| 3425 return v; | 3446 return v; |
| 3426 | 3447 |
| 3427 onError: | 3448 onError: |
| 3428 Py_XDECREF(v); | 3449 Py_XDECREF(v); |
| 3429 return NULL; | 3450 return NULL; |
| 3430 } | 3451 } |
| OLD | NEW |