blob: 3d52bedc0e675291da79401977c2e6989c42fc74 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Tokenizer implementation */
3
Jack Jansen7b8c7542002-04-14 20:12:41 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005#include "pgenheaders.h"
6
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007#include <ctype.h>
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00008#include <assert.h>
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010#include "tokenizer.h"
11#include "errcode.h"
12
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +000013#ifndef PGEN
14#include "unicodeobject.h"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000015#include "bytesobject.h"
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +000016#include "fileobject.h"
17#include "codecs.h"
18#include "abstract.h"
19#endif /* PGEN */
20
Martin v. Löwis5b222132007-06-10 09:51:05 +000021#define is_potential_identifier_start(c) (\
22 (c >= 'a' && c <= 'z')\
23 || (c >= 'A' && c <= 'Z')\
Martin v. Löwis47383402007-08-15 07:32:56 +000024 || c == '_'\
25 || (c >= 128))
Martin v. Löwis5b222132007-06-10 09:51:05 +000026
27#define is_potential_identifier_char(c) (\
28 (c >= 'a' && c <= 'z')\
29 || (c >= 'A' && c <= 'Z')\
30 || (c >= '0' && c <= '9')\
Martin v. Löwis47383402007-08-15 07:32:56 +000031 || c == '_'\
32 || (c >= 128))
Martin v. Löwis5b222132007-06-10 09:51:05 +000033
Martin v. Löwis566f6af2002-10-26 14:39:10 +000034extern char *PyOS_Readline(FILE *, FILE *, char *);
Guido van Rossumf4b1a641994-08-29 12:43:07 +000035/* Return malloc'ed string including trailing \n;
36 empty malloc'ed string for EOF;
37 NULL if interrupted */
38
Guido van Rossum4fe87291992-02-26 15:24:44 +000039/* Don't ever change this -- it would break the portability of Python code */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040#define TABSIZE 8
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Guido van Rossum3f5da241990-12-20 15:06:42 +000042/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000043static struct tok_state *tok_new(void);
44static int tok_nextc(struct tok_state *tok);
45static void tok_backup(struct tok_state *tok, int c);
Guido van Rossum3f5da241990-12-20 15:06:42 +000046
Brett Cannond5ec98c2007-10-20 02:54:14 +000047
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048/* Token names */
49
Guido van Rossum86bea461997-04-29 21:03:06 +000050char *_PyParser_TokenNames[] = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 "ENDMARKER",
52 "NAME",
53 "NUMBER",
54 "STRING",
55 "NEWLINE",
56 "INDENT",
57 "DEDENT",
58 "LPAR",
59 "RPAR",
60 "LSQB",
61 "RSQB",
62 "COLON",
63 "COMMA",
64 "SEMI",
65 "PLUS",
66 "MINUS",
67 "STAR",
68 "SLASH",
69 "VBAR",
70 "AMPER",
71 "LESS",
72 "GREATER",
73 "EQUAL",
74 "DOT",
75 "PERCENT",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 "LBRACE",
77 "RBRACE",
Guido van Rossumfbab9051991-10-20 20:25:03 +000078 "EQEQUAL",
79 "NOTEQUAL",
80 "LESSEQUAL",
81 "GREATEREQUAL",
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +000082 "TILDE",
83 "CIRCUMFLEX",
84 "LEFTSHIFT",
85 "RIGHTSHIFT",
Guido van Rossumf595fde1996-01-12 01:31:58 +000086 "DOUBLESTAR",
Thomas Wouters434d0822000-08-24 20:11:32 +000087 "PLUSEQUAL",
88 "MINEQUAL",
89 "STAREQUAL",
90 "SLASHEQUAL",
91 "PERCENTEQUAL",
92 "AMPEREQUAL",
93 "VBAREQUAL",
94 "CIRCUMFLEXEQUAL",
95 "LEFTSHIFTEQUAL",
96 "RIGHTSHIFTEQUAL",
97 "DOUBLESTAREQUAL",
Guido van Rossum4668b002001-08-08 05:00:18 +000098 "DOUBLESLASH",
99 "DOUBLESLASHEQUAL",
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000100 "AT",
Neal Norwitzc1505362006-12-28 06:47:50 +0000101 "RARROW",
Georg Brandldde00282007-03-18 19:01:53 +0000102 "ELLIPSIS",
Guido van Rossumfbab9051991-10-20 20:25:03 +0000103 /* This table must match the #defines in token.h! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104 "OP",
105 "<ERRORTOKEN>",
106 "<N_TOKENS>"
107};
108
109
110/* Create and initialize a new tok_state structure */
111
112static struct tok_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000113tok_new(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 struct tok_state *tok = (struct tok_state *)PyMem_MALLOC(
116 sizeof(struct tok_state));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117 if (tok == NULL)
118 return NULL;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000119 tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 tok->done = E_OK;
121 tok->fp = NULL;
122 tok->tabsize = TABSIZE;
123 tok->indent = 0;
124 tok->indstack[0] = 0;
125 tok->atbol = 1;
126 tok->pendin = 0;
127 tok->prompt = tok->nextprompt = NULL;
128 tok->lineno = 0;
Guido van Rossuma849b831993-05-12 11:35:44 +0000129 tok->level = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +0000130 tok->filename = NULL;
Thomas Wouters6caa07b2006-04-14 11:33:28 +0000131 tok->altwarning = 1;
132 tok->alterror = 1;
Guido van Rossum926f13a1998-04-09 21:38:06 +0000133 tok->alttabsize = 1;
134 tok->altindstack[0] = 0;
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000135 tok->decoding_state = STATE_INIT;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000136 tok->decoding_erred = 0;
137 tok->read_coding_spec = 0;
Brett Cannonda780432008-10-17 03:38:50 +0000138 tok->enc = NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000139 tok->encoding = NULL;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000140 tok->cont_line = 0;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000141#ifndef PGEN
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000142 tok->decoding_readline = NULL;
143 tok->decoding_buffer = NULL;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000144#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145 return tok;
146}
147
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000148#ifdef PGEN
149
150static char *
151decoding_fgets(char *s, int size, struct tok_state *tok)
152{
153 return fgets(s, size, tok->fp);
154}
155
156static int
157decoding_feof(struct tok_state *tok)
158{
159 return feof(tok->fp);
160}
161
162static const char *
163decode_str(const char *str, struct tok_state *tok)
164{
165 return str;
166}
167
168#else /* PGEN */
169
170static char *
171error_ret(struct tok_state *tok) /* XXX */
172{
173 tok->decoding_erred = 1;
174 if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175 PyMem_FREE(tok->buf);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000176 tok->buf = NULL;
177 return NULL; /* as if it were EOF */
178}
179
180static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000181new_string(const char *s, Py_ssize_t len)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000182{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183 char* result = (char *)PyMem_MALLOC(len + 1);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000184 if (result != NULL) {
185 memcpy(result, s, len);
186 result[len] = '\0';
187 }
188 return result;
189}
190
191static char *
192get_normal_name(char *s) /* for utf-8 and latin-1 */
193{
194 char buf[13];
195 int i;
196 for (i = 0; i < 12; i++) {
197 int c = s[i];
198 if (c == '\0') break;
199 else if (c == '_') buf[i] = '-';
200 else buf[i] = tolower(c);
201 }
202 buf[i] = '\0';
203 if (strcmp(buf, "utf-8") == 0 ||
204 strncmp(buf, "utf-8-", 6) == 0) return "utf-8";
205 else if (strcmp(buf, "latin-1") == 0 ||
206 strcmp(buf, "iso-8859-1") == 0 ||
207 strcmp(buf, "iso-latin-1") == 0 ||
208 strncmp(buf, "latin-1-", 8) == 0 ||
209 strncmp(buf, "iso-8859-1-", 11) == 0 ||
210 strncmp(buf, "iso-latin-1-", 12) == 0) return "iso-8859-1";
211 else return s;
212}
213
214/* Return the coding spec in S, or NULL if none is found. */
215
216static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000217get_coding_spec(const char *s, Py_ssize_t size)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000218{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000219 Py_ssize_t i;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000220 /* Coding spec must be in a comment, and that comment must be
221 * the only statement on the source code line. */
222 for (i = 0; i < size - 6; i++) {
223 if (s[i] == '#')
224 break;
225 if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014')
226 return NULL;
227 }
228 for (; i < size - 6; i++) { /* XXX inefficient search */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000229 const char* t = s + i;
230 if (strncmp(t, "coding", 6) == 0) {
231 const char* begin = NULL;
232 t += 6;
233 if (t[0] != ':' && t[0] != '=')
234 continue;
235 do {
236 t++;
237 } while (t[0] == '\x20' || t[0] == '\t');
238
239 begin = t;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000240 while (isalnum(Py_CHARMASK(t[0])) ||
Neal Norwitze08e1bc2002-11-02 20:43:25 +0000241 t[0] == '-' || t[0] == '_' || t[0] == '.')
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000242 t++;
243
244 if (begin < t) {
245 char* r = new_string(begin, t - begin);
246 char* q = get_normal_name(r);
247 if (r != q) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248 PyMem_FREE(r);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000249 r = new_string(q, strlen(q));
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000250 }
251 return r;
252 }
253 }
254 }
255 return NULL;
256}
257
258/* Check whether the line contains a coding spec. If it does,
259 invoke the set_readline function for the new encoding.
260 This function receives the tok_state and the new encoding.
261 Return 1 on success, 0 on failure. */
262
263static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000264check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000265 int set_readline(struct tok_state *, const char *))
266{
Tim Peters17db21f2002-09-03 15:39:58 +0000267 char * cs;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000268 int r = 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000269
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000270 if (tok->cont_line)
271 /* It's a continuation line, so it can't be a coding spec. */
272 return 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000273 cs = get_coding_spec(line, size);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000274 if (cs != NULL) {
275 tok->read_coding_spec = 1;
276 if (tok->encoding == NULL) {
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000277 assert(tok->decoding_state == STATE_RAW);
Brett Cannonda780432008-10-17 03:38:50 +0000278 if (strcmp(cs, "utf-8") == 0) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000279 tok->encoding = cs;
280 } else {
281 r = set_readline(tok, cs);
282 if (r) {
283 tok->encoding = cs;
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000284 tok->decoding_state = STATE_NORMAL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000285 }
Neal Norwitzc0d5faa2005-10-21 06:05:33 +0000286 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 PyMem_FREE(cs);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000288 }
289 } else { /* then, compare cs with BOM */
290 r = (strcmp(tok->encoding, cs) == 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000291 PyMem_FREE(cs);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000292 }
293 }
Neal Norwitzdb83eb32005-12-18 05:29:30 +0000294 if (!r) {
295 cs = tok->encoding;
296 if (!cs)
297 cs = "with BOM";
298 PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
299 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000300 return r;
301}
302
303/* See whether the file starts with a BOM. If it does,
304 invoke the set_readline function with the new encoding.
305 Return 1 on success, 0 on failure. */
306
307static int
308check_bom(int get_char(struct tok_state *),
309 void unget_char(int, struct tok_state *),
310 int set_readline(struct tok_state *, const char *),
311 struct tok_state *tok)
312{
313 int ch = get_char(tok);
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000314 tok->decoding_state = STATE_RAW;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000315 if (ch == EOF) {
316 return 1;
317 } else if (ch == 0xEF) {
Amaury Forgeot d'Arcaf593462007-11-22 20:53:01 +0000318 ch = get_char(tok);
319 if (ch != 0xBB) {
320 unget_char(ch, tok);
321 unget_char(0xEF, tok);
322 /* any token beginning with '\xEF' is a bad token */
323 return 1;
324 }
325 ch = get_char(tok);
326 if (ch != 0xBF) {
327 unget_char(ch, tok);
328 unget_char(0xBB, tok);
329 unget_char(0xEF, tok);
330 /* any token beginning with '\xEF' is a bad token */
331 return 1;
332 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000333#if 0
334 /* Disable support for UTF-16 BOMs until a decision
335 is made whether this needs to be supported. */
336 } else if (ch == 0xFE) {
337 ch = get_char(tok); if (ch != 0xFF) goto NON_BOM;
338 if (!set_readline(tok, "utf-16-be")) return 0;
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000339 tok->decoding_state = STATE_NORMAL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000340 } else if (ch == 0xFF) {
341 ch = get_char(tok); if (ch != 0xFE) goto NON_BOM;
342 if (!set_readline(tok, "utf-16-le")) return 0;
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000343 tok->decoding_state = STATE_NORMAL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000344#endif
345 } else {
346 unget_char(ch, tok);
347 return 1;
348 }
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000349 if (tok->encoding != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000350 PyMem_FREE(tok->encoding);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000351 tok->encoding = new_string("utf-8", 5); /* resulting is in utf-8 */
Amaury Forgeot d'Arcaf593462007-11-22 20:53:01 +0000352 /* No need to set_readline: input is already utf-8 */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000353 return 1;
354}
355
356/* Read a line of text from TOK into S, using the stream in TOK.
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000357 Return NULL on failure, else S.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000359 On entry, tok->decoding_buffer will be one of:
360 1) NULL: need to call tok->decoding_readline to get a new line
361 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
362 stored the result in tok->decoding_buffer
Christian Heimes9c4756e2008-05-26 13:22:05 +0000363 3) PyByteArrayObject *: previous call to fp_readl did not have enough room
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000364 (in the s buffer) to copy entire contents of the line read
365 by tok->decoding_readline. tok->decoding_buffer has the overflow.
366 In this case, fp_readl is called in a loop (with an expanded buffer)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367 until the buffer ends with a '\n' (or until the end of the file is
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000368 reached): see tok_nextc and its calls to decoding_fgets.
369*/
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000370
371static char *
372fp_readl(char *s, int size, struct tok_state *tok)
373{
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000374 PyObject* bufobj;
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000375 const char *buf;
376 Py_ssize_t buflen;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000377
378 /* Ask for one less byte so we can terminate it */
379 assert(size > 0);
380 size--;
381
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000382 if (tok->decoding_buffer) {
383 bufobj = tok->decoding_buffer;
384 Py_INCREF(bufobj);
385 }
386 else
387 {
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000388 bufobj = PyObject_CallObject(tok->decoding_readline, NULL);
389 if (bufobj == NULL)
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000390 goto error;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000391 }
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000392 if (PyUnicode_CheckExact(bufobj))
393 {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000394 buf = _PyUnicode_AsStringAndSize(bufobj, &buflen);
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000395 if (buf == NULL) {
396 goto error;
397 }
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000398 }
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000399 else
400 {
Christian Heimes9c4756e2008-05-26 13:22:05 +0000401 buf = PyByteArray_AsString(bufobj);
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000402 if (buf == NULL) {
403 goto error;
404 }
Christian Heimes9c4756e2008-05-26 13:22:05 +0000405 buflen = PyByteArray_GET_SIZE(bufobj);
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000406 }
407
408 Py_XDECREF(tok->decoding_buffer);
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000409 if (buflen > size) {
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000410 /* Too many chars, the rest goes into tok->decoding_buffer */
Christian Heimes9c4756e2008-05-26 13:22:05 +0000411 tok->decoding_buffer = PyByteArray_FromStringAndSize(buf+size,
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000412 buflen-size);
413 if (tok->decoding_buffer == NULL)
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000414 goto error;
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000415 buflen = size;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000416 }
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000417 else
418 tok->decoding_buffer = NULL;
419
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000420 memcpy(s, buf, buflen);
421 s[buflen] = '\0';
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000422 if (buflen == 0) /* EOF */
423 s = NULL;
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000424 Py_DECREF(bufobj);
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000425 return s;
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000426
427error:
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000428 Py_XDECREF(bufobj);
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000429 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000430}
431
432/* Set the readline function for TOK to a StreamReader's
433 readline function. The StreamReader is named ENC.
434
435 This function is called from check_bom and check_coding_spec.
436
437 ENC is usually identical to the future value of tok->encoding,
438 except for the (currently unsupported) case of UTF-16.
439
440 Return 1 on success, 0 on failure. */
441
442static int
443fp_setreadl(struct tok_state *tok, const char* enc)
444{
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000445 PyObject *readline = NULL, *stream = NULL, *io = NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000446
Christian Heimes819b8bf2008-01-03 23:05:47 +0000447 io = PyImport_ImportModuleNoBlock("io");
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000448 if (io == NULL)
449 goto cleanup;
450
Brett Cannon8a9583e2008-09-04 05:04:25 +0000451 if (tok->filename)
452 stream = PyObject_CallMethod(io, "open", "ssis",
453 tok->filename, "r", -1, enc);
454 else
Kristján Valur Jónsson19288c22008-12-18 17:15:54 +0000455 stream = PyObject_CallMethod(io, "open", "isisOOO",
456 fileno(tok->fp), "r", -1, enc, Py_None, Py_None, Py_False);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000457 if (stream == NULL)
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000458 goto cleanup;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000459
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000460 Py_XDECREF(tok->decoding_readline);
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000461 readline = PyObject_GetAttrString(stream, "readline");
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000462 tok->decoding_readline = readline;
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000463
Amaury Forgeot d'Arccf8016a2008-10-09 23:37:48 +0000464 /* The file has been reopened; parsing will restart from
465 * the beginning of the file, we have to reset the line number.
466 * But this function has been called from inside tok_nextc() which
467 * will increment lineno before it returns. So we set it -1 so that
468 * the next call to tok_nextc() will start with tok->lineno == 0.
469 */
470 tok->lineno = -1;
471
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000472 cleanup:
473 Py_XDECREF(stream);
474 Py_XDECREF(io);
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000475 return readline != NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000476}
477
478/* Fetch the next byte from TOK. */
479
480static int fp_getc(struct tok_state *tok) {
481 return getc(tok->fp);
482}
483
484/* Unfetch the last byte back into TOK. */
485
486static void fp_ungetc(int c, struct tok_state *tok) {
487 ungetc(c, tok->fp);
488}
489
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000490/* Check whether the characters at s start a valid
491 UTF-8 sequence. Return the number of characters forming
492 the sequence if yes, 0 if not. */
493static int valid_utf8(const unsigned char* s)
494{
495 int expected = 0;
496 int length;
497 if (*s < 0x80)
498 /* single-byte code */
499 return 1;
500 if (*s < 0xc0)
501 /* following byte */
502 return 0;
503 if (*s < 0xE0)
504 expected = 1;
505 else if (*s < 0xF0)
506 expected = 2;
507 else if (*s < 0xF8)
508 expected = 3;
509 else
510 return 0;
511 length = expected + 1;
512 for (; expected; expected--)
513 if (s[expected] < 0x80 || s[expected] >= 0xC0)
514 return 0;
515 return length;
516}
517
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000518/* Read a line of input from TOK. Determine encoding
519 if necessary. */
520
521static char *
522decoding_fgets(char *s, int size, struct tok_state *tok)
523{
Martin v. Löwis2863c102002-08-07 15:18:57 +0000524 char *line = NULL;
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000525 int badchar = 0;
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000526 for (;;) {
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000527 if (tok->decoding_state == STATE_NORMAL) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000528 /* We already have a codec associated with
529 this input. */
530 line = fp_readl(s, size, tok);
531 break;
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000532 } else if (tok->decoding_state == STATE_RAW) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000533 /* We want a 'raw' read. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000534 line = Py_UniversalNewlineFgets(s, size,
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000535 tok->fp, NULL);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000536 break;
537 } else {
538 /* We have not yet determined the encoding.
539 If an encoding is found, use the file-pointer
540 reader functions from now on. */
541 if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok))
542 return error_ret(tok);
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000543 assert(tok->decoding_state != STATE_INIT);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000544 }
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000545 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000546 if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) {
547 if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) {
548 return error_ret(tok);
549 }
550 }
551#ifndef PGEN
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000552 /* The default encoding is UTF-8, so make sure we don't have any
553 non-UTF-8 sequences in it. */
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000554 if (line && !tok->encoding) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000555 unsigned char *c;
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000556 int length;
557 for (c = (unsigned char *)line; *c; c += length)
558 if (!(length = valid_utf8(c))) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000559 badchar = *c;
560 break;
561 }
562 }
563 if (badchar) {
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000564 char buf[500];
Martin v. Löwis725bb232002-08-05 01:49:16 +0000565 /* Need to add 1 to the line number, since this line
566 has not been counted, yet. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567 sprintf(buf,
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000568 "Non-UTF-8 code starting with '\\x%.2x' "
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000569 "in file %.200s on line %i, "
570 "but no encoding declared; "
Guido van Rossum21b731f2007-08-30 00:10:46 +0000571 "see http://python.org/dev/peps/pep-0263/ for details",
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000572 badchar, tok->filename, tok->lineno + 1);
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000573 PyErr_SetString(PyExc_SyntaxError, buf);
574 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000575 }
576#endif
577 return line;
578}
579
580static int
581decoding_feof(struct tok_state *tok)
582{
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000583 if (tok->decoding_state != STATE_NORMAL) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000584 return feof(tok->fp);
585 } else {
586 PyObject* buf = tok->decoding_buffer;
587 if (buf == NULL) {
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000588 buf = PyObject_CallObject(tok->decoding_readline, NULL);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000589 if (buf == NULL) {
590 error_ret(tok);
591 return 1;
592 } else {
593 tok->decoding_buffer = buf;
594 }
595 }
596 return PyObject_Length(buf) == 0;
597 }
598}
599
600/* Fetch a byte from TOK, using the string buffer. */
601
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602static int
603buf_getc(struct tok_state *tok) {
Just van Rossumf032f862003-02-09 20:38:48 +0000604 return Py_CHARMASK(*tok->str++);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000605}
606
607/* Unfetch a byte from TOK, using the string buffer. */
608
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000609static void
610buf_ungetc(int c, struct tok_state *tok) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000611 tok->str--;
Just van Rossumf032f862003-02-09 20:38:48 +0000612 assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000613}
614
615/* Set the readline function for TOK to ENC. For the string-based
616 tokenizer, this means to just record the encoding. */
617
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000618static int
619buf_setreadl(struct tok_state *tok, const char* enc) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000620 tok->enc = enc;
621 return 1;
622}
623
624/* Return a UTF-8 encoding Python string object from the
625 C byte string STR, which is encoded with ENC. */
626
627static PyObject *
628translate_into_utf8(const char* str, const char* enc) {
629 PyObject *utf8;
630 PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL);
631 if (buf == NULL)
632 return NULL;
633 utf8 = PyUnicode_AsUTF8String(buf);
634 Py_DECREF(buf);
635 return utf8;
636}
637
638/* Decode a byte string STR for use as the buffer of TOK.
639 Look for encoding declarations inside STR, and record them
640 inside TOK. */
641
642static const char *
643decode_str(const char *str, struct tok_state *tok)
644{
645 PyObject* utf8 = NULL;
646 const char *s;
Christian Heimes1af737c2008-01-23 08:24:23 +0000647 const char *newl[2] = {NULL, NULL};
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000648 int lineno = 0;
649 tok->enc = NULL;
650 tok->str = str;
651 if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000652 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000653 str = tok->str; /* string after BOM if any */
Tim Peters2c3f9c62002-08-04 17:58:34 +0000654 assert(str);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000655 if (tok->enc != NULL) {
656 utf8 = translate_into_utf8(str, tok->enc);
657 if (utf8 == NULL)
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000658 return error_ret(tok);
Christian Heimes72b710a2008-05-26 13:28:38 +0000659 str = PyBytes_AsString(utf8);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000660 }
661 for (s = str;; s++) {
662 if (*s == '\0') break;
663 else if (*s == '\n') {
Christian Heimes412dc9c2008-01-27 18:55:54 +0000664 assert(lineno < 2);
Georg Brandl86def6c2008-01-21 20:36:10 +0000665 newl[lineno] = s;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000666 lineno++;
667 if (lineno == 2) break;
668 }
669 }
670 tok->enc = NULL;
Georg Brandl86def6c2008-01-21 20:36:10 +0000671 /* need to check line 1 and 2 separately since check_coding_spec
672 assumes a single line as input */
673 if (newl[0]) {
674 if (!check_coding_spec(str, newl[0] - str, tok, buf_setreadl))
675 return error_ret(tok);
676 if (tok->enc == NULL && newl[1]) {
677 if (!check_coding_spec(newl[0]+1, newl[1] - newl[0],
678 tok, buf_setreadl))
679 return error_ret(tok);
680 }
681 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000682 if (tok->enc != NULL) {
683 assert(utf8 == NULL);
684 utf8 = translate_into_utf8(str, tok->enc);
Neal Norwitz40d37812005-10-02 01:48:49 +0000685 if (utf8 == NULL) {
686 PyErr_Format(PyExc_SyntaxError,
687 "unknown encoding: %s", tok->enc);
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000688 return error_ret(tok);
Neal Norwitz40d37812005-10-02 01:48:49 +0000689 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000690 str = PyBytes_AS_STRING(utf8);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000691 }
692 assert(tok->decoding_buffer == NULL);
693 tok->decoding_buffer = utf8; /* CAUTION */
694 return str;
695}
696
697#endif /* PGEN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000698
699/* Set up tokenizer for string */
700
701struct tok_state *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000702PyTokenizer_FromString(const char *str)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703{
704 struct tok_state *tok = tok_new();
705 if (tok == NULL)
706 return NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000707 str = (char *)decode_str(str, tok);
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000708 if (str == NULL) {
709 PyTokenizer_Free(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000710 return NULL;
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000711 }
712
Martin v. Löwis95292d62002-12-11 14:04:59 +0000713 /* XXX: constify members. */
714 tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000715 return tok;
716}
717
718
Guido van Rossum8c11a5c1991-07-27 21:42:56 +0000719/* Set up tokenizer for file */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000720
721struct tok_state *
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000722PyTokenizer_FromFile(FILE *fp, char* enc, char *ps1, char *ps2)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000723{
724 struct tok_state *tok = tok_new();
725 if (tok == NULL)
726 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000727 if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) {
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000728 PyTokenizer_Free(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000729 return NULL;
730 }
731 tok->cur = tok->inp = tok->buf;
732 tok->end = tok->buf + BUFSIZ;
733 tok->fp = fp;
734 tok->prompt = ps1;
735 tok->nextprompt = ps2;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000736 if (enc != NULL) {
737 /* Must copy encoding declaration since it
738 gets copied into the parse tree. */
739 tok->encoding = PyMem_MALLOC(strlen(enc)+1);
740 if (!tok->encoding) {
741 PyTokenizer_Free(tok);
742 return NULL;
743 }
744 strcpy(tok->encoding, enc);
Neil Schemenauer3f993c32007-09-21 20:50:26 +0000745 tok->decoding_state = STATE_NORMAL;
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000746 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000747 return tok;
748}
749
750
751/* Free a tok_state structure */
752
753void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000754PyTokenizer_Free(struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000755{
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000756 if (tok->encoding != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757 PyMem_FREE(tok->encoding);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000758#ifndef PGEN
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000759 Py_XDECREF(tok->decoding_readline);
760 Py_XDECREF(tok->decoding_buffer);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000761#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000762 if (tok->fp != NULL && tok->buf != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000763 PyMem_FREE(tok->buf);
764 PyMem_FREE(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765}
766
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000767/* Get next char, updating state; error code goes into tok->done */
768
769static int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000770tok_nextc(register struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772 for (;;) {
Guido van Rossum1a817c01994-09-19 08:06:25 +0000773 if (tok->cur != tok->inp) {
Guido van Rossumcf57d8b1998-01-19 22:07:46 +0000774 return Py_CHARMASK(*tok->cur++); /* Fast path */
Guido van Rossum1a817c01994-09-19 08:06:25 +0000775 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000776 if (tok->done != E_OK)
777 return EOF;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000778 if (tok->fp == NULL) {
Guido van Rossum1a817c01994-09-19 08:06:25 +0000779 char *end = strchr(tok->inp, '\n');
780 if (end != NULL)
781 end++;
782 else {
783 end = strchr(tok->inp, '\0');
784 if (end == tok->inp) {
785 tok->done = E_EOF;
786 return EOF;
787 }
788 }
789 if (tok->start == NULL)
790 tok->buf = tok->cur;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000791 tok->line_start = tok->cur;
Guido van Rossum1a817c01994-09-19 08:06:25 +0000792 tok->lineno++;
793 tok->inp = end;
Guido van Rossumcf57d8b1998-01-19 22:07:46 +0000794 return Py_CHARMASK(*tok->cur++);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000796 if (tok->prompt != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000797 char *newtok = PyOS_Readline(stdin, stdout, tok->prompt);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000798#ifndef PGEN
799 if (tok->encoding && newtok && *newtok) {
800 /* Recode to UTF-8 */
801 Py_ssize_t buflen;
802 const char* buf;
803 PyObject *u = translate_into_utf8(newtok, tok->encoding);
804 PyMem_FREE(newtok);
805 if (!u) {
806 tok->done = E_DECODE;
807 return EOF;
808 }
Christian Heimes72b710a2008-05-26 13:28:38 +0000809 buflen = PyBytes_GET_SIZE(u);
810 buf = PyBytes_AS_STRING(u);
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000811 if (!buf) {
812 Py_DECREF(u);
813 tok->done = E_DECODE;
814 return EOF;
815 }
816 newtok = PyMem_MALLOC(buflen+1);
817 strcpy(newtok, buf);
818 Py_DECREF(u);
819 }
820#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000821 if (tok->nextprompt != NULL)
822 tok->prompt = tok->nextprompt;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000823 if (newtok == NULL)
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000824 tok->done = E_INTR;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000825 else if (*newtok == '\0') {
826 PyMem_FREE(newtok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000827 tok->done = E_EOF;
828 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000829 else if (tok->start != NULL) {
Guido van Rossum6da34342000-06-28 22:00:02 +0000830 size_t start = tok->start - tok->buf;
831 size_t oldlen = tok->cur - tok->buf;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000832 size_t newlen = oldlen + strlen(newtok);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000833 char *buf = tok->buf;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000834 buf = (char *)PyMem_REALLOC(buf, newlen+1);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000835 tok->lineno++;
836 if (buf == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837 PyMem_FREE(tok->buf);
Guido van Rossum588633d1994-12-30 15:46:02 +0000838 tok->buf = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000839 PyMem_FREE(newtok);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000840 tok->done = E_NOMEM;
841 return EOF;
842 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000843 tok->buf = buf;
844 tok->cur = tok->buf + oldlen;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000845 tok->line_start = tok->cur;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846 strcpy(tok->buf + oldlen, newtok);
847 PyMem_FREE(newtok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000848 tok->inp = tok->buf + newlen;
849 tok->end = tok->inp + 1;
850 tok->start = tok->buf + start;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000851 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000852 else {
853 tok->lineno++;
854 if (tok->buf != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000855 PyMem_FREE(tok->buf);
856 tok->buf = newtok;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000857 tok->line_start = tok->buf;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000858 tok->cur = tok->buf;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000859 tok->line_start = tok->buf;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000860 tok->inp = strchr(tok->buf, '\0');
861 tok->end = tok->inp + 1;
862 }
863 }
864 else {
865 int done = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000866 Py_ssize_t cur = 0;
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000867 char *pt;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000868 if (tok->start == NULL) {
869 if (tok->buf == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000870 tok->buf = (char *)
871 PyMem_MALLOC(BUFSIZ);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000872 if (tok->buf == NULL) {
873 tok->done = E_NOMEM;
874 return EOF;
875 }
876 tok->end = tok->buf + BUFSIZ;
877 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000878 if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
879 tok) == NULL) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000880 tok->done = E_EOF;
881 done = 1;
882 }
883 else {
884 tok->done = E_OK;
885 tok->inp = strchr(tok->buf, '\0');
886 done = tok->inp[-1] == '\n';
887 }
888 }
889 else {
890 cur = tok->cur - tok->buf;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000891 if (decoding_feof(tok)) {
Guido van Rossum78c05351995-01-17 16:12:13 +0000892 tok->done = E_EOF;
893 done = 1;
894 }
895 else
896 tok->done = E_OK;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000897 }
898 tok->lineno++;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000899 /* Read until '\n' or EOF */
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000900 while (!done) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000901 Py_ssize_t curstart = tok->start == NULL ? -1 :
902 tok->start - tok->buf;
903 Py_ssize_t curvalid = tok->inp - tok->buf;
904 Py_ssize_t newsize = curvalid + BUFSIZ;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000905 char *newbuf = tok->buf;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000906 newbuf = (char *)PyMem_REALLOC(newbuf,
907 newsize);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000908 if (newbuf == NULL) {
909 tok->done = E_NOMEM;
910 tok->cur = tok->inp;
911 return EOF;
912 }
913 tok->buf = newbuf;
914 tok->inp = tok->buf + curvalid;
915 tok->end = tok->buf + newsize;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000916 tok->start = curstart < 0 ? NULL :
917 tok->buf + curstart;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000918 if (decoding_fgets(tok->inp,
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000919 (int)(tok->end - tok->inp),
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000920 tok) == NULL) {
Thomas Wouters7eaf2aa2006-03-02 20:41:27 +0000921 /* Break out early on decoding
922 errors, as tok->buf will be NULL
923 */
924 if (tok->decoding_erred)
925 return EOF;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000926 /* Last line does not end in \n,
927 fake one */
928 strcpy(tok->inp, "\n");
929 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000930 tok->inp = strchr(tok->inp, '\0');
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000931 done = tok->inp[-1] == '\n';
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000932 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000933 if (tok->buf != NULL) {
934 tok->cur = tok->buf + cur;
935 tok->line_start = tok->cur;
936 /* replace "\r\n" with "\n" */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000937 /* For Mac leave the \r, giving a syntax error */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000938 pt = tok->inp - 2;
939 if (pt >= tok->buf && *pt == '\r') {
940 *pt++ = '\n';
941 *pt = '\0';
942 tok->inp = pt;
943 }
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000944 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000945 }
946 if (tok->done != E_OK) {
947 if (tok->prompt != NULL)
Guido van Rossum6e73bf41998-08-25 18:13:04 +0000948 PySys_WriteStderr("\n");
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000949 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000950 return EOF;
951 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000952 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000953 /*NOTREACHED*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000954}
955
956
957/* Back-up one character */
958
959static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000960tok_backup(register struct tok_state *tok, register int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000961{
962 if (c != EOF) {
Guido van Rossum588633d1994-12-30 15:46:02 +0000963 if (--tok->cur < tok->buf)
Guido van Rossum86bea461997-04-29 21:03:06 +0000964 Py_FatalError("tok_backup: begin of buffer");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965 if (*tok->cur != c)
966 *tok->cur = c;
967 }
968}
969
970
971/* Return the token corresponding to a single character */
972
973int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000974PyToken_OneChar(int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000975{
976 switch (c) {
977 case '(': return LPAR;
978 case ')': return RPAR;
979 case '[': return LSQB;
980 case ']': return RSQB;
981 case ':': return COLON;
982 case ',': return COMMA;
983 case ';': return SEMI;
984 case '+': return PLUS;
985 case '-': return MINUS;
986 case '*': return STAR;
987 case '/': return SLASH;
988 case '|': return VBAR;
989 case '&': return AMPER;
990 case '<': return LESS;
991 case '>': return GREATER;
992 case '=': return EQUAL;
993 case '.': return DOT;
994 case '%': return PERCENT;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000995 case '{': return LBRACE;
996 case '}': return RBRACE;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000997 case '^': return CIRCUMFLEX;
998 case '~': return TILDE;
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000999 case '@': return AT;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001000 default: return OP;
1001 }
1002}
1003
1004
Guido van Rossumfbab9051991-10-20 20:25:03 +00001005int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001006PyToken_TwoChars(int c1, int c2)
Guido van Rossumfbab9051991-10-20 20:25:03 +00001007{
1008 switch (c1) {
1009 case '=':
1010 switch (c2) {
1011 case '=': return EQEQUAL;
1012 }
1013 break;
1014 case '!':
1015 switch (c2) {
1016 case '=': return NOTEQUAL;
1017 }
1018 break;
1019 case '<':
1020 switch (c2) {
Guido van Rossumfbab9051991-10-20 20:25:03 +00001021 case '=': return LESSEQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001022 case '<': return LEFTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001023 }
1024 break;
1025 case '>':
1026 switch (c2) {
1027 case '=': return GREATEREQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001028 case '>': return RIGHTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001029 }
1030 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001031 case '+':
1032 switch (c2) {
1033 case '=': return PLUSEQUAL;
1034 }
1035 break;
1036 case '-':
1037 switch (c2) {
1038 case '=': return MINEQUAL;
Neal Norwitzc1505362006-12-28 06:47:50 +00001039 case '>': return RARROW;
Thomas Wouters434d0822000-08-24 20:11:32 +00001040 }
1041 break;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001042 case '*':
1043 switch (c2) {
1044 case '*': return DOUBLESTAR;
Thomas Wouters434d0822000-08-24 20:11:32 +00001045 case '=': return STAREQUAL;
1046 }
1047 break;
1048 case '/':
1049 switch (c2) {
Guido van Rossum4668b002001-08-08 05:00:18 +00001050 case '/': return DOUBLESLASH;
Thomas Wouters434d0822000-08-24 20:11:32 +00001051 case '=': return SLASHEQUAL;
1052 }
1053 break;
1054 case '|':
1055 switch (c2) {
1056 case '=': return VBAREQUAL;
1057 }
1058 break;
1059 case '%':
1060 switch (c2) {
1061 case '=': return PERCENTEQUAL;
1062 }
1063 break;
1064 case '&':
1065 switch (c2) {
1066 case '=': return AMPEREQUAL;
1067 }
1068 break;
1069 case '^':
1070 switch (c2) {
1071 case '=': return CIRCUMFLEXEQUAL;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001072 }
1073 break;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001074 }
1075 return OP;
1076}
1077
Thomas Wouters434d0822000-08-24 20:11:32 +00001078int
1079PyToken_ThreeChars(int c1, int c2, int c3)
1080{
1081 switch (c1) {
1082 case '<':
1083 switch (c2) {
1084 case '<':
1085 switch (c3) {
1086 case '=':
1087 return LEFTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001088 }
1089 break;
1090 }
1091 break;
1092 case '>':
1093 switch (c2) {
1094 case '>':
1095 switch (c3) {
1096 case '=':
1097 return RIGHTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001098 }
1099 break;
1100 }
1101 break;
1102 case '*':
1103 switch (c2) {
1104 case '*':
1105 switch (c3) {
1106 case '=':
1107 return DOUBLESTAREQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001108 }
1109 break;
1110 }
1111 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001112 case '/':
1113 switch (c2) {
1114 case '/':
1115 switch (c3) {
1116 case '=':
1117 return DOUBLESLASHEQUAL;
1118 }
1119 break;
1120 }
1121 break;
Georg Brandldde00282007-03-18 19:01:53 +00001122 case '.':
1123 switch (c2) {
1124 case '.':
1125 switch (c3) {
1126 case '.':
1127 return ELLIPSIS;
1128 }
1129 break;
1130 }
1131 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001132 }
1133 return OP;
1134}
Guido van Rossumfbab9051991-10-20 20:25:03 +00001135
Guido van Rossum926f13a1998-04-09 21:38:06 +00001136static int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001137indenterror(struct tok_state *tok)
Guido van Rossum926f13a1998-04-09 21:38:06 +00001138{
1139 if (tok->alterror) {
Fred Drake85f36392000-07-11 17:53:00 +00001140 tok->done = E_TABSPACE;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001141 tok->cur = tok->inp;
1142 return 1;
1143 }
1144 if (tok->altwarning) {
Fred Drake85f36392000-07-11 17:53:00 +00001145 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
1146 "in indentation\n", tok->filename);
Guido van Rossum926f13a1998-04-09 21:38:06 +00001147 tok->altwarning = 0;
1148 }
1149 return 0;
1150}
1151
Martin v. Löwis47383402007-08-15 07:32:56 +00001152#ifdef PGEN
1153#define verify_identifier(s,e) 1
1154#else
1155/* Verify that the identifier follows PEP 3131. */
1156static int
1157verify_identifier(char *start, char *end)
1158{
Guido van Rossume3e37012007-08-29 18:54:41 +00001159 PyObject *s;
1160 int result;
1161 s = PyUnicode_DecodeUTF8(start, end-start, NULL);
1162 if (s == NULL) {
1163 PyErr_Clear();
1164 return 0;
1165 }
1166 result = PyUnicode_IsIdentifier(s);
Martin v. Löwis47383402007-08-15 07:32:56 +00001167 Py_DECREF(s);
1168 return result;
1169}
1170#endif
Guido van Rossum926f13a1998-04-09 21:38:06 +00001171
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172/* Get next token, after space stripping etc. */
1173
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001174static int
1175tok_get(register struct tok_state *tok, char **p_start, char **p_end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001176{
1177 register int c;
Martin v. Löwis47383402007-08-15 07:32:56 +00001178 int blankline, nonascii;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001179
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001180 *p_start = *p_end = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001181 nextline:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001182 tok->start = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001183 blankline = 0;
1184
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001185 /* Get indentation level */
1186 if (tok->atbol) {
1187 register int col = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001188 register int altcol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001189 tok->atbol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001190 for (;;) {
1191 c = tok_nextc(tok);
1192 if (c == ' ')
Guido van Rossum926f13a1998-04-09 21:38:06 +00001193 col++, altcol++;
1194 else if (c == '\t') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001195 col = (col/tok->tabsize + 1) * tok->tabsize;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001196 altcol = (altcol/tok->alttabsize + 1)
1197 * tok->alttabsize;
1198 }
Guido van Rossum94d32b11995-07-07 22:27:27 +00001199 else if (c == '\014') /* Control-L (formfeed) */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001200 col = altcol = 0; /* For Emacs users */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001201 else
1202 break;
1203 }
1204 tok_backup(tok, c);
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001205 if (c == '#' || c == '\n') {
1206 /* Lines with only whitespace and/or comments
1207 shouldn't affect the indentation and are
1208 not passed to the parser as NEWLINE tokens,
1209 except *totally* empty lines in interactive
1210 mode, which signal the end of a command group. */
1211 if (col == 0 && c == '\n' && tok->prompt != NULL)
1212 blankline = 0; /* Let it through */
1213 else
1214 blankline = 1; /* Ignore completely */
1215 /* We can't jump back right here since we still
1216 may need to skip to the end of a comment */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001217 }
Guido van Rossuma849b831993-05-12 11:35:44 +00001218 if (!blankline && tok->level == 0) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001219 if (col == tok->indstack[tok->indent]) {
1220 /* No change */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001221 if (altcol != tok->altindstack[tok->indent]) {
1222 if (indenterror(tok))
1223 return ERRORTOKEN;
1224 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001225 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001226 else if (col > tok->indstack[tok->indent]) {
1227 /* Indent -- always one */
1228 if (tok->indent+1 >= MAXINDENT) {
Fred Drake85f36392000-07-11 17:53:00 +00001229 tok->done = E_TOODEEP;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001230 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001231 return ERRORTOKEN;
1232 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001233 if (altcol <= tok->altindstack[tok->indent]) {
1234 if (indenterror(tok))
1235 return ERRORTOKEN;
1236 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001237 tok->pendin++;
1238 tok->indstack[++tok->indent] = col;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001239 tok->altindstack[tok->indent] = altcol;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001240 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001241 else /* col < tok->indstack[tok->indent] */ {
1242 /* Dedent -- any number, must be consistent */
1243 while (tok->indent > 0 &&
1244 col < tok->indstack[tok->indent]) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001245 tok->pendin--;
Guido van Rossum54758fa1998-02-16 22:18:00 +00001246 tok->indent--;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001247 }
1248 if (col != tok->indstack[tok->indent]) {
Fred Drake85f36392000-07-11 17:53:00 +00001249 tok->done = E_DEDENT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001250 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001251 return ERRORTOKEN;
1252 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001253 if (altcol != tok->altindstack[tok->indent]) {
1254 if (indenterror(tok))
1255 return ERRORTOKEN;
1256 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001257 }
1258 }
1259 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001260
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001261 tok->start = tok->cur;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001262
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263 /* Return pending indents/dedents */
1264 if (tok->pendin != 0) {
1265 if (tok->pendin < 0) {
1266 tok->pendin++;
1267 return DEDENT;
1268 }
1269 else {
1270 tok->pendin--;
1271 return INDENT;
1272 }
1273 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001274
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001275 again:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001276 tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001277 /* Skip spaces */
1278 do {
1279 c = tok_nextc(tok);
Guido van Rossum94d32b11995-07-07 22:27:27 +00001280 } while (c == ' ' || c == '\t' || c == '\014');
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001281
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001282 /* Set start of current token */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001283 tok->start = tok->cur - 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284
Thomas Wouters6caa07b2006-04-14 11:33:28 +00001285 /* Skip comment */
1286 if (c == '#')
Guido van Rossumab5ca152000-03-31 00:52:27 +00001287 while (c != EOF && c != '\n')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001288 c = tok_nextc(tok);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001289
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001290 /* Check for EOF and errors now */
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001291 if (c == EOF) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001292 return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001293 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001294
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001295 /* Identifier (most frequent token!) */
Martin v. Löwis47383402007-08-15 07:32:56 +00001296 nonascii = 0;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001297 if (is_potential_identifier_start(c)) {
Guido van Rossumcf171a72007-11-16 00:51:45 +00001298 /* Process b"", r"" and br"" */
1299 if (c == 'b' || c == 'B') {
Guido van Rossum5026cb41997-04-25 17:32:00 +00001300 c = tok_nextc(tok);
1301 if (c == '"' || c == '\'')
1302 goto letter_quote;
1303 }
Guido van Rossumcf171a72007-11-16 00:51:45 +00001304 if (c == 'r' || c == 'R') {
1305 c = tok_nextc(tok);
1306 if (c == '"' || c == '\'')
1307 goto letter_quote;
1308 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001309 while (is_potential_identifier_char(c)) {
Martin v. Löwis47383402007-08-15 07:32:56 +00001310 if (c >= 128)
1311 nonascii = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001312 c = tok_nextc(tok);
Guido van Rossum24dacb31997-04-06 03:46:20 +00001313 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001314 tok_backup(tok, c);
Guido van Rossumcf171a72007-11-16 00:51:45 +00001315 if (nonascii &&
Martin v. Löwis47383402007-08-15 07:32:56 +00001316 !verify_identifier(tok->start, tok->cur)) {
1317 tok->done = E_IDENTIFIER;
1318 return ERRORTOKEN;
1319 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001320 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001321 *p_end = tok->cur;
1322 return NAME;
1323 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001324
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001325 /* Newline */
1326 if (c == '\n') {
1327 tok->atbol = 1;
Guido van Rossuma849b831993-05-12 11:35:44 +00001328 if (blankline || tok->level > 0)
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001329 goto nextline;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001330 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001331 *p_end = tok->cur - 1; /* Leave '\n' out of the string */
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001332 tok->cont_line = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001333 return NEWLINE;
1334 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001335
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001336 /* Period or number starting with period? */
1337 if (c == '.') {
1338 c = tok_nextc(tok);
1339 if (isdigit(c)) {
1340 goto fraction;
Georg Brandldde00282007-03-18 19:01:53 +00001341 } else if (c == '.') {
1342 c = tok_nextc(tok);
1343 if (c == '.') {
1344 *p_start = tok->start;
Guido van Rossumcf171a72007-11-16 00:51:45 +00001345 *p_end = tok->cur;
Georg Brandldde00282007-03-18 19:01:53 +00001346 return ELLIPSIS;
1347 } else {
1348 tok_backup(tok, c);
1349 }
1350 tok_backup(tok, '.');
1351 } else {
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001352 tok_backup(tok, c);
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001353 }
Georg Brandldde00282007-03-18 19:01:53 +00001354 *p_start = tok->start;
1355 *p_end = tok->cur;
1356 return DOT;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001357 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001358
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001359 /* Number */
1360 if (isdigit(c)) {
1361 if (c == '0') {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001362 /* Hex, octal or binary -- maybe. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001363 c = tok_nextc(tok);
1364 if (c == '.')
1365 goto fraction;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001366#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001367 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001368 goto imaginary;
1369#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001370 if (c == 'x' || c == 'X') {
Georg Brandlfceab5a2008-01-19 20:08:23 +00001371
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001372 /* Hex */
Georg Brandlfceab5a2008-01-19 20:08:23 +00001373 c = tok_nextc(tok);
1374 if (!isxdigit(c)) {
1375 tok->done = E_TOKEN;
1376 tok_backup(tok, c);
1377 return ERRORTOKEN;
1378 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001379 do {
1380 c = tok_nextc(tok);
1381 } while (isxdigit(c));
1382 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001383 else if (c == 'o' || c == 'O') {
1384 /* Octal */
Georg Brandlfceab5a2008-01-19 20:08:23 +00001385 c = tok_nextc(tok);
Christian Heimes81ee3ef2008-05-04 22:42:01 +00001386 if (c < '0' || c >= '8') {
Georg Brandlfceab5a2008-01-19 20:08:23 +00001387 tok->done = E_TOKEN;
1388 tok_backup(tok, c);
1389 return ERRORTOKEN;
1390 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001391 do {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001392 c = tok_nextc(tok);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001393 } while ('0' <= c && c < '8');
1394 }
1395 else if (c == 'b' || c == 'B') {
1396 /* Binary */
Georg Brandlfceab5a2008-01-19 20:08:23 +00001397 c = tok_nextc(tok);
1398 if (c != '0' && c != '1') {
1399 tok->done = E_TOKEN;
1400 tok_backup(tok, c);
1401 return ERRORTOKEN;
1402 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001403 do {
1404 c = tok_nextc(tok);
1405 } while (c == '0' || c == '1');
1406 }
1407 else {
1408 int nonzero = 0;
1409 /* maybe old-style octal; c is first char of it */
1410 /* in any case, allow '0' as a literal */
1411 while (c == '0')
1412 c = tok_nextc(tok);
1413 while (isdigit(c)) {
1414 nonzero = 1;
1415 c = tok_nextc(tok);
Tim Petersd507dab2001-08-30 20:51:59 +00001416 }
1417 if (c == '.')
1418 goto fraction;
1419 else if (c == 'e' || c == 'E')
1420 goto exponent;
1421#ifndef WITHOUT_COMPLEX
1422 else if (c == 'j' || c == 'J')
1423 goto imaginary;
1424#endif
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001425 else if (nonzero) {
Tim Petersd507dab2001-08-30 20:51:59 +00001426 tok->done = E_TOKEN;
1427 tok_backup(tok, c);
1428 return ERRORTOKEN;
1429 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001430 }
1431 }
1432 else {
1433 /* Decimal */
1434 do {
1435 c = tok_nextc(tok);
1436 } while (isdigit(c));
Guido van Rossume2a383d2007-01-15 16:59:06 +00001437 {
Tim Peters9aa70d92001-08-27 19:19:28 +00001438 /* Accept floating point numbers. */
Guido van Rossumf023c461991-05-05 20:16:20 +00001439 if (c == '.') {
1440 fraction:
1441 /* Fraction */
1442 do {
1443 c = tok_nextc(tok);
1444 } while (isdigit(c));
1445 }
1446 if (c == 'e' || c == 'E') {
Tim Petersd507dab2001-08-30 20:51:59 +00001447 exponent:
Guido van Rossumf023c461991-05-05 20:16:20 +00001448 /* Exponent part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001449 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001450 if (c == '+' || c == '-')
1451 c = tok_nextc(tok);
Tim Peters9aa70d92001-08-27 19:19:28 +00001452 if (!isdigit(c)) {
1453 tok->done = E_TOKEN;
1454 tok_backup(tok, c);
1455 return ERRORTOKEN;
Guido van Rossumf023c461991-05-05 20:16:20 +00001456 }
Tim Peters9aa70d92001-08-27 19:19:28 +00001457 do {
1458 c = tok_nextc(tok);
1459 } while (isdigit(c));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001460 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001461#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001462 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001463 /* Imaginary part */
1464 imaginary:
1465 c = tok_nextc(tok);
1466#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001467 }
1468 }
1469 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001470 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001471 *p_end = tok->cur;
1472 return NUMBER;
1473 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001474
1475 letter_quote:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001476 /* String */
1477 if (c == '\'' || c == '"') {
Guido van Rossumcf171a72007-11-16 00:51:45 +00001478 int quote = c;
1479 int quote_size = 1; /* 1 or 3 */
1480 int end_quote_size = 0;
1481
1482 /* Find the quote size and start of string */
1483 c = tok_nextc(tok);
1484 if (c == quote) {
1485 c = tok_nextc(tok);
1486 if (c == quote)
1487 quote_size = 3;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001488 else
Guido van Rossumcf171a72007-11-16 00:51:45 +00001489 end_quote_size = 1; /* empty string found */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001490 }
Guido van Rossumcf171a72007-11-16 00:51:45 +00001491 if (c != quote)
1492 tok_backup(tok, c);
1493
1494 /* Get rest of string */
1495 while (end_quote_size != quote_size) {
1496 c = tok_nextc(tok);
1497 if (c == EOF) {
1498 if (quote_size == 3)
1499 tok->done = E_EOFS;
1500 else
1501 tok->done = E_EOLS;
1502 tok->cur = tok->inp;
1503 return ERRORTOKEN;
1504 }
1505 if (quote_size == 1 && c == '\n') {
1506 tok->done = E_EOLS;
1507 tok->cur = tok->inp;
1508 return ERRORTOKEN;
1509 }
1510 if (c == quote)
1511 end_quote_size += 1;
1512 else {
1513 end_quote_size = 0;
1514 if (c == '\\')
1515 c = tok_nextc(tok); /* skip escaped char */
1516 }
1517 }
1518
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001519 *p_start = tok->start;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001520 *p_end = tok->cur;
1521 return STRING;
1522 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001523
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001524 /* Line continuation */
1525 if (c == '\\') {
1526 c = tok_nextc(tok);
1527 if (c != '\n') {
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001528 tok->done = E_LINECONT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001529 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001530 return ERRORTOKEN;
1531 }
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001532 tok->cont_line = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001533 goto again; /* Read next line */
1534 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001535
Guido van Rossumfbab9051991-10-20 20:25:03 +00001536 /* Check for two-character token */
1537 {
1538 int c2 = tok_nextc(tok);
Guido van Rossum86bea461997-04-29 21:03:06 +00001539 int token = PyToken_TwoChars(c, c2);
Guido van Rossumfbab9051991-10-20 20:25:03 +00001540 if (token != OP) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001541 int c3 = tok_nextc(tok);
1542 int token3 = PyToken_ThreeChars(c, c2, c3);
1543 if (token3 != OP) {
1544 token = token3;
1545 } else {
1546 tok_backup(tok, c3);
1547 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001548 *p_start = tok->start;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001549 *p_end = tok->cur;
1550 return token;
1551 }
1552 tok_backup(tok, c2);
1553 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001554
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001555 /* Keep track of parentheses nesting level */
Guido van Rossuma849b831993-05-12 11:35:44 +00001556 switch (c) {
1557 case '(':
1558 case '[':
1559 case '{':
1560 tok->level++;
1561 break;
1562 case ')':
1563 case ']':
1564 case '}':
1565 tok->level--;
1566 break;
1567 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001568
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001569 /* Punctuation character */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001570 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001571 *p_end = tok->cur;
Guido van Rossum86bea461997-04-29 21:03:06 +00001572 return PyToken_OneChar(c);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001573}
1574
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001575int
1576PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
1577{
1578 int result = tok_get(tok, p_start, p_end);
1579 if (tok->decoding_erred) {
1580 result = ERRORTOKEN;
1581 tok->done = E_DECODE;
1582 }
1583 return result;
1584}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001585
Guido van Rossum40d20bc2007-10-22 00:09:51 +00001586/* Get -*- encoding -*- from a Python file.
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001587
1588 PyTokenizer_FindEncoding returns NULL when it can't find the encoding in
Guido van Rossumcf171a72007-11-16 00:51:45 +00001589 the first or second line of the file (in which case the encoding
Brett Cannone4539892007-10-20 03:46:49 +00001590 should be assumed to be PyUnicode_GetDefaultEncoding()).
1591
Guido van Rossum40d20bc2007-10-22 00:09:51 +00001592 The char * returned is malloc'ed via PyMem_MALLOC() and thus must be freed
1593 by the caller.
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001594*/
1595char *
Guido van Rossum40d20bc2007-10-22 00:09:51 +00001596PyTokenizer_FindEncoding(int fd)
1597{
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001598 struct tok_state *tok;
Guido van Rossum40d20bc2007-10-22 00:09:51 +00001599 FILE *fp;
1600 char *p_start =NULL , *p_end =NULL , *encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001601
Guido van Rossum40d20bc2007-10-22 00:09:51 +00001602 fd = dup(fd);
1603 if (fd < 0) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001604 return NULL;
1605 }
Guido van Rossum40d20bc2007-10-22 00:09:51 +00001606 fp = fdopen(fd, "r");
1607 if (fp == NULL) {
1608 return NULL;
1609 }
1610 tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL);
1611 if (tok == NULL) {
1612 fclose(fp);
1613 return NULL;
1614 }
1615 while (tok->lineno < 2 && tok->done == E_OK) {
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001616 PyTokenizer_Get(tok, &p_start, &p_end);
1617 }
Guido van Rossum40d20bc2007-10-22 00:09:51 +00001618 fclose(fp);
Brett Cannond5ec98c2007-10-20 02:54:14 +00001619 if (tok->encoding) {
Brett Cannonc2954e52007-10-21 02:45:33 +00001620 encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1);
Amaury Forgeot d'Arc1b933ed2008-09-04 22:34:09 +00001621 if (encoding)
1622 strcpy(encoding, tok->encoding);
Brett Cannond5ec98c2007-10-20 02:54:14 +00001623 }
1624 PyTokenizer_Free(tok);
Brett Cannond5ec98c2007-10-20 02:54:14 +00001625 return encoding;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001626}
Thomas Wouters89d996e2007-09-08 17:39:28 +00001627
Guido van Rossum408027e1996-12-30 16:17:54 +00001628#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001629
1630void
Thomas Wouters23c9e002000-07-22 19:20:54 +00001631tok_dump(int type, char *start, char *end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001632{
Guido van Rossum86bea461997-04-29 21:03:06 +00001633 printf("%s", _PyParser_TokenNames[type]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001634 if (type == NAME || type == NUMBER || type == STRING || type == OP)
1635 printf("(%.*s)", (int)(end - start), start);
1636}
1637
1638#endif