blob: 776183d80582d7d7d92e548d44f4cc8e35a47e9d [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"
15#include "stringobject.h"
16#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 Rossumcf57d8b1998-01-19 22:07:46 +000042/* Convert a possibly signed character to a nonnegative int */
43/* XXX This assumes characters are 8 bits wide */
44#ifdef __CHAR_UNSIGNED__
45#define Py_CHARMASK(c) (c)
46#else
47#define Py_CHARMASK(c) ((c) & 0xff)
48#endif
49
Guido van Rossum3f5da241990-12-20 15:06:42 +000050/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000051static struct tok_state *tok_new(void);
52static int tok_nextc(struct tok_state *tok);
53static void tok_backup(struct tok_state *tok, int c);
Guido van Rossum3f5da241990-12-20 15:06:42 +000054
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055/* Token names */
56
Guido van Rossum86bea461997-04-29 21:03:06 +000057char *_PyParser_TokenNames[] = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000058 "ENDMARKER",
59 "NAME",
60 "NUMBER",
61 "STRING",
62 "NEWLINE",
63 "INDENT",
64 "DEDENT",
65 "LPAR",
66 "RPAR",
67 "LSQB",
68 "RSQB",
69 "COLON",
70 "COMMA",
71 "SEMI",
72 "PLUS",
73 "MINUS",
74 "STAR",
75 "SLASH",
76 "VBAR",
77 "AMPER",
78 "LESS",
79 "GREATER",
80 "EQUAL",
81 "DOT",
82 "PERCENT",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000083 "LBRACE",
84 "RBRACE",
Guido van Rossumfbab9051991-10-20 20:25:03 +000085 "EQEQUAL",
86 "NOTEQUAL",
87 "LESSEQUAL",
88 "GREATEREQUAL",
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +000089 "TILDE",
90 "CIRCUMFLEX",
91 "LEFTSHIFT",
92 "RIGHTSHIFT",
Guido van Rossumf595fde1996-01-12 01:31:58 +000093 "DOUBLESTAR",
Thomas Wouters434d0822000-08-24 20:11:32 +000094 "PLUSEQUAL",
95 "MINEQUAL",
96 "STAREQUAL",
97 "SLASHEQUAL",
98 "PERCENTEQUAL",
99 "AMPEREQUAL",
100 "VBAREQUAL",
101 "CIRCUMFLEXEQUAL",
102 "LEFTSHIFTEQUAL",
103 "RIGHTSHIFTEQUAL",
104 "DOUBLESTAREQUAL",
Guido van Rossum4668b002001-08-08 05:00:18 +0000105 "DOUBLESLASH",
106 "DOUBLESLASHEQUAL",
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000107 "AT",
Neal Norwitzc1505362006-12-28 06:47:50 +0000108 "RARROW",
Georg Brandldde00282007-03-18 19:01:53 +0000109 "ELLIPSIS",
Guido van Rossumfbab9051991-10-20 20:25:03 +0000110 /* This table must match the #defines in token.h! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 "OP",
112 "<ERRORTOKEN>",
113 "<N_TOKENS>"
114};
115
116
117/* Create and initialize a new tok_state structure */
118
119static struct tok_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000120tok_new(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000122 struct tok_state *tok = (struct tok_state *)PyMem_MALLOC(
123 sizeof(struct tok_state));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 if (tok == NULL)
125 return NULL;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000126 tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 tok->done = E_OK;
128 tok->fp = NULL;
129 tok->tabsize = TABSIZE;
130 tok->indent = 0;
131 tok->indstack[0] = 0;
132 tok->atbol = 1;
133 tok->pendin = 0;
134 tok->prompt = tok->nextprompt = NULL;
135 tok->lineno = 0;
Guido van Rossuma849b831993-05-12 11:35:44 +0000136 tok->level = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +0000137 tok->filename = NULL;
Thomas Wouters6caa07b2006-04-14 11:33:28 +0000138 tok->altwarning = 1;
139 tok->alterror = 1;
Guido van Rossum926f13a1998-04-09 21:38:06 +0000140 tok->alttabsize = 1;
141 tok->altindstack[0] = 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000142 tok->decoding_state = 0;
143 tok->decoding_erred = 0;
144 tok->read_coding_spec = 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000145 tok->encoding = NULL;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000146 tok->cont_line = 0;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000147#ifndef PGEN
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000148 tok->decoding_readline = NULL;
149 tok->decoding_buffer = NULL;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000150#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 return tok;
152}
153
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000154#ifdef PGEN
155
156static char *
157decoding_fgets(char *s, int size, struct tok_state *tok)
158{
159 return fgets(s, size, tok->fp);
160}
161
162static int
163decoding_feof(struct tok_state *tok)
164{
165 return feof(tok->fp);
166}
167
168static const char *
169decode_str(const char *str, struct tok_state *tok)
170{
171 return str;
172}
173
174#else /* PGEN */
175
176static char *
177error_ret(struct tok_state *tok) /* XXX */
178{
179 tok->decoding_erred = 1;
180 if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000181 PyMem_FREE(tok->buf);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000182 tok->buf = NULL;
183 return NULL; /* as if it were EOF */
184}
185
186static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000187new_string(const char *s, Py_ssize_t len)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000188{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189 char* result = (char *)PyMem_MALLOC(len + 1);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000190 if (result != NULL) {
191 memcpy(result, s, len);
192 result[len] = '\0';
193 }
194 return result;
195}
196
197static char *
198get_normal_name(char *s) /* for utf-8 and latin-1 */
199{
200 char buf[13];
201 int i;
202 for (i = 0; i < 12; i++) {
203 int c = s[i];
204 if (c == '\0') break;
205 else if (c == '_') buf[i] = '-';
206 else buf[i] = tolower(c);
207 }
208 buf[i] = '\0';
209 if (strcmp(buf, "utf-8") == 0 ||
210 strncmp(buf, "utf-8-", 6) == 0) return "utf-8";
211 else if (strcmp(buf, "latin-1") == 0 ||
212 strcmp(buf, "iso-8859-1") == 0 ||
213 strcmp(buf, "iso-latin-1") == 0 ||
214 strncmp(buf, "latin-1-", 8) == 0 ||
215 strncmp(buf, "iso-8859-1-", 11) == 0 ||
216 strncmp(buf, "iso-latin-1-", 12) == 0) return "iso-8859-1";
217 else return s;
218}
219
220/* Return the coding spec in S, or NULL if none is found. */
221
222static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000223get_coding_spec(const char *s, Py_ssize_t size)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000224{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000225 Py_ssize_t i;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000226 /* Coding spec must be in a comment, and that comment must be
227 * the only statement on the source code line. */
228 for (i = 0; i < size - 6; i++) {
229 if (s[i] == '#')
230 break;
231 if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014')
232 return NULL;
233 }
234 for (; i < size - 6; i++) { /* XXX inefficient search */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000235 const char* t = s + i;
236 if (strncmp(t, "coding", 6) == 0) {
237 const char* begin = NULL;
238 t += 6;
239 if (t[0] != ':' && t[0] != '=')
240 continue;
241 do {
242 t++;
243 } while (t[0] == '\x20' || t[0] == '\t');
244
245 begin = t;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000246 while (isalnum(Py_CHARMASK(t[0])) ||
Neal Norwitze08e1bc2002-11-02 20:43:25 +0000247 t[0] == '-' || t[0] == '_' || t[0] == '.')
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000248 t++;
249
250 if (begin < t) {
251 char* r = new_string(begin, t - begin);
252 char* q = get_normal_name(r);
253 if (r != q) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254 PyMem_FREE(r);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000255 r = new_string(q, strlen(q));
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000256 }
257 return r;
258 }
259 }
260 }
261 return NULL;
262}
263
264/* Check whether the line contains a coding spec. If it does,
265 invoke the set_readline function for the new encoding.
266 This function receives the tok_state and the new encoding.
267 Return 1 on success, 0 on failure. */
268
269static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000270check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000271 int set_readline(struct tok_state *, const char *))
272{
Tim Peters17db21f2002-09-03 15:39:58 +0000273 char * cs;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000274 int r = 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000275
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000276 if (tok->cont_line)
277 /* It's a continuation line, so it can't be a coding spec. */
278 return 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000279 cs = get_coding_spec(line, size);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000280 if (cs != NULL) {
281 tok->read_coding_spec = 1;
282 if (tok->encoding == NULL) {
283 assert(tok->decoding_state == 1); /* raw */
284 if (strcmp(cs, "utf-8") == 0 ||
285 strcmp(cs, "iso-8859-1") == 0) {
286 tok->encoding = cs;
287 } else {
288 r = set_readline(tok, cs);
289 if (r) {
290 tok->encoding = cs;
291 tok->decoding_state = -1;
292 }
Neal Norwitzc0d5faa2005-10-21 06:05:33 +0000293 else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294 PyMem_FREE(cs);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000295 }
296 } else { /* then, compare cs with BOM */
297 r = (strcmp(tok->encoding, cs) == 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000298 PyMem_FREE(cs);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000299 }
300 }
Neal Norwitzdb83eb32005-12-18 05:29:30 +0000301 if (!r) {
302 cs = tok->encoding;
303 if (!cs)
304 cs = "with BOM";
305 PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
306 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000307 return r;
308}
309
310/* See whether the file starts with a BOM. If it does,
311 invoke the set_readline function with the new encoding.
312 Return 1 on success, 0 on failure. */
313
314static int
315check_bom(int get_char(struct tok_state *),
316 void unget_char(int, struct tok_state *),
317 int set_readline(struct tok_state *, const char *),
318 struct tok_state *tok)
319{
320 int ch = get_char(tok);
321 tok->decoding_state = 1;
322 if (ch == EOF) {
323 return 1;
324 } else if (ch == 0xEF) {
325 ch = get_char(tok); if (ch != 0xBB) goto NON_BOM;
326 ch = get_char(tok); if (ch != 0xBF) goto NON_BOM;
327#if 0
328 /* Disable support for UTF-16 BOMs until a decision
329 is made whether this needs to be supported. */
330 } else if (ch == 0xFE) {
331 ch = get_char(tok); if (ch != 0xFF) goto NON_BOM;
332 if (!set_readline(tok, "utf-16-be")) return 0;
333 tok->decoding_state = -1;
334 } else if (ch == 0xFF) {
335 ch = get_char(tok); if (ch != 0xFE) goto NON_BOM;
336 if (!set_readline(tok, "utf-16-le")) return 0;
337 tok->decoding_state = -1;
338#endif
339 } else {
340 unget_char(ch, tok);
341 return 1;
342 }
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000343 if (tok->encoding != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344 PyMem_FREE(tok->encoding);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000345 tok->encoding = new_string("utf-8", 5); /* resulting is in utf-8 */
346 return 1;
347 NON_BOM:
348 /* any token beginning with '\xEF', '\xFE', '\xFF' is a bad token */
349 unget_char(0xFF, tok); /* XXX this will cause a syntax error */
350 return 1;
351}
352
353/* Read a line of text from TOK into S, using the stream in TOK.
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000354 Return NULL on failure, else S.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000356 On entry, tok->decoding_buffer will be one of:
357 1) NULL: need to call tok->decoding_readline to get a new line
358 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
359 stored the result in tok->decoding_buffer
360 3) PyStringObject *: previous call to fp_readl did not have enough room
361 (in the s buffer) to copy entire contents of the line read
362 by tok->decoding_readline. tok->decoding_buffer has the overflow.
363 In this case, fp_readl is called in a loop (with an expanded buffer)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000364 until the buffer ends with a '\n' (or until the end of the file is
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000365 reached): see tok_nextc and its calls to decoding_fgets.
366*/
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000367
368static char *
369fp_readl(char *s, int size, struct tok_state *tok)
370{
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000371 PyObject* bufobj = tok->decoding_buffer;
372 const char *buf;
373 Py_ssize_t buflen;
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000374 int allocated = 0;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000375
376 /* Ask for one less byte so we can terminate it */
377 assert(size > 0);
378 size--;
379
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000380 if (bufobj == NULL) {
381 bufobj = PyObject_CallObject(tok->decoding_readline, NULL);
382 if (bufobj == NULL)
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000383 goto error;
384 allocated = 1;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000385 }
Guido van Rossum7d1df6c2007-08-29 13:53:23 +0000386 buf = PyUnicode_AsStringAndSize(bufobj, &buflen);
387 if (buf == NULL) {
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000388 goto error;
389 }
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000390 if (buflen > size) {
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000391 Py_XDECREF(tok->decoding_buffer);
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000392 tok->decoding_buffer = PyBytes_FromStringAndSize(buf+size,
393 buflen-size);
394 if (tok->decoding_buffer == NULL)
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000395 goto error;
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000396 buflen = size;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000397 }
Guido van Rossumccf4f0f2007-05-09 23:38:34 +0000398 memcpy(s, buf, buflen);
399 s[buflen] = '\0';
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000400 if (buflen == 0) /* EOF */
401 s = NULL;
402 if (allocated) {
403 Py_DECREF(bufobj);
404 }
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000405 return s;
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000406
407error:
408 if (allocated) {
409 Py_XDECREF(bufobj);
410 }
411 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000412}
413
414/* Set the readline function for TOK to a StreamReader's
415 readline function. The StreamReader is named ENC.
416
417 This function is called from check_bom and check_coding_spec.
418
419 ENC is usually identical to the future value of tok->encoding,
420 except for the (currently unsupported) case of UTF-16.
421
422 Return 1 on success, 0 on failure. */
423
424static int
425fp_setreadl(struct tok_state *tok, const char* enc)
426{
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000427 PyObject *readline = NULL, *stream = NULL, *io = NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000428
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000429 io = PyImport_ImportModule("io");
430 if (io == NULL)
431 goto cleanup;
432
433 stream = PyObject_CallMethod(io, "open", "ssis",
434 tok->filename, "r", -1, enc);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000435 if (stream == NULL)
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000436 goto cleanup;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000437
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000438 Py_XDECREF(tok->decoding_readline);
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000439 readline = PyObject_GetAttrString(stream, "readline");
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000440 tok->decoding_readline = readline;
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000441
442 cleanup:
443 Py_XDECREF(stream);
444 Py_XDECREF(io);
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000445 return readline != NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000446}
447
448/* Fetch the next byte from TOK. */
449
450static int fp_getc(struct tok_state *tok) {
451 return getc(tok->fp);
452}
453
454/* Unfetch the last byte back into TOK. */
455
456static void fp_ungetc(int c, struct tok_state *tok) {
457 ungetc(c, tok->fp);
458}
459
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000460/* Check whether the characters at s start a valid
461 UTF-8 sequence. Return the number of characters forming
462 the sequence if yes, 0 if not. */
463static int valid_utf8(const unsigned char* s)
464{
465 int expected = 0;
466 int length;
467 if (*s < 0x80)
468 /* single-byte code */
469 return 1;
470 if (*s < 0xc0)
471 /* following byte */
472 return 0;
473 if (*s < 0xE0)
474 expected = 1;
475 else if (*s < 0xF0)
476 expected = 2;
477 else if (*s < 0xF8)
478 expected = 3;
479 else
480 return 0;
481 length = expected + 1;
482 for (; expected; expected--)
483 if (s[expected] < 0x80 || s[expected] >= 0xC0)
484 return 0;
485 return length;
486}
487
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000488/* Read a line of input from TOK. Determine encoding
489 if necessary. */
490
491static char *
492decoding_fgets(char *s, int size, struct tok_state *tok)
493{
Martin v. Löwis2863c102002-08-07 15:18:57 +0000494 char *line = NULL;
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000495 int badchar = 0;
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000496 for (;;) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000497 if (tok->decoding_state < 0) {
498 /* We already have a codec associated with
499 this input. */
500 line = fp_readl(s, size, tok);
501 break;
502 } else if (tok->decoding_state > 0) {
503 /* We want a 'raw' read. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000504 line = Py_UniversalNewlineFgets(s, size,
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000505 tok->fp, NULL);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000506 break;
507 } else {
508 /* We have not yet determined the encoding.
509 If an encoding is found, use the file-pointer
510 reader functions from now on. */
511 if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok))
512 return error_ret(tok);
513 assert(tok->decoding_state != 0);
514 }
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000515 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000516 if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) {
517 if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) {
518 return error_ret(tok);
519 }
520 }
521#ifndef PGEN
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000522 /* The default encoding is UTF-8, so make sure we don't have any
523 non-UTF-8 sequences in it. */
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000524 if (line && !tok->encoding) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000525 unsigned char *c;
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000526 int length;
527 for (c = (unsigned char *)line; *c; c += length)
528 if (!(length = valid_utf8(c))) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000529 badchar = *c;
530 break;
531 }
532 }
533 if (badchar) {
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000534 char buf[500];
Martin v. Löwis725bb232002-08-05 01:49:16 +0000535 /* Need to add 1 to the line number, since this line
536 has not been counted, yet. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537 sprintf(buf,
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000538 "Non-UTF-8 code starting with '\\x%.2x' "
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000539 "in file %.200s on line %i, "
540 "but no encoding declared; "
Guido van Rossum21b731f2007-08-30 00:10:46 +0000541 "see http://python.org/dev/peps/pep-0263/ for details",
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000542 badchar, tok->filename, tok->lineno + 1);
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000543 PyErr_SetString(PyExc_SyntaxError, buf);
544 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000545 }
546#endif
547 return line;
548}
549
550static int
551decoding_feof(struct tok_state *tok)
552{
553 if (tok->decoding_state >= 0) {
554 return feof(tok->fp);
555 } else {
556 PyObject* buf = tok->decoding_buffer;
557 if (buf == NULL) {
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000558 buf = PyObject_CallObject(tok->decoding_readline, NULL);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000559 if (buf == NULL) {
560 error_ret(tok);
561 return 1;
562 } else {
563 tok->decoding_buffer = buf;
564 }
565 }
566 return PyObject_Length(buf) == 0;
567 }
568}
569
570/* Fetch a byte from TOK, using the string buffer. */
571
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000572static int
573buf_getc(struct tok_state *tok) {
Just van Rossumf032f862003-02-09 20:38:48 +0000574 return Py_CHARMASK(*tok->str++);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000575}
576
577/* Unfetch a byte from TOK, using the string buffer. */
578
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579static void
580buf_ungetc(int c, struct tok_state *tok) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000581 tok->str--;
Just van Rossumf032f862003-02-09 20:38:48 +0000582 assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000583}
584
585/* Set the readline function for TOK to ENC. For the string-based
586 tokenizer, this means to just record the encoding. */
587
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000588static int
589buf_setreadl(struct tok_state *tok, const char* enc) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000590 tok->enc = enc;
591 return 1;
592}
593
594/* Return a UTF-8 encoding Python string object from the
595 C byte string STR, which is encoded with ENC. */
596
597static PyObject *
598translate_into_utf8(const char* str, const char* enc) {
599 PyObject *utf8;
600 PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL);
601 if (buf == NULL)
602 return NULL;
603 utf8 = PyUnicode_AsUTF8String(buf);
604 Py_DECREF(buf);
605 return utf8;
606}
607
608/* Decode a byte string STR for use as the buffer of TOK.
609 Look for encoding declarations inside STR, and record them
610 inside TOK. */
611
612static const char *
613decode_str(const char *str, struct tok_state *tok)
614{
615 PyObject* utf8 = NULL;
616 const char *s;
617 int lineno = 0;
618 tok->enc = NULL;
619 tok->str = str;
620 if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000621 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000622 str = tok->str; /* string after BOM if any */
Tim Peters2c3f9c62002-08-04 17:58:34 +0000623 assert(str);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000624 if (tok->enc != NULL) {
625 utf8 = translate_into_utf8(str, tok->enc);
626 if (utf8 == NULL)
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000627 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000628 str = PyString_AsString(utf8);
629 }
630 for (s = str;; s++) {
631 if (*s == '\0') break;
632 else if (*s == '\n') {
633 lineno++;
634 if (lineno == 2) break;
635 }
636 }
637 tok->enc = NULL;
638 if (!check_coding_spec(str, s - str, tok, buf_setreadl))
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000639 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000640 if (tok->enc != NULL) {
641 assert(utf8 == NULL);
642 utf8 = translate_into_utf8(str, tok->enc);
Neal Norwitz40d37812005-10-02 01:48:49 +0000643 if (utf8 == NULL) {
644 PyErr_Format(PyExc_SyntaxError,
645 "unknown encoding: %s", tok->enc);
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000646 return error_ret(tok);
Neal Norwitz40d37812005-10-02 01:48:49 +0000647 }
Neal Norwitzf7f28fc2007-08-11 21:31:25 +0000648 str = PyBytes_AsString(utf8);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000649 }
650 assert(tok->decoding_buffer == NULL);
651 tok->decoding_buffer = utf8; /* CAUTION */
652 return str;
653}
654
655#endif /* PGEN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000656
657/* Set up tokenizer for string */
658
659struct tok_state *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000660PyTokenizer_FromString(const char *str)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000661{
662 struct tok_state *tok = tok_new();
663 if (tok == NULL)
664 return NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000665 str = (char *)decode_str(str, tok);
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000666 if (str == NULL) {
667 PyTokenizer_Free(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000668 return NULL;
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000669 }
670
Martin v. Löwis95292d62002-12-11 14:04:59 +0000671 /* XXX: constify members. */
672 tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673 return tok;
674}
675
676
Guido van Rossum8c11a5c1991-07-27 21:42:56 +0000677/* Set up tokenizer for file */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000678
679struct tok_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000680PyTokenizer_FromFile(FILE *fp, char *ps1, char *ps2)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681{
682 struct tok_state *tok = tok_new();
683 if (tok == NULL)
684 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000685 if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) {
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000686 PyTokenizer_Free(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000687 return NULL;
688 }
689 tok->cur = tok->inp = tok->buf;
690 tok->end = tok->buf + BUFSIZ;
691 tok->fp = fp;
692 tok->prompt = ps1;
693 tok->nextprompt = ps2;
694 return tok;
695}
696
697
698/* Free a tok_state structure */
699
700void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000701PyTokenizer_Free(struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702{
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000703 if (tok->encoding != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 PyMem_FREE(tok->encoding);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000705#ifndef PGEN
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000706 Py_XDECREF(tok->decoding_readline);
707 Py_XDECREF(tok->decoding_buffer);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000708#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000709 if (tok->fp != NULL && tok->buf != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 PyMem_FREE(tok->buf);
711 PyMem_FREE(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712}
713
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714/* Get next char, updating state; error code goes into tok->done */
715
716static int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000717tok_nextc(register struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000718{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000719 for (;;) {
Guido van Rossum1a817c01994-09-19 08:06:25 +0000720 if (tok->cur != tok->inp) {
Guido van Rossumcf57d8b1998-01-19 22:07:46 +0000721 return Py_CHARMASK(*tok->cur++); /* Fast path */
Guido van Rossum1a817c01994-09-19 08:06:25 +0000722 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000723 if (tok->done != E_OK)
724 return EOF;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000725 if (tok->fp == NULL) {
Guido van Rossum1a817c01994-09-19 08:06:25 +0000726 char *end = strchr(tok->inp, '\n');
727 if (end != NULL)
728 end++;
729 else {
730 end = strchr(tok->inp, '\0');
731 if (end == tok->inp) {
732 tok->done = E_EOF;
733 return EOF;
734 }
735 }
736 if (tok->start == NULL)
737 tok->buf = tok->cur;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000738 tok->line_start = tok->cur;
Guido van Rossum1a817c01994-09-19 08:06:25 +0000739 tok->lineno++;
740 tok->inp = end;
Guido van Rossumcf57d8b1998-01-19 22:07:46 +0000741 return Py_CHARMASK(*tok->cur++);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000742 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000743 if (tok->prompt != NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744 char *newtok = PyOS_Readline(stdin, stdout, tok->prompt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000745 if (tok->nextprompt != NULL)
746 tok->prompt = tok->nextprompt;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000747 if (newtok == NULL)
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000748 tok->done = E_INTR;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000749 else if (*newtok == '\0') {
750 PyMem_FREE(newtok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000751 tok->done = E_EOF;
752 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000753 else if (tok->start != NULL) {
Guido van Rossum6da34342000-06-28 22:00:02 +0000754 size_t start = tok->start - tok->buf;
755 size_t oldlen = tok->cur - tok->buf;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000756 size_t newlen = oldlen + strlen(newtok);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000757 char *buf = tok->buf;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000758 buf = (char *)PyMem_REALLOC(buf, newlen+1);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000759 tok->lineno++;
760 if (buf == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761 PyMem_FREE(tok->buf);
Guido van Rossum588633d1994-12-30 15:46:02 +0000762 tok->buf = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000763 PyMem_FREE(newtok);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000764 tok->done = E_NOMEM;
765 return EOF;
766 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000767 tok->buf = buf;
768 tok->cur = tok->buf + oldlen;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000769 tok->line_start = tok->cur;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000770 strcpy(tok->buf + oldlen, newtok);
771 PyMem_FREE(newtok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000772 tok->inp = tok->buf + newlen;
773 tok->end = tok->inp + 1;
774 tok->start = tok->buf + start;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000775 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000776 else {
777 tok->lineno++;
778 if (tok->buf != NULL)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000779 PyMem_FREE(tok->buf);
780 tok->buf = newtok;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000781 tok->line_start = tok->buf;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000782 tok->cur = tok->buf;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000783 tok->line_start = tok->buf;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000784 tok->inp = strchr(tok->buf, '\0');
785 tok->end = tok->inp + 1;
786 }
787 }
788 else {
789 int done = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000790 Py_ssize_t cur = 0;
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000791 char *pt;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000792 if (tok->start == NULL) {
793 if (tok->buf == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000794 tok->buf = (char *)
795 PyMem_MALLOC(BUFSIZ);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000796 if (tok->buf == NULL) {
797 tok->done = E_NOMEM;
798 return EOF;
799 }
800 tok->end = tok->buf + BUFSIZ;
801 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000802 if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
803 tok) == NULL) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000804 tok->done = E_EOF;
805 done = 1;
806 }
807 else {
808 tok->done = E_OK;
809 tok->inp = strchr(tok->buf, '\0');
810 done = tok->inp[-1] == '\n';
811 }
812 }
813 else {
814 cur = tok->cur - tok->buf;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000815 if (decoding_feof(tok)) {
Guido van Rossum78c05351995-01-17 16:12:13 +0000816 tok->done = E_EOF;
817 done = 1;
818 }
819 else
820 tok->done = E_OK;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000821 }
822 tok->lineno++;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000823 /* Read until '\n' or EOF */
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000824 while (!done) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000825 Py_ssize_t curstart = tok->start == NULL ? -1 :
826 tok->start - tok->buf;
827 Py_ssize_t curvalid = tok->inp - tok->buf;
828 Py_ssize_t newsize = curvalid + BUFSIZ;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000829 char *newbuf = tok->buf;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000830 newbuf = (char *)PyMem_REALLOC(newbuf,
831 newsize);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000832 if (newbuf == NULL) {
833 tok->done = E_NOMEM;
834 tok->cur = tok->inp;
835 return EOF;
836 }
837 tok->buf = newbuf;
838 tok->inp = tok->buf + curvalid;
839 tok->end = tok->buf + newsize;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000840 tok->start = curstart < 0 ? NULL :
841 tok->buf + curstart;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000842 if (decoding_fgets(tok->inp,
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000843 (int)(tok->end - tok->inp),
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000844 tok) == NULL) {
Thomas Wouters7eaf2aa2006-03-02 20:41:27 +0000845 /* Break out early on decoding
846 errors, as tok->buf will be NULL
847 */
848 if (tok->decoding_erred)
849 return EOF;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000850 /* Last line does not end in \n,
851 fake one */
852 strcpy(tok->inp, "\n");
853 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000854 tok->inp = strchr(tok->inp, '\0');
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000855 done = tok->inp[-1] == '\n';
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000856 }
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000857 if (tok->buf != NULL) {
858 tok->cur = tok->buf + cur;
859 tok->line_start = tok->cur;
860 /* replace "\r\n" with "\n" */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000861 /* For Mac leave the \r, giving a syntax error */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000862 pt = tok->inp - 2;
863 if (pt >= tok->buf && *pt == '\r') {
864 *pt++ = '\n';
865 *pt = '\0';
866 tok->inp = pt;
867 }
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000868 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869 }
870 if (tok->done != E_OK) {
871 if (tok->prompt != NULL)
Guido van Rossum6e73bf41998-08-25 18:13:04 +0000872 PySys_WriteStderr("\n");
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000873 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000874 return EOF;
875 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000876 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000877 /*NOTREACHED*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000878}
879
880
881/* Back-up one character */
882
883static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000884tok_backup(register struct tok_state *tok, register int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000885{
886 if (c != EOF) {
Guido van Rossum588633d1994-12-30 15:46:02 +0000887 if (--tok->cur < tok->buf)
Guido van Rossum86bea461997-04-29 21:03:06 +0000888 Py_FatalError("tok_backup: begin of buffer");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889 if (*tok->cur != c)
890 *tok->cur = c;
891 }
892}
893
894
895/* Return the token corresponding to a single character */
896
897int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000898PyToken_OneChar(int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000899{
900 switch (c) {
901 case '(': return LPAR;
902 case ')': return RPAR;
903 case '[': return LSQB;
904 case ']': return RSQB;
905 case ':': return COLON;
906 case ',': return COMMA;
907 case ';': return SEMI;
908 case '+': return PLUS;
909 case '-': return MINUS;
910 case '*': return STAR;
911 case '/': return SLASH;
912 case '|': return VBAR;
913 case '&': return AMPER;
914 case '<': return LESS;
915 case '>': return GREATER;
916 case '=': return EQUAL;
917 case '.': return DOT;
918 case '%': return PERCENT;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000919 case '{': return LBRACE;
920 case '}': return RBRACE;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000921 case '^': return CIRCUMFLEX;
922 case '~': return TILDE;
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000923 case '@': return AT;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000924 default: return OP;
925 }
926}
927
928
Guido van Rossumfbab9051991-10-20 20:25:03 +0000929int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000930PyToken_TwoChars(int c1, int c2)
Guido van Rossumfbab9051991-10-20 20:25:03 +0000931{
932 switch (c1) {
933 case '=':
934 switch (c2) {
935 case '=': return EQEQUAL;
936 }
937 break;
938 case '!':
939 switch (c2) {
940 case '=': return NOTEQUAL;
941 }
942 break;
943 case '<':
944 switch (c2) {
Guido van Rossumfbab9051991-10-20 20:25:03 +0000945 case '=': return LESSEQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000946 case '<': return LEFTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000947 }
948 break;
949 case '>':
950 switch (c2) {
951 case '=': return GREATEREQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000952 case '>': return RIGHTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000953 }
954 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000955 case '+':
956 switch (c2) {
957 case '=': return PLUSEQUAL;
958 }
959 break;
960 case '-':
961 switch (c2) {
962 case '=': return MINEQUAL;
Neal Norwitzc1505362006-12-28 06:47:50 +0000963 case '>': return RARROW;
Thomas Wouters434d0822000-08-24 20:11:32 +0000964 }
965 break;
Guido van Rossumf595fde1996-01-12 01:31:58 +0000966 case '*':
967 switch (c2) {
968 case '*': return DOUBLESTAR;
Thomas Wouters434d0822000-08-24 20:11:32 +0000969 case '=': return STAREQUAL;
970 }
971 break;
972 case '/':
973 switch (c2) {
Guido van Rossum4668b002001-08-08 05:00:18 +0000974 case '/': return DOUBLESLASH;
Thomas Wouters434d0822000-08-24 20:11:32 +0000975 case '=': return SLASHEQUAL;
976 }
977 break;
978 case '|':
979 switch (c2) {
980 case '=': return VBAREQUAL;
981 }
982 break;
983 case '%':
984 switch (c2) {
985 case '=': return PERCENTEQUAL;
986 }
987 break;
988 case '&':
989 switch (c2) {
990 case '=': return AMPEREQUAL;
991 }
992 break;
993 case '^':
994 switch (c2) {
995 case '=': return CIRCUMFLEXEQUAL;
Guido van Rossumf595fde1996-01-12 01:31:58 +0000996 }
997 break;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000998 }
999 return OP;
1000}
1001
Thomas Wouters434d0822000-08-24 20:11:32 +00001002int
1003PyToken_ThreeChars(int c1, int c2, int c3)
1004{
1005 switch (c1) {
1006 case '<':
1007 switch (c2) {
1008 case '<':
1009 switch (c3) {
1010 case '=':
1011 return LEFTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001012 }
1013 break;
1014 }
1015 break;
1016 case '>':
1017 switch (c2) {
1018 case '>':
1019 switch (c3) {
1020 case '=':
1021 return RIGHTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001022 }
1023 break;
1024 }
1025 break;
1026 case '*':
1027 switch (c2) {
1028 case '*':
1029 switch (c3) {
1030 case '=':
1031 return DOUBLESTAREQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001032 }
1033 break;
1034 }
1035 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001036 case '/':
1037 switch (c2) {
1038 case '/':
1039 switch (c3) {
1040 case '=':
1041 return DOUBLESLASHEQUAL;
1042 }
1043 break;
1044 }
1045 break;
Georg Brandldde00282007-03-18 19:01:53 +00001046 case '.':
1047 switch (c2) {
1048 case '.':
1049 switch (c3) {
1050 case '.':
1051 return ELLIPSIS;
1052 }
1053 break;
1054 }
1055 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001056 }
1057 return OP;
1058}
Guido van Rossumfbab9051991-10-20 20:25:03 +00001059
Guido van Rossum926f13a1998-04-09 21:38:06 +00001060static int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001061indenterror(struct tok_state *tok)
Guido van Rossum926f13a1998-04-09 21:38:06 +00001062{
1063 if (tok->alterror) {
Fred Drake85f36392000-07-11 17:53:00 +00001064 tok->done = E_TABSPACE;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001065 tok->cur = tok->inp;
1066 return 1;
1067 }
1068 if (tok->altwarning) {
Fred Drake85f36392000-07-11 17:53:00 +00001069 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
1070 "in indentation\n", tok->filename);
Guido van Rossum926f13a1998-04-09 21:38:06 +00001071 tok->altwarning = 0;
1072 }
1073 return 0;
1074}
1075
Martin v. Löwis47383402007-08-15 07:32:56 +00001076#ifdef PGEN
1077#define verify_identifier(s,e) 1
1078#else
1079/* Verify that the identifier follows PEP 3131. */
1080static int
1081verify_identifier(char *start, char *end)
1082{
Guido van Rossume3e37012007-08-29 18:54:41 +00001083 PyObject *s;
1084 int result;
1085 s = PyUnicode_DecodeUTF8(start, end-start, NULL);
1086 if (s == NULL) {
1087 PyErr_Clear();
1088 return 0;
1089 }
1090 result = PyUnicode_IsIdentifier(s);
Martin v. Löwis47383402007-08-15 07:32:56 +00001091 Py_DECREF(s);
1092 return result;
1093}
1094#endif
Guido van Rossum926f13a1998-04-09 21:38:06 +00001095
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001096/* Get next token, after space stripping etc. */
1097
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001098static int
1099tok_get(register struct tok_state *tok, char **p_start, char **p_end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001100{
1101 register int c;
Martin v. Löwis47383402007-08-15 07:32:56 +00001102 int blankline, nonascii;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001103
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001104 *p_start = *p_end = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001105 nextline:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001106 tok->start = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001107 blankline = 0;
1108
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001109 /* Get indentation level */
1110 if (tok->atbol) {
1111 register int col = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001112 register int altcol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001113 tok->atbol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001114 for (;;) {
1115 c = tok_nextc(tok);
1116 if (c == ' ')
Guido van Rossum926f13a1998-04-09 21:38:06 +00001117 col++, altcol++;
1118 else if (c == '\t') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001119 col = (col/tok->tabsize + 1) * tok->tabsize;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001120 altcol = (altcol/tok->alttabsize + 1)
1121 * tok->alttabsize;
1122 }
Guido van Rossum94d32b11995-07-07 22:27:27 +00001123 else if (c == '\014') /* Control-L (formfeed) */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001124 col = altcol = 0; /* For Emacs users */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001125 else
1126 break;
1127 }
1128 tok_backup(tok, c);
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001129 if (c == '#' || c == '\n') {
1130 /* Lines with only whitespace and/or comments
1131 shouldn't affect the indentation and are
1132 not passed to the parser as NEWLINE tokens,
1133 except *totally* empty lines in interactive
1134 mode, which signal the end of a command group. */
1135 if (col == 0 && c == '\n' && tok->prompt != NULL)
1136 blankline = 0; /* Let it through */
1137 else
1138 blankline = 1; /* Ignore completely */
1139 /* We can't jump back right here since we still
1140 may need to skip to the end of a comment */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001141 }
Guido van Rossuma849b831993-05-12 11:35:44 +00001142 if (!blankline && tok->level == 0) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001143 if (col == tok->indstack[tok->indent]) {
1144 /* No change */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001145 if (altcol != tok->altindstack[tok->indent]) {
1146 if (indenterror(tok))
1147 return ERRORTOKEN;
1148 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001149 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001150 else if (col > tok->indstack[tok->indent]) {
1151 /* Indent -- always one */
1152 if (tok->indent+1 >= MAXINDENT) {
Fred Drake85f36392000-07-11 17:53:00 +00001153 tok->done = E_TOODEEP;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001154 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001155 return ERRORTOKEN;
1156 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001157 if (altcol <= tok->altindstack[tok->indent]) {
1158 if (indenterror(tok))
1159 return ERRORTOKEN;
1160 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001161 tok->pendin++;
1162 tok->indstack[++tok->indent] = col;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001163 tok->altindstack[tok->indent] = altcol;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001164 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001165 else /* col < tok->indstack[tok->indent] */ {
1166 /* Dedent -- any number, must be consistent */
1167 while (tok->indent > 0 &&
1168 col < tok->indstack[tok->indent]) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001169 tok->pendin--;
Guido van Rossum54758fa1998-02-16 22:18:00 +00001170 tok->indent--;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001171 }
1172 if (col != tok->indstack[tok->indent]) {
Fred Drake85f36392000-07-11 17:53:00 +00001173 tok->done = E_DEDENT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001174 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001175 return ERRORTOKEN;
1176 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001177 if (altcol != tok->altindstack[tok->indent]) {
1178 if (indenterror(tok))
1179 return ERRORTOKEN;
1180 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001181 }
1182 }
1183 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001184
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001185 tok->start = tok->cur;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001186
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001187 /* Return pending indents/dedents */
1188 if (tok->pendin != 0) {
1189 if (tok->pendin < 0) {
1190 tok->pendin++;
1191 return DEDENT;
1192 }
1193 else {
1194 tok->pendin--;
1195 return INDENT;
1196 }
1197 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001198
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001199 again:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001200 tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001201 /* Skip spaces */
1202 do {
1203 c = tok_nextc(tok);
Guido van Rossum94d32b11995-07-07 22:27:27 +00001204 } while (c == ' ' || c == '\t' || c == '\014');
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001205
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001206 /* Set start of current token */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001207 tok->start = tok->cur - 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001208
Thomas Wouters6caa07b2006-04-14 11:33:28 +00001209 /* Skip comment */
1210 if (c == '#')
Guido van Rossumab5ca152000-03-31 00:52:27 +00001211 while (c != EOF && c != '\n')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001212 c = tok_nextc(tok);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001213
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001214 /* Check for EOF and errors now */
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001215 if (c == EOF) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001216 return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001217 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001218
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001219 /* Identifier (most frequent token!) */
Martin v. Löwis47383402007-08-15 07:32:56 +00001220 nonascii = 0;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001221 if (is_potential_identifier_start(c)) {
Guido van Rossum86016cb2000-03-10 22:56:54 +00001222 /* Process r"", u"" and ur"" */
Guido van Rossum5026cb41997-04-25 17:32:00 +00001223 switch (c) {
1224 case 'r':
1225 case 'R':
1226 c = tok_nextc(tok);
1227 if (c == '"' || c == '\'')
1228 goto letter_quote;
Guido van Rossum86016cb2000-03-10 22:56:54 +00001229 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001230 case 'b':
1231 case 'B':
1232 c = tok_nextc(tok);
1233 if (c == 'r' || c == 'R')
1234 c = tok_nextc(tok);
1235 if (c == '"' || c == '\'')
1236 goto letter_quote;
1237 break;
Guido van Rossum5026cb41997-04-25 17:32:00 +00001238 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001239 while (is_potential_identifier_char(c)) {
Martin v. Löwis47383402007-08-15 07:32:56 +00001240 if (c >= 128)
1241 nonascii = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001242 c = tok_nextc(tok);
Guido van Rossum24dacb31997-04-06 03:46:20 +00001243 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001244 tok_backup(tok, c);
Martin v. Löwis47383402007-08-15 07:32:56 +00001245 if (nonascii &&
1246 !verify_identifier(tok->start, tok->cur)) {
1247 tok->done = E_IDENTIFIER;
1248 return ERRORTOKEN;
1249 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001250 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001251 *p_end = tok->cur;
1252 return NAME;
1253 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001254
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001255 /* Newline */
1256 if (c == '\n') {
1257 tok->atbol = 1;
Guido van Rossuma849b831993-05-12 11:35:44 +00001258 if (blankline || tok->level > 0)
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001259 goto nextline;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001260 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001261 *p_end = tok->cur - 1; /* Leave '\n' out of the string */
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001262 tok->cont_line = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263 return NEWLINE;
1264 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001265
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001266 /* Period or number starting with period? */
1267 if (c == '.') {
1268 c = tok_nextc(tok);
1269 if (isdigit(c)) {
1270 goto fraction;
Georg Brandldde00282007-03-18 19:01:53 +00001271 } else if (c == '.') {
1272 c = tok_nextc(tok);
1273 if (c == '.') {
1274 *p_start = tok->start;
1275 *p_end = tok->cur;
1276 return ELLIPSIS;
1277 } else {
1278 tok_backup(tok, c);
1279 }
1280 tok_backup(tok, '.');
1281 } else {
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001282 tok_backup(tok, c);
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001283 }
Georg Brandldde00282007-03-18 19:01:53 +00001284 *p_start = tok->start;
1285 *p_end = tok->cur;
1286 return DOT;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001287 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001288
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001289 /* Number */
1290 if (isdigit(c)) {
1291 if (c == '0') {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001292 /* Hex, octal or binary -- maybe. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001293 c = tok_nextc(tok);
1294 if (c == '.')
1295 goto fraction;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001296#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001297 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001298 goto imaginary;
1299#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001300 if (c == 'x' || c == 'X') {
1301 /* Hex */
1302 do {
1303 c = tok_nextc(tok);
1304 } while (isxdigit(c));
1305 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001306 else if (c == 'o' || c == 'O') {
1307 /* Octal */
1308 do {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001309 c = tok_nextc(tok);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001310 } while ('0' <= c && c < '8');
1311 }
1312 else if (c == 'b' || c == 'B') {
1313 /* Binary */
1314 do {
1315 c = tok_nextc(tok);
1316 } while (c == '0' || c == '1');
1317 }
1318 else {
1319 int nonzero = 0;
1320 /* maybe old-style octal; c is first char of it */
1321 /* in any case, allow '0' as a literal */
1322 while (c == '0')
1323 c = tok_nextc(tok);
1324 while (isdigit(c)) {
1325 nonzero = 1;
1326 c = tok_nextc(tok);
Tim Petersd507dab2001-08-30 20:51:59 +00001327 }
1328 if (c == '.')
1329 goto fraction;
1330 else if (c == 'e' || c == 'E')
1331 goto exponent;
1332#ifndef WITHOUT_COMPLEX
1333 else if (c == 'j' || c == 'J')
1334 goto imaginary;
1335#endif
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001336 else if (nonzero) {
Tim Petersd507dab2001-08-30 20:51:59 +00001337 tok->done = E_TOKEN;
1338 tok_backup(tok, c);
1339 return ERRORTOKEN;
1340 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001341 }
1342 }
1343 else {
1344 /* Decimal */
1345 do {
1346 c = tok_nextc(tok);
1347 } while (isdigit(c));
Guido van Rossume2a383d2007-01-15 16:59:06 +00001348 {
Tim Peters9aa70d92001-08-27 19:19:28 +00001349 /* Accept floating point numbers. */
Guido van Rossumf023c461991-05-05 20:16:20 +00001350 if (c == '.') {
1351 fraction:
1352 /* Fraction */
1353 do {
1354 c = tok_nextc(tok);
1355 } while (isdigit(c));
1356 }
1357 if (c == 'e' || c == 'E') {
Tim Petersd507dab2001-08-30 20:51:59 +00001358 exponent:
Guido van Rossumf023c461991-05-05 20:16:20 +00001359 /* Exponent part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001360 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001361 if (c == '+' || c == '-')
1362 c = tok_nextc(tok);
Tim Peters9aa70d92001-08-27 19:19:28 +00001363 if (!isdigit(c)) {
1364 tok->done = E_TOKEN;
1365 tok_backup(tok, c);
1366 return ERRORTOKEN;
Guido van Rossumf023c461991-05-05 20:16:20 +00001367 }
Tim Peters9aa70d92001-08-27 19:19:28 +00001368 do {
1369 c = tok_nextc(tok);
1370 } while (isdigit(c));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001371 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001372#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001373 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001374 /* Imaginary part */
1375 imaginary:
1376 c = tok_nextc(tok);
1377#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001378 }
1379 }
1380 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001381 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001382 *p_end = tok->cur;
1383 return NUMBER;
1384 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001385
1386 letter_quote:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001387 /* String */
1388 if (c == '\'' || c == '"') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001389 Py_ssize_t quote2 = tok->cur - tok->start + 1;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001390 int quote = c;
1391 int triple = 0;
1392 int tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001393 for (;;) {
1394 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001395 if (c == '\n') {
1396 if (!triple) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001397 tok->done = E_EOLS;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001398 tok_backup(tok, c);
1399 return ERRORTOKEN;
1400 }
1401 tripcount = 0;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001402 tok->cont_line = 1; /* multiline string. */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001403 }
1404 else if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001405 if (triple)
1406 tok->done = E_EOFS;
1407 else
1408 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001409 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001410 return ERRORTOKEN;
1411 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001412 else if (c == quote) {
1413 tripcount++;
Guido van Rossum35685241998-02-16 15:42:50 +00001414 if (tok->cur - tok->start == quote2) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001415 c = tok_nextc(tok);
1416 if (c == quote) {
1417 triple = 1;
1418 tripcount = 0;
1419 continue;
1420 }
1421 tok_backup(tok, c);
1422 }
1423 if (!triple || tripcount == 3)
1424 break;
1425 }
1426 else if (c == '\\') {
1427 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001428 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001429 if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001430 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001431 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001432 return ERRORTOKEN;
1433 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001434 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001435 else
1436 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001437 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001438 *p_start = tok->start;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001439 *p_end = tok->cur;
1440 return STRING;
1441 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001442
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001443 /* Line continuation */
1444 if (c == '\\') {
1445 c = tok_nextc(tok);
1446 if (c != '\n') {
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001447 tok->done = E_LINECONT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001448 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001449 return ERRORTOKEN;
1450 }
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001451 tok->cont_line = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001452 goto again; /* Read next line */
1453 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001454
Guido van Rossumfbab9051991-10-20 20:25:03 +00001455 /* Check for two-character token */
1456 {
1457 int c2 = tok_nextc(tok);
Guido van Rossum86bea461997-04-29 21:03:06 +00001458 int token = PyToken_TwoChars(c, c2);
Guido van Rossumfbab9051991-10-20 20:25:03 +00001459 if (token != OP) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001460 int c3 = tok_nextc(tok);
1461 int token3 = PyToken_ThreeChars(c, c2, c3);
1462 if (token3 != OP) {
1463 token = token3;
1464 } else {
1465 tok_backup(tok, c3);
1466 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001467 *p_start = tok->start;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001468 *p_end = tok->cur;
1469 return token;
1470 }
1471 tok_backup(tok, c2);
1472 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001473
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001474 /* Keep track of parentheses nesting level */
Guido van Rossuma849b831993-05-12 11:35:44 +00001475 switch (c) {
1476 case '(':
1477 case '[':
1478 case '{':
1479 tok->level++;
1480 break;
1481 case ')':
1482 case ']':
1483 case '}':
1484 tok->level--;
1485 break;
1486 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001487
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001488 /* Punctuation character */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001489 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001490 *p_end = tok->cur;
Guido van Rossum86bea461997-04-29 21:03:06 +00001491 return PyToken_OneChar(c);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001492}
1493
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001494int
1495PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
1496{
1497 int result = tok_get(tok, p_start, p_end);
1498 if (tok->decoding_erred) {
1499 result = ERRORTOKEN;
1500 tok->done = E_DECODE;
1501 }
1502 return result;
1503}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001504
Guido van Rossum408027e1996-12-30 16:17:54 +00001505#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001506
1507void
Thomas Wouters23c9e002000-07-22 19:20:54 +00001508tok_dump(int type, char *start, char *end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001509{
Guido van Rossum86bea461997-04-29 21:03:06 +00001510 printf("%s", _PyParser_TokenNames[type]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001511 if (type == NAME || type == NUMBER || type == STRING || type == OP)
1512 printf("(%.*s)", (int)(end - start), start);
1513}
1514
1515#endif