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" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 15 | #include "bytesobject.h" |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 16 | #include "fileobject.h" |
| 17 | #include "codecs.h" |
| 18 | #include "abstract.h" |
| 19 | #endif /* PGEN */ |
| 20 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 21 | #define is_potential_identifier_start(c) (\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | (c >= 'a' && c <= 'z')\ |
| 23 | || (c >= 'A' && c <= 'Z')\ |
| 24 | || c == '_'\ |
| 25 | || (c >= 128)) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 26 | |
| 27 | #define is_potential_identifier_char(c) (\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 28 | (c >= 'a' && c <= 'z')\ |
| 29 | || (c >= 'A' && c <= 'Z')\ |
| 30 | || (c >= '0' && c <= '9')\ |
| 31 | || c == '_'\ |
| 32 | || (c >= 128)) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 33 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 34 | extern char *PyOS_Readline(FILE *, FILE *, const char *); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 35 | /* Return malloc'ed string including trailing \n; |
| 36 | empty malloc'ed string for EOF; |
| 37 | NULL if interrupted */ |
| 38 | |
Guido van Rossum | 4fe8729 | 1992-02-26 15:24:44 +0000 | [diff] [blame] | 39 | /* 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] | 40 | #define TABSIZE 8 |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 42 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 43 | static struct tok_state *tok_new(void); |
| 44 | static int tok_nextc(struct tok_state *tok); |
| 45 | static void tok_backup(struct tok_state *tok, int c); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 46 | |
Brett Cannon | d5ec98c | 2007-10-20 02:54:14 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | /* Token names */ |
| 49 | |
Benjamin Peterson | d084558 | 2012-10-24 08:21:52 -0700 | [diff] [blame] | 50 | const char *_PyParser_TokenNames[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | "ENDMARKER", |
| 52 | "NAME", |
| 53 | "NUMBER", |
| 54 | "STRING", |
| 55 | "NEWLINE", |
| 56 | "INDENT", |
| 57 | "DEDENT", |
| 58 | "LPAR", |
| 59 | "RPAR", |
| 60 | "LSQB", |
| 61 | "RSQB", |
| 62 | "COLON", |
| 63 | "COMMA", |
| 64 | "SEMI", |
| 65 | "PLUS", |
| 66 | "MINUS", |
| 67 | "STAR", |
| 68 | "SLASH", |
| 69 | "VBAR", |
| 70 | "AMPER", |
| 71 | "LESS", |
| 72 | "GREATER", |
| 73 | "EQUAL", |
| 74 | "DOT", |
| 75 | "PERCENT", |
| 76 | "LBRACE", |
| 77 | "RBRACE", |
| 78 | "EQEQUAL", |
| 79 | "NOTEQUAL", |
| 80 | "LESSEQUAL", |
| 81 | "GREATEREQUAL", |
| 82 | "TILDE", |
| 83 | "CIRCUMFLEX", |
| 84 | "LEFTSHIFT", |
| 85 | "RIGHTSHIFT", |
| 86 | "DOUBLESTAR", |
| 87 | "PLUSEQUAL", |
| 88 | "MINEQUAL", |
| 89 | "STAREQUAL", |
| 90 | "SLASHEQUAL", |
| 91 | "PERCENTEQUAL", |
| 92 | "AMPEREQUAL", |
| 93 | "VBAREQUAL", |
| 94 | "CIRCUMFLEXEQUAL", |
| 95 | "LEFTSHIFTEQUAL", |
| 96 | "RIGHTSHIFTEQUAL", |
| 97 | "DOUBLESTAREQUAL", |
| 98 | "DOUBLESLASH", |
| 99 | "DOUBLESLASHEQUAL", |
| 100 | "AT", |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 101 | "ATEQUAL", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 102 | "RARROW", |
| 103 | "ELLIPSIS", |
| 104 | /* This table must match the #defines in token.h! */ |
| 105 | "OP", |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 106 | "AWAIT", |
| 107 | "ASYNC", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | "<ERRORTOKEN>", |
| 109 | "<N_TOKENS>" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | |
| 113 | /* Create and initialize a new tok_state structure */ |
| 114 | |
| 115 | static struct tok_state * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 116 | tok_new(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | struct tok_state *tok = (struct tok_state *)PyMem_MALLOC( |
| 119 | sizeof(struct tok_state)); |
| 120 | if (tok == NULL) |
| 121 | return NULL; |
| 122 | tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL; |
| 123 | tok->done = E_OK; |
| 124 | tok->fp = NULL; |
| 125 | tok->input = NULL; |
| 126 | tok->tabsize = TABSIZE; |
| 127 | tok->indent = 0; |
| 128 | tok->indstack[0] = 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | tok->atbol = 1; |
| 131 | tok->pendin = 0; |
| 132 | tok->prompt = tok->nextprompt = NULL; |
| 133 | tok->lineno = 0; |
| 134 | tok->level = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | tok->altwarning = 1; |
| 136 | tok->alterror = 1; |
| 137 | tok->alttabsize = 1; |
| 138 | tok->altindstack[0] = 0; |
| 139 | tok->decoding_state = STATE_INIT; |
| 140 | tok->decoding_erred = 0; |
| 141 | tok->read_coding_spec = 0; |
| 142 | tok->enc = NULL; |
| 143 | tok->encoding = NULL; |
| 144 | tok->cont_line = 0; |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 145 | #ifndef PGEN |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 146 | tok->filename = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | tok->decoding_readline = NULL; |
| 148 | tok->decoding_buffer = NULL; |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 149 | #endif |
Yury Selivanov | 96ec934 | 2015-07-23 15:01:58 +0300 | [diff] [blame] | 150 | |
| 151 | tok->async_def = 0; |
| 152 | tok->async_def_indent = 0; |
| 153 | tok->async_def_nl = 0; |
| 154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | return tok; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 158 | static char * |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 159 | new_string(const char *s, Py_ssize_t len, struct tok_state *tok) |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 160 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | char* result = (char *)PyMem_MALLOC(len + 1); |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 162 | if (!result) { |
| 163 | tok->done = E_NOMEM; |
| 164 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | } |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 166 | memcpy(result, s, len); |
| 167 | result[len] = '\0'; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 168 | return result; |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 171 | #ifdef PGEN |
| 172 | |
| 173 | static char * |
| 174 | decoding_fgets(char *s, int size, struct tok_state *tok) |
| 175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | return fgets(s, size, tok->fp); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static int |
| 180 | decoding_feof(struct tok_state *tok) |
| 181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | return feof(tok->fp); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 185 | static char * |
| 186 | decode_str(const char *str, int exec_input, struct tok_state *tok) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 187 | { |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 188 | return new_string(str, strlen(str), tok); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | #else /* PGEN */ |
| 192 | |
| 193 | static char * |
| 194 | error_ret(struct tok_state *tok) /* XXX */ |
| 195 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | tok->decoding_erred = 1; |
| 197 | if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */ |
| 198 | PyMem_FREE(tok->buf); |
Serhiy Storchaka | 0d44111 | 2015-11-14 15:10:35 +0200 | [diff] [blame] | 199 | tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL; |
| 200 | tok->done = E_DECODE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 201 | return NULL; /* as if it were EOF */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 204 | |
| 205 | static char * |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | get_normal_name(char *s) /* for utf-8 and latin-1 */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 207 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | char buf[13]; |
| 209 | int i; |
| 210 | for (i = 0; i < 12; i++) { |
| 211 | int c = s[i]; |
| 212 | if (c == '\0') |
| 213 | break; |
| 214 | else if (c == '_') |
| 215 | buf[i] = '-'; |
| 216 | else |
| 217 | buf[i] = tolower(c); |
| 218 | } |
| 219 | buf[i] = '\0'; |
| 220 | if (strcmp(buf, "utf-8") == 0 || |
| 221 | strncmp(buf, "utf-8-", 6) == 0) |
| 222 | return "utf-8"; |
| 223 | else if (strcmp(buf, "latin-1") == 0 || |
| 224 | strcmp(buf, "iso-8859-1") == 0 || |
| 225 | strcmp(buf, "iso-latin-1") == 0 || |
| 226 | strncmp(buf, "latin-1-", 8) == 0 || |
| 227 | strncmp(buf, "iso-8859-1-", 11) == 0 || |
| 228 | strncmp(buf, "iso-latin-1-", 12) == 0) |
| 229 | return "iso-8859-1"; |
| 230 | else |
| 231 | return s; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* Return the coding spec in S, or NULL if none is found. */ |
| 235 | |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 236 | static int |
| 237 | get_coding_spec(const char *s, char **spec, Py_ssize_t size, struct tok_state *tok) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 238 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | Py_ssize_t i; |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 240 | *spec = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | /* Coding spec must be in a comment, and that comment must be |
| 242 | * the only statement on the source code line. */ |
| 243 | for (i = 0; i < size - 6; i++) { |
| 244 | if (s[i] == '#') |
| 245 | break; |
| 246 | if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014') |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 247 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | } |
| 249 | for (; i < size - 6; i++) { /* XXX inefficient search */ |
| 250 | const char* t = s + i; |
| 251 | if (strncmp(t, "coding", 6) == 0) { |
| 252 | const char* begin = NULL; |
| 253 | t += 6; |
| 254 | if (t[0] != ':' && t[0] != '=') |
| 255 | continue; |
| 256 | do { |
| 257 | t++; |
| 258 | } while (t[0] == '\x20' || t[0] == '\t'); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | begin = t; |
| 261 | while (Py_ISALNUM(t[0]) || |
| 262 | t[0] == '-' || t[0] == '_' || t[0] == '.') |
| 263 | t++; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | if (begin < t) { |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 266 | char* r = new_string(begin, t - begin, tok); |
Benjamin Peterson | 265fba4 | 2013-07-15 20:50:22 -0700 | [diff] [blame] | 267 | char* q; |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 268 | if (!r) |
| 269 | return 0; |
Benjamin Peterson | 265fba4 | 2013-07-15 20:50:22 -0700 | [diff] [blame] | 270 | q = get_normal_name(r); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | if (r != q) { |
| 272 | PyMem_FREE(r); |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 273 | r = new_string(q, strlen(q), tok); |
| 274 | if (!r) |
| 275 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | } |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 277 | *spec = r; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 281 | return 1; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* Check whether the line contains a coding spec. If it does, |
| 285 | invoke the set_readline function for the new encoding. |
| 286 | This function receives the tok_state and the new encoding. |
| 287 | Return 1 on success, 0 on failure. */ |
| 288 | |
| 289 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 290 | check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | int set_readline(struct tok_state *, const char *)) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 292 | { |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 293 | char *cs; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | int r = 1; |
Tim Peters | 17db21f | 2002-09-03 15:39:58 +0000 | [diff] [blame] | 295 | |
Serhiy Storchaka | 768c16c | 2014-01-09 18:36:09 +0200 | [diff] [blame] | 296 | if (tok->cont_line) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | /* It's a continuation line, so it can't be a coding spec. */ |
Serhiy Storchaka | 768c16c | 2014-01-09 18:36:09 +0200 | [diff] [blame] | 298 | tok->read_coding_spec = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | return 1; |
Serhiy Storchaka | 768c16c | 2014-01-09 18:36:09 +0200 | [diff] [blame] | 300 | } |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 301 | if (!get_coding_spec(line, &cs, size, tok)) |
| 302 | return 0; |
Serhiy Storchaka | 768c16c | 2014-01-09 18:36:09 +0200 | [diff] [blame] | 303 | if (!cs) { |
| 304 | Py_ssize_t i; |
| 305 | for (i = 0; i < size; i++) { |
| 306 | if (line[i] == '#' || line[i] == '\n' || line[i] == '\r') |
| 307 | break; |
| 308 | if (line[i] != ' ' && line[i] != '\t' && line[i] != '\014') { |
| 309 | /* Stop checking coding spec after a line containing |
| 310 | * anything except a comment. */ |
| 311 | tok->read_coding_spec = 1; |
| 312 | break; |
| 313 | } |
| 314 | } |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 315 | return 1; |
Serhiy Storchaka | 768c16c | 2014-01-09 18:36:09 +0200 | [diff] [blame] | 316 | } |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 317 | tok->read_coding_spec = 1; |
| 318 | if (tok->encoding == NULL) { |
| 319 | assert(tok->decoding_state == STATE_RAW); |
| 320 | if (strcmp(cs, "utf-8") == 0) { |
| 321 | tok->encoding = cs; |
| 322 | } else { |
| 323 | r = set_readline(tok, cs); |
| 324 | if (r) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | tok->encoding = cs; |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 326 | tok->decoding_state = STATE_NORMAL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 327 | } |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 328 | else { |
Serhiy Storchaka | 3af14aa | 2013-06-09 16:51:52 +0300 | [diff] [blame] | 329 | PyErr_Format(PyExc_SyntaxError, |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 330 | "encoding problem: %s", cs); |
| 331 | PyMem_FREE(cs); |
| 332 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | } |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 334 | } else { /* then, compare cs with BOM */ |
| 335 | r = (strcmp(tok->encoding, cs) == 0); |
| 336 | if (!r) |
| 337 | PyErr_Format(PyExc_SyntaxError, |
| 338 | "encoding problem: %s with BOM", cs); |
| 339 | PyMem_FREE(cs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | return r; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* See whether the file starts with a BOM. If it does, |
| 345 | invoke the set_readline function with the new encoding. |
| 346 | Return 1 on success, 0 on failure. */ |
| 347 | |
| 348 | static int |
| 349 | check_bom(int get_char(struct tok_state *), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 350 | void unget_char(int, struct tok_state *), |
| 351 | int set_readline(struct tok_state *, const char *), |
| 352 | struct tok_state *tok) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | int ch1, ch2, ch3; |
| 355 | ch1 = get_char(tok); |
| 356 | tok->decoding_state = STATE_RAW; |
| 357 | if (ch1 == EOF) { |
| 358 | return 1; |
| 359 | } else if (ch1 == 0xEF) { |
| 360 | ch2 = get_char(tok); |
| 361 | if (ch2 != 0xBB) { |
| 362 | unget_char(ch2, tok); |
| 363 | unget_char(ch1, tok); |
| 364 | return 1; |
| 365 | } |
| 366 | ch3 = get_char(tok); |
| 367 | if (ch3 != 0xBF) { |
| 368 | unget_char(ch3, tok); |
| 369 | unget_char(ch2, tok); |
| 370 | unget_char(ch1, tok); |
| 371 | return 1; |
| 372 | } |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 373 | #if 0 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 374 | /* Disable support for UTF-16 BOMs until a decision |
| 375 | is made whether this needs to be supported. */ |
| 376 | } else if (ch1 == 0xFE) { |
| 377 | ch2 = get_char(tok); |
| 378 | if (ch2 != 0xFF) { |
| 379 | unget_char(ch2, tok); |
| 380 | unget_char(ch1, tok); |
| 381 | return 1; |
| 382 | } |
| 383 | if (!set_readline(tok, "utf-16-be")) |
| 384 | return 0; |
| 385 | tok->decoding_state = STATE_NORMAL; |
| 386 | } else if (ch1 == 0xFF) { |
| 387 | ch2 = get_char(tok); |
| 388 | if (ch2 != 0xFE) { |
| 389 | unget_char(ch2, tok); |
| 390 | unget_char(ch1, tok); |
| 391 | return 1; |
| 392 | } |
| 393 | if (!set_readline(tok, "utf-16-le")) |
| 394 | return 0; |
| 395 | tok->decoding_state = STATE_NORMAL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 396 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | } else { |
| 398 | unget_char(ch1, tok); |
| 399 | return 1; |
| 400 | } |
| 401 | if (tok->encoding != NULL) |
| 402 | PyMem_FREE(tok->encoding); |
Benjamin Peterson | 2dbfd88 | 2013-07-15 19:15:34 -0700 | [diff] [blame] | 403 | tok->encoding = new_string("utf-8", 5, tok); |
| 404 | if (!tok->encoding) |
| 405 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 406 | /* No need to set_readline: input is already utf-8 */ |
| 407 | return 1; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /* Read a line of text from TOK into S, using the stream in TOK. |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 411 | Return NULL on failure, else S. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 412 | |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 413 | On entry, tok->decoding_buffer will be one of: |
| 414 | 1) NULL: need to call tok->decoding_readline to get a new line |
| 415 | 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | stored the result in tok->decoding_buffer |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 417 | 3) PyByteArrayObject *: previous call to fp_readl did not have enough room |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | (in the s buffer) to copy entire contents of the line read |
| 419 | by tok->decoding_readline. tok->decoding_buffer has the overflow. |
| 420 | In this case, fp_readl is called in a loop (with an expanded buffer) |
| 421 | until the buffer ends with a '\n' (or until the end of the file is |
| 422 | reached): see tok_nextc and its calls to decoding_fgets. |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 423 | */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 424 | |
| 425 | static char * |
| 426 | fp_readl(char *s, int size, struct tok_state *tok) |
| 427 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | PyObject* bufobj; |
| 429 | const char *buf; |
| 430 | Py_ssize_t buflen; |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | /* Ask for one less byte so we can terminate it */ |
| 433 | assert(size > 0); |
| 434 | size--; |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | if (tok->decoding_buffer) { |
| 437 | bufobj = tok->decoding_buffer; |
| 438 | Py_INCREF(bufobj); |
| 439 | } |
| 440 | else |
| 441 | { |
| 442 | bufobj = PyObject_CallObject(tok->decoding_readline, NULL); |
| 443 | if (bufobj == NULL) |
| 444 | goto error; |
| 445 | } |
| 446 | if (PyUnicode_CheckExact(bufobj)) |
| 447 | { |
| 448 | buf = _PyUnicode_AsStringAndSize(bufobj, &buflen); |
| 449 | if (buf == NULL) { |
| 450 | goto error; |
| 451 | } |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | buf = PyByteArray_AsString(bufobj); |
| 456 | if (buf == NULL) { |
| 457 | goto error; |
| 458 | } |
| 459 | buflen = PyByteArray_GET_SIZE(bufobj); |
| 460 | } |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 462 | Py_XDECREF(tok->decoding_buffer); |
| 463 | if (buflen > size) { |
| 464 | /* Too many chars, the rest goes into tok->decoding_buffer */ |
| 465 | tok->decoding_buffer = PyByteArray_FromStringAndSize(buf+size, |
| 466 | buflen-size); |
| 467 | if (tok->decoding_buffer == NULL) |
| 468 | goto error; |
| 469 | buflen = size; |
| 470 | } |
| 471 | else |
| 472 | tok->decoding_buffer = NULL; |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 473 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | memcpy(s, buf, buflen); |
| 475 | s[buflen] = '\0'; |
| 476 | if (buflen == 0) /* EOF */ |
| 477 | s = NULL; |
| 478 | Py_DECREF(bufobj); |
| 479 | return s; |
Neal Norwitz | 41eaedd | 2007-08-12 00:03:22 +0000 | [diff] [blame] | 480 | |
| 481 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | Py_XDECREF(bufobj); |
| 483 | return error_ret(tok); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /* Set the readline function for TOK to a StreamReader's |
| 487 | readline function. The StreamReader is named ENC. |
| 488 | |
| 489 | This function is called from check_bom and check_coding_spec. |
| 490 | |
| 491 | ENC is usually identical to the future value of tok->encoding, |
| 492 | except for the (currently unsupported) case of UTF-16. |
| 493 | |
| 494 | Return 1 on success, 0 on failure. */ |
| 495 | |
| 496 | static int |
| 497 | fp_setreadl(struct tok_state *tok, const char* enc) |
| 498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | PyObject *readline = NULL, *stream = NULL, *io = NULL; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 500 | _Py_IDENTIFIER(open); |
| 501 | _Py_IDENTIFIER(readline); |
Victor Stinner | 22a351a | 2010-10-14 12:04:34 +0000 | [diff] [blame] | 502 | int fd; |
Serhiy Storchaka | 768c16c | 2014-01-09 18:36:09 +0200 | [diff] [blame] | 503 | long pos; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | io = PyImport_ImportModuleNoBlock("io"); |
| 506 | if (io == NULL) |
| 507 | goto cleanup; |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 508 | |
Victor Stinner | 22a351a | 2010-10-14 12:04:34 +0000 | [diff] [blame] | 509 | fd = fileno(tok->fp); |
Serhiy Storchaka | 768c16c | 2014-01-09 18:36:09 +0200 | [diff] [blame] | 510 | /* Due to buffering the file offset for fd can be different from the file |
Martin v. Löwis | 815b41b | 2014-02-28 15:27:29 +0100 | [diff] [blame] | 511 | * position of tok->fp. If tok->fp was opened in text mode on Windows, |
| 512 | * its file position counts CRLF as one char and can't be directly mapped |
| 513 | * to the file offset for fd. Instead we step back one byte and read to |
| 514 | * the end of line.*/ |
Serhiy Storchaka | 768c16c | 2014-01-09 18:36:09 +0200 | [diff] [blame] | 515 | pos = ftell(tok->fp); |
Martin v. Löwis | 815b41b | 2014-02-28 15:27:29 +0100 | [diff] [blame] | 516 | if (pos == -1 || |
| 517 | lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) { |
Victor Stinner | 22a351a | 2010-10-14 12:04:34 +0000 | [diff] [blame] | 518 | PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL); |
| 519 | goto cleanup; |
| 520 | } |
| 521 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 522 | stream = _PyObject_CallMethodId(io, &PyId_open, "isisOOO", |
Victor Stinner | 22a351a | 2010-10-14 12:04:34 +0000 | [diff] [blame] | 523 | fd, "r", -1, enc, Py_None, Py_None, Py_False); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 524 | if (stream == NULL) |
| 525 | goto cleanup; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 526 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 527 | readline = _PyObject_GetAttrId(stream, &PyId_readline); |
Serhiy Storchaka | 5a57ade | 2015-12-24 10:35:59 +0200 | [diff] [blame] | 528 | Py_SETREF(tok->decoding_readline, readline); |
Martin v. Löwis | 815b41b | 2014-02-28 15:27:29 +0100 | [diff] [blame] | 529 | if (pos > 0) { |
| 530 | if (PyObject_CallObject(readline, NULL) == NULL) { |
| 531 | readline = NULL; |
| 532 | goto cleanup; |
| 533 | } |
| 534 | } |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 535 | |
| 536 | cleanup: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | Py_XDECREF(stream); |
| 538 | Py_XDECREF(io); |
| 539 | return readline != NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* Fetch the next byte from TOK. */ |
| 543 | |
| 544 | static int fp_getc(struct tok_state *tok) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | return getc(tok->fp); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* Unfetch the last byte back into TOK. */ |
| 549 | |
| 550 | static void fp_ungetc(int c, struct tok_state *tok) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 551 | ungetc(c, tok->fp); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Martin v. Löwis | 447d33e | 2007-07-29 18:10:01 +0000 | [diff] [blame] | 554 | /* 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. */ |
| 557 | static int valid_utf8(const unsigned char* s) |
| 558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | 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öwis | 447d33e | 2007-07-29 18:10:01 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 582 | /* Read a line of input from TOK. Determine encoding |
| 583 | if necessary. */ |
| 584 | |
| 585 | static char * |
| 586 | decoding_fgets(char *s, int size, struct tok_state *tok) |
| 587 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | 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öwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 615 | #ifndef PGEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | /* 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 Cea | c1935d2 | 2011-04-25 04:03:58 +0200 | [diff] [blame] | 630 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | return error_ret(tok); |
| 637 | } |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 638 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | return line; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | static int |
| 643 | decoding_feof(struct tok_state *tok) |
| 644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | if (tok->decoding_state != STATE_NORMAL) { |
| 646 | return feof(tok->fp); |
| 647 | } else { |
| 648 | PyObject* buf = tok->decoding_buffer; |
| 649 | if (buf == NULL) { |
| 650 | buf = PyObject_CallObject(tok->decoding_readline, NULL); |
| 651 | 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öwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /* Fetch a byte from TOK, using the string buffer. */ |
| 663 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 664 | static int |
| 665 | buf_getc(struct tok_state *tok) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | return Py_CHARMASK(*tok->str++); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /* Unfetch a byte from TOK, using the string buffer. */ |
| 670 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 671 | static void |
| 672 | buf_ungetc(int c, struct tok_state *tok) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | tok->str--; |
| 674 | 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] | 675 | } |
| 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 680 | static int |
| 681 | buf_setreadl(struct tok_state *tok, const char* enc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | tok->enc = enc; |
| 683 | return 1; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* Return a UTF-8 encoding Python string object from the |
| 687 | C byte string STR, which is encoded with ENC. */ |
| 688 | |
| 689 | static PyObject * |
| 690 | translate_into_utf8(const char* str, const char* enc) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | 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öwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 700 | |
| 701 | static char * |
| 702 | translate_newlines(const char *s, int exec_input, struct tok_state *tok) { |
Victor Stinner | 7969773 | 2013-06-05 00:44:00 +0200 | [diff] [blame] | 703 | int skip_next_lf = 0; |
| 704 | size_t needed_length = strlen(s) + 2, final_length; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 705 | 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 Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 742 | /* 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 | |
| 746 | static const char * |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 747 | decode_str(const char *input, int single, struct tok_state *tok) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 748 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | 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 Storchaka | 768c16c | 2014-01-09 18:36:09 +0200 | [diff] [blame] | 784 | if (tok->enc == NULL && !tok->read_coding_spec && newl[1]) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | 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öwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | #endif /* PGEN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 803 | |
| 804 | /* Set up tokenizer for string */ |
| 805 | |
| 806 | struct tok_state * |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 807 | PyTokenizer_FromString(const char *str, int exec_input) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | struct tok_state *tok = tok_new(); |
| 810 | if (tok == NULL) |
| 811 | return NULL; |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 812 | str = decode_str(str, exec_input, tok); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | if (str == NULL) { |
| 814 | PyTokenizer_Free(tok); |
| 815 | return NULL; |
| 816 | } |
Neal Norwitz | dee2fd5 | 2005-11-16 05:12:59 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | /* XXX: constify members. */ |
| 819 | tok->buf = tok->cur = tok->end = tok->inp = (char*)str; |
| 820 | return tok; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 823 | struct tok_state * |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 824 | PyTokenizer_FromUTF8(const char *str, int exec_input) |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | struct tok_state *tok = tok_new(); |
| 827 | if (tok == NULL) |
| 828 | return NULL; |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 829 | #ifndef PGEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | tok->input = str = translate_newlines(str, exec_input, tok); |
Benjamin Peterson | aeaa592 | 2009-11-13 00:17:59 +0000 | [diff] [blame] | 831 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | 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 Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 847 | /* XXX: constify members. */ |
| 848 | tok->buf = tok->cur = tok->end = tok->inp = (char*)str; |
| 849 | return tok; |
Benjamin Peterson | f5b5224 | 2009-03-02 23:31:26 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 852 | /* Set up tokenizer for file */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 853 | |
| 854 | struct tok_state * |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 855 | PyTokenizer_FromFile(FILE *fp, const char* enc, |
| 856 | const char *ps1, const char *ps2) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | |
| 885 | /* Free a tok_state structure */ |
| 886 | |
| 887 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 888 | PyTokenizer_Free(struct tok_state *tok) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | if (tok->encoding != NULL) |
| 891 | PyMem_FREE(tok->encoding); |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 892 | #ifndef PGEN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | Py_XDECREF(tok->decoding_readline); |
| 894 | Py_XDECREF(tok->decoding_buffer); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 895 | Py_XDECREF(tok->filename); |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 896 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 897 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 904 | /* Get next char, updating state; error code goes into tok->done */ |
| 905 | |
| 906 | static int |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 907 | tok_nextc(struct tok_state *tok) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 909 | 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 Stinner | 034c753 | 2011-01-07 18:56:19 +0000 | [diff] [blame] | 935 | #ifndef PGEN |
Victor Stinner | 89e3436 | 2011-01-07 18:47:22 +0000 | [diff] [blame] | 936 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | newtok = PyMem_MALLOC(buflen+1); |
| 956 | strcpy(newtok, buf); |
| 957 | Py_DECREF(u); |
| 958 | } |
Martin v. Löwis | 85bcc66 | 2007-09-04 09:18:06 +0000 | [diff] [blame] | 959 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | 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 Storchaka | 0d44111 | 2015-11-14 15:10:35 +0200 | [diff] [blame] | 1018 | if (!tok->decoding_erred) |
| 1019 | tok->done = E_EOF; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | done = 1; |
| 1021 | } |
| 1022 | else { |
| 1023 | tok->done = E_OK; |
| 1024 | tok->inp = strchr(tok->buf, '\0'); |
| 1025 | done = tok->inp[-1] == '\n'; |
| 1026 | } |
| 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 Storchaka | 0d44111 | 2015-11-14 15:10:35 +0200 | [diff] [blame] | 1053 | tok->cur = tok->buf + cur; |
| 1054 | tok->line_start = tok->cur; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | |
| 1098 | /* Back-up one character */ |
| 1099 | |
| 1100 | static void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1101 | tok_backup(struct tok_state *tok, int c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | /* Return the token corresponding to a single character */ |
| 1113 | |
| 1114 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 1115 | PyToken_OneChar(int c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | 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 Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1140 | case '@': return AT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1141 | default: return OP; |
| 1142 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1146 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 1147 | PyToken_TwoChars(int c1, int c2) |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | 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 Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 1216 | case '@': |
| 1217 | switch (c2) { |
| 1218 | case '=': return ATEQUAL; |
| 1219 | } |
| 1220 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | } |
| 1222 | return OP; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1225 | int |
| 1226 | PyToken_ThreeChars(int c1, int c2, int c3) |
| 1227 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | 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 Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 1271 | case '.': |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1272 | switch (c3) { |
| 1273 | case '.': |
| 1274 | return ELLIPSIS; |
| 1275 | } |
| 1276 | break; |
| 1277 | } |
| 1278 | break; |
| 1279 | } |
| 1280 | return OP; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1281 | } |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1282 | |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1283 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 1284 | indenterror(struct tok_state *tok) |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | if (tok->alterror) { |
| 1287 | tok->done = E_TABSPACE; |
| 1288 | tok->cur = tok->inp; |
| 1289 | return 1; |
| 1290 | } |
| 1291 | if (tok->altwarning) { |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1292 | #ifdef PGEN |
| 1293 | PySys_WriteStderr("inconsistent use of tabs and spaces " |
| 1294 | "in indentation\n"); |
| 1295 | #else |
| 1296 | PySys_FormatStderr("%U: inconsistent use of tabs and spaces " |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | "in indentation\n", tok->filename); |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1298 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1299 | tok->altwarning = 0; |
| 1300 | } |
| 1301 | return 0; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1304 | #ifdef PGEN |
Victor Stinner | 52f6dd7 | 2010-03-12 14:45:56 +0000 | [diff] [blame] | 1305 | #define verify_identifier(tok) 1 |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1306 | #else |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1307 | /* Verify that the identifier follows PEP 3131. |
| 1308 | All identifier strings are guaranteed to be "ready" unicode objects. |
| 1309 | */ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1310 | static int |
Victor Stinner | 52f6dd7 | 2010-03-12 14:45:56 +0000 | [diff] [blame] | 1311 | verify_identifier(struct tok_state *tok) |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1312 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | PyObject *s; |
| 1314 | int result; |
Benjamin Peterson | d73aca7 | 2015-04-21 12:05:19 -0400 | [diff] [blame] | 1315 | if (tok->decoding_erred) |
| 1316 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1318 | if (s == NULL || PyUnicode_READY(s) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { |
| 1320 | PyErr_Clear(); |
| 1321 | tok->done = E_IDENTIFIER; |
| 1322 | } else { |
| 1323 | tok->done = E_ERROR; |
| 1324 | } |
| 1325 | return 0; |
| 1326 | } |
| 1327 | result = PyUnicode_IsIdentifier(s); |
| 1328 | Py_DECREF(s); |
| 1329 | if (result == 0) |
| 1330 | tok->done = E_IDENTIFIER; |
| 1331 | return result; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1332 | } |
| 1333 | #endif |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1334 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1335 | /* Get next token, after space stripping etc. */ |
| 1336 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1337 | static int |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1338 | tok_get(struct tok_state *tok, char **p_start, char **p_end) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1339 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1340 | int c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | int blankline, nonascii; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | *p_start = *p_end = NULL; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1344 | nextline: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | tok->start = NULL; |
| 1346 | blankline = 0; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | /* Get indentation level */ |
| 1349 | if (tok->atbol) { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1350 | int col = 0; |
| 1351 | int altcol = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1352 | tok->atbol = 0; |
| 1353 | for (;;) { |
| 1354 | c = tok_nextc(tok); |
| 1355 | if (c == ' ') |
| 1356 | col++, altcol++; |
| 1357 | else if (c == '\t') { |
| 1358 | col = (col/tok->tabsize + 1) * tok->tabsize; |
| 1359 | altcol = (altcol/tok->alttabsize + 1) |
| 1360 | * tok->alttabsize; |
| 1361 | } |
| 1362 | else if (c == '\014') /* Control-L (formfeed) */ |
| 1363 | col = altcol = 0; /* For Emacs users */ |
| 1364 | else |
| 1365 | break; |
| 1366 | } |
| 1367 | tok_backup(tok, c); |
| 1368 | if (c == '#' || c == '\n') { |
| 1369 | /* Lines with only whitespace and/or comments |
| 1370 | shouldn't affect the indentation and are |
| 1371 | not passed to the parser as NEWLINE tokens, |
| 1372 | except *totally* empty lines in interactive |
| 1373 | mode, which signal the end of a command group. */ |
| 1374 | if (col == 0 && c == '\n' && tok->prompt != NULL) |
| 1375 | blankline = 0; /* Let it through */ |
| 1376 | else |
| 1377 | blankline = 1; /* Ignore completely */ |
| 1378 | /* We can't jump back right here since we still |
| 1379 | may need to skip to the end of a comment */ |
| 1380 | } |
| 1381 | if (!blankline && tok->level == 0) { |
| 1382 | if (col == tok->indstack[tok->indent]) { |
| 1383 | /* No change */ |
| 1384 | if (altcol != tok->altindstack[tok->indent]) { |
| 1385 | if (indenterror(tok)) |
| 1386 | return ERRORTOKEN; |
| 1387 | } |
| 1388 | } |
| 1389 | else if (col > tok->indstack[tok->indent]) { |
| 1390 | /* Indent -- always one */ |
| 1391 | if (tok->indent+1 >= MAXINDENT) { |
| 1392 | tok->done = E_TOODEEP; |
| 1393 | tok->cur = tok->inp; |
| 1394 | return ERRORTOKEN; |
| 1395 | } |
| 1396 | if (altcol <= tok->altindstack[tok->indent]) { |
| 1397 | if (indenterror(tok)) |
| 1398 | return ERRORTOKEN; |
| 1399 | } |
| 1400 | tok->pendin++; |
| 1401 | tok->indstack[++tok->indent] = col; |
| 1402 | tok->altindstack[tok->indent] = altcol; |
| 1403 | } |
| 1404 | else /* col < tok->indstack[tok->indent] */ { |
| 1405 | /* Dedent -- any number, must be consistent */ |
| 1406 | while (tok->indent > 0 && |
| 1407 | col < tok->indstack[tok->indent]) { |
| 1408 | tok->pendin--; |
| 1409 | tok->indent--; |
| 1410 | } |
| 1411 | if (col != tok->indstack[tok->indent]) { |
| 1412 | tok->done = E_DEDENT; |
| 1413 | tok->cur = tok->inp; |
| 1414 | return ERRORTOKEN; |
| 1415 | } |
| 1416 | if (altcol != tok->altindstack[tok->indent]) { |
| 1417 | if (indenterror(tok)) |
| 1418 | return ERRORTOKEN; |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | tok->start = tok->cur; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1426 | /* Return pending indents/dedents */ |
| 1427 | if (tok->pendin != 0) { |
| 1428 | if (tok->pendin < 0) { |
| 1429 | tok->pendin++; |
| 1430 | return DEDENT; |
| 1431 | } |
| 1432 | else { |
| 1433 | tok->pendin--; |
| 1434 | return INDENT; |
| 1435 | } |
| 1436 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1437 | |
Yury Selivanov | 96ec934 | 2015-07-23 15:01:58 +0300 | [diff] [blame] | 1438 | if (tok->async_def |
| 1439 | && !blankline |
| 1440 | && tok->level == 0 |
| 1441 | /* There was a NEWLINE after ASYNC DEF, |
| 1442 | so we're past the signature. */ |
| 1443 | && tok->async_def_nl |
| 1444 | /* Current indentation level is less than where |
| 1445 | the async function was defined */ |
| 1446 | && tok->async_def_indent >= tok->indent) |
Yury Selivanov | 8fb307c | 2015-07-22 13:33:45 +0300 | [diff] [blame] | 1447 | { |
Yury Selivanov | 96ec934 | 2015-07-23 15:01:58 +0300 | [diff] [blame] | 1448 | tok->async_def = 0; |
| 1449 | tok->async_def_indent = 0; |
| 1450 | tok->async_def_nl = 0; |
Yury Selivanov | 8fb307c | 2015-07-22 13:33:45 +0300 | [diff] [blame] | 1451 | } |
| 1452 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1453 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | tok->start = NULL; |
| 1455 | /* Skip spaces */ |
| 1456 | do { |
| 1457 | c = tok_nextc(tok); |
| 1458 | } while (c == ' ' || c == '\t' || c == '\014'); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1459 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | /* Set start of current token */ |
| 1461 | tok->start = tok->cur - 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | /* Skip comment */ |
| 1464 | if (c == '#') |
| 1465 | while (c != EOF && c != '\n') |
| 1466 | c = tok_nextc(tok); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | /* Check for EOF and errors now */ |
| 1469 | if (c == EOF) { |
| 1470 | return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN; |
| 1471 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | /* Identifier (most frequent token!) */ |
| 1474 | nonascii = 0; |
| 1475 | if (is_potential_identifier_start(c)) { |
Christian Heimes | 0b3847d | 2012-06-20 11:17:58 +0200 | [diff] [blame] | 1476 | /* Process b"", r"", u"", br"" and rb"" */ |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 1477 | int saw_b = 0, saw_r = 0, saw_u = 0; |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 1478 | while (1) { |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 1479 | if (!(saw_b || saw_u) && (c == 'b' || c == 'B')) |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 1480 | saw_b = 1; |
Armin Ronacher | 6ecf77b | 2012-03-04 12:04:06 +0000 | [diff] [blame] | 1481 | /* Since this is a backwards compatibility support literal we don't |
| 1482 | want to support it in arbitrary order like byte literals. */ |
| 1483 | else if (!(saw_b || saw_u || saw_r) && (c == 'u' || c == 'U')) |
| 1484 | saw_u = 1; |
Christian Heimes | 0b3847d | 2012-06-20 11:17:58 +0200 | [diff] [blame] | 1485 | /* ur"" and ru"" are not supported */ |
| 1486 | else if (!(saw_r || saw_u) && (c == 'r' || c == 'R')) |
Antoine Pitrou | 3a5d4cb | 2012-01-12 22:46:19 +0100 | [diff] [blame] | 1487 | saw_r = 1; |
| 1488 | else |
| 1489 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | c = tok_nextc(tok); |
| 1491 | if (c == '"' || c == '\'') |
| 1492 | goto letter_quote; |
| 1493 | } |
| 1494 | while (is_potential_identifier_char(c)) { |
| 1495 | if (c >= 128) |
| 1496 | nonascii = 1; |
| 1497 | c = tok_nextc(tok); |
| 1498 | } |
| 1499 | tok_backup(tok, c); |
Benjamin Peterson | d73aca7 | 2015-04-21 12:05:19 -0400 | [diff] [blame] | 1500 | if (nonascii && !verify_identifier(tok)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | return ERRORTOKEN; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | *p_start = tok->start; |
| 1503 | *p_end = tok->cur; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1504 | |
Yury Selivanov | 96ec934 | 2015-07-23 15:01:58 +0300 | [diff] [blame] | 1505 | /* async/await parsing block. */ |
| 1506 | if (tok->cur - tok->start == 5) { |
| 1507 | /* Current token length is 5. */ |
| 1508 | if (tok->async_def) { |
| 1509 | /* We're inside an 'async def' function. */ |
| 1510 | if (memcmp(tok->start, "async", 5) == 0) |
| 1511 | return ASYNC; |
| 1512 | if (memcmp(tok->start, "await", 5) == 0) |
| 1513 | return AWAIT; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1514 | } |
Yury Selivanov | 96ec934 | 2015-07-23 15:01:58 +0300 | [diff] [blame] | 1515 | else if (memcmp(tok->start, "async", 5) == 0) { |
| 1516 | /* The current token is 'async'. |
| 1517 | Look ahead one token.*/ |
Yury Selivanov | 8085b80 | 2015-05-18 12:50:52 -0400 | [diff] [blame] | 1518 | |
Yury Selivanov | 96ec934 | 2015-07-23 15:01:58 +0300 | [diff] [blame] | 1519 | struct tok_state ahead_tok; |
| 1520 | char *ahead_tok_start = NULL, *ahead_tok_end = NULL; |
| 1521 | int ahead_tok_kind; |
Yury Selivanov | 8085b80 | 2015-05-18 12:50:52 -0400 | [diff] [blame] | 1522 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1523 | memcpy(&ahead_tok, tok, sizeof(ahead_tok)); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1524 | ahead_tok_kind = tok_get(&ahead_tok, &ahead_tok_start, |
Yury Selivanov | 96ec934 | 2015-07-23 15:01:58 +0300 | [diff] [blame] | 1525 | &ahead_tok_end); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1526 | |
Yury Selivanov | 8fb307c | 2015-07-22 13:33:45 +0300 | [diff] [blame] | 1527 | if (ahead_tok_kind == NAME |
| 1528 | && ahead_tok.cur - ahead_tok.start == 3 |
| 1529 | && memcmp(ahead_tok.start, "def", 3) == 0) |
| 1530 | { |
| 1531 | /* The next token is going to be 'def', so instead of |
| 1532 | returning 'async' NAME token, we return ASYNC. */ |
Yury Selivanov | 96ec934 | 2015-07-23 15:01:58 +0300 | [diff] [blame] | 1533 | tok->async_def_indent = tok->indent; |
| 1534 | tok->async_def = 1; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1535 | return ASYNC; |
| 1536 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1537 | } |
| 1538 | } |
| 1539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | return NAME; |
| 1541 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | /* Newline */ |
| 1544 | if (c == '\n') { |
| 1545 | tok->atbol = 1; |
| 1546 | if (blankline || tok->level > 0) |
| 1547 | goto nextline; |
| 1548 | *p_start = tok->start; |
| 1549 | *p_end = tok->cur - 1; /* Leave '\n' out of the string */ |
| 1550 | tok->cont_line = 0; |
Yury Selivanov | 96ec934 | 2015-07-23 15:01:58 +0300 | [diff] [blame] | 1551 | if (tok->async_def) { |
| 1552 | /* We're somewhere inside an 'async def' function, and |
| 1553 | we've encountered a NEWLINE after its signature. */ |
| 1554 | tok->async_def_nl = 1; |
Yury Selivanov | 8fb307c | 2015-07-22 13:33:45 +0300 | [diff] [blame] | 1555 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | return NEWLINE; |
| 1557 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | /* Period or number starting with period? */ |
| 1560 | if (c == '.') { |
| 1561 | c = tok_nextc(tok); |
| 1562 | if (isdigit(c)) { |
| 1563 | goto fraction; |
| 1564 | } else if (c == '.') { |
| 1565 | c = tok_nextc(tok); |
| 1566 | if (c == '.') { |
| 1567 | *p_start = tok->start; |
| 1568 | *p_end = tok->cur; |
| 1569 | return ELLIPSIS; |
| 1570 | } else { |
| 1571 | tok_backup(tok, c); |
| 1572 | } |
| 1573 | tok_backup(tok, '.'); |
| 1574 | } else { |
| 1575 | tok_backup(tok, c); |
| 1576 | } |
| 1577 | *p_start = tok->start; |
| 1578 | *p_end = tok->cur; |
| 1579 | return DOT; |
| 1580 | } |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | /* Number */ |
| 1583 | if (isdigit(c)) { |
| 1584 | if (c == '0') { |
| 1585 | /* Hex, octal or binary -- maybe. */ |
| 1586 | c = tok_nextc(tok); |
| 1587 | if (c == '.') |
| 1588 | goto fraction; |
| 1589 | if (c == 'j' || c == 'J') |
| 1590 | goto imaginary; |
| 1591 | if (c == 'x' || c == 'X') { |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 1592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1593 | /* Hex */ |
| 1594 | c = tok_nextc(tok); |
| 1595 | if (!isxdigit(c)) { |
| 1596 | tok->done = E_TOKEN; |
| 1597 | tok_backup(tok, c); |
| 1598 | return ERRORTOKEN; |
| 1599 | } |
| 1600 | do { |
| 1601 | c = tok_nextc(tok); |
| 1602 | } while (isxdigit(c)); |
| 1603 | } |
| 1604 | else if (c == 'o' || c == 'O') { |
| 1605 | /* Octal */ |
| 1606 | c = tok_nextc(tok); |
| 1607 | if (c < '0' || c >= '8') { |
| 1608 | tok->done = E_TOKEN; |
| 1609 | tok_backup(tok, c); |
| 1610 | return ERRORTOKEN; |
| 1611 | } |
| 1612 | do { |
| 1613 | c = tok_nextc(tok); |
| 1614 | } while ('0' <= c && c < '8'); |
| 1615 | } |
| 1616 | else if (c == 'b' || c == 'B') { |
| 1617 | /* Binary */ |
| 1618 | c = tok_nextc(tok); |
| 1619 | if (c != '0' && c != '1') { |
| 1620 | tok->done = E_TOKEN; |
| 1621 | tok_backup(tok, c); |
| 1622 | return ERRORTOKEN; |
| 1623 | } |
| 1624 | do { |
| 1625 | c = tok_nextc(tok); |
| 1626 | } while (c == '0' || c == '1'); |
| 1627 | } |
| 1628 | else { |
| 1629 | int nonzero = 0; |
| 1630 | /* maybe old-style octal; c is first char of it */ |
| 1631 | /* in any case, allow '0' as a literal */ |
| 1632 | while (c == '0') |
| 1633 | c = tok_nextc(tok); |
| 1634 | while (isdigit(c)) { |
| 1635 | nonzero = 1; |
| 1636 | c = tok_nextc(tok); |
| 1637 | } |
| 1638 | if (c == '.') |
| 1639 | goto fraction; |
| 1640 | else if (c == 'e' || c == 'E') |
| 1641 | goto exponent; |
| 1642 | else if (c == 'j' || c == 'J') |
| 1643 | goto imaginary; |
| 1644 | else if (nonzero) { |
| 1645 | tok->done = E_TOKEN; |
| 1646 | tok_backup(tok, c); |
| 1647 | return ERRORTOKEN; |
| 1648 | } |
| 1649 | } |
| 1650 | } |
| 1651 | else { |
| 1652 | /* Decimal */ |
| 1653 | do { |
| 1654 | c = tok_nextc(tok); |
| 1655 | } while (isdigit(c)); |
| 1656 | { |
| 1657 | /* Accept floating point numbers. */ |
| 1658 | if (c == '.') { |
| 1659 | fraction: |
| 1660 | /* Fraction */ |
| 1661 | do { |
| 1662 | c = tok_nextc(tok); |
| 1663 | } while (isdigit(c)); |
| 1664 | } |
| 1665 | if (c == 'e' || c == 'E') { |
Benjamin Peterson | c416162 | 2014-06-07 12:36:39 -0700 | [diff] [blame] | 1666 | int e; |
| 1667 | exponent: |
| 1668 | e = c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | /* Exponent part */ |
| 1670 | c = tok_nextc(tok); |
Benjamin Peterson | c416162 | 2014-06-07 12:36:39 -0700 | [diff] [blame] | 1671 | if (c == '+' || c == '-') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1672 | c = tok_nextc(tok); |
Benjamin Peterson | c416162 | 2014-06-07 12:36:39 -0700 | [diff] [blame] | 1673 | if (!isdigit(c)) { |
| 1674 | tok->done = E_TOKEN; |
| 1675 | tok_backup(tok, c); |
| 1676 | return ERRORTOKEN; |
| 1677 | } |
| 1678 | } else if (!isdigit(c)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | tok_backup(tok, c); |
Benjamin Peterson | c416162 | 2014-06-07 12:36:39 -0700 | [diff] [blame] | 1680 | tok_backup(tok, e); |
| 1681 | *p_start = tok->start; |
| 1682 | *p_end = tok->cur; |
| 1683 | return NUMBER; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | } |
| 1685 | do { |
| 1686 | c = tok_nextc(tok); |
| 1687 | } while (isdigit(c)); |
| 1688 | } |
| 1689 | if (c == 'j' || c == 'J') |
| 1690 | /* Imaginary part */ |
| 1691 | imaginary: |
| 1692 | c = tok_nextc(tok); |
| 1693 | } |
| 1694 | } |
| 1695 | tok_backup(tok, c); |
| 1696 | *p_start = tok->start; |
| 1697 | *p_end = tok->cur; |
| 1698 | return NUMBER; |
| 1699 | } |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 1700 | |
| 1701 | letter_quote: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | /* String */ |
| 1703 | if (c == '\'' || c == '"') { |
| 1704 | int quote = c; |
| 1705 | int quote_size = 1; /* 1 or 3 */ |
| 1706 | int end_quote_size = 0; |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1708 | /* Find the quote size and start of string */ |
| 1709 | c = tok_nextc(tok); |
| 1710 | if (c == quote) { |
| 1711 | c = tok_nextc(tok); |
| 1712 | if (c == quote) |
| 1713 | quote_size = 3; |
| 1714 | else |
| 1715 | end_quote_size = 1; /* empty string found */ |
| 1716 | } |
| 1717 | if (c != quote) |
| 1718 | tok_backup(tok, c); |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | /* Get rest of string */ |
| 1721 | while (end_quote_size != quote_size) { |
| 1722 | c = tok_nextc(tok); |
| 1723 | if (c == EOF) { |
| 1724 | if (quote_size == 3) |
| 1725 | tok->done = E_EOFS; |
| 1726 | else |
| 1727 | tok->done = E_EOLS; |
| 1728 | tok->cur = tok->inp; |
| 1729 | return ERRORTOKEN; |
| 1730 | } |
| 1731 | if (quote_size == 1 && c == '\n') { |
| 1732 | tok->done = E_EOLS; |
| 1733 | tok->cur = tok->inp; |
| 1734 | return ERRORTOKEN; |
| 1735 | } |
| 1736 | if (c == quote) |
| 1737 | end_quote_size += 1; |
| 1738 | else { |
| 1739 | end_quote_size = 0; |
| 1740 | if (c == '\\') |
| 1741 | c = tok_nextc(tok); /* skip escaped char */ |
| 1742 | } |
| 1743 | } |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | *p_start = tok->start; |
| 1746 | *p_end = tok->cur; |
| 1747 | return STRING; |
| 1748 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | /* Line continuation */ |
| 1751 | if (c == '\\') { |
| 1752 | c = tok_nextc(tok); |
| 1753 | if (c != '\n') { |
| 1754 | tok->done = E_LINECONT; |
| 1755 | tok->cur = tok->inp; |
| 1756 | return ERRORTOKEN; |
| 1757 | } |
| 1758 | tok->cont_line = 1; |
| 1759 | goto again; /* Read next line */ |
| 1760 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | /* Check for two-character token */ |
| 1763 | { |
| 1764 | int c2 = tok_nextc(tok); |
| 1765 | int token = PyToken_TwoChars(c, c2); |
| 1766 | if (token != OP) { |
| 1767 | int c3 = tok_nextc(tok); |
| 1768 | int token3 = PyToken_ThreeChars(c, c2, c3); |
| 1769 | if (token3 != OP) { |
| 1770 | token = token3; |
| 1771 | } else { |
| 1772 | tok_backup(tok, c3); |
| 1773 | } |
| 1774 | *p_start = tok->start; |
| 1775 | *p_end = tok->cur; |
| 1776 | return token; |
| 1777 | } |
| 1778 | tok_backup(tok, c2); |
| 1779 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | /* Keep track of parentheses nesting level */ |
| 1782 | switch (c) { |
| 1783 | case '(': |
| 1784 | case '[': |
| 1785 | case '{': |
| 1786 | tok->level++; |
| 1787 | break; |
| 1788 | case ')': |
| 1789 | case ']': |
| 1790 | case '}': |
| 1791 | tok->level--; |
| 1792 | break; |
| 1793 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1794 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | /* Punctuation character */ |
| 1796 | *p_start = tok->start; |
| 1797 | *p_end = tok->cur; |
| 1798 | return PyToken_OneChar(c); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1801 | int |
| 1802 | PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end) |
| 1803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1804 | int result = tok_get(tok, p_start, p_end); |
| 1805 | if (tok->decoding_erred) { |
| 1806 | result = ERRORTOKEN; |
| 1807 | tok->done = E_DECODE; |
| 1808 | } |
| 1809 | return result; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1810 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1811 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 1812 | /* Get the encoding of a Python file. Check for the coding cookie and check if |
| 1813 | the file starts with a BOM. |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1814 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 1815 | PyTokenizer_FindEncodingFilename() returns NULL when it can't find the |
| 1816 | encoding in the first or second line of the file (in which case the encoding |
| 1817 | should be assumed to be UTF-8). |
Brett Cannon | e453989 | 2007-10-20 03:46:49 +0000 | [diff] [blame] | 1818 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 1819 | The char* returned is malloc'ed via PyMem_MALLOC() and thus must be freed |
| 1820 | by the caller. */ |
| 1821 | |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1822 | char * |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 1823 | PyTokenizer_FindEncodingFilename(int fd, PyObject *filename) |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 1824 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | struct tok_state *tok; |
| 1826 | FILE *fp; |
| 1827 | char *p_start =NULL , *p_end =NULL , *encoding = NULL; |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1828 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1829 | #ifndef PGEN |
| 1830 | fd = _Py_dup(fd); |
| 1831 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | fd = dup(fd); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1833 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | if (fd < 0) { |
| 1835 | return NULL; |
| 1836 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | fp = fdopen(fd, "r"); |
| 1839 | if (fp == NULL) { |
| 1840 | return NULL; |
| 1841 | } |
| 1842 | tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL); |
| 1843 | if (tok == NULL) { |
| 1844 | fclose(fp); |
| 1845 | return NULL; |
| 1846 | } |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1847 | #ifndef PGEN |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 1848 | if (filename != NULL) { |
| 1849 | Py_INCREF(filename); |
| 1850 | tok->filename = filename; |
| 1851 | } |
| 1852 | else { |
| 1853 | tok->filename = PyUnicode_FromString("<string>"); |
| 1854 | if (tok->filename == NULL) { |
| 1855 | fclose(fp); |
| 1856 | PyTokenizer_Free(tok); |
| 1857 | return encoding; |
| 1858 | } |
| 1859 | } |
Victor Stinner | 7f2fee3 | 2011-04-05 00:39:01 +0200 | [diff] [blame] | 1860 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | while (tok->lineno < 2 && tok->done == E_OK) { |
| 1862 | PyTokenizer_Get(tok, &p_start, &p_end); |
| 1863 | } |
| 1864 | fclose(fp); |
| 1865 | if (tok->encoding) { |
| 1866 | encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1); |
| 1867 | if (encoding) |
| 1868 | strcpy(encoding, tok->encoding); |
| 1869 | } |
| 1870 | PyTokenizer_Free(tok); |
| 1871 | return encoding; |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1872 | } |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 1873 | |
Victor Stinner | fe7c5b5 | 2011-04-05 01:48:03 +0200 | [diff] [blame] | 1874 | char * |
| 1875 | PyTokenizer_FindEncoding(int fd) |
| 1876 | { |
| 1877 | return PyTokenizer_FindEncodingFilename(fd, NULL); |
| 1878 | } |
| 1879 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 1880 | #ifdef Py_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1881 | |
| 1882 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 1883 | tok_dump(int type, char *start, char *end) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | printf("%s", _PyParser_TokenNames[type]); |
| 1886 | if (type == NAME || type == NUMBER || type == STRING || type == OP) |
| 1887 | printf("(%.*s)", (int)(end - start), start); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | #endif |