blob: fbc98880c9a5b1060734de0907e8cea4ff915ce9 [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"
Christian Heimes2c9c7a52008-05-26 13:42:13 +000015#include "bytesobject.h"
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +000016#include "fileobject.h"
17#include "codecs.h"
18#include "abstract.h"
19#endif /* PGEN */
20
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -080021/* Alternate tab spacing */
22#define ALTTABSIZE 1
23
Martin v. Löwis5b222132007-06-10 09:51:05 +000024#define is_potential_identifier_start(c) (\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 (c >= 'a' && c <= 'z')\
26 || (c >= 'A' && c <= 'Z')\
27 || c == '_'\
28 || (c >= 128))
Martin v. Löwis5b222132007-06-10 09:51:05 +000029
30#define is_potential_identifier_char(c) (\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 (c >= 'a' && c <= 'z')\
32 || (c >= 'A' && c <= 'Z')\
33 || (c >= '0' && c <= '9')\
34 || c == '_'\
35 || (c >= 128))
Martin v. Löwis5b222132007-06-10 09:51:05 +000036
Serhiy Storchakac6792272013-10-19 21:03:34 +030037extern char *PyOS_Readline(FILE *, FILE *, const char *);
Guido van Rossumf4b1a641994-08-29 12:43:07 +000038/* Return malloc'ed string including trailing \n;
39 empty malloc'ed string for EOF;
40 NULL if interrupted */
41
Guido van Rossum4fe87291992-02-26 15:24:44 +000042/* Don't ever change this -- it would break the portability of Python code */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043#define TABSIZE 8
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Guido van Rossum3f5da241990-12-20 15:06:42 +000045/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000046static struct tok_state *tok_new(void);
47static int tok_nextc(struct tok_state *tok);
48static void tok_backup(struct tok_state *tok, int c);
Guido van Rossum3f5da241990-12-20 15:06:42 +000049
Brett Cannond5ec98c2007-10-20 02:54:14 +000050
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051/* Token names */
52
Benjamin Petersond0845582012-10-24 08:21:52 -070053const char *_PyParser_TokenNames[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 "ENDMARKER",
55 "NAME",
56 "NUMBER",
57 "STRING",
58 "NEWLINE",
59 "INDENT",
60 "DEDENT",
61 "LPAR",
62 "RPAR",
63 "LSQB",
64 "RSQB",
65 "COLON",
66 "COMMA",
67 "SEMI",
68 "PLUS",
69 "MINUS",
70 "STAR",
71 "SLASH",
72 "VBAR",
73 "AMPER",
74 "LESS",
75 "GREATER",
76 "EQUAL",
77 "DOT",
78 "PERCENT",
79 "LBRACE",
80 "RBRACE",
81 "EQEQUAL",
82 "NOTEQUAL",
83 "LESSEQUAL",
84 "GREATEREQUAL",
85 "TILDE",
86 "CIRCUMFLEX",
87 "LEFTSHIFT",
88 "RIGHTSHIFT",
89 "DOUBLESTAR",
90 "PLUSEQUAL",
91 "MINEQUAL",
92 "STAREQUAL",
93 "SLASHEQUAL",
94 "PERCENTEQUAL",
95 "AMPEREQUAL",
96 "VBAREQUAL",
97 "CIRCUMFLEXEQUAL",
98 "LEFTSHIFTEQUAL",
99 "RIGHTSHIFTEQUAL",
100 "DOUBLESTAREQUAL",
101 "DOUBLESLASH",
102 "DOUBLESLASHEQUAL",
103 "AT",
Benjamin Petersond51374e2014-04-09 23:55:56 -0400104 "ATEQUAL",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 "RARROW",
106 "ELLIPSIS",
107 /* This table must match the #defines in token.h! */
108 "OP",
109 "<ERRORTOKEN>",
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100110 "COMMENT",
111 "NL",
Albert-Jan Nijburgc9ccace2017-06-01 21:51:27 +0100112 "ENCODING",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 "<N_TOKENS>"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114};
115
116
117/* Create and initialize a new tok_state structure */
118
119static struct tok_state *
Thomas Wouters23c9e002000-07-22 19:20:54 +0000120tok_new(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 struct tok_state *tok = (struct tok_state *)PyMem_MALLOC(
123 sizeof(struct tok_state));
124 if (tok == NULL)
125 return NULL;
126 tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL;
127 tok->done = E_OK;
128 tok->fp = NULL;
129 tok->input = NULL;
130 tok->tabsize = TABSIZE;
131 tok->indent = 0;
132 tok->indstack[0] = 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 tok->atbol = 1;
135 tok->pendin = 0;
136 tok->prompt = tok->nextprompt = NULL;
137 tok->lineno = 0;
138 tok->level = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 tok->altindstack[0] = 0;
140 tok->decoding_state = STATE_INIT;
141 tok->decoding_erred = 0;
142 tok->read_coding_spec = 0;
143 tok->enc = NULL;
144 tok->encoding = NULL;
145 tok->cont_line = 0;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000146#ifndef PGEN
Victor Stinner7f2fee32011-04-05 00:39:01 +0200147 tok->filename = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 tok->decoding_readline = NULL;
149 tok->decoding_buffer = NULL;
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000150#endif
Yury Selivanov96ec9342015-07-23 15:01:58 +0300151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 return tok;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000153}
154
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000155static char *
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700156new_string(const char *s, Py_ssize_t len, struct tok_state *tok)
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 char* result = (char *)PyMem_MALLOC(len + 1);
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700159 if (!result) {
160 tok->done = E_NOMEM;
161 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700163 memcpy(result, s, len);
164 result[len] = '\0';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 return result;
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000166}
167
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000168#ifdef PGEN
169
170static char *
171decoding_fgets(char *s, int size, struct tok_state *tok)
172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 return fgets(s, size, tok->fp);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000174}
175
176static int
177decoding_feof(struct tok_state *tok)
178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179 return feof(tok->fp);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000180}
181
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000182static char *
183decode_str(const char *str, int exec_input, struct tok_state *tok)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000184{
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700185 return new_string(str, strlen(str), tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000186}
187
188#else /* PGEN */
189
190static char *
191error_ret(struct tok_state *tok) /* XXX */
192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 tok->decoding_erred = 1;
194 if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */
195 PyMem_FREE(tok->buf);
Serhiy Storchaka0d441112015-11-14 15:10:35 +0200196 tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL;
197 tok->done = E_DECODE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 return NULL; /* as if it were EOF */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000199}
200
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000201
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200202static const char *
203get_normal_name(const char *s) /* for utf-8 and latin-1 */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 char buf[13];
206 int i;
207 for (i = 0; i < 12; i++) {
208 int c = s[i];
209 if (c == '\0')
210 break;
211 else if (c == '_')
212 buf[i] = '-';
213 else
214 buf[i] = tolower(c);
215 }
216 buf[i] = '\0';
217 if (strcmp(buf, "utf-8") == 0 ||
218 strncmp(buf, "utf-8-", 6) == 0)
219 return "utf-8";
220 else if (strcmp(buf, "latin-1") == 0 ||
221 strcmp(buf, "iso-8859-1") == 0 ||
222 strcmp(buf, "iso-latin-1") == 0 ||
223 strncmp(buf, "latin-1-", 8) == 0 ||
224 strncmp(buf, "iso-8859-1-", 11) == 0 ||
225 strncmp(buf, "iso-latin-1-", 12) == 0)
226 return "iso-8859-1";
227 else
228 return s;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000229}
230
231/* Return the coding spec in S, or NULL if none is found. */
232
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700233static int
234get_coding_spec(const char *s, char **spec, Py_ssize_t size, struct tok_state *tok)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 Py_ssize_t i;
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700237 *spec = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 /* Coding spec must be in a comment, and that comment must be
239 * the only statement on the source code line. */
240 for (i = 0; i < size - 6; i++) {
241 if (s[i] == '#')
242 break;
243 if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014')
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700244 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 }
246 for (; i < size - 6; i++) { /* XXX inefficient search */
247 const char* t = s + i;
248 if (strncmp(t, "coding", 6) == 0) {
249 const char* begin = NULL;
250 t += 6;
251 if (t[0] != ':' && t[0] != '=')
252 continue;
253 do {
254 t++;
255 } while (t[0] == '\x20' || t[0] == '\t');
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 begin = t;
258 while (Py_ISALNUM(t[0]) ||
259 t[0] == '-' || t[0] == '_' || t[0] == '.')
260 t++;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (begin < t) {
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700263 char* r = new_string(begin, t - begin, tok);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200264 const char* q;
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700265 if (!r)
266 return 0;
Benjamin Peterson265fba42013-07-15 20:50:22 -0700267 q = get_normal_name(r);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if (r != q) {
269 PyMem_FREE(r);
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700270 r = new_string(q, strlen(q), tok);
271 if (!r)
272 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700274 *spec = r;
Serhiy Storchakae431d3c2016-03-20 23:36:29 +0200275 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 }
277 }
278 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700279 return 1;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000280}
281
282/* Check whether the line contains a coding spec. If it does,
283 invoke the set_readline function for the new encoding.
284 This function receives the tok_state and the new encoding.
285 Return 1 on success, 0 on failure. */
286
287static int
Martin v. Löwis18e16552006-02-15 17:27:45 +0000288check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 int set_readline(struct tok_state *, const char *))
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000290{
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700291 char *cs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 int r = 1;
Tim Peters17db21f2002-09-03 15:39:58 +0000293
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200294 if (tok->cont_line) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* It's a continuation line, so it can't be a coding spec. */
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200296 tok->read_coding_spec = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 return 1;
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200298 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700299 if (!get_coding_spec(line, &cs, size, tok))
300 return 0;
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200301 if (!cs) {
302 Py_ssize_t i;
303 for (i = 0; i < size; i++) {
304 if (line[i] == '#' || line[i] == '\n' || line[i] == '\r')
305 break;
306 if (line[i] != ' ' && line[i] != '\t' && line[i] != '\014') {
307 /* Stop checking coding spec after a line containing
308 * anything except a comment. */
309 tok->read_coding_spec = 1;
310 break;
311 }
312 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700313 return 1;
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200314 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700315 tok->read_coding_spec = 1;
316 if (tok->encoding == NULL) {
317 assert(tok->decoding_state == STATE_RAW);
318 if (strcmp(cs, "utf-8") == 0) {
319 tok->encoding = cs;
320 } else {
321 r = set_readline(tok, cs);
322 if (r) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 tok->encoding = cs;
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700324 tok->decoding_state = STATE_NORMAL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700326 else {
Serhiy Storchaka3af14aa2013-06-09 16:51:52 +0300327 PyErr_Format(PyExc_SyntaxError,
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700328 "encoding problem: %s", cs);
329 PyMem_FREE(cs);
330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 }
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700332 } else { /* then, compare cs with BOM */
333 r = (strcmp(tok->encoding, cs) == 0);
334 if (!r)
335 PyErr_Format(PyExc_SyntaxError,
336 "encoding problem: %s with BOM", cs);
337 PyMem_FREE(cs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 return r;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000340}
341
342/* See whether the file starts with a BOM. If it does,
343 invoke the set_readline function with the new encoding.
344 Return 1 on success, 0 on failure. */
345
346static int
347check_bom(int get_char(struct tok_state *),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 void unget_char(int, struct tok_state *),
349 int set_readline(struct tok_state *, const char *),
350 struct tok_state *tok)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 int ch1, ch2, ch3;
353 ch1 = get_char(tok);
354 tok->decoding_state = STATE_RAW;
355 if (ch1 == EOF) {
356 return 1;
357 } else if (ch1 == 0xEF) {
358 ch2 = get_char(tok);
359 if (ch2 != 0xBB) {
360 unget_char(ch2, tok);
361 unget_char(ch1, tok);
362 return 1;
363 }
364 ch3 = get_char(tok);
365 if (ch3 != 0xBF) {
366 unget_char(ch3, tok);
367 unget_char(ch2, tok);
368 unget_char(ch1, tok);
369 return 1;
370 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000371#if 0
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 /* Disable support for UTF-16 BOMs until a decision
373 is made whether this needs to be supported. */
374 } else if (ch1 == 0xFE) {
375 ch2 = get_char(tok);
376 if (ch2 != 0xFF) {
377 unget_char(ch2, tok);
378 unget_char(ch1, tok);
379 return 1;
380 }
381 if (!set_readline(tok, "utf-16-be"))
382 return 0;
383 tok->decoding_state = STATE_NORMAL;
384 } else if (ch1 == 0xFF) {
385 ch2 = get_char(tok);
386 if (ch2 != 0xFE) {
387 unget_char(ch2, tok);
388 unget_char(ch1, tok);
389 return 1;
390 }
391 if (!set_readline(tok, "utf-16-le"))
392 return 0;
393 tok->decoding_state = STATE_NORMAL;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000394#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 } else {
396 unget_char(ch1, tok);
397 return 1;
398 }
399 if (tok->encoding != NULL)
400 PyMem_FREE(tok->encoding);
Benjamin Peterson2dbfd882013-07-15 19:15:34 -0700401 tok->encoding = new_string("utf-8", 5, tok);
402 if (!tok->encoding)
403 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 /* No need to set_readline: input is already utf-8 */
405 return 1;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000406}
407
408/* Read a line of text from TOK into S, using the stream in TOK.
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000409 Return NULL on failure, else S.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000411 On entry, tok->decoding_buffer will be one of:
412 1) NULL: need to call tok->decoding_readline to get a new line
413 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 stored the result in tok->decoding_buffer
Christian Heimes9c4756e2008-05-26 13:22:05 +0000415 3) PyByteArrayObject *: previous call to fp_readl did not have enough room
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 (in the s buffer) to copy entire contents of the line read
417 by tok->decoding_readline. tok->decoding_buffer has the overflow.
418 In this case, fp_readl is called in a loop (with an expanded buffer)
419 until the buffer ends with a '\n' (or until the end of the file is
420 reached): see tok_nextc and its calls to decoding_fgets.
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000421*/
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000422
423static char *
424fp_readl(char *s, int size, struct tok_state *tok)
425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 PyObject* bufobj;
427 const char *buf;
428 Py_ssize_t buflen;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 /* Ask for one less byte so we can terminate it */
431 assert(size > 0);
432 size--;
Walter Dörwaldc1f5fff2005-07-12 21:53:43 +0000433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (tok->decoding_buffer) {
435 bufobj = tok->decoding_buffer;
436 Py_INCREF(bufobj);
437 }
438 else
439 {
Victor Stinnera5ed5f02016-12-06 18:45:50 +0100440 bufobj = _PyObject_CallNoArg(tok->decoding_readline);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (bufobj == NULL)
442 goto error;
443 }
444 if (PyUnicode_CheckExact(bufobj))
445 {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200446 buf = PyUnicode_AsUTF8AndSize(bufobj, &buflen);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (buf == NULL) {
448 goto error;
449 }
450 }
451 else
452 {
453 buf = PyByteArray_AsString(bufobj);
454 if (buf == NULL) {
455 goto error;
456 }
457 buflen = PyByteArray_GET_SIZE(bufobj);
458 }
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 Py_XDECREF(tok->decoding_buffer);
461 if (buflen > size) {
462 /* Too many chars, the rest goes into tok->decoding_buffer */
463 tok->decoding_buffer = PyByteArray_FromStringAndSize(buf+size,
464 buflen-size);
465 if (tok->decoding_buffer == NULL)
466 goto error;
467 buflen = size;
468 }
469 else
470 tok->decoding_buffer = NULL;
Amaury Forgeot d'Arc65f9ace2007-11-15 23:19:43 +0000471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 memcpy(s, buf, buflen);
473 s[buflen] = '\0';
474 if (buflen == 0) /* EOF */
475 s = NULL;
476 Py_DECREF(bufobj);
477 return s;
Neal Norwitz41eaedd2007-08-12 00:03:22 +0000478
479error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 Py_XDECREF(bufobj);
481 return error_ret(tok);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000482}
483
484/* Set the readline function for TOK to a StreamReader's
485 readline function. The StreamReader is named ENC.
486
487 This function is called from check_bom and check_coding_spec.
488
489 ENC is usually identical to the future value of tok->encoding,
490 except for the (currently unsupported) case of UTF-16.
491
492 Return 1 on success, 0 on failure. */
493
494static int
495fp_setreadl(struct tok_state *tok, const char* enc)
496{
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700497 PyObject *readline, *io, *stream;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200498 _Py_IDENTIFIER(open);
499 _Py_IDENTIFIER(readline);
Victor Stinner22a351a2010-10-14 12:04:34 +0000500 int fd;
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200501 long pos;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000502
Victor Stinner22a351a2010-10-14 12:04:34 +0000503 fd = fileno(tok->fp);
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200504 /* Due to buffering the file offset for fd can be different from the file
Martin v. Löwis815b41b2014-02-28 15:27:29 +0100505 * position of tok->fp. If tok->fp was opened in text mode on Windows,
506 * its file position counts CRLF as one char and can't be directly mapped
507 * to the file offset for fd. Instead we step back one byte and read to
508 * the end of line.*/
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200509 pos = ftell(tok->fp);
Martin v. Löwis815b41b2014-02-28 15:27:29 +0100510 if (pos == -1 ||
511 lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) {
Victor Stinner22a351a2010-10-14 12:04:34 +0000512 PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700513 return 0;
Victor Stinner22a351a2010-10-14 12:04:34 +0000514 }
515
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700516 io = PyImport_ImportModuleNoBlock("io");
517 if (io == NULL)
518 return 0;
519
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200520 stream = _PyObject_CallMethodId(io, &PyId_open, "isisOOO",
Victor Stinner22a351a2010-10-14 12:04:34 +0000521 fd, "r", -1, enc, Py_None, Py_None, Py_False);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700522 Py_DECREF(io);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (stream == NULL)
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700524 return 0;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000525
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200526 readline = _PyObject_GetAttrId(stream, &PyId_readline);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700527 Py_DECREF(stream);
528 if (readline == NULL)
529 return 0;
Serhiy Storchaka48842712016-04-06 09:45:48 +0300530 Py_XSETREF(tok->decoding_readline, readline);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700531
Martin v. Löwis815b41b2014-02-28 15:27:29 +0100532 if (pos > 0) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +0100533 PyObject *bufobj = _PyObject_CallNoArg(readline);
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700534 if (bufobj == NULL)
535 return 0;
536 Py_DECREF(bufobj);
Martin v. Löwis815b41b2014-02-28 15:27:29 +0100537 }
Guido van Rossum9cbfffd2007-06-07 00:54:15 +0000538
Benjamin Peterson35ee9482016-09-12 22:06:58 -0700539 return 1;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000540}
541
542/* Fetch the next byte from TOK. */
543
544static int fp_getc(struct tok_state *tok) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 return getc(tok->fp);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000546}
547
548/* Unfetch the last byte back into TOK. */
549
550static void fp_ungetc(int c, struct tok_state *tok) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 ungetc(c, tok->fp);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000552}
553
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000554/* Check whether the characters at s start a valid
555 UTF-8 sequence. Return the number of characters forming
556 the sequence if yes, 0 if not. */
557static int valid_utf8(const unsigned char* s)
558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 int expected = 0;
560 int length;
561 if (*s < 0x80)
562 /* single-byte code */
563 return 1;
564 if (*s < 0xc0)
565 /* following byte */
566 return 0;
567 if (*s < 0xE0)
568 expected = 1;
569 else if (*s < 0xF0)
570 expected = 2;
571 else if (*s < 0xF8)
572 expected = 3;
573 else
574 return 0;
575 length = expected + 1;
576 for (; expected; expected--)
577 if (s[expected] < 0x80 || s[expected] >= 0xC0)
578 return 0;
579 return length;
Martin v. Löwis447d33e2007-07-29 18:10:01 +0000580}
581
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000582/* Read a line of input from TOK. Determine encoding
583 if necessary. */
584
585static char *
586decoding_fgets(char *s, int size, struct tok_state *tok)
587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 char *line = NULL;
589 int badchar = 0;
590 for (;;) {
591 if (tok->decoding_state == STATE_NORMAL) {
592 /* We already have a codec associated with
593 this input. */
594 line = fp_readl(s, size, tok);
595 break;
596 } else if (tok->decoding_state == STATE_RAW) {
597 /* We want a 'raw' read. */
598 line = Py_UniversalNewlineFgets(s, size,
599 tok->fp, NULL);
600 break;
601 } else {
602 /* We have not yet determined the encoding.
603 If an encoding is found, use the file-pointer
604 reader functions from now on. */
605 if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok))
606 return error_ret(tok);
607 assert(tok->decoding_state != STATE_INIT);
608 }
609 }
610 if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) {
611 if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) {
612 return error_ret(tok);
613 }
614 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000615#ifndef PGEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 /* The default encoding is UTF-8, so make sure we don't have any
617 non-UTF-8 sequences in it. */
618 if (line && !tok->encoding) {
619 unsigned char *c;
620 int length;
621 for (c = (unsigned char *)line; *c; c += length)
622 if (!(length = valid_utf8(c))) {
623 badchar = *c;
624 break;
625 }
626 }
627 if (badchar) {
628 /* Need to add 1 to the line number, since this line
629 has not been counted, yet. */
Jesus Ceac1935d22011-04-25 04:03:58 +0200630 PyErr_Format(PyExc_SyntaxError,
631 "Non-UTF-8 code starting with '\\x%.2x' "
632 "in file %U on line %i, "
633 "but no encoding declared; "
634 "see http://python.org/dev/peps/pep-0263/ for details",
635 badchar, tok->filename, tok->lineno + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 return error_ret(tok);
637 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000638#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 return line;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000640}
641
642static int
643decoding_feof(struct tok_state *tok)
644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 if (tok->decoding_state != STATE_NORMAL) {
646 return feof(tok->fp);
647 } else {
648 PyObject* buf = tok->decoding_buffer;
649 if (buf == NULL) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +0100650 buf = _PyObject_CallNoArg(tok->decoding_readline);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 if (buf == NULL) {
652 error_ret(tok);
653 return 1;
654 } else {
655 tok->decoding_buffer = buf;
656 }
657 }
658 return PyObject_Length(buf) == 0;
659 }
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000660}
661
662/* Fetch a byte from TOK, using the string buffer. */
663
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000664static int
665buf_getc(struct tok_state *tok) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 return Py_CHARMASK(*tok->str++);
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000667}
668
669/* Unfetch a byte from TOK, using the string buffer. */
670
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671static void
672buf_ungetc(int c, struct tok_state *tok) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 tok->str--;
674 assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000675}
676
677/* Set the readline function for TOK to ENC. For the string-based
678 tokenizer, this means to just record the encoding. */
679
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680static int
681buf_setreadl(struct tok_state *tok, const char* enc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 tok->enc = enc;
683 return 1;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000684}
685
686/* Return a UTF-8 encoding Python string object from the
687 C byte string STR, which is encoded with ENC. */
688
689static PyObject *
690translate_into_utf8(const char* str, const char* enc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 PyObject *utf8;
692 PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL);
693 if (buf == NULL)
694 return NULL;
695 utf8 = PyUnicode_AsUTF8String(buf);
696 Py_DECREF(buf);
697 return utf8;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000698}
699
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000700
701static char *
702translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
Victor Stinner79697732013-06-05 00:44:00 +0200703 int skip_next_lf = 0;
704 size_t needed_length = strlen(s) + 2, final_length;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 char *buf, *current;
706 char c = '\0';
707 buf = PyMem_MALLOC(needed_length);
708 if (buf == NULL) {
709 tok->done = E_NOMEM;
710 return NULL;
711 }
712 for (current = buf; *s; s++, current++) {
713 c = *s;
714 if (skip_next_lf) {
715 skip_next_lf = 0;
716 if (c == '\n') {
717 c = *++s;
718 if (!c)
719 break;
720 }
721 }
722 if (c == '\r') {
723 skip_next_lf = 1;
724 c = '\n';
725 }
726 *current = c;
727 }
728 /* If this is exec input, add a newline to the end of the string if
729 there isn't one already. */
730 if (exec_input && c != '\n') {
731 *current = '\n';
732 current++;
733 }
734 *current = '\0';
735 final_length = current - buf + 1;
736 if (final_length < needed_length && final_length)
737 /* should never fail */
738 buf = PyMem_REALLOC(buf, final_length);
739 return buf;
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000740}
741
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000742/* Decode a byte string STR for use as the buffer of TOK.
743 Look for encoding declarations inside STR, and record them
744 inside TOK. */
745
746static const char *
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000747decode_str(const char *input, int single, struct tok_state *tok)
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 PyObject* utf8 = NULL;
750 const char *str;
751 const char *s;
752 const char *newl[2] = {NULL, NULL};
753 int lineno = 0;
754 tok->input = str = translate_newlines(input, single, tok);
755 if (str == NULL)
756 return NULL;
757 tok->enc = NULL;
758 tok->str = str;
759 if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
760 return error_ret(tok);
761 str = tok->str; /* string after BOM if any */
762 assert(str);
763 if (tok->enc != NULL) {
764 utf8 = translate_into_utf8(str, tok->enc);
765 if (utf8 == NULL)
766 return error_ret(tok);
767 str = PyBytes_AsString(utf8);
768 }
769 for (s = str;; s++) {
770 if (*s == '\0') break;
771 else if (*s == '\n') {
772 assert(lineno < 2);
773 newl[lineno] = s;
774 lineno++;
775 if (lineno == 2) break;
776 }
777 }
778 tok->enc = NULL;
779 /* need to check line 1 and 2 separately since check_coding_spec
780 assumes a single line as input */
781 if (newl[0]) {
782 if (!check_coding_spec(str, newl[0] - str, tok, buf_setreadl))
783 return error_ret(tok);
Serhiy Storchaka768c16c2014-01-09 18:36:09 +0200784 if (tok->enc == NULL && !tok->read_coding_spec && newl[1]) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 if (!check_coding_spec(newl[0]+1, newl[1] - newl[0],
786 tok, buf_setreadl))
787 return error_ret(tok);
788 }
789 }
790 if (tok->enc != NULL) {
791 assert(utf8 == NULL);
792 utf8 = translate_into_utf8(str, tok->enc);
793 if (utf8 == NULL)
794 return error_ret(tok);
795 str = PyBytes_AS_STRING(utf8);
796 }
797 assert(tok->decoding_buffer == NULL);
798 tok->decoding_buffer = utf8; /* CAUTION */
799 return str;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +0000800}
801
802#endif /* PGEN */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000803
804/* Set up tokenizer for string */
805
806struct tok_state *
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000807PyTokenizer_FromString(const char *str, int exec_input)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 struct tok_state *tok = tok_new();
810 if (tok == NULL)
811 return NULL;
Serhiy Storchakac6792272013-10-19 21:03:34 +0300812 str = decode_str(str, exec_input, tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (str == NULL) {
814 PyTokenizer_Free(tok);
815 return NULL;
816 }
Neal Norwitzdee2fd52005-11-16 05:12:59 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 /* XXX: constify members. */
819 tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
820 return tok;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000821}
822
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000823struct tok_state *
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000824PyTokenizer_FromUTF8(const char *str, int exec_input)
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 struct tok_state *tok = tok_new();
827 if (tok == NULL)
828 return NULL;
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000829#ifndef PGEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 tok->input = str = translate_newlines(str, exec_input, tok);
Benjamin Petersonaeaa5922009-11-13 00:17:59 +0000831#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 if (str == NULL) {
833 PyTokenizer_Free(tok);
834 return NULL;
835 }
836 tok->decoding_state = STATE_RAW;
837 tok->read_coding_spec = 1;
838 tok->enc = NULL;
839 tok->str = str;
840 tok->encoding = (char *)PyMem_MALLOC(6);
841 if (!tok->encoding) {
842 PyTokenizer_Free(tok);
843 return NULL;
844 }
845 strcpy(tok->encoding, "utf-8");
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 /* XXX: constify members. */
848 tok->buf = tok->cur = tok->end = tok->inp = (char*)str;
849 return tok;
Benjamin Petersonf5b52242009-03-02 23:31:26 +0000850}
851
Guido van Rossum8c11a5c1991-07-27 21:42:56 +0000852/* Set up tokenizer for file */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000853
854struct tok_state *
Serhiy Storchakac6792272013-10-19 21:03:34 +0300855PyTokenizer_FromFile(FILE *fp, const char* enc,
856 const char *ps1, const char *ps2)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 struct tok_state *tok = tok_new();
859 if (tok == NULL)
860 return NULL;
861 if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) {
862 PyTokenizer_Free(tok);
863 return NULL;
864 }
865 tok->cur = tok->inp = tok->buf;
866 tok->end = tok->buf + BUFSIZ;
867 tok->fp = fp;
868 tok->prompt = ps1;
869 tok->nextprompt = ps2;
870 if (enc != NULL) {
871 /* Must copy encoding declaration since it
872 gets copied into the parse tree. */
873 tok->encoding = PyMem_MALLOC(strlen(enc)+1);
874 if (!tok->encoding) {
875 PyTokenizer_Free(tok);
876 return NULL;
877 }
878 strcpy(tok->encoding, enc);
879 tok->decoding_state = STATE_NORMAL;
880 }
881 return tok;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000882}
883
884
885/* Free a tok_state structure */
886
887void
Thomas Wouters23c9e002000-07-22 19:20:54 +0000888PyTokenizer_Free(struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 if (tok->encoding != NULL)
891 PyMem_FREE(tok->encoding);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000892#ifndef PGEN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 Py_XDECREF(tok->decoding_readline);
894 Py_XDECREF(tok->decoding_buffer);
Victor Stinner7f2fee32011-04-05 00:39:01 +0200895 Py_XDECREF(tok->filename);
Martin v. Löwis1ee99d32002-08-04 20:10:29 +0000896#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (tok->fp != NULL && tok->buf != NULL)
898 PyMem_FREE(tok->buf);
899 if (tok->input)
900 PyMem_FREE((char *)tok->input);
901 PyMem_FREE(tok);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000902}
903
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904/* Get next char, updating state; error code goes into tok->done */
905
906static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200907tok_nextc(struct tok_state *tok)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 for (;;) {
910 if (tok->cur != tok->inp) {
911 return Py_CHARMASK(*tok->cur++); /* Fast path */
912 }
913 if (tok->done != E_OK)
914 return EOF;
915 if (tok->fp == NULL) {
916 char *end = strchr(tok->inp, '\n');
917 if (end != NULL)
918 end++;
919 else {
920 end = strchr(tok->inp, '\0');
921 if (end == tok->inp) {
922 tok->done = E_EOF;
923 return EOF;
924 }
925 }
926 if (tok->start == NULL)
927 tok->buf = tok->cur;
928 tok->line_start = tok->cur;
929 tok->lineno++;
930 tok->inp = end;
931 return Py_CHARMASK(*tok->cur++);
932 }
933 if (tok->prompt != NULL) {
934 char *newtok = PyOS_Readline(stdin, stdout, tok->prompt);
Victor Stinner034c7532011-01-07 18:56:19 +0000935#ifndef PGEN
Victor Stinner89e34362011-01-07 18:47:22 +0000936 if (newtok != NULL) {
937 char *translated = translate_newlines(newtok, 0, tok);
938 PyMem_FREE(newtok);
939 if (translated == NULL)
940 return EOF;
941 newtok = translated;
942 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 if (tok->encoding && newtok && *newtok) {
944 /* Recode to UTF-8 */
945 Py_ssize_t buflen;
946 const char* buf;
947 PyObject *u = translate_into_utf8(newtok, tok->encoding);
948 PyMem_FREE(newtok);
949 if (!u) {
950 tok->done = E_DECODE;
951 return EOF;
952 }
953 buflen = PyBytes_GET_SIZE(u);
954 buf = PyBytes_AS_STRING(u);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 newtok = PyMem_MALLOC(buflen+1);
956 strcpy(newtok, buf);
957 Py_DECREF(u);
958 }
Martin v. Löwis85bcc662007-09-04 09:18:06 +0000959#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 if (tok->nextprompt != NULL)
961 tok->prompt = tok->nextprompt;
962 if (newtok == NULL)
963 tok->done = E_INTR;
964 else if (*newtok == '\0') {
965 PyMem_FREE(newtok);
966 tok->done = E_EOF;
967 }
968 else if (tok->start != NULL) {
969 size_t start = tok->start - tok->buf;
970 size_t oldlen = tok->cur - tok->buf;
971 size_t newlen = oldlen + strlen(newtok);
972 char *buf = tok->buf;
973 buf = (char *)PyMem_REALLOC(buf, newlen+1);
974 tok->lineno++;
975 if (buf == NULL) {
976 PyMem_FREE(tok->buf);
977 tok->buf = NULL;
978 PyMem_FREE(newtok);
979 tok->done = E_NOMEM;
980 return EOF;
981 }
982 tok->buf = buf;
983 tok->cur = tok->buf + oldlen;
984 tok->line_start = tok->cur;
985 strcpy(tok->buf + oldlen, newtok);
986 PyMem_FREE(newtok);
987 tok->inp = tok->buf + newlen;
988 tok->end = tok->inp + 1;
989 tok->start = tok->buf + start;
990 }
991 else {
992 tok->lineno++;
993 if (tok->buf != NULL)
994 PyMem_FREE(tok->buf);
995 tok->buf = newtok;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 tok->cur = tok->buf;
997 tok->line_start = tok->buf;
998 tok->inp = strchr(tok->buf, '\0');
999 tok->end = tok->inp + 1;
1000 }
1001 }
1002 else {
1003 int done = 0;
1004 Py_ssize_t cur = 0;
1005 char *pt;
1006 if (tok->start == NULL) {
1007 if (tok->buf == NULL) {
1008 tok->buf = (char *)
1009 PyMem_MALLOC(BUFSIZ);
1010 if (tok->buf == NULL) {
1011 tok->done = E_NOMEM;
1012 return EOF;
1013 }
1014 tok->end = tok->buf + BUFSIZ;
1015 }
1016 if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf),
1017 tok) == NULL) {
Serhiy Storchaka0d441112015-11-14 15:10:35 +02001018 if (!tok->decoding_erred)
1019 tok->done = E_EOF;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 done = 1;
1021 }
1022 else {
1023 tok->done = E_OK;
1024 tok->inp = strchr(tok->buf, '\0');
Benjamin Peterson26d998c2016-09-18 23:41:11 -07001025 done = tok->inp == tok->buf || tok->inp[-1] == '\n';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 }
1027 }
1028 else {
1029 cur = tok->cur - tok->buf;
1030 if (decoding_feof(tok)) {
1031 tok->done = E_EOF;
1032 done = 1;
1033 }
1034 else
1035 tok->done = E_OK;
1036 }
1037 tok->lineno++;
1038 /* Read until '\n' or EOF */
1039 while (!done) {
1040 Py_ssize_t curstart = tok->start == NULL ? -1 :
1041 tok->start - tok->buf;
1042 Py_ssize_t curvalid = tok->inp - tok->buf;
1043 Py_ssize_t newsize = curvalid + BUFSIZ;
1044 char *newbuf = tok->buf;
1045 newbuf = (char *)PyMem_REALLOC(newbuf,
1046 newsize);
1047 if (newbuf == NULL) {
1048 tok->done = E_NOMEM;
1049 tok->cur = tok->inp;
1050 return EOF;
1051 }
1052 tok->buf = newbuf;
Serhiy Storchaka0d441112015-11-14 15:10:35 +02001053 tok->cur = tok->buf + cur;
1054 tok->line_start = tok->cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 tok->inp = tok->buf + curvalid;
1056 tok->end = tok->buf + newsize;
1057 tok->start = curstart < 0 ? NULL :
1058 tok->buf + curstart;
1059 if (decoding_fgets(tok->inp,
1060 (int)(tok->end - tok->inp),
1061 tok) == NULL) {
1062 /* Break out early on decoding
1063 errors, as tok->buf will be NULL
1064 */
1065 if (tok->decoding_erred)
1066 return EOF;
1067 /* Last line does not end in \n,
1068 fake one */
1069 strcpy(tok->inp, "\n");
1070 }
1071 tok->inp = strchr(tok->inp, '\0');
1072 done = tok->inp[-1] == '\n';
1073 }
1074 if (tok->buf != NULL) {
1075 tok->cur = tok->buf + cur;
1076 tok->line_start = tok->cur;
1077 /* replace "\r\n" with "\n" */
1078 /* For Mac leave the \r, giving a syntax error */
1079 pt = tok->inp - 2;
1080 if (pt >= tok->buf && *pt == '\r') {
1081 *pt++ = '\n';
1082 *pt = '\0';
1083 tok->inp = pt;
1084 }
1085 }
1086 }
1087 if (tok->done != E_OK) {
1088 if (tok->prompt != NULL)
1089 PySys_WriteStderr("\n");
1090 tok->cur = tok->inp;
1091 return EOF;
1092 }
1093 }
1094 /*NOTREACHED*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001095}
1096
1097
1098/* Back-up one character */
1099
1100static void
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001101tok_backup(struct tok_state *tok, int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 if (c != EOF) {
1104 if (--tok->cur < tok->buf)
1105 Py_FatalError("tok_backup: beginning of buffer");
1106 if (*tok->cur != c)
1107 *tok->cur = c;
1108 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001109}
1110
1111
1112/* Return the token corresponding to a single character */
1113
1114int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001115PyToken_OneChar(int c)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 switch (c) {
1118 case '(': return LPAR;
1119 case ')': return RPAR;
1120 case '[': return LSQB;
1121 case ']': return RSQB;
1122 case ':': return COLON;
1123 case ',': return COMMA;
1124 case ';': return SEMI;
1125 case '+': return PLUS;
1126 case '-': return MINUS;
1127 case '*': return STAR;
1128 case '/': return SLASH;
1129 case '|': return VBAR;
1130 case '&': return AMPER;
1131 case '<': return LESS;
1132 case '>': return GREATER;
1133 case '=': return EQUAL;
1134 case '.': return DOT;
1135 case '%': return PERCENT;
1136 case '{': return LBRACE;
1137 case '}': return RBRACE;
1138 case '^': return CIRCUMFLEX;
1139 case '~': return TILDE;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001140 case '@': return AT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 default: return OP;
1142 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001143}
1144
1145
Guido van Rossumfbab9051991-10-20 20:25:03 +00001146int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001147PyToken_TwoChars(int c1, int c2)
Guido van Rossumfbab9051991-10-20 20:25:03 +00001148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 switch (c1) {
1150 case '=':
1151 switch (c2) {
1152 case '=': return EQEQUAL;
1153 }
1154 break;
1155 case '!':
1156 switch (c2) {
1157 case '=': return NOTEQUAL;
1158 }
1159 break;
1160 case '<':
1161 switch (c2) {
1162 case '>': return NOTEQUAL;
1163 case '=': return LESSEQUAL;
1164 case '<': return LEFTSHIFT;
1165 }
1166 break;
1167 case '>':
1168 switch (c2) {
1169 case '=': return GREATEREQUAL;
1170 case '>': return RIGHTSHIFT;
1171 }
1172 break;
1173 case '+':
1174 switch (c2) {
1175 case '=': return PLUSEQUAL;
1176 }
1177 break;
1178 case '-':
1179 switch (c2) {
1180 case '=': return MINEQUAL;
1181 case '>': return RARROW;
1182 }
1183 break;
1184 case '*':
1185 switch (c2) {
1186 case '*': return DOUBLESTAR;
1187 case '=': return STAREQUAL;
1188 }
1189 break;
1190 case '/':
1191 switch (c2) {
1192 case '/': return DOUBLESLASH;
1193 case '=': return SLASHEQUAL;
1194 }
1195 break;
1196 case '|':
1197 switch (c2) {
1198 case '=': return VBAREQUAL;
1199 }
1200 break;
1201 case '%':
1202 switch (c2) {
1203 case '=': return PERCENTEQUAL;
1204 }
1205 break;
1206 case '&':
1207 switch (c2) {
1208 case '=': return AMPEREQUAL;
1209 }
1210 break;
1211 case '^':
1212 switch (c2) {
1213 case '=': return CIRCUMFLEXEQUAL;
1214 }
1215 break;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001216 case '@':
1217 switch (c2) {
1218 case '=': return ATEQUAL;
1219 }
1220 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 }
1222 return OP;
Guido van Rossumfbab9051991-10-20 20:25:03 +00001223}
1224
Thomas Wouters434d0822000-08-24 20:11:32 +00001225int
1226PyToken_ThreeChars(int c1, int c2, int c3)
1227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 switch (c1) {
1229 case '<':
1230 switch (c2) {
1231 case '<':
1232 switch (c3) {
1233 case '=':
1234 return LEFTSHIFTEQUAL;
1235 }
1236 break;
1237 }
1238 break;
1239 case '>':
1240 switch (c2) {
1241 case '>':
1242 switch (c3) {
1243 case '=':
1244 return RIGHTSHIFTEQUAL;
1245 }
1246 break;
1247 }
1248 break;
1249 case '*':
1250 switch (c2) {
1251 case '*':
1252 switch (c3) {
1253 case '=':
1254 return DOUBLESTAREQUAL;
1255 }
1256 break;
1257 }
1258 break;
1259 case '/':
1260 switch (c2) {
1261 case '/':
1262 switch (c3) {
1263 case '=':
1264 return DOUBLESLASHEQUAL;
1265 }
1266 break;
1267 }
1268 break;
1269 case '.':
1270 switch (c2) {
Georg Brandldde00282007-03-18 19:01:53 +00001271 case '.':
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 switch (c3) {
1273 case '.':
1274 return ELLIPSIS;
1275 }
1276 break;
1277 }
1278 break;
1279 }
1280 return OP;
Thomas Wouters434d0822000-08-24 20:11:32 +00001281}
Guido van Rossumfbab9051991-10-20 20:25:03 +00001282
Guido van Rossum926f13a1998-04-09 21:38:06 +00001283static int
Thomas Wouters23c9e002000-07-22 19:20:54 +00001284indenterror(struct tok_state *tok)
Guido van Rossum926f13a1998-04-09 21:38:06 +00001285{
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001286 tok->done = E_TABSPACE;
1287 tok->cur = tok->inp;
1288 return ERRORTOKEN;
Guido van Rossum926f13a1998-04-09 21:38:06 +00001289}
1290
Martin v. Löwis47383402007-08-15 07:32:56 +00001291#ifdef PGEN
Victor Stinner52f6dd72010-03-12 14:45:56 +00001292#define verify_identifier(tok) 1
Martin v. Löwis47383402007-08-15 07:32:56 +00001293#else
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001294/* Verify that the identifier follows PEP 3131.
1295 All identifier strings are guaranteed to be "ready" unicode objects.
1296 */
Martin v. Löwis47383402007-08-15 07:32:56 +00001297static int
Victor Stinner52f6dd72010-03-12 14:45:56 +00001298verify_identifier(struct tok_state *tok)
Martin v. Löwis47383402007-08-15 07:32:56 +00001299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 PyObject *s;
1301 int result;
Benjamin Petersond73aca72015-04-21 12:05:19 -04001302 if (tok->decoding_erred)
1303 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001305 if (s == NULL || PyUnicode_READY(s) == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
1307 PyErr_Clear();
1308 tok->done = E_IDENTIFIER;
1309 } else {
1310 tok->done = E_ERROR;
1311 }
1312 return 0;
1313 }
1314 result = PyUnicode_IsIdentifier(s);
1315 Py_DECREF(s);
1316 if (result == 0)
1317 tok->done = E_IDENTIFIER;
1318 return result;
Martin v. Löwis47383402007-08-15 07:32:56 +00001319}
1320#endif
Guido van Rossum926f13a1998-04-09 21:38:06 +00001321
Brett Cannona721aba2016-09-09 14:57:09 -07001322static int
1323tok_decimal_tail(struct tok_state *tok)
1324{
1325 int c;
1326
1327 while (1) {
1328 do {
1329 c = tok_nextc(tok);
1330 } while (isdigit(c));
1331 if (c != '_') {
1332 break;
1333 }
1334 c = tok_nextc(tok);
1335 if (!isdigit(c)) {
1336 tok->done = E_TOKEN;
1337 tok_backup(tok, c);
1338 return 0;
1339 }
1340 }
1341 return c;
1342}
1343
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001344/* Get next token, after space stripping etc. */
1345
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001346static int
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001347tok_get(struct tok_state *tok, char **p_start, char **p_end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001348{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001349 int c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 int blankline, nonascii;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 *p_start = *p_end = NULL;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001353 nextline:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 tok->start = NULL;
1355 blankline = 0;
Guido van Rossum8c11a5c1991-07-27 21:42:56 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 /* Get indentation level */
1358 if (tok->atbol) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001359 int col = 0;
1360 int altcol = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 tok->atbol = 0;
1362 for (;;) {
1363 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001364 if (c == ' ') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 col++, altcol++;
Brett Cannona721aba2016-09-09 14:57:09 -07001366 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 else if (c == '\t') {
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001368 col = (col / tok->tabsize + 1) * tok->tabsize;
1369 altcol = (altcol / ALTTABSIZE + 1) * ALTTABSIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 }
Brett Cannona721aba2016-09-09 14:57:09 -07001371 else if (c == '\014') {/* Control-L (formfeed) */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 col = altcol = 0; /* For Emacs users */
Brett Cannona721aba2016-09-09 14:57:09 -07001373 }
1374 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 break;
Brett Cannona721aba2016-09-09 14:57:09 -07001376 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 }
1378 tok_backup(tok, c);
1379 if (c == '#' || c == '\n') {
1380 /* Lines with only whitespace and/or comments
1381 shouldn't affect the indentation and are
1382 not passed to the parser as NEWLINE tokens,
1383 except *totally* empty lines in interactive
1384 mode, which signal the end of a command group. */
Brett Cannona721aba2016-09-09 14:57:09 -07001385 if (col == 0 && c == '\n' && tok->prompt != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 blankline = 0; /* Let it through */
Brett Cannona721aba2016-09-09 14:57:09 -07001387 }
1388 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 blankline = 1; /* Ignore completely */
Brett Cannona721aba2016-09-09 14:57:09 -07001390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 /* We can't jump back right here since we still
1392 may need to skip to the end of a comment */
1393 }
1394 if (!blankline && tok->level == 0) {
1395 if (col == tok->indstack[tok->indent]) {
1396 /* No change */
1397 if (altcol != tok->altindstack[tok->indent]) {
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001398 return indenterror(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 }
1400 }
1401 else if (col > tok->indstack[tok->indent]) {
1402 /* Indent -- always one */
1403 if (tok->indent+1 >= MAXINDENT) {
1404 tok->done = E_TOODEEP;
1405 tok->cur = tok->inp;
1406 return ERRORTOKEN;
1407 }
1408 if (altcol <= tok->altindstack[tok->indent]) {
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001409 return indenterror(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 }
1411 tok->pendin++;
1412 tok->indstack[++tok->indent] = col;
1413 tok->altindstack[tok->indent] = altcol;
1414 }
1415 else /* col < tok->indstack[tok->indent] */ {
1416 /* Dedent -- any number, must be consistent */
1417 while (tok->indent > 0 &&
1418 col < tok->indstack[tok->indent]) {
1419 tok->pendin--;
1420 tok->indent--;
1421 }
1422 if (col != tok->indstack[tok->indent]) {
1423 tok->done = E_DEDENT;
1424 tok->cur = tok->inp;
1425 return ERRORTOKEN;
1426 }
1427 if (altcol != tok->altindstack[tok->indent]) {
Victor Stinnerf2ddc6a2017-11-17 01:25:47 -08001428 return indenterror(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 }
1430 }
1431 }
1432 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 tok->start = tok->cur;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* Return pending indents/dedents */
1437 if (tok->pendin != 0) {
1438 if (tok->pendin < 0) {
1439 tok->pendin++;
1440 return DEDENT;
1441 }
1442 else {
1443 tok->pendin--;
1444 return INDENT;
1445 }
1446 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001447
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001448 again:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 tok->start = NULL;
1450 /* Skip spaces */
1451 do {
1452 c = tok_nextc(tok);
1453 } while (c == ' ' || c == '\t' || c == '\014');
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 /* Set start of current token */
1456 tok->start = tok->cur - 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 /* Skip comment */
Brett Cannona721aba2016-09-09 14:57:09 -07001459 if (c == '#') {
1460 while (c != EOF && c != '\n') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001462 }
1463 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 /* Check for EOF and errors now */
1466 if (c == EOF) {
1467 return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN;
1468 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 /* Identifier (most frequent token!) */
1471 nonascii = 0;
1472 if (is_potential_identifier_start(c)) {
Berker Peksag6f805622017-02-05 04:32:39 +03001473 /* Process the various legal combinations of b"", r"", u"", and f"". */
Eric V. Smith235a6f02015-09-19 14:51:32 -04001474 int saw_b = 0, saw_r = 0, saw_u = 0, saw_f = 0;
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01001475 while (1) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04001476 if (!(saw_b || saw_u || saw_f) && (c == 'b' || c == 'B'))
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01001477 saw_b = 1;
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00001478 /* Since this is a backwards compatibility support literal we don't
1479 want to support it in arbitrary order like byte literals. */
Brett Cannona721aba2016-09-09 14:57:09 -07001480 else if (!(saw_b || saw_u || saw_r || saw_f)
1481 && (c == 'u'|| c == 'U')) {
Armin Ronacher6ecf77b2012-03-04 12:04:06 +00001482 saw_u = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001483 }
Christian Heimes0b3847d2012-06-20 11:17:58 +02001484 /* ur"" and ru"" are not supported */
Brett Cannona721aba2016-09-09 14:57:09 -07001485 else if (!(saw_r || saw_u) && (c == 'r' || c == 'R')) {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01001486 saw_r = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001487 }
1488 else if (!(saw_f || saw_b || saw_u) && (c == 'f' || c == 'F')) {
Eric V. Smith235a6f02015-09-19 14:51:32 -04001489 saw_f = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001490 }
1491 else {
Antoine Pitrou3a5d4cb2012-01-12 22:46:19 +01001492 break;
Brett Cannona721aba2016-09-09 14:57:09 -07001493 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001495 if (c == '"' || c == '\'') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 goto letter_quote;
Brett Cannona721aba2016-09-09 14:57:09 -07001497 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 }
1499 while (is_potential_identifier_char(c)) {
Brett Cannona721aba2016-09-09 14:57:09 -07001500 if (c >= 128) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 nonascii = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 c = tok_nextc(tok);
1504 }
1505 tok_backup(tok, c);
Brett Cannona721aba2016-09-09 14:57:09 -07001506 if (nonascii && !verify_identifier(tok)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 return ERRORTOKEN;
Brett Cannona721aba2016-09-09 14:57:09 -07001508 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 *p_start = tok->start;
1510 *p_end = tok->cur;
Yury Selivanov75445082015-05-11 22:57:16 -04001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 return NAME;
1513 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 /* Newline */
1516 if (c == '\n') {
1517 tok->atbol = 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001518 if (blankline || tok->level > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 goto nextline;
Brett Cannona721aba2016-09-09 14:57:09 -07001520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 *p_start = tok->start;
1522 *p_end = tok->cur - 1; /* Leave '\n' out of the string */
1523 tok->cont_line = 0;
1524 return NEWLINE;
1525 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 /* Period or number starting with period? */
1528 if (c == '.') {
1529 c = tok_nextc(tok);
1530 if (isdigit(c)) {
1531 goto fraction;
1532 } else if (c == '.') {
1533 c = tok_nextc(tok);
1534 if (c == '.') {
1535 *p_start = tok->start;
1536 *p_end = tok->cur;
1537 return ELLIPSIS;
Brett Cannona721aba2016-09-09 14:57:09 -07001538 }
1539 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 tok_backup(tok, c);
1541 }
1542 tok_backup(tok, '.');
Brett Cannona721aba2016-09-09 14:57:09 -07001543 }
1544 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 tok_backup(tok, c);
1546 }
1547 *p_start = tok->start;
1548 *p_end = tok->cur;
1549 return DOT;
1550 }
Guido van Rossumf595fde1996-01-12 01:31:58 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 /* Number */
1553 if (isdigit(c)) {
1554 if (c == '0') {
1555 /* Hex, octal or binary -- maybe. */
1556 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (c == 'x' || c == 'X') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 /* Hex */
1559 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 do {
Brett Cannona721aba2016-09-09 14:57:09 -07001561 if (c == '_') {
1562 c = tok_nextc(tok);
1563 }
1564 if (!isxdigit(c)) {
1565 tok->done = E_TOKEN;
1566 tok_backup(tok, c);
1567 return ERRORTOKEN;
1568 }
1569 do {
1570 c = tok_nextc(tok);
1571 } while (isxdigit(c));
1572 } while (c == '_');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 }
1574 else if (c == 'o' || c == 'O') {
1575 /* Octal */
1576 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 do {
Brett Cannona721aba2016-09-09 14:57:09 -07001578 if (c == '_') {
1579 c = tok_nextc(tok);
1580 }
1581 if (c < '0' || c >= '8') {
1582 tok->done = E_TOKEN;
1583 tok_backup(tok, c);
1584 return ERRORTOKEN;
1585 }
1586 do {
1587 c = tok_nextc(tok);
1588 } while ('0' <= c && c < '8');
1589 } while (c == '_');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 }
1591 else if (c == 'b' || c == 'B') {
1592 /* Binary */
1593 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 do {
Brett Cannona721aba2016-09-09 14:57:09 -07001595 if (c == '_') {
1596 c = tok_nextc(tok);
1597 }
1598 if (c != '0' && c != '1') {
1599 tok->done = E_TOKEN;
1600 tok_backup(tok, c);
1601 return ERRORTOKEN;
1602 }
1603 do {
1604 c = tok_nextc(tok);
1605 } while (c == '0' || c == '1');
1606 } while (c == '_');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 }
1608 else {
1609 int nonzero = 0;
1610 /* maybe old-style octal; c is first char of it */
1611 /* in any case, allow '0' as a literal */
Brett Cannona721aba2016-09-09 14:57:09 -07001612 while (1) {
1613 if (c == '_') {
1614 c = tok_nextc(tok);
1615 if (!isdigit(c)) {
1616 tok->done = E_TOKEN;
1617 tok_backup(tok, c);
1618 return ERRORTOKEN;
1619 }
1620 }
1621 if (c != '0') {
1622 break;
1623 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 c = tok_nextc(tok);
1625 }
Brett Cannona721aba2016-09-09 14:57:09 -07001626 if (isdigit(c)) {
1627 nonzero = 1;
1628 c = tok_decimal_tail(tok);
1629 if (c == 0) {
1630 return ERRORTOKEN;
1631 }
1632 }
1633 if (c == '.') {
1634 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 goto fraction;
Brett Cannona721aba2016-09-09 14:57:09 -07001636 }
1637 else if (c == 'e' || c == 'E') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 goto exponent;
Brett Cannona721aba2016-09-09 14:57:09 -07001639 }
1640 else if (c == 'j' || c == 'J') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 goto imaginary;
Brett Cannona721aba2016-09-09 14:57:09 -07001642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 else if (nonzero) {
Brett Cannona721aba2016-09-09 14:57:09 -07001644 /* Old-style octal: now disallowed. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 tok->done = E_TOKEN;
1646 tok_backup(tok, c);
1647 return ERRORTOKEN;
1648 }
1649 }
1650 }
1651 else {
1652 /* Decimal */
Brett Cannona721aba2016-09-09 14:57:09 -07001653 c = tok_decimal_tail(tok);
1654 if (c == 0) {
1655 return ERRORTOKEN;
1656 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 {
1658 /* Accept floating point numbers. */
1659 if (c == '.') {
Brett Cannona721aba2016-09-09 14:57:09 -07001660 c = tok_nextc(tok);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 fraction:
1662 /* Fraction */
Brett Cannona721aba2016-09-09 14:57:09 -07001663 if (isdigit(c)) {
1664 c = tok_decimal_tail(tok);
1665 if (c == 0) {
1666 return ERRORTOKEN;
1667 }
1668 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 }
1670 if (c == 'e' || c == 'E') {
Benjamin Petersonc4161622014-06-07 12:36:39 -07001671 int e;
1672 exponent:
1673 e = c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 /* Exponent part */
1675 c = tok_nextc(tok);
Benjamin Petersonc4161622014-06-07 12:36:39 -07001676 if (c == '+' || c == '-') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 c = tok_nextc(tok);
Benjamin Petersonc4161622014-06-07 12:36:39 -07001678 if (!isdigit(c)) {
1679 tok->done = E_TOKEN;
1680 tok_backup(tok, c);
1681 return ERRORTOKEN;
1682 }
1683 } else if (!isdigit(c)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 tok_backup(tok, c);
Benjamin Petersonc4161622014-06-07 12:36:39 -07001685 tok_backup(tok, e);
1686 *p_start = tok->start;
1687 *p_end = tok->cur;
1688 return NUMBER;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 }
Brett Cannona721aba2016-09-09 14:57:09 -07001690 c = tok_decimal_tail(tok);
1691 if (c == 0) {
1692 return ERRORTOKEN;
1693 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 }
Brett Cannona721aba2016-09-09 14:57:09 -07001695 if (c == 'j' || c == 'J') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 /* Imaginary part */
1697 imaginary:
1698 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001699 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 }
1701 }
1702 tok_backup(tok, c);
1703 *p_start = tok->start;
1704 *p_end = tok->cur;
1705 return NUMBER;
1706 }
Guido van Rossum24dacb31997-04-06 03:46:20 +00001707
1708 letter_quote:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 /* String */
1710 if (c == '\'' || c == '"') {
1711 int quote = c;
1712 int quote_size = 1; /* 1 or 3 */
1713 int end_quote_size = 0;
Guido van Rossumcf171a72007-11-16 00:51:45 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 /* Find the quote size and start of string */
1716 c = tok_nextc(tok);
1717 if (c == quote) {
1718 c = tok_nextc(tok);
Brett Cannona721aba2016-09-09 14:57:09 -07001719 if (c == quote) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 quote_size = 3;
Brett Cannona721aba2016-09-09 14:57:09 -07001721 }
1722 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 end_quote_size = 1; /* empty string found */
Brett Cannona721aba2016-09-09 14:57:09 -07001724 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 }
Brett Cannona721aba2016-09-09 14:57:09 -07001726 if (c != quote) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 tok_backup(tok, c);
Brett Cannona721aba2016-09-09 14:57:09 -07001728 }
Guido van Rossumcf171a72007-11-16 00:51:45 +00001729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 /* Get rest of string */
1731 while (end_quote_size != quote_size) {
1732 c = tok_nextc(tok);
1733 if (c == EOF) {
Brett Cannona721aba2016-09-09 14:57:09 -07001734 if (quote_size == 3) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 tok->done = E_EOFS;
Brett Cannona721aba2016-09-09 14:57:09 -07001736 }
1737 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738 tok->done = E_EOLS;
Brett Cannona721aba2016-09-09 14:57:09 -07001739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 tok->cur = tok->inp;
1741 return ERRORTOKEN;
1742 }
1743 if (quote_size == 1 && c == '\n') {
1744 tok->done = E_EOLS;
1745 tok->cur = tok->inp;
1746 return ERRORTOKEN;
1747 }
Brett Cannona721aba2016-09-09 14:57:09 -07001748 if (c == quote) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 end_quote_size += 1;
Brett Cannona721aba2016-09-09 14:57:09 -07001750 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 else {
1752 end_quote_size = 0;
Brett Cannona721aba2016-09-09 14:57:09 -07001753 if (c == '\\') {
Christian Heimesc6cc23d2016-09-09 00:09:45 +02001754 tok_nextc(tok); /* skip escaped char */
Brett Cannona721aba2016-09-09 14:57:09 -07001755 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 }
1757 }
Guido van Rossumcf171a72007-11-16 00:51:45 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 *p_start = tok->start;
1760 *p_end = tok->cur;
1761 return STRING;
1762 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 /* Line continuation */
1765 if (c == '\\') {
1766 c = tok_nextc(tok);
1767 if (c != '\n') {
1768 tok->done = E_LINECONT;
1769 tok->cur = tok->inp;
1770 return ERRORTOKEN;
1771 }
1772 tok->cont_line = 1;
1773 goto again; /* Read next line */
1774 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 /* Check for two-character token */
1777 {
1778 int c2 = tok_nextc(tok);
1779 int token = PyToken_TwoChars(c, c2);
1780 if (token != OP) {
1781 int c3 = tok_nextc(tok);
1782 int token3 = PyToken_ThreeChars(c, c2, c3);
1783 if (token3 != OP) {
1784 token = token3;
Brett Cannona721aba2016-09-09 14:57:09 -07001785 }
1786 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 tok_backup(tok, c3);
1788 }
1789 *p_start = tok->start;
1790 *p_end = tok->cur;
1791 return token;
1792 }
1793 tok_backup(tok, c2);
1794 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 /* Keep track of parentheses nesting level */
1797 switch (c) {
1798 case '(':
1799 case '[':
1800 case '{':
1801 tok->level++;
1802 break;
1803 case ')':
1804 case ']':
1805 case '}':
1806 tok->level--;
1807 break;
1808 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 /* Punctuation character */
1811 *p_start = tok->start;
1812 *p_end = tok->cur;
1813 return PyToken_OneChar(c);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001814}
1815
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001816int
1817PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end)
1818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 int result = tok_get(tok, p_start, p_end);
1820 if (tok->decoding_erred) {
1821 result = ERRORTOKEN;
1822 tok->done = E_DECODE;
1823 }
1824 return result;
Martin v. Löwis00f1e3f2002-08-04 17:29:52 +00001825}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001826
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001827/* Get the encoding of a Python file. Check for the coding cookie and check if
1828 the file starts with a BOM.
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001829
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001830 PyTokenizer_FindEncodingFilename() returns NULL when it can't find the
1831 encoding in the first or second line of the file (in which case the encoding
1832 should be assumed to be UTF-8).
Brett Cannone4539892007-10-20 03:46:49 +00001833
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001834 The char* returned is malloc'ed via PyMem_MALLOC() and thus must be freed
1835 by the caller. */
1836
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001837char *
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001838PyTokenizer_FindEncodingFilename(int fd, PyObject *filename)
Guido van Rossum40d20bc2007-10-22 00:09:51 +00001839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 struct tok_state *tok;
1841 FILE *fp;
1842 char *p_start =NULL , *p_end =NULL , *encoding = NULL;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001843
Victor Stinnerdaf45552013-08-28 00:53:59 +02001844#ifndef PGEN
1845 fd = _Py_dup(fd);
1846#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 fd = dup(fd);
Victor Stinnerdaf45552013-08-28 00:53:59 +02001848#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 if (fd < 0) {
1850 return NULL;
1851 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 fp = fdopen(fd, "r");
1854 if (fp == NULL) {
1855 return NULL;
1856 }
1857 tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL);
1858 if (tok == NULL) {
1859 fclose(fp);
1860 return NULL;
1861 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001862#ifndef PGEN
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001863 if (filename != NULL) {
1864 Py_INCREF(filename);
1865 tok->filename = filename;
1866 }
1867 else {
1868 tok->filename = PyUnicode_FromString("<string>");
1869 if (tok->filename == NULL) {
1870 fclose(fp);
1871 PyTokenizer_Free(tok);
1872 return encoding;
1873 }
1874 }
Victor Stinner7f2fee32011-04-05 00:39:01 +02001875#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 while (tok->lineno < 2 && tok->done == E_OK) {
1877 PyTokenizer_Get(tok, &p_start, &p_end);
1878 }
1879 fclose(fp);
1880 if (tok->encoding) {
1881 encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1);
1882 if (encoding)
1883 strcpy(encoding, tok->encoding);
1884 }
1885 PyTokenizer_Free(tok);
1886 return encoding;
Guido van Rossumce3a72a2007-10-19 23:16:50 +00001887}
Thomas Wouters89d996e2007-09-08 17:39:28 +00001888
Victor Stinnerfe7c5b52011-04-05 01:48:03 +02001889char *
1890PyTokenizer_FindEncoding(int fd)
1891{
1892 return PyTokenizer_FindEncodingFilename(fd, NULL);
1893}
1894
Guido van Rossum408027e1996-12-30 16:17:54 +00001895#ifdef Py_DEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001896
1897void
Thomas Wouters23c9e002000-07-22 19:20:54 +00001898tok_dump(int type, char *start, char *end)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001899{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 printf("%s", _PyParser_TokenNames[type]);
1901 if (type == NAME || type == NUMBER || type == STRING || type == OP)
1902 printf("(%.*s)", (int)(end - start), start);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001903}
1904
1905#endif