blob: bbfbe7d3452c973e79f694814efa446ab43c3aaa [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"
Christian Heimes729ab152007-11-23 09:10:36 +000019#include "pydebug.h"
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +000020#endif /* PGEN */
21
Martin v. Löwis566f6af2002-10-26 14:39:10 +000022extern char *PyOS_Readline(FILE *, FILE *, char *);
Guido van Rossumf4b1a641994-08-29 12:43:07 +000023/* Return malloc'ed string including trailing \n;
24 empty malloc'ed string for EOF;
25 NULL if interrupted */
26
Guido van Rossum4fe87291992-02-26 15:24:44 +000027/* Don't ever change this -- it would break the portability of Python code */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028#define TABSIZE 8
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029
Guido van Rossumcf57d8b1998-01-19 22:07:46 +000030/* Convert a possibly signed character to a nonnegative int */
31/* XXX This assumes characters are 8 bits wide */
32#ifdef __CHAR_UNSIGNED__
33#define Py_CHARMASK(c) (c)
34#else
35#define Py_CHARMASK(c) ((c) & 0xff)
36#endif
37
Guido van Rossum3f5da241990-12-20 15:06:42 +000038/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000039static struct tok_state *tok_new(void);
40static int tok_nextc(struct tok_state *tok);
41static void tok_backup(struct tok_state *tok, int c);
Guido van Rossum3f5da241990-12-20 15:06:42 +000042
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043/* Token names */
44
Guido van Rossum86bea461997-04-29 21:03:06 +000045char *_PyParser_TokenNames[] = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046 "ENDMARKER",
47 "NAME",
48 "NUMBER",
49 "STRING",
50 "NEWLINE",
51 "INDENT",
52 "DEDENT",
53 "LPAR",
54 "RPAR",
55 "LSQB",
56 "RSQB",
57 "COLON",
58 "COMMA",
59 "SEMI",
60 "PLUS",
61 "MINUS",
62 "STAR",
63 "SLASH",
64 "VBAR",
65 "AMPER",
66 "LESS",
67 "GREATER",
68 "EQUAL",
69 "DOT",
70 "PERCENT",
71 "BACKQUOTE",
72 "LBRACE",
73 "RBRACE",
Guido van Rossumfbab9051991-10-20 20:25:03 +000074 "EQEQUAL",
75 "NOTEQUAL",
76 "LESSEQUAL",
77 "GREATEREQUAL",
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +000078 "TILDE",
79 "CIRCUMFLEX",
80 "LEFTSHIFT",
81 "RIGHTSHIFT",
Guido van Rossumf595fde1996-01-12 01:31:58 +000082 "DOUBLESTAR",
Thomas Wouters434d0822000-08-24 20:11:32 +000083 "PLUSEQUAL",
84 "MINEQUAL",
85 "STAREQUAL",
86 "SLASHEQUAL",
87 "PERCENTEQUAL",
88 "AMPEREQUAL",
89 "VBAREQUAL",
90 "CIRCUMFLEXEQUAL",
91 "LEFTSHIFTEQUAL",
92 "RIGHTSHIFTEQUAL",
93 "DOUBLESTAREQUAL",
Guido van Rossum4668b002001-08-08 05:00:18 +000094 "DOUBLESLASH",
95 "DOUBLESLASHEQUAL",
Anthony Baxterc2a5a632004-08-02 06:10:11 +000096 "AT",
Guido van Rossumfbab9051991-10-20 20:25:03 +000097 /* This table must match the #defines in token.h! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098 "OP",
99 "<ERRORTOKEN>",
100 "<N_TOKENS>"
101};
102
103
104/* Create and initialize a new tok_state structure */
105
106static struct tok_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000107tok_new(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108{
Anthony Baxter11490022006-04-11 05:39:14 +0000109 struct tok_state *tok = (struct tok_state *)PyMem_MALLOC(
110 sizeof(struct tok_state));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 if (tok == NULL)
112 return NULL;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000113 tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 tok->done = E_OK;
115 tok->fp = NULL;
116 tok->tabsize = TABSIZE;
117 tok->indent = 0;
118 tok->indstack[0] = 0;
119 tok->atbol = 1;
120 tok->pendin = 0;
121 tok->prompt = tok->nextprompt = NULL;
122 tok->lineno = 0;
Guido van Rossuma849b831993-05-12 11:35:44 +0000123 tok->level = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +0000124 tok->filename = NULL;
125 tok->altwarning = 0;
126 tok->alterror = 0;
127 tok->alttabsize = 1;
128 tok->altindstack[0] = 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000129 tok->decoding_state = 0;
130 tok->decoding_erred = 0;
131 tok->read_coding_spec = 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000132 tok->encoding = NULL;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000133 tok->cont_line = 0;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000134#ifndef PGEN
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000135 tok->decoding_readline = NULL;
136 tok->decoding_buffer = NULL;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000137#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 return tok;
139}
140
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000141#ifdef PGEN
142
143static char *
144decoding_fgets(char *s, int size, struct tok_state *tok)
145{
146 return fgets(s, size, tok->fp);
147}
148
149static int
150decoding_feof(struct tok_state *tok)
151{
152 return feof(tok->fp);
153}
154
155static const char *
156decode_str(const char *str, struct tok_state *tok)
157{
158 return str;
159}
160
161#else /* PGEN */
162
163static char *
164error_ret(struct tok_state *tok) /* XXX */
165{
166 tok->decoding_erred = 1;
167 if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
Neal Norwitz08062d62006-04-11 08:19:15 +0000168 PyMem_FREE(tok->buf);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000169 tok->buf = NULL;
170 return NULL; /* as if it were EOF */
171}
172
173static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000174new_string(const char *s, Py_ssize_t len)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000175{
Neal Norwitz08062d62006-04-11 08:19:15 +0000176 char* result = (char *)PyMem_MALLOC(len + 1);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000177 if (result != NULL) {
178 memcpy(result, s, len);
179 result[len] = '\0';
180 }
181 return result;
182}
183
184static char *
185get_normal_name(char *s) /* for utf-8 and latin-1 */
186{
187 char buf[13];
188 int i;
189 for (i = 0; i < 12; i++) {
190 int c = s[i];
191 if (c == '\0') break;
192 else if (c == '_') buf[i] = '-';
193 else buf[i] = tolower(c);
194 }
195 buf[i] = '\0';
196 if (strcmp(buf, "utf-8") == 0 ||
197 strncmp(buf, "utf-8-", 6) == 0) return "utf-8";
198 else if (strcmp(buf, "latin-1") == 0 ||
199 strcmp(buf, "iso-8859-1") == 0 ||
200 strcmp(buf, "iso-latin-1") == 0 ||
201 strncmp(buf, "latin-1-", 8) == 0 ||
202 strncmp(buf, "iso-8859-1-", 11) == 0 ||
203 strncmp(buf, "iso-latin-1-", 12) == 0) return "iso-8859-1";
204 else return s;
205}
206
207/* Return the coding spec in S, or NULL if none is found. */
208
209static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000210get_coding_spec(const char *s, Py_ssize_t size)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000211{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000212 Py_ssize_t i;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000213 /* Coding spec must be in a comment, and that comment must be
214 * the only statement on the source code line. */
215 for (i = 0; i < size - 6; i++) {
216 if (s[i] == '#')
217 break;
218 if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014')
219 return NULL;
220 }
221 for (; i < size - 6; i++) { /* XXX inefficient search */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000222 const char* t = s + i;
223 if (strncmp(t, "coding", 6) == 0) {
224 const char* begin = NULL;
225 t += 6;
226 if (t[0] != ':' && t[0] != '=')
227 continue;
228 do {
229 t++;
230 } while (t[0] == '\x20' || t[0] == '\t');
231
232 begin = t;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000233 while (isalnum(Py_CHARMASK(t[0])) ||
Neal Norwitze08e1bc2002-11-02 20:43:25 +0000234 t[0] == '-' || t[0] == '_' || t[0] == '.')
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000235 t++;
236
237 if (begin < t) {
238 char* r = new_string(begin, t - begin);
239 char* q = get_normal_name(r);
240 if (r != q) {
Neal Norwitz08062d62006-04-11 08:19:15 +0000241 PyMem_FREE(r);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000242 r = new_string(q, strlen(q));
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000243 }
244 return r;
245 }
246 }
247 }
248 return NULL;
249}
250
251/* Check whether the line contains a coding spec. If it does,
252 invoke the set_readline function for the new encoding.
253 This function receives the tok_state and the new encoding.
254 Return 1 on success, 0 on failure. */
255
256static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000257check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000258 int set_readline(struct tok_state *, const char *))
259{
Tim Peters17db21f2002-09-03 15:39:58 +0000260 char * cs;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000261 int r = 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000262
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000263 if (tok->cont_line)
264 /* It's a continuation line, so it can't be a coding spec. */
265 return 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000266 cs = get_coding_spec(line, size);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000267 if (cs != NULL) {
268 tok->read_coding_spec = 1;
269 if (tok->encoding == NULL) {
270 assert(tok->decoding_state == 1); /* raw */
271 if (strcmp(cs, "utf-8") == 0 ||
272 strcmp(cs, "iso-8859-1") == 0) {
273 tok->encoding = cs;
274 } else {
Martin v. Löwis019934b2002-08-07 12:33:18 +0000275#ifdef Py_USING_UNICODE
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000276 r = set_readline(tok, cs);
277 if (r) {
278 tok->encoding = cs;
279 tok->decoding_state = -1;
280 }
Neal Norwitzc0d5faa2005-10-21 06:05:33 +0000281 else
Neal Norwitz08062d62006-04-11 08:19:15 +0000282 PyMem_FREE(cs);
Martin v. Löwis019934b2002-08-07 12:33:18 +0000283#else
284 /* Without Unicode support, we cannot
285 process the coding spec. Since there
286 won't be any Unicode literals, that
287 won't matter. */
Neal Norwitz08062d62006-04-11 08:19:15 +0000288 PyMem_FREE(cs);
Martin v. Löwis019934b2002-08-07 12:33:18 +0000289#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000290 }
291 } else { /* then, compare cs with BOM */
292 r = (strcmp(tok->encoding, cs) == 0);
Neal Norwitz08062d62006-04-11 08:19:15 +0000293 PyMem_FREE(cs);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000294 }
295 }
Neal Norwitzdb83eb32005-12-18 05:29:30 +0000296 if (!r) {
297 cs = tok->encoding;
298 if (!cs)
299 cs = "with BOM";
300 PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
301 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000302 return r;
303}
304
305/* See whether the file starts with a BOM. If it does,
306 invoke the set_readline function with the new encoding.
307 Return 1 on success, 0 on failure. */
308
309static int
310check_bom(int get_char(struct tok_state *),
311 void unget_char(int, struct tok_state *),
312 int set_readline(struct tok_state *, const char *),
313 struct tok_state *tok)
314{
315 int ch = get_char(tok);
316 tok->decoding_state = 1;
317 if (ch == EOF) {
318 return 1;
319 } else if (ch == 0xEF) {
320 ch = get_char(tok); if (ch != 0xBB) goto NON_BOM;
321 ch = get_char(tok); if (ch != 0xBF) goto NON_BOM;
322#if 0
323 /* Disable support for UTF-16 BOMs until a decision
324 is made whether this needs to be supported. */
325 } else if (ch == 0xFE) {
326 ch = get_char(tok); if (ch != 0xFF) goto NON_BOM;
327 if (!set_readline(tok, "utf-16-be")) return 0;
328 tok->decoding_state = -1;
329 } else if (ch == 0xFF) {
330 ch = get_char(tok); if (ch != 0xFE) goto NON_BOM;
331 if (!set_readline(tok, "utf-16-le")) return 0;
332 tok->decoding_state = -1;
333#endif
334 } else {
335 unget_char(ch, tok);
336 return 1;
337 }
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000338 if (tok->encoding != NULL)
Neal Norwitz08062d62006-04-11 08:19:15 +0000339 PyMem_FREE(tok->encoding);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000340 tok->encoding = new_string("utf-8", 5); /* resulting is in utf-8 */
341 return 1;
342 NON_BOM:
343 /* any token beginning with '\xEF', '\xFE', '\xFF' is a bad token */
344 unget_char(0xFF, tok); /* XXX this will cause a syntax error */
345 return 1;
346}
347
348/* Read a line of text from TOK into S, using the stream in TOK.
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000349 Return NULL on failure, else S.
Tim Petersc9d78aa2006-03-26 23:27:58 +0000350
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000351 On entry, tok->decoding_buffer will be one of:
352 1) NULL: need to call tok->decoding_readline to get a new line
353 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
354 stored the result in tok->decoding_buffer
355 3) PyStringObject *: previous call to fp_readl did not have enough room
356 (in the s buffer) to copy entire contents of the line read
357 by tok->decoding_readline. tok->decoding_buffer has the overflow.
358 In this case, fp_readl is called in a loop (with an expanded buffer)
Tim Petersc9d78aa2006-03-26 23:27:58 +0000359 until the buffer ends with a '\n' (or until the end of the file is
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000360 reached): see tok_nextc and its calls to decoding_fgets.
361*/
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000362
363static char *
364fp_readl(char *s, int size, struct tok_state *tok)
365{
Martin v. Löwis019934b2002-08-07 12:33:18 +0000366#ifndef Py_USING_UNICODE
367 /* In a non-Unicode built, this should never be called. */
Martin v. Löwis2863c102002-08-07 15:18:57 +0000368 Py_FatalError("fp_readl should not be called in this build.");
Guido van Rossum84b2bed2002-08-16 17:01:09 +0000369 return NULL; /* Keep compiler happy (not reachable) */
Martin v. Löwis019934b2002-08-07 12:33:18 +0000370#else
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000371 PyObject* utf8 = NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000372 PyObject* buf = tok->decoding_buffer;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000373 char *str;
Martin v. Löwisf5adf1e2006-02-16 14:35:38 +0000374 Py_ssize_t utf8len;
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
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000380 if (buf == NULL) {
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000381 buf = PyObject_CallObject(tok->decoding_readline, NULL);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000382 if (buf == NULL)
383 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000384 } else {
385 tok->decoding_buffer = NULL;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000386 if (PyString_CheckExact(buf))
387 utf8 = buf;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000388 }
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000389 if (utf8 == NULL) {
390 utf8 = PyUnicode_AsUTF8String(buf);
391 Py_DECREF(buf);
392 if (utf8 == NULL)
393 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000394 }
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000395 str = PyString_AsString(utf8);
396 utf8len = PyString_GET_SIZE(utf8);
397 if (utf8len > size) {
398 tok->decoding_buffer = PyString_FromStringAndSize(str+size, utf8len-size);
399 if (tok->decoding_buffer == NULL) {
400 Py_DECREF(utf8);
401 return error_ret(tok);
402 }
403 utf8len = size;
404 }
405 memcpy(s, str, utf8len);
406 s[utf8len] = '\0';
407 Py_DECREF(utf8);
408 if (utf8len == 0) return NULL; /* EOF */
409 return s;
Martin v. Löwis019934b2002-08-07 12:33:18 +0000410#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000411}
412
413/* Set the readline function for TOK to a StreamReader's
414 readline function. The StreamReader is named ENC.
415
416 This function is called from check_bom and check_coding_spec.
417
418 ENC is usually identical to the future value of tok->encoding,
419 except for the (currently unsupported) case of UTF-16.
420
421 Return 1 on success, 0 on failure. */
422
423static int
424fp_setreadl(struct tok_state *tok, const char* enc)
425{
426 PyObject *reader, *stream, *readline;
427
Martin v. Löwis95292d62002-12-11 14:04:59 +0000428 /* XXX: constify filename argument. */
429 stream = PyFile_FromFile(tok->fp, (char*)tok->filename, "rb", NULL);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000430 if (stream == NULL)
431 return 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000432
433 reader = PyCodec_StreamReader(enc, stream, NULL);
434 Py_DECREF(stream);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000435 if (reader == NULL)
436 return 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000437
438 readline = PyObject_GetAttrString(reader, "readline");
439 Py_DECREF(reader);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000440 if (readline == NULL)
441 return 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000442
443 tok->decoding_readline = readline;
444 return 1;
445}
446
447/* Fetch the next byte from TOK. */
448
449static int fp_getc(struct tok_state *tok) {
450 return getc(tok->fp);
451}
452
453/* Unfetch the last byte back into TOK. */
454
455static void fp_ungetc(int c, struct tok_state *tok) {
456 ungetc(c, tok->fp);
457}
458
459/* Read a line of input from TOK. Determine encoding
460 if necessary. */
461
462static char *
463decoding_fgets(char *s, int size, struct tok_state *tok)
464{
Martin v. Löwis2863c102002-08-07 15:18:57 +0000465 char *line = NULL;
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000466 int badchar = 0;
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000467 for (;;) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000468 if (tok->decoding_state < 0) {
469 /* We already have a codec associated with
470 this input. */
471 line = fp_readl(s, size, tok);
472 break;
473 } else if (tok->decoding_state > 0) {
474 /* We want a 'raw' read. */
Tim Petersc9d78aa2006-03-26 23:27:58 +0000475 line = Py_UniversalNewlineFgets(s, size,
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000476 tok->fp, NULL);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000477 break;
478 } else {
479 /* We have not yet determined the encoding.
480 If an encoding is found, use the file-pointer
481 reader functions from now on. */
482 if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok))
483 return error_ret(tok);
484 assert(tok->decoding_state != 0);
485 }
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000486 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000487 if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) {
488 if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) {
489 return error_ret(tok);
490 }
491 }
492#ifndef PGEN
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000493 /* The default encoding is ASCII, so make sure we don't have any
494 non-ASCII bytes in it. */
495 if (line && !tok->encoding) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000496 unsigned char *c;
Jack Jansencf0a2cf2002-08-05 14:14:05 +0000497 for (c = (unsigned char *)line; *c; c++)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000498 if (*c > 127) {
499 badchar = *c;
500 break;
501 }
502 }
503 if (badchar) {
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000504 char buf[500];
Martin v. Löwis725bb232002-08-05 01:49:16 +0000505 /* Need to add 1 to the line number, since this line
506 has not been counted, yet. */
Tim Petersc9d78aa2006-03-26 23:27:58 +0000507 sprintf(buf,
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000508 "Non-ASCII character '\\x%.2x' "
509 "in file %.200s on line %i, "
510 "but no encoding declared; "
Tim Petersc9d78aa2006-03-26 23:27:58 +0000511 "see http://www.python.org/peps/pep-0263.html for details",
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000512 badchar, tok->filename, tok->lineno + 1);
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000513 PyErr_SetString(PyExc_SyntaxError, buf);
514 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000515 }
516#endif
517 return line;
518}
519
520static int
521decoding_feof(struct tok_state *tok)
522{
523 if (tok->decoding_state >= 0) {
524 return feof(tok->fp);
525 } else {
526 PyObject* buf = tok->decoding_buffer;
527 if (buf == NULL) {
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000528 buf = PyObject_CallObject(tok->decoding_readline, NULL);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000529 if (buf == NULL) {
530 error_ret(tok);
531 return 1;
532 } else {
533 tok->decoding_buffer = buf;
534 }
535 }
536 return PyObject_Length(buf) == 0;
537 }
538}
539
540/* Fetch a byte from TOK, using the string buffer. */
541
Tim Petersc9d78aa2006-03-26 23:27:58 +0000542static int
543buf_getc(struct tok_state *tok) {
Just van Rossumf032f862003-02-09 20:38:48 +0000544 return Py_CHARMASK(*tok->str++);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000545}
546
547/* Unfetch a byte from TOK, using the string buffer. */
548
Tim Petersc9d78aa2006-03-26 23:27:58 +0000549static void
550buf_ungetc(int c, struct tok_state *tok) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000551 tok->str--;
Just van Rossumf032f862003-02-09 20:38:48 +0000552 assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000553}
554
555/* Set the readline function for TOK to ENC. For the string-based
556 tokenizer, this means to just record the encoding. */
557
Tim Petersc9d78aa2006-03-26 23:27:58 +0000558static int
559buf_setreadl(struct tok_state *tok, const char* enc) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000560 tok->enc = enc;
561 return 1;
562}
563
564/* Return a UTF-8 encoding Python string object from the
565 C byte string STR, which is encoded with ENC. */
566
Martin v. Löwis019934b2002-08-07 12:33:18 +0000567#ifdef Py_USING_UNICODE
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000568static PyObject *
569translate_into_utf8(const char* str, const char* enc) {
570 PyObject *utf8;
571 PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL);
572 if (buf == NULL)
573 return NULL;
574 utf8 = PyUnicode_AsUTF8String(buf);
575 Py_DECREF(buf);
576 return utf8;
577}
Martin v. Löwis019934b2002-08-07 12:33:18 +0000578#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000579
580/* Decode a byte string STR for use as the buffer of TOK.
581 Look for encoding declarations inside STR, and record them
582 inside TOK. */
583
584static const char *
585decode_str(const char *str, struct tok_state *tok)
586{
587 PyObject* utf8 = NULL;
588 const char *s;
Georg Brandl38d17152008-01-21 18:35:49 +0000589 char *newl[2] = {NULL, NULL};
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000590 int lineno = 0;
591 tok->enc = NULL;
592 tok->str = str;
593 if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000594 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000595 str = tok->str; /* string after BOM if any */
Tim Peters2c3f9c62002-08-04 17:58:34 +0000596 assert(str);
Martin v. Löwis019934b2002-08-07 12:33:18 +0000597#ifdef Py_USING_UNICODE
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000598 if (tok->enc != NULL) {
599 utf8 = translate_into_utf8(str, tok->enc);
600 if (utf8 == NULL)
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000601 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000602 str = PyString_AsString(utf8);
603 }
Martin v. Löwis019934b2002-08-07 12:33:18 +0000604#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000605 for (s = str;; s++) {
606 if (*s == '\0') break;
607 else if (*s == '\n') {
Georg Brandl38d17152008-01-21 18:35:49 +0000608 newl[lineno] = s;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000609 lineno++;
610 if (lineno == 2) break;
611 }
612 }
613 tok->enc = NULL;
Georg Brandl38d17152008-01-21 18:35:49 +0000614 /* need to check line 1 and 2 separately since check_coding_spec
615 assumes a single line as input */
616 if (newl[0]) {
617 if (!check_coding_spec(str, newl[0] - str, tok, buf_setreadl))
618 return error_ret(tok);
619 if (tok->enc == NULL && newl[1]) {
620 if (!check_coding_spec(newl[0]+1, newl[1] - newl[0],
621 tok, buf_setreadl))
622 return error_ret(tok);
623 }
624 }
Martin v. Löwis019934b2002-08-07 12:33:18 +0000625#ifdef Py_USING_UNICODE
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000626 if (tok->enc != NULL) {
627 assert(utf8 == NULL);
628 utf8 = translate_into_utf8(str, tok->enc);
Neal Norwitz40d37812005-10-02 01:48:49 +0000629 if (utf8 == NULL) {
630 PyErr_Format(PyExc_SyntaxError,
631 "unknown encoding: %s", tok->enc);
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000632 return error_ret(tok);
Neal Norwitz40d37812005-10-02 01:48:49 +0000633 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000634 str = PyString_AsString(utf8);
635 }
Martin v. Löwis019934b2002-08-07 12:33:18 +0000636#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000637 assert(tok->decoding_buffer == NULL);
638 tok->decoding_buffer = utf8; /* CAUTION */
639 return str;
640}
641
642#endif /* PGEN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643
644/* Set up tokenizer for string */
645
646struct tok_state *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000647PyTokenizer_FromString(const char *str)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648{
649 struct tok_state *tok = tok_new();
650 if (tok == NULL)
651 return NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000652 str = (char *)decode_str(str, tok);
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000653 if (str == NULL) {
654 PyTokenizer_Free(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000655 return NULL;
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000656 }
657
Martin v. Löwis95292d62002-12-11 14:04:59 +0000658 /* XXX: constify members. */
659 tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660 return tok;
661}
662
663
Guido van Rossum8c11a5c1991-07-27 21:42:56 +0000664/* Set up tokenizer for file */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665
666struct tok_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000667PyTokenizer_FromFile(FILE *fp, char *ps1, char *ps2)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000668{
669 struct tok_state *tok = tok_new();
670 if (tok == NULL)
671 return NULL;
Neal Norwitz08062d62006-04-11 08:19:15 +0000672 if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) {
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000673 PyTokenizer_Free(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000674 return NULL;
675 }
676 tok->cur = tok->inp = tok->buf;
677 tok->end = tok->buf + BUFSIZ;
678 tok->fp = fp;
679 tok->prompt = ps1;
680 tok->nextprompt = ps2;
681 return tok;
682}
683
684
685/* Free a tok_state structure */
686
687void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000688PyTokenizer_Free(struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000689{
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000690 if (tok->encoding != NULL)
Neal Norwitz08062d62006-04-11 08:19:15 +0000691 PyMem_FREE(tok->encoding);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000692#ifndef PGEN
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000693 Py_XDECREF(tok->decoding_readline);
694 Py_XDECREF(tok->decoding_buffer);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000695#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000696 if (tok->fp != NULL && tok->buf != NULL)
Neal Norwitz08062d62006-04-11 08:19:15 +0000697 PyMem_FREE(tok->buf);
Tim Petersc9d78aa2006-03-26 23:27:58 +0000698 PyMem_FREE(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699}
700
Hye-Shik Chang7df44b32004-08-04 17:36:41 +0000701#if !defined(PGEN) && defined(Py_USING_UNICODE)
702static int
703tok_stdin_decode(struct tok_state *tok, char **inp)
704{
705 PyObject *enc, *sysstdin, *decoded, *utf8;
706 const char *encoding;
707 char *converted;
708
709 if (PySys_GetFile((char *)"stdin", NULL) != stdin)
710 return 0;
711 sysstdin = PySys_GetObject("stdin");
712 if (sysstdin == NULL || !PyFile_Check(sysstdin))
713 return 0;
714
715 enc = ((PyFileObject *)sysstdin)->f_encoding;
716 if (enc == NULL || !PyString_Check(enc))
717 return 0;
718 Py_INCREF(enc);
719
720 encoding = PyString_AsString(enc);
721 decoded = PyUnicode_Decode(*inp, strlen(*inp), encoding, NULL);
722 if (decoded == NULL)
723 goto error_clear;
724
725 utf8 = PyUnicode_AsEncodedString(decoded, "utf-8", NULL);
726 Py_DECREF(decoded);
727 if (utf8 == NULL)
728 goto error_clear;
729
Neal Norwitz2aa9a5d2006-03-20 01:53:23 +0000730 assert(PyString_Check(utf8));
731 converted = new_string(PyString_AS_STRING(utf8),
732 PyString_GET_SIZE(utf8));
Hye-Shik Chang7df44b32004-08-04 17:36:41 +0000733 Py_DECREF(utf8);
734 if (converted == NULL)
735 goto error_nomem;
736
Neal Norwitz08062d62006-04-11 08:19:15 +0000737 PyMem_FREE(*inp);
Hye-Shik Chang7df44b32004-08-04 17:36:41 +0000738 *inp = converted;
739 if (tok->encoding != NULL)
Neal Norwitz08062d62006-04-11 08:19:15 +0000740 PyMem_FREE(tok->encoding);
Hye-Shik Chang7df44b32004-08-04 17:36:41 +0000741 tok->encoding = new_string(encoding, strlen(encoding));
742 if (tok->encoding == NULL)
743 goto error_nomem;
744
745 Py_DECREF(enc);
746 return 0;
747
748error_nomem:
749 Py_DECREF(enc);
750 tok->done = E_NOMEM;
751 return -1;
752
753error_clear:
754 /* Fallback to iso-8859-1: for backward compatibility */
755 Py_DECREF(enc);
756 PyErr_Clear();
757 return 0;
758}
759#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000760
761/* Get next char, updating state; error code goes into tok->done */
762
763static int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000764tok_nextc(register struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000765{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000766 for (;;) {
Guido van Rossum1a817c01994-09-19 08:06:25 +0000767 if (tok->cur != tok->inp) {
Guido van Rossumcf57d8b1998-01-19 22:07:46 +0000768 return Py_CHARMASK(*tok->cur++); /* Fast path */
Guido van Rossum1a817c01994-09-19 08:06:25 +0000769 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000770 if (tok->done != E_OK)
771 return EOF;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772 if (tok->fp == NULL) {
Guido van Rossum1a817c01994-09-19 08:06:25 +0000773 char *end = strchr(tok->inp, '\n');
774 if (end != NULL)
775 end++;
776 else {
777 end = strchr(tok->inp, '\0');
778 if (end == tok->inp) {
779 tok->done = E_EOF;
780 return EOF;
781 }
782 }
783 if (tok->start == NULL)
784 tok->buf = tok->cur;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000785 tok->line_start = tok->cur;
Guido van Rossum1a817c01994-09-19 08:06:25 +0000786 tok->lineno++;
787 tok->inp = end;
Guido van Rossumcf57d8b1998-01-19 22:07:46 +0000788 return Py_CHARMASK(*tok->cur++);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790 if (tok->prompt != NULL) {
Anthony Baxter11490022006-04-11 05:39:14 +0000791 char *newtok = PyOS_Readline(stdin, stdout, tok->prompt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792 if (tok->nextprompt != NULL)
793 tok->prompt = tok->nextprompt;
Anthony Baxter11490022006-04-11 05:39:14 +0000794 if (newtok == NULL)
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000795 tok->done = E_INTR;
Anthony Baxter11490022006-04-11 05:39:14 +0000796 else if (*newtok == '\0') {
Neal Norwitz08062d62006-04-11 08:19:15 +0000797 PyMem_FREE(newtok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000798 tok->done = E_EOF;
799 }
Hye-Shik Chang7df44b32004-08-04 17:36:41 +0000800#if !defined(PGEN) && defined(Py_USING_UNICODE)
Anthony Baxter11490022006-04-11 05:39:14 +0000801 else if (tok_stdin_decode(tok, &newtok) != 0)
Neal Norwitz08062d62006-04-11 08:19:15 +0000802 PyMem_FREE(newtok);
Hye-Shik Chang7df44b32004-08-04 17:36:41 +0000803#endif
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000804 else if (tok->start != NULL) {
Guido van Rossum6da34342000-06-28 22:00:02 +0000805 size_t start = tok->start - tok->buf;
806 size_t oldlen = tok->cur - tok->buf;
Anthony Baxter11490022006-04-11 05:39:14 +0000807 size_t newlen = oldlen + strlen(newtok);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000808 char *buf = tok->buf;
Neal Norwitz08062d62006-04-11 08:19:15 +0000809 buf = (char *)PyMem_REALLOC(buf, newlen+1);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000810 tok->lineno++;
811 if (buf == NULL) {
Neal Norwitz08062d62006-04-11 08:19:15 +0000812 PyMem_FREE(tok->buf);
Guido van Rossum588633d1994-12-30 15:46:02 +0000813 tok->buf = NULL;
Neal Norwitz08062d62006-04-11 08:19:15 +0000814 PyMem_FREE(newtok);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000815 tok->done = E_NOMEM;
816 return EOF;
817 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000818 tok->buf = buf;
819 tok->cur = tok->buf + oldlen;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000820 tok->line_start = tok->cur;
Anthony Baxter11490022006-04-11 05:39:14 +0000821 strcpy(tok->buf + oldlen, newtok);
Neal Norwitz08062d62006-04-11 08:19:15 +0000822 PyMem_FREE(newtok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000823 tok->inp = tok->buf + newlen;
824 tok->end = tok->inp + 1;
825 tok->start = tok->buf + start;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000826 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000827 else {
828 tok->lineno++;
829 if (tok->buf != NULL)
Neal Norwitz08062d62006-04-11 08:19:15 +0000830 PyMem_FREE(tok->buf);
Anthony Baxter11490022006-04-11 05:39:14 +0000831 tok->buf = newtok;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000832 tok->line_start = tok->buf;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000833 tok->cur = tok->buf;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000834 tok->line_start = tok->buf;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000835 tok->inp = strchr(tok->buf, '\0');
836 tok->end = tok->inp + 1;
837 }
838 }
839 else {
840 int done = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000841 Py_ssize_t cur = 0;
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000842 char *pt;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000843 if (tok->start == NULL) {
844 if (tok->buf == NULL) {
Tim Petersc9d78aa2006-03-26 23:27:58 +0000845 tok->buf = (char *)
Neal Norwitz08062d62006-04-11 08:19:15 +0000846 PyMem_MALLOC(BUFSIZ);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000847 if (tok->buf == NULL) {
848 tok->done = E_NOMEM;
849 return EOF;
850 }
851 tok->end = tok->buf + BUFSIZ;
852 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000853 if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
854 tok) == NULL) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000855 tok->done = E_EOF;
856 done = 1;
857 }
858 else {
859 tok->done = E_OK;
860 tok->inp = strchr(tok->buf, '\0');
861 done = tok->inp[-1] == '\n';
862 }
863 }
864 else {
865 cur = tok->cur - tok->buf;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000866 if (decoding_feof(tok)) {
Guido van Rossum78c05351995-01-17 16:12:13 +0000867 tok->done = E_EOF;
868 done = 1;
869 }
870 else
871 tok->done = E_OK;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000872 }
873 tok->lineno++;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000874 /* Read until '\n' or EOF */
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000875 while (!done) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000876 Py_ssize_t curstart = tok->start == NULL ? -1 :
877 tok->start - tok->buf;
878 Py_ssize_t curvalid = tok->inp - tok->buf;
879 Py_ssize_t newsize = curvalid + BUFSIZ;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000880 char *newbuf = tok->buf;
Neal Norwitz08062d62006-04-11 08:19:15 +0000881 newbuf = (char *)PyMem_REALLOC(newbuf,
882 newsize);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000883 if (newbuf == NULL) {
884 tok->done = E_NOMEM;
885 tok->cur = tok->inp;
886 return EOF;
887 }
888 tok->buf = newbuf;
889 tok->inp = tok->buf + curvalid;
890 tok->end = tok->buf + newsize;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000891 tok->start = curstart < 0 ? NULL :
892 tok->buf + curstart;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000893 if (decoding_fgets(tok->inp,
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000894 (int)(tok->end - tok->inp),
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000895 tok) == NULL) {
Thomas Wouters7eaf2aa2006-03-02 20:41:27 +0000896 /* Break out early on decoding
897 errors, as tok->buf will be NULL
898 */
899 if (tok->decoding_erred)
900 return EOF;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000901 /* Last line does not end in \n,
902 fake one */
903 strcpy(tok->inp, "\n");
904 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000905 tok->inp = strchr(tok->inp, '\0');
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000906 done = tok->inp[-1] == '\n';
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000907 }
Neal Norwitzd21a7ff2006-06-02 06:23:00 +0000908 if (tok->buf != NULL) {
909 tok->cur = tok->buf + cur;
910 tok->line_start = tok->cur;
911 /* replace "\r\n" with "\n" */
Andrew M. Kuchling9b3a8242006-10-06 18:51:55 +0000912 /* For Mac leave the \r, giving a syntax error */
Neal Norwitzd21a7ff2006-06-02 06:23:00 +0000913 pt = tok->inp - 2;
914 if (pt >= tok->buf && *pt == '\r') {
915 *pt++ = '\n';
916 *pt = '\0';
917 tok->inp = pt;
918 }
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000919 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000920 }
921 if (tok->done != E_OK) {
922 if (tok->prompt != NULL)
Guido van Rossum6e73bf41998-08-25 18:13:04 +0000923 PySys_WriteStderr("\n");
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000924 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000925 return EOF;
926 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000927 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000928 /*NOTREACHED*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000929}
930
931
932/* Back-up one character */
933
934static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000935tok_backup(register struct tok_state *tok, register int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000936{
937 if (c != EOF) {
Guido van Rossum588633d1994-12-30 15:46:02 +0000938 if (--tok->cur < tok->buf)
Guido van Rossum86bea461997-04-29 21:03:06 +0000939 Py_FatalError("tok_backup: begin of buffer");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000940 if (*tok->cur != c)
941 *tok->cur = c;
942 }
943}
944
945
946/* Return the token corresponding to a single character */
947
948int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000949PyToken_OneChar(int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000950{
951 switch (c) {
952 case '(': return LPAR;
953 case ')': return RPAR;
954 case '[': return LSQB;
955 case ']': return RSQB;
956 case ':': return COLON;
957 case ',': return COMMA;
958 case ';': return SEMI;
959 case '+': return PLUS;
960 case '-': return MINUS;
961 case '*': return STAR;
962 case '/': return SLASH;
963 case '|': return VBAR;
964 case '&': return AMPER;
965 case '<': return LESS;
966 case '>': return GREATER;
967 case '=': return EQUAL;
968 case '.': return DOT;
969 case '%': return PERCENT;
970 case '`': return BACKQUOTE;
971 case '{': return LBRACE;
972 case '}': return RBRACE;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000973 case '^': return CIRCUMFLEX;
974 case '~': return TILDE;
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000975 case '@': return AT;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000976 default: return OP;
977 }
978}
979
980
Guido van Rossumfbab9051991-10-20 20:25:03 +0000981int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000982PyToken_TwoChars(int c1, int c2)
Guido van Rossumfbab9051991-10-20 20:25:03 +0000983{
984 switch (c1) {
985 case '=':
986 switch (c2) {
987 case '=': return EQEQUAL;
988 }
989 break;
990 case '!':
991 switch (c2) {
992 case '=': return NOTEQUAL;
993 }
994 break;
995 case '<':
996 switch (c2) {
Christian Heimes02c9ab52007-11-23 12:12:02 +0000997 case '>': return NOTEQUAL;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000998 case '=': return LESSEQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000999 case '<': return LEFTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001000 }
1001 break;
1002 case '>':
1003 switch (c2) {
1004 case '=': return GREATEREQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001005 case '>': return RIGHTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001006 }
1007 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001008 case '+':
1009 switch (c2) {
1010 case '=': return PLUSEQUAL;
1011 }
1012 break;
1013 case '-':
1014 switch (c2) {
1015 case '=': return MINEQUAL;
1016 }
1017 break;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001018 case '*':
1019 switch (c2) {
1020 case '*': return DOUBLESTAR;
Thomas Wouters434d0822000-08-24 20:11:32 +00001021 case '=': return STAREQUAL;
1022 }
1023 break;
1024 case '/':
1025 switch (c2) {
Guido van Rossum4668b002001-08-08 05:00:18 +00001026 case '/': return DOUBLESLASH;
Thomas Wouters434d0822000-08-24 20:11:32 +00001027 case '=': return SLASHEQUAL;
1028 }
1029 break;
1030 case '|':
1031 switch (c2) {
1032 case '=': return VBAREQUAL;
1033 }
1034 break;
1035 case '%':
1036 switch (c2) {
1037 case '=': return PERCENTEQUAL;
1038 }
1039 break;
1040 case '&':
1041 switch (c2) {
1042 case '=': return AMPEREQUAL;
1043 }
1044 break;
1045 case '^':
1046 switch (c2) {
1047 case '=': return CIRCUMFLEXEQUAL;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001048 }
1049 break;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001050 }
1051 return OP;
1052}
1053
Thomas Wouters434d0822000-08-24 20:11:32 +00001054int
1055PyToken_ThreeChars(int c1, int c2, int c3)
1056{
1057 switch (c1) {
1058 case '<':
1059 switch (c2) {
1060 case '<':
1061 switch (c3) {
1062 case '=':
1063 return LEFTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001064 }
1065 break;
1066 }
1067 break;
1068 case '>':
1069 switch (c2) {
1070 case '>':
1071 switch (c3) {
1072 case '=':
1073 return RIGHTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001074 }
1075 break;
1076 }
1077 break;
1078 case '*':
1079 switch (c2) {
1080 case '*':
1081 switch (c3) {
1082 case '=':
1083 return DOUBLESTAREQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001084 }
1085 break;
1086 }
1087 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001088 case '/':
1089 switch (c2) {
1090 case '/':
1091 switch (c3) {
1092 case '=':
1093 return DOUBLESLASHEQUAL;
1094 }
1095 break;
1096 }
1097 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001098 }
1099 return OP;
1100}
Guido van Rossumfbab9051991-10-20 20:25:03 +00001101
Guido van Rossum926f13a1998-04-09 21:38:06 +00001102static int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001103indenterror(struct tok_state *tok)
Guido van Rossum926f13a1998-04-09 21:38:06 +00001104{
1105 if (tok->alterror) {
Fred Drake85f36392000-07-11 17:53:00 +00001106 tok->done = E_TABSPACE;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001107 tok->cur = tok->inp;
1108 return 1;
1109 }
1110 if (tok->altwarning) {
Fred Drake85f36392000-07-11 17:53:00 +00001111 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
1112 "in indentation\n", tok->filename);
Guido van Rossum926f13a1998-04-09 21:38:06 +00001113 tok->altwarning = 0;
1114 }
1115 return 0;
1116}
1117
1118
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001119/* Get next token, after space stripping etc. */
1120
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001121static int
1122tok_get(register struct tok_state *tok, char **p_start, char **p_end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001123{
1124 register int c;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001125 int blankline;
1126
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001127 *p_start = *p_end = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001128 nextline:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001129 tok->start = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001130 blankline = 0;
1131
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001132 /* Get indentation level */
1133 if (tok->atbol) {
1134 register int col = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001135 register int altcol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001136 tok->atbol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001137 for (;;) {
1138 c = tok_nextc(tok);
1139 if (c == ' ')
Guido van Rossum926f13a1998-04-09 21:38:06 +00001140 col++, altcol++;
1141 else if (c == '\t') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001142 col = (col/tok->tabsize + 1) * tok->tabsize;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001143 altcol = (altcol/tok->alttabsize + 1)
1144 * tok->alttabsize;
1145 }
Guido van Rossum94d32b11995-07-07 22:27:27 +00001146 else if (c == '\014') /* Control-L (formfeed) */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001147 col = altcol = 0; /* For Emacs users */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001148 else
1149 break;
1150 }
1151 tok_backup(tok, c);
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001152 if (c == '#' || c == '\n') {
1153 /* Lines with only whitespace and/or comments
1154 shouldn't affect the indentation and are
1155 not passed to the parser as NEWLINE tokens,
1156 except *totally* empty lines in interactive
1157 mode, which signal the end of a command group. */
1158 if (col == 0 && c == '\n' && tok->prompt != NULL)
1159 blankline = 0; /* Let it through */
1160 else
1161 blankline = 1; /* Ignore completely */
1162 /* We can't jump back right here since we still
1163 may need to skip to the end of a comment */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001164 }
Guido van Rossuma849b831993-05-12 11:35:44 +00001165 if (!blankline && tok->level == 0) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001166 if (col == tok->indstack[tok->indent]) {
1167 /* No change */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001168 if (altcol != tok->altindstack[tok->indent]) {
1169 if (indenterror(tok))
1170 return ERRORTOKEN;
1171 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001173 else if (col > tok->indstack[tok->indent]) {
1174 /* Indent -- always one */
1175 if (tok->indent+1 >= MAXINDENT) {
Fred Drake85f36392000-07-11 17:53:00 +00001176 tok->done = E_TOODEEP;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001177 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001178 return ERRORTOKEN;
1179 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001180 if (altcol <= tok->altindstack[tok->indent]) {
1181 if (indenterror(tok))
1182 return ERRORTOKEN;
1183 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001184 tok->pendin++;
1185 tok->indstack[++tok->indent] = col;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001186 tok->altindstack[tok->indent] = altcol;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001187 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001188 else /* col < tok->indstack[tok->indent] */ {
1189 /* Dedent -- any number, must be consistent */
1190 while (tok->indent > 0 &&
1191 col < tok->indstack[tok->indent]) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001192 tok->pendin--;
Guido van Rossum54758fa1998-02-16 22:18:00 +00001193 tok->indent--;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001194 }
1195 if (col != tok->indstack[tok->indent]) {
Fred Drake85f36392000-07-11 17:53:00 +00001196 tok->done = E_DEDENT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001197 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001198 return ERRORTOKEN;
1199 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001200 if (altcol != tok->altindstack[tok->indent]) {
1201 if (indenterror(tok))
1202 return ERRORTOKEN;
1203 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001204 }
1205 }
1206 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001207
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001208 tok->start = tok->cur;
Tim Petersc9d78aa2006-03-26 23:27:58 +00001209
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001210 /* Return pending indents/dedents */
1211 if (tok->pendin != 0) {
1212 if (tok->pendin < 0) {
1213 tok->pendin++;
1214 return DEDENT;
1215 }
1216 else {
1217 tok->pendin--;
1218 return INDENT;
1219 }
1220 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001221
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001222 again:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001223 tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001224 /* Skip spaces */
1225 do {
1226 c = tok_nextc(tok);
Guido van Rossum94d32b11995-07-07 22:27:27 +00001227 } while (c == ' ' || c == '\t' || c == '\014');
Tim Petersc9d78aa2006-03-26 23:27:58 +00001228
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001229 /* Set start of current token */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001230 tok->start = tok->cur - 1;
Tim Petersc9d78aa2006-03-26 23:27:58 +00001231
Guido van Rossumab5ca152000-03-31 00:52:27 +00001232 /* Skip comment, while looking for tab-setting magic */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001233 if (c == '#') {
Guido van Rossumab5ca152000-03-31 00:52:27 +00001234 static char *tabforms[] = {
1235 "tab-width:", /* Emacs */
1236 ":tabstop=", /* vim, full form */
1237 ":ts=", /* vim, abbreviated form */
1238 "set tabsize=", /* will vi never die? */
1239 /* more templates can be added here to support other editors */
1240 };
1241 char cbuf[80];
1242 char *tp, **cp;
1243 tp = cbuf;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001244 do {
Guido van Rossumab5ca152000-03-31 00:52:27 +00001245 *tp++ = c = tok_nextc(tok);
1246 } while (c != EOF && c != '\n' &&
Neal Norwitz71e05f12006-06-12 02:07:57 +00001247 (size_t)(tp - cbuf + 1) < sizeof(cbuf));
Guido van Rossumab5ca152000-03-31 00:52:27 +00001248 *tp = '\0';
Tim Petersc9d78aa2006-03-26 23:27:58 +00001249 for (cp = tabforms;
Guido van Rossumab5ca152000-03-31 00:52:27 +00001250 cp < tabforms + sizeof(tabforms)/sizeof(tabforms[0]);
1251 cp++) {
1252 if ((tp = strstr(cbuf, *cp))) {
1253 int newsize = atoi(tp + strlen(*cp));
1254
1255 if (newsize >= 1 && newsize <= 40) {
1256 tok->tabsize = newsize;
Guido van Rossum6c981ad2000-04-03 23:02:17 +00001257 if (Py_VerboseFlag)
1258 PySys_WriteStderr(
Guido van Rossumab5ca152000-03-31 00:52:27 +00001259 "Tab size set to %d\n",
1260 newsize);
1261 }
1262 }
1263 }
1264 while (c != EOF && c != '\n')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001265 c = tok_nextc(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001266 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001267
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001268 /* Check for EOF and errors now */
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001269 if (c == EOF) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001270 return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001271 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001272
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001273 /* Identifier (most frequent token!) */
1274 if (isalpha(c) || c == '_') {
Guido van Rossum86016cb2000-03-10 22:56:54 +00001275 /* Process r"", u"" and ur"" */
Guido van Rossum5026cb41997-04-25 17:32:00 +00001276 switch (c) {
Christian Heimes288e89a2008-01-18 18:24:07 +00001277 case 'b':
1278 case 'B':
1279 c = tok_nextc(tok);
1280 if (c == 'r' || c == 'R')
1281 c = tok_nextc(tok);
1282 if (c == '"' || c == '\'')
1283 goto letter_quote;
1284 break;
Guido van Rossum5026cb41997-04-25 17:32:00 +00001285 case 'r':
1286 case 'R':
1287 c = tok_nextc(tok);
1288 if (c == '"' || c == '\'')
1289 goto letter_quote;
Guido van Rossum86016cb2000-03-10 22:56:54 +00001290 break;
1291 case 'u':
1292 case 'U':
1293 c = tok_nextc(tok);
1294 if (c == 'r' || c == 'R')
1295 c = tok_nextc(tok);
1296 if (c == '"' || c == '\'')
1297 goto letter_quote;
1298 break;
Guido van Rossum5026cb41997-04-25 17:32:00 +00001299 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001300 while (isalnum(c) || c == '_') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001301 c = tok_nextc(tok);
Guido van Rossum24dacb31997-04-06 03:46:20 +00001302 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001303 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001304 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001305 *p_end = tok->cur;
1306 return NAME;
1307 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001308
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001309 /* Newline */
1310 if (c == '\n') {
1311 tok->atbol = 1;
Guido van Rossuma849b831993-05-12 11:35:44 +00001312 if (blankline || tok->level > 0)
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001313 goto nextline;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001314 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001315 *p_end = tok->cur - 1; /* Leave '\n' out of the string */
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001316 tok->cont_line = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001317 return NEWLINE;
1318 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001319
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001320 /* Period or number starting with period? */
1321 if (c == '.') {
1322 c = tok_nextc(tok);
1323 if (isdigit(c)) {
1324 goto fraction;
1325 }
1326 else {
1327 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001328 *p_start = tok->start;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001329 *p_end = tok->cur;
1330 return DOT;
1331 }
1332 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001333
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001334 /* Number */
1335 if (isdigit(c)) {
1336 if (c == '0') {
Tim Petersd507dab2001-08-30 20:51:59 +00001337 /* Hex or octal -- maybe. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001338 c = tok_nextc(tok);
1339 if (c == '.')
1340 goto fraction;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001341#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001342 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001343 goto imaginary;
1344#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001345 if (c == 'x' || c == 'X') {
Georg Brandl14404b62008-01-19 19:27:05 +00001346
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347 /* Hex */
Georg Brandl14404b62008-01-19 19:27:05 +00001348 c = tok_nextc(tok);
1349 if (!isxdigit(c)) {
1350 tok->done = E_TOKEN;
1351 tok_backup(tok, c);
1352 return ERRORTOKEN;
1353 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001354 do {
1355 c = tok_nextc(tok);
1356 } while (isxdigit(c));
1357 }
1358 else {
Tim Petersd507dab2001-08-30 20:51:59 +00001359 int found_decimal = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001360 /* Octal; c is first char of it */
1361 /* There's no 'isoctdigit' macro, sigh */
1362 while ('0' <= c && c < '8') {
1363 c = tok_nextc(tok);
1364 }
Tim Petersd507dab2001-08-30 20:51:59 +00001365 if (isdigit(c)) {
1366 found_decimal = 1;
1367 do {
1368 c = tok_nextc(tok);
1369 } while (isdigit(c));
1370 }
1371 if (c == '.')
1372 goto fraction;
1373 else if (c == 'e' || c == 'E')
1374 goto exponent;
1375#ifndef WITHOUT_COMPLEX
1376 else if (c == 'j' || c == 'J')
1377 goto imaginary;
1378#endif
1379 else if (found_decimal) {
1380 tok->done = E_TOKEN;
1381 tok_backup(tok, c);
1382 return ERRORTOKEN;
1383 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001384 }
Guido van Rossumf023c461991-05-05 20:16:20 +00001385 if (c == 'l' || c == 'L')
1386 c = tok_nextc(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001387 }
1388 else {
1389 /* Decimal */
1390 do {
1391 c = tok_nextc(tok);
1392 } while (isdigit(c));
Guido van Rossumf023c461991-05-05 20:16:20 +00001393 if (c == 'l' || c == 'L')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001394 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001395 else {
Tim Peters9aa70d92001-08-27 19:19:28 +00001396 /* Accept floating point numbers. */
Guido van Rossumf023c461991-05-05 20:16:20 +00001397 if (c == '.') {
1398 fraction:
1399 /* Fraction */
1400 do {
1401 c = tok_nextc(tok);
1402 } while (isdigit(c));
1403 }
1404 if (c == 'e' || c == 'E') {
Tim Petersd507dab2001-08-30 20:51:59 +00001405 exponent:
Guido van Rossumf023c461991-05-05 20:16:20 +00001406 /* Exponent part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001407 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001408 if (c == '+' || c == '-')
1409 c = tok_nextc(tok);
Tim Peters9aa70d92001-08-27 19:19:28 +00001410 if (!isdigit(c)) {
1411 tok->done = E_TOKEN;
1412 tok_backup(tok, c);
1413 return ERRORTOKEN;
Guido van Rossumf023c461991-05-05 20:16:20 +00001414 }
Tim Peters9aa70d92001-08-27 19:19:28 +00001415 do {
1416 c = tok_nextc(tok);
1417 } while (isdigit(c));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001418 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001419#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001420 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001421 /* Imaginary part */
1422 imaginary:
1423 c = tok_nextc(tok);
1424#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001425 }
1426 }
1427 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001428 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001429 *p_end = tok->cur;
1430 return NUMBER;
1431 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001432
1433 letter_quote:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001434 /* String */
1435 if (c == '\'' || c == '"') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001436 Py_ssize_t quote2 = tok->cur - tok->start + 1;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001437 int quote = c;
1438 int triple = 0;
1439 int tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001440 for (;;) {
1441 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001442 if (c == '\n') {
1443 if (!triple) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001444 tok->done = E_EOLS;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001445 tok_backup(tok, c);
1446 return ERRORTOKEN;
1447 }
1448 tripcount = 0;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001449 tok->cont_line = 1; /* multiline string. */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001450 }
1451 else if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001452 if (triple)
1453 tok->done = E_EOFS;
1454 else
1455 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001456 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001457 return ERRORTOKEN;
1458 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001459 else if (c == quote) {
1460 tripcount++;
Guido van Rossum35685241998-02-16 15:42:50 +00001461 if (tok->cur - tok->start == quote2) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001462 c = tok_nextc(tok);
1463 if (c == quote) {
1464 triple = 1;
1465 tripcount = 0;
1466 continue;
1467 }
1468 tok_backup(tok, c);
1469 }
1470 if (!triple || tripcount == 3)
1471 break;
1472 }
1473 else if (c == '\\') {
1474 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001475 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001476 if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001477 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001478 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001479 return ERRORTOKEN;
1480 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001481 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001482 else
1483 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001484 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001485 *p_start = tok->start;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001486 *p_end = tok->cur;
1487 return STRING;
1488 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001489
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001490 /* Line continuation */
1491 if (c == '\\') {
1492 c = tok_nextc(tok);
1493 if (c != '\n') {
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001494 tok->done = E_LINECONT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001495 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001496 return ERRORTOKEN;
1497 }
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001498 tok->cont_line = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001499 goto again; /* Read next line */
1500 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001501
Guido van Rossumfbab9051991-10-20 20:25:03 +00001502 /* Check for two-character token */
1503 {
1504 int c2 = tok_nextc(tok);
Guido van Rossum86bea461997-04-29 21:03:06 +00001505 int token = PyToken_TwoChars(c, c2);
Christian Heimes02c9ab52007-11-23 12:12:02 +00001506#ifndef PGEN
Amaury Forgeot d'Arc6dae85f2007-11-24 13:20:22 +00001507 if (Py_Py3kWarningFlag && token == NOTEQUAL && c == '<') {
Christian Heimes02c9ab52007-11-23 12:12:02 +00001508 if (PyErr_WarnExplicit(PyExc_DeprecationWarning,
1509 "<> not supported in 3.x",
1510 tok->filename, tok->lineno,
1511 NULL, NULL)) {
1512 return ERRORTOKEN;
1513 }
1514 }
1515#endif
Guido van Rossumfbab9051991-10-20 20:25:03 +00001516 if (token != OP) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001517 int c3 = tok_nextc(tok);
1518 int token3 = PyToken_ThreeChars(c, c2, c3);
1519 if (token3 != OP) {
1520 token = token3;
1521 } else {
1522 tok_backup(tok, c3);
1523 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001524 *p_start = tok->start;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001525 *p_end = tok->cur;
1526 return token;
1527 }
1528 tok_backup(tok, c2);
1529 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001530
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001531 /* Keep track of parentheses nesting level */
Guido van Rossuma849b831993-05-12 11:35:44 +00001532 switch (c) {
1533 case '(':
1534 case '[':
1535 case '{':
1536 tok->level++;
1537 break;
1538 case ')':
1539 case ']':
1540 case '}':
1541 tok->level--;
1542 break;
1543 }
Tim Petersc9d78aa2006-03-26 23:27:58 +00001544
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001545 /* Punctuation character */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001546 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001547 *p_end = tok->cur;
Guido van Rossum86bea461997-04-29 21:03:06 +00001548 return PyToken_OneChar(c);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001549}
1550
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001551int
1552PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
1553{
1554 int result = tok_get(tok, p_start, p_end);
1555 if (tok->decoding_erred) {
1556 result = ERRORTOKEN;
1557 tok->done = E_DECODE;
1558 }
1559 return result;
1560}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001561
Martin v. Löwisa5136192007-09-04 14:19:28 +00001562/* This function is only called from parsetok. However, it cannot live
1563 there, as it must be empty for PGEN, and we can check for PGEN only
1564 in this file. */
1565
1566#ifdef PGEN
1567char*
1568PyTokenizer_RestoreEncoding(struct tok_state* tok, int len, int* offset)
1569{
1570 return NULL;
1571}
1572#else
Georg Brandl76b30d12008-01-07 18:41:34 +00001573#ifdef Py_USING_UNICODE
Martin v. Löwisa5136192007-09-04 14:19:28 +00001574static PyObject *
1575dec_utf8(const char *enc, const char *text, size_t len) {
1576 PyObject *ret = NULL;
1577 PyObject *unicode_text = PyUnicode_DecodeUTF8(text, len, "replace");
1578 if (unicode_text) {
1579 ret = PyUnicode_AsEncodedString(unicode_text, enc, "replace");
1580 Py_DECREF(unicode_text);
1581 }
1582 if (!ret) {
Guido van Rossum9fc1b962007-10-15 15:54:11 +00001583 PyErr_Clear();
Martin v. Löwisa5136192007-09-04 14:19:28 +00001584 }
1585 return ret;
1586}
1587
1588char *
1589PyTokenizer_RestoreEncoding(struct tok_state* tok, int len, int *offset)
1590{
1591 char *text = NULL;
1592 if (tok->encoding) {
1593 /* convert source to original encondig */
1594 PyObject *lineobj = dec_utf8(tok->encoding, tok->buf, len);
1595 if (lineobj != NULL) {
1596 int linelen = PyString_Size(lineobj);
1597 const char *line = PyString_AsString(lineobj);
1598 text = PyObject_MALLOC(linelen + 1);
1599 if (text != NULL && line != NULL) {
1600 if (linelen)
1601 strncpy(text, line, linelen);
1602 text[linelen] = '\0';
1603 }
1604 Py_DECREF(lineobj);
1605
1606 /* adjust error offset */
1607 if (*offset > 1) {
1608 PyObject *offsetobj = dec_utf8(tok->encoding,
1609 tok->buf, *offset-1);
1610 if (offsetobj) {
1611 *offset = PyString_Size(offsetobj) + 1;
1612 Py_DECREF(offsetobj);
1613 }
1614 }
1615
1616 }
1617 }
1618 return text;
1619
1620}
Georg Brandl76b30d12008-01-07 18:41:34 +00001621#endif /* defined(Py_USING_UNICODE) */
Martin v. Löwisa5136192007-09-04 14:19:28 +00001622#endif
1623
Martin v. Löwisa5136192007-09-04 14:19:28 +00001624
Guido van Rossum408027e1996-12-30 16:17:54 +00001625#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001626
1627void
Thomas Wouters23c9e002000-07-22 19:20:54 +00001628tok_dump(int type, char *start, char *end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001629{
Guido van Rossum86bea461997-04-29 21:03:06 +00001630 printf("%s", _PyParser_TokenNames[type]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001631 if (type == NAME || type == NUMBER || type == STRING || type == OP)
1632 printf("(%.*s)", (int)(end - start), start);
1633}
1634
1635#endif