Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Parser-tokenizer link implementation */ |
| 3 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | #include "tokenizer.h" |
| 6 | #include "node.h" |
| 7 | #include "grammar.h" |
| 8 | #include "parser.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | #include "parsetok.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | #include "errcode.h" |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 11 | #include "graminit.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 6135df6 | 1998-04-10 19:35:06 +0000 | [diff] [blame] | 13 | int Py_TabcheckFlag; |
| 14 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 15 | |
| 16 | /* Forward */ |
Christian Heimes | 3c60833 | 2008-03-26 22:01:37 +0000 | [diff] [blame] | 17 | static node *parsetok(struct tok_state *, grammar *, int, perrdetail *, int *); |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 18 | static void initerr(perrdetail *err_ret, const char* filename); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 19 | |
| 20 | /* Parse input coming from a string. Return error code, print some errors. */ |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 21 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 22 | PyParser_ParseString(const char *s, grammar *g, int start, perrdetail *err_ret) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 23 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 24 | return PyParser_ParseStringFlagsFilename(s, NULL, g, start, err_ret, 0); |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 28 | PyParser_ParseStringFlags(const char *s, grammar *g, int start, |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 29 | perrdetail *err_ret, int flags) |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 30 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 31 | return PyParser_ParseStringFlagsFilename(s, NULL, |
| 32 | g, start, err_ret, flags); |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 36 | PyParser_ParseStringFlagsFilename(const char *s, const char *filename, |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 37 | grammar *g, int start, |
| 38 | perrdetail *err_ret, int flags) |
Thomas Heller | 6b17abf | 2002-07-09 09:23:27 +0000 | [diff] [blame] | 39 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 40 | int iflags = flags; |
| 41 | return PyParser_ParseStringFlagsFilenameEx(s, filename, g, start, |
| 42 | err_ret, &iflags); |
Christian Heimes | 3c60833 | 2008-03-26 22:01:37 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | node * |
| 46 | PyParser_ParseStringFlagsFilenameEx(const char *s, const char *filename, |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 47 | grammar *g, int start, |
| 48 | perrdetail *err_ret, int *flags) |
Christian Heimes | 3c60833 | 2008-03-26 22:01:37 +0000 | [diff] [blame] | 49 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 50 | struct tok_state *tok; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 51 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 52 | initerr(err_ret, filename); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 53 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 54 | if ((tok = PyTokenizer_FromString(s)) == NULL) { |
| 55 | err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM; |
| 56 | return NULL; |
| 57 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 58 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 59 | tok->filename = filename ? filename : "<string>"; |
| 60 | if (Py_TabcheckFlag || Py_VerboseFlag) { |
| 61 | tok->altwarning = (tok->filename != NULL); |
| 62 | if (Py_TabcheckFlag >= 2) |
| 63 | tok->alterror++; |
| 64 | } |
Guido van Rossum | 89ce454 | 1998-12-21 18:32:40 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 66 | return parsetok(tok, g, start, err_ret, flags); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 69 | /* Parse input coming from a file. Return error code, print some errors. */ |
| 70 | |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 71 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 72 | PyParser_ParseFile(FILE *fp, const char *filename, grammar *g, int start, |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 73 | char *ps1, char *ps2, perrdetail *err_ret) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 74 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 75 | return PyParser_ParseFileFlags(fp, filename, g, start, ps1, ps2, |
| 76 | err_ret, 0); |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | node * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 80 | PyParser_ParseFileFlags(FILE *fp, const char *filename, grammar *g, int start, |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 81 | char *ps1, char *ps2, perrdetail *err_ret, int flags) |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 82 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 83 | int iflags = flags; |
| 84 | return PyParser_ParseFileFlagsEx(fp, filename, g, start, ps1, ps2, err_ret, &iflags); |
Christian Heimes | 3c60833 | 2008-03-26 22:01:37 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | node * |
| 88 | PyParser_ParseFileFlagsEx(FILE *fp, const char *filename, grammar *g, int start, |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 89 | char *ps1, char *ps2, perrdetail *err_ret, int *flags) |
Christian Heimes | 3c60833 | 2008-03-26 22:01:37 +0000 | [diff] [blame] | 90 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 91 | struct tok_state *tok; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 92 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 93 | initerr(err_ret, filename); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 94 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 95 | if ((tok = PyTokenizer_FromFile(fp, ps1, ps2)) == NULL) { |
| 96 | err_ret->error = E_NOMEM; |
| 97 | return NULL; |
| 98 | } |
| 99 | tok->filename = filename; |
| 100 | if (Py_TabcheckFlag || Py_VerboseFlag) { |
| 101 | tok->altwarning = (filename != NULL); |
| 102 | if (Py_TabcheckFlag >= 2) |
| 103 | tok->alterror++; |
| 104 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 106 | return parsetok(tok, g, start, err_ret, flags); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 107 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | |
Neal Norwitz | ca460d9 | 2006-09-06 06:28:06 +0000 | [diff] [blame] | 109 | #if 0 |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 110 | static char with_msg[] = |
| 111 | "%s:%d: Warning: 'with' will become a reserved keyword in Python 2.6\n"; |
| 112 | |
| 113 | static char as_msg[] = |
| 114 | "%s:%d: Warning: 'as' will become a reserved keyword in Python 2.6\n"; |
| 115 | |
| 116 | static void |
| 117 | warn(const char *msg, const char *filename, int lineno) |
| 118 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 119 | if (filename == NULL) |
| 120 | filename = "<string>"; |
| 121 | PySys_WriteStderr(msg, filename, lineno); |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 122 | } |
Neal Norwitz | ca460d9 | 2006-09-06 06:28:06 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
| 125 | /* Parse input coming from the given tokenizer structure. |
| 126 | Return error code. */ |
Guido van Rossum | da62ecc | 2001-07-17 16:53:11 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 128 | static node * |
Tim Peters | fe2127d | 2001-07-16 05:37:24 +0000 | [diff] [blame] | 129 | parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret, |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 130 | int *flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 131 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 132 | parser_state *ps; |
| 133 | node *n; |
| 134 | int started = 0, handling_import = 0, handling_with = 0; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 136 | if ((ps = PyParser_New(g, start)) == NULL) { |
| 137 | fprintf(stderr, "no mem for new parser\n"); |
| 138 | err_ret->error = E_NOMEM; |
| 139 | PyTokenizer_Free(tok); |
| 140 | return NULL; |
| 141 | } |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 142 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 143 | if (*flags & PyPARSE_PRINT_IS_FUNCTION) { |
| 144 | ps->p_flags |= CO_FUTURE_PRINT_FUNCTION; |
| 145 | } |
| 146 | if (*flags & PyPARSE_UNICODE_LITERALS) { |
| 147 | ps->p_flags |= CO_FUTURE_UNICODE_LITERALS; |
| 148 | } |
Christian Heimes | 3c60833 | 2008-03-26 22:01:37 +0000 | [diff] [blame] | 149 | |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 150 | #endif |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 151 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 152 | for (;;) { |
| 153 | char *a, *b; |
| 154 | int type; |
| 155 | size_t len; |
| 156 | char *str; |
| 157 | int col_offset; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 158 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 159 | type = PyTokenizer_Get(tok, &a, &b); |
| 160 | if (type == ERRORTOKEN) { |
| 161 | err_ret->error = tok->done; |
| 162 | break; |
| 163 | } |
| 164 | if (type == ENDMARKER && started) { |
| 165 | type = NEWLINE; /* Add an extra newline */ |
| 166 | handling_with = handling_import = 0; |
| 167 | started = 0; |
| 168 | /* Add the right number of dedent tokens, |
| 169 | except if a certain flag is given -- |
| 170 | codeop.py uses this. */ |
| 171 | if (tok->indent && |
| 172 | !(*flags & PyPARSE_DONT_IMPLY_DEDENT)) |
| 173 | { |
| 174 | tok->pendin = -tok->indent; |
| 175 | tok->indent = 0; |
| 176 | } |
| 177 | } |
| 178 | else |
| 179 | started = 1; |
| 180 | len = b - a; /* XXX this may compute NULL - NULL */ |
| 181 | str = (char *) PyObject_MALLOC(len + 1); |
| 182 | if (str == NULL) { |
| 183 | fprintf(stderr, "no mem for next token\n"); |
| 184 | err_ret->error = E_NOMEM; |
| 185 | break; |
| 186 | } |
| 187 | if (len > 0) |
| 188 | strncpy(str, a, len); |
| 189 | str[len] = '\0'; |
Guido van Rossum | da62ecc | 2001-07-17 16:53:11 +0000 | [diff] [blame] | 190 | |
Thomas Wouters | 34aa7ba | 2006-02-28 19:02:24 +0000 | [diff] [blame] | 191 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Neil Schemenauer | c24ea08 | 2002-03-22 23:53:36 +0000 | [diff] [blame] | 192 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 193 | if (a >= tok->line_start) |
| 194 | col_offset = a - tok->line_start; |
| 195 | else |
| 196 | col_offset = -1; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 197 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 198 | if ((err_ret->error = |
| 199 | PyParser_AddToken(ps, (int)type, str, tok->lineno, col_offset, |
| 200 | &(err_ret->expected))) != E_OK) { |
| 201 | if (err_ret->error != E_DONE) { |
| 202 | PyObject_FREE(str); |
| 203 | err_ret->token = type; |
| 204 | } |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (err_ret->error == E_DONE) { |
| 210 | n = ps->p_tree; |
| 211 | ps->p_tree = NULL; |
| 212 | } |
| 213 | else |
| 214 | n = NULL; |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 215 | |
Christian Heimes | 342212c | 2008-03-26 22:20:26 +0000 | [diff] [blame] | 216 | #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 217 | *flags = ps->p_flags; |
Christian Heimes | 342212c | 2008-03-26 22:20:26 +0000 | [diff] [blame] | 218 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 219 | PyParser_Delete(ps); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 220 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 221 | if (n == NULL) { |
| 222 | if (tok->lineno <= 1 && tok->done == E_EOF) |
| 223 | err_ret->error = E_EOF; |
| 224 | err_ret->lineno = tok->lineno; |
| 225 | if (tok->buf != NULL) { |
| 226 | char *text = NULL; |
| 227 | size_t len; |
| 228 | assert(tok->cur - tok->buf < INT_MAX); |
| 229 | err_ret->offset = (int)(tok->cur - tok->buf); |
| 230 | len = tok->inp - tok->buf; |
Martin v. Löwis | a513619 | 2007-09-04 14:19:28 +0000 | [diff] [blame] | 231 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 232 | text = PyTokenizer_RestoreEncoding(tok, len, &err_ret->offset); |
Martin v. Löwis | a513619 | 2007-09-04 14:19:28 +0000 | [diff] [blame] | 233 | |
| 234 | #endif |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 235 | if (text == NULL) { |
| 236 | text = (char *) PyObject_MALLOC(len + 1); |
| 237 | if (text != NULL) { |
| 238 | if (len > 0) |
| 239 | strncpy(text, tok->buf, len); |
| 240 | text[len] = '\0'; |
| 241 | } |
| 242 | } |
| 243 | err_ret->text = text; |
| 244 | } |
| 245 | } else if (tok->encoding != NULL) { |
| 246 | node* r = PyNode_New(encoding_decl); |
| 247 | if (!r) { |
| 248 | err_ret->error = E_NOMEM; |
| 249 | n = NULL; |
| 250 | goto done; |
| 251 | } |
| 252 | r->n_str = tok->encoding; |
| 253 | r->n_nchildren = 1; |
| 254 | r->n_child = n; |
| 255 | tok->encoding = NULL; |
| 256 | n = r; |
| 257 | } |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 258 | |
Neal Norwitz | 6bf1a8f | 2006-08-12 01:49:12 +0000 | [diff] [blame] | 259 | done: |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 260 | PyTokenizer_Free(tok); |
Guido van Rossum | bd0389d | 1994-08-29 12:25:45 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 262 | return n; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | } |
Guido van Rossum | 0c156a5 | 2001-10-20 14:27:56 +0000 | [diff] [blame] | 264 | |
| 265 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 266 | initerr(perrdetail *err_ret, const char *filename) |
Guido van Rossum | 0c156a5 | 2001-10-20 14:27:56 +0000 | [diff] [blame] | 267 | { |
Antoine Pitrou | c7c96a9 | 2010-05-09 15:15:40 +0000 | [diff] [blame] | 268 | err_ret->error = E_OK; |
| 269 | err_ret->filename = filename; |
| 270 | err_ret->lineno = 0; |
| 271 | err_ret->offset = 0; |
| 272 | err_ret->text = NULL; |
| 273 | err_ret->token = -1; |
| 274 | err_ret->expected = -1; |
Guido van Rossum | 0c156a5 | 2001-10-20 14:27:56 +0000 | [diff] [blame] | 275 | } |