blob: 036bed8e0a8ce401111fa5127a8dc00cda642694 [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;
767 tok->lineno++;
768 tok->inp = end;
Guido van Rossumcf57d8b1998-01-19 22:07:46 +0000769 return Py_CHARMASK(*tok->cur++);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000770 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000771 if (tok->prompt != NULL) {
Martin v. Löwis566f6af2002-10-26 14:39:10 +0000772 char *new = PyOS_Readline(stdin, stdout, tok->prompt);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000773 if (tok->nextprompt != NULL)
774 tok->prompt = tok->nextprompt;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000775 if (new == NULL)
776 tok->done = E_INTR;
777 else if (*new == '\0') {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000778 PyMem_FREE(new);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000779 tok->done = E_EOF;
780 }
Hye-Shik Chang7df44b32004-08-04 17:36:41 +0000781#if !defined(PGEN) && defined(Py_USING_UNICODE)
782 else if (tok_stdin_decode(tok, &new) != 0)
783 PyMem_FREE(new);
784#endif
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000785 else if (tok->start != NULL) {
Guido van Rossum6da34342000-06-28 22:00:02 +0000786 size_t start = tok->start - tok->buf;
787 size_t oldlen = tok->cur - tok->buf;
788 size_t newlen = oldlen + strlen(new);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000789 char *buf = tok->buf;
790 PyMem_RESIZE(buf, char, newlen+1);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000791 tok->lineno++;
792 if (buf == NULL) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000793 PyMem_DEL(tok->buf);
Guido van Rossum588633d1994-12-30 15:46:02 +0000794 tok->buf = NULL;
Guido van Rossumb18618d2000-05-03 23:44:39 +0000795 PyMem_FREE(new);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000796 tok->done = E_NOMEM;
797 return EOF;
798 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000799 tok->buf = buf;
800 tok->cur = tok->buf + oldlen;
801 strcpy(tok->buf + oldlen, new);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000802 PyMem_FREE(new);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000803 tok->inp = tok->buf + newlen;
804 tok->end = tok->inp + 1;
805 tok->start = tok->buf + start;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000806 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000807 else {
808 tok->lineno++;
809 if (tok->buf != NULL)
Guido van Rossumb18618d2000-05-03 23:44:39 +0000810 PyMem_DEL(tok->buf);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000811 tok->buf = new;
812 tok->cur = tok->buf;
813 tok->inp = strchr(tok->buf, '\0');
814 tok->end = tok->inp + 1;
815 }
816 }
817 else {
818 int done = 0;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000819 Py_ssize_t cur = 0;
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000820 char *pt;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000821 if (tok->start == NULL) {
822 if (tok->buf == NULL) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000823 tok->buf = PyMem_NEW(char, BUFSIZ);
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000824 if (tok->buf == NULL) {
825 tok->done = E_NOMEM;
826 return EOF;
827 }
828 tok->end = tok->buf + BUFSIZ;
829 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000830 if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
831 tok) == NULL) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000832 tok->done = E_EOF;
833 done = 1;
834 }
835 else {
836 tok->done = E_OK;
837 tok->inp = strchr(tok->buf, '\0');
838 done = tok->inp[-1] == '\n';
839 }
840 }
841 else {
842 cur = tok->cur - tok->buf;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000843 if (decoding_feof(tok)) {
Guido van Rossum78c05351995-01-17 16:12:13 +0000844 tok->done = E_EOF;
845 done = 1;
846 }
847 else
848 tok->done = E_OK;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000849 }
850 tok->lineno++;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000851 /* Read until '\n' or EOF */
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000852 while (!done) {
Martin v. Löwis18e16552006-02-15 17:27:45 +0000853 Py_ssize_t curstart = tok->start == NULL ? -1 :
854 tok->start - tok->buf;
855 Py_ssize_t curvalid = tok->inp - tok->buf;
856 Py_ssize_t newsize = curvalid + BUFSIZ;
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000857 char *newbuf = tok->buf;
Guido van Rossum86bea461997-04-29 21:03:06 +0000858 PyMem_RESIZE(newbuf, char, newsize);
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000859 if (newbuf == NULL) {
860 tok->done = E_NOMEM;
861 tok->cur = tok->inp;
862 return EOF;
863 }
864 tok->buf = newbuf;
865 tok->inp = tok->buf + curvalid;
866 tok->end = tok->buf + newsize;
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000867 tok->start = curstart < 0 ? NULL :
868 tok->buf + curstart;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000869 if (decoding_fgets(tok->inp,
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000870 (int)(tok->end - tok->inp),
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000871 tok) == NULL) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000872 /* Last line does not end in \n,
873 fake one */
874 strcpy(tok->inp, "\n");
875 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000876 tok->inp = strchr(tok->inp, '\0');
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000877 done = tok->inp[-1] == '\n';
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000878 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +0000879 tok->cur = tok->buf + cur;
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000880 /* replace "\r\n" with "\n" */
Guido van Rossum2d45be11997-04-11 19:16:25 +0000881 /* For Mac we leave the \r, giving a syntax error */
Guido van Rossum2e96eb91995-06-14 18:26:02 +0000882 pt = tok->inp - 2;
883 if (pt >= tok->buf && *pt == '\r') {
884 *pt++ = '\n';
885 *pt = '\0';
886 tok->inp = pt;
887 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000888 }
889 if (tok->done != E_OK) {
890 if (tok->prompt != NULL)
Guido van Rossum6e73bf41998-08-25 18:13:04 +0000891 PySys_WriteStderr("\n");
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000892 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000893 return EOF;
894 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895 }
Guido van Rossum6ac258d1993-05-12 08:24:20 +0000896 /*NOTREACHED*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000897}
898
899
900/* Back-up one character */
901
902static void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000903tok_backup(register struct tok_state *tok, register int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904{
905 if (c != EOF) {
Guido van Rossum588633d1994-12-30 15:46:02 +0000906 if (--tok->cur < tok->buf)
Guido van Rossum86bea461997-04-29 21:03:06 +0000907 Py_FatalError("tok_backup: begin of buffer");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908 if (*tok->cur != c)
909 *tok->cur = c;
910 }
911}
912
913
914/* Return the token corresponding to a single character */
915
916int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000917PyToken_OneChar(int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000918{
919 switch (c) {
920 case '(': return LPAR;
921 case ')': return RPAR;
922 case '[': return LSQB;
923 case ']': return RSQB;
924 case ':': return COLON;
925 case ',': return COMMA;
926 case ';': return SEMI;
927 case '+': return PLUS;
928 case '-': return MINUS;
929 case '*': return STAR;
930 case '/': return SLASH;
931 case '|': return VBAR;
932 case '&': return AMPER;
933 case '<': return LESS;
934 case '>': return GREATER;
935 case '=': return EQUAL;
936 case '.': return DOT;
937 case '%': return PERCENT;
938 case '`': return BACKQUOTE;
939 case '{': return LBRACE;
940 case '}': return RBRACE;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000941 case '^': return CIRCUMFLEX;
942 case '~': return TILDE;
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000943 case '@': return AT;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000944 default: return OP;
945 }
946}
947
948
Guido van Rossumfbab9051991-10-20 20:25:03 +0000949int
Thomas Wouters23c9e002000-07-22 19:20:54 +0000950PyToken_TwoChars(int c1, int c2)
Guido van Rossumfbab9051991-10-20 20:25:03 +0000951{
952 switch (c1) {
953 case '=':
954 switch (c2) {
955 case '=': return EQEQUAL;
956 }
957 break;
958 case '!':
959 switch (c2) {
960 case '=': return NOTEQUAL;
961 }
962 break;
963 case '<':
964 switch (c2) {
965 case '>': return NOTEQUAL;
966 case '=': return LESSEQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000967 case '<': return LEFTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000968 }
969 break;
970 case '>':
971 switch (c2) {
972 case '=': return GREATEREQUAL;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +0000973 case '>': return RIGHTSHIFT;
Guido van Rossumfbab9051991-10-20 20:25:03 +0000974 }
975 break;
Thomas Wouters434d0822000-08-24 20:11:32 +0000976 case '+':
977 switch (c2) {
978 case '=': return PLUSEQUAL;
979 }
980 break;
981 case '-':
982 switch (c2) {
983 case '=': return MINEQUAL;
984 }
985 break;
Guido van Rossumf595fde1996-01-12 01:31:58 +0000986 case '*':
987 switch (c2) {
988 case '*': return DOUBLESTAR;
Thomas Wouters434d0822000-08-24 20:11:32 +0000989 case '=': return STAREQUAL;
990 }
991 break;
992 case '/':
993 switch (c2) {
Guido van Rossum4668b002001-08-08 05:00:18 +0000994 case '/': return DOUBLESLASH;
Thomas Wouters434d0822000-08-24 20:11:32 +0000995 case '=': return SLASHEQUAL;
996 }
997 break;
998 case '|':
999 switch (c2) {
1000 case '=': return VBAREQUAL;
1001 }
1002 break;
1003 case '%':
1004 switch (c2) {
1005 case '=': return PERCENTEQUAL;
1006 }
1007 break;
1008 case '&':
1009 switch (c2) {
1010 case '=': return AMPEREQUAL;
1011 }
1012 break;
1013 case '^':
1014 switch (c2) {
1015 case '=': return CIRCUMFLEXEQUAL;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001016 }
1017 break;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001018 }
1019 return OP;
1020}
1021
Thomas Wouters434d0822000-08-24 20:11:32 +00001022int
1023PyToken_ThreeChars(int c1, int c2, int c3)
1024{
1025 switch (c1) {
1026 case '<':
1027 switch (c2) {
1028 case '<':
1029 switch (c3) {
1030 case '=':
1031 return LEFTSHIFTEQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001032 }
1033 break;
1034 }
1035 break;
1036 case '>':
1037 switch (c2) {
1038 case '>':
1039 switch (c3) {
1040 case '=':
1041 return RIGHTSHIFTEQUAL;
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 DOUBLESTAREQUAL;
Thomas Wouters434d0822000-08-24 20:11:32 +00001052 }
1053 break;
1054 }
1055 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001056 case '/':
1057 switch (c2) {
1058 case '/':
1059 switch (c3) {
1060 case '=':
1061 return DOUBLESLASHEQUAL;
1062 }
1063 break;
1064 }
1065 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001066 }
1067 return OP;
1068}
Guido van Rossumfbab9051991-10-20 20:25:03 +00001069
Guido van Rossum926f13a1998-04-09 21:38:06 +00001070static int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001071indenterror(struct tok_state *tok)
Guido van Rossum926f13a1998-04-09 21:38:06 +00001072{
1073 if (tok->alterror) {
Fred Drake85f36392000-07-11 17:53:00 +00001074 tok->done = E_TABSPACE;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001075 tok->cur = tok->inp;
1076 return 1;
1077 }
1078 if (tok->altwarning) {
Fred Drake85f36392000-07-11 17:53:00 +00001079 PySys_WriteStderr("%s: inconsistent use of tabs and spaces "
1080 "in indentation\n", tok->filename);
Guido van Rossum926f13a1998-04-09 21:38:06 +00001081 tok->altwarning = 0;
1082 }
1083 return 0;
1084}
1085
1086
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001087/* Get next token, after space stripping etc. */
1088
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001089static int
1090tok_get(register struct tok_state *tok, char **p_start, char **p_end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001091{
1092 register int c;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001093 int blankline;
1094
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001095 *p_start = *p_end = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001096 nextline:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001097 tok->start = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001098 blankline = 0;
1099
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001100 /* Get indentation level */
1101 if (tok->atbol) {
1102 register int col = 0;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001103 register int altcol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001104 tok->atbol = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105 for (;;) {
1106 c = tok_nextc(tok);
1107 if (c == ' ')
Guido van Rossum926f13a1998-04-09 21:38:06 +00001108 col++, altcol++;
1109 else if (c == '\t') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001110 col = (col/tok->tabsize + 1) * tok->tabsize;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001111 altcol = (altcol/tok->alttabsize + 1)
1112 * tok->alttabsize;
1113 }
Guido van Rossum94d32b11995-07-07 22:27:27 +00001114 else if (c == '\014') /* Control-L (formfeed) */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001115 col = altcol = 0; /* For Emacs users */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001116 else
1117 break;
1118 }
1119 tok_backup(tok, c);
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001120 if (c == '#' || c == '\n') {
1121 /* Lines with only whitespace and/or comments
1122 shouldn't affect the indentation and are
1123 not passed to the parser as NEWLINE tokens,
1124 except *totally* empty lines in interactive
1125 mode, which signal the end of a command group. */
1126 if (col == 0 && c == '\n' && tok->prompt != NULL)
1127 blankline = 0; /* Let it through */
1128 else
1129 blankline = 1; /* Ignore completely */
1130 /* We can't jump back right here since we still
1131 may need to skip to the end of a comment */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001132 }
Guido van Rossuma849b831993-05-12 11:35:44 +00001133 if (!blankline && tok->level == 0) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001134 if (col == tok->indstack[tok->indent]) {
1135 /* No change */
Guido van Rossum926f13a1998-04-09 21:38:06 +00001136 if (altcol != tok->altindstack[tok->indent]) {
1137 if (indenterror(tok))
1138 return ERRORTOKEN;
1139 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001140 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001141 else if (col > tok->indstack[tok->indent]) {
1142 /* Indent -- always one */
1143 if (tok->indent+1 >= MAXINDENT) {
Fred Drake85f36392000-07-11 17:53:00 +00001144 tok->done = E_TOODEEP;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001145 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001146 return ERRORTOKEN;
1147 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001148 if (altcol <= tok->altindstack[tok->indent]) {
1149 if (indenterror(tok))
1150 return ERRORTOKEN;
1151 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001152 tok->pendin++;
1153 tok->indstack[++tok->indent] = col;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001154 tok->altindstack[tok->indent] = altcol;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001155 }
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001156 else /* col < tok->indstack[tok->indent] */ {
1157 /* Dedent -- any number, must be consistent */
1158 while (tok->indent > 0 &&
1159 col < tok->indstack[tok->indent]) {
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001160 tok->pendin--;
Guido van Rossum54758fa1998-02-16 22:18:00 +00001161 tok->indent--;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001162 }
1163 if (col != tok->indstack[tok->indent]) {
Fred Drake85f36392000-07-11 17:53:00 +00001164 tok->done = E_DEDENT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001165 tok->cur = tok->inp;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001166 return ERRORTOKEN;
1167 }
Guido van Rossum926f13a1998-04-09 21:38:06 +00001168 if (altcol != tok->altindstack[tok->indent]) {
1169 if (indenterror(tok))
1170 return ERRORTOKEN;
1171 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172 }
1173 }
1174 }
1175
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001176 tok->start = tok->cur;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001177
1178 /* Return pending indents/dedents */
1179 if (tok->pendin != 0) {
1180 if (tok->pendin < 0) {
1181 tok->pendin++;
1182 return DEDENT;
1183 }
1184 else {
1185 tok->pendin--;
1186 return INDENT;
1187 }
1188 }
1189
1190 again:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001191 tok->start = NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001192 /* Skip spaces */
1193 do {
1194 c = tok_nextc(tok);
Guido van Rossum94d32b11995-07-07 22:27:27 +00001195 } while (c == ' ' || c == '\t' || c == '\014');
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001196
1197 /* Set start of current token */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001198 tok->start = tok->cur - 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001199
Guido van Rossumab5ca152000-03-31 00:52:27 +00001200 /* Skip comment, while looking for tab-setting magic */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001201 if (c == '#') {
Guido van Rossumab5ca152000-03-31 00:52:27 +00001202 static char *tabforms[] = {
1203 "tab-width:", /* Emacs */
1204 ":tabstop=", /* vim, full form */
1205 ":ts=", /* vim, abbreviated form */
1206 "set tabsize=", /* will vi never die? */
1207 /* more templates can be added here to support other editors */
1208 };
1209 char cbuf[80];
1210 char *tp, **cp;
1211 tp = cbuf;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001212 do {
Guido van Rossumab5ca152000-03-31 00:52:27 +00001213 *tp++ = c = tok_nextc(tok);
1214 } while (c != EOF && c != '\n' &&
1215 tp - cbuf + 1 < sizeof(cbuf));
1216 *tp = '\0';
1217 for (cp = tabforms;
1218 cp < tabforms + sizeof(tabforms)/sizeof(tabforms[0]);
1219 cp++) {
1220 if ((tp = strstr(cbuf, *cp))) {
1221 int newsize = atoi(tp + strlen(*cp));
1222
1223 if (newsize >= 1 && newsize <= 40) {
1224 tok->tabsize = newsize;
Guido van Rossum6c981ad2000-04-03 23:02:17 +00001225 if (Py_VerboseFlag)
1226 PySys_WriteStderr(
Guido van Rossumab5ca152000-03-31 00:52:27 +00001227 "Tab size set to %d\n",
1228 newsize);
1229 }
1230 }
1231 }
1232 while (c != EOF && c != '\n')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001233 c = tok_nextc(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001234 }
1235
1236 /* Check for EOF and errors now */
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001237 if (c == EOF) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001238 return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001239 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001240
1241 /* Identifier (most frequent token!) */
1242 if (isalpha(c) || c == '_') {
Guido van Rossum86016cb2000-03-10 22:56:54 +00001243 /* Process r"", u"" and ur"" */
Guido van Rossum5026cb41997-04-25 17:32:00 +00001244 switch (c) {
1245 case 'r':
1246 case 'R':
1247 c = tok_nextc(tok);
1248 if (c == '"' || c == '\'')
1249 goto letter_quote;
Guido van Rossum86016cb2000-03-10 22:56:54 +00001250 break;
1251 case 'u':
1252 case 'U':
1253 c = tok_nextc(tok);
1254 if (c == 'r' || c == 'R')
1255 c = tok_nextc(tok);
1256 if (c == '"' || c == '\'')
1257 goto letter_quote;
1258 break;
Guido van Rossum5026cb41997-04-25 17:32:00 +00001259 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001260 while (isalnum(c) || c == '_') {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001261 c = tok_nextc(tok);
Guido van Rossum24dacb31997-04-06 03:46:20 +00001262 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001263 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001264 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001265 *p_end = tok->cur;
1266 return NAME;
1267 }
1268
1269 /* Newline */
1270 if (c == '\n') {
1271 tok->atbol = 1;
Guido van Rossuma849b831993-05-12 11:35:44 +00001272 if (blankline || tok->level > 0)
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001273 goto nextline;
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 - 1; /* Leave '\n' out of the string */
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001276 tok->cont_line = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001277 return NEWLINE;
1278 }
1279
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001280 /* Period or number starting with period? */
1281 if (c == '.') {
1282 c = tok_nextc(tok);
1283 if (isdigit(c)) {
1284 goto fraction;
1285 }
1286 else {
1287 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001288 *p_start = tok->start;
Guido van Rossumbaf0ebf1991-10-24 14:59:40 +00001289 *p_end = tok->cur;
1290 return DOT;
1291 }
1292 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001293
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001294 /* Number */
1295 if (isdigit(c)) {
1296 if (c == '0') {
Tim Petersd507dab2001-08-30 20:51:59 +00001297 /* Hex or octal -- maybe. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001298 c = tok_nextc(tok);
1299 if (c == '.')
1300 goto fraction;
Guido van Rossumf595fde1996-01-12 01:31:58 +00001301#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001302 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001303 goto imaginary;
1304#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001305 if (c == 'x' || c == 'X') {
1306 /* Hex */
1307 do {
1308 c = tok_nextc(tok);
1309 } while (isxdigit(c));
1310 }
1311 else {
Tim Petersd507dab2001-08-30 20:51:59 +00001312 int found_decimal = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001313 /* Octal; c is first char of it */
1314 /* There's no 'isoctdigit' macro, sigh */
1315 while ('0' <= c && c < '8') {
1316 c = tok_nextc(tok);
1317 }
Tim Petersd507dab2001-08-30 20:51:59 +00001318 if (isdigit(c)) {
1319 found_decimal = 1;
1320 do {
1321 c = tok_nextc(tok);
1322 } while (isdigit(c));
1323 }
1324 if (c == '.')
1325 goto fraction;
1326 else if (c == 'e' || c == 'E')
1327 goto exponent;
1328#ifndef WITHOUT_COMPLEX
1329 else if (c == 'j' || c == 'J')
1330 goto imaginary;
1331#endif
1332 else if (found_decimal) {
1333 tok->done = E_TOKEN;
1334 tok_backup(tok, c);
1335 return ERRORTOKEN;
1336 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001337 }
Guido van Rossumf023c461991-05-05 20:16:20 +00001338 if (c == 'l' || c == 'L')
1339 c = tok_nextc(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001340 }
1341 else {
1342 /* Decimal */
1343 do {
1344 c = tok_nextc(tok);
1345 } while (isdigit(c));
Guido van Rossumf023c461991-05-05 20:16:20 +00001346 if (c == 'l' || c == 'L')
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001348 else {
Tim Peters9aa70d92001-08-27 19:19:28 +00001349 /* Accept floating point numbers. */
Guido van Rossumf023c461991-05-05 20:16:20 +00001350 if (c == '.') {
1351 fraction:
1352 /* Fraction */
1353 do {
1354 c = tok_nextc(tok);
1355 } while (isdigit(c));
1356 }
1357 if (c == 'e' || c == 'E') {
Tim Petersd507dab2001-08-30 20:51:59 +00001358 exponent:
Guido van Rossumf023c461991-05-05 20:16:20 +00001359 /* Exponent part */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001360 c = tok_nextc(tok);
Guido van Rossumf023c461991-05-05 20:16:20 +00001361 if (c == '+' || c == '-')
1362 c = tok_nextc(tok);
Tim Peters9aa70d92001-08-27 19:19:28 +00001363 if (!isdigit(c)) {
1364 tok->done = E_TOKEN;
1365 tok_backup(tok, c);
1366 return ERRORTOKEN;
Guido van Rossumf023c461991-05-05 20:16:20 +00001367 }
Tim Peters9aa70d92001-08-27 19:19:28 +00001368 do {
1369 c = tok_nextc(tok);
1370 } while (isdigit(c));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001371 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001372#ifndef WITHOUT_COMPLEX
Guido van Rossumfaa436c1996-01-26 18:59:07 +00001373 if (c == 'j' || c == 'J')
Guido van Rossumf595fde1996-01-12 01:31:58 +00001374 /* Imaginary part */
1375 imaginary:
1376 c = tok_nextc(tok);
1377#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001378 }
1379 }
1380 tok_backup(tok, c);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001381 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001382 *p_end = tok->cur;
1383 return NUMBER;
1384 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001385
1386 letter_quote:
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001387 /* String */
1388 if (c == '\'' || c == '"') {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001389 Py_ssize_t quote2 = tok->cur - tok->start + 1;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001390 int quote = c;
1391 int triple = 0;
1392 int tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001393 for (;;) {
1394 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001395 if (c == '\n') {
1396 if (!triple) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001397 tok->done = E_EOLS;
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001398 tok_backup(tok, c);
1399 return ERRORTOKEN;
1400 }
1401 tripcount = 0;
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001402 tok->cont_line = 1; /* multiline string. */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001403 }
1404 else if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001405 if (triple)
1406 tok->done = E_EOFS;
1407 else
1408 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001409 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001410 return ERRORTOKEN;
1411 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001412 else if (c == quote) {
1413 tripcount++;
Guido van Rossum35685241998-02-16 15:42:50 +00001414 if (tok->cur - tok->start == quote2) {
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001415 c = tok_nextc(tok);
1416 if (c == quote) {
1417 triple = 1;
1418 tripcount = 0;
1419 continue;
1420 }
1421 tok_backup(tok, c);
1422 }
1423 if (!triple || tripcount == 3)
1424 break;
1425 }
1426 else if (c == '\\') {
1427 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001428 c = tok_nextc(tok);
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001429 if (c == EOF) {
Skip Montanaro118ec702002-08-15 01:20:16 +00001430 tok->done = E_EOLS;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001431 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001432 return ERRORTOKEN;
1433 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001434 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001435 else
1436 tripcount = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001437 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001438 *p_start = tok->start;
Guido van Rossum8054fad1993-10-26 15:19:44 +00001439 *p_end = tok->cur;
1440 return STRING;
1441 }
1442
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001443 /* Line continuation */
1444 if (c == '\\') {
1445 c = tok_nextc(tok);
1446 if (c != '\n') {
Martin v. Löwis4bf108d2005-03-03 11:45:45 +00001447 tok->done = E_LINECONT;
Guido van Rossum6ac258d1993-05-12 08:24:20 +00001448 tok->cur = tok->inp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001449 return ERRORTOKEN;
1450 }
Martin v. Löwisf62a89b2002-09-03 11:52:44 +00001451 tok->cont_line = 1;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001452 goto again; /* Read next line */
1453 }
1454
Guido van Rossumfbab9051991-10-20 20:25:03 +00001455 /* Check for two-character token */
1456 {
1457 int c2 = tok_nextc(tok);
Guido van Rossum86bea461997-04-29 21:03:06 +00001458 int token = PyToken_TwoChars(c, c2);
Guido van Rossumfbab9051991-10-20 20:25:03 +00001459 if (token != OP) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001460 int c3 = tok_nextc(tok);
1461 int token3 = PyToken_ThreeChars(c, c2, c3);
1462 if (token3 != OP) {
1463 token = token3;
1464 } else {
1465 tok_backup(tok, c3);
1466 }
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001467 *p_start = tok->start;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001468 *p_end = tok->cur;
1469 return token;
1470 }
1471 tok_backup(tok, c2);
1472 }
1473
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001474 /* Keep track of parentheses nesting level */
Guido van Rossuma849b831993-05-12 11:35:44 +00001475 switch (c) {
1476 case '(':
1477 case '[':
1478 case '{':
1479 tok->level++;
1480 break;
1481 case ')':
1482 case ']':
1483 case '}':
1484 tok->level--;
1485 break;
1486 }
1487
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001488 /* Punctuation character */
Guido van Rossumf4b1a641994-08-29 12:43:07 +00001489 *p_start = tok->start;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001490 *p_end = tok->cur;
Guido van Rossum86bea461997-04-29 21:03:06 +00001491 return PyToken_OneChar(c);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001492}
1493
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001494int
1495PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
1496{
1497 int result = tok_get(tok, p_start, p_end);
1498 if (tok->decoding_erred) {
1499 result = ERRORTOKEN;
1500 tok->done = E_DECODE;
1501 }
1502 return result;
1503}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001504
Guido van Rossum408027e1996-12-30 16:17:54 +00001505#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001506
1507void
Thomas Wouters23c9e002000-07-22 19:20:54 +00001508tok_dump(int type, char *start, char *end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001509{
Guido van Rossum86bea461997-04-29 21:03:06 +00001510 printf("%s", _PyParser_TokenNames[type]);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001511 if (type == NAME || type == NUMBER || type == STRING || type == OP)
1512 printf("(%.*s)", (int)(end - start), start);
1513}
1514
1515#endif