Mercurial > cpython-lru_cache-weakref
annotate Python/ast.c @ 42054:063530fcc32e
Minimal changes to make the "freeze" tool work again.
There are other issues left, but these were basics (e.g. keys().sort()).
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Tue, 12 Jun 2007 00:28:30 +0000 |
| parents | cd125fe83051 |
| children | 229c28cb9afb |
| rev | line source |
|---|---|
| 35742 | 1 /* |
| 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(). | |
| 4 * | |
| 5 */ | |
| 6 #include "Python.h" | |
| 7 #include "Python-ast.h" | |
| 8 #include "grammar.h" | |
| 9 #include "node.h" | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
10 #include "pyarena.h" |
| 35742 | 11 #include "ast.h" |
| 12 #include "token.h" | |
| 13 #include "parsetok.h" | |
| 14 #include "graminit.h" | |
| 15 | |
| 16 #include <assert.h> | |
| 17 | |
| 18 /* Data structure used internally */ | |
| 19 struct compiling { | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
20 char *c_encoding; /* source encoding */ |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
21 PyArena *c_arena; /* arena for allocating memeory */ |
| 35742 | 22 }; |
| 23 | |
| 24 static asdl_seq *seq_for_testlist(struct compiling *, const node *); | |
| 25 static expr_ty ast_for_expr(struct compiling *, const node *); | |
| 26 static stmt_ty ast_for_stmt(struct compiling *, const node *); | |
| 27 static asdl_seq *ast_for_suite(struct compiling *, const node *); | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
28 static asdl_seq *ast_for_exprlist(struct compiling *, const node *, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
29 expr_context_ty); |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
30 static expr_ty ast_for_testlist(struct compiling *, const node *); |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
31 static stmt_ty ast_for_classdef(struct compiling *, const node *, asdl_seq *); |
| 35742 | 32 |
| 33 /* Note different signature for ast_for_call */ | |
| 34 static expr_ty ast_for_call(struct compiling *, const node *, expr_ty); | |
| 35 | |
| 36 static PyObject *parsenumber(const char *); | |
| 40894 | 37 static PyObject *parsestr(const node *n, const char *encoding, int *bytesmode); |
| 38 static PyObject *parsestrplus(struct compiling *, const node *n, | |
| 39 int *bytesmode); | |
| 35742 | 40 |
| 41 #ifndef LINENO | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
42 #define LINENO(n) ((n)->n_lineno) |
| 35742 | 43 #endif |
| 44 | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
45 #define COMP_GENEXP 0 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
46 #define COMP_LISTCOMP 1 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
47 #define COMP_SETCOMP 2 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
48 |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
49 static identifier |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
50 new_identifier(const char* n, PyArena *arena) { |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
51 PyObject* id = PyUnicode_DecodeUTF8(n, strlen(n), NULL); |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
52 PyUnicode_InternInPlace(&id); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
53 PyArena_AddPyObject(arena, id); |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
54 return id; |
| 35742 | 55 } |
| 56 | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
57 #define NEW_IDENTIFIER(n) new_identifier(STR(n), c->c_arena) |
| 35742 | 58 |
| 59 /* This routine provides an invalid object for the syntax error. | |
| 60 The outermost routine must unpack this error and create the | |
| 61 proper object. We do this so that we don't have to pass | |
| 62 the filename to everything function. | |
| 63 | |
| 64 XXX Maybe we should just pass the filename... | |
| 65 */ | |
| 66 | |
| 67 static int | |
| 68 ast_error(const node *n, const char *errstr) | |
| 69 { | |
| 70 PyObject *u = Py_BuildValue("zi", errstr, LINENO(n)); | |
| 71 if (!u) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
72 return 0; |
| 35742 | 73 PyErr_SetObject(PyExc_SyntaxError, u); |
| 74 Py_DECREF(u); | |
| 75 return 0; | |
| 76 } | |
| 77 | |
| 78 static void | |
| 79 ast_error_finish(const char *filename) | |
| 80 { | |
| 81 PyObject *type, *value, *tback, *errstr, *loc, *tmp; | |
|
36231
d3f4c467e439
Fix icc warnings: conversion from "long" to "int" may lose significant bits
Neal Norwitz <nnorwitz@gmail.com>
parents:
36224
diff
changeset
|
82 long lineno; |
| 35742 | 83 |
| 84 assert(PyErr_Occurred()); | |
| 85 if (!PyErr_ExceptionMatches(PyExc_SyntaxError)) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
86 return; |
| 35742 | 87 |
| 88 PyErr_Fetch(&type, &value, &tback); | |
| 89 errstr = PyTuple_GetItem(value, 0); | |
| 90 if (!errstr) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
91 return; |
| 35742 | 92 Py_INCREF(errstr); |
| 93 lineno = PyInt_AsLong(PyTuple_GetItem(value, 1)); | |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
94 if (lineno == -1) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
95 Py_DECREF(errstr); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
96 return; |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
97 } |
| 35742 | 98 Py_DECREF(value); |
| 99 | |
| 100 loc = PyErr_ProgramText(filename, lineno); | |
| 101 if (!loc) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
102 Py_INCREF(Py_None); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
103 loc = Py_None; |
| 35742 | 104 } |
|
36231
d3f4c467e439
Fix icc warnings: conversion from "long" to "int" may lose significant bits
Neal Norwitz <nnorwitz@gmail.com>
parents:
36224
diff
changeset
|
105 tmp = Py_BuildValue("(zlOO)", filename, lineno, Py_None, loc); |
| 35742 | 106 Py_DECREF(loc); |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
107 if (!tmp) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
108 Py_DECREF(errstr); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
109 return; |
|
36000
cc3261597136
SF #1370197, memory leak - ast_error_finish (in error conditions).
Neal Norwitz <nnorwitz@gmail.com>
parents:
35990
diff
changeset
|
110 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
111 value = PyTuple_Pack(2, errstr, tmp); |
| 35742 | 112 Py_DECREF(errstr); |
| 113 Py_DECREF(tmp); | |
| 114 if (!value) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
115 return; |
| 35742 | 116 PyErr_Restore(type, value, tback); |
| 117 } | |
| 118 | |
| 119 /* num_stmts() returns number of contained statements. | |
| 120 | |
| 121 Use this routine to determine how big a sequence is needed for | |
| 122 the statements in a parse tree. Its raison d'etre is this bit of | |
| 123 grammar: | |
| 124 | |
| 125 stmt: simple_stmt | compound_stmt | |
| 126 simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE | |
| 127 | |
| 128 A simple_stmt can contain multiple small_stmt elements joined | |
| 129 by semicolons. If the arg is a simple_stmt, the number of | |
| 130 small_stmt elements is returned. | |
| 131 */ | |
| 132 | |
| 133 static int | |
| 134 num_stmts(const node *n) | |
| 135 { | |
| 136 int i, l; | |
| 137 node *ch; | |
| 138 | |
| 139 switch (TYPE(n)) { | |
| 140 case single_input: | |
| 141 if (TYPE(CHILD(n, 0)) == NEWLINE) | |
| 142 return 0; | |
| 143 else | |
| 144 return num_stmts(CHILD(n, 0)); | |
| 145 case file_input: | |
| 146 l = 0; | |
| 147 for (i = 0; i < NCH(n); i++) { | |
| 148 ch = CHILD(n, i); | |
| 149 if (TYPE(ch) == stmt) | |
| 150 l += num_stmts(ch); | |
| 151 } | |
| 152 return l; | |
| 153 case stmt: | |
| 154 return num_stmts(CHILD(n, 0)); | |
| 155 case compound_stmt: | |
| 156 return 1; | |
| 157 case simple_stmt: | |
| 158 return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ | |
| 159 case suite: | |
| 160 if (NCH(n) == 1) | |
| 161 return num_stmts(CHILD(n, 0)); | |
| 162 else { | |
| 163 l = 0; | |
| 164 for (i = 2; i < (NCH(n) - 1); i++) | |
| 165 l += num_stmts(CHILD(n, i)); | |
| 166 return l; | |
| 167 } | |
| 168 default: { | |
| 169 char buf[128]; | |
| 170 | |
| 171 sprintf(buf, "Non-statement found: %d %d\n", | |
| 172 TYPE(n), NCH(n)); | |
| 173 Py_FatalError(buf); | |
| 174 } | |
| 175 } | |
| 176 assert(0); | |
| 177 return 0; | |
| 178 } | |
| 179 | |
| 180 /* Transform the CST rooted at node * to the appropriate AST | |
| 181 */ | |
| 182 | |
| 183 mod_ty | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
184 PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename, |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
185 PyArena *arena) |
| 35742 | 186 { |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
187 int i, j, k, num; |
| 35742 | 188 asdl_seq *stmts = NULL; |
| 189 stmt_ty s; | |
| 190 node *ch; | |
| 191 struct compiling c; | |
| 192 | |
| 193 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
194 c.c_encoding = "utf-8"; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
195 if (TYPE(n) == encoding_decl) { |
|
42054
063530fcc32e
Minimal changes to make the "freeze" tool work again.
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
196 #if 0 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
197 ast_error(n, "encoding declaration in Unicode string"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
198 goto error; |
|
42054
063530fcc32e
Minimal changes to make the "freeze" tool work again.
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
199 #endif |
|
063530fcc32e
Minimal changes to make the "freeze" tool work again.
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
200 n = CHILD(n, 0); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
201 } |
| 35742 | 202 } else if (TYPE(n) == encoding_decl) { |
| 203 c.c_encoding = STR(n); | |
| 204 n = CHILD(n, 0); | |
| 205 } else { | |
| 206 c.c_encoding = NULL; | |
| 207 } | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
208 c.c_arena = arena; |
| 35742 | 209 |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
210 k = 0; |
| 35742 | 211 switch (TYPE(n)) { |
| 212 case file_input: | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
213 stmts = asdl_seq_new(num_stmts(n), arena); |
| 35742 | 214 if (!stmts) |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
215 return NULL; |
| 35742 | 216 for (i = 0; i < NCH(n) - 1; i++) { |
| 217 ch = CHILD(n, i); | |
| 218 if (TYPE(ch) == NEWLINE) | |
| 219 continue; | |
| 220 REQ(ch, stmt); | |
| 221 num = num_stmts(ch); | |
| 222 if (num == 1) { | |
| 223 s = ast_for_stmt(&c, ch); | |
| 224 if (!s) | |
| 225 goto error; | |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
226 asdl_seq_SET(stmts, k++, s); |
| 35742 | 227 } |
| 228 else { | |
| 229 ch = CHILD(ch, 0); | |
| 230 REQ(ch, simple_stmt); | |
| 231 for (j = 0; j < num; j++) { | |
| 232 s = ast_for_stmt(&c, CHILD(ch, j * 2)); | |
| 233 if (!s) | |
| 234 goto error; | |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
235 asdl_seq_SET(stmts, k++, s); |
| 35742 | 236 } |
| 237 } | |
| 238 } | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
239 return Module(stmts, arena); |
| 35742 | 240 case eval_input: { |
| 241 expr_ty testlist_ast; | |
| 242 | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
243 /* XXX Why not comp_for here? */ |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
244 testlist_ast = ast_for_testlist(&c, CHILD(n, 0)); |
| 35742 | 245 if (!testlist_ast) |
| 246 goto error; | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
247 return Expression(testlist_ast, arena); |
| 35742 | 248 } |
| 249 case single_input: | |
| 250 if (TYPE(CHILD(n, 0)) == NEWLINE) { | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
251 stmts = asdl_seq_new(1, arena); |
| 35742 | 252 if (!stmts) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
253 goto error; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
254 asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
255 arena)); |
|
41716
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41699
diff
changeset
|
256 if (!asdl_seq_GET(stmts, 0)) |
|
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41699
diff
changeset
|
257 goto error; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
258 return Interactive(stmts, arena); |
| 35742 | 259 } |
| 260 else { | |
| 261 n = CHILD(n, 0); | |
| 262 num = num_stmts(n); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
263 stmts = asdl_seq_new(num, arena); |
| 35742 | 264 if (!stmts) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
265 goto error; |
| 35742 | 266 if (num == 1) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
267 s = ast_for_stmt(&c, n); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
268 if (!s) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
269 goto error; |
| 35742 | 270 asdl_seq_SET(stmts, 0, s); |
| 271 } | |
| 272 else { | |
| 273 /* Only a simple_stmt can contain multiple statements. */ | |
| 274 REQ(n, simple_stmt); | |
| 275 for (i = 0; i < NCH(n); i += 2) { | |
| 276 if (TYPE(CHILD(n, i)) == NEWLINE) | |
| 277 break; | |
| 278 s = ast_for_stmt(&c, CHILD(n, i)); | |
| 279 if (!s) | |
| 280 goto error; | |
| 281 asdl_seq_SET(stmts, i / 2, s); | |
| 282 } | |
| 283 } | |
| 284 | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
285 return Interactive(stmts, arena); |
| 35742 | 286 } |
| 287 default: | |
|
41716
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41699
diff
changeset
|
288 PyErr_Format(PyExc_SystemError, |
|
00c02c4488ce
Merged revisions 55325-55327 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41699
diff
changeset
|
289 "invalid node %d for PyAST_FromNode", TYPE(n)); |
| 35742 | 290 goto error; |
| 291 } | |
| 292 error: | |
| 293 ast_error_finish(filename); | |
| 294 return NULL; | |
| 295 } | |
| 296 | |
| 297 /* Return the AST repr. of the operator represented as syntax (|, ^, etc.) | |
| 298 */ | |
| 299 | |
| 300 static operator_ty | |
| 301 get_operator(const node *n) | |
| 302 { | |
| 303 switch (TYPE(n)) { | |
| 304 case VBAR: | |
| 305 return BitOr; | |
| 306 case CIRCUMFLEX: | |
| 307 return BitXor; | |
| 308 case AMPER: | |
| 309 return BitAnd; | |
| 310 case LEFTSHIFT: | |
| 311 return LShift; | |
| 312 case RIGHTSHIFT: | |
| 313 return RShift; | |
| 314 case PLUS: | |
| 315 return Add; | |
| 316 case MINUS: | |
| 317 return Sub; | |
| 318 case STAR: | |
| 319 return Mult; | |
| 320 case SLASH: | |
| 321 return Div; | |
| 322 case DOUBLESLASH: | |
| 323 return FloorDiv; | |
| 324 case PERCENT: | |
| 325 return Mod; | |
| 326 default: | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
327 return (operator_ty)0; |
| 35742 | 328 } |
| 329 } | |
| 330 | |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
331 static const char* FORBIDDEN[] = { |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
332 "None", |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
333 "True", |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
334 "False", |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
335 NULL, |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
336 }; |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
337 |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
338 static int |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
339 forbidden_name(expr_ty e, const node *n) |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
340 { |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
341 const char **p; |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
342 assert(PyUnicode_Check(e->v.Name.id)); |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
343 for (p = FORBIDDEN; *p; p++) { |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
344 if (PyUnicode_CompareWithASCIIString(e->v.Name.id, *p) == 0) { |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
345 ast_error(n, "assignment to keyword"); |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
346 return 1; |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
347 } |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
348 } |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
349 return 0; |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
350 } |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
351 |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
352 /* Set the context ctx for expr_ty e, recursively traversing e. |
| 35742 | 353 |
| 354 Only sets context for expr kinds that "can appear in assignment context" | |
| 355 (according to ../Parser/Python.asdl). For other expr kinds, it sets | |
| 356 an appropriate syntax error and returns false. | |
| 357 */ | |
| 358 | |
| 359 static int | |
| 360 set_context(expr_ty e, expr_context_ty ctx, const node *n) | |
| 361 { | |
| 362 asdl_seq *s = NULL; | |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
363 /* If a particular expression type can't be used for assign / delete, |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
364 set expr_name to its name and an error message will be generated. |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
365 */ |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
366 const char* expr_name = NULL; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
367 |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
368 /* The ast defines augmented store and load contexts, but the |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
369 implementation here doesn't actually use them. The code may be |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
370 a little more complex than necessary as a result. It also means |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
371 that expressions in an augmented assignment have a Store context. |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
372 Consider restructuring so that augmented assignment uses |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
373 set_context(), too. |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
374 */ |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
375 assert(ctx != AugStore && ctx != AugLoad); |
| 35742 | 376 |
| 377 switch (e->kind) { | |
| 378 case Attribute_kind: | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
379 if (ctx == Store && |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
380 !PyUnicode_CompareWithASCIIString(e->v.Attribute.attr, "None")) { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
381 return ast_error(n, "assignment to None"); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
382 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
383 e->v.Attribute.ctx = ctx; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
384 break; |
| 35742 | 385 case Subscript_kind: |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
386 e->v.Subscript.ctx = ctx; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
387 break; |
|
41699
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
388 case Starred_kind: |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
389 e->v.Starred.ctx = ctx; |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
390 if (!set_context(e->v.Starred.value, ctx, n)) |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
391 return 0; |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
392 break; |
| 35742 | 393 case Name_kind: |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
394 if (ctx == Store) { |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
395 if (forbidden_name(e, n)) |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
396 return 0; /* forbidden_name() calls ast_error() */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
397 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
398 e->v.Name.ctx = ctx; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
399 break; |
| 35742 | 400 case List_kind: |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
401 e->v.List.ctx = ctx; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
402 s = e->v.List.elts; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
403 break; |
| 35742 | 404 case Tuple_kind: |
| 405 if (asdl_seq_LEN(e->v.Tuple.elts) == 0) | |
| 406 return ast_error(n, "can't assign to ()"); | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
407 e->v.Tuple.ctx = ctx; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
408 s = e->v.Tuple.elts; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
409 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
410 case Lambda_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
411 expr_name = "lambda"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
412 break; |
| 35742 | 413 case Call_kind: |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
414 expr_name = "function call"; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
415 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
416 case BoolOp_kind: |
| 35742 | 417 case BinOp_kind: |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
418 case UnaryOp_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
419 expr_name = "operator"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
420 break; |
| 35742 | 421 case GeneratorExp_kind: |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
422 expr_name = "generator expression"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
423 break; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
424 case Yield_kind: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
425 expr_name = "yield expression"; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
426 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
427 case ListComp_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
428 expr_name = "list comprehension"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
429 break; |
|
41699
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
430 case SetComp_kind: |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
431 expr_name = "set comprehension"; |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
432 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
433 case Dict_kind: |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
434 case Set_kind: |
| 35742 | 435 case Num_kind: |
| 436 case Str_kind: | |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
437 expr_name = "literal"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
438 break; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
439 case Ellipsis_kind: |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
440 expr_name = "Ellipsis"; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
441 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
442 case Compare_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
443 expr_name = "comparison"; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
444 break; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
445 case IfExp_kind: |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
446 expr_name = "conditional expression"; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
447 break; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
448 default: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
449 PyErr_Format(PyExc_SystemError, |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
450 "unexpected expression in assignment %d (line %d)", |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
451 e->kind, e->lineno); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
452 return 0; |
| 35742 | 453 } |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
454 /* Check for error string set by switch */ |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
455 if (expr_name) { |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
456 char buf[300]; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
457 PyOS_snprintf(buf, sizeof(buf), |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
458 "can't %s %s", |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
459 ctx == Store ? "assign to" : "delete", |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
460 expr_name); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
461 return ast_error(n, buf); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
462 } |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
463 |
| 35742 | 464 /* If the LHS is a list or tuple, we need to set the assignment |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
465 context for all the contained elements. |
| 35742 | 466 */ |
| 467 if (s) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
468 int i; |
| 35742 | 469 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
470 for (i = 0; i < asdl_seq_LEN(s); i++) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
471 if (!set_context((expr_ty)asdl_seq_GET(s, i), ctx, n)) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
472 return 0; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
473 } |
| 35742 | 474 } |
| 475 return 1; | |
| 476 } | |
| 477 | |
| 478 static operator_ty | |
| 479 ast_for_augassign(const node *n) | |
| 480 { | |
| 481 REQ(n, augassign); | |
| 482 n = CHILD(n, 0); | |
| 483 switch (STR(n)[0]) { | |
| 484 case '+': | |
| 485 return Add; | |
| 486 case '-': | |
| 487 return Sub; | |
| 488 case '/': | |
| 489 if (STR(n)[1] == '/') | |
| 490 return FloorDiv; | |
| 491 else | |
| 492 return Div; | |
| 493 case '%': | |
| 494 return Mod; | |
| 495 case '<': | |
| 496 return LShift; | |
| 497 case '>': | |
| 498 return RShift; | |
| 499 case '&': | |
| 500 return BitAnd; | |
| 501 case '^': | |
| 502 return BitXor; | |
| 503 case '|': | |
| 504 return BitOr; | |
| 505 case '*': | |
| 506 if (STR(n)[1] == '*') | |
| 507 return Pow; | |
| 508 else | |
| 509 return Mult; | |
| 510 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
511 PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
512 return (operator_ty)0; |
| 35742 | 513 } |
| 514 } | |
| 515 | |
| 516 static cmpop_ty | |
| 517 ast_for_comp_op(const node *n) | |
| 518 { | |
|
39648
efe3f2c0da5e
Killed the <> operator. You must now use !=.
Guido van Rossum <guido@python.org>
parents:
39610
diff
changeset
|
519 /* comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is' |
| 35742 | 520 |'is' 'not' |
| 521 */ | |
| 522 REQ(n, comp_op); | |
| 523 if (NCH(n) == 1) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
524 n = CHILD(n, 0); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
525 switch (TYPE(n)) { |
| 35742 | 526 case LESS: |
| 527 return Lt; | |
| 528 case GREATER: | |
| 529 return Gt; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
530 case EQEQUAL: /* == */ |
| 35742 | 531 return Eq; |
| 532 case LESSEQUAL: | |
| 533 return LtE; | |
| 534 case GREATEREQUAL: | |
| 535 return GtE; | |
| 536 case NOTEQUAL: | |
| 537 return NotEq; | |
| 538 case NAME: | |
| 539 if (strcmp(STR(n), "in") == 0) | |
| 540 return In; | |
| 541 if (strcmp(STR(n), "is") == 0) | |
| 542 return Is; | |
| 543 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
544 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s", |
| 35742 | 545 STR(n)); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
546 return (cmpop_ty)0; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
547 } |
| 35742 | 548 } |
| 549 else if (NCH(n) == 2) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
550 /* handle "not in" and "is not" */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
551 switch (TYPE(CHILD(n, 0))) { |
| 35742 | 552 case NAME: |
| 553 if (strcmp(STR(CHILD(n, 1)), "in") == 0) | |
| 554 return NotIn; | |
| 555 if (strcmp(STR(CHILD(n, 0)), "is") == 0) | |
| 556 return IsNot; | |
| 557 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
558 PyErr_Format(PyExc_SystemError, "invalid comp_op: %s %s", |
| 35742 | 559 STR(CHILD(n, 0)), STR(CHILD(n, 1))); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
560 return (cmpop_ty)0; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
561 } |
| 35742 | 562 } |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
563 PyErr_Format(PyExc_SystemError, "invalid comp_op: has %d children", |
| 35742 | 564 NCH(n)); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
565 return (cmpop_ty)0; |
| 35742 | 566 } |
| 567 | |
| 568 static asdl_seq * | |
| 569 seq_for_testlist(struct compiling *c, const node *n) | |
| 570 { | |
| 571 /* testlist: test (',' test)* [','] */ | |
|
35759
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
572 asdl_seq *seq; |
|
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
573 expr_ty expression; |
|
d91ed7107fd2
ANSI-C-ify the placement of local var declarations.
Armin Rigo <arigo@tunes.org>
parents:
35748
diff
changeset
|
574 int i; |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
575 assert(TYPE(n) == testlist || TYPE(n) == testlist_comp); |
| 35742 | 576 |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
577 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 35742 | 578 if (!seq) |
| 579 return NULL; | |
| 580 | |
| 581 for (i = 0; i < NCH(n); i += 2) { | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
582 assert(TYPE(CHILD(n, i)) == test || TYPE(CHILD(n, i)) == test_nocond); |
| 35742 | 583 |
| 584 expression = ast_for_expr(c, CHILD(n, i)); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
585 if (!expression) |
| 35742 | 586 return NULL; |
| 587 | |
| 588 assert(i / 2 < seq->size); | |
| 589 asdl_seq_SET(seq, i / 2, expression); | |
| 590 } | |
| 591 return seq; | |
| 592 } | |
| 593 | |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
594 static arg_ty |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
595 compiler_arg(struct compiling *c, const node *n) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
596 { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
597 identifier name; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
598 expr_ty annotation = NULL; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
599 node *ch; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
600 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
601 assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef); |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
602 ch = CHILD(n, 0); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
603 if (!strcmp(STR(ch), "None")) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
604 ast_error(ch, "assignment to None"); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
605 return NULL; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
606 } |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
607 name = NEW_IDENTIFIER(ch); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
608 if (!name) |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
609 return NULL; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
610 |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
611 if (NCH(n) == 3 && TYPE(CHILD(n, 1)) == COLON) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
612 annotation = ast_for_expr(c, CHILD(n, 2)); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
613 if (!annotation) |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
614 return NULL; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
615 } |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
616 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
617 return arg(name, annotation, c->c_arena); |
| 35742 | 618 } |
| 619 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
620 /* returns -1 if failed to handle keyword only arguments |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
621 returns new position to keep processing if successful |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
622 (',' tfpdef ['=' test])* |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
623 ^^^ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
624 start pointing here |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
625 */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
626 static int |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
627 handle_keywordonly_args(struct compiling *c, const node *n, int start, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
628 asdl_seq *kwonlyargs, asdl_seq *kwdefaults) |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
629 { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
630 node *ch; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
631 expr_ty expression, annotation; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
632 arg_ty arg; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
633 int i = start; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
634 int j = 0; /* index for kwdefaults and kwonlyargs */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
635 assert(kwonlyargs != NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
636 assert(kwdefaults != NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
637 while (i < NCH(n)) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
638 ch = CHILD(n, i); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
639 switch (TYPE(ch)) { |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
640 case vfpdef: |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
641 case tfpdef: |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
642 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
643 expression = ast_for_expr(c, CHILD(n, i + 2)); |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
644 if (!expression) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
645 ast_error(ch, "assignment to None"); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
646 goto error; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
647 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
648 asdl_seq_SET(kwdefaults, j, expression); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
649 i += 2; /* '=' and test */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
650 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
651 else { /* setting NULL if no default value exists */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
652 asdl_seq_SET(kwdefaults, j, NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
653 } |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
654 if (NCH(ch) == 3) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
655 /* ch is NAME ':' test */ |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
656 annotation = ast_for_expr(c, CHILD(ch, 2)); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
657 if (!annotation) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
658 ast_error(ch, "expected expression"); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
659 goto error; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
660 } |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
661 } |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
662 else { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
663 annotation = NULL; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
664 } |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
665 ch = CHILD(ch, 0); |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
666 if (!strcmp(STR(ch), "None")) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
667 ast_error(ch, "assignment to None"); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
668 goto error; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
669 } |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
670 arg = arg(NEW_IDENTIFIER(ch), annotation, c->c_arena); |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
671 if (!arg) { |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
672 ast_error(ch, "expecting name"); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
673 goto error; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
674 } |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
675 asdl_seq_SET(kwonlyargs, j++, arg); |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
676 i += 2; /* the name and the comma */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
677 break; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
678 case DOUBLESTAR: |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
679 return i; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
680 default: |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
681 ast_error(ch, "unexpected node"); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
682 goto error; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
683 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
684 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
685 return i; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
686 error: |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
687 return -1; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
688 } |
| 35742 | 689 |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
690 /* Create AST for argument list. */ |
| 35742 | 691 |
| 692 static arguments_ty | |
| 693 ast_for_arguments(struct compiling *c, const node *n) | |
| 694 { | |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
695 /* This function handles both typedargslist (function definition) |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
696 and varargslist (lambda definition). |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
697 |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
698 parameters: '(' [typedargslist] ')' |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
699 typedargslist: ((tfpdef ['=' test] ',')* |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
700 ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
701 | '**' tfpdef) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
702 | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
703 tfpdef: NAME [':' test] |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
704 varargslist: ((vfpdef ['=' test] ',')* |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
705 ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
706 | '**' vfpdef) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
707 | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
708 vfpdef: NAME |
| 35742 | 709 */ |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
710 int i, j, k, nposargs = 0, nkwonlyargs = 0; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
711 int nposdefaults = 0, found_default = 0; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
712 asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; |
| 35742 | 713 identifier vararg = NULL, kwarg = NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
714 arg_ty arg; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
715 expr_ty varargannotation = NULL, kwargannotation = NULL; |
| 35742 | 716 node *ch; |
| 717 | |
| 718 if (TYPE(n) == parameters) { | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
719 if (NCH(n) == 2) /* () as argument list */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
720 return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
721 NULL, c->c_arena); |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
722 n = CHILD(n, 1); |
| 35742 | 723 } |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
724 assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); |
| 35742 | 725 |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
726 /* first count the number of positional args & defaults */ |
| 35742 | 727 for (i = 0; i < NCH(n); i++) { |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
728 ch = CHILD(n, i); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
729 if (TYPE(ch) == STAR) { |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
730 /* skip star and possible argument */ |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
731 i++; |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
732 i += (TYPE(CHILD(n, i)) == tfpdef |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
733 || TYPE(CHILD(n, i)) == vfpdef); |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
734 break; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
735 } |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
736 if (TYPE(ch) == DOUBLESTAR) break; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
737 if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
738 if (TYPE(ch) == EQUAL) nposdefaults++; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
739 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
740 /* count the number of keyword only args & |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
741 defaults for keyword only args */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
742 for ( ; i < NCH(n); ++i) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
743 ch = CHILD(n, i); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
744 if (TYPE(ch) == DOUBLESTAR) break; |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
745 if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; |
| 35742 | 746 } |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
747 posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
748 if (!posargs && nposargs) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
749 goto error; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
750 kwonlyargs = (nkwonlyargs ? |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
751 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
752 if (!kwonlyargs && nkwonlyargs) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
753 goto error; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
754 posdefaults = (nposdefaults ? |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
755 asdl_seq_new(nposdefaults, c->c_arena) : NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
756 if (!posdefaults && nposdefaults) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
757 goto error; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
758 /* The length of kwonlyargs and kwdefaults are same |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
759 since we set NULL as default for keyword only argument w/o default |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
760 - we have sequence data structure, but no dictionary */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
761 kwdefaults = (nkwonlyargs ? |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
762 asdl_seq_new(nkwonlyargs, c->c_arena) : NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
763 if (!kwdefaults && nkwonlyargs) |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
764 goto error; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
765 |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
766 if (nposargs + nkwonlyargs > 255) { |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
767 ast_error(n, "more than 255 arguments"); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
768 return NULL; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
769 } |
| 35742 | 770 |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
771 /* tfpdef: NAME [':' test] |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
772 vfpdef: NAME |
| 35742 | 773 */ |
| 774 i = 0; | |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
775 j = 0; /* index for defaults */ |
|
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
776 k = 0; /* index for args */ |
| 35742 | 777 while (i < NCH(n)) { |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
778 ch = CHILD(n, i); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
779 switch (TYPE(ch)) { |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
780 case tfpdef: |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
781 case vfpdef: |
| 35742 | 782 /* XXX Need to worry about checking if TYPE(CHILD(n, i+1)) is |
| 783 anything other than EQUAL or a comma? */ | |
| 784 /* XXX Should NCH(n) check be made a separate check? */ | |
| 785 if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) { | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
786 expr_ty expression = ast_for_expr(c, CHILD(n, i + 2)); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
787 if (!expression) |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
788 goto error; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
789 assert(posdefaults != NULL); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
790 asdl_seq_SET(posdefaults, j++, expression); |
| 35742 | 791 i += 2; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
792 found_default = 1; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
793 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
794 else if (found_default) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
795 ast_error(n, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
796 "non-default argument follows default argument"); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
797 goto error; |
| 35742 | 798 } |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
799 arg = compiler_arg(c, ch); |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
800 if (!arg) |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
801 goto error; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
802 asdl_seq_SET(posargs, k++, arg); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
803 |
| 35742 | 804 i += 2; /* the name and the comma */ |
| 805 break; | |
| 806 case STAR: | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
807 if (i+1 >= NCH(n)) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
808 ast_error(CHILD(n, i), "no name for vararg"); |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
809 goto error; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
810 } |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
811 ch = CHILD(n, i+1); /* tfpdef or COMMA */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
812 if (TYPE(ch) == COMMA) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
813 int res = 0; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
814 i += 2; /* now follows keyword only arguments */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
815 res = handle_keywordonly_args(c, n, i, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
816 kwonlyargs, kwdefaults); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
817 if (res == -1) goto error; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
818 i = res; /* res has new position to process */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
819 } |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
820 else if (!strcmp(STR(CHILD(ch, 0)), "None")) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
821 ast_error(CHILD(ch, 0), "assignment to None"); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
822 goto error; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
823 } |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
824 else { |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
825 vararg = NEW_IDENTIFIER(CHILD(ch, 0)); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
826 if (NCH(ch) > 1) { |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
827 /* there is an annotation on the vararg */ |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
828 varargannotation = ast_for_expr(c, CHILD(ch, 2)); |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
829 } |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
830 i += 3; |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
831 if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
832 || TYPE(CHILD(n, i)) == vfpdef)) { |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
833 int res = 0; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
834 res = handle_keywordonly_args(c, n, i, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
835 kwonlyargs, kwdefaults); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
836 if (res == -1) goto error; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
837 i = res; /* res has new position to process */ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
838 } |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
839 } |
| 35742 | 840 break; |
| 841 case DOUBLESTAR: | |
|
41717
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
842 ch = CHILD(n, i+1); /* tfpdef */ |
|
9ad84070b22d
Merged revisions 55328-55341 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41716
diff
changeset
|
843 assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
844 if (!strcmp(STR(CHILD(ch, 0)), "None")) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
845 ast_error(CHILD(ch, 0), "assignment to None"); |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
846 goto error; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
847 } |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
848 kwarg = NEW_IDENTIFIER(CHILD(ch, 0)); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
849 if (NCH(ch) > 1) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
850 /* there is an annotation on the kwarg */ |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
851 kwargannotation = ast_for_expr(c, CHILD(ch, 2)); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
852 } |
| 35742 | 853 i += 3; |
| 854 break; | |
| 855 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
856 PyErr_Format(PyExc_SystemError, |
| 35742 | 857 "unexpected node in varargslist: %d @ %d", |
| 858 TYPE(ch), i); | |
| 859 goto error; | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39783
diff
changeset
|
860 } |
| 35742 | 861 } |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
862 return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg, |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
863 kwargannotation, posdefaults, kwdefaults, c->c_arena); |
| 35742 | 864 error: |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
865 Py_XDECREF(vararg); |
|
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
866 Py_XDECREF(kwarg); |
| 35742 | 867 return NULL; |
| 868 } | |
| 869 | |
| 870 static expr_ty | |
| 871 ast_for_dotted_name(struct compiling *c, const node *n) | |
| 872 { | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
873 expr_ty e; |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
874 identifier id; |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
875 int lineno, col_offset; |
| 35742 | 876 int i; |
| 877 | |
| 878 REQ(n, dotted_name); | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
879 |
|
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
880 lineno = LINENO(n); |
|
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
881 col_offset = n->n_col_offset; |
|
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
882 |
| 35742 | 883 id = NEW_IDENTIFIER(CHILD(n, 0)); |
| 884 if (!id) | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
885 return NULL; |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
886 e = Name(id, Load, lineno, col_offset, c->c_arena); |
| 35742 | 887 if (!e) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
888 return NULL; |
| 35742 | 889 |
| 890 for (i = 2; i < NCH(n); i+=2) { | |
| 891 id = NEW_IDENTIFIER(CHILD(n, i)); | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
892 if (!id) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
893 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
894 e = Attribute(e, id, Load, lineno, col_offset, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
895 if (!e) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
896 return NULL; |
| 35742 | 897 } |
| 898 | |
| 899 return e; | |
| 900 } | |
| 901 | |
| 902 static expr_ty | |
| 903 ast_for_decorator(struct compiling *c, const node *n) | |
| 904 { | |
| 905 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */ | |
| 906 expr_ty d = NULL; | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
907 expr_ty name_expr; |
| 35742 | 908 |
| 909 REQ(n, decorator); | |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
910 REQ(CHILD(n, 0), AT); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
911 REQ(RCHILD(n, -1), NEWLINE); |
| 35742 | 912 |
| 913 name_expr = ast_for_dotted_name(c, CHILD(n, 1)); | |
| 914 if (!name_expr) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
915 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
916 |
| 35742 | 917 if (NCH(n) == 3) { /* No arguments */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
918 d = name_expr; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
919 name_expr = NULL; |
| 35742 | 920 } |
| 921 else if (NCH(n) == 5) { /* Call with no arguments */ | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
922 d = Call(name_expr, NULL, NULL, NULL, NULL, LINENO(n), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
923 n->n_col_offset, c->c_arena); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
924 if (!d) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
925 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
926 name_expr = NULL; |
| 35742 | 927 } |
| 928 else { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
929 d = ast_for_call(c, CHILD(n, 3), name_expr); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
930 if (!d) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
931 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
932 name_expr = NULL; |
| 35742 | 933 } |
| 934 | |
| 935 return d; | |
| 936 } | |
| 937 | |
| 938 static asdl_seq* | |
| 939 ast_for_decorators(struct compiling *c, const node *n) | |
| 940 { | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
941 asdl_seq* decorator_seq; |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
942 expr_ty d; |
| 35742 | 943 int i; |
| 944 | |
| 945 REQ(n, decorators); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
946 decorator_seq = asdl_seq_new(NCH(n), c->c_arena); |
| 35742 | 947 if (!decorator_seq) |
| 948 return NULL; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
949 |
| 35742 | 950 for (i = 0; i < NCH(n); i++) { |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
951 d = ast_for_decorator(c, CHILD(n, i)); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
952 if (!d) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
953 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
954 asdl_seq_SET(decorator_seq, i, d); |
| 35742 | 955 } |
| 956 return decorator_seq; | |
| 957 } | |
| 958 | |
| 959 static stmt_ty | |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
960 ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 35742 | 961 { |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
962 /* funcdef: 'def' NAME parameters ['->' test] ':' suite */ |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
963 identifier name; |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
964 arguments_ty args; |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
965 asdl_seq *body; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
966 expr_ty returns = NULL; |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
967 int name_i = 1; |
| 35742 | 968 |
| 969 REQ(n, funcdef); | |
| 970 | |
| 971 name = NEW_IDENTIFIER(CHILD(n, name_i)); | |
| 972 if (!name) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
973 return NULL; |
| 35742 | 974 else if (!strcmp(STR(CHILD(n, name_i)), "None")) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
975 ast_error(CHILD(n, name_i), "assignment to None"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
976 return NULL; |
| 35742 | 977 } |
| 978 args = ast_for_arguments(c, CHILD(n, name_i + 1)); | |
| 979 if (!args) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
980 return NULL; |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
981 if (TYPE(CHILD(n, name_i+2)) == RARROW) { |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
982 returns = ast_for_expr(c, CHILD(n, name_i + 3)); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
983 if (!returns) |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
984 return NULL; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
985 name_i += 2; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
986 } |
| 35742 | 987 body = ast_for_suite(c, CHILD(n, name_i + 3)); |
| 988 if (!body) | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
989 return NULL; |
| 35742 | 990 |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
991 return FunctionDef(name, args, body, decorator_seq, returns, LINENO(n), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
992 n->n_col_offset, c->c_arena); |
| 35742 | 993 } |
| 994 | |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
995 static stmt_ty |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
996 ast_for_decorated(struct compiling *c, const node *n) |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
997 { |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
998 /* decorated: decorators (classdef | funcdef) */ |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
999 stmt_ty thing = NULL; |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1000 asdl_seq *decorator_seq = NULL; |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1001 |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1002 REQ(n, decorated); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1003 |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1004 decorator_seq = ast_for_decorators(c, CHILD(n, 0)); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1005 if (!decorator_seq) |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1006 return NULL; |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1007 |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1008 assert(TYPE(CHILD(n, 1)) == funcdef || |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1009 TYPE(CHILD(n, 1)) == classdef); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1010 |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1011 if (TYPE(CHILD(n, 1)) == funcdef) { |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1012 thing = ast_for_funcdef(c, CHILD(n, 1), decorator_seq); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1013 } else if (TYPE(CHILD(n, 1)) == classdef) { |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1014 thing = ast_for_classdef(c, CHILD(n, 1), decorator_seq); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1015 } |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1016 return thing; |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1017 } |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
1018 |
| 35742 | 1019 static expr_ty |
| 1020 ast_for_lambdef(struct compiling *c, const node *n) | |
| 1021 { | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1022 /* lambdef: 'lambda' [varargslist] ':' test |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1023 lambdef_nocond: 'lambda' [varargslist] ':' test_nocond */ |
| 35742 | 1024 arguments_ty args; |
| 1025 expr_ty expression; | |
| 1026 | |
| 1027 if (NCH(n) == 3) { | |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1028 args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1029 NULL, c->c_arena); |
| 35742 | 1030 if (!args) |
| 1031 return NULL; | |
| 1032 expression = ast_for_expr(c, CHILD(n, 2)); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1033 if (!expression) |
| 35742 | 1034 return NULL; |
| 1035 } | |
| 1036 else { | |
| 1037 args = ast_for_arguments(c, CHILD(n, 1)); | |
| 1038 if (!args) | |
| 1039 return NULL; | |
| 1040 expression = ast_for_expr(c, CHILD(n, 3)); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1041 if (!expression) |
| 35742 | 1042 return NULL; |
| 1043 } | |
| 1044 | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1045 return Lambda(args, expression, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1046 } |
| 1047 | |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1048 static expr_ty |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1049 ast_for_ifexpr(struct compiling *c, const node *n) |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1050 { |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1051 /* test: or_test 'if' or_test 'else' test */ |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1052 expr_ty expression, body, orelse; |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1053 |
|
36676
67c74efad8c2
Fix old not-reading-pep-308-right artifact.
Thomas Wouters <thomas@python.org>
parents:
36670
diff
changeset
|
1054 assert(NCH(n) == 5); |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1055 body = ast_for_expr(c, CHILD(n, 0)); |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1056 if (!body) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1057 return NULL; |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1058 expression = ast_for_expr(c, CHILD(n, 2)); |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1059 if (!expression) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1060 return NULL; |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1061 orelse = ast_for_expr(c, CHILD(n, 4)); |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1062 if (!orelse) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1063 return NULL; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1064 return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1065 c->c_arena); |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1066 } |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1067 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1068 /* |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1069 Count the number of 'for' loops in a comprehension. |
| 35742 | 1070 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1071 Helper for ast_for_comprehension(). |
| 35742 | 1072 */ |
| 1073 | |
| 1074 static int | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1075 count_comp_fors(const node *n) |
| 35742 | 1076 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1077 int n_fors = 0; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1078 node *ch = CHILD(n, 1); |
| 35742 | 1079 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1080 count_comp_for: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1081 n_fors++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1082 REQ(ch, comp_for); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1083 if (NCH(ch) == 5) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1084 ch = CHILD(ch, 4); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1085 else |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1086 return n_fors; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1087 count_comp_iter: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1088 REQ(ch, comp_iter); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1089 ch = CHILD(ch, 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1090 if (TYPE(ch) == comp_for) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1091 goto count_comp_for; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1092 else if (TYPE(ch) == comp_if) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1093 if (NCH(ch) == 3) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1094 ch = CHILD(ch, 2); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1095 goto count_comp_iter; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1096 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1097 else |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1098 return n_fors; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1099 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1100 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1101 /* Should never be reached */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1102 PyErr_SetString(PyExc_SystemError, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1103 "logic error in count_comp_fors"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1104 return -1; |
| 35742 | 1105 } |
| 1106 | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1107 /* Count the number of 'if' statements in a comprehension. |
| 35742 | 1108 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1109 Helper for ast_for_comprehension(). |
| 35742 | 1110 */ |
| 1111 | |
| 1112 static int | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1113 count_comp_ifs(const node *n) |
| 35742 | 1114 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1115 int n_ifs = 0; |
| 35742 | 1116 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1117 while (1) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1118 REQ(n, comp_iter); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1119 if (TYPE(CHILD(n, 0)) == comp_for) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1120 return n_ifs; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1121 n = CHILD(n, 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1122 REQ(n, comp_if); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1123 n_ifs++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1124 if (NCH(n) == 2) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1125 return n_ifs; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1126 n = CHILD(n, 2); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1127 } |
| 35742 | 1128 } |
| 1129 | |
| 1130 static expr_ty | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1131 ast_for_comprehension(struct compiling *c, const node *n, int type) |
| 35742 | 1132 { |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1133 /* testlist_comp: test ( comp_for | (',' test)* [','] ) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1134 argument: [test '='] test [comp_for] # Really [keyword '='] test */ |
| 35742 | 1135 expr_ty elt; |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1136 asdl_seq *comps; |
| 35742 | 1137 int i, n_fors; |
| 1138 node *ch; | |
| 1139 | |
| 1140 assert(NCH(n) > 1); | |
| 1141 | |
| 1142 elt = ast_for_expr(c, CHILD(n, 0)); | |
| 1143 if (!elt) | |
| 1144 return NULL; | |
| 1145 | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1146 n_fors = count_comp_fors(n); |
| 35742 | 1147 if (n_fors == -1) |
| 1148 return NULL; | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1149 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1150 comps = asdl_seq_new(n_fors, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1151 if (!comps) |
| 35742 | 1152 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1153 |
| 35742 | 1154 ch = CHILD(n, 1); |
| 1155 for (i = 0; i < n_fors; i++) { | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1156 comprehension_ty comp; |
| 35742 | 1157 asdl_seq *t; |
| 1158 expr_ty expression; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1159 node *for_ch; |
| 35742 | 1160 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1161 REQ(ch, comp_for); |
| 35742 | 1162 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1163 for_ch = CHILD(ch, 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1164 t = ast_for_exprlist(c, for_ch, Store); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1165 if (!t) |
| 35742 | 1166 return NULL; |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1167 expression = ast_for_expr(c, CHILD(ch, 3)); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1168 if (!expression) |
| 35742 | 1169 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1170 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1171 /* Check the # of children rather than the length of t, since |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1172 (x for x, in ...) has 1 element in t, but still requires a Tuple. */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1173 if (NCH(for_ch) == 1) |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1174 comp = comprehension((expr_ty)asdl_seq_GET(t, 0), expression, |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1175 NULL, c->c_arena); |
| 35742 | 1176 else |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1177 comp = comprehension(Tuple(t, Store, LINENO(ch), ch->n_col_offset, |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1178 c->c_arena), |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1179 expression, NULL, c->c_arena); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1180 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1181 if (!comp) |
| 35742 | 1182 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1183 |
| 35742 | 1184 if (NCH(ch) == 5) { |
| 1185 int j, n_ifs; | |
| 1186 asdl_seq *ifs; | |
| 1187 | |
| 1188 ch = CHILD(ch, 4); | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1189 n_ifs = count_comp_ifs(ch); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1190 if (n_ifs == -1) |
| 35742 | 1191 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1192 |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1193 ifs = asdl_seq_new(n_ifs, c->c_arena); |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1194 if (!ifs) |
| 35742 | 1195 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1196 |
| 35742 | 1197 for (j = 0; j < n_ifs; j++) { |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1198 REQ(ch, comp_iter); |
| 35742 | 1199 ch = CHILD(ch, 0); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1200 REQ(ch, comp_if); |
| 35742 | 1201 |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1202 expression = ast_for_expr(c, CHILD(ch, 1)); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1203 if (!expression) |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1204 return NULL; |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
1205 asdl_seq_SET(ifs, j, expression); |
| 35742 | 1206 if (NCH(ch) == 3) |
| 1207 ch = CHILD(ch, 2); | |
| 1208 } | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1209 /* on exit, must guarantee that ch is a comp_for */ |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1210 if (TYPE(ch) == comp_iter) |
| 35742 | 1211 ch = CHILD(ch, 0); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1212 comp->ifs = ifs; |
| 35742 | 1213 } |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1214 asdl_seq_SET(comps, i, comp); |
| 35742 | 1215 } |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1216 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1217 if (type == COMP_GENEXP) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1218 return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1219 else if (type == COMP_LISTCOMP) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1220 return ListComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1221 else if (type == COMP_SETCOMP) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1222 return SetComp(elt, comps, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1223 else |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1224 /* Should never happen */ |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1225 return NULL; |
| 35742 | 1226 } |
| 1227 | |
| 1228 static expr_ty | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1229 ast_for_genexp(struct compiling *c, const node *n) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1230 { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1231 assert(TYPE(n) == (testlist_comp) || TYPE(n) == (argument)); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1232 return ast_for_comprehension(c, n, COMP_GENEXP); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1233 } |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1234 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1235 static expr_ty |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1236 ast_for_listcomp(struct compiling *c, const node *n) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1237 { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1238 assert(TYPE(n) == (testlist_comp)); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1239 return ast_for_comprehension(c, n, COMP_LISTCOMP); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1240 } |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1241 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1242 static expr_ty |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1243 ast_for_setcomp(struct compiling *c, const node *n) |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1244 { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1245 assert(TYPE(n) == (dictorsetmaker)); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1246 return ast_for_comprehension(c, n, COMP_SETCOMP); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1247 } |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1248 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1249 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1250 static expr_ty |
| 35742 | 1251 ast_for_atom(struct compiling *c, const node *n) |
| 1252 { | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1253 /* atom: '(' [yield_expr|testlist_comp] ')' | '[' [testlist_comp] ']' |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1254 | '{' [dictmaker|testlist_comp] '}' | NAME | NUMBER | STRING+ |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
1255 | '...' | 'None' | 'True' | 'False' |
| 35742 | 1256 */ |
| 1257 node *ch = CHILD(n, 0); | |
| 40894 | 1258 int bytesmode = 0; |
| 35742 | 1259 |
| 1260 switch (TYPE(ch)) { | |
| 1261 case NAME: | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1262 /* All names start in Load context, but may later be |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1263 changed. */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1264 return Name(NEW_IDENTIFIER(ch), Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1265 case STRING: { |
| 40894 | 1266 PyObject *str = parsestrplus(c, n, &bytesmode); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1267 if (!str) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1268 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1269 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1270 PyArena_AddPyObject(c->c_arena, str); |
| 40894 | 1271 if (bytesmode) |
| 1272 return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena); | |
| 1273 else | |
| 1274 return Str(str, LINENO(n), n->n_col_offset, c->c_arena); | |
| 35742 | 1275 } |
| 1276 case NUMBER: { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1277 PyObject *pynum = parsenumber(STR(ch)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1278 if (!pynum) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1279 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1280 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1281 PyArena_AddPyObject(c->c_arena, pynum); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1282 return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1283 } |
|
41230
b1a18c7ed5ca
Make ELLIPSIS a separate token. This makes it a syntax error to write ". . ." for Ellipsis.
Georg Brandl <georg@python.org>
parents:
41226
diff
changeset
|
1284 case ELLIPSIS: /* Ellipsis */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1285 return Ellipsis(LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1286 case LPAR: /* some parenthesized expressions */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1287 ch = CHILD(n, 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1288 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1289 if (TYPE(ch) == RPAR) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1290 return Tuple(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1291 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1292 if (TYPE(ch) == yield_expr) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1293 return ast_for_expr(c, ch); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1294 |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1295 /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1296 if ((NCH(ch) > 1) && (TYPE(CHILD(ch, 1)) == comp_for)) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1297 return ast_for_genexp(c, ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1298 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1299 return ast_for_testlist(c, ch); |
| 35742 | 1300 case LSQB: /* list (or list comprehension) */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1301 ch = CHILD(n, 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1302 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1303 if (TYPE(ch) == RSQB) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1304 return List(NULL, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1305 |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1306 REQ(ch, testlist_comp); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1307 if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1308 asdl_seq *elts = seq_for_testlist(c, ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1309 if (!elts) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1310 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1311 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1312 return List(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1313 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1314 else |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1315 return ast_for_listcomp(c, ch); |
| 35742 | 1316 case LBRACE: { |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1317 /* dictorsetmaker: test ':' test (',' test ':' test)* [','] | |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1318 * test (gen_for | (',' test)* [',']) */ |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1319 int i, size; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1320 asdl_seq *keys, *values; |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1321 |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
1322 ch = CHILD(n, 1); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1323 if (TYPE(ch) == RBRACE) { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1324 /* it's an empty dict */ |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1325 return Dict(NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1326 } else if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1327 /* it's a simple set */ |
|
41482
a5e410639e24
Make it compile with C89.
Guido van Rossum <guido@python.org>
parents:
41464
diff
changeset
|
1328 asdl_seq *elts; |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1329 size = (NCH(ch) + 1) / 2; /* +1 in case no trailing comma */ |
|
41482
a5e410639e24
Make it compile with C89.
Guido van Rossum <guido@python.org>
parents:
41464
diff
changeset
|
1330 elts = asdl_seq_new(size, c->c_arena); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1331 if (!elts) |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1332 return NULL; |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1333 for (i = 0; i < NCH(ch); i += 2) { |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1334 expr_ty expression; |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1335 expression = ast_for_expr(c, CHILD(ch, i)); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1336 if (!expression) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1337 return NULL; |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1338 asdl_seq_SET(elts, i / 2, expression); |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1339 } |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1340 return Set(elts, LINENO(n), n->n_col_offset, c->c_arena); |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1341 } else if (TYPE(CHILD(ch, 1)) == comp_for) { |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1342 /* it's a set comprehension */ |
|
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1343 return ast_for_setcomp(c, ch); |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1344 } else { |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1345 /* it's a dict */ |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1346 size = (NCH(ch) + 1) / 4; /* +1 in case no trailing comma */ |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1347 keys = asdl_seq_new(size, c->c_arena); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1348 if (!keys) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1349 return NULL; |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1350 |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1351 values = asdl_seq_new(size, c->c_arena); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1352 if (!values) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1353 return NULL; |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1354 |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1355 for (i = 0; i < NCH(ch); i += 4) { |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1356 expr_ty expression; |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1357 |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1358 expression = ast_for_expr(c, CHILD(ch, i)); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1359 if (!expression) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1360 return NULL; |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1361 |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1362 asdl_seq_SET(keys, i / 4, expression); |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1363 |
|
39693
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1364 expression = ast_for_expr(c, CHILD(ch, i + 2)); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1365 if (!expression) |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1366 return NULL; |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1367 |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1368 asdl_seq_SET(values, i / 4, expression); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1369 } |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1370 return Dict(keys, values, LINENO(n), n->n_col_offset, c->c_arena); |
|
d0b83c4630c0
SF patch 1547796 by Georg Brandl -- set literals.
Guido van Rossum <guido@python.org>
parents:
39678
diff
changeset
|
1371 } |
| 35742 | 1372 } |
| 1373 default: | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1374 PyErr_Format(PyExc_SystemError, "unhandled atom %d", TYPE(ch)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1375 return NULL; |
| 35742 | 1376 } |
| 1377 } | |
| 1378 | |
| 1379 static slice_ty | |
| 1380 ast_for_slice(struct compiling *c, const node *n) | |
| 1381 { | |
| 1382 node *ch; | |
| 1383 expr_ty lower = NULL, upper = NULL, step = NULL; | |
| 1384 | |
| 1385 REQ(n, subscript); | |
| 1386 | |
| 1387 /* | |
|
39783
7a0ad110e91e
Patch #1550786: ellipsis literal.
Georg Brandl <georg@python.org>
parents:
39782
diff
changeset
|
1388 subscript: test | [test] ':' [test] [sliceop] |
| 35742 | 1389 sliceop: ':' [test] |
| 1390 */ | |
| 1391 ch = CHILD(n, 0); | |
| 1392 if (NCH(n) == 1 && TYPE(ch) == test) { | |
| 1393 /* 'step' variable hold no significance in terms of being used over | |
| 1394 other vars */ | |
| 1395 step = ast_for_expr(c, ch); | |
| 1396 if (!step) | |
| 1397 return NULL; | |
| 1398 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1399 return Index(step, c->c_arena); |
| 35742 | 1400 } |
| 1401 | |
| 1402 if (TYPE(ch) == test) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1403 lower = ast_for_expr(c, ch); |
| 35742 | 1404 if (!lower) |
| 1405 return NULL; | |
| 1406 } | |
| 1407 | |
| 1408 /* If there's an upper bound it's in the second or third position. */ | |
| 1409 if (TYPE(ch) == COLON) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1410 if (NCH(n) > 1) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1411 node *n2 = CHILD(n, 1); |
| 35742 | 1412 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1413 if (TYPE(n2) == test) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1414 upper = ast_for_expr(c, n2); |
| 35742 | 1415 if (!upper) |
| 1416 return NULL; | |
| 1417 } | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1418 } |
| 35742 | 1419 } else if (NCH(n) > 2) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1420 node *n2 = CHILD(n, 2); |
| 35742 | 1421 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1422 if (TYPE(n2) == test) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1423 upper = ast_for_expr(c, n2); |
| 35742 | 1424 if (!upper) |
| 1425 return NULL; | |
| 1426 } | |
| 1427 } | |
| 1428 | |
| 1429 ch = CHILD(n, NCH(n) - 1); | |
| 1430 if (TYPE(ch) == sliceop) { | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1431 if (NCH(ch) == 1) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1432 /* No expression, so step is None */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1433 ch = CHILD(ch, 0); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1434 step = Name(new_identifier("None", c->c_arena), Load, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1435 LINENO(ch), ch->n_col_offset, c->c_arena); |
| 35742 | 1436 if (!step) |
| 1437 return NULL; | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1438 } else { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1439 ch = CHILD(ch, 1); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1440 if (TYPE(ch) == test) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1441 step = ast_for_expr(c, ch); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1442 if (!step) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1443 return NULL; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1444 } |
| 35742 | 1445 } |
| 1446 } | |
| 1447 | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1448 return Slice(lower, upper, step, c->c_arena); |
| 35742 | 1449 } |
| 1450 | |
| 1451 static expr_ty | |
| 1452 ast_for_binop(struct compiling *c, const node *n) | |
| 1453 { | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1454 /* Must account for a sequence of expressions. |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1455 How should A op B op C by represented? |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1456 BinOp(BinOp(A, op, B), op, C). |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1457 */ |
| 35742 | 1458 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1459 int i, nops; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1460 expr_ty expr1, expr2, result; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1461 operator_ty newoperator; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1462 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1463 expr1 = ast_for_expr(c, CHILD(n, 0)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1464 if (!expr1) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1465 return NULL; |
| 35742 | 1466 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1467 expr2 = ast_for_expr(c, CHILD(n, 2)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1468 if (!expr2) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1469 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1470 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1471 newoperator = get_operator(CHILD(n, 1)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1472 if (!newoperator) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1473 return NULL; |
| 35742 | 1474 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1475 result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1476 c->c_arena); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1477 if (!result) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1478 return NULL; |
| 35742 | 1479 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1480 nops = (NCH(n) - 1) / 2; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1481 for (i = 1; i < nops; i++) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1482 expr_ty tmp_result, tmp; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1483 const node* next_oper = CHILD(n, i * 2 + 1); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1484 |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1485 newoperator = get_operator(next_oper); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1486 if (!newoperator) |
| 35742 | 1487 return NULL; |
| 1488 | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1489 tmp = ast_for_expr(c, CHILD(n, i * 2 + 2)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1490 if (!tmp) |
| 35742 | 1491 return NULL; |
| 1492 | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1493 tmp_result = BinOp(result, newoperator, tmp, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1494 LINENO(next_oper), next_oper->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1495 c->c_arena); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1496 if (!tmp) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1497 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1498 result = tmp_result; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1499 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
1500 return result; |
| 35742 | 1501 } |
| 1502 | |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1503 static expr_ty |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1504 ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1505 { |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1506 /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1507 subscriptlist: subscript (',' subscript)* [','] |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1508 subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1509 */ |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1510 REQ(n, trailer); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1511 if (TYPE(CHILD(n, 0)) == LPAR) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1512 if (NCH(n) == 2) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1513 return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n), |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1514 n->n_col_offset, c->c_arena); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1515 else |
|
36679
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1516 return ast_for_call(c, CHILD(n, 1), left_expr); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1517 } |
|
36679
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1518 else if (TYPE(CHILD(n, 0)) == DOT ) { |
|
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1519 return Attribute(left_expr, NEW_IDENTIFIER(CHILD(n, 1)), Load, |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1520 LINENO(n), n->n_col_offset, c->c_arena); |
|
36679
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1521 } |
|
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1522 else { |
|
e1391910b026
Simplify ast_for_trailer() in anticipation of more changes.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36676
diff
changeset
|
1523 REQ(CHILD(n, 0), LSQB); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1524 REQ(CHILD(n, 2), RSQB); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1525 n = CHILD(n, 1); |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1526 if (NCH(n) == 1) { |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1527 slice_ty slc = ast_for_slice(c, CHILD(n, 0)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1528 if (!slc) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1529 return NULL; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1530 return Subscript(left_expr, slc, Load, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1531 c->c_arena); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1532 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1533 else { |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1534 /* The grammar is ambiguous here. The ambiguity is resolved |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1535 by treating the sequence as a tuple literal if there are |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1536 no slice features. |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1537 */ |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1538 int j; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1539 slice_ty slc; |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1540 expr_ty e; |
|
40941
2ca14d6a369b
Whoops, fix build breakage. There were still a few uses of the bool type.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40894
diff
changeset
|
1541 int simple = 1; |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1542 asdl_seq *slices, *elts; |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1543 slices = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1544 if (!slices) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1545 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1546 for (j = 0; j < NCH(n); j += 2) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1547 slc = ast_for_slice(c, CHILD(n, j)); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1548 if (!slc) |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1549 return NULL; |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1550 if (slc->kind != Index_kind) |
|
40941
2ca14d6a369b
Whoops, fix build breakage. There were still a few uses of the bool type.
Neal Norwitz <nnorwitz@gmail.com>
parents:
40894
diff
changeset
|
1551 simple = 0; |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1552 asdl_seq_SET(slices, j / 2, slc); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1553 } |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1554 if (!simple) { |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1555 return Subscript(left_expr, ExtSlice(slices, c->c_arena), |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1556 Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1557 } |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1558 /* extract Index values and put them in a Tuple */ |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1559 elts = asdl_seq_new(asdl_seq_LEN(slices), c->c_arena); |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36786
diff
changeset
|
1560 if (!elts) |
|
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36786
diff
changeset
|
1561 return NULL; |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1562 for (j = 0; j < asdl_seq_LEN(slices); ++j) { |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1563 slc = (slice_ty)asdl_seq_GET(slices, j); |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1564 assert(slc->kind == Index_kind && slc->v.Index.value); |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1565 asdl_seq_SET(elts, j, slc->v.Index.value); |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1566 } |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1567 e = Tuple(elts, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
36686
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1568 if (!e) |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1569 return NULL; |
|
8611f39cd82b
Fix parsing of subscriptlist.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36682
diff
changeset
|
1570 return Subscript(left_expr, Index(e, c->c_arena), |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1571 Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1572 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1573 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1574 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1575 |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1576 static expr_ty |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1577 ast_for_factor(struct compiling *c, const node *n) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1578 { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1579 node *pfactor, *ppower, *patom, *pnum; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1580 expr_ty expression; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1581 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1582 /* If the unary - operator is applied to a constant, don't generate |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1583 a UNARY_NEGATIVE opcode. Just store the approriate value as a |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1584 constant. The peephole optimizer already does something like |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1585 this but it doesn't handle the case where the constant is |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1586 (sys.maxint - 1). In that case, we want a PyIntObject, not a |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1587 PyLongObject. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1588 */ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1589 if (TYPE(CHILD(n, 0)) == MINUS |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1590 && NCH(n) == 2 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1591 && TYPE((pfactor = CHILD(n, 1))) == factor |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1592 && NCH(pfactor) == 1 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1593 && TYPE((ppower = CHILD(pfactor, 0))) == power |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1594 && NCH(ppower) == 1 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1595 && TYPE((patom = CHILD(ppower, 0))) == atom |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1596 && TYPE((pnum = CHILD(patom, 0))) == NUMBER) { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1597 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1598 if (s == NULL) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1599 return NULL; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1600 s[0] = '-'; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1601 strcpy(s + 1, STR(pnum)); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1602 PyObject_FREE(STR(pnum)); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1603 STR(pnum) = s; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1604 return ast_for_atom(c, patom); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1605 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1606 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1607 expression = ast_for_expr(c, CHILD(n, 1)); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1608 if (!expression) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1609 return NULL; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1610 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1611 switch (TYPE(CHILD(n, 0))) { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1612 case PLUS: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1613 return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1614 c->c_arena); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1615 case MINUS: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1616 return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1617 c->c_arena); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1618 case TILDE: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1619 return UnaryOp(Invert, expression, LINENO(n), |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1620 n->n_col_offset, c->c_arena); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1621 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1622 PyErr_Format(PyExc_SystemError, "unhandled factor: %d", |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1623 TYPE(CHILD(n, 0))); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1624 return NULL; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1625 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1626 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1627 static expr_ty |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1628 ast_for_power(struct compiling *c, const node *n) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1629 { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1630 /* power: atom trailer* ('**' factor)* |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1631 */ |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1632 int i; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1633 expr_ty e, tmp; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1634 REQ(n, power); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1635 e = ast_for_atom(c, CHILD(n, 0)); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1636 if (!e) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1637 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1638 if (NCH(n) == 1) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1639 return e; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1640 for (i = 1; i < NCH(n); i++) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1641 node *ch = CHILD(n, i); |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1642 if (TYPE(ch) != trailer) |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1643 break; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1644 tmp = ast_for_trailer(c, ch, e); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1645 if (!tmp) |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1646 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1647 tmp->lineno = e->lineno; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1648 tmp->col_offset = e->col_offset; |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1649 e = tmp; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1650 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1651 if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1652 expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1653 if (!f) |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1654 return NULL; |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1655 tmp = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, c->c_arena); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1656 if (!tmp) |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1657 return NULL; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1658 e = tmp; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1659 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1660 return e; |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1661 } |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1662 |
|
41699
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1663 static expr_ty |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1664 ast_for_starred(struct compiling *c, const node *n) |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1665 { |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1666 expr_ty tmp; |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1667 REQ(n, star_expr); |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1668 |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1669 tmp = ast_for_expr(c, CHILD(n, 1)); |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1670 if (!tmp) |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1671 return NULL; |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1672 |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1673 /* The Load context is changed later. */ |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1674 return Starred(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1675 } |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1676 |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1677 |
| 35742 | 1678 /* Do not name a variable 'expr'! Will cause a compile error. |
| 1679 */ | |
| 1680 | |
| 1681 static expr_ty | |
| 1682 ast_for_expr(struct compiling *c, const node *n) | |
| 1683 { | |
| 1684 /* handle the full range of simple expressions | |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1685 test: or_test ['if' or_test 'else' test] | lambdef |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1686 test_nocond: or_test | lambdef_nocond |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1687 or_test: and_test ('or' and_test)* |
| 35742 | 1688 and_test: not_test ('and' not_test)* |
| 1689 not_test: 'not' not_test | comparison | |
| 1690 comparison: expr (comp_op expr)* | |
| 1691 expr: xor_expr ('|' xor_expr)* | |
| 1692 xor_expr: and_expr ('^' and_expr)* | |
| 1693 and_expr: shift_expr ('&' shift_expr)* | |
| 1694 shift_expr: arith_expr (('<<'|'>>') arith_expr)* | |
| 1695 arith_expr: term (('+'|'-') term)* | |
| 1696 term: factor (('*'|'/'|'%'|'//') factor)* | |
| 1697 factor: ('+'|'-'|'~') factor | power | |
| 1698 power: atom trailer* ('**' factor)* | |
| 1699 */ | |
| 1700 | |
| 1701 asdl_seq *seq; | |
| 1702 int i; | |
| 1703 | |
| 1704 loop: | |
| 1705 switch (TYPE(n)) { | |
| 1706 case test: | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1707 case test_nocond: |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1708 if (TYPE(CHILD(n, 0)) == lambdef || |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1709 TYPE(CHILD(n, 0)) == lambdef_nocond) |
| 35742 | 1710 return ast_for_lambdef(c, CHILD(n, 0)); |
|
36666
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1711 else if (NCH(n) > 1) |
|
6165927431d6
PEP 308 implementation, including minor refdocs and some testcases. It
Thomas Wouters <thomas@python.org>
parents:
36444
diff
changeset
|
1712 return ast_for_ifexpr(c, n); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1713 /* Fallthrough */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1714 case or_test: |
| 35742 | 1715 case and_test: |
| 1716 if (NCH(n) == 1) { | |
| 1717 n = CHILD(n, 0); | |
| 1718 goto loop; | |
| 1719 } | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1720 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 35742 | 1721 if (!seq) |
| 1722 return NULL; | |
| 1723 for (i = 0; i < NCH(n); i += 2) { | |
| 1724 expr_ty e = ast_for_expr(c, CHILD(n, i)); | |
| 1725 if (!e) | |
| 1726 return NULL; | |
| 1727 asdl_seq_SET(seq, i / 2, e); | |
| 1728 } | |
| 1729 if (!strcmp(STR(CHILD(n, 1)), "and")) | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1730 return BoolOp(And, seq, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1731 c->c_arena); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1732 assert(!strcmp(STR(CHILD(n, 1)), "or")); |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1733 return BoolOp(Or, seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1734 case not_test: |
| 1735 if (NCH(n) == 1) { | |
| 1736 n = CHILD(n, 0); | |
| 1737 goto loop; | |
| 1738 } | |
| 1739 else { | |
| 1740 expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | |
| 1741 if (!expression) | |
| 1742 return NULL; | |
| 1743 | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1744 return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1745 c->c_arena); |
| 35742 | 1746 } |
| 1747 case comparison: | |
| 1748 if (NCH(n) == 1) { | |
| 1749 n = CHILD(n, 0); | |
| 1750 goto loop; | |
| 1751 } | |
| 1752 else { | |
| 1753 expr_ty expression; | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1754 asdl_int_seq *ops; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1755 asdl_seq *cmps; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1756 ops = asdl_int_seq_new(NCH(n) / 2, c->c_arena); |
| 35742 | 1757 if (!ops) |
| 1758 return NULL; | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1759 cmps = asdl_seq_new(NCH(n) / 2, c->c_arena); |
| 35742 | 1760 if (!cmps) { |
| 1761 return NULL; | |
| 1762 } | |
| 1763 for (i = 1; i < NCH(n); i += 2) { | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1764 cmpop_ty newoperator; |
| 35742 | 1765 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1766 newoperator = ast_for_comp_op(CHILD(n, i)); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1767 if (!newoperator) { |
| 35742 | 1768 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1769 } |
| 35742 | 1770 |
| 1771 expression = ast_for_expr(c, CHILD(n, i + 1)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1772 if (!expression) { |
| 35742 | 1773 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1774 } |
| 35742 | 1775 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1776 asdl_seq_SET(ops, i / 2, newoperator); |
| 35742 | 1777 asdl_seq_SET(cmps, i / 2, expression); |
| 1778 } | |
| 1779 expression = ast_for_expr(c, CHILD(n, 0)); | |
|
35873
76ed7fdc9d85
Fix a whole bunch of potential memory leaks (and some real ones too)
Neal Norwitz <nnorwitz@gmail.com>
parents:
35870
diff
changeset
|
1780 if (!expression) { |
| 35742 | 1781 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1782 } |
| 35742 | 1783 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1784 return Compare(expression, ops, cmps, LINENO(n), |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1785 n->n_col_offset, c->c_arena); |
| 35742 | 1786 } |
| 1787 break; | |
| 1788 | |
|
41699
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1789 case star_expr: |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1790 if (TYPE(CHILD(n, 0)) == STAR) { |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1791 return ast_for_starred(c, n); |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1792 } |
|
e16c8e5c5ca4
Merged revisions 55225-55227,55229-55269 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41698
diff
changeset
|
1793 /* Fallthrough */ |
| 35742 | 1794 /* The next five cases all handle BinOps. The main body of code |
| 1795 is the same in each case, but the switch turned inside out to | |
| 1796 reuse the code for each type of operator. | |
| 1797 */ | |
| 1798 case expr: | |
| 1799 case xor_expr: | |
| 1800 case and_expr: | |
| 1801 case shift_expr: | |
| 1802 case arith_expr: | |
| 1803 case term: | |
| 1804 if (NCH(n) == 1) { | |
| 1805 n = CHILD(n, 0); | |
| 1806 goto loop; | |
| 1807 } | |
| 1808 return ast_for_binop(c, n); | |
| 1809 case yield_expr: { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1810 expr_ty exp = NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1811 if (NCH(n) == 2) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1812 exp = ast_for_testlist(c, CHILD(n, 1)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1813 if (!exp) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1814 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1815 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1816 return Yield(exp, LINENO(n), n->n_col_offset, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1817 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1818 case factor: |
| 35742 | 1819 if (NCH(n) == 1) { |
| 1820 n = CHILD(n, 0); | |
| 1821 goto loop; | |
| 1822 } | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1823 return ast_for_factor(c, n); |
|
35799
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1824 case power: |
|
842caa245917
Refactor code for translating "power" nodes.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35798
diff
changeset
|
1825 return ast_for_power(c, n); |
| 35742 | 1826 default: |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
1827 PyErr_Format(PyExc_SystemError, "unhandled expr: %d", TYPE(n)); |
| 35742 | 1828 return NULL; |
| 1829 } | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1830 /* should never get here unless if error is set */ |
| 35742 | 1831 return NULL; |
| 1832 } | |
| 1833 | |
| 1834 static expr_ty | |
| 1835 ast_for_call(struct compiling *c, const node *n, expr_ty func) | |
| 1836 { | |
| 1837 /* | |
| 1838 arglist: (argument ',')* (argument [',']| '*' test [',' '**' test] | |
| 1839 | '**' test) | |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1840 argument: [test '='] test [comp_for] # Really [keyword '='] test |
| 35742 | 1841 */ |
| 1842 | |
| 1843 int i, nargs, nkeywords, ngens; | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1844 asdl_seq *args; |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
1845 asdl_seq *keywords; |
| 35742 | 1846 expr_ty vararg = NULL, kwarg = NULL; |
| 1847 | |
| 1848 REQ(n, arglist); | |
| 1849 | |
| 1850 nargs = 0; | |
| 1851 nkeywords = 0; | |
| 1852 ngens = 0; | |
| 1853 for (i = 0; i < NCH(n); i++) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1854 node *ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1855 if (TYPE(ch) == argument) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1856 if (NCH(ch) == 1) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1857 nargs++; |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1858 else if (TYPE(CHILD(ch, 1)) == comp_for) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1859 ngens++; |
| 35742 | 1860 else |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1861 nkeywords++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1862 } |
| 35742 | 1863 } |
| 1864 if (ngens > 1 || (ngens && (nargs || nkeywords))) { | |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
1865 ast_error(n, "Generator expression must be parenthesized " |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1866 "if not sole argument"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1867 return NULL; |
| 35742 | 1868 } |
| 1869 | |
| 1870 if (nargs + nkeywords + ngens > 255) { | |
| 1871 ast_error(n, "more than 255 arguments"); | |
| 1872 return NULL; | |
| 1873 } | |
| 1874 | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1875 args = asdl_seq_new(nargs + ngens, c->c_arena); |
| 35742 | 1876 if (!args) |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1877 return NULL; |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1878 keywords = asdl_seq_new(nkeywords, c->c_arena); |
| 35742 | 1879 if (!keywords) |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1880 return NULL; |
| 35742 | 1881 nargs = 0; |
| 1882 nkeywords = 0; | |
| 1883 for (i = 0; i < NCH(n); i++) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1884 node *ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1885 if (TYPE(ch) == argument) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1886 expr_ty e; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1887 if (NCH(ch) == 1) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1888 if (nkeywords) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1889 ast_error(CHILD(ch, 0), |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1890 "non-keyword arg after keyword arg"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1891 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1892 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1893 e = ast_for_expr(c, CHILD(ch, 0)); |
| 35742 | 1894 if (!e) |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1895 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1896 asdl_seq_SET(args, nargs++, e); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1897 } |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1898 else if (TYPE(CHILD(ch, 1)) == comp_for) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1899 e = ast_for_genexp(c, ch); |
| 35742 | 1900 if (!e) |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1901 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1902 asdl_seq_SET(args, nargs++, e); |
| 35742 | 1903 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1904 else { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1905 keyword_ty kw; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1906 identifier key; |
| 35742 | 1907 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1908 /* CHILD(ch, 0) is test, but must be an identifier? */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1909 e = ast_for_expr(c, CHILD(ch, 0)); |
| 35742 | 1910 if (!e) |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1911 return NULL; |
| 35742 | 1912 /* f(lambda x: x[0] = 3) ends up getting parsed with |
| 1913 * LHS test = lambda x: x[0], and RHS test = 3. | |
| 1914 * SF bug 132313 points out that complaining about a keyword | |
| 1915 * then is very confusing. | |
| 1916 */ | |
| 1917 if (e->kind == Lambda_kind) { | |
| 1918 ast_error(CHILD(ch, 0), "lambda cannot contain assignment"); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1919 return NULL; |
| 35742 | 1920 } else if (e->kind != Name_kind) { |
| 1921 ast_error(CHILD(ch, 0), "keyword can't be an expression"); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1922 return NULL; |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
1923 } else if (forbidden_name(e, ch)) { |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
1924 return NULL; |
|
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
1925 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1926 key = e->v.Name.id; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1927 e = ast_for_expr(c, CHILD(ch, 2)); |
| 35742 | 1928 if (!e) |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1929 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1930 kw = keyword(key, e, c->c_arena); |
| 35742 | 1931 if (!kw) |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
1932 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1933 asdl_seq_SET(keywords, nkeywords++, kw); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1934 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1935 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1936 else if (TYPE(ch) == STAR) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1937 vararg = ast_for_expr(c, CHILD(n, i+1)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1938 i++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1939 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1940 else if (TYPE(ch) == DOUBLESTAR) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1941 kwarg = ast_for_expr(c, CHILD(n, i+1)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1942 i++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1943 } |
| 35742 | 1944 } |
| 1945 | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
1946 return Call(func, args, keywords, vararg, kwarg, func->lineno, func->col_offset, c->c_arena); |
| 35742 | 1947 } |
| 1948 | |
| 1949 static expr_ty | |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1950 ast_for_testlist(struct compiling *c, const node* n) |
| 35742 | 1951 { |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1952 /* testlist_comp: test (comp_for | (',' test)* [',']) */ |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1953 /* testlist: test (',' test)* [','] */ |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1954 /* testlist1: test (',' test)* */ |
| 35742 | 1955 assert(NCH(n) > 0); |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1956 if (TYPE(n) == testlist_comp) { |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1957 if (NCH(n) > 1) |
|
41464
060c79f6d919
Hide list comp variables and support set comprehensions
Nick Coghlan <ncoghlan@gmail.com>
parents:
41235
diff
changeset
|
1958 assert(TYPE(CHILD(n, 1)) != comp_for); |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1959 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1960 else { |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1961 assert(TYPE(n) == testlist || |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1962 TYPE(n) == testlist1); |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1963 } |
| 35742 | 1964 if (NCH(n) == 1) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1965 return ast_for_expr(c, CHILD(n, 0)); |
| 35742 | 1966 else { |
| 1967 asdl_seq *tmp = seq_for_testlist(c, n); | |
| 1968 if (!tmp) | |
| 1969 return NULL; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1970 return Tuple(tmp, Load, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1971 } |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1972 } |
|
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
1973 |
| 35742 | 1974 static stmt_ty |
| 1975 ast_for_expr_stmt(struct compiling *c, const node *n) | |
| 1976 { | |
| 1977 REQ(n, expr_stmt); | |
| 1978 /* expr_stmt: testlist (augassign (yield_expr|testlist) | |
| 1979 | ('=' (yield_expr|testlist))*) | |
| 1980 testlist: test (',' test)* [','] | |
| 1981 augassign: '+=' | '-=' | '*=' | '/=' | '%=' | '&=' | '|=' | '^=' | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1982 | '<<=' | '>>=' | '**=' | '//=' |
| 35742 | 1983 test: ... here starts the operator precendence dance |
| 1984 */ | |
| 1985 | |
| 1986 if (NCH(n) == 1) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1987 expr_ty e = ast_for_testlist(c, CHILD(n, 0)); |
| 35742 | 1988 if (!e) |
| 1989 return NULL; | |
| 1990 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1991 return Expr(e, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 1992 } |
| 1993 else if (TYPE(CHILD(n, 1)) == augassign) { | |
| 1994 expr_ty expr1, expr2; | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
1995 operator_ty newoperator; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1996 node *ch = CHILD(n, 0); |
| 35742 | 1997 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
1998 expr1 = ast_for_testlist(c, ch); |
| 35742 | 1999 if (!expr1) |
| 2000 return NULL; | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2001 /* TODO(nas): Remove duplicated error checks (set_context does it) */ |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2002 switch (expr1->kind) { |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2003 case GeneratorExp_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2004 ast_error(ch, "augmented assignment to generator " |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2005 "expression not possible"); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2006 return NULL; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2007 case Yield_kind: |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2008 ast_error(ch, "augmented assignment to yield " |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2009 "expression not possible"); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2010 return NULL; |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2011 case Name_kind: { |
|
41969
9d02a4adf574
Merged revisions 55631-55794 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41824
diff
changeset
|
2012 if (forbidden_name(expr1, ch)) |
|
36418
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2013 return NULL; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2014 break; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2015 } |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2016 case Attribute_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2017 case Subscript_kind: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2018 break; |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2019 default: |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2020 ast_error(ch, "illegal expression for augmented " |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2021 "assignment"); |
|
a625e8876407
Improved handling of syntax errors.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36242
diff
changeset
|
2022 return NULL; |
| 35742 | 2023 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2024 set_context(expr1, Store, ch); |
| 35742 | 2025 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2026 ch = CHILD(n, 2); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2027 if (TYPE(ch) == testlist) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2028 expr2 = ast_for_testlist(c, ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2029 else |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2030 expr2 = ast_for_expr(c, ch); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2031 if (!expr2) |
| 35742 | 2032 return NULL; |
| 2033 | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2034 newoperator = ast_for_augassign(CHILD(n, 1)); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2035 if (!newoperator) |
| 35742 | 2036 return NULL; |
| 2037 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2038 return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2039 } |
| 2040 else { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2041 int i; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2042 asdl_seq *targets; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2043 node *value; |
| 35742 | 2044 expr_ty expression; |
| 2045 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2046 /* a normal assignment */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2047 REQ(CHILD(n, 1), EQUAL); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2048 targets = asdl_seq_new(NCH(n) / 2, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2049 if (!targets) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2050 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2051 for (i = 0; i < NCH(n) - 2; i += 2) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2052 expr_ty e; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2053 node *ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2054 if (TYPE(ch) == yield_expr) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2055 ast_error(ch, "assignment to yield expression not possible"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2056 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2057 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2058 e = ast_for_testlist(c, ch); |
| 35742 | 2059 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2060 /* set context to assign */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2061 if (!e) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2062 return NULL; |
| 35742 | 2063 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2064 if (!set_context(e, Store, CHILD(n, i))) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2065 return NULL; |
| 35742 | 2066 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2067 asdl_seq_SET(targets, i / 2, e); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2068 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2069 value = CHILD(n, NCH(n) - 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2070 if (TYPE(value) == testlist) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2071 expression = ast_for_testlist(c, value); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2072 else |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2073 expression = ast_for_expr(c, value); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2074 if (!expression) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2075 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2076 return Assign(targets, expression, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2077 } |
| 2078 } | |
| 2079 | |
| 2080 static asdl_seq * | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2081 ast_for_exprlist(struct compiling *c, const node *n, expr_context_ty context) |
| 35742 | 2082 { |
| 2083 asdl_seq *seq; | |
| 2084 int i; | |
| 2085 expr_ty e; | |
| 2086 | |
| 2087 REQ(n, exprlist); | |
| 2088 | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2089 seq = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
| 35742 | 2090 if (!seq) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2091 return NULL; |
| 35742 | 2092 for (i = 0; i < NCH(n); i += 2) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2093 e = ast_for_expr(c, CHILD(n, i)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2094 if (!e) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2095 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2096 asdl_seq_SET(seq, i / 2, e); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2097 if (context && !set_context(e, context, CHILD(n, i))) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2098 return NULL; |
| 35742 | 2099 } |
| 2100 return seq; | |
| 2101 } | |
| 2102 | |
| 2103 static stmt_ty | |
| 2104 ast_for_del_stmt(struct compiling *c, const node *n) | |
| 2105 { | |
| 2106 asdl_seq *expr_list; | |
| 2107 | |
| 2108 /* del_stmt: 'del' exprlist */ | |
| 2109 REQ(n, del_stmt); | |
| 2110 | |
| 2111 expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); | |
| 2112 if (!expr_list) | |
| 2113 return NULL; | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2114 return Delete(expr_list, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2115 } |
| 2116 | |
| 2117 static stmt_ty | |
| 2118 ast_for_flow_stmt(struct compiling *c, const node *n) | |
| 2119 { | |
| 2120 /* | |
| 2121 flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt | |
| 2122 | yield_stmt | |
| 2123 break_stmt: 'break' | |
| 2124 continue_stmt: 'continue' | |
| 2125 return_stmt: 'return' [testlist] | |
| 2126 yield_stmt: yield_expr | |
| 2127 yield_expr: 'yield' testlist | |
| 2128 raise_stmt: 'raise' [test [',' test [',' test]]] | |
| 2129 */ | |
| 2130 node *ch; | |
| 2131 | |
| 2132 REQ(n, flow_stmt); | |
| 2133 ch = CHILD(n, 0); | |
| 2134 switch (TYPE(ch)) { | |
| 2135 case break_stmt: | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2136 return Break(LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2137 case continue_stmt: |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2138 return Continue(LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2139 case yield_stmt: { /* will reduce to yield_expr */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2140 expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2141 if (!exp) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2142 return NULL; |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2143 return Expr(exp, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2144 } |
| 2145 case return_stmt: | |
| 2146 if (NCH(ch) == 1) | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2147 return Return(NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2148 else { |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2149 expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); |
| 35742 | 2150 if (!expression) |
| 2151 return NULL; | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2152 return Return(expression, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2153 } |
| 2154 case raise_stmt: | |
| 2155 if (NCH(ch) == 1) | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2156 return Raise(NULL, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2157 else if (NCH(ch) == 2) { |
| 2158 expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); | |
| 2159 if (!expression) | |
| 2160 return NULL; | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2161 return Raise(expression, NULL, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2162 } |
| 2163 else if (NCH(ch) == 4) { | |
| 2164 expr_ty expr1, expr2; | |
| 2165 | |
| 2166 expr1 = ast_for_expr(c, CHILD(ch, 1)); | |
| 2167 if (!expr1) | |
| 2168 return NULL; | |
| 2169 expr2 = ast_for_expr(c, CHILD(ch, 3)); | |
| 2170 if (!expr2) | |
| 2171 return NULL; | |
| 2172 | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2173 return Raise(expr1, expr2, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2174 } |
| 2175 else if (NCH(ch) == 6) { | |
| 2176 expr_ty expr1, expr2, expr3; | |
| 2177 | |
| 2178 expr1 = ast_for_expr(c, CHILD(ch, 1)); | |
| 2179 if (!expr1) | |
| 2180 return NULL; | |
| 2181 expr2 = ast_for_expr(c, CHILD(ch, 3)); | |
| 2182 if (!expr2) | |
| 2183 return NULL; | |
| 2184 expr3 = ast_for_expr(c, CHILD(ch, 5)); | |
| 2185 if (!expr3) | |
| 2186 return NULL; | |
| 2187 | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2188 return Raise(expr1, expr2, expr3, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2189 } |
| 2190 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2191 PyErr_Format(PyExc_SystemError, |
| 35742 | 2192 "unexpected flow_stmt: %d", TYPE(ch)); |
| 2193 return NULL; | |
| 2194 } | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2195 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2196 PyErr_SetString(PyExc_SystemError, "unhandled flow statement"); |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2197 return NULL; |
| 35742 | 2198 } |
| 2199 | |
| 2200 static alias_ty | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2201 alias_for_import_name(struct compiling *c, const node *n) |
| 35742 | 2202 { |
| 2203 /* | |
|
36742
658f8ff2a442
Make 'as' an actual keyword when with's future statement is used. Not
Thomas Wouters <thomas@python.org>
parents:
36718
diff
changeset
|
2204 import_as_name: NAME ['as' NAME] |
|
658f8ff2a442
Make 'as' an actual keyword when with's future statement is used. Not
Thomas Wouters <thomas@python.org>
parents:
36718
diff
changeset
|
2205 dotted_as_name: dotted_name ['as' NAME] |
| 35742 | 2206 dotted_name: NAME ('.' NAME)* |
| 2207 */ | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2208 PyObject *str; |
|
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2209 |
| 35742 | 2210 loop: |
| 2211 switch (TYPE(n)) { | |
| 2212 case import_as_name: | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2213 str = NULL; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2214 if (NCH(n) == 3) { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2215 str = NEW_IDENTIFIER(CHILD(n, 2)); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2216 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2217 return alias(NEW_IDENTIFIER(CHILD(n, 0)), str, c->c_arena); |
| 35742 | 2218 case dotted_as_name: |
| 2219 if (NCH(n) == 1) { | |
| 2220 n = CHILD(n, 0); | |
| 2221 goto loop; | |
| 2222 } | |
| 2223 else { | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2224 alias_ty a = alias_for_import_name(c, CHILD(n, 0)); |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
2225 if (!a) |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39442
diff
changeset
|
2226 return NULL; |
| 35742 | 2227 assert(!a->asname); |
| 2228 a->asname = NEW_IDENTIFIER(CHILD(n, 2)); | |
| 2229 return a; | |
| 2230 } | |
| 2231 break; | |
| 2232 case dotted_name: | |
| 2233 if (NCH(n) == 1) | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2234 return alias(NEW_IDENTIFIER(CHILD(n, 0)), NULL, c->c_arena); |
| 35742 | 2235 else { |
| 2236 /* Create a string of the form "a.b.c" */ | |
|
36242
0f5189607264
Repair bizarre indentation created by VC 7.1.
Tim Peters <tim.peters@gmail.com>
parents:
36241
diff
changeset
|
2237 int i; |
|
36241
557e9b6ee7e1
alias_for_import_name(): Dueling compiler warnings ;-)
Tim Peters <tim.peters@gmail.com>
parents:
36231
diff
changeset
|
2238 size_t len; |
| 35742 | 2239 char *s; |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2240 PyObject *uni; |
| 35742 | 2241 |
| 2242 len = 0; | |
| 2243 for (i = 0; i < NCH(n); i += 2) | |
| 2244 /* length of string plus one for the dot */ | |
| 2245 len += strlen(STR(CHILD(n, i))) + 1; | |
| 2246 len--; /* the last name doesn't have a dot */ | |
| 2247 str = PyString_FromStringAndSize(NULL, len); | |
| 2248 if (!str) | |
| 2249 return NULL; | |
| 2250 s = PyString_AS_STRING(str); | |
| 2251 if (!s) | |
| 2252 return NULL; | |
| 2253 for (i = 0; i < NCH(n); i += 2) { | |
| 2254 char *sch = STR(CHILD(n, i)); | |
| 2255 strcpy(s, STR(CHILD(n, i))); | |
| 2256 s += strlen(sch); | |
| 2257 *s++ = '.'; | |
| 2258 } | |
| 2259 --s; | |
| 2260 *s = '\0'; | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2261 uni = PyUnicode_DecodeUTF8(PyString_AS_STRING(str), |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2262 PyString_GET_SIZE(str), |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2263 NULL); |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2264 Py_DECREF(str); |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2265 if (!uni) |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2266 return NULL; |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2267 str = uni; |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2268 PyUnicode_InternInPlace(&str); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2269 PyArena_AddPyObject(c->c_arena, str); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2270 return alias(str, NULL, c->c_arena); |
| 35742 | 2271 } |
| 2272 break; | |
| 2273 case STAR: | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41969
diff
changeset
|
2274 str = PyUnicode_InternFromString("*"); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2275 PyArena_AddPyObject(c->c_arena, str); |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2276 return alias(str, NULL, c->c_arena); |
| 35742 | 2277 default: |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2278 PyErr_Format(PyExc_SystemError, |
| 35742 | 2279 "unexpected import name: %d", TYPE(n)); |
| 2280 return NULL; | |
| 2281 } | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2282 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2283 PyErr_SetString(PyExc_SystemError, "unhandled import name condition"); |
| 35742 | 2284 return NULL; |
| 2285 } | |
| 2286 | |
| 2287 static stmt_ty | |
| 2288 ast_for_import_stmt(struct compiling *c, const node *n) | |
| 2289 { | |
| 2290 /* | |
| 2291 import_stmt: import_name | import_from | |
| 2292 import_name: 'import' dotted_as_names | |
|
41235
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2293 import_from: 'from' (('.' | '...')* dotted_name | ('.' | '...')+) |
|
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2294 'import' ('*' | '(' import_as_names ')' | import_as_names) |
| 35742 | 2295 */ |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2296 int lineno; |
|
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2297 int col_offset; |
| 35742 | 2298 int i; |
| 2299 asdl_seq *aliases; | |
| 2300 | |
| 2301 REQ(n, import_stmt); | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2302 lineno = LINENO(n); |
|
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2303 col_offset = n->n_col_offset; |
| 35742 | 2304 n = CHILD(n, 0); |
|
36682
52cbc447e1c9
And some more cleanup.
Thomas Wouters <thomas@python.org>
parents:
36681
diff
changeset
|
2305 if (TYPE(n) == import_name) { |
| 35742 | 2306 n = CHILD(n, 1); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2307 REQ(n, dotted_as_names); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2308 aliases = asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2309 if (!aliases) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2310 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2311 for (i = 0; i < NCH(n); i += 2) { |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2312 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i)); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2313 if (!import_alias) |
| 35742 | 2314 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2315 asdl_seq_SET(aliases, i / 2, import_alias); |
| 35742 | 2316 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2317 return Import(aliases, lineno, col_offset, c->c_arena); |
| 35742 | 2318 } |
|
36682
52cbc447e1c9
And some more cleanup.
Thomas Wouters <thomas@python.org>
parents:
36681
diff
changeset
|
2319 else if (TYPE(n) == import_from) { |
| 35742 | 2320 int n_children; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2321 int idx, ndots = 0; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2322 alias_ty mod = NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2323 identifier modname; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2324 |
|
36712
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2325 /* Count the number of dots (for relative imports) and check for the |
|
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2326 optional module name */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2327 for (idx = 1; idx < NCH(n); idx++) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2328 if (TYPE(CHILD(n, idx)) == dotted_name) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2329 mod = alias_for_import_name(c, CHILD(n, idx)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2330 idx++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2331 break; |
|
41235
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2332 } else if (TYPE(CHILD(n, idx)) == ELLIPSIS) { |
|
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2333 /* three consecutive dots are tokenized as one ELLIPSIS */ |
|
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2334 ndots += 3; |
|
6c8c33a43a15
"from ... import x" should not be a syntax error... make
Georg Brandl <georg@python.org>
parents:
41230
diff
changeset
|
2335 continue; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2336 } else if (TYPE(CHILD(n, idx)) != DOT) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2337 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2338 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2339 ndots++; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2340 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2341 idx++; /* skip over the 'import' keyword */ |
|
36712
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2342 switch (TYPE(CHILD(n, idx))) { |
|
36681
e9f0db429f2b
Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents:
36679
diff
changeset
|
2343 case STAR: |
|
e9f0db429f2b
Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents:
36679
diff
changeset
|
2344 /* from ... import * */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2345 n = CHILD(n, idx); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2346 n_children = 1; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2347 if (ndots) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2348 ast_error(n, "'import *' not allowed with 'from .'"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2349 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2350 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2351 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2352 case LPAR: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2353 /* from ... import (x, y, z) */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2354 n = CHILD(n, idx + 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2355 n_children = NCH(n); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2356 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2357 case import_as_names: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2358 /* from ... import x, y, z */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2359 n = CHILD(n, idx); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2360 n_children = NCH(n); |
|
36681
e9f0db429f2b
Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents:
36679
diff
changeset
|
2361 if (n_children % 2 == 0) { |
| 35742 | 2362 ast_error(n, "trailing comma not allowed without" |
| 2363 " surrounding parentheses"); | |
| 2364 return NULL; | |
| 2365 } | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2366 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2367 default: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2368 ast_error(n, "Unexpected node-type in from-import"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2369 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2370 } |
| 35742 | 2371 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2372 aliases = asdl_seq_new((n_children + 1) / 2, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2373 if (!aliases) |
| 35742 | 2374 return NULL; |
| 2375 | |
| 2376 /* handle "from ... import *" special b/c there's no children */ | |
|
36681
e9f0db429f2b
Clean up from-import handling.
Thomas Wouters <thomas@python.org>
parents:
36679
diff
changeset
|
2377 if (TYPE(n) == STAR) { |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2378 alias_ty import_alias = alias_for_import_name(c, n); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2379 if (!import_alias) |
| 35742 | 2380 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2381 asdl_seq_SET(aliases, 0, import_alias); |
| 35742 | 2382 } |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
2383 else { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2384 for (i = 0; i < NCH(n); i += 2) { |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
2385 alias_ty import_alias = alias_for_import_name(c, CHILD(n, i)); |
|
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
2386 if (!import_alias) |
|
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
2387 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2388 asdl_seq_SET(aliases, i / 2, import_alias); |
|
36718
45e6f901902c
Remove asdl_seq_APPEND() and simplify asdl seq implementation.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
36712
diff
changeset
|
2389 } |
| 35742 | 2390 } |
|
36712
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2391 if (mod != NULL) |
|
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2392 modname = mod->name; |
|
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2393 else |
|
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2394 modname = new_identifier("", c->c_arena); |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2395 return ImportFrom(modname, aliases, ndots, lineno, col_offset, |
|
36712
5c36c222e7ca
SF patch #1438387, PEP 328: relative and absolute imports.
Thomas Wouters <thomas@python.org>
parents:
36695
diff
changeset
|
2396 c->c_arena); |
| 35742 | 2397 } |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2398 PyErr_Format(PyExc_SystemError, |
| 35742 | 2399 "unknown import statement: starts with command '%s'", |
| 2400 STR(CHILD(n, 0))); | |
| 2401 return NULL; | |
| 2402 } | |
| 2403 | |
| 2404 static stmt_ty | |
| 2405 ast_for_global_stmt(struct compiling *c, const node *n) | |
| 2406 { | |
| 2407 /* global_stmt: 'global' NAME (',' NAME)* */ | |
| 2408 identifier name; | |
| 2409 asdl_seq *s; | |
| 2410 int i; | |
| 2411 | |
| 2412 REQ(n, global_stmt); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2413 s = asdl_seq_new(NCH(n) / 2, c->c_arena); |
| 35742 | 2414 if (!s) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2415 return NULL; |
| 35742 | 2416 for (i = 1; i < NCH(n); i += 2) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2417 name = NEW_IDENTIFIER(CHILD(n, i)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2418 if (!name) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2419 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2420 asdl_seq_SET(s, i / 2, name); |
| 35742 | 2421 } |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2422 return Global(s, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2423 } |
| 2424 | |
| 2425 static stmt_ty | |
|
40965
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2426 ast_for_nonlocal_stmt(struct compiling *c, const node *n) |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2427 { |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2428 /* nonlocal_stmt: 'nonlocal' NAME (',' NAME)* */ |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2429 identifier name; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2430 asdl_seq *s; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2431 int i; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2432 |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2433 REQ(n, nonlocal_stmt); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2434 s = asdl_seq_new(NCH(n) / 2, c->c_arena); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2435 if (!s) |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2436 return NULL; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2437 for (i = 1; i < NCH(n); i += 2) { |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2438 name = NEW_IDENTIFIER(CHILD(n, i)); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2439 if (!name) |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2440 return NULL; |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2441 asdl_seq_SET(s, i / 2, name); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2442 } |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2443 return Nonlocal(s, LINENO(n), n->n_col_offset, c->c_arena); |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2444 } |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2445 |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2446 static stmt_ty |
| 35742 | 2447 ast_for_assert_stmt(struct compiling *c, const node *n) |
| 2448 { | |
| 2449 /* assert_stmt: 'assert' test [',' test] */ | |
| 2450 REQ(n, assert_stmt); | |
| 2451 if (NCH(n) == 2) { | |
| 2452 expr_ty expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2453 if (!expression) | |
| 2454 return NULL; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2455 return Assert(expression, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2456 } |
| 2457 else if (NCH(n) == 4) { | |
| 2458 expr_ty expr1, expr2; | |
| 2459 | |
| 2460 expr1 = ast_for_expr(c, CHILD(n, 1)); | |
| 2461 if (!expr1) | |
| 2462 return NULL; | |
| 2463 expr2 = ast_for_expr(c, CHILD(n, 3)); | |
| 2464 if (!expr2) | |
| 2465 return NULL; | |
| 2466 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2467 return Assert(expr1, expr2, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2468 } |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2469 PyErr_Format(PyExc_SystemError, |
| 35742 | 2470 "improper number of parts to 'assert' statement: %d", |
| 2471 NCH(n)); | |
| 2472 return NULL; | |
| 2473 } | |
| 2474 | |
| 2475 static asdl_seq * | |
| 2476 ast_for_suite(struct compiling *c, const node *n) | |
| 2477 { | |
| 2478 /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2479 asdl_seq *seq; |
| 35742 | 2480 stmt_ty s; |
| 2481 int i, total, num, end, pos = 0; | |
| 2482 node *ch; | |
| 2483 | |
| 2484 REQ(n, suite); | |
| 2485 | |
| 2486 total = num_stmts(n); | |
|
36065
ea083efdc96c
Merge from ast-arena. This reduces the code in Python/ast.c by ~300 lines,
Neal Norwitz <nnorwitz@gmail.com>
parents:
36001
diff
changeset
|
2487 seq = asdl_seq_new(total, c->c_arena); |
| 35742 | 2488 if (!seq) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2489 return NULL; |
| 35742 | 2490 if (TYPE(CHILD(n, 0)) == simple_stmt) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2491 n = CHILD(n, 0); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2492 /* simple_stmt always ends with a NEWLINE, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2493 and may have a trailing SEMI |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2494 */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2495 end = NCH(n) - 1; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2496 if (TYPE(CHILD(n, end - 1)) == SEMI) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2497 end--; |
| 35742 | 2498 /* loop by 2 to skip semi-colons */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2499 for (i = 0; i < end; i += 2) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2500 ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2501 s = ast_for_stmt(c, ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2502 if (!s) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2503 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2504 asdl_seq_SET(seq, pos++, s); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2505 } |
| 35742 | 2506 } |
| 2507 else { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2508 for (i = 2; i < (NCH(n) - 1); i++) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2509 ch = CHILD(n, i); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2510 REQ(ch, stmt); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2511 num = num_stmts(ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2512 if (num == 1) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2513 /* small_stmt or compound_stmt with only one child */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2514 s = ast_for_stmt(c, ch); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2515 if (!s) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2516 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2517 asdl_seq_SET(seq, pos++, s); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2518 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2519 else { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2520 int j; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2521 ch = CHILD(ch, 0); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2522 REQ(ch, simple_stmt); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2523 for (j = 0; j < NCH(ch); j += 2) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2524 /* statement terminates with a semi-colon ';' */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2525 if (NCH(CHILD(ch, j)) == 0) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2526 assert((j + 1) == NCH(ch)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2527 break; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2528 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2529 s = ast_for_stmt(c, CHILD(ch, j)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2530 if (!s) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2531 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2532 asdl_seq_SET(seq, pos++, s); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2533 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2534 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2535 } |
| 35742 | 2536 } |
| 2537 assert(pos == seq->size); | |
| 2538 return seq; | |
| 2539 } | |
| 2540 | |
| 2541 static stmt_ty | |
| 2542 ast_for_if_stmt(struct compiling *c, const node *n) | |
| 2543 { | |
| 2544 /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)* | |
| 2545 ['else' ':' suite] | |
| 2546 */ | |
| 2547 char *s; | |
| 2548 | |
| 2549 REQ(n, if_stmt); | |
| 2550 | |
| 2551 if (NCH(n) == 4) { | |
| 2552 expr_ty expression; | |
| 2553 asdl_seq *suite_seq; | |
| 2554 | |
| 2555 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2556 if (!expression) | |
| 2557 return NULL; | |
| 2558 suite_seq = ast_for_suite(c, CHILD(n, 3)); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2559 if (!suite_seq) |
| 35742 | 2560 return NULL; |
| 2561 | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2562 return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2563 c->c_arena); |
| 35742 | 2564 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2565 |
| 35742 | 2566 s = STR(CHILD(n, 4)); |
| 2567 /* s[2], the third character in the string, will be | |
| 2568 's' for el_s_e, or | |
| 2569 'i' for el_i_f | |
| 2570 */ | |
| 2571 if (s[2] == 's') { | |
| 2572 expr_ty expression; | |
| 2573 asdl_seq *seq1, *seq2; | |
| 2574 | |
| 2575 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2576 if (!expression) | |
| 2577 return NULL; | |
| 2578 seq1 = ast_for_suite(c, CHILD(n, 3)); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2579 if (!seq1) |
| 35742 | 2580 return NULL; |
| 2581 seq2 = ast_for_suite(c, CHILD(n, 6)); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2582 if (!seq2) |
| 35742 | 2583 return NULL; |
| 2584 | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2585 return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2586 c->c_arena); |
| 35742 | 2587 } |
| 2588 else if (s[2] == 'i') { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2589 int i, n_elif, has_else = 0; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2590 expr_ty expression; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2591 asdl_seq *suite_seq; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2592 asdl_seq *orelse = NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2593 n_elif = NCH(n) - 4; |
| 35742 | 2594 /* must reference the child n_elif+1 since 'else' token is third, |
| 2595 not fourth, child from the end. */ | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2596 if (TYPE(CHILD(n, (n_elif + 1))) == NAME |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2597 && STR(CHILD(n, (n_elif + 1)))[2] == 's') { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2598 has_else = 1; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2599 n_elif -= 3; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2600 } |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2601 n_elif /= 4; |
| 35742 | 2602 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2603 if (has_else) { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2604 asdl_seq *suite_seq2; |
| 35742 | 2605 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2606 orelse = asdl_seq_new(1, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2607 if (!orelse) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2608 return NULL; |
| 35742 | 2609 expression = ast_for_expr(c, CHILD(n, NCH(n) - 6)); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2610 if (!expression) |
| 35742 | 2611 return NULL; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2612 suite_seq = ast_for_suite(c, CHILD(n, NCH(n) - 4)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2613 if (!suite_seq) |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2614 return NULL; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2615 suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2616 if (!suite_seq2) |
| 35742 | 2617 return NULL; |
| 2618 | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2619 asdl_seq_SET(orelse, 0, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2620 If(expression, suite_seq, suite_seq2, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2621 LINENO(CHILD(n, NCH(n) - 6)), |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2622 CHILD(n, NCH(n) - 6)->n_col_offset, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2623 c->c_arena)); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2624 /* the just-created orelse handled the last elif */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2625 n_elif--; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2626 } |
| 35742 | 2627 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2628 for (i = 0; i < n_elif; i++) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2629 int off = 5 + (n_elif - i - 1) * 4; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2630 asdl_seq *newobj = asdl_seq_new(1, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2631 if (!newobj) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2632 return NULL; |
| 35742 | 2633 expression = ast_for_expr(c, CHILD(n, off)); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2634 if (!expression) |
| 35742 | 2635 return NULL; |
| 2636 suite_seq = ast_for_suite(c, CHILD(n, off + 2)); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2637 if (!suite_seq) |
| 35742 | 2638 return NULL; |
| 2639 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2640 asdl_seq_SET(newobj, 0, |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2641 If(expression, suite_seq, orelse, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2642 LINENO(CHILD(n, off)), |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2643 CHILD(n, off)->n_col_offset, c->c_arena)); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2644 orelse = newobj; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2645 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2646 expression = ast_for_expr(c, CHILD(n, 1)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2647 if (!expression) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2648 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2649 suite_seq = ast_for_suite(c, CHILD(n, 3)); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2650 if (!suite_seq) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2651 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2652 return If(expression, suite_seq, orelse, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
2653 LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2654 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2655 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2656 PyErr_Format(PyExc_SystemError, |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2657 "unexpected token in 'if' statement: %s", s); |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2658 return NULL; |
| 35742 | 2659 } |
| 2660 | |
| 2661 static stmt_ty | |
| 2662 ast_for_while_stmt(struct compiling *c, const node *n) | |
| 2663 { | |
| 2664 /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ | |
| 2665 REQ(n, while_stmt); | |
| 2666 | |
| 2667 if (NCH(n) == 4) { | |
| 2668 expr_ty expression; | |
| 2669 asdl_seq *suite_seq; | |
| 2670 | |
| 2671 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2672 if (!expression) | |
| 2673 return NULL; | |
| 2674 suite_seq = ast_for_suite(c, CHILD(n, 3)); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2675 if (!suite_seq) |
| 35742 | 2676 return NULL; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2677 return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2678 } |
| 2679 else if (NCH(n) == 7) { | |
| 2680 expr_ty expression; | |
| 2681 asdl_seq *seq1, *seq2; | |
| 2682 | |
| 2683 expression = ast_for_expr(c, CHILD(n, 1)); | |
| 2684 if (!expression) | |
| 2685 return NULL; | |
| 2686 seq1 = ast_for_suite(c, CHILD(n, 3)); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2687 if (!seq1) |
| 35742 | 2688 return NULL; |
| 2689 seq2 = ast_for_suite(c, CHILD(n, 6)); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2690 if (!seq2) |
| 35742 | 2691 return NULL; |
| 2692 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2693 return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2694 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2695 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2696 PyErr_Format(PyExc_SystemError, |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2697 "wrong number of tokens for 'while' statement: %d", |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2698 NCH(n)); |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2699 return NULL; |
| 35742 | 2700 } |
| 2701 | |
| 2702 static stmt_ty | |
| 2703 ast_for_for_stmt(struct compiling *c, const node *n) | |
| 2704 { | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2705 asdl_seq *_target, *seq = NULL, *suite_seq; |
| 35742 | 2706 expr_ty expression; |
| 2707 expr_ty target; | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2708 const node *node_target; |
| 35742 | 2709 /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */ |
| 2710 REQ(n, for_stmt); | |
| 2711 | |
| 2712 if (NCH(n) == 9) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2713 seq = ast_for_suite(c, CHILD(n, 8)); |
| 35742 | 2714 if (!seq) |
| 2715 return NULL; | |
| 2716 } | |
| 2717 | |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2718 node_target = CHILD(n, 1); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2719 _target = ast_for_exprlist(c, node_target, Store); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2720 if (!_target) |
| 35742 | 2721 return NULL; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2722 /* Check the # of children rather than the length of _target, since |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2723 for x, in ... has 1 element in _target, but still requires a Tuple. */ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2724 if (NCH(node_target) == 1) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2725 target = (expr_ty)asdl_seq_GET(_target, 0); |
| 35742 | 2726 else |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2727 target = Tuple(_target, Store, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2728 |
|
35798
857f68fba275
Write a separate ast_for_testlist_gexp() function instead of overloading
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35771
diff
changeset
|
2729 expression = ast_for_testlist(c, CHILD(n, 3)); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2730 if (!expression) |
| 35742 | 2731 return NULL; |
| 2732 suite_seq = ast_for_suite(c, CHILD(n, 5)); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2733 if (!suite_seq) |
| 35742 | 2734 return NULL; |
| 2735 | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2736 return For(target, expression, suite_seq, seq, LINENO(n), n->n_col_offset, |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2737 c->c_arena); |
| 35742 | 2738 } |
| 2739 | |
| 2740 static excepthandler_ty | |
| 2741 ast_for_except_clause(struct compiling *c, const node *exc, node *body) | |
| 2742 { | |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
2743 /* except_clause: 'except' [test ['as' test]] */ |
| 35742 | 2744 REQ(exc, except_clause); |
| 2745 REQ(body, suite); | |
| 2746 | |
| 2747 if (NCH(exc) == 1) { | |
| 2748 asdl_seq *suite_seq = ast_for_suite(c, body); | |
| 2749 if (!suite_seq) | |
| 2750 return NULL; | |
| 2751 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2752 return excepthandler(NULL, NULL, suite_seq, LINENO(exc), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2753 exc->n_col_offset, c->c_arena); |
| 35742 | 2754 } |
| 2755 else if (NCH(exc) == 2) { | |
| 2756 expr_ty expression; | |
| 2757 asdl_seq *suite_seq; | |
| 2758 | |
| 2759 expression = ast_for_expr(c, CHILD(exc, 1)); | |
| 2760 if (!expression) | |
| 2761 return NULL; | |
| 2762 suite_seq = ast_for_suite(c, body); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2763 if (!suite_seq) |
| 35742 | 2764 return NULL; |
| 2765 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2766 return excepthandler(expression, NULL, suite_seq, LINENO(exc), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2767 exc->n_col_offset, c->c_arena); |
| 35742 | 2768 } |
| 2769 else if (NCH(exc) == 4) { | |
| 2770 asdl_seq *suite_seq; | |
| 2771 expr_ty expression; | |
|
40588
4bab65fcca8e
Some more changes related to the new except syntax and semantics,
Guido van Rossum <guido@python.org>
parents:
40529
diff
changeset
|
2772 identifier e = NEW_IDENTIFIER(CHILD(exc, 3)); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2773 if (!e) |
| 35742 | 2774 return NULL; |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2775 expression = ast_for_expr(c, CHILD(exc, 1)); |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2776 if (!expression) |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2777 return NULL; |
| 35742 | 2778 suite_seq = ast_for_suite(c, body); |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2779 if (!suite_seq) |
| 35742 | 2780 return NULL; |
| 2781 | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2782 return excepthandler(expression, e, suite_seq, LINENO(exc), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2783 exc->n_col_offset, c->c_arena); |
| 35742 | 2784 } |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2785 |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2786 PyErr_Format(PyExc_SystemError, |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2787 "wrong number of children for 'except' clause: %d", |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2788 NCH(exc)); |
|
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2789 return NULL; |
| 35742 | 2790 } |
| 2791 | |
| 2792 static stmt_ty | |
| 2793 ast_for_try_stmt(struct compiling *c, const node *n) | |
| 2794 { | |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2795 const int nch = NCH(n); |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2796 int n_except = (nch - 3)/3; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2797 asdl_seq *body, *orelse = NULL, *finally = NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2798 |
| 35742 | 2799 REQ(n, try_stmt); |
| 2800 | |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2801 body = ast_for_suite(c, CHILD(n, 2)); |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2802 if (body == NULL) |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2803 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2804 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2805 if (TYPE(CHILD(n, nch - 3)) == NAME) { |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2806 if (strcmp(STR(CHILD(n, nch - 3)), "finally") == 0) { |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2807 if (nch >= 9 && TYPE(CHILD(n, nch - 6)) == NAME) { |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2808 /* we can assume it's an "else", |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2809 because nch >= 9 for try-else-finally and |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2810 it would otherwise have a type of except_clause */ |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2811 orelse = ast_for_suite(c, CHILD(n, nch - 4)); |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2812 if (orelse == NULL) |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2813 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2814 n_except--; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2815 } |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2816 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2817 finally = ast_for_suite(c, CHILD(n, nch - 1)); |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2818 if (finally == NULL) |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2819 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2820 n_except--; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2821 } |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2822 else { |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2823 /* we can assume it's an "else", |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2824 otherwise it would have a type of except_clause */ |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2825 orelse = ast_for_suite(c, CHILD(n, nch - 1)); |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2826 if (orelse == NULL) |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2827 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2828 n_except--; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2829 } |
| 35742 | 2830 } |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2831 else if (TYPE(CHILD(n, nch - 3)) != except_clause) { |
|
35867
f4842f42700b
remove useless debug print helper. fix a couple of exceptions
Neal Norwitz <nnorwitz@gmail.com>
parents:
35799
diff
changeset
|
2832 ast_error(n, "malformed 'try' statement"); |
| 35742 | 2833 return NULL; |
| 2834 } | |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2835 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2836 if (n_except > 0) { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2837 int i; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2838 stmt_ty except_st; |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2839 /* process except statements to create a try ... except */ |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2840 asdl_seq *handlers = asdl_seq_new(n_except, c->c_arena); |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2841 if (handlers == NULL) |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2842 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2843 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2844 for (i = 0; i < n_except; i++) { |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2845 excepthandler_ty e = ast_for_except_clause(c, CHILD(n, 3 + i * 3), |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2846 CHILD(n, 5 + i * 3)); |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2847 if (!e) |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2848 return NULL; |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2849 asdl_seq_SET(handlers, i, e); |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2850 } |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2851 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2852 except_st = TryExcept(body, handlers, orelse, LINENO(n), |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36877
diff
changeset
|
2853 n->n_col_offset, c->c_arena); |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2854 if (!finally) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2855 return except_st; |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2856 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2857 /* if a 'finally' is present too, we nest the TryExcept within a |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2858 TryFinally to emulate try ... except ... finally */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2859 body = asdl_seq_new(1, c->c_arena); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2860 if (body == NULL) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2861 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2862 asdl_seq_SET(body, 0, except_st); |
|
36066
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2863 } |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2864 |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2865 /* must be a try ... finally (except clauses are in body, if any exist) */ |
|
719ba02d2e2c
SF patch #1355913, PEP 341 - Unification of try/except and try/finally
Neal Norwitz <nnorwitz@gmail.com>
parents:
36065
diff
changeset
|
2866 assert(finally != NULL); |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2867 return TryFinally(body, finally, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2868 } |
| 2869 | |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2870 static expr_ty |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2871 ast_for_with_var(struct compiling *c, const node *n) |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2872 { |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2873 REQ(n, with_var); |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2874 return ast_for_expr(c, CHILD(n, 1)); |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2875 } |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2876 |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2877 /* with_stmt: 'with' test [ with_var ] ':' suite */ |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2878 static stmt_ty |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2879 ast_for_with_stmt(struct compiling *c, const node *n) |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2880 { |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2881 expr_ty context_expr, optional_vars = NULL; |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2882 int suite_index = 3; /* skip 'with', test, and ':' */ |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2883 asdl_seq *suite_seq; |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2884 |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2885 assert(TYPE(n) == with_stmt); |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2886 context_expr = ast_for_expr(c, CHILD(n, 1)); |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2887 if (TYPE(CHILD(n, 2)) == with_var) { |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2888 optional_vars = ast_for_with_var(c, CHILD(n, 2)); |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2889 |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2890 if (!optional_vars) { |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2891 return NULL; |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2892 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2893 if (!set_context(optional_vars, Store, n)) { |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2894 return NULL; |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2895 } |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2896 suite_index = 4; |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2897 } |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2898 |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2899 suite_seq = ast_for_suite(c, CHILD(n, suite_index)); |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2900 if (!suite_seq) { |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2901 return NULL; |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2902 } |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2903 return With(context_expr, optional_vars, suite_seq, LINENO(n), |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2904 n->n_col_offset, c->c_arena); |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2905 } |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
2906 |
| 35742 | 2907 static stmt_ty |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
2908 ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) |
| 35742 | 2909 { |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2910 /* classdef: 'class' NAME ['(' arglist ')'] ':' suite */ |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2911 asdl_seq *s; |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2912 expr_ty call, dummy; |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2913 |
| 35742 | 2914 REQ(n, classdef); |
| 2915 | |
| 2916 if (!strcmp(STR(CHILD(n, 1)), "None")) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2917 ast_error(n, "assignment to None"); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2918 return NULL; |
| 35742 | 2919 } |
| 2920 | |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2921 if (NCH(n) == 4) { /* class NAME ':' suite */ |
| 35742 | 2922 s = ast_for_suite(c, CHILD(n, 3)); |
| 2923 if (!s) | |
| 2924 return NULL; | |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2925 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, NULL, NULL, NULL, s, |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
2926 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2927 } |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2928 |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2929 if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2930 s = ast_for_suite(c, CHILD(n,5)); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2931 if (!s) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2932 return NULL; |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2933 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), NULL, NULL, NULL, NULL, s, |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
2934 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2935 } |
| 2936 | |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2937 /* class NAME '(' arglist ')' ':' suite */ |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2938 /* build up a fake Call node so we can extract its pieces */ |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2939 dummy = Name(NEW_IDENTIFIER(CHILD(n, 1)), Load, LINENO(n), n->n_col_offset, c->c_arena); |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2940 call = ast_for_call(c, CHILD(n, 3), dummy); |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2941 if (!call) |
| 35742 | 2942 return NULL; |
| 2943 s = ast_for_suite(c, CHILD(n, 6)); | |
|
36072
de16a85c3b4c
Cleanup a bit and make things more consistent.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36066
diff
changeset
|
2944 if (!s) |
| 35742 | 2945 return NULL; |
|
41226
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2946 |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2947 return ClassDef(NEW_IDENTIFIER(CHILD(n, 1)), |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2948 call->v.Call.args, call->v.Call.keywords, |
|
19f7ff443718
Implement PEP 3115 -- new metaclass syntax and semantics.
Guido van Rossum <guido@python.org>
parents:
40965
diff
changeset
|
2949 call->v.Call.starargs, call->v.Call.kwargs, s, |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
2950 decorator_seq, LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2951 } |
| 2952 | |
| 2953 static stmt_ty | |
| 2954 ast_for_stmt(struct compiling *c, const node *n) | |
| 2955 { | |
| 2956 if (TYPE(n) == stmt) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2957 assert(NCH(n) == 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2958 n = CHILD(n, 0); |
| 35742 | 2959 } |
| 2960 if (TYPE(n) == simple_stmt) { | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2961 assert(num_stmts(n) == 1); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2962 n = CHILD(n, 0); |
| 35742 | 2963 } |
| 2964 if (TYPE(n) == small_stmt) { | |
|
40529
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
2965 REQ(n, small_stmt); |
|
54cd47788811
PEP 3107 - Function Annotations thanks to Tony Lownds
Neal Norwitz <nnorwitz@gmail.com>
parents:
40450
diff
changeset
|
2966 n = CHILD(n, 0); |
|
40965
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2967 /* small_stmt: expr_stmt | del_stmt | pass_stmt | flow_stmt |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2968 | import_stmt | global_stmt | nonlocal_stmt | assert_stmt |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2969 */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2970 switch (TYPE(n)) { |
| 35742 | 2971 case expr_stmt: |
| 2972 return ast_for_expr_stmt(c, n); | |
| 2973 case del_stmt: | |
| 2974 return ast_for_del_stmt(c, n); | |
| 2975 case pass_stmt: | |
|
36786
57322cb2ca11
Patch #1440601: Add col_offset attribute to AST nodes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36780
diff
changeset
|
2976 return Pass(LINENO(n), n->n_col_offset, c->c_arena); |
| 35742 | 2977 case flow_stmt: |
| 2978 return ast_for_flow_stmt(c, n); | |
| 2979 case import_stmt: | |
| 2980 return ast_for_import_stmt(c, n); | |
| 2981 case global_stmt: | |
| 2982 return ast_for_global_stmt(c, n); | |
|
40965
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2983 case nonlocal_stmt: |
|
30e5cb3a55ac
Provisional implementation of PEP 3104.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
40941
diff
changeset
|
2984 return ast_for_nonlocal_stmt(c, n); |
| 35742 | 2985 case assert_stmt: |
| 2986 return ast_for_assert_stmt(c, n); | |
| 2987 default: | |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
2988 PyErr_Format(PyExc_SystemError, |
| 35742 | 2989 "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 2990 TYPE(n), NCH(n)); | |
| 2991 return NULL; | |
| 2992 } | |
| 2993 } | |
| 2994 else { | |
| 2995 /* compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
2996 | funcdef | classdef | decorated |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2997 */ |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2998 node *ch = CHILD(n, 0); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
2999 REQ(n, compound_stmt); |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3000 switch (TYPE(ch)) { |
| 35742 | 3001 case if_stmt: |
| 3002 return ast_for_if_stmt(c, ch); | |
| 3003 case while_stmt: | |
| 3004 return ast_for_while_stmt(c, ch); | |
| 3005 case for_stmt: | |
| 3006 return ast_for_for_stmt(c, ch); | |
| 3007 case try_stmt: | |
| 3008 return ast_for_try_stmt(c, ch); | |
|
36695
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
3009 case with_stmt: |
|
b6d2f6fd3e11
PEP 343 -- the with-statement.
Guido van Rossum <guido@python.org>
parents:
36686
diff
changeset
|
3010 return ast_for_with_stmt(c, ch); |
| 35742 | 3011 case funcdef: |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
3012 return ast_for_funcdef(c, ch, NULL); |
| 35742 | 3013 case classdef: |
|
41824
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
3014 return ast_for_classdef(c, ch, NULL); |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
3015 case decorated: |
|
24b60622b944
Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41717
diff
changeset
|
3016 return ast_for_decorated(c, ch); |
| 35742 | 3017 default: |
|
35870
f531c05cd240
Convert all internal errors from Exception to SystemError
Neal Norwitz <nnorwitz@gmail.com>
parents:
35868
diff
changeset
|
3018 PyErr_Format(PyExc_SystemError, |
| 35742 | 3019 "unhandled small_stmt: TYPE=%d NCH=%d\n", |
| 3020 TYPE(n), NCH(n)); | |
| 3021 return NULL; | |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3022 } |
| 35742 | 3023 } |
| 3024 } | |
| 3025 | |
| 3026 static PyObject * | |
| 3027 parsenumber(const char *s) | |
| 3028 { | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3029 const char *end; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3030 long x; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3031 double dx; |
| 35742 | 3032 #ifndef WITHOUT_COMPLEX |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3033 Py_complex c; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3034 int imflag; |
| 35742 | 3035 #endif |
| 3036 | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3037 errno = 0; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3038 end = s + strlen(s) - 1; |
| 35742 | 3039 #ifndef WITHOUT_COMPLEX |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3040 imflag = *end == 'j' || *end == 'J'; |
| 35742 | 3041 #endif |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3042 if (*end == 'l' || *end == 'L') |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3043 return PyLong_FromString((char *)s, (char **)0, 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3044 if (s[0] == '0') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3045 x = (long) PyOS_strtoul((char *)s, (char **)&end, 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3046 if (x < 0 && errno == 0) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3047 return PyLong_FromString((char *)s, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3048 (char **)0, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3049 0); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3050 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3051 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3052 else |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3053 x = PyOS_strtol((char *)s, (char **)&end, 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3054 if (*end == '\0') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3055 if (errno != 0) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3056 return PyLong_FromString((char *)s, (char **)0, 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3057 return PyInt_FromLong(x); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3058 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3059 /* XXX Huge floats may silently fail */ |
| 35742 | 3060 #ifndef WITHOUT_COMPLEX |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3061 if (imflag) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3062 c.real = 0.; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3063 PyFPE_START_PROTECT("atof", return 0) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3064 c.imag = PyOS_ascii_atof(s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3065 PyFPE_END_PROTECT(c) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3066 return PyComplex_FromCComplex(c); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3067 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3068 else |
| 35742 | 3069 #endif |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3070 { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3071 PyFPE_START_PROTECT("atof", return 0) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3072 dx = PyOS_ascii_atof(s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3073 PyFPE_END_PROTECT(dx) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3074 return PyFloat_FromDouble(dx); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3075 } |
| 35742 | 3076 } |
| 3077 | |
| 3078 static PyObject * | |
| 3079 decode_utf8(const char **sPtr, const char *end, char* encoding) | |
| 3080 { | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3081 PyObject *u, *v; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3082 char *s, *t; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3083 t = s = (char *)*sPtr; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3084 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3085 while (s < end && (*s & 0x80)) s++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3086 *sPtr = s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3087 u = PyUnicode_DecodeUTF8(t, s - t, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3088 if (u == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3089 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3090 v = PyUnicode_AsEncodedString(u, encoding, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3091 Py_DECREF(u); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3092 return v; |
| 35742 | 3093 } |
| 3094 | |
| 3095 static PyObject * | |
| 3096 decode_unicode(const char *s, size_t len, int rawmode, const char *encoding) | |
| 3097 { | |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3098 PyObject *v, *u; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3099 char *buf; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3100 char *p; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3101 const char *end; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3102 if (encoding == NULL) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3103 buf = (char *)s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3104 u = NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3105 } else if (strcmp(encoding, "iso-8859-1") == 0) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3106 buf = (char *)s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3107 u = NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3108 } else { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3109 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3110 u = PyString_FromStringAndSize((char *)NULL, len * 4); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3111 if (u == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3112 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3113 p = buf = PyString_AsString(u); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3114 end = s + len; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3115 while (s < end) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3116 if (*s == '\\') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3117 *p++ = *s++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3118 if (*s & 0x80) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3119 strcpy(p, "u005c"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3120 p += 5; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3121 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3122 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3123 if (*s & 0x80) { /* XXX inefficient */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3124 PyObject *w; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3125 char *r; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3126 Py_ssize_t rn, i; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3127 w = decode_utf8(&s, end, "utf-16-be"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3128 if (w == NULL) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3129 Py_DECREF(u); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3130 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3131 } |
|
41595
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41587
diff
changeset
|
3132 assert(PyBytes_Check(w)); |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41587
diff
changeset
|
3133 r = PyBytes_AsString(w); |
|
2c54763c4504
More coding by random modification.
Guido van Rossum <guido@python.org>
parents:
41587
diff
changeset
|
3134 rn = PyBytes_Size(w); |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3135 assert(rn % 2 == 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3136 for (i = 0; i < rn; i += 2) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3137 sprintf(p, "\\u%02x%02x", |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3138 r[i + 0] & 0xFF, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3139 r[i + 1] & 0xFF); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3140 p += 6; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3141 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3142 Py_DECREF(w); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3143 } else { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3144 *p++ = *s++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3145 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3146 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3147 len = p - buf; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3148 s = buf; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3149 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3150 if (rawmode) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3151 v = PyUnicode_DecodeRawUnicodeEscape(s, len, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3152 else |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3153 v = PyUnicode_DecodeUnicodeEscape(s, len, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3154 Py_XDECREF(u); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3155 return v; |
| 35742 | 3156 } |
| 3157 | |
| 3158 /* s is a Python string literal, including the bracketing quote characters, | |
| 3159 * and r &/or u prefixes (if any), and embedded escape sequences (if any). | |
| 3160 * parsestr parses it, and returns the decoded Python string object. | |
| 3161 */ | |
| 3162 static PyObject * | |
| 40894 | 3163 parsestr(const node *n, const char *encoding, int *bytesmode) |
| 35742 | 3164 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3165 size_t len; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3166 const char *s = STR(n); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3167 int quote = Py_CHARMASK(*s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3168 int rawmode = 0; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3169 int need_encoding; |
| 35742 | 3170 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3171 if (isalpha(quote) || quote == '_') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3172 if (quote == 'b' || quote == 'B') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3173 quote = *++s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3174 *bytesmode = 1; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3175 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3176 if (quote == 'r' || quote == 'R') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3177 quote = *++s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3178 rawmode = 1; |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3179 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3180 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3181 if (quote != '\'' && quote != '\"') { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3182 PyErr_BadInternalCall(); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3183 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3184 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3185 s++; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3186 len = strlen(s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3187 if (len > INT_MAX) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3188 PyErr_SetString(PyExc_OverflowError, |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3189 "string to parse is too long"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3190 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3191 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3192 if (s[--len] != quote) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3193 PyErr_BadInternalCall(); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3194 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3195 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3196 if (len >= 4 && s[0] == quote && s[1] == quote) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3197 s += 2; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3198 len -= 2; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3199 if (s[--len] != quote || s[--len] != quote) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3200 PyErr_BadInternalCall(); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3201 return NULL; |
| 40894 | 3202 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3203 } |
|
41558
29ae667f3239
Checkpoint. Manipulated things so that string literals are always
Guido van Rossum <guido@python.org>
parents:
41554
diff
changeset
|
3204 if (!*bytesmode) { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3205 return decode_unicode(s, len, rawmode, encoding); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3206 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3207 if (*bytesmode) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3208 /* Disallow non-ascii characters (but not escapes) */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3209 const char *c; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3210 for (c = s; *c; c++) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3211 if (Py_CHARMASK(*c) >= 0x80) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3212 ast_error(n, "bytes can only contain ASCII " |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3213 "literal characters."); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3214 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3215 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3216 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3217 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3218 need_encoding = (!*bytesmode && encoding != NULL && |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3219 strcmp(encoding, "utf-8") != 0 && |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3220 strcmp(encoding, "iso-8859-1") != 0); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3221 if (rawmode || strchr(s, '\\') == NULL) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3222 if (need_encoding) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3223 PyObject *v, *u = PyUnicode_DecodeUTF8(s, len, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3224 if (u == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3225 return NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3226 v = PyUnicode_AsEncodedString(u, encoding, NULL); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3227 Py_DECREF(u); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3228 return v; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3229 } else { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3230 return PyString_FromStringAndSize(s, len); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3231 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3232 } |
| 35742 | 3233 |
|
41698
7d49038e99c9
Remove support for u"..." literals.
Guido van Rossum <guido@python.org>
parents:
41595
diff
changeset
|
3234 return PyString_DecodeEscape(s, len, NULL, 1, |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3235 need_encoding ? encoding : NULL); |
| 35742 | 3236 } |
| 3237 | |
| 3238 /* Build a Python string object out of a STRING atom. This takes care of | |
| 3239 * compile-time literal catenation, calling parsestr() on each piece, and | |
| 3240 * pasting the intermediate results together. | |
| 3241 */ | |
| 3242 static PyObject * | |
| 40894 | 3243 parsestrplus(struct compiling *c, const node *n, int *bytesmode) |
| 35742 | 3244 { |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3245 PyObject *v; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3246 int i; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3247 REQ(CHILD(n, 0), STRING); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3248 v = parsestr(CHILD(n, 0), c->c_encoding, bytesmode); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3249 if (v != NULL) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3250 /* String literal concatenation */ |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3251 for (i = 1; i < NCH(n); i++) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3252 PyObject *s; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3253 int subbm = 0; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3254 s = parsestr(CHILD(n, i), c->c_encoding, &subbm); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3255 if (s == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3256 goto onError; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3257 if (*bytesmode != subbm) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3258 ast_error(n, "cannot mix bytes and nonbytes" |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3259 "literals"); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3260 goto onError; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3261 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3262 if (PyString_Check(v) && PyString_Check(s)) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3263 PyString_ConcatAndDel(&v, s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3264 if (v == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3265 goto onError; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3266 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3267 else { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3268 PyObject *temp = PyUnicode_Concat(v, s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3269 Py_DECREF(s); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3270 Py_DECREF(v); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3271 v = temp; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3272 if (v == NULL) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3273 goto onError; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3274 } |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
40215
diff
changeset
|
3275 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3276 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3277 return v; |
| 35742 | 3278 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3279 onError: |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3280 Py_XDECREF(v); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41509
diff
changeset
|
3281 return NULL; |
| 35742 | 3282 } |
