Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "node.h" |
| 3 | #include "token.h" |
| 4 | #include "graminit.h" |
| 5 | #include "compile.h" |
| 6 | #include "symtable.h" |
| 7 | |
| 8 | #define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined" |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 9 | #define FUTURE_IMPORT_STAR "future statement does not support import *" |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 10 | |
Jeremy Hylton | 1e35ce5 | 2001-08-20 20:32:33 +0000 | [diff] [blame] | 11 | /* FUTURE_POSSIBLE() is provided to accomodate doc strings, which is |
| 12 | the only statement that can occur before a future statement. |
| 13 | */ |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 14 | #define FUTURE_POSSIBLE(FF) ((FF)->ff_last_lineno == -1) |
| 15 | |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 16 | static int |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 17 | future_check_features(PyFutureFeatures *ff, node *n, char *filename) |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 18 | { |
| 19 | int i; |
| 20 | char *feature; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 21 | node *ch; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 22 | |
| 23 | REQ(n, import_stmt); /* must by from __future__ import ... */ |
| 24 | |
Fred Drake | 198457a | 2001-03-10 02:15:37 +0000 | [diff] [blame] | 25 | for (i = 3; i < NCH(n); i += 2) { |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 26 | ch = CHILD(n, i); |
| 27 | if (TYPE(ch) == STAR) { |
| 28 | PyErr_SetString(PyExc_SyntaxError, |
| 29 | FUTURE_IMPORT_STAR); |
| 30 | PyErr_SyntaxLocation(filename, ch->n_lineno); |
| 31 | return -1; |
| 32 | } |
| 33 | REQ(ch, import_as_name); |
| 34 | feature = STR(CHILD(ch, 0)); |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 35 | if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 36 | continue; |
Guido van Rossum | b09f7ed | 2001-07-15 21:08:29 +0000 | [diff] [blame] | 37 | } else if (strcmp(feature, FUTURE_GENERATORS) == 0) { |
Tim Peters | 2bbdba3 | 2002-04-12 01:20:10 +0000 | [diff] [blame^] | 38 | continue; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 39 | } else if (strcmp(feature, FUTURE_DIVISION) == 0) { |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 40 | ff->ff_features |= CO_FUTURE_DIVISION; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 41 | } else if (strcmp(feature, "braces") == 0) { |
| 42 | PyErr_SetString(PyExc_SyntaxError, |
| 43 | "not a chance"); |
| 44 | PyErr_SyntaxLocation(filename, CHILD(ch, 0)->n_lineno); |
| 45 | return -1; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 46 | } else { |
| 47 | PyErr_Format(PyExc_SyntaxError, |
| 48 | UNDEFINED_FUTURE_FEATURE, feature); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 49 | PyErr_SyntaxLocation(filename, CHILD(ch, 0)->n_lineno); |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 50 | return -1; |
| 51 | } |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 56 | static void |
| 57 | future_error(node *n, char *filename) |
| 58 | { |
| 59 | PyErr_SetString(PyExc_SyntaxError, |
| 60 | "from __future__ imports must occur at the " |
| 61 | "beginning of the file"); |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 62 | PyErr_SyntaxLocation(filename, n->n_lineno); |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 65 | /* Relevant portions of the grammar: |
| 66 | |
| 67 | single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 68 | file_input: (NEWLINE | stmt)* ENDMARKER |
| 69 | stmt: simple_stmt | compound_stmt |
| 70 | simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 71 | small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt |
| 72 | | import_stmt | global_stmt | exec_stmt | assert_stmt |
| 73 | import_stmt: 'import' dotted_as_name (',' dotted_as_name)* |
| 74 | | 'from' dotted_name 'import' ('*' | import_as_name (',' import_as_name)*) |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 75 | import_as_name: NAME [NAME NAME] |
| 76 | dotted_as_name: dotted_name [NAME NAME] |
| 77 | dotted_name: NAME ('.' NAME)* |
| 78 | */ |
| 79 | |
Jeremy Hylton | 1e35ce5 | 2001-08-20 20:32:33 +0000 | [diff] [blame] | 80 | /* future_parse() finds future statements at the beginnning of a |
| 81 | module. The function calls itself recursively, rather than |
| 82 | factoring out logic for different kinds of statements into |
| 83 | different routines. |
| 84 | |
| 85 | Return values: |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 86 | -1 indicates an error occurred, e.g. unknown feature name |
| 87 | 0 indicates no feature was found |
| 88 | 1 indicates a feature was found |
| 89 | */ |
| 90 | |
| 91 | static int |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 92 | future_parse(PyFutureFeatures *ff, node *n, char *filename) |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 93 | { |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 94 | int i, r; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 95 | loop: |
| 96 | |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 97 | switch (TYPE(n)) { |
| 98 | |
Jeremy Hylton | 280c81a | 2001-02-28 02:26:14 +0000 | [diff] [blame] | 99 | case single_input: |
| 100 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
| 101 | n = CHILD(n, 0); |
| 102 | goto loop; |
| 103 | } |
| 104 | return 0; |
| 105 | |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 106 | case file_input: |
Jeremy Hylton | 1e35ce5 | 2001-08-20 20:32:33 +0000 | [diff] [blame] | 107 | /* Check each statement in the file, starting with the |
| 108 | first, and continuing until the first statement |
| 109 | that isn't a future statement. |
| 110 | */ |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 111 | for (i = 0; i < NCH(n); i++) { |
| 112 | node *ch = CHILD(n, i); |
| 113 | if (TYPE(ch) == stmt) { |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 114 | r = future_parse(ff, ch, filename); |
Jeremy Hylton | 1e35ce5 | 2001-08-20 20:32:33 +0000 | [diff] [blame] | 115 | /* Need to check both conditions below |
| 116 | to accomodate doc strings, which |
| 117 | causes r < 0. |
| 118 | */ |
| 119 | if (r < 1 && !FUTURE_POSSIBLE(ff)) |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 120 | return r; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | return 0; |
| 124 | |
| 125 | case simple_stmt: |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 126 | if (NCH(n) == 2) { |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 127 | REQ(CHILD(n, 0), small_stmt); |
| 128 | n = CHILD(n, 0); |
| 129 | goto loop; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 130 | } else { |
| 131 | /* Deal with the special case of a series of |
| 132 | small statements on a single line. If a |
| 133 | future statement follows some other |
| 134 | statement, the SyntaxError is raised here. |
| 135 | In all other cases, the symtable pass |
| 136 | raises the exception. |
| 137 | */ |
| 138 | int found = 0, end_of_future = 0; |
| 139 | |
| 140 | for (i = 0; i < NCH(n); i += 2) { |
| 141 | if (TYPE(CHILD(n, i)) == small_stmt) { |
| 142 | r = future_parse(ff, CHILD(n, i), |
| 143 | filename); |
| 144 | if (r < 1) |
| 145 | end_of_future = 1; |
| 146 | else { |
| 147 | found = 1; |
| 148 | if (end_of_future) { |
| 149 | future_error(n, |
| 150 | filename); |
| 151 | return -1; |
| 152 | } |
| 153 | } |
| 154 | } |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 155 | } |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 156 | |
| 157 | /* If we found one and only one, then the |
| 158 | current lineno is legal. |
| 159 | */ |
| 160 | if (found) |
| 161 | ff->ff_last_lineno = n->n_lineno + 1; |
| 162 | else |
| 163 | ff->ff_last_lineno = n->n_lineno; |
| 164 | |
| 165 | if (end_of_future && found) |
| 166 | return 1; |
| 167 | else |
| 168 | return 0; |
| 169 | } |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 170 | |
| 171 | case stmt: |
| 172 | if (TYPE(CHILD(n, 0)) == simple_stmt) { |
| 173 | n = CHILD(n, 0); |
| 174 | goto loop; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 175 | } else if (TYPE(CHILD(n, 0)) == expr_stmt) { |
| 176 | n = CHILD(n, 0); |
| 177 | goto loop; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 178 | } else { |
| 179 | REQ(CHILD(n, 0), compound_stmt); |
| 180 | ff->ff_last_lineno = n->n_lineno; |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | case small_stmt: |
| 185 | n = CHILD(n, 0); |
| 186 | goto loop; |
| 187 | |
| 188 | case import_stmt: { |
| 189 | node *name; |
| 190 | |
| 191 | if (STR(CHILD(n, 0))[0] != 'f') { /* from */ |
| 192 | ff->ff_last_lineno = n->n_lineno; |
| 193 | return 0; |
| 194 | } |
| 195 | name = CHILD(n, 1); |
| 196 | if (strcmp(STR(CHILD(name, 0)), "__future__") != 0) |
| 197 | return 0; |
Jeremy Hylton | ad3d3f2 | 2001-02-28 17:47:12 +0000 | [diff] [blame] | 198 | if (future_check_features(ff, n, filename) < 0) |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 199 | return -1; |
Jeremy Hylton | 280c81a | 2001-02-28 02:26:14 +0000 | [diff] [blame] | 200 | ff->ff_last_lineno = n->n_lineno + 1; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 201 | return 1; |
| 202 | } |
| 203 | |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 204 | /* The cases below -- all of them! -- are necessary to find |
| 205 | and skip doc strings. */ |
| 206 | case expr_stmt: |
| 207 | case testlist: |
| 208 | case test: |
| 209 | case and_test: |
| 210 | case not_test: |
| 211 | case comparison: |
| 212 | case expr: |
| 213 | case xor_expr: |
| 214 | case and_expr: |
| 215 | case shift_expr: |
| 216 | case arith_expr: |
| 217 | case term: |
| 218 | case factor: |
| 219 | case power: |
| 220 | if (NCH(n) == 1) { |
| 221 | n = CHILD(n, 0); |
| 222 | goto loop; |
| 223 | } |
| 224 | break; |
| 225 | |
| 226 | case atom: |
| 227 | if (TYPE(CHILD(n, 0)) == STRING |
| 228 | && ff->ff_found_docstring == 0) { |
| 229 | ff->ff_found_docstring = 1; |
| 230 | return 0; |
| 231 | } |
| 232 | ff->ff_last_lineno = n->n_lineno; |
| 233 | return 0; |
| 234 | |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 235 | default: |
| 236 | ff->ff_last_lineno = n->n_lineno; |
| 237 | return 0; |
| 238 | } |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 239 | return 0; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | PyFutureFeatures * |
| 243 | PyNode_Future(node *n, char *filename) |
| 244 | { |
| 245 | PyFutureFeatures *ff; |
| 246 | |
| 247 | ff = (PyFutureFeatures *)PyMem_Malloc(sizeof(PyFutureFeatures)); |
| 248 | if (ff == NULL) |
| 249 | return NULL; |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 250 | ff->ff_found_docstring = 0; |
| 251 | ff->ff_last_lineno = -1; |
Jeremy Hylton | b857ba2 | 2001-08-10 21:41:33 +0000 | [diff] [blame] | 252 | ff->ff_features = 0; |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 253 | |
Jeremy Hylton | 39e2f3f | 2001-02-28 01:58:08 +0000 | [diff] [blame] | 254 | if (future_parse(ff, n, filename) < 0) { |
Jeremy Hylton | 4db62b1 | 2001-02-27 19:07:02 +0000 | [diff] [blame] | 255 | PyMem_Free((void *)ff); |
| 256 | return NULL; |
| 257 | } |
| 258 | return ff; |
| 259 | } |
| 260 | |