blob: 4174e9cc1d5949bb9685ab03ac5926114b9310e2 [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) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000876 /* Last line does not end in \n,
877 fake one */
878 strcpy(tok->inp, "\n");
879 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000880 tok->inp = strchr(tok->inp, '\0');
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000881 done = tok->inp[-1] == '\n';
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000882 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000883 tok->cur = tok->buf + cur;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000884 tok->line_start = tok->cur;
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000885 /* replace "\r\n" with "\n" */
Guido van Rossum2d45be11997-04-11 19:16:25 +0000886 /* For Mac we leave the \r, giving a syntax error */
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000887 pt = tok->inp - 2;
888 if (pt >= tok->buf && *pt == '\r') {
889 *pt++ = '\n';
890 *pt = '\0';
891 tok->inp = pt;
892 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000893 }
894 if (tok->done != E_OK) {
895 if (tok->prompt != NULL)
Guido van Rossum6e73bf41998-08-25 18:13:04 +0000896 PySys_WriteStderr("\n");
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000897 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000898 return EOF;
899 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000900 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000901 /*NOTREACHED*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000902}
903
904
905/* Back-up one character */
906
907static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000908tok_backup(register struct tok_state *tok, register int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000909{
910 if (c != EOF) {
Guido van Rossum588633d1994-12-30 15:46:02 +0000911 if (--tok->cur < tok->buf)
Guido van Rossum86bea461997-04-29 21:03:06 +0000912 Py_FatalError("tok_backup: begin of buffer");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000913 if (*tok->cur != c)
914 *tok->cur = c;
915 }
916}
917
918
919/* Return the token corresponding to a single character */
920
921int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000922PyToken_OneChar(int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000923{
924 switch (c) {
925 case '(': return LPAR;
926 case ')': return RPAR;
927 case '[': return LSQB;
928 case ']': return RSQB;
929 case ':': return COLON;
930 case ',': return COMMA;
931 case ';': return SEMI;
932 case '+': return PLUS;
933 case '-': return MINUS;
934 case '*': return STAR;
935 case '/': return SLASH;
936 case '|': return VBAR;
937 case '&': return AMPER;
938 case '<': return LESS;
939 case '>': return GREATER;
940 case '=': return EQUAL;
941 case '.': return DOT;
942 case '%': return PERCENT;
943 case '`': return BACKQUOTE;
944 case '{': return LBRACE;
945 case '}': return RBRACE;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000946 case '^': return CIRCUMFLEX;
947 case '~': return TILDE;
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000948 case '@': return AT;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000949 default: return OP;
950 }
951}
952
953
Guido van Rossumfbab9051991-10-20 20:25:03 +0000954int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000955PyToken_TwoChars(int c1, int c2)
Guido van Rossumfbab9051991-10-20 20:25:03 +0000956{
957 switch (c1) {
958 case '=':
959 switch (c2) {
960 case '=': return EQEQUAL;
961 }
962 break;
963 case '!':
964 switch (c2) {
965 case '=': return NOTEQUAL;
966 }
967 break;
968 case '<':
969 switch (c2) {
970 case '>': return NOTEQUAL;
971 case '=': return LESSEQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000972 case '<': return LEFTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000973 }
974 break;
975 case '>':
976 switch (c2) {
977 case '=': return GREATEREQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000978 case '>': return RIGHTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000979 }
980 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000981 case '+':
982 switch (c2) {
983 case '=': return PLUSEQUAL;
984 }
985 break;
986 case '-':
987 switch (c2) {
988 case '=': return MINEQUAL;
989 }
990 break;
Guido van Rossumf595fde1996-01-12 01:31:58 +0000991 case '*':
992 switch (c2) {
993 case '*': return DOUBLESTAR;
Thomas Wouters434d0822000-08-24 20:11:32 +0000994 case '=': return STAREQUAL;
995 }
996 break;
997 case '/':
998 switch (c2) {
Guido van Rossum4668b002001-08-08 05:00:18 +0000999 case '/': return DOUBLESLASH;
Thomas Wouters434d0822000-08-24 20:11:32 +00001000 case '=': return SLASHEQUAL;
1001 }
1002 break;
1003 case '|':
1004 switch (c2) {
1005 case '=': return VBAREQUAL;
1006 }
1007 break;
1008 case '%':
1009 switch (c2) {
1010 case '=': return PERCENTEQUAL;
1011 }
1012 break;
1013 case '&':
1014 switch (c2) {
1015 case '=': return AMPEREQUAL;
1016 }
1017 break;
1018 case '^':
1019 switch (c2) {
1020 case '=': return CIRCUMFLEXEQUAL;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001021 }
1022 break;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001023 }
1024 return OP;
1025}
1026
Thomas Wouters434d0822000-08-24 20:11:32 +00001027int
1028PyToken_ThreeChars(int c1, int c2, int c3)
1029{
1030 switch (c1) {
1031 case '<':
1032 switch (c2) {
1033 case '<':
1034 switch (c3) {
1035 case '=':
1036 return LEFTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001037 }
1038 break;
1039 }
1040 break;
1041 case '>':
1042 switch (c2) {
1043 case '>':
1044 switch (c3) {
1045 case '=':
1046 return RIGHTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001047 }
1048 break;
1049 }
1050 break;
1051 case '*':
1052 switch (c2) {
1053 case '*':
1054 switch (c3) {
1055 case '=':
1056 return DOUBLESTAREQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001057 }
1058 break;
1059 }
1060 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001061 case '/':
1062 switch (c2) {
1063 case '/':
1064 switch (c3) {
1065 case '=':
1066 return DOUBLESLASHEQUAL;
1067 }
1068 break;
1069 }
1070 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001071 }
1072 return OP;
1073}
Guido van Rossumfbab9051991-10-20 20:25:03 +00001074
Guido van Rossum926f13a1998-04-09 21:38:06 +00001075static int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001076indenterror(struct tok_state *tok)
Guido van Rossum926f13a1998-04-09 21:38:06 +00001077{
1078 if (tok->alterror) {
Fred Drake85f36392000-07-11 17:53:00 +00001079 tok->done = E_TABSPACE;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001080 tok->cur = tok->inp;
1081 return 1;
1082 }
1083 if (tok->altwarning) {
Fred Drake85f36392000-07-11 17:53:00 +00001084 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
1085 "in indentation\n", tok->filename);
Guido van Rossum926f13a1998-04-09 21:38:06 +00001086 tok->altwarning = 0;
1087 }
1088 return 0;
1089}
1090
1091
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001092/* Get next token, after space stripping etc. */
1093
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001094static int
1095tok_get(register struct tok_state *tok, char **p_start, char **p_end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001096{
1097 register int c;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001098 int blankline;
1099
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001100 *p_start = *p_end = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001101 nextline:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001102 tok->start = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001103 blankline = 0;
1104
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105 /* Get indentation level */
1106 if (tok->atbol) {
1107 register int col = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001108 register int altcol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001109 tok->atbol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001110 for (;;) {
1111 c = tok_nextc(tok);
1112 if (c == ' ')
Guido van Rossum926f13a1998-04-09 21:38:06 +00001113 col++, altcol++;
1114 else if (c == '\t') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001115 col = (col/tok->tabsize + 1) * tok->tabsize;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001116 altcol = (altcol/tok->alttabsize + 1)
1117 * tok->alttabsize;
1118 }
Guido van Rossum94d32b11995-07-07 22:27:27 +00001119 else if (c == '\014') /* Control-L (formfeed) */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001120 col = altcol = 0; /* For Emacs users */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001121 else
1122 break;
1123 }
1124 tok_backup(tok, c);
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001125 if (c == '#' || c == '\n') {
1126 /* Lines with only whitespace and/or comments
1127 shouldn't affect the indentation and are
1128 not passed to the parser as NEWLINE tokens,
1129 except *totally* empty lines in interactive
1130 mode, which signal the end of a command group. */
1131 if (col == 0 && c == '\n' && tok->prompt != NULL)
1132 blankline = 0; /* Let it through */
1133 else
1134 blankline = 1; /* Ignore completely */
1135 /* We can't jump back right here since we still
1136 may need to skip to the end of a comment */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001137 }
Guido van Rossuma849b831993-05-12 11:35:44 +00001138 if (!blankline && tok->level == 0) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001139 if (col == tok->indstack[tok->indent]) {
1140 /* No change */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001141 if (altcol != tok->altindstack[tok->indent]) {
1142 if (indenterror(tok))
1143 return ERRORTOKEN;
1144 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001145 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001146 else if (col > tok->indstack[tok->indent]) {
1147 /* Indent -- always one */
1148 if (tok->indent+1 >= MAXINDENT) {
Fred Drake85f36392000-07-11 17:53:00 +00001149 tok->done = E_TOODEEP;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001150 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001151 return ERRORTOKEN;
1152 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001153 if (altcol <= tok->altindstack[tok->indent]) {
1154 if (indenterror(tok))
1155 return ERRORTOKEN;
1156 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001157 tok->pendin++;
1158 tok->indstack[++tok->indent] = col;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001159 tok->altindstack[tok->indent] = altcol;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001160 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001161 else /* col < tok->indstack[tok->indent] */ {
1162 /* Dedent -- any number, must be consistent */
1163 while (tok->indent > 0 &&
1164 col < tok->indstack[tok->indent]) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001165 tok->pendin--;
Guido van Rossum54758fa1998-02-16 22:18:00 +00001166 tok->indent--;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001167 }
1168 if (col != tok->indstack[tok->indent]) {
Fred Drake85f36392000-07-11 17:53:00 +00001169 tok->done = E_DEDENT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001170 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001171 return ERRORTOKEN;
1172 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001173 if (altcol != tok->altindstack[tok->indent]) {
1174 if (indenterror(tok))
1175 return ERRORTOKEN;
1176 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001177 }
1178 }
1179 }
1180
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001181 tok->start = tok->cur;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001182
1183 /* Return pending indents/dedents */
1184 if (tok->pendin != 0) {
1185 if (tok->pendin < 0) {
1186 tok->pendin++;
1187 return DEDENT;
1188 }
1189 else {
1190 tok->pendin--;
1191 return INDENT;
1192 }
1193 }
1194
1195 again:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001196 tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001197 /* Skip spaces */
1198 do {
1199 c = tok_nextc(tok);
Guido van Rossum94d32b11995-07-07 22:27:27 +00001200 } while (c == ' ' || c == '\t' || c == '\014');
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001201
1202 /* Set start of current token */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001203 tok->start = tok->cur - 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001204
Guido van Rossumab5ca152000-03-31 00:52:27 +00001205 /* Skip comment, while looking for tab-setting magic */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001206 if (c == '#') {
Guido van Rossumab5ca152000-03-31 00:52:27 +00001207 static char *tabforms[] = {
1208 "tab-width:", /* Emacs */
1209 ":tabstop=", /* vim, full form */
1210 ":ts=", /* vim, abbreviated form */
1211 "set tabsize=", /* will vi never die? */
1212 /* more templates can be added here to support other editors */
1213 };
1214 char cbuf[80];
1215 char *tp, **cp;
1216 tp = cbuf;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001217 do {
Guido van Rossumab5ca152000-03-31 00:52:27 +00001218 *tp++ = c = tok_nextc(tok);
1219 } while (c != EOF && c != '\n' &&
1220 tp - cbuf + 1 < sizeof(cbuf));
1221 *tp = '\0';
1222 for (cp = tabforms;
1223 cp < tabforms + sizeof(tabforms)/sizeof(tabforms[0]);
1224 cp++) {
1225 if ((tp = strstr(cbuf, *cp))) {
1226 int newsize = atoi(tp + strlen(*cp));
1227
1228 if (newsize >= 1 && newsize <= 40) {
1229 tok->tabsize = newsize;
Guido van Rossum6c981ad2000-04-03 23:02:17 +00001230 if (Py_VerboseFlag)
1231 PySys_WriteStderr(
Guido van Rossumab5ca152000-03-31 00:52:27 +00001232 "Tab size set to %d\n",
1233 newsize);
1234 }
1235 }
1236 }
1237 while (c != EOF && c != '\n')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001238 c = tok_nextc(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001239 }
1240
1241 /* Check for EOF and errors now */
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001242 if (c == EOF) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001243 return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001244 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001245
1246 /* Identifier (most frequent token!) */
1247 if (isalpha(c) || c == '_') {
Guido van Rossum86016cb2000-03-10 22:56:54 +00001248 /* Process r"", u"" and ur"" */
Guido van Rossum5026cb41997-04-25 17:32:00 +00001249 switch (c) {
1250 case 'r':
1251 case 'R':
1252 c = tok_nextc(tok);
1253 if (c == '"' || c == '\'')
1254 goto letter_quote;
Guido van Rossum86016cb2000-03-10 22:56:54 +00001255 break;
1256 case 'u':
1257 case 'U':
1258 c = tok_nextc(tok);
1259 if (c == 'r' || c == 'R')
1260 c = tok_nextc(tok);
1261 if (c == '"' || c == '\'')
1262 goto letter_quote;
1263 break;
Guido van Rossum5026cb41997-04-25 17:32:00 +00001264 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001265 while (isalnum(c) || c == '_') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001266 c = tok_nextc(tok);
Guido van Rossum24dacb31997-04-06 03:46:20 +00001267 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001268 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001269 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001270 *p_end = tok->cur;
1271 return NAME;
1272 }
1273
1274 /* Newline */
1275 if (c == '\n') {
1276 tok->atbol = 1;
Guido van Rossuma849b831993-05-12 11:35:44 +00001277 if (blankline || tok->level > 0)
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001278 goto nextline;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001279 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001280 *p_end = tok->cur - 1; /* Leave '\n' out of the string */
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001281 tok->cont_line = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001282 return NEWLINE;
1283 }
1284
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001285 /* Period or number starting with period? */
1286 if (c == '.') {
1287 c = tok_nextc(tok);
1288 if (isdigit(c)) {
1289 goto fraction;
1290 }
1291 else {
1292 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001293 *p_start = tok->start;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001294 *p_end = tok->cur;
1295 return DOT;
1296 }
1297 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001298
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001299 /* Number */
1300 if (isdigit(c)) {
1301 if (c == '0') {
Tim Petersd507dab2001-08-30 20:51:59 +00001302 /* Hex or octal -- maybe. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001303 c = tok_nextc(tok);
1304 if (c == '.')
1305 goto fraction;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001306#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001307 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001308 goto imaginary;
1309#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001310 if (c == 'x' || c == 'X') {
1311 /* Hex */
1312 do {
1313 c = tok_nextc(tok);
1314 } while (isxdigit(c));
1315 }
1316 else {
Tim Petersd507dab2001-08-30 20:51:59 +00001317 int found_decimal = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001318 /* Octal; c is first char of it */
1319 /* There's no 'isoctdigit' macro, sigh */
1320 while ('0' <= c && c < '8') {
1321 c = tok_nextc(tok);
1322 }
Tim Petersd507dab2001-08-30 20:51:59 +00001323 if (isdigit(c)) {
1324 found_decimal = 1;
1325 do {
1326 c = tok_nextc(tok);
1327 } while (isdigit(c));
1328 }
1329 if (c == '.')
1330 goto fraction;
1331 else if (c == 'e' || c == 'E')
1332 goto exponent;
1333#ifndef WITHOUT_COMPLEX
1334 else if (c == 'j' || c == 'J')
1335 goto imaginary;
1336#endif
1337 else if (found_decimal) {
1338 tok->done = E_TOKEN;
1339 tok_backup(tok, c);
1340 return ERRORTOKEN;
1341 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001342 }
Guido van Rossumf023c461991-05-05 20:16:20 +00001343 if (c == 'l' || c == 'L')
1344 c = tok_nextc(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001345 }
1346 else {
1347 /* Decimal */
1348 do {
1349 c = tok_nextc(tok);
1350 } while (isdigit(c));
Guido van Rossumf023c461991-05-05 20:16:20 +00001351 if (c == 'l' || c == 'L')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001352 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001353 else {
Tim Peters9aa70d92001-08-27 19:19:28 +00001354 /* Accept floating point numbers. */
Guido van Rossumf023c461991-05-05 20:16:20 +00001355 if (c == '.') {
1356 fraction:
1357 /* Fraction */
1358 do {
1359 c = tok_nextc(tok);
1360 } while (isdigit(c));
1361 }
1362 if (c == 'e' || c == 'E') {
Tim Petersd507dab2001-08-30 20:51:59 +00001363 exponent:
Guido van Rossumf023c461991-05-05 20:16:20 +00001364 /* Exponent part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001365 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001366 if (c == '+' || c == '-')
1367 c = tok_nextc(tok);
Tim Peters9aa70d92001-08-27 19:19:28 +00001368 if (!isdigit(c)) {
1369 tok->done = E_TOKEN;
1370 tok_backup(tok, c);
1371 return ERRORTOKEN;
Guido van Rossumf023c461991-05-05 20:16:20 +00001372 }
Tim Peters9aa70d92001-08-27 19:19:28 +00001373 do {
1374 c = tok_nextc(tok);
1375 } while (isdigit(c));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001376 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001377#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001378 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001379 /* Imaginary part */
1380 imaginary:
1381 c = tok_nextc(tok);
1382#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001383 }
1384 }
1385 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001386 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001387 *p_end = tok->cur;
1388 return NUMBER;
1389 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001390
1391 letter_quote:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001392 /* String */
1393 if (c == '\'' || c == '"') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001394 Py_ssize_t quote2 = tok->cur - tok->start + 1;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001395 int quote = c;
1396 int triple = 0;
1397 int tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001398 for (;;) {
1399 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001400 if (c == '\n') {
1401 if (!triple) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001402 tok->done = E_EOLS;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001403 tok_backup(tok, c);
1404 return ERRORTOKEN;
1405 }
1406 tripcount = 0;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001407 tok->cont_line = 1; /* multiline string. */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001408 }
1409 else if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001410 if (triple)
1411 tok->done = E_EOFS;
1412 else
1413 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001414 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001415 return ERRORTOKEN;
1416 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001417 else if (c == quote) {
1418 tripcount++;
Guido van Rossum35685241998-02-16 15:42:50 +00001419 if (tok->cur - tok->start == quote2) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001420 c = tok_nextc(tok);
1421 if (c == quote) {
1422 triple = 1;
1423 tripcount = 0;
1424 continue;
1425 }
1426 tok_backup(tok, c);
1427 }
1428 if (!triple || tripcount == 3)
1429 break;
1430 }
1431 else if (c == '\\') {
1432 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001433 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001434 if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001435 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001436 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001437 return ERRORTOKEN;
1438 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001439 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001440 else
1441 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001442 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001443 *p_start = tok->start;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001444 *p_end = tok->cur;
1445 return STRING;
1446 }
1447
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001448 /* Line continuation */
1449 if (c == '\\') {
1450 c = tok_nextc(tok);
1451 if (c != '\n') {
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001452 tok->done = E_LINECONT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001453 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001454 return ERRORTOKEN;
1455 }
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001456 tok->cont_line = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001457 goto again; /* Read next line */
1458 }
1459
Guido van Rossumfbab9051991-10-20 20:25:03 +00001460 /* Check for two-character token */
1461 {
1462 int c2 = tok_nextc(tok);
Guido van Rossum86bea461997-04-29 21:03:06 +00001463 int token = PyToken_TwoChars(c, c2);
Guido van Rossumfbab9051991-10-20 20:25:03 +00001464 if (token != OP) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001465 int c3 = tok_nextc(tok);
1466 int token3 = PyToken_ThreeChars(c, c2, c3);
1467 if (token3 != OP) {
1468 token = token3;
1469 } else {
1470 tok_backup(tok, c3);
1471 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001472 *p_start = tok->start;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001473 *p_end = tok->cur;
1474 return token;
1475 }
1476 tok_backup(tok, c2);
1477 }
1478
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001479 /* Keep track of parentheses nesting level */
Guido van Rossuma849b831993-05-12 11:35:44 +00001480 switch (c) {
1481 case '(':
1482 case '[':
1483 case '{':
1484 tok->level++;
1485 break;
1486 case ')':
1487 case ']':
1488 case '}':
1489 tok->level--;
1490 break;
1491 }
1492
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001493 /* Punctuation character */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001494 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001495 *p_end = tok->cur;
Guido van Rossum86bea461997-04-29 21:03:06 +00001496 return PyToken_OneChar(c);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001497}
1498
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001499int
1500PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
1501{
1502 int result = tok_get(tok, p_start, p_end);
1503 if (tok->decoding_erred) {
1504 result = ERRORTOKEN;
1505 tok->done = E_DECODE;
1506 }
1507 return result;
1508}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001509
Guido van Rossum408027e1996-12-30 16:17:54 +00001510#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001511
1512void
Thomas Wouters23c9e002000-07-22 19:20:54 +00001513tok_dump(int type, char *start, char *end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001514{
Guido van Rossum86bea461997-04-29 21:03:06 +00001515 printf("%s", _PyParser_TokenNames[type]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001516 if (type == NAME || type == NUMBER || type == STRING || type == OP)
1517 printf("(%.*s)", (int)(end - start), start);
1518}
1519
1520#endif