blob: 3c8258832e7519cace80e80755ff1181795cf0a9 [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öwis566f6af2002-10-26 14:39:10 +000021extern char *PyOS_Readline(FILE *, FILE *, char *);
Guido van Rossumf4b1a641994-08-29 12:43:07 +000022/* Return malloc'ed string including trailing \n;
23 empty malloc'ed string for EOF;
24 NULL if interrupted */
25
Guido van Rossum4fe87291992-02-26 15:24:44 +000026/* Don't ever change this -- it would break the portability of Python code */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000027#define TABSIZE 8
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossumcf57d8b1998-01-19 22:07:46 +000029/* Convert a possibly signed character to a nonnegative int */
30/* XXX This assumes characters are 8 bits wide */
31#ifdef __CHAR_UNSIGNED__
32#define Py_CHARMASK(c) (c)
33#else
34#define Py_CHARMASK(c) ((c) & 0xff)
35#endif
36
Guido van Rossum3f5da241990-12-20 15:06:42 +000037/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000038static struct tok_state *tok_new(void);
39static int tok_nextc(struct tok_state *tok);
40static void tok_backup(struct tok_state *tok, int c);
Guido van Rossum3f5da241990-12-20 15:06:42 +000041
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042/* Token names */
43
Guido van Rossum86bea461997-04-29 21:03:06 +000044char *_PyParser_TokenNames[] = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045 "ENDMARKER",
46 "NAME",
47 "NUMBER",
48 "STRING",
49 "NEWLINE",
50 "INDENT",
51 "DEDENT",
52 "LPAR",
53 "RPAR",
54 "LSQB",
55 "RSQB",
56 "COLON",
57 "COMMA",
58 "SEMI",
59 "PLUS",
60 "MINUS",
61 "STAR",
62 "SLASH",
63 "VBAR",
64 "AMPER",
65 "LESS",
66 "GREATER",
67 "EQUAL",
68 "DOT",
69 "PERCENT",
70 "BACKQUOTE",
71 "LBRACE",
72 "RBRACE",
Guido van Rossumfbab9051991-10-20 20:25:03 +000073 "EQEQUAL",
74 "NOTEQUAL",
75 "LESSEQUAL",
76 "GREATEREQUAL",
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +000077 "TILDE",
78 "CIRCUMFLEX",
79 "LEFTSHIFT",
80 "RIGHTSHIFT",
Guido van Rossumf595fde1996-01-12 01:31:58 +000081 "DOUBLESTAR",
Thomas Wouters434d0822000-08-24 20:11:32 +000082 "PLUSEQUAL",
83 "MINEQUAL",
84 "STAREQUAL",
85 "SLASHEQUAL",
86 "PERCENTEQUAL",
87 "AMPEREQUAL",
88 "VBAREQUAL",
89 "CIRCUMFLEXEQUAL",
90 "LEFTSHIFTEQUAL",
91 "RIGHTSHIFTEQUAL",
92 "DOUBLESTAREQUAL",
Guido van Rossum4668b002001-08-08 05:00:18 +000093 "DOUBLESLASH",
94 "DOUBLESLASHEQUAL",
Anthony Baxterc2a5a632004-08-02 06:10:11 +000095 "AT",
Guido van Rossumfbab9051991-10-20 20:25:03 +000096 /* This table must match the #defines in token.h! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000097 "OP",
98 "<ERRORTOKEN>",
99 "<N_TOKENS>"
100};
101
102
103/* Create and initialize a new tok_state structure */
104
105static struct tok_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000106tok_new(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107{
Guido van Rossum86bea461997-04-29 21:03:06 +0000108 struct tok_state *tok = PyMem_NEW(struct tok_state, 1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 if (tok == NULL)
110 return NULL;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000111 tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 tok->done = E_OK;
113 tok->fp = NULL;
114 tok->tabsize = TABSIZE;
115 tok->indent = 0;
116 tok->indstack[0] = 0;
117 tok->atbol = 1;
118 tok->pendin = 0;
119 tok->prompt = tok->nextprompt = NULL;
120 tok->lineno = 0;
Guido van Rossuma849b831993-05-12 11:35:44 +0000121 tok->level = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +0000122 tok->filename = NULL;
123 tok->altwarning = 0;
124 tok->alterror = 0;
125 tok->alttabsize = 1;
126 tok->altindstack[0] = 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000127 tok->decoding_state = 0;
128 tok->decoding_erred = 0;
129 tok->read_coding_spec = 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000130 tok->encoding = NULL;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000131 tok->cont_line = 0;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000132#ifndef PGEN
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000133 tok->decoding_readline = NULL;
134 tok->decoding_buffer = NULL;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000135#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136 return tok;
137}
138
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000139#ifdef PGEN
140
141static char *
142decoding_fgets(char *s, int size, struct tok_state *tok)
143{
144 return fgets(s, size, tok->fp);
145}
146
147static int
148decoding_feof(struct tok_state *tok)
149{
150 return feof(tok->fp);
151}
152
153static const char *
154decode_str(const char *str, struct tok_state *tok)
155{
156 return str;
157}
158
159#else /* PGEN */
160
161static char *
162error_ret(struct tok_state *tok) /* XXX */
163{
164 tok->decoding_erred = 1;
165 if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
166 PyMem_DEL(tok->buf);
167 tok->buf = NULL;
168 return NULL; /* as if it were EOF */
169}
170
171static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000172new_string(const char *s, Py_ssize_t len)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000173{
174 char* result = PyMem_NEW(char, len + 1);
175 if (result != NULL) {
176 memcpy(result, s, len);
177 result[len] = '\0';
178 }
179 return result;
180}
181
182static char *
183get_normal_name(char *s) /* for utf-8 and latin-1 */
184{
185 char buf[13];
186 int i;
187 for (i = 0; i < 12; i++) {
188 int c = s[i];
189 if (c == '\0') break;
190 else if (c == '_') buf[i] = '-';
191 else buf[i] = tolower(c);
192 }
193 buf[i] = '\0';
194 if (strcmp(buf, "utf-8") == 0 ||
195 strncmp(buf, "utf-8-", 6) == 0) return "utf-8";
196 else if (strcmp(buf, "latin-1") == 0 ||
197 strcmp(buf, "iso-8859-1") == 0 ||
198 strcmp(buf, "iso-latin-1") == 0 ||
199 strncmp(buf, "latin-1-", 8) == 0 ||
200 strncmp(buf, "iso-8859-1-", 11) == 0 ||
201 strncmp(buf, "iso-latin-1-", 12) == 0) return "iso-8859-1";
202 else return s;
203}
204
205/* Return the coding spec in S, or NULL if none is found. */
206
207static char *
Martin v. Löwis18e16552006-02-15 17:27:45 +0000208get_coding_spec(const char *s, Py_ssize_t size)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000209{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000210 Py_ssize_t i;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000211 /* Coding spec must be in a comment, and that comment must be
212 * the only statement on the source code line. */
213 for (i = 0; i < size - 6; i++) {
214 if (s[i] == '#')
215 break;
216 if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014')
217 return NULL;
218 }
219 for (; i < size - 6; i++) { /* XXX inefficient search */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000220 const char* t = s + i;
221 if (strncmp(t, "coding", 6) == 0) {
222 const char* begin = NULL;
223 t += 6;
224 if (t[0] != ':' && t[0] != '=')
225 continue;
226 do {
227 t++;
228 } while (t[0] == '\x20' || t[0] == '\t');
229
230 begin = t;
Neal Norwitz30b5c5d2005-12-19 06:05:18 +0000231 while (isalnum(Py_CHARMASK(t[0])) ||
Neal Norwitze08e1bc2002-11-02 20:43:25 +0000232 t[0] == '-' || t[0] == '_' || t[0] == '.')
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000233 t++;
234
235 if (begin < t) {
236 char* r = new_string(begin, t - begin);
237 char* q = get_normal_name(r);
238 if (r != q) {
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000239 PyMem_DEL(r);
240 r = new_string(q, strlen(q));
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000241 }
242 return r;
243 }
244 }
245 }
246 return NULL;
247}
248
249/* Check whether the line contains a coding spec. If it does,
250 invoke the set_readline function for the new encoding.
251 This function receives the tok_state and the new encoding.
252 Return 1 on success, 0 on failure. */
253
254static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000255check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000256 int set_readline(struct tok_state *, const char *))
257{
Tim Peters17db21f2002-09-03 15:39:58 +0000258 char * cs;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000259 int r = 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000260
Martin v. Löwisf62a89b2002-09-03 11:52:44 +0000261 if (tok->cont_line)
262 /* It's a continuation line, so it can't be a coding spec. */
263 return 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000264 cs = get_coding_spec(line, size);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000265 if (cs != NULL) {
266 tok->read_coding_spec = 1;
267 if (tok->encoding == NULL) {
268 assert(tok->decoding_state == 1); /* raw */
269 if (strcmp(cs, "utf-8") == 0 ||
270 strcmp(cs, "iso-8859-1") == 0) {
271 tok->encoding = cs;
272 } else {
Martin v. Löwis019934b2002-08-07 12:33:18 +0000273#ifdef Py_USING_UNICODE
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000274 r = set_readline(tok, cs);
275 if (r) {
276 tok->encoding = cs;
277 tok->decoding_state = -1;
278 }
Neal Norwitzc0d5faa2005-10-21 06:05:33 +0000279 else
280 PyMem_DEL(cs);
Martin v. Löwis019934b2002-08-07 12:33:18 +0000281#else
282 /* Without Unicode support, we cannot
283 process the coding spec. Since there
284 won't be any Unicode literals, that
285 won't matter. */
Neal Norwitzc0d5faa2005-10-21 06:05:33 +0000286 PyMem_DEL(cs);
Martin v. Löwis019934b2002-08-07 12:33:18 +0000287#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000288 }
289 } else { /* then, compare cs with BOM */
290 r = (strcmp(tok->encoding, cs) == 0);
291 PyMem_DEL(cs);
292 }
293 }
Neal Norwitzdb83eb32005-12-18 05:29:30 +0000294 if (!r) {
295 cs = tok->encoding;
296 if (!cs)
297 cs = "with BOM";
298 PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
299 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000300 return r;
301}
302
303/* See whether the file starts with a BOM. If it does,
304 invoke the set_readline function with the new encoding.
305 Return 1 on success, 0 on failure. */
306
307static int
308check_bom(int get_char(struct tok_state *),
309 void unget_char(int, struct tok_state *),
310 int set_readline(struct tok_state *, const char *),
311 struct tok_state *tok)
312{
313 int ch = get_char(tok);
314 tok->decoding_state = 1;
315 if (ch == EOF) {
316 return 1;
317 } else if (ch == 0xEF) {
318 ch = get_char(tok); if (ch != 0xBB) goto NON_BOM;
319 ch = get_char(tok); if (ch != 0xBF) goto NON_BOM;
320#if 0
321 /* Disable support for UTF-16 BOMs until a decision
322 is made whether this needs to be supported. */
323 } else if (ch == 0xFE) {
324 ch = get_char(tok); if (ch != 0xFF) goto NON_BOM;
325 if (!set_readline(tok, "utf-16-be")) return 0;
326 tok->decoding_state = -1;
327 } else if (ch == 0xFF) {
328 ch = get_char(tok); if (ch != 0xFE) goto NON_BOM;
329 if (!set_readline(tok, "utf-16-le")) return 0;
330 tok->decoding_state = -1;
331#endif
332 } else {
333 unget_char(ch, tok);
334 return 1;
335 }
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000336 if (tok->encoding != NULL)
337 PyMem_DEL(tok->encoding);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000338 tok->encoding = new_string("utf-8", 5); /* resulting is in utf-8 */
339 return 1;
340 NON_BOM:
341 /* any token beginning with '\xEF', '\xFE', '\xFF' is a bad token */
342 unget_char(0xFF, tok); /* XXX this will cause a syntax error */
343 return 1;
344}
345
346/* Read a line of text from TOK into S, using the stream in TOK.
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000347 Return NULL on failure, else S.
348
349 On entry, tok->decoding_buffer will be one of:
350 1) NULL: need to call tok->decoding_readline to get a new line
351 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
352 stored the result in tok->decoding_buffer
353 3) PyStringObject *: previous call to fp_readl did not have enough room
354 (in the s buffer) to copy entire contents of the line read
355 by tok->decoding_readline. tok->decoding_buffer has the overflow.
356 In this case, fp_readl is called in a loop (with an expanded buffer)
357 until the buffer ends with a '\n' (or until the end of the file is
358 reached): see tok_nextc and its calls to decoding_fgets.
359*/
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000360
361static char *
362fp_readl(char *s, int size, struct tok_state *tok)
363{
Martin v. Löwis019934b2002-08-07 12:33:18 +0000364#ifndef Py_USING_UNICODE
365 /* In a non-Unicode built, this should never be called. */
Martin v. Löwis2863c102002-08-07 15:18:57 +0000366 Py_FatalError("fp_readl should not be called in this build.");
Guido van Rossum84b2bed2002-08-16 17:01:09 +0000367 return NULL; /* Keep compiler happy (not reachable) */
Martin v. Löwis019934b2002-08-07 12:33:18 +0000368#else
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000369 PyObject* utf8 = NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000370 PyObject* buf = tok->decoding_buffer;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000371 char *str;
Martin v. Löwisf5adf1e2006-02-16 14:35:38 +0000372 Py_ssize_t utf8len;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000373
374 /* Ask for one less byte so we can terminate it */
375 assert(size > 0);
376 size--;
377
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000378 if (buf == NULL) {
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000379 buf = PyObject_CallObject(tok->decoding_readline, NULL);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000380 if (buf == NULL)
381 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000382 } else {
383 tok->decoding_buffer = NULL;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000384 if (PyString_CheckExact(buf))
385 utf8 = buf;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000386 }
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000387 if (utf8 == NULL) {
388 utf8 = PyUnicode_AsUTF8String(buf);
389 Py_DECREF(buf);
390 if (utf8 == NULL)
391 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000392 }
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000393 str = PyString_AsString(utf8);
394 utf8len = PyString_GET_SIZE(utf8);
395 if (utf8len > size) {
396 tok->decoding_buffer = PyString_FromStringAndSize(str+size, utf8len-size);
397 if (tok->decoding_buffer == NULL) {
398 Py_DECREF(utf8);
399 return error_ret(tok);
400 }
401 utf8len = size;
402 }
403 memcpy(s, str, utf8len);
404 s[utf8len] = '\0';
405 Py_DECREF(utf8);
406 if (utf8len == 0) return NULL; /* EOF */
407 return s;
Martin v. Löwis019934b2002-08-07 12:33:18 +0000408#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000409}
410
411/* Set the readline function for TOK to a StreamReader's
412 readline function. The StreamReader is named ENC.
413
414 This function is called from check_bom and check_coding_spec.
415
416 ENC is usually identical to the future value of tok->encoding,
417 except for the (currently unsupported) case of UTF-16.
418
419 Return 1 on success, 0 on failure. */
420
421static int
422fp_setreadl(struct tok_state *tok, const char* enc)
423{
424 PyObject *reader, *stream, *readline;
425
Martin v. Löwis95292d62002-12-11 14:04:59 +0000426 /* XXX: constify filename argument. */
427 stream = PyFile_FromFile(tok->fp, (char*)tok->filename, "rb", NULL);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000428 if (stream == NULL)
429 return 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000430
431 reader = PyCodec_StreamReader(enc, stream, NULL);
432 Py_DECREF(stream);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000433 if (reader == NULL)
434 return 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000435
436 readline = PyObject_GetAttrString(reader, "readline");
437 Py_DECREF(reader);
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000438 if (readline == NULL)
439 return 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000440
441 tok->decoding_readline = readline;
442 return 1;
443}
444
445/* Fetch the next byte from TOK. */
446
447static int fp_getc(struct tok_state *tok) {
448 return getc(tok->fp);
449}
450
451/* Unfetch the last byte back into TOK. */
452
453static void fp_ungetc(int c, struct tok_state *tok) {
454 ungetc(c, tok->fp);
455}
456
457/* Read a line of input from TOK. Determine encoding
458 if necessary. */
459
460static char *
461decoding_fgets(char *s, int size, struct tok_state *tok)
462{
Martin v. Löwis2863c102002-08-07 15:18:57 +0000463 char *line = NULL;
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000464 int badchar = 0;
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000465 for (;;) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000466 if (tok->decoding_state < 0) {
467 /* We already have a codec associated with
468 this input. */
469 line = fp_readl(s, size, tok);
470 break;
471 } else if (tok->decoding_state > 0) {
472 /* We want a 'raw' read. */
473 line = Py_UniversalNewlineFgets(s, size,
474 tok->fp, NULL);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000475 break;
476 } else {
477 /* We have not yet determined the encoding.
478 If an encoding is found, use the file-pointer
479 reader functions from now on. */
480 if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok))
481 return error_ret(tok);
482 assert(tok->decoding_state != 0);
483 }
Martin v. Löwiscd280fb2002-08-04 18:28:44 +0000484 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000485 if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) {
486 if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) {
487 return error_ret(tok);
488 }
489 }
490#ifndef PGEN
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000491 /* The default encoding is ASCII, so make sure we don't have any
492 non-ASCII bytes in it. */
493 if (line && !tok->encoding) {
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000494 unsigned char *c;
Jack Jansencf0a2cf2002-08-05 14:14:05 +0000495 for (c = (unsigned char *)line; *c; c++)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000496 if (*c > 127) {
497 badchar = *c;
498 break;
499 }
500 }
501 if (badchar) {
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000502 char buf[500];
Martin v. Löwis725bb232002-08-05 01:49:16 +0000503 /* Need to add 1 to the line number, since this line
504 has not been counted, yet. */
Marc-André Lemburg1fb14002003-02-17 18:31:57 +0000505 sprintf(buf,
506 "Non-ASCII character '\\x%.2x' "
507 "in file %.200s on line %i, "
508 "but no encoding declared; "
509 "see http://www.python.org/peps/pep-0263.html for details",
510 badchar, tok->filename, tok->lineno + 1);
Martin v. Löwis6cba2562006-02-28 22:41:29 +0000511 PyErr_SetString(PyExc_SyntaxError, buf);
512 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000513 }
514#endif
515 return line;
516}
517
518static int
519decoding_feof(struct tok_state *tok)
520{
521 if (tok->decoding_state >= 0) {
522 return feof(tok->fp);
523 } else {
524 PyObject* buf = tok->decoding_buffer;
525 if (buf == NULL) {
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000526 buf = PyObject_CallObject(tok->decoding_readline, NULL);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000527 if (buf == NULL) {
528 error_ret(tok);
529 return 1;
530 } else {
531 tok->decoding_buffer = buf;
532 }
533 }
534 return PyObject_Length(buf) == 0;
535 }
536}
537
538/* Fetch a byte from TOK, using the string buffer. */
539
540static int buf_getc(struct tok_state *tok) {
Just van Rossumf032f862003-02-09 20:38:48 +0000541 return Py_CHARMASK(*tok->str++);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000542}
543
544/* Unfetch a byte from TOK, using the string buffer. */
545
546static void buf_ungetc(int c, struct tok_state *tok) {
547 tok->str--;
Just van Rossumf032f862003-02-09 20:38:48 +0000548 assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000549}
550
551/* Set the readline function for TOK to ENC. For the string-based
552 tokenizer, this means to just record the encoding. */
553
554static int buf_setreadl(struct tok_state *tok, const char* enc) {
555 tok->enc = enc;
556 return 1;
557}
558
559/* Return a UTF-8 encoding Python string object from the
560 C byte string STR, which is encoded with ENC. */
561
Martin v. Löwis019934b2002-08-07 12:33:18 +0000562#ifdef Py_USING_UNICODE
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000563static PyObject *
564translate_into_utf8(const char* str, const char* enc) {
565 PyObject *utf8;
566 PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL);
567 if (buf == NULL)
568 return NULL;
569 utf8 = PyUnicode_AsUTF8String(buf);
570 Py_DECREF(buf);
571 return utf8;
572}
Martin v. Löwis019934b2002-08-07 12:33:18 +0000573#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000574
575/* Decode a byte string STR for use as the buffer of TOK.
576 Look for encoding declarations inside STR, and record them
577 inside TOK. */
578
579static const char *
580decode_str(const char *str, struct tok_state *tok)
581{
582 PyObject* utf8 = NULL;
583 const char *s;
584 int lineno = 0;
585 tok->enc = NULL;
586 tok->str = str;
587 if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000588 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000589 str = tok->str; /* string after BOM if any */
Tim Peters2c3f9c62002-08-04 17:58:34 +0000590 assert(str);
Martin v. Löwis019934b2002-08-07 12:33:18 +0000591#ifdef Py_USING_UNICODE
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000592 if (tok->enc != NULL) {
593 utf8 = translate_into_utf8(str, tok->enc);
594 if (utf8 == NULL)
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000595 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000596 str = PyString_AsString(utf8);
597 }
Martin v. Löwis019934b2002-08-07 12:33:18 +0000598#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000599 for (s = str;; s++) {
600 if (*s == '\0') break;
601 else if (*s == '\n') {
602 lineno++;
603 if (lineno == 2) break;
604 }
605 }
606 tok->enc = NULL;
607 if (!check_coding_spec(str, s - str, tok, buf_setreadl))
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000608 return error_ret(tok);
Martin v. Löwis019934b2002-08-07 12:33:18 +0000609#ifdef Py_USING_UNICODE
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000610 if (tok->enc != NULL) {
611 assert(utf8 == NULL);
612 utf8 = translate_into_utf8(str, tok->enc);
Neal Norwitz40d37812005-10-02 01:48:49 +0000613 if (utf8 == NULL) {
614 PyErr_Format(PyExc_SyntaxError,
615 "unknown encoding: %s", tok->enc);
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000616 return error_ret(tok);
Neal Norwitz40d37812005-10-02 01:48:49 +0000617 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000618 str = PyString_AsString(utf8);
619 }
Martin v. Löwis019934b2002-08-07 12:33:18 +0000620#endif
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000621 assert(tok->decoding_buffer == NULL);
622 tok->decoding_buffer = utf8; /* CAUTION */
623 return str;
624}
625
626#endif /* PGEN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627
628/* Set up tokenizer for string */
629
630struct tok_state *
Martin v. Löwis95292d62002-12-11 14:04:59 +0000631PyTokenizer_FromString(const char *str)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632{
633 struct tok_state *tok = tok_new();
634 if (tok == NULL)
635 return NULL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000636 str = (char *)decode_str(str, tok);
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000637 if (str == NULL) {
638 PyTokenizer_Free(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000639 return NULL;
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000640 }
641
Martin v. Löwis95292d62002-12-11 14:04:59 +0000642 /* XXX: constify members. */
643 tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644 return tok;
645}
646
647
Guido van Rossum8c11a5c1991-07-27 21:42:56 +0000648/* Set up tokenizer for file */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649
650struct tok_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000651PyTokenizer_FromFile(FILE *fp, char *ps1, char *ps2)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000652{
653 struct tok_state *tok = tok_new();
654 if (tok == NULL)
655 return NULL;
Guido van Rossum86bea461997-04-29 21:03:06 +0000656 if ((tok->buf = PyMem_NEW(char, BUFSIZ)) == NULL) {
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000657 PyTokenizer_Free(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000658 return NULL;
659 }
660 tok->cur = tok->inp = tok->buf;
661 tok->end = tok->buf + BUFSIZ;
662 tok->fp = fp;
663 tok->prompt = ps1;
664 tok->nextprompt = ps2;
665 return tok;
666}
667
668
669/* Free a tok_state structure */
670
671void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000672PyTokenizer_Free(struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000673{
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000674 if (tok->encoding != NULL)
675 PyMem_DEL(tok->encoding);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000676#ifndef PGEN
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000677 Py_XDECREF(tok->decoding_readline);
678 Py_XDECREF(tok->decoding_buffer);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000679#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000680 if (tok->fp != NULL && tok->buf != NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +0000681 PyMem_DEL(tok->buf);
682 PyMem_DEL(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000683}
684
Hye-Shik Chang7df44b32004-08-04 17:36:41 +0000685#if !defined(PGEN) && defined(Py_USING_UNICODE)
686static int
687tok_stdin_decode(struct tok_state *tok, char **inp)
688{
689 PyObject *enc, *sysstdin, *decoded, *utf8;
690 const char *encoding;
691 char *converted;
692
693 if (PySys_GetFile((char *)"stdin", NULL) != stdin)
694 return 0;
695 sysstdin = PySys_GetObject("stdin");
696 if (sysstdin == NULL || !PyFile_Check(sysstdin))
697 return 0;
698
699 enc = ((PyFileObject *)sysstdin)->f_encoding;
700 if (enc == NULL || !PyString_Check(enc))
701 return 0;
702 Py_INCREF(enc);
703
704 encoding = PyString_AsString(enc);
705 decoded = PyUnicode_Decode(*inp, strlen(*inp), encoding, NULL);
706 if (decoded == NULL)
707 goto error_clear;
708
709 utf8 = PyUnicode_AsEncodedString(decoded, "utf-8", NULL);
710 Py_DECREF(decoded);
711 if (utf8 == NULL)
712 goto error_clear;
713
714 converted = new_string(PyString_AsString(utf8), PyString_Size(utf8));
715 Py_DECREF(utf8);
716 if (converted == NULL)
717 goto error_nomem;
718
719 PyMem_FREE(*inp);
720 *inp = converted;
721 if (tok->encoding != NULL)
722 PyMem_DEL(tok->encoding);
723 tok->encoding = new_string(encoding, strlen(encoding));
724 if (tok->encoding == NULL)
725 goto error_nomem;
726
727 Py_DECREF(enc);
728 return 0;
729
730error_nomem:
731 Py_DECREF(enc);
732 tok->done = E_NOMEM;
733 return -1;
734
735error_clear:
736 /* Fallback to iso-8859-1: for backward compatibility */
737 Py_DECREF(enc);
738 PyErr_Clear();
739 return 0;
740}
741#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000742
743/* Get next char, updating state; error code goes into tok->done */
744
745static int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000746tok_nextc(register struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000747{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000748 for (;;) {
Guido van Rossum1a817c01994-09-19 08:06:25 +0000749 if (tok->cur != tok->inp) {
Guido van Rossumcf57d8b1998-01-19 22:07:46 +0000750 return Py_CHARMASK(*tok->cur++); /* Fast path */
Guido van Rossum1a817c01994-09-19 08:06:25 +0000751 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000752 if (tok->done != E_OK)
753 return EOF;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000754 if (tok->fp == NULL) {
Guido van Rossum1a817c01994-09-19 08:06:25 +0000755 char *end = strchr(tok->inp, '\n');
756 if (end != NULL)
757 end++;
758 else {
759 end = strchr(tok->inp, '\0');
760 if (end == tok->inp) {
761 tok->done = E_EOF;
762 return EOF;
763 }
764 }
765 if (tok->start == NULL)
766 tok->buf = tok->cur;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000767 tok->line_start = tok->cur;
Guido van Rossum1a817c01994-09-19 08:06:25 +0000768 tok->lineno++;
769 tok->inp = end;
Guido van Rossumcf57d8b1998-01-19 22:07:46 +0000770 return Py_CHARMASK(*tok->cur++);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000772 if (tok->prompt != NULL) {
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000773 char *new = PyOS_Readline(stdin, stdout, tok->prompt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000774 if (tok->nextprompt != NULL)
775 tok->prompt = tok->nextprompt;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000776 if (new == NULL)
777 tok->done = E_INTR;
778 else if (*new == '\0') {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000779 PyMem_FREE(new);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780 tok->done = E_EOF;
781 }
Hye-Shik Chang7df44b32004-08-04 17:36:41 +0000782#if !defined(PGEN) && defined(Py_USING_UNICODE)
783 else if (tok_stdin_decode(tok, &new) != 0)
784 PyMem_FREE(new);
785#endif
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000786 else if (tok->start != NULL) {
Guido van Rossum6da34342000-06-28 22:00:02 +0000787 size_t start = tok->start - tok->buf;
788 size_t oldlen = tok->cur - tok->buf;
789 size_t newlen = oldlen + strlen(new);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000790 char *buf = tok->buf;
791 PyMem_RESIZE(buf, char, newlen+1);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000792 tok->lineno++;
793 if (buf == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000794 PyMem_DEL(tok->buf);
Guido van Rossum588633d1994-12-30 15:46:02 +0000795 tok->buf = NULL;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000796 PyMem_FREE(new);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000797 tok->done = E_NOMEM;
798 return EOF;
799 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000800 tok->buf = buf;
801 tok->cur = tok->buf + oldlen;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000802 tok->line_start = tok->cur;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000803 strcpy(tok->buf + oldlen, new);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000804 PyMem_FREE(new);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000805 tok->inp = tok->buf + newlen;
806 tok->end = tok->inp + 1;
807 tok->start = tok->buf + start;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000808 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000809 else {
810 tok->lineno++;
811 if (tok->buf != NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000812 PyMem_DEL(tok->buf);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000813 tok->buf = new;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000814 tok->line_start = tok->buf;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000815 tok->cur = tok->buf;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000816 tok->line_start = tok->buf;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000817 tok->inp = strchr(tok->buf, '\0');
818 tok->end = tok->inp + 1;
819 }
820 }
821 else {
822 int done = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000823 Py_ssize_t cur = 0;
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000824 char *pt;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000825 if (tok->start == NULL) {
826 if (tok->buf == NULL) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000827 tok->buf = PyMem_NEW(char, BUFSIZ);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000828 if (tok->buf == NULL) {
829 tok->done = E_NOMEM;
830 return EOF;
831 }
832 tok->end = tok->buf + BUFSIZ;
833 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000834 if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
835 tok) == NULL) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000836 tok->done = E_EOF;
837 done = 1;
838 }
839 else {
840 tok->done = E_OK;
841 tok->inp = strchr(tok->buf, '\0');
842 done = tok->inp[-1] == '\n';
843 }
844 }
845 else {
846 cur = tok->cur - tok->buf;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000847 if (decoding_feof(tok)) {
Guido van Rossum78c05351995-01-17 16:12:13 +0000848 tok->done = E_EOF;
849 done = 1;
850 }
851 else
852 tok->done = E_OK;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000853 }
854 tok->lineno++;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000855 /* Read until '\n' or EOF */
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000856 while (!done) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000857 Py_ssize_t curstart = tok->start == NULL ? -1 :
858 tok->start - tok->buf;
859 Py_ssize_t curvalid = tok->inp - tok->buf;
860 Py_ssize_t newsize = curvalid + BUFSIZ;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000861 char *newbuf = tok->buf;
Guido van Rossum86bea461997-04-29 21:03:06 +0000862 PyMem_RESIZE(newbuf, char, newsize);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000863 if (newbuf == NULL) {
864 tok->done = E_NOMEM;
865 tok->cur = tok->inp;
866 return EOF;
867 }
868 tok->buf = newbuf;
869 tok->inp = tok->buf + curvalid;
870 tok->end = tok->buf + newsize;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000871 tok->start = curstart < 0 ? NULL :
872 tok->buf + curstart;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000873 if (decoding_fgets(tok->inp,
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000874 (int)(tok->end - tok->inp),
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000875 tok) == NULL) {
Thomas Wouters7eaf2aa2006-03-02 20:41:27 +0000876 /* Break out early on decoding
877 errors, as tok->buf will be NULL
878 */
879 if (tok->decoding_erred)
880 return EOF;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000881 /* Last line does not end in \n,
882 fake one */
883 strcpy(tok->inp, "\n");
884 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000885 tok->inp = strchr(tok->inp, '\0');
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000886 done = tok->inp[-1] == '\n';
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000887 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000888 tok->cur = tok->buf + cur;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000889 tok->line_start = tok->cur;
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000890 /* replace "\r\n" with "\n" */
Guido van Rossum2d45be11997-04-11 19:16:25 +0000891 /* For Mac we leave the \r, giving a syntax error */
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000892 pt = tok->inp - 2;
893 if (pt >= tok->buf && *pt == '\r') {
894 *pt++ = '\n';
895 *pt = '\0';
896 tok->inp = pt;
897 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000898 }
899 if (tok->done != E_OK) {
900 if (tok->prompt != NULL)
Guido van Rossum6e73bf41998-08-25 18:13:04 +0000901 PySys_WriteStderr("\n");
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000902 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000903 return EOF;
904 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000905 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000906 /*NOTREACHED*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000907}
908
909
910/* Back-up one character */
911
912static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000913tok_backup(register struct tok_state *tok, register int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000914{
915 if (c != EOF) {
Guido van Rossum588633d1994-12-30 15:46:02 +0000916 if (--tok->cur < tok->buf)
Guido van Rossum86bea461997-04-29 21:03:06 +0000917 Py_FatalError("tok_backup: begin of buffer");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000918 if (*tok->cur != c)
919 *tok->cur = c;
920 }
921}
922
923
924/* Return the token corresponding to a single character */
925
926int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000927PyToken_OneChar(int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000928{
929 switch (c) {
930 case '(': return LPAR;
931 case ')': return RPAR;
932 case '[': return LSQB;
933 case ']': return RSQB;
934 case ':': return COLON;
935 case ',': return COMMA;
936 case ';': return SEMI;
937 case '+': return PLUS;
938 case '-': return MINUS;
939 case '*': return STAR;
940 case '/': return SLASH;
941 case '|': return VBAR;
942 case '&': return AMPER;
943 case '<': return LESS;
944 case '>': return GREATER;
945 case '=': return EQUAL;
946 case '.': return DOT;
947 case '%': return PERCENT;
948 case '`': return BACKQUOTE;
949 case '{': return LBRACE;
950 case '}': return RBRACE;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000951 case '^': return CIRCUMFLEX;
952 case '~': return TILDE;
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000953 case '@': return AT;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000954 default: return OP;
955 }
956}
957
958
Guido van Rossumfbab9051991-10-20 20:25:03 +0000959int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000960PyToken_TwoChars(int c1, int c2)
Guido van Rossumfbab9051991-10-20 20:25:03 +0000961{
962 switch (c1) {
963 case '=':
964 switch (c2) {
965 case '=': return EQEQUAL;
966 }
967 break;
968 case '!':
969 switch (c2) {
970 case '=': return NOTEQUAL;
971 }
972 break;
973 case '<':
974 switch (c2) {
975 case '>': return NOTEQUAL;
976 case '=': return LESSEQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000977 case '<': return LEFTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000978 }
979 break;
980 case '>':
981 switch (c2) {
982 case '=': return GREATEREQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000983 case '>': return RIGHTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000984 }
985 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000986 case '+':
987 switch (c2) {
988 case '=': return PLUSEQUAL;
989 }
990 break;
991 case '-':
992 switch (c2) {
993 case '=': return MINEQUAL;
994 }
995 break;
Guido van Rossumf595fde1996-01-12 01:31:58 +0000996 case '*':
997 switch (c2) {
998 case '*': return DOUBLESTAR;
Thomas Wouters434d0822000-08-24 20:11:32 +0000999 case '=': return STAREQUAL;
1000 }
1001 break;
1002 case '/':
1003 switch (c2) {
Guido van Rossum4668b002001-08-08 05:00:18 +00001004 case '/': return DOUBLESLASH;
Thomas Wouters434d0822000-08-24 20:11:32 +00001005 case '=': return SLASHEQUAL;
1006 }
1007 break;
1008 case '|':
1009 switch (c2) {
1010 case '=': return VBAREQUAL;
1011 }
1012 break;
1013 case '%':
1014 switch (c2) {
1015 case '=': return PERCENTEQUAL;
1016 }
1017 break;
1018 case '&':
1019 switch (c2) {
1020 case '=': return AMPEREQUAL;
1021 }
1022 break;
1023 case '^':
1024 switch (c2) {
1025 case '=': return CIRCUMFLEXEQUAL;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001026 }
1027 break;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001028 }
1029 return OP;
1030}
1031
Thomas Wouters434d0822000-08-24 20:11:32 +00001032int
1033PyToken_ThreeChars(int c1, int c2, int c3)
1034{
1035 switch (c1) {
1036 case '<':
1037 switch (c2) {
1038 case '<':
1039 switch (c3) {
1040 case '=':
1041 return LEFTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001042 }
1043 break;
1044 }
1045 break;
1046 case '>':
1047 switch (c2) {
1048 case '>':
1049 switch (c3) {
1050 case '=':
1051 return RIGHTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001052 }
1053 break;
1054 }
1055 break;
1056 case '*':
1057 switch (c2) {
1058 case '*':
1059 switch (c3) {
1060 case '=':
1061 return DOUBLESTAREQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001062 }
1063 break;
1064 }
1065 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001066 case '/':
1067 switch (c2) {
1068 case '/':
1069 switch (c3) {
1070 case '=':
1071 return DOUBLESLASHEQUAL;
1072 }
1073 break;
1074 }
1075 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001076 }
1077 return OP;
1078}
Guido van Rossumfbab9051991-10-20 20:25:03 +00001079
Guido van Rossum926f13a1998-04-09 21:38:06 +00001080static int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001081indenterror(struct tok_state *tok)
Guido van Rossum926f13a1998-04-09 21:38:06 +00001082{
1083 if (tok->alterror) {
Fred Drake85f36392000-07-11 17:53:00 +00001084 tok->done = E_TABSPACE;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001085 tok->cur = tok->inp;
1086 return 1;
1087 }
1088 if (tok->altwarning) {
Fred Drake85f36392000-07-11 17:53:00 +00001089 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
1090 "in indentation\n", tok->filename);
Guido van Rossum926f13a1998-04-09 21:38:06 +00001091 tok->altwarning = 0;
1092 }
1093 return 0;
1094}
1095
1096
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001097/* Get next token, after space stripping etc. */
1098
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001099static int
1100tok_get(register struct tok_state *tok, char **p_start, char **p_end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001101{
1102 register int c;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001103 int blankline;
1104
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001105 *p_start = *p_end = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001106 nextline:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001107 tok->start = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001108 blankline = 0;
1109
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001110 /* Get indentation level */
1111 if (tok->atbol) {
1112 register int col = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001113 register int altcol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001114 tok->atbol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001115 for (;;) {
1116 c = tok_nextc(tok);
1117 if (c == ' ')
Guido van Rossum926f13a1998-04-09 21:38:06 +00001118 col++, altcol++;
1119 else if (c == '\t') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001120 col = (col/tok->tabsize + 1) * tok->tabsize;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001121 altcol = (altcol/tok->alttabsize + 1)
1122 * tok->alttabsize;
1123 }
Guido van Rossum94d32b11995-07-07 22:27:27 +00001124 else if (c == '\014') /* Control-L (formfeed) */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001125 col = altcol = 0; /* For Emacs users */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001126 else
1127 break;
1128 }
1129 tok_backup(tok, c);
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001130 if (c == '#' || c == '\n') {
1131 /* Lines with only whitespace and/or comments
1132 shouldn't affect the indentation and are
1133 not passed to the parser as NEWLINE tokens,
1134 except *totally* empty lines in interactive
1135 mode, which signal the end of a command group. */
1136 if (col == 0 && c == '\n' && tok->prompt != NULL)
1137 blankline = 0; /* Let it through */
1138 else
1139 blankline = 1; /* Ignore completely */
1140 /* We can't jump back right here since we still
1141 may need to skip to the end of a comment */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001142 }
Guido van Rossuma849b831993-05-12 11:35:44 +00001143 if (!blankline && tok->level == 0) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001144 if (col == tok->indstack[tok->indent]) {
1145 /* No change */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001146 if (altcol != tok->altindstack[tok->indent]) {
1147 if (indenterror(tok))
1148 return ERRORTOKEN;
1149 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001150 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001151 else if (col > tok->indstack[tok->indent]) {
1152 /* Indent -- always one */
1153 if (tok->indent+1 >= MAXINDENT) {
Fred Drake85f36392000-07-11 17:53:00 +00001154 tok->done = E_TOODEEP;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001155 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001156 return ERRORTOKEN;
1157 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001158 if (altcol <= tok->altindstack[tok->indent]) {
1159 if (indenterror(tok))
1160 return ERRORTOKEN;
1161 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001162 tok->pendin++;
1163 tok->indstack[++tok->indent] = col;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001164 tok->altindstack[tok->indent] = altcol;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001165 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001166 else /* col < tok->indstack[tok->indent] */ {
1167 /* Dedent -- any number, must be consistent */
1168 while (tok->indent > 0 &&
1169 col < tok->indstack[tok->indent]) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001170 tok->pendin--;
Guido van Rossum54758fa1998-02-16 22:18:00 +00001171 tok->indent--;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001172 }
1173 if (col != tok->indstack[tok->indent]) {
Fred Drake85f36392000-07-11 17:53:00 +00001174 tok->done = E_DEDENT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001175 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001176 return ERRORTOKEN;
1177 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001178 if (altcol != tok->altindstack[tok->indent]) {
1179 if (indenterror(tok))
1180 return ERRORTOKEN;
1181 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001182 }
1183 }
1184 }
1185
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001186 tok->start = tok->cur;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001187
1188 /* Return pending indents/dedents */
1189 if (tok->pendin != 0) {
1190 if (tok->pendin < 0) {
1191 tok->pendin++;
1192 return DEDENT;
1193 }
1194 else {
1195 tok->pendin--;
1196 return INDENT;
1197 }
1198 }
1199
1200 again:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001201 tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001202 /* Skip spaces */
1203 do {
1204 c = tok_nextc(tok);
Guido van Rossum94d32b11995-07-07 22:27:27 +00001205 } while (c == ' ' || c == '\t' || c == '\014');
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001206
1207 /* Set start of current token */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001208 tok->start = tok->cur - 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001209
Guido van Rossumab5ca152000-03-31 00:52:27 +00001210 /* Skip comment, while looking for tab-setting magic */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001211 if (c == '#') {
Guido van Rossumab5ca152000-03-31 00:52:27 +00001212 static char *tabforms[] = {
1213 "tab-width:", /* Emacs */
1214 ":tabstop=", /* vim, full form */
1215 ":ts=", /* vim, abbreviated form */
1216 "set tabsize=", /* will vi never die? */
1217 /* more templates can be added here to support other editors */
1218 };
1219 char cbuf[80];
1220 char *tp, **cp;
1221 tp = cbuf;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001222 do {
Guido van Rossumab5ca152000-03-31 00:52:27 +00001223 *tp++ = c = tok_nextc(tok);
1224 } while (c != EOF && c != '\n' &&
1225 tp - cbuf + 1 < sizeof(cbuf));
1226 *tp = '\0';
1227 for (cp = tabforms;
1228 cp < tabforms + sizeof(tabforms)/sizeof(tabforms[0]);
1229 cp++) {
1230 if ((tp = strstr(cbuf, *cp))) {
1231 int newsize = atoi(tp + strlen(*cp));
1232
1233 if (newsize >= 1 && newsize <= 40) {
1234 tok->tabsize = newsize;
Guido van Rossum6c981ad2000-04-03 23:02:17 +00001235 if (Py_VerboseFlag)
1236 PySys_WriteStderr(
Guido van Rossumab5ca152000-03-31 00:52:27 +00001237 "Tab size set to %d\n",
1238 newsize);
1239 }
1240 }
1241 }
1242 while (c != EOF && c != '\n')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001243 c = tok_nextc(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001244 }
1245
1246 /* Check for EOF and errors now */
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001247 if (c == EOF) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001248 return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001249 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001250
1251 /* Identifier (most frequent token!) */
1252 if (isalpha(c) || c == '_') {
Guido van Rossum86016cb2000-03-10 22:56:54 +00001253 /* Process r"", u"" and ur"" */
Guido van Rossum5026cb41997-04-25 17:32:00 +00001254 switch (c) {
1255 case 'r':
1256 case 'R':
1257 c = tok_nextc(tok);
1258 if (c == '"' || c == '\'')
1259 goto letter_quote;
Guido van Rossum86016cb2000-03-10 22:56:54 +00001260 break;
1261 case 'u':
1262 case 'U':
1263 c = tok_nextc(tok);
1264 if (c == 'r' || c == 'R')
1265 c = tok_nextc(tok);
1266 if (c == '"' || c == '\'')
1267 goto letter_quote;
1268 break;
Guido van Rossum5026cb41997-04-25 17:32:00 +00001269 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001270 while (isalnum(c) || c == '_') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001271 c = tok_nextc(tok);
Guido van Rossum24dacb31997-04-06 03:46:20 +00001272 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001273 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001274 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001275 *p_end = tok->cur;
1276 return NAME;
1277 }
1278
1279 /* Newline */
1280 if (c == '\n') {
1281 tok->atbol = 1;
Guido van Rossuma849b831993-05-12 11:35:44 +00001282 if (blankline || tok->level > 0)
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001283 goto nextline;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001284 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001285 *p_end = tok->cur - 1; /* Leave '\n' out of the string */
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001286 tok->cont_line = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001287 return NEWLINE;
1288 }
1289
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001290 /* Period or number starting with period? */
1291 if (c == '.') {
1292 c = tok_nextc(tok);
1293 if (isdigit(c)) {
1294 goto fraction;
1295 }
1296 else {
1297 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001298 *p_start = tok->start;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001299 *p_end = tok->cur;
1300 return DOT;
1301 }
1302 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001303
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001304 /* Number */
1305 if (isdigit(c)) {
1306 if (c == '0') {
Tim Petersd507dab2001-08-30 20:51:59 +00001307 /* Hex or octal -- maybe. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001308 c = tok_nextc(tok);
1309 if (c == '.')
1310 goto fraction;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001311#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001312 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001313 goto imaginary;
1314#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001315 if (c == 'x' || c == 'X') {
1316 /* Hex */
1317 do {
1318 c = tok_nextc(tok);
1319 } while (isxdigit(c));
1320 }
1321 else {
Tim Petersd507dab2001-08-30 20:51:59 +00001322 int found_decimal = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001323 /* Octal; c is first char of it */
1324 /* There's no 'isoctdigit' macro, sigh */
1325 while ('0' <= c && c < '8') {
1326 c = tok_nextc(tok);
1327 }
Tim Petersd507dab2001-08-30 20:51:59 +00001328 if (isdigit(c)) {
1329 found_decimal = 1;
1330 do {
1331 c = tok_nextc(tok);
1332 } while (isdigit(c));
1333 }
1334 if (c == '.')
1335 goto fraction;
1336 else if (c == 'e' || c == 'E')
1337 goto exponent;
1338#ifndef WITHOUT_COMPLEX
1339 else if (c == 'j' || c == 'J')
1340 goto imaginary;
1341#endif
1342 else if (found_decimal) {
1343 tok->done = E_TOKEN;
1344 tok_backup(tok, c);
1345 return ERRORTOKEN;
1346 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347 }
Guido van Rossumf023c461991-05-05 20:16:20 +00001348 if (c == 'l' || c == 'L')
1349 c = tok_nextc(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001350 }
1351 else {
1352 /* Decimal */
1353 do {
1354 c = tok_nextc(tok);
1355 } while (isdigit(c));
Guido van Rossumf023c461991-05-05 20:16:20 +00001356 if (c == 'l' || c == 'L')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001357 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001358 else {
Tim Peters9aa70d92001-08-27 19:19:28 +00001359 /* Accept floating point numbers. */
Guido van Rossumf023c461991-05-05 20:16:20 +00001360 if (c == '.') {
1361 fraction:
1362 /* Fraction */
1363 do {
1364 c = tok_nextc(tok);
1365 } while (isdigit(c));
1366 }
1367 if (c == 'e' || c == 'E') {
Tim Petersd507dab2001-08-30 20:51:59 +00001368 exponent:
Guido van Rossumf023c461991-05-05 20:16:20 +00001369 /* Exponent part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001370 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001371 if (c == '+' || c == '-')
1372 c = tok_nextc(tok);
Tim Peters9aa70d92001-08-27 19:19:28 +00001373 if (!isdigit(c)) {
1374 tok->done = E_TOKEN;
1375 tok_backup(tok, c);
1376 return ERRORTOKEN;
Guido van Rossumf023c461991-05-05 20:16:20 +00001377 }
Tim Peters9aa70d92001-08-27 19:19:28 +00001378 do {
1379 c = tok_nextc(tok);
1380 } while (isdigit(c));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001381 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001382#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001383 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001384 /* Imaginary part */
1385 imaginary:
1386 c = tok_nextc(tok);
1387#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001388 }
1389 }
1390 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001391 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001392 *p_end = tok->cur;
1393 return NUMBER;
1394 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001395
1396 letter_quote:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001397 /* String */
1398 if (c == '\'' || c == '"') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001399 Py_ssize_t quote2 = tok->cur - tok->start + 1;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001400 int quote = c;
1401 int triple = 0;
1402 int tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001403 for (;;) {
1404 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001405 if (c == '\n') {
1406 if (!triple) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001407 tok->done = E_EOLS;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001408 tok_backup(tok, c);
1409 return ERRORTOKEN;
1410 }
1411 tripcount = 0;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001412 tok->cont_line = 1; /* multiline string. */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001413 }
1414 else if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001415 if (triple)
1416 tok->done = E_EOFS;
1417 else
1418 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001419 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001420 return ERRORTOKEN;
1421 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001422 else if (c == quote) {
1423 tripcount++;
Guido van Rossum35685241998-02-16 15:42:50 +00001424 if (tok->cur - tok->start == quote2) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001425 c = tok_nextc(tok);
1426 if (c == quote) {
1427 triple = 1;
1428 tripcount = 0;
1429 continue;
1430 }
1431 tok_backup(tok, c);
1432 }
1433 if (!triple || tripcount == 3)
1434 break;
1435 }
1436 else if (c == '\\') {
1437 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001438 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001439 if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001440 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001441 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001442 return ERRORTOKEN;
1443 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001444 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001445 else
1446 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001447 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001448 *p_start = tok->start;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001449 *p_end = tok->cur;
1450 return STRING;
1451 }
1452
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001453 /* Line continuation */
1454 if (c == '\\') {
1455 c = tok_nextc(tok);
1456 if (c != '\n') {
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001457 tok->done = E_LINECONT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001458 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001459 return ERRORTOKEN;
1460 }
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001461 tok->cont_line = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001462 goto again; /* Read next line */
1463 }
1464
Guido van Rossumfbab9051991-10-20 20:25:03 +00001465 /* Check for two-character token */
1466 {
1467 int c2 = tok_nextc(tok);
Guido van Rossum86bea461997-04-29 21:03:06 +00001468 int token = PyToken_TwoChars(c, c2);
Guido van Rossumfbab9051991-10-20 20:25:03 +00001469 if (token != OP) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001470 int c3 = tok_nextc(tok);
1471 int token3 = PyToken_ThreeChars(c, c2, c3);
1472 if (token3 != OP) {
1473 token = token3;
1474 } else {
1475 tok_backup(tok, c3);
1476 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001477 *p_start = tok->start;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001478 *p_end = tok->cur;
1479 return token;
1480 }
1481 tok_backup(tok, c2);
1482 }
1483
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001484 /* Keep track of parentheses nesting level */
Guido van Rossuma849b831993-05-12 11:35:44 +00001485 switch (c) {
1486 case '(':
1487 case '[':
1488 case '{':
1489 tok->level++;
1490 break;
1491 case ')':
1492 case ']':
1493 case '}':
1494 tok->level--;
1495 break;
1496 }
1497
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001498 /* Punctuation character */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001499 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001500 *p_end = tok->cur;
Guido van Rossum86bea461997-04-29 21:03:06 +00001501 return PyToken_OneChar(c);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001502}
1503
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001504int
1505PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
1506{
1507 int result = tok_get(tok, p_start, p_end);
1508 if (tok->decoding_erred) {
1509 result = ERRORTOKEN;
1510 tok->done = E_DECODE;
1511 }
1512 return result;
1513}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001514
Guido van Rossum408027e1996-12-30 16:17:54 +00001515#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001516
1517void
Thomas Wouters23c9e002000-07-22 19:20:54 +00001518tok_dump(int type, char *start, char *end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001519{
Guido van Rossum86bea461997-04-29 21:03:06 +00001520 printf("%s", _PyParser_TokenNames[type]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001521 if (type == NAME || type == NUMBER || type == STRING || type == OP)
1522 printf("(%.*s)", (int)(end - start), start);
1523}
1524
1525#endif