Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Tokenizer implementation */ |
| 3 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | #include "pgenheaders.h" |
| 6 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 8 | #include <assert.h> |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | #include "tokenizer.h" |
| 11 | #include "errcode.h" |
| 12 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 13 | #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öwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 21 | extern char *PyOS_Readline(FILE *, FILE *, char *); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 22 | /* Return malloc'ed string including trailing \n; |
| 23 | empty malloc'ed string for EOF; |
| 24 | NULL if interrupted */ |
| 25 | |
Guido van Rossum | 4fe8729 | 1992-02-26 15:24:44 +0000 | [diff] [blame] | 26 | /* Don't ever change this -- it would break the portability of Python code */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | #define TABSIZE 8 |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | cf57d8b | 1998-01-19 22:07:46 +0000 | [diff] [blame] | 29 | /* 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 Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 37 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 38 | static struct tok_state *tok_new(void); |
| 39 | static int tok_nextc(struct tok_state *tok); |
| 40 | static void tok_backup(struct tok_state *tok, int c); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | /* Token names */ |
| 43 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 44 | char *_PyParser_TokenNames[] = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | "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 Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 73 | "EQEQUAL", |
| 74 | "NOTEQUAL", |
| 75 | "LESSEQUAL", |
| 76 | "GREATEREQUAL", |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 77 | "TILDE", |
| 78 | "CIRCUMFLEX", |
| 79 | "LEFTSHIFT", |
| 80 | "RIGHTSHIFT", |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 81 | "DOUBLESTAR", |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 82 | "PLUSEQUAL", |
| 83 | "MINEQUAL", |
| 84 | "STAREQUAL", |
| 85 | "SLASHEQUAL", |
| 86 | "PERCENTEQUAL", |
| 87 | "AMPEREQUAL", |
| 88 | "VBAREQUAL", |
| 89 | "CIRCUMFLEXEQUAL", |
| 90 | "LEFTSHIFTEQUAL", |
| 91 | "RIGHTSHIFTEQUAL", |
| 92 | "DOUBLESTAREQUAL", |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 93 | "DOUBLESLASH", |
| 94 | "DOUBLESLASHEQUAL", |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 95 | /* This table must match the #defines in token.h! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | "OP", |
| 97 | "<ERRORTOKEN>", |
| 98 | "<N_TOKENS>" |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | /* Create and initialize a new tok_state structure */ |
| 103 | |
| 104 | static struct tok_state * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 105 | tok_new(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 107 | struct tok_state *tok = PyMem_NEW(struct tok_state, 1); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 108 | if (tok == NULL) |
| 109 | return NULL; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 110 | tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | tok->done = E_OK; |
| 112 | tok->fp = NULL; |
| 113 | tok->tabsize = TABSIZE; |
| 114 | tok->indent = 0; |
| 115 | tok->indstack[0] = 0; |
| 116 | tok->atbol = 1; |
| 117 | tok->pendin = 0; |
| 118 | tok->prompt = tok->nextprompt = NULL; |
| 119 | tok->lineno = 0; |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 120 | tok->level = 0; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 121 | tok->filename = NULL; |
| 122 | tok->altwarning = 0; |
| 123 | tok->alterror = 0; |
| 124 | tok->alttabsize = 1; |
| 125 | tok->altindstack[0] = 0; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 126 | tok->decoding_state = 0; |
| 127 | tok->decoding_erred = 0; |
| 128 | tok->read_coding_spec = 0; |
| 129 | tok->issued_encoding_warning = 0; |
| 130 | tok->encoding = NULL; |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 131 | tok->cont_line = 0; |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 132 | #ifndef PGEN |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 133 | tok->decoding_readline = NULL; |
| 134 | tok->decoding_buffer = NULL; |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 135 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | return tok; |
| 137 | } |
| 138 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 139 | #ifdef PGEN |
| 140 | |
| 141 | static char * |
| 142 | decoding_fgets(char *s, int size, struct tok_state *tok) |
| 143 | { |
| 144 | return fgets(s, size, tok->fp); |
| 145 | } |
| 146 | |
| 147 | static int |
| 148 | decoding_feof(struct tok_state *tok) |
| 149 | { |
| 150 | return feof(tok->fp); |
| 151 | } |
| 152 | |
| 153 | static const char * |
| 154 | decode_str(const char *str, struct tok_state *tok) |
| 155 | { |
| 156 | return str; |
| 157 | } |
| 158 | |
| 159 | #else /* PGEN */ |
| 160 | |
| 161 | static char * |
| 162 | error_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 | |
| 171 | static char * |
| 172 | new_string(const char *s, int len) |
| 173 | { |
| 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 | |
| 182 | static char * |
| 183 | get_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 | |
| 207 | static char * |
| 208 | get_coding_spec(const char *s, int size) |
| 209 | { |
| 210 | int i; |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 211 | /* 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öwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 220 | 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 Norwitz | e08e1bc | 2002-11-02 20:43:25 +0000 | [diff] [blame] | 231 | while (isalnum((int)t[0]) || |
| 232 | t[0] == '-' || t[0] == '_' || t[0] == '.') |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 233 | 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öwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 239 | PyMem_DEL(r); |
| 240 | r = new_string(q, strlen(q)); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 241 | } |
| 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 | |
| 254 | static int |
| 255 | check_coding_spec(const char* line, int size, struct tok_state *tok, |
| 256 | int set_readline(struct tok_state *, const char *)) |
| 257 | { |
Tim Peters | 17db21f | 2002-09-03 15:39:58 +0000 | [diff] [blame] | 258 | char * cs; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 259 | int r = 1; |
Tim Peters | 17db21f | 2002-09-03 15:39:58 +0000 | [diff] [blame] | 260 | |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 261 | if (tok->cont_line) |
| 262 | /* It's a continuation line, so it can't be a coding spec. */ |
| 263 | return 1; |
Tim Peters | 17db21f | 2002-09-03 15:39:58 +0000 | [diff] [blame] | 264 | cs = get_coding_spec(line, size); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 265 | 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öwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 273 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 274 | r = set_readline(tok, cs); |
| 275 | if (r) { |
| 276 | tok->encoding = cs; |
| 277 | tok->decoding_state = -1; |
| 278 | } |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 279 | #else |
| 280 | /* Without Unicode support, we cannot |
| 281 | process the coding spec. Since there |
| 282 | won't be any Unicode literals, that |
| 283 | won't matter. */ |
| 284 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 285 | } |
| 286 | } else { /* then, compare cs with BOM */ |
| 287 | r = (strcmp(tok->encoding, cs) == 0); |
| 288 | PyMem_DEL(cs); |
| 289 | } |
| 290 | } |
| 291 | return r; |
| 292 | } |
| 293 | |
| 294 | /* See whether the file starts with a BOM. If it does, |
| 295 | invoke the set_readline function with the new encoding. |
| 296 | Return 1 on success, 0 on failure. */ |
| 297 | |
| 298 | static int |
| 299 | check_bom(int get_char(struct tok_state *), |
| 300 | void unget_char(int, struct tok_state *), |
| 301 | int set_readline(struct tok_state *, const char *), |
| 302 | struct tok_state *tok) |
| 303 | { |
| 304 | int ch = get_char(tok); |
| 305 | tok->decoding_state = 1; |
| 306 | if (ch == EOF) { |
| 307 | return 1; |
| 308 | } else if (ch == 0xEF) { |
| 309 | ch = get_char(tok); if (ch != 0xBB) goto NON_BOM; |
| 310 | ch = get_char(tok); if (ch != 0xBF) goto NON_BOM; |
| 311 | #if 0 |
| 312 | /* Disable support for UTF-16 BOMs until a decision |
| 313 | is made whether this needs to be supported. */ |
| 314 | } else if (ch == 0xFE) { |
| 315 | ch = get_char(tok); if (ch != 0xFF) goto NON_BOM; |
| 316 | if (!set_readline(tok, "utf-16-be")) return 0; |
| 317 | tok->decoding_state = -1; |
| 318 | } else if (ch == 0xFF) { |
| 319 | ch = get_char(tok); if (ch != 0xFE) goto NON_BOM; |
| 320 | if (!set_readline(tok, "utf-16-le")) return 0; |
| 321 | tok->decoding_state = -1; |
| 322 | #endif |
| 323 | } else { |
| 324 | unget_char(ch, tok); |
| 325 | return 1; |
| 326 | } |
| 327 | tok->encoding = new_string("utf-8", 5); /* resulting is in utf-8 */ |
| 328 | return 1; |
| 329 | NON_BOM: |
| 330 | /* any token beginning with '\xEF', '\xFE', '\xFF' is a bad token */ |
| 331 | unget_char(0xFF, tok); /* XXX this will cause a syntax error */ |
| 332 | return 1; |
| 333 | } |
| 334 | |
| 335 | /* Read a line of text from TOK into S, using the stream in TOK. |
| 336 | Return NULL on failure, else S. */ |
| 337 | |
| 338 | static char * |
| 339 | fp_readl(char *s, int size, struct tok_state *tok) |
| 340 | { |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 341 | #ifndef Py_USING_UNICODE |
| 342 | /* In a non-Unicode built, this should never be called. */ |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 343 | Py_FatalError("fp_readl should not be called in this build."); |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 344 | return NULL; /* Keep compiler happy (not reachable) */ |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 345 | #else |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 346 | PyObject* utf8; |
| 347 | PyObject* buf = tok->decoding_buffer; |
| 348 | if (buf == NULL) { |
Mark Hammond | a2e303c | 2003-01-14 23:15:22 +0000 | [diff] [blame] | 349 | /* Ask for one less byte so we can terminate it */ |
| 350 | PyObject *args = Py_BuildValue("(i)", size-1); |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 351 | if (args == NULL) |
| 352 | return error_ret(tok); |
| 353 | buf = PyObject_Call(tok->decoding_readline, args, NULL); |
| 354 | Py_DECREF(args); |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 355 | if (buf == NULL) |
| 356 | return error_ret(tok); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 357 | } else { |
| 358 | tok->decoding_buffer = NULL; |
| 359 | } |
| 360 | utf8 = PyUnicode_AsUTF8String(buf); |
| 361 | Py_DECREF(buf); |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 362 | if (utf8 == NULL) |
| 363 | return error_ret(tok); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 364 | else { |
| 365 | const char* str = PyString_AsString(utf8); |
Tim Peters | 919603b | 2002-08-04 17:56:42 +0000 | [diff] [blame] | 366 | assert(strlen(str) < (size_t)size); /* XXX */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 367 | strcpy(s, str); |
| 368 | Py_DECREF(utf8); |
| 369 | if (s[0] == '\0') return NULL; /* EOF */ |
| 370 | return s; |
| 371 | } |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 372 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* Set the readline function for TOK to a StreamReader's |
| 376 | readline function. The StreamReader is named ENC. |
| 377 | |
| 378 | This function is called from check_bom and check_coding_spec. |
| 379 | |
| 380 | ENC is usually identical to the future value of tok->encoding, |
| 381 | except for the (currently unsupported) case of UTF-16. |
| 382 | |
| 383 | Return 1 on success, 0 on failure. */ |
| 384 | |
| 385 | static int |
| 386 | fp_setreadl(struct tok_state *tok, const char* enc) |
| 387 | { |
| 388 | PyObject *reader, *stream, *readline; |
| 389 | |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 390 | /* XXX: constify filename argument. */ |
| 391 | stream = PyFile_FromFile(tok->fp, (char*)tok->filename, "rb", NULL); |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 392 | if (stream == NULL) |
| 393 | return 0; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 394 | |
| 395 | reader = PyCodec_StreamReader(enc, stream, NULL); |
| 396 | Py_DECREF(stream); |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 397 | if (reader == NULL) |
| 398 | return 0; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 399 | |
| 400 | readline = PyObject_GetAttrString(reader, "readline"); |
| 401 | Py_DECREF(reader); |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 402 | if (readline == NULL) |
| 403 | return 0; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 404 | |
| 405 | tok->decoding_readline = readline; |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | /* Fetch the next byte from TOK. */ |
| 410 | |
| 411 | static int fp_getc(struct tok_state *tok) { |
| 412 | return getc(tok->fp); |
| 413 | } |
| 414 | |
| 415 | /* Unfetch the last byte back into TOK. */ |
| 416 | |
| 417 | static void fp_ungetc(int c, struct tok_state *tok) { |
| 418 | ungetc(c, tok->fp); |
| 419 | } |
| 420 | |
| 421 | /* Read a line of input from TOK. Determine encoding |
| 422 | if necessary. */ |
| 423 | |
| 424 | static char * |
| 425 | decoding_fgets(char *s, int size, struct tok_state *tok) |
| 426 | { |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 427 | char *line = NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 428 | int warn = 0, badchar = 0; |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 429 | for (;;) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 430 | if (tok->decoding_state < 0) { |
| 431 | /* We already have a codec associated with |
| 432 | this input. */ |
| 433 | line = fp_readl(s, size, tok); |
| 434 | break; |
| 435 | } else if (tok->decoding_state > 0) { |
| 436 | /* We want a 'raw' read. */ |
| 437 | line = Py_UniversalNewlineFgets(s, size, |
| 438 | tok->fp, NULL); |
| 439 | warn = 1; |
| 440 | break; |
| 441 | } else { |
| 442 | /* We have not yet determined the encoding. |
| 443 | If an encoding is found, use the file-pointer |
| 444 | reader functions from now on. */ |
| 445 | if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok)) |
| 446 | return error_ret(tok); |
| 447 | assert(tok->decoding_state != 0); |
| 448 | } |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 449 | } |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 450 | if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) { |
| 451 | if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) { |
| 452 | return error_ret(tok); |
| 453 | } |
| 454 | } |
| 455 | #ifndef PGEN |
| 456 | if (warn && line && !tok->issued_encoding_warning && !tok->encoding) { |
| 457 | unsigned char *c; |
Jack Jansen | cf0a2cf | 2002-08-05 14:14:05 +0000 | [diff] [blame] | 458 | for (c = (unsigned char *)line; *c; c++) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 459 | if (*c > 127) { |
| 460 | badchar = *c; |
| 461 | break; |
| 462 | } |
| 463 | } |
| 464 | if (badchar) { |
Marc-André Lemburg | 1fb1400 | 2003-02-17 18:31:57 +0000 | [diff] [blame] | 465 | char buf[500]; |
Martin v. Löwis | 725bb23 | 2002-08-05 01:49:16 +0000 | [diff] [blame] | 466 | /* Need to add 1 to the line number, since this line |
| 467 | has not been counted, yet. */ |
Marc-André Lemburg | 1fb1400 | 2003-02-17 18:31:57 +0000 | [diff] [blame] | 468 | sprintf(buf, |
| 469 | "Non-ASCII character '\\x%.2x' " |
| 470 | "in file %.200s on line %i, " |
| 471 | "but no encoding declared; " |
| 472 | "see http://www.python.org/peps/pep-0263.html for details", |
| 473 | badchar, tok->filename, tok->lineno + 1); |
| 474 | /* We don't use PyErr_WarnExplicit() here because |
| 475 | printing the line in question to e.g. a log file |
| 476 | could result in sensitive information being |
| 477 | exposed. */ |
| 478 | PyErr_Warn(PyExc_DeprecationWarning, buf); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 479 | tok->issued_encoding_warning = 1; |
| 480 | } |
| 481 | #endif |
| 482 | return line; |
| 483 | } |
| 484 | |
| 485 | static int |
| 486 | decoding_feof(struct tok_state *tok) |
| 487 | { |
| 488 | if (tok->decoding_state >= 0) { |
| 489 | return feof(tok->fp); |
| 490 | } else { |
| 491 | PyObject* buf = tok->decoding_buffer; |
| 492 | if (buf == NULL) { |
Guido van Rossum | 84b2bed | 2002-08-16 17:01:09 +0000 | [diff] [blame] | 493 | PyObject *args = PyTuple_New(0); |
| 494 | if (args == NULL) { |
| 495 | error_ret(tok); |
| 496 | return 1; |
| 497 | } |
| 498 | buf = PyObject_Call(tok->decoding_readline, |
| 499 | args, NULL); |
| 500 | Py_DECREF(args); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 501 | if (buf == NULL) { |
| 502 | error_ret(tok); |
| 503 | return 1; |
| 504 | } else { |
| 505 | tok->decoding_buffer = buf; |
| 506 | } |
| 507 | } |
| 508 | return PyObject_Length(buf) == 0; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /* Fetch a byte from TOK, using the string buffer. */ |
| 513 | |
| 514 | static int buf_getc(struct tok_state *tok) { |
Just van Rossum | f032f86 | 2003-02-09 20:38:48 +0000 | [diff] [blame] | 515 | return Py_CHARMASK(*tok->str++); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* Unfetch a byte from TOK, using the string buffer. */ |
| 519 | |
| 520 | static void buf_ungetc(int c, struct tok_state *tok) { |
| 521 | tok->str--; |
Just van Rossum | f032f86 | 2003-02-09 20:38:48 +0000 | [diff] [blame] | 522 | assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* Set the readline function for TOK to ENC. For the string-based |
| 526 | tokenizer, this means to just record the encoding. */ |
| 527 | |
| 528 | static int buf_setreadl(struct tok_state *tok, const char* enc) { |
| 529 | tok->enc = enc; |
| 530 | return 1; |
| 531 | } |
| 532 | |
| 533 | /* Return a UTF-8 encoding Python string object from the |
| 534 | C byte string STR, which is encoded with ENC. */ |
| 535 | |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 536 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 537 | static PyObject * |
| 538 | translate_into_utf8(const char* str, const char* enc) { |
| 539 | PyObject *utf8; |
| 540 | PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL); |
| 541 | if (buf == NULL) |
| 542 | return NULL; |
| 543 | utf8 = PyUnicode_AsUTF8String(buf); |
| 544 | Py_DECREF(buf); |
| 545 | return utf8; |
| 546 | } |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 547 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 548 | |
| 549 | /* Decode a byte string STR for use as the buffer of TOK. |
| 550 | Look for encoding declarations inside STR, and record them |
| 551 | inside TOK. */ |
| 552 | |
| 553 | static const char * |
| 554 | decode_str(const char *str, struct tok_state *tok) |
| 555 | { |
| 556 | PyObject* utf8 = NULL; |
| 557 | const char *s; |
| 558 | int lineno = 0; |
| 559 | tok->enc = NULL; |
| 560 | tok->str = str; |
| 561 | if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok)) |
| 562 | return NULL; |
| 563 | str = tok->str; /* string after BOM if any */ |
Tim Peters | 2c3f9c6 | 2002-08-04 17:58:34 +0000 | [diff] [blame] | 564 | assert(str); |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 565 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 566 | if (tok->enc != NULL) { |
| 567 | utf8 = translate_into_utf8(str, tok->enc); |
| 568 | if (utf8 == NULL) |
| 569 | return NULL; |
| 570 | str = PyString_AsString(utf8); |
| 571 | } |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 572 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 573 | for (s = str;; s++) { |
| 574 | if (*s == '\0') break; |
| 575 | else if (*s == '\n') { |
| 576 | lineno++; |
| 577 | if (lineno == 2) break; |
| 578 | } |
| 579 | } |
| 580 | tok->enc = NULL; |
| 581 | if (!check_coding_spec(str, s - str, tok, buf_setreadl)) |
| 582 | return NULL; |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 583 | #ifdef Py_USING_UNICODE |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 584 | if (tok->enc != NULL) { |
| 585 | assert(utf8 == NULL); |
| 586 | utf8 = translate_into_utf8(str, tok->enc); |
| 587 | if (utf8 == NULL) |
| 588 | return NULL; |
| 589 | str = PyString_AsString(utf8); |
| 590 | } |
Martin v. Löwis | 019934b | 2002-08-07 12:33:18 +0000 | [diff] [blame] | 591 | #endif |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 592 | assert(tok->decoding_buffer == NULL); |
| 593 | tok->decoding_buffer = utf8; /* CAUTION */ |
| 594 | return str; |
| 595 | } |
| 596 | |
| 597 | #endif /* PGEN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 598 | |
| 599 | /* Set up tokenizer for string */ |
| 600 | |
| 601 | struct tok_state * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 602 | PyTokenizer_FromString(const char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 603 | { |
| 604 | struct tok_state *tok = tok_new(); |
| 605 | if (tok == NULL) |
| 606 | return NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 607 | str = (char *)decode_str(str, tok); |
| 608 | if (str == NULL) |
| 609 | return NULL; |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 610 | /* XXX: constify members. */ |
| 611 | tok->buf = tok->cur = tok->end = tok->inp = (char*)str; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 612 | return tok; |
| 613 | } |
| 614 | |
| 615 | |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 616 | /* Set up tokenizer for file */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 617 | |
| 618 | struct tok_state * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 619 | PyTokenizer_FromFile(FILE *fp, char *ps1, char *ps2) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 620 | { |
| 621 | struct tok_state *tok = tok_new(); |
| 622 | if (tok == NULL) |
| 623 | return NULL; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 624 | if ((tok->buf = PyMem_NEW(char, BUFSIZ)) == NULL) { |
| 625 | PyMem_DEL(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 626 | return NULL; |
| 627 | } |
| 628 | tok->cur = tok->inp = tok->buf; |
| 629 | tok->end = tok->buf + BUFSIZ; |
| 630 | tok->fp = fp; |
| 631 | tok->prompt = ps1; |
| 632 | tok->nextprompt = ps2; |
| 633 | return tok; |
| 634 | } |
| 635 | |
| 636 | |
| 637 | /* Free a tok_state structure */ |
| 638 | |
| 639 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 640 | PyTokenizer_Free(struct tok_state *tok) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 641 | { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 642 | if (tok->encoding != NULL) |
| 643 | PyMem_DEL(tok->encoding); |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 644 | #ifndef PGEN |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 645 | Py_XDECREF(tok->decoding_readline); |
| 646 | Py_XDECREF(tok->decoding_buffer); |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 647 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 648 | if (tok->fp != NULL && tok->buf != NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 649 | PyMem_DEL(tok->buf); |
| 650 | PyMem_DEL(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | |
| 654 | /* Get next char, updating state; error code goes into tok->done */ |
| 655 | |
| 656 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 657 | tok_nextc(register struct tok_state *tok) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 659 | for (;;) { |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 660 | if (tok->cur != tok->inp) { |
Guido van Rossum | cf57d8b | 1998-01-19 22:07:46 +0000 | [diff] [blame] | 661 | return Py_CHARMASK(*tok->cur++); /* Fast path */ |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 662 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 663 | if (tok->done != E_OK) |
| 664 | return EOF; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 665 | if (tok->fp == NULL) { |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 666 | char *end = strchr(tok->inp, '\n'); |
| 667 | if (end != NULL) |
| 668 | end++; |
| 669 | else { |
| 670 | end = strchr(tok->inp, '\0'); |
| 671 | if (end == tok->inp) { |
| 672 | tok->done = E_EOF; |
| 673 | return EOF; |
| 674 | } |
| 675 | } |
| 676 | if (tok->start == NULL) |
| 677 | tok->buf = tok->cur; |
| 678 | tok->lineno++; |
| 679 | tok->inp = end; |
Guido van Rossum | cf57d8b | 1998-01-19 22:07:46 +0000 | [diff] [blame] | 680 | return Py_CHARMASK(*tok->cur++); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 681 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 682 | if (tok->prompt != NULL) { |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 683 | char *new = PyOS_Readline(stdin, stdout, tok->prompt); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 684 | if (tok->nextprompt != NULL) |
| 685 | tok->prompt = tok->nextprompt; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 686 | if (new == NULL) |
| 687 | tok->done = E_INTR; |
| 688 | else if (*new == '\0') { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 689 | PyMem_FREE(new); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 690 | tok->done = E_EOF; |
| 691 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 692 | else if (tok->start != NULL) { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 693 | size_t start = tok->start - tok->buf; |
| 694 | size_t oldlen = tok->cur - tok->buf; |
| 695 | size_t newlen = oldlen + strlen(new); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 696 | char *buf = tok->buf; |
| 697 | PyMem_RESIZE(buf, char, newlen+1); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 698 | tok->lineno++; |
| 699 | if (buf == NULL) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 700 | PyMem_DEL(tok->buf); |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 701 | tok->buf = NULL; |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 702 | PyMem_FREE(new); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 703 | tok->done = E_NOMEM; |
| 704 | return EOF; |
| 705 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 706 | tok->buf = buf; |
| 707 | tok->cur = tok->buf + oldlen; |
| 708 | strcpy(tok->buf + oldlen, new); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 709 | PyMem_FREE(new); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 710 | tok->inp = tok->buf + newlen; |
| 711 | tok->end = tok->inp + 1; |
| 712 | tok->start = tok->buf + start; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 713 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 714 | else { |
| 715 | tok->lineno++; |
| 716 | if (tok->buf != NULL) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 717 | PyMem_DEL(tok->buf); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 718 | tok->buf = new; |
| 719 | tok->cur = tok->buf; |
| 720 | tok->inp = strchr(tok->buf, '\0'); |
| 721 | tok->end = tok->inp + 1; |
| 722 | } |
| 723 | } |
| 724 | else { |
| 725 | int done = 0; |
| 726 | int cur = 0; |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 727 | char *pt; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 728 | if (tok->start == NULL) { |
| 729 | if (tok->buf == NULL) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 730 | tok->buf = PyMem_NEW(char, BUFSIZ); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 731 | if (tok->buf == NULL) { |
| 732 | tok->done = E_NOMEM; |
| 733 | return EOF; |
| 734 | } |
| 735 | tok->end = tok->buf + BUFSIZ; |
| 736 | } |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 737 | if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf), |
| 738 | tok) == NULL) { |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 739 | tok->done = E_EOF; |
| 740 | done = 1; |
| 741 | } |
| 742 | else { |
| 743 | tok->done = E_OK; |
| 744 | tok->inp = strchr(tok->buf, '\0'); |
| 745 | done = tok->inp[-1] == '\n'; |
| 746 | } |
| 747 | } |
| 748 | else { |
| 749 | cur = tok->cur - tok->buf; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 750 | if (decoding_feof(tok)) { |
Guido van Rossum | 78c0535 | 1995-01-17 16:12:13 +0000 | [diff] [blame] | 751 | tok->done = E_EOF; |
| 752 | done = 1; |
| 753 | } |
| 754 | else |
| 755 | tok->done = E_OK; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 756 | } |
| 757 | tok->lineno++; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 758 | /* Read until '\n' or EOF */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 759 | while (!done) { |
| 760 | int curstart = tok->start == NULL ? -1 : |
| 761 | tok->start - tok->buf; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 762 | int curvalid = tok->inp - tok->buf; |
Guido van Rossum | 3f6bb86 | 1995-09-21 20:36:34 +0000 | [diff] [blame] | 763 | int newsize = curvalid + BUFSIZ; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 764 | char *newbuf = tok->buf; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 765 | PyMem_RESIZE(newbuf, char, newsize); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 766 | if (newbuf == NULL) { |
| 767 | tok->done = E_NOMEM; |
| 768 | tok->cur = tok->inp; |
| 769 | return EOF; |
| 770 | } |
| 771 | tok->buf = newbuf; |
| 772 | tok->inp = tok->buf + curvalid; |
| 773 | tok->end = tok->buf + newsize; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 774 | tok->start = curstart < 0 ? NULL : |
| 775 | tok->buf + curstart; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 776 | if (decoding_fgets(tok->inp, |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 777 | (int)(tok->end - tok->inp), |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 778 | tok) == NULL) { |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 779 | /* Last line does not end in \n, |
| 780 | fake one */ |
| 781 | strcpy(tok->inp, "\n"); |
| 782 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 783 | tok->inp = strchr(tok->inp, '\0'); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 784 | done = tok->inp[-1] == '\n'; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 785 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 786 | tok->cur = tok->buf + cur; |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 787 | /* replace "\r\n" with "\n" */ |
Guido van Rossum | 2d45be1 | 1997-04-11 19:16:25 +0000 | [diff] [blame] | 788 | /* For Mac we leave the \r, giving a syntax error */ |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 789 | pt = tok->inp - 2; |
| 790 | if (pt >= tok->buf && *pt == '\r') { |
| 791 | *pt++ = '\n'; |
| 792 | *pt = '\0'; |
| 793 | tok->inp = pt; |
| 794 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 795 | } |
| 796 | if (tok->done != E_OK) { |
| 797 | if (tok->prompt != NULL) |
Guido van Rossum | 6e73bf4 | 1998-08-25 18:13:04 +0000 | [diff] [blame] | 798 | PySys_WriteStderr("\n"); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 799 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 800 | return EOF; |
| 801 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 802 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 803 | /*NOTREACHED*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | |
| 807 | /* Back-up one character */ |
| 808 | |
| 809 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 810 | tok_backup(register struct tok_state *tok, register int c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 811 | { |
| 812 | if (c != EOF) { |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 813 | if (--tok->cur < tok->buf) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 814 | Py_FatalError("tok_backup: begin of buffer"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 815 | if (*tok->cur != c) |
| 816 | *tok->cur = c; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | |
| 821 | /* Return the token corresponding to a single character */ |
| 822 | |
| 823 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 824 | PyToken_OneChar(int c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 825 | { |
| 826 | switch (c) { |
| 827 | case '(': return LPAR; |
| 828 | case ')': return RPAR; |
| 829 | case '[': return LSQB; |
| 830 | case ']': return RSQB; |
| 831 | case ':': return COLON; |
| 832 | case ',': return COMMA; |
| 833 | case ';': return SEMI; |
| 834 | case '+': return PLUS; |
| 835 | case '-': return MINUS; |
| 836 | case '*': return STAR; |
| 837 | case '/': return SLASH; |
| 838 | case '|': return VBAR; |
| 839 | case '&': return AMPER; |
| 840 | case '<': return LESS; |
| 841 | case '>': return GREATER; |
| 842 | case '=': return EQUAL; |
| 843 | case '.': return DOT; |
| 844 | case '%': return PERCENT; |
| 845 | case '`': return BACKQUOTE; |
| 846 | case '{': return LBRACE; |
| 847 | case '}': return RBRACE; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 848 | case '^': return CIRCUMFLEX; |
| 849 | case '~': return TILDE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 850 | default: return OP; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 855 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 856 | PyToken_TwoChars(int c1, int c2) |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 857 | { |
| 858 | switch (c1) { |
| 859 | case '=': |
| 860 | switch (c2) { |
| 861 | case '=': return EQEQUAL; |
| 862 | } |
| 863 | break; |
| 864 | case '!': |
| 865 | switch (c2) { |
| 866 | case '=': return NOTEQUAL; |
| 867 | } |
| 868 | break; |
| 869 | case '<': |
| 870 | switch (c2) { |
| 871 | case '>': return NOTEQUAL; |
| 872 | case '=': return LESSEQUAL; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 873 | case '<': return LEFTSHIFT; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 874 | } |
| 875 | break; |
| 876 | case '>': |
| 877 | switch (c2) { |
| 878 | case '=': return GREATEREQUAL; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 879 | case '>': return RIGHTSHIFT; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 880 | } |
| 881 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 882 | case '+': |
| 883 | switch (c2) { |
| 884 | case '=': return PLUSEQUAL; |
| 885 | } |
| 886 | break; |
| 887 | case '-': |
| 888 | switch (c2) { |
| 889 | case '=': return MINEQUAL; |
| 890 | } |
| 891 | break; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 892 | case '*': |
| 893 | switch (c2) { |
| 894 | case '*': return DOUBLESTAR; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 895 | case '=': return STAREQUAL; |
| 896 | } |
| 897 | break; |
| 898 | case '/': |
| 899 | switch (c2) { |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 900 | case '/': return DOUBLESLASH; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 901 | case '=': return SLASHEQUAL; |
| 902 | } |
| 903 | break; |
| 904 | case '|': |
| 905 | switch (c2) { |
| 906 | case '=': return VBAREQUAL; |
| 907 | } |
| 908 | break; |
| 909 | case '%': |
| 910 | switch (c2) { |
| 911 | case '=': return PERCENTEQUAL; |
| 912 | } |
| 913 | break; |
| 914 | case '&': |
| 915 | switch (c2) { |
| 916 | case '=': return AMPEREQUAL; |
| 917 | } |
| 918 | break; |
| 919 | case '^': |
| 920 | switch (c2) { |
| 921 | case '=': return CIRCUMFLEXEQUAL; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 922 | } |
| 923 | break; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 924 | } |
| 925 | return OP; |
| 926 | } |
| 927 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 928 | int |
| 929 | PyToken_ThreeChars(int c1, int c2, int c3) |
| 930 | { |
| 931 | switch (c1) { |
| 932 | case '<': |
| 933 | switch (c2) { |
| 934 | case '<': |
| 935 | switch (c3) { |
| 936 | case '=': |
| 937 | return LEFTSHIFTEQUAL; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 938 | } |
| 939 | break; |
| 940 | } |
| 941 | break; |
| 942 | case '>': |
| 943 | switch (c2) { |
| 944 | case '>': |
| 945 | switch (c3) { |
| 946 | case '=': |
| 947 | return RIGHTSHIFTEQUAL; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 948 | } |
| 949 | break; |
| 950 | } |
| 951 | break; |
| 952 | case '*': |
| 953 | switch (c2) { |
| 954 | case '*': |
| 955 | switch (c3) { |
| 956 | case '=': |
| 957 | return DOUBLESTAREQUAL; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 958 | } |
| 959 | break; |
| 960 | } |
| 961 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 962 | case '/': |
| 963 | switch (c2) { |
| 964 | case '/': |
| 965 | switch (c3) { |
| 966 | case '=': |
| 967 | return DOUBLESLASHEQUAL; |
| 968 | } |
| 969 | break; |
| 970 | } |
| 971 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 972 | } |
| 973 | return OP; |
| 974 | } |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 975 | |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 976 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 977 | indenterror(struct tok_state *tok) |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 978 | { |
| 979 | if (tok->alterror) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 980 | tok->done = E_TABSPACE; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 981 | tok->cur = tok->inp; |
| 982 | return 1; |
| 983 | } |
| 984 | if (tok->altwarning) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 985 | PySys_WriteStderr("%s: inconsistent use of tabs and spaces " |
| 986 | "in indentation\n", tok->filename); |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 987 | tok->altwarning = 0; |
| 988 | } |
| 989 | return 0; |
| 990 | } |
| 991 | |
| 992 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 993 | /* Get next token, after space stripping etc. */ |
| 994 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 995 | static int |
| 996 | tok_get(register struct tok_state *tok, char **p_start, char **p_end) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 997 | { |
| 998 | register int c; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 999 | int blankline; |
| 1000 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1001 | *p_start = *p_end = NULL; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1002 | nextline: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1003 | tok->start = NULL; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1004 | blankline = 0; |
| 1005 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1006 | /* Get indentation level */ |
| 1007 | if (tok->atbol) { |
| 1008 | register int col = 0; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1009 | register int altcol = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1010 | tok->atbol = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1011 | for (;;) { |
| 1012 | c = tok_nextc(tok); |
| 1013 | if (c == ' ') |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1014 | col++, altcol++; |
| 1015 | else if (c == '\t') { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1016 | col = (col/tok->tabsize + 1) * tok->tabsize; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1017 | altcol = (altcol/tok->alttabsize + 1) |
| 1018 | * tok->alttabsize; |
| 1019 | } |
Guido van Rossum | 94d32b1 | 1995-07-07 22:27:27 +0000 | [diff] [blame] | 1020 | else if (c == '\014') /* Control-L (formfeed) */ |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1021 | col = altcol = 0; /* For Emacs users */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1022 | else |
| 1023 | break; |
| 1024 | } |
| 1025 | tok_backup(tok, c); |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1026 | if (c == '#' || c == '\n') { |
| 1027 | /* Lines with only whitespace and/or comments |
| 1028 | shouldn't affect the indentation and are |
| 1029 | not passed to the parser as NEWLINE tokens, |
| 1030 | except *totally* empty lines in interactive |
| 1031 | mode, which signal the end of a command group. */ |
| 1032 | if (col == 0 && c == '\n' && tok->prompt != NULL) |
| 1033 | blankline = 0; /* Let it through */ |
| 1034 | else |
| 1035 | blankline = 1; /* Ignore completely */ |
| 1036 | /* We can't jump back right here since we still |
| 1037 | may need to skip to the end of a comment */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1038 | } |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 1039 | if (!blankline && tok->level == 0) { |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1040 | if (col == tok->indstack[tok->indent]) { |
| 1041 | /* No change */ |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1042 | if (altcol != tok->altindstack[tok->indent]) { |
| 1043 | if (indenterror(tok)) |
| 1044 | return ERRORTOKEN; |
| 1045 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1046 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1047 | else if (col > tok->indstack[tok->indent]) { |
| 1048 | /* Indent -- always one */ |
| 1049 | if (tok->indent+1 >= MAXINDENT) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1050 | tok->done = E_TOODEEP; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1051 | tok->cur = tok->inp; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1052 | return ERRORTOKEN; |
| 1053 | } |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1054 | if (altcol <= tok->altindstack[tok->indent]) { |
| 1055 | if (indenterror(tok)) |
| 1056 | return ERRORTOKEN; |
| 1057 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1058 | tok->pendin++; |
| 1059 | tok->indstack[++tok->indent] = col; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1060 | tok->altindstack[tok->indent] = altcol; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1061 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1062 | else /* col < tok->indstack[tok->indent] */ { |
| 1063 | /* Dedent -- any number, must be consistent */ |
| 1064 | while (tok->indent > 0 && |
| 1065 | col < tok->indstack[tok->indent]) { |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1066 | tok->pendin--; |
Guido van Rossum | 54758fa | 1998-02-16 22:18:00 +0000 | [diff] [blame] | 1067 | tok->indent--; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1068 | } |
| 1069 | if (col != tok->indstack[tok->indent]) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1070 | tok->done = E_DEDENT; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1071 | tok->cur = tok->inp; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1072 | return ERRORTOKEN; |
| 1073 | } |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1074 | if (altcol != tok->altindstack[tok->indent]) { |
| 1075 | if (indenterror(tok)) |
| 1076 | return ERRORTOKEN; |
| 1077 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1082 | tok->start = tok->cur; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1083 | |
| 1084 | /* Return pending indents/dedents */ |
| 1085 | if (tok->pendin != 0) { |
| 1086 | if (tok->pendin < 0) { |
| 1087 | tok->pendin++; |
| 1088 | return DEDENT; |
| 1089 | } |
| 1090 | else { |
| 1091 | tok->pendin--; |
| 1092 | return INDENT; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | again: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1097 | tok->start = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1098 | /* Skip spaces */ |
| 1099 | do { |
| 1100 | c = tok_nextc(tok); |
Guido van Rossum | 94d32b1 | 1995-07-07 22:27:27 +0000 | [diff] [blame] | 1101 | } while (c == ' ' || c == '\t' || c == '\014'); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1102 | |
| 1103 | /* Set start of current token */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1104 | tok->start = tok->cur - 1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1105 | |
Guido van Rossum | ab5ca15 | 2000-03-31 00:52:27 +0000 | [diff] [blame] | 1106 | /* Skip comment, while looking for tab-setting magic */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1107 | if (c == '#') { |
Guido van Rossum | ab5ca15 | 2000-03-31 00:52:27 +0000 | [diff] [blame] | 1108 | static char *tabforms[] = { |
| 1109 | "tab-width:", /* Emacs */ |
| 1110 | ":tabstop=", /* vim, full form */ |
| 1111 | ":ts=", /* vim, abbreviated form */ |
| 1112 | "set tabsize=", /* will vi never die? */ |
| 1113 | /* more templates can be added here to support other editors */ |
| 1114 | }; |
| 1115 | char cbuf[80]; |
| 1116 | char *tp, **cp; |
| 1117 | tp = cbuf; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1118 | do { |
Guido van Rossum | ab5ca15 | 2000-03-31 00:52:27 +0000 | [diff] [blame] | 1119 | *tp++ = c = tok_nextc(tok); |
| 1120 | } while (c != EOF && c != '\n' && |
| 1121 | tp - cbuf + 1 < sizeof(cbuf)); |
| 1122 | *tp = '\0'; |
| 1123 | for (cp = tabforms; |
| 1124 | cp < tabforms + sizeof(tabforms)/sizeof(tabforms[0]); |
| 1125 | cp++) { |
| 1126 | if ((tp = strstr(cbuf, *cp))) { |
| 1127 | int newsize = atoi(tp + strlen(*cp)); |
| 1128 | |
| 1129 | if (newsize >= 1 && newsize <= 40) { |
| 1130 | tok->tabsize = newsize; |
Guido van Rossum | 6c981ad | 2000-04-03 23:02:17 +0000 | [diff] [blame] | 1131 | if (Py_VerboseFlag) |
| 1132 | PySys_WriteStderr( |
Guido van Rossum | ab5ca15 | 2000-03-31 00:52:27 +0000 | [diff] [blame] | 1133 | "Tab size set to %d\n", |
| 1134 | newsize); |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | while (c != EOF && c != '\n') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1139 | c = tok_nextc(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | /* Check for EOF and errors now */ |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1143 | if (c == EOF) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1144 | return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1145 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1146 | |
| 1147 | /* Identifier (most frequent token!) */ |
| 1148 | if (isalpha(c) || c == '_') { |
Guido van Rossum | 86016cb | 2000-03-10 22:56:54 +0000 | [diff] [blame] | 1149 | /* Process r"", u"" and ur"" */ |
Guido van Rossum | 5026cb4 | 1997-04-25 17:32:00 +0000 | [diff] [blame] | 1150 | switch (c) { |
| 1151 | case 'r': |
| 1152 | case 'R': |
| 1153 | c = tok_nextc(tok); |
| 1154 | if (c == '"' || c == '\'') |
| 1155 | goto letter_quote; |
Guido van Rossum | 86016cb | 2000-03-10 22:56:54 +0000 | [diff] [blame] | 1156 | break; |
| 1157 | case 'u': |
| 1158 | case 'U': |
| 1159 | c = tok_nextc(tok); |
| 1160 | if (c == 'r' || c == 'R') |
| 1161 | c = tok_nextc(tok); |
| 1162 | if (c == '"' || c == '\'') |
| 1163 | goto letter_quote; |
| 1164 | break; |
Guido van Rossum | 5026cb4 | 1997-04-25 17:32:00 +0000 | [diff] [blame] | 1165 | } |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 1166 | while (isalnum(c) || c == '_') { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1167 | c = tok_nextc(tok); |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 1168 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1169 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1170 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1171 | *p_end = tok->cur; |
| 1172 | return NAME; |
| 1173 | } |
| 1174 | |
| 1175 | /* Newline */ |
| 1176 | if (c == '\n') { |
| 1177 | tok->atbol = 1; |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 1178 | if (blankline || tok->level > 0) |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1179 | goto nextline; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1180 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1181 | *p_end = tok->cur - 1; /* Leave '\n' out of the string */ |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 1182 | tok->cont_line = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1183 | return NEWLINE; |
| 1184 | } |
| 1185 | |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1186 | /* Period or number starting with period? */ |
| 1187 | if (c == '.') { |
| 1188 | c = tok_nextc(tok); |
| 1189 | if (isdigit(c)) { |
| 1190 | goto fraction; |
| 1191 | } |
| 1192 | else { |
| 1193 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1194 | *p_start = tok->start; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1195 | *p_end = tok->cur; |
| 1196 | return DOT; |
| 1197 | } |
| 1198 | } |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1199 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1200 | /* Number */ |
| 1201 | if (isdigit(c)) { |
| 1202 | if (c == '0') { |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 1203 | /* Hex or octal -- maybe. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1204 | c = tok_nextc(tok); |
| 1205 | if (c == '.') |
| 1206 | goto fraction; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1207 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | faa436c | 1996-01-26 18:59:07 +0000 | [diff] [blame] | 1208 | if (c == 'j' || c == 'J') |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1209 | goto imaginary; |
| 1210 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1211 | if (c == 'x' || c == 'X') { |
| 1212 | /* Hex */ |
| 1213 | do { |
| 1214 | c = tok_nextc(tok); |
| 1215 | } while (isxdigit(c)); |
| 1216 | } |
| 1217 | else { |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 1218 | int found_decimal = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1219 | /* Octal; c is first char of it */ |
| 1220 | /* There's no 'isoctdigit' macro, sigh */ |
| 1221 | while ('0' <= c && c < '8') { |
| 1222 | c = tok_nextc(tok); |
| 1223 | } |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 1224 | if (isdigit(c)) { |
| 1225 | found_decimal = 1; |
| 1226 | do { |
| 1227 | c = tok_nextc(tok); |
| 1228 | } while (isdigit(c)); |
| 1229 | } |
| 1230 | if (c == '.') |
| 1231 | goto fraction; |
| 1232 | else if (c == 'e' || c == 'E') |
| 1233 | goto exponent; |
| 1234 | #ifndef WITHOUT_COMPLEX |
| 1235 | else if (c == 'j' || c == 'J') |
| 1236 | goto imaginary; |
| 1237 | #endif |
| 1238 | else if (found_decimal) { |
| 1239 | tok->done = E_TOKEN; |
| 1240 | tok_backup(tok, c); |
| 1241 | return ERRORTOKEN; |
| 1242 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1243 | } |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1244 | if (c == 'l' || c == 'L') |
| 1245 | c = tok_nextc(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1246 | } |
| 1247 | else { |
| 1248 | /* Decimal */ |
| 1249 | do { |
| 1250 | c = tok_nextc(tok); |
| 1251 | } while (isdigit(c)); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1252 | if (c == 'l' || c == 'L') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1253 | c = tok_nextc(tok); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1254 | else { |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 1255 | /* Accept floating point numbers. */ |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1256 | if (c == '.') { |
| 1257 | fraction: |
| 1258 | /* Fraction */ |
| 1259 | do { |
| 1260 | c = tok_nextc(tok); |
| 1261 | } while (isdigit(c)); |
| 1262 | } |
| 1263 | if (c == 'e' || c == 'E') { |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 1264 | exponent: |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1265 | /* Exponent part */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1266 | c = tok_nextc(tok); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1267 | if (c == '+' || c == '-') |
| 1268 | c = tok_nextc(tok); |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 1269 | if (!isdigit(c)) { |
| 1270 | tok->done = E_TOKEN; |
| 1271 | tok_backup(tok, c); |
| 1272 | return ERRORTOKEN; |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1273 | } |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 1274 | do { |
| 1275 | c = tok_nextc(tok); |
| 1276 | } while (isdigit(c)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1277 | } |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1278 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | faa436c | 1996-01-26 18:59:07 +0000 | [diff] [blame] | 1279 | if (c == 'j' || c == 'J') |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1280 | /* Imaginary part */ |
| 1281 | imaginary: |
| 1282 | c = tok_nextc(tok); |
| 1283 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1287 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1288 | *p_end = tok->cur; |
| 1289 | return NUMBER; |
| 1290 | } |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 1291 | |
| 1292 | letter_quote: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1293 | /* String */ |
| 1294 | if (c == '\'' || c == '"') { |
Guido van Rossum | 3568524 | 1998-02-16 15:42:50 +0000 | [diff] [blame] | 1295 | int quote2 = tok->cur - tok->start + 1; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1296 | int quote = c; |
| 1297 | int triple = 0; |
| 1298 | int tripcount = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1299 | for (;;) { |
| 1300 | c = tok_nextc(tok); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1301 | if (c == '\n') { |
| 1302 | if (!triple) { |
Skip Montanaro | 118ec70 | 2002-08-15 01:20:16 +0000 | [diff] [blame] | 1303 | tok->done = E_EOLS; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1304 | tok_backup(tok, c); |
| 1305 | return ERRORTOKEN; |
| 1306 | } |
| 1307 | tripcount = 0; |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 1308 | tok->cont_line = 1; /* multiline string. */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1309 | } |
| 1310 | else if (c == EOF) { |
Skip Montanaro | 118ec70 | 2002-08-15 01:20:16 +0000 | [diff] [blame] | 1311 | if (triple) |
| 1312 | tok->done = E_EOFS; |
| 1313 | else |
| 1314 | tok->done = E_EOLS; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1315 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1316 | return ERRORTOKEN; |
| 1317 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1318 | else if (c == quote) { |
| 1319 | tripcount++; |
Guido van Rossum | 3568524 | 1998-02-16 15:42:50 +0000 | [diff] [blame] | 1320 | if (tok->cur - tok->start == quote2) { |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1321 | c = tok_nextc(tok); |
| 1322 | if (c == quote) { |
| 1323 | triple = 1; |
| 1324 | tripcount = 0; |
| 1325 | continue; |
| 1326 | } |
| 1327 | tok_backup(tok, c); |
| 1328 | } |
| 1329 | if (!triple || tripcount == 3) |
| 1330 | break; |
| 1331 | } |
| 1332 | else if (c == '\\') { |
| 1333 | tripcount = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1334 | c = tok_nextc(tok); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1335 | if (c == EOF) { |
Skip Montanaro | 118ec70 | 2002-08-15 01:20:16 +0000 | [diff] [blame] | 1336 | tok->done = E_EOLS; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1337 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1338 | return ERRORTOKEN; |
| 1339 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1340 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1341 | else |
| 1342 | tripcount = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1343 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1344 | *p_start = tok->start; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1345 | *p_end = tok->cur; |
| 1346 | return STRING; |
| 1347 | } |
| 1348 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1349 | /* Line continuation */ |
| 1350 | if (c == '\\') { |
| 1351 | c = tok_nextc(tok); |
| 1352 | if (c != '\n') { |
| 1353 | tok->done = E_TOKEN; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1354 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1355 | return ERRORTOKEN; |
| 1356 | } |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 1357 | tok->cont_line = 1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1358 | goto again; /* Read next line */ |
| 1359 | } |
| 1360 | |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1361 | /* Check for two-character token */ |
| 1362 | { |
| 1363 | int c2 = tok_nextc(tok); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 1364 | int token = PyToken_TwoChars(c, c2); |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1365 | if (token != OP) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1366 | int c3 = tok_nextc(tok); |
| 1367 | int token3 = PyToken_ThreeChars(c, c2, c3); |
| 1368 | if (token3 != OP) { |
| 1369 | token = token3; |
| 1370 | } else { |
| 1371 | tok_backup(tok, c3); |
| 1372 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1373 | *p_start = tok->start; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1374 | *p_end = tok->cur; |
| 1375 | return token; |
| 1376 | } |
| 1377 | tok_backup(tok, c2); |
| 1378 | } |
| 1379 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1380 | /* Keep track of parentheses nesting level */ |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 1381 | switch (c) { |
| 1382 | case '(': |
| 1383 | case '[': |
| 1384 | case '{': |
| 1385 | tok->level++; |
| 1386 | break; |
| 1387 | case ')': |
| 1388 | case ']': |
| 1389 | case '}': |
| 1390 | tok->level--; |
| 1391 | break; |
| 1392 | } |
| 1393 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1394 | /* Punctuation character */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1395 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1396 | *p_end = tok->cur; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 1397 | return PyToken_OneChar(c); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1400 | int |
| 1401 | PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end) |
| 1402 | { |
| 1403 | int result = tok_get(tok, p_start, p_end); |
| 1404 | if (tok->decoding_erred) { |
| 1405 | result = ERRORTOKEN; |
| 1406 | tok->done = E_DECODE; |
| 1407 | } |
| 1408 | return result; |
| 1409 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1410 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 1411 | #ifdef Py_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1412 | |
| 1413 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 1414 | tok_dump(int type, char *start, char *end) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1415 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 1416 | printf("%s", _PyParser_TokenNames[type]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1417 | if (type == NAME || type == NUMBER || type == STRING || type == OP) |
| 1418 | printf("(%.*s)", (int)(end - start), start); |
| 1419 | } |
| 1420 | |
| 1421 | #endif |