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