blob: 629dee565cd9266ad50e9f309252cfb6005261d5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Parser-tokenizer link implementation */
3
Guido van Rossum3f5da241990-12-20 15:06:42 +00004#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005#include "tokenizer.h"
6#include "node.h"
7#include "grammar.h"
8#include "parser.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00009#include "parsetok.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010#include "errcode.h"
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +000011#include "graminit.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
Guido van Rossum3f5da241990-12-20 15:06:42 +000013
14/* Forward */
Christian Heimes4d6ec852008-03-26 22:34:47 +000015static node *parsetok(struct tok_state *, grammar *, int, perrdetail *, int *);
Victor Stinner14e461d2013-08-26 22:28:21 +020016static int initerr(perrdetail *err_ret, PyObject * filename);
Guido van Rossum3f5da241990-12-20 15:06:42 +000017
18/* Parse input coming from a string. Return error code, print some errors. */
Guido van Rossumbd0389d1994-08-29 12:25:45 +000019node *
Martin v. Löwis95292d62002-12-11 14:04:59 +000020PyParser_ParseString(const char *s, grammar *g, int start, perrdetail *err_ret)
Guido van Rossum3f5da241990-12-20 15:06:42 +000021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 return PyParser_ParseStringFlagsFilename(s, NULL, g, start, err_ret, 0);
Tim Petersfe2127d2001-07-16 05:37:24 +000023}
24
25node *
Martin v. Löwis95292d62002-12-11 14:04:59 +000026PyParser_ParseStringFlags(const char *s, grammar *g, int start,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 perrdetail *err_ret, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +000028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 return PyParser_ParseStringFlagsFilename(s, NULL,
30 g, start, err_ret, flags);
Thomas Heller6b17abf2002-07-09 09:23:27 +000031}
32
33node *
Martin v. Löwis95292d62002-12-11 14:04:59 +000034PyParser_ParseStringFlagsFilename(const char *s, const char *filename,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 grammar *g, int start,
36 perrdetail *err_ret, int flags)
Thomas Heller6b17abf2002-07-09 09:23:27 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 int iflags = flags;
39 return PyParser_ParseStringFlagsFilenameEx(s, filename, g, start,
40 err_ret, &iflags);
Christian Heimes4d6ec852008-03-26 22:34:47 +000041}
42
43node *
Victor Stinner14e461d2013-08-26 22:28:21 +020044PyParser_ParseStringObject(const char *s, PyObject *filename,
45 grammar *g, int start,
46 perrdetail *err_ret, int *flags)
Christian Heimes4d6ec852008-03-26 22:34:47 +000047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 struct tok_state *tok;
49 int exec_input = start == file_input;
Guido van Rossumbd0389d1994-08-29 12:25:45 +000050
Victor Stinner7f2fee32011-04-05 00:39:01 +020051 if (initerr(err_ret, filename) < 0)
52 return NULL;
Guido van Rossumbd0389d1994-08-29 12:25:45 +000053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 if (*flags & PyPARSE_IGNORE_COOKIE)
55 tok = PyTokenizer_FromUTF8(s, exec_input);
56 else
57 tok = PyTokenizer_FromString(s, exec_input);
58 if (tok == NULL) {
59 err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM;
60 return NULL;
61 }
Guido van Rossumbd0389d1994-08-29 12:25:45 +000062
Victor Stinner7f2fee32011-04-05 00:39:01 +020063#ifndef PGEN
64 Py_INCREF(err_ret->filename);
65 tok->filename = err_ret->filename;
66#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 return parsetok(tok, g, start, err_ret, flags);
Guido van Rossum3f5da241990-12-20 15:06:42 +000068}
69
Victor Stinner14e461d2013-08-26 22:28:21 +020070node *
71PyParser_ParseStringFlagsFilenameEx(const char *s, const char *filename_str,
72 grammar *g, int start,
73 perrdetail *err_ret, int *flags)
74{
75 node *n;
76 PyObject *filename = NULL;
77#ifndef PGEN
78 if (filename_str != NULL) {
79 filename = PyUnicode_DecodeFSDefault(filename_str);
80 if (filename == NULL) {
81 err_ret->error = E_ERROR;
82 return NULL;
83 }
84 }
85#endif
86 n = PyParser_ParseStringObject(s, filename, g, start, err_ret, flags);
87#ifndef PGEN
88 Py_XDECREF(filename);
89#endif
90 return n;
91}
92
Guido van Rossum3f5da241990-12-20 15:06:42 +000093/* Parse input coming from a file. Return error code, print some errors. */
94
Guido van Rossumbd0389d1994-08-29 12:25:45 +000095node *
Martin v. Löwis95292d62002-12-11 14:04:59 +000096PyParser_ParseFile(FILE *fp, const char *filename, grammar *g, int start,
Serhiy Storchakac6792272013-10-19 21:03:34 +030097 const char *ps1, const char *ps2,
98 perrdetail *err_ret)
Guido van Rossum3f5da241990-12-20 15:06:42 +000099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 return PyParser_ParseFileFlags(fp, filename, NULL,
101 g, start, ps1, ps2, err_ret, 0);
Tim Petersfe2127d2001-07-16 05:37:24 +0000102}
103
104node *
Christian Heimes4d6ec852008-03-26 22:34:47 +0000105PyParser_ParseFileFlags(FILE *fp, const char *filename, const char *enc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 grammar *g, int start,
Serhiy Storchakac6792272013-10-19 21:03:34 +0300107 const char *ps1, const char *ps2,
108 perrdetail *err_ret, int flags)
Tim Petersfe2127d2001-07-16 05:37:24 +0000109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 int iflags = flags;
111 return PyParser_ParseFileFlagsEx(fp, filename, enc, g, start, ps1,
112 ps2, err_ret, &iflags);
Christian Heimes4d6ec852008-03-26 22:34:47 +0000113}
114
115node *
Victor Stinner14e461d2013-08-26 22:28:21 +0200116PyParser_ParseFileObject(FILE *fp, PyObject *filename,
117 const char *enc, grammar *g, int start,
Serhiy Storchakac6792272013-10-19 21:03:34 +0300118 const char *ps1, const char *ps2,
119 perrdetail *err_ret, int *flags)
Christian Heimes4d6ec852008-03-26 22:34:47 +0000120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 struct tok_state *tok;
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000122
Victor Stinner7f2fee32011-04-05 00:39:01 +0200123 if (initerr(err_ret, filename) < 0)
124 return NULL;
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000125
Serhiy Storchakac6792272013-10-19 21:03:34 +0300126 if ((tok = PyTokenizer_FromFile(fp, enc, ps1, ps2)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 err_ret->error = E_NOMEM;
128 return NULL;
129 }
Victor Stinner7f2fee32011-04-05 00:39:01 +0200130#ifndef PGEN
131 Py_INCREF(err_ret->filename);
132 tok->filename = err_ret->filename;
133#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 return parsetok(tok, g, start, err_ret, flags);
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000135}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136
Victor Stinner14e461d2013-08-26 22:28:21 +0200137node *
138PyParser_ParseFileFlagsEx(FILE *fp, const char *filename,
139 const char *enc, grammar *g, int start,
Serhiy Storchakac6792272013-10-19 21:03:34 +0300140 const char *ps1, const char *ps2,
141 perrdetail *err_ret, int *flags)
Victor Stinner14e461d2013-08-26 22:28:21 +0200142{
143 node *n;
144 PyObject *fileobj = NULL;
145#ifndef PGEN
146 if (filename != NULL) {
147 fileobj = PyUnicode_DecodeFSDefault(filename);
148 if (fileobj == NULL) {
149 err_ret->error = E_ERROR;
150 return NULL;
151 }
152 }
153#endif
154 n = PyParser_ParseFileObject(fp, fileobj, enc, g,
155 start, ps1, ps2, err_ret, flags);
156#ifndef PGEN
157 Py_XDECREF(fileobj);
158#endif
159 return n;
160}
161
Neal Norwitze4993c72006-03-16 06:01:25 +0000162#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
Brett Cannone3944a52009-04-01 05:08:41 +0000163#if 0
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000164static char with_msg[] =
165"%s:%d: Warning: 'with' will become a reserved keyword in Python 2.6\n";
166
167static char as_msg[] =
168"%s:%d: Warning: 'as' will become a reserved keyword in Python 2.6\n";
169
170static void
171warn(const char *msg, const char *filename, int lineno)
172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 if (filename == NULL)
174 filename = "<string>";
175 PySys_WriteStderr(msg, filename, lineno);
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000176}
Neal Norwitzfc85c922006-03-17 05:44:46 +0000177#endif
Brett Cannone3944a52009-04-01 05:08:41 +0000178#endif
Guido van Rossumda62ecc2001-07-17 16:53:11 +0000179
Thomas Wouters89f507f2006-12-13 04:49:30 +0000180/* Parse input coming from the given tokenizer structure.
181 Return error code. */
182
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000183static node *
Tim Petersfe2127d2001-07-16 05:37:24 +0000184parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 int *flags)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 parser_state *ps;
188 node *n;
Brett Cannonb94767f2011-02-22 20:15:44 +0000189 int started = 0;
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 if ((ps = PyParser_New(g, start)) == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 err_ret->error = E_NOMEM;
193 PyTokenizer_Free(tok);
194 return NULL;
195 }
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000196#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 if (*flags & PyPARSE_BARRY_AS_BDFL)
198 ps->p_flags |= CO_FUTURE_BARRY_AS_BDFL;
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000199#endif
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 for (;;) {
202 char *a, *b;
203 int type;
204 size_t len;
205 char *str;
206 int col_offset;
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 type = PyTokenizer_Get(tok, &a, &b);
209 if (type == ERRORTOKEN) {
210 err_ret->error = tok->done;
211 break;
212 }
213 if (type == ENDMARKER && started) {
214 type = NEWLINE; /* Add an extra newline */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 started = 0;
216 /* Add the right number of dedent tokens,
217 except if a certain flag is given --
218 codeop.py uses this. */
219 if (tok->indent &&
220 !(*flags & PyPARSE_DONT_IMPLY_DEDENT))
221 {
222 tok->pendin = -tok->indent;
223 tok->indent = 0;
224 }
225 }
226 else
227 started = 1;
228 len = b - a; /* XXX this may compute NULL - NULL */
229 str = (char *) PyObject_MALLOC(len + 1);
230 if (str == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 err_ret->error = E_NOMEM;
232 break;
233 }
234 if (len > 0)
235 strncpy(str, a, len);
236 str[len] = '\0';
Guido van Rossumda62ecc2001-07-17 16:53:11 +0000237
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000238#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 if (type == NOTEQUAL) {
240 if (!(ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) &&
241 strcmp(str, "!=")) {
Antoine Pitrou9ec25932011-11-13 01:01:23 +0100242 PyObject_FREE(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 err_ret->error = E_SYNTAX;
244 break;
245 }
246 else if ((ps->p_flags & CO_FUTURE_BARRY_AS_BDFL) &&
247 strcmp(str, "<>")) {
Antoine Pitrou9ec25932011-11-13 01:01:23 +0100248 PyObject_FREE(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 err_ret->text = "with Barry as BDFL, use '<>' "
250 "instead of '!='";
251 err_ret->error = E_SYNTAX;
252 break;
253 }
254 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +0000255#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 if (a >= tok->line_start)
Victor Stinnercad876d2013-11-18 01:09:51 +0100257 col_offset = Py_SAFE_DOWNCAST(a - tok->line_start,
258 Py_intptr_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 else
260 col_offset = -1;
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if ((err_ret->error =
263 PyParser_AddToken(ps, (int)type, str,
264 tok->lineno, col_offset,
265 &(err_ret->expected))) != E_OK) {
266 if (err_ret->error != E_DONE) {
267 PyObject_FREE(str);
268 err_ret->token = type;
269 }
270 break;
271 }
272 }
273
274 if (err_ret->error == E_DONE) {
275 n = ps->p_tree;
276 ps->p_tree = NULL;
Meador Ingefa21bf02012-01-19 01:08:41 -0600277
Benjamin Peterson79c1f962012-01-19 08:48:11 -0500278#ifndef PGEN
Meador Ingefa21bf02012-01-19 01:08:41 -0600279 /* Check that the source for a single input statement really
280 is a single statement by looking at what is left in the
281 buffer after parsing. Trailing whitespace and comments
282 are OK. */
283 if (start == single_input) {
284 char *cur = tok->cur;
285 char c = *tok->cur;
286
Benjamin Petersoncff92372012-01-19 17:46:13 -0500287 for (;;) {
288 while (c == ' ' || c == '\t' || c == '\n' || c == '\014')
289 c = *++cur;
Meador Ingefa21bf02012-01-19 01:08:41 -0600290
Benjamin Petersoncff92372012-01-19 17:46:13 -0500291 if (!c)
292 break;
293
294 if (c != '#') {
295 err_ret->error = E_BADSINGLE;
296 PyNode_Free(n);
297 n = NULL;
298 break;
299 }
300
301 /* Suck up comment. */
302 while (c && c != '\n')
303 c = *++cur;
Meador Ingefa21bf02012-01-19 01:08:41 -0600304 }
305 }
Benjamin Peterson79c1f962012-01-19 08:48:11 -0500306#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 }
308 else
309 n = NULL;
Christian Heimesb1b3efc2008-03-26 23:24:27 +0000310
Christian Heimes4d6ec852008-03-26 22:34:47 +0000311#ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 *flags = ps->p_flags;
Christian Heimes4d6ec852008-03-26 22:34:47 +0000313#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 PyParser_Delete(ps);
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 if (n == NULL) {
Benjamin Peterson758888d2011-05-30 11:12:38 -0500317 if (tok->done == E_EOF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 err_ret->error = E_EOF;
319 err_ret->lineno = tok->lineno;
320 if (tok->buf != NULL) {
321 size_t len;
322 assert(tok->cur - tok->buf < INT_MAX);
323 err_ret->offset = (int)(tok->cur - tok->buf);
324 len = tok->inp - tok->buf;
325 err_ret->text = (char *) PyObject_MALLOC(len + 1);
326 if (err_ret->text != NULL) {
327 if (len > 0)
328 strncpy(err_ret->text, tok->buf, len);
329 err_ret->text[len] = '\0';
330 }
331 }
332 } else if (tok->encoding != NULL) {
333 /* 'nodes->n_str' uses PyObject_*, while 'tok->encoding' was
334 * allocated using PyMem_
335 */
336 node* r = PyNode_New(encoding_decl);
337 if (r)
338 r->n_str = PyObject_MALLOC(strlen(tok->encoding)+1);
339 if (!r || !r->n_str) {
340 err_ret->error = E_NOMEM;
341 if (r)
342 PyObject_FREE(r);
343 n = NULL;
344 goto done;
345 }
346 strcpy(r->n_str, tok->encoding);
347 PyMem_FREE(tok->encoding);
348 tok->encoding = NULL;
349 r->n_nchildren = 1;
350 r->n_child = n;
351 n = r;
352 }
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000353
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000354done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 PyTokenizer_Free(tok);
Guido van Rossumbd0389d1994-08-29 12:25:45 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 return n;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358}
Guido van Rossum0c156a52001-10-20 14:27:56 +0000359
Victor Stinner7f2fee32011-04-05 00:39:01 +0200360static int
Victor Stinner14e461d2013-08-26 22:28:21 +0200361initerr(perrdetail *err_ret, PyObject *filename)
Guido van Rossum0c156a52001-10-20 14:27:56 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 err_ret->error = E_OK;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 err_ret->lineno = 0;
365 err_ret->offset = 0;
366 err_ret->text = NULL;
367 err_ret->token = -1;
368 err_ret->expected = -1;
Victor Stinner7f2fee32011-04-05 00:39:01 +0200369#ifndef PGEN
Victor Stinner14e461d2013-08-26 22:28:21 +0200370 if (filename) {
371 Py_INCREF(filename);
372 err_ret->filename = filename;
373 }
374 else {
Victor Stinner7f2fee32011-04-05 00:39:01 +0200375 err_ret->filename = PyUnicode_FromString("<string>");
Victor Stinner14e461d2013-08-26 22:28:21 +0200376 if (err_ret->filename == NULL) {
377 err_ret->error = E_ERROR;
378 return -1;
379 }
Victor Stinner7f2fee32011-04-05 00:39:01 +0200380 }
381#endif
382 return 0;
Guido van Rossum0c156a52001-10-20 14:27:56 +0000383}