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) (\ |
| 22 | (c >= 'a' && c <= 'z')\ |
| 23 | || (c >= 'A' && c <= 'Z')\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 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) (\ |
| 28 | (c >= 'a' && c <= 'z')\ |
| 29 | || (c >= 'A' && c <= 'Z')\ |
| 30 | || (c >= '0' && c <= '9')\ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 31 | || c == '_'\ |
| 32 | || (c >= 128)) |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 33 | |
Martin v. Löwis | 566f6af | 2002-10-26 14:39:10 +0000 | [diff] [blame] | 34 | extern char *PyOS_Readline(FILE *, FILE *, 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 | |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 50 | char *_PyParser_TokenNames[] = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +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", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | "LBRACE", |
| 77 | "RBRACE", |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 78 | "EQEQUAL", |
| 79 | "NOTEQUAL", |
| 80 | "LESSEQUAL", |
| 81 | "GREATEREQUAL", |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 82 | "TILDE", |
| 83 | "CIRCUMFLEX", |
| 84 | "LEFTSHIFT", |
| 85 | "RIGHTSHIFT", |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 86 | "DOUBLESTAR", |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 87 | "PLUSEQUAL", |
| 88 | "MINEQUAL", |
| 89 | "STAREQUAL", |
| 90 | "SLASHEQUAL", |
| 91 | "PERCENTEQUAL", |
| 92 | "AMPEREQUAL", |
| 93 | "VBAREQUAL", |
| 94 | "CIRCUMFLEXEQUAL", |
| 95 | "LEFTSHIFTEQUAL", |
| 96 | "RIGHTSHIFTEQUAL", |
| 97 | "DOUBLESTAREQUAL", |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 98 | "DOUBLESLASH", |
| 99 | "DOUBLESLASHEQUAL", |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 100 | "AT", |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 101 | "RARROW", |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 102 | "ELLIPSIS", |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 103 | /* This table must match the #defines in token.h! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | "OP", |
| 105 | "<ERRORTOKEN>", |
| 106 | "<N_TOKENS>" |
| 107 | }; |
| 108 | |
| 109 | |
| 110 | /* Create and initialize a new tok_state structure */ |
| 111 | |
| 112 | static struct tok_state * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 113 | tok_new(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 115 | struct tok_state *tok = (struct tok_state *)PyMem_MALLOC( |
| 116 | sizeof(struct tok_state)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | if (tok == NULL) |
| 118 | return NULL; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 119 | tok->buf = tok->cur = tok->end = tok->inp = tok->start = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | tok->done = E_OK; |
| 121 | tok->fp = NULL; |
| 122 | tok->tabsize = TABSIZE; |
| 123 | tok->indent = 0; |
| 124 | tok->indstack[0] = 0; |
| 125 | tok->atbol = 1; |
| 126 | tok->pendin = 0; |
| 127 | tok->prompt = tok->nextprompt = NULL; |
| 128 | tok->lineno = 0; |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 129 | tok->level = 0; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 130 | tok->filename = NULL; |
Thomas Wouters | 6caa07b | 2006-04-14 11:33:28 +0000 | [diff] [blame] | 131 | tok->altwarning = 1; |
| 132 | tok->alterror = 1; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 133 | tok->alttabsize = 1; |
| 134 | tok->altindstack[0] = 0; |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 135 | tok->decoding_state = STATE_INIT; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 136 | tok->decoding_erred = 0; |
| 137 | tok->read_coding_spec = 0; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 138 | tok->encoding = NULL; |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 139 | tok->cont_line = 0; |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 140 | #ifndef PGEN |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 141 | tok->decoding_readline = NULL; |
| 142 | tok->decoding_buffer = NULL; |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 143 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | return tok; |
| 145 | } |
| 146 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 147 | #ifdef PGEN |
| 148 | |
| 149 | static char * |
| 150 | decoding_fgets(char *s, int size, struct tok_state *tok) |
| 151 | { |
| 152 | return fgets(s, size, tok->fp); |
| 153 | } |
| 154 | |
| 155 | static int |
| 156 | decoding_feof(struct tok_state *tok) |
| 157 | { |
| 158 | return feof(tok->fp); |
| 159 | } |
| 160 | |
| 161 | static const char * |
| 162 | decode_str(const char *str, struct tok_state *tok) |
| 163 | { |
| 164 | return str; |
| 165 | } |
| 166 | |
| 167 | #else /* PGEN */ |
| 168 | |
| 169 | static char * |
| 170 | error_ret(struct tok_state *tok) /* XXX */ |
| 171 | { |
| 172 | tok->decoding_erred = 1; |
| 173 | if (tok->fp != NULL && tok->buf != NULL) /* see PyTokenizer_Free */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 174 | PyMem_FREE(tok->buf); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 175 | tok->buf = NULL; |
| 176 | return NULL; /* as if it were EOF */ |
| 177 | } |
| 178 | |
| 179 | static char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 180 | new_string(const char *s, Py_ssize_t len) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 181 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 182 | char* result = (char *)PyMem_MALLOC(len + 1); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 183 | if (result != NULL) { |
| 184 | memcpy(result, s, len); |
| 185 | result[len] = '\0'; |
| 186 | } |
| 187 | return result; |
| 188 | } |
| 189 | |
| 190 | static char * |
| 191 | get_normal_name(char *s) /* for utf-8 and latin-1 */ |
| 192 | { |
| 193 | char buf[13]; |
| 194 | int i; |
| 195 | for (i = 0; i < 12; i++) { |
| 196 | int c = s[i]; |
| 197 | if (c == '\0') break; |
| 198 | else if (c == '_') buf[i] = '-'; |
| 199 | else buf[i] = tolower(c); |
| 200 | } |
| 201 | buf[i] = '\0'; |
| 202 | if (strcmp(buf, "utf-8") == 0 || |
| 203 | strncmp(buf, "utf-8-", 6) == 0) return "utf-8"; |
| 204 | else if (strcmp(buf, "latin-1") == 0 || |
| 205 | strcmp(buf, "iso-8859-1") == 0 || |
| 206 | strcmp(buf, "iso-latin-1") == 0 || |
| 207 | strncmp(buf, "latin-1-", 8) == 0 || |
| 208 | strncmp(buf, "iso-8859-1-", 11) == 0 || |
| 209 | strncmp(buf, "iso-latin-1-", 12) == 0) return "iso-8859-1"; |
| 210 | else return s; |
| 211 | } |
| 212 | |
| 213 | /* Return the coding spec in S, or NULL if none is found. */ |
| 214 | |
| 215 | static char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 216 | get_coding_spec(const char *s, Py_ssize_t size) |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 217 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 218 | Py_ssize_t i; |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 219 | /* Coding spec must be in a comment, and that comment must be |
| 220 | * the only statement on the source code line. */ |
| 221 | for (i = 0; i < size - 6; i++) { |
| 222 | if (s[i] == '#') |
| 223 | break; |
| 224 | if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014') |
| 225 | return NULL; |
| 226 | } |
| 227 | for (; i < size - 6; i++) { /* XXX inefficient search */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 228 | const char* t = s + i; |
| 229 | if (strncmp(t, "coding", 6) == 0) { |
| 230 | const char* begin = NULL; |
| 231 | t += 6; |
| 232 | if (t[0] != ':' && t[0] != '=') |
| 233 | continue; |
| 234 | do { |
| 235 | t++; |
| 236 | } while (t[0] == '\x20' || t[0] == '\t'); |
| 237 | |
| 238 | begin = t; |
Neal Norwitz | 30b5c5d | 2005-12-19 06:05:18 +0000 | [diff] [blame] | 239 | while (isalnum(Py_CHARMASK(t[0])) || |
Neal Norwitz | e08e1bc | 2002-11-02 20:43:25 +0000 | [diff] [blame] | 240 | t[0] == '-' || t[0] == '_' || t[0] == '.') |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 241 | t++; |
| 242 | |
| 243 | if (begin < t) { |
| 244 | char* r = new_string(begin, t - begin); |
| 245 | char* q = get_normal_name(r); |
| 246 | if (r != q) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 247 | PyMem_FREE(r); |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 248 | r = new_string(q, strlen(q)); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 249 | } |
| 250 | return r; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | /* Check whether the line contains a coding spec. If it does, |
| 258 | invoke the set_readline function for the new encoding. |
| 259 | This function receives the tok_state and the new encoding. |
| 260 | Return 1 on success, 0 on failure. */ |
| 261 | |
| 262 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 263 | check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok, |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 264 | int set_readline(struct tok_state *, const char *)) |
| 265 | { |
Tim Peters | 17db21f | 2002-09-03 15:39:58 +0000 | [diff] [blame] | 266 | char * cs; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 267 | int r = 1; |
Tim Peters | 17db21f | 2002-09-03 15:39:58 +0000 | [diff] [blame] | 268 | |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 269 | if (tok->cont_line) |
| 270 | /* It's a continuation line, so it can't be a coding spec. */ |
| 271 | return 1; |
Tim Peters | 17db21f | 2002-09-03 15:39:58 +0000 | [diff] [blame] | 272 | cs = get_coding_spec(line, size); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 273 | if (cs != NULL) { |
| 274 | tok->read_coding_spec = 1; |
| 275 | if (tok->encoding == NULL) { |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 276 | assert(tok->decoding_state == STATE_RAW); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 277 | if (strcmp(cs, "utf-8") == 0 || |
| 278 | strcmp(cs, "iso-8859-1") == 0) { |
| 279 | tok->encoding = cs; |
| 280 | } else { |
| 281 | r = set_readline(tok, cs); |
| 282 | if (r) { |
| 283 | tok->encoding = cs; |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 284 | tok->decoding_state = STATE_NORMAL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 285 | } |
Neal Norwitz | c0d5faa | 2005-10-21 06:05:33 +0000 | [diff] [blame] | 286 | else |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 287 | PyMem_FREE(cs); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 288 | } |
| 289 | } else { /* then, compare cs with BOM */ |
| 290 | r = (strcmp(tok->encoding, cs) == 0); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 291 | PyMem_FREE(cs); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 292 | } |
| 293 | } |
Neal Norwitz | db83eb3 | 2005-12-18 05:29:30 +0000 | [diff] [blame] | 294 | if (!r) { |
| 295 | cs = tok->encoding; |
| 296 | if (!cs) |
| 297 | cs = "with BOM"; |
| 298 | PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs); |
| 299 | } |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 300 | return r; |
| 301 | } |
| 302 | |
| 303 | /* See whether the file starts with a BOM. If it does, |
| 304 | invoke the set_readline function with the new encoding. |
| 305 | Return 1 on success, 0 on failure. */ |
| 306 | |
| 307 | static int |
| 308 | check_bom(int get_char(struct tok_state *), |
| 309 | void unget_char(int, struct tok_state *), |
| 310 | int set_readline(struct tok_state *, const char *), |
| 311 | struct tok_state *tok) |
| 312 | { |
| 313 | int ch = get_char(tok); |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 314 | tok->decoding_state = STATE_RAW; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 315 | if (ch == EOF) { |
| 316 | return 1; |
| 317 | } else if (ch == 0xEF) { |
Amaury Forgeot d'Arc | af59346 | 2007-11-22 20:53:01 +0000 | [diff] [blame] | 318 | ch = get_char(tok); |
| 319 | if (ch != 0xBB) { |
| 320 | unget_char(ch, tok); |
| 321 | unget_char(0xEF, tok); |
| 322 | /* any token beginning with '\xEF' is a bad token */ |
| 323 | return 1; |
| 324 | } |
| 325 | ch = get_char(tok); |
| 326 | if (ch != 0xBF) { |
| 327 | unget_char(ch, tok); |
| 328 | unget_char(0xBB, tok); |
| 329 | unget_char(0xEF, tok); |
| 330 | /* any token beginning with '\xEF' is a bad token */ |
| 331 | return 1; |
| 332 | } |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 333 | #if 0 |
| 334 | /* Disable support for UTF-16 BOMs until a decision |
| 335 | is made whether this needs to be supported. */ |
| 336 | } else if (ch == 0xFE) { |
| 337 | ch = get_char(tok); if (ch != 0xFF) goto NON_BOM; |
| 338 | if (!set_readline(tok, "utf-16-be")) return 0; |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 339 | tok->decoding_state = STATE_NORMAL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 340 | } else if (ch == 0xFF) { |
| 341 | ch = get_char(tok); if (ch != 0xFE) goto NON_BOM; |
| 342 | if (!set_readline(tok, "utf-16-le")) return 0; |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 343 | tok->decoding_state = STATE_NORMAL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 344 | #endif |
| 345 | } else { |
| 346 | unget_char(ch, tok); |
| 347 | return 1; |
| 348 | } |
Neal Norwitz | dee2fd5 | 2005-11-16 05:12:59 +0000 | [diff] [blame] | 349 | if (tok->encoding != NULL) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 350 | PyMem_FREE(tok->encoding); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 351 | tok->encoding = new_string("utf-8", 5); /* resulting is in utf-8 */ |
Amaury Forgeot d'Arc | af59346 | 2007-11-22 20:53:01 +0000 | [diff] [blame] | 352 | /* No need to set_readline: input is already utf-8 */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | /* 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] | 357 | Return NULL on failure, else S. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 358 | |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 359 | On entry, tok->decoding_buffer will be one of: |
| 360 | 1) NULL: need to call tok->decoding_readline to get a new line |
| 361 | 2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and |
| 362 | stored the result in tok->decoding_buffer |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 363 | 3) PyByteArrayObject *: previous call to fp_readl did not have enough room |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 364 | (in the s buffer) to copy entire contents of the line read |
| 365 | by tok->decoding_readline. tok->decoding_buffer has the overflow. |
| 366 | In this case, fp_readl is called in a loop (with an expanded buffer) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 367 | until the buffer ends with a '\n' (or until the end of the file is |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 368 | reached): see tok_nextc and its calls to decoding_fgets. |
| 369 | */ |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 370 | |
| 371 | static char * |
| 372 | fp_readl(char *s, int size, struct tok_state *tok) |
| 373 | { |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 374 | PyObject* bufobj; |
Guido van Rossum | ccf4f0f | 2007-05-09 23:38:34 +0000 | [diff] [blame] | 375 | const char *buf; |
| 376 | Py_ssize_t buflen; |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 377 | |
| 378 | /* Ask for one less byte so we can terminate it */ |
| 379 | assert(size > 0); |
| 380 | size--; |
| 381 | |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 382 | if (tok->decoding_buffer) { |
| 383 | bufobj = tok->decoding_buffer; |
| 384 | Py_INCREF(bufobj); |
| 385 | } |
| 386 | else |
| 387 | { |
Guido van Rossum | ccf4f0f | 2007-05-09 23:38:34 +0000 | [diff] [blame] | 388 | bufobj = PyObject_CallObject(tok->decoding_readline, NULL); |
| 389 | if (bufobj == NULL) |
Neal Norwitz | 41eaedd | 2007-08-12 00:03:22 +0000 | [diff] [blame] | 390 | goto error; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 391 | } |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 392 | if (PyUnicode_CheckExact(bufobj)) |
| 393 | { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame^] | 394 | buf = _PyUnicode_AsStringAndSize(bufobj, &buflen); |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 395 | if (buf == NULL) { |
| 396 | goto error; |
| 397 | } |
Neal Norwitz | 41eaedd | 2007-08-12 00:03:22 +0000 | [diff] [blame] | 398 | } |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 399 | else |
| 400 | { |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 401 | buf = PyByteArray_AsString(bufobj); |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 402 | if (buf == NULL) { |
| 403 | goto error; |
| 404 | } |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 405 | buflen = PyByteArray_GET_SIZE(bufobj); |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | Py_XDECREF(tok->decoding_buffer); |
Guido van Rossum | ccf4f0f | 2007-05-09 23:38:34 +0000 | [diff] [blame] | 409 | if (buflen > size) { |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 410 | /* Too many chars, the rest goes into tok->decoding_buffer */ |
Christian Heimes | 9c4756e | 2008-05-26 13:22:05 +0000 | [diff] [blame] | 411 | tok->decoding_buffer = PyByteArray_FromStringAndSize(buf+size, |
Guido van Rossum | ccf4f0f | 2007-05-09 23:38:34 +0000 | [diff] [blame] | 412 | buflen-size); |
| 413 | if (tok->decoding_buffer == NULL) |
Neal Norwitz | 41eaedd | 2007-08-12 00:03:22 +0000 | [diff] [blame] | 414 | goto error; |
Guido van Rossum | ccf4f0f | 2007-05-09 23:38:34 +0000 | [diff] [blame] | 415 | buflen = size; |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 416 | } |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 417 | else |
| 418 | tok->decoding_buffer = NULL; |
| 419 | |
Guido van Rossum | ccf4f0f | 2007-05-09 23:38:34 +0000 | [diff] [blame] | 420 | memcpy(s, buf, buflen); |
| 421 | s[buflen] = '\0'; |
Neal Norwitz | 41eaedd | 2007-08-12 00:03:22 +0000 | [diff] [blame] | 422 | if (buflen == 0) /* EOF */ |
| 423 | s = NULL; |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 424 | Py_DECREF(bufobj); |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 425 | return s; |
Neal Norwitz | 41eaedd | 2007-08-12 00:03:22 +0000 | [diff] [blame] | 426 | |
| 427 | error: |
Amaury Forgeot d'Arc | 65f9ace | 2007-11-15 23:19:43 +0000 | [diff] [blame] | 428 | Py_XDECREF(bufobj); |
Neal Norwitz | 41eaedd | 2007-08-12 00:03:22 +0000 | [diff] [blame] | 429 | return error_ret(tok); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* Set the readline function for TOK to a StreamReader's |
| 433 | readline function. The StreamReader is named ENC. |
| 434 | |
| 435 | This function is called from check_bom and check_coding_spec. |
| 436 | |
| 437 | ENC is usually identical to the future value of tok->encoding, |
| 438 | except for the (currently unsupported) case of UTF-16. |
| 439 | |
| 440 | Return 1 on success, 0 on failure. */ |
| 441 | |
| 442 | static int |
| 443 | fp_setreadl(struct tok_state *tok, const char* enc) |
| 444 | { |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 445 | PyObject *readline = NULL, *stream = NULL, *io = NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 446 | |
Christian Heimes | 819b8bf | 2008-01-03 23:05:47 +0000 | [diff] [blame] | 447 | io = PyImport_ImportModuleNoBlock("io"); |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 448 | if (io == NULL) |
| 449 | goto cleanup; |
| 450 | |
| 451 | stream = PyObject_CallMethod(io, "open", "ssis", |
| 452 | tok->filename, "r", -1, enc); |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 453 | if (stream == NULL) |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 454 | goto cleanup; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 455 | |
Neal Norwitz | 41eaedd | 2007-08-12 00:03:22 +0000 | [diff] [blame] | 456 | Py_XDECREF(tok->decoding_readline); |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 457 | readline = PyObject_GetAttrString(stream, "readline"); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 458 | tok->decoding_readline = readline; |
Guido van Rossum | 9cbfffd | 2007-06-07 00:54:15 +0000 | [diff] [blame] | 459 | |
| 460 | cleanup: |
| 461 | Py_XDECREF(stream); |
| 462 | Py_XDECREF(io); |
Neal Norwitz | 41eaedd | 2007-08-12 00:03:22 +0000 | [diff] [blame] | 463 | return readline != NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* Fetch the next byte from TOK. */ |
| 467 | |
| 468 | static int fp_getc(struct tok_state *tok) { |
| 469 | return getc(tok->fp); |
| 470 | } |
| 471 | |
| 472 | /* Unfetch the last byte back into TOK. */ |
| 473 | |
| 474 | static void fp_ungetc(int c, struct tok_state *tok) { |
| 475 | ungetc(c, tok->fp); |
| 476 | } |
| 477 | |
Martin v. Löwis | 447d33e | 2007-07-29 18:10:01 +0000 | [diff] [blame] | 478 | /* Check whether the characters at s start a valid |
| 479 | UTF-8 sequence. Return the number of characters forming |
| 480 | the sequence if yes, 0 if not. */ |
| 481 | static int valid_utf8(const unsigned char* s) |
| 482 | { |
| 483 | int expected = 0; |
| 484 | int length; |
| 485 | if (*s < 0x80) |
| 486 | /* single-byte code */ |
| 487 | return 1; |
| 488 | if (*s < 0xc0) |
| 489 | /* following byte */ |
| 490 | return 0; |
| 491 | if (*s < 0xE0) |
| 492 | expected = 1; |
| 493 | else if (*s < 0xF0) |
| 494 | expected = 2; |
| 495 | else if (*s < 0xF8) |
| 496 | expected = 3; |
| 497 | else |
| 498 | return 0; |
| 499 | length = expected + 1; |
| 500 | for (; expected; expected--) |
| 501 | if (s[expected] < 0x80 || s[expected] >= 0xC0) |
| 502 | return 0; |
| 503 | return length; |
| 504 | } |
| 505 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 506 | /* Read a line of input from TOK. Determine encoding |
| 507 | if necessary. */ |
| 508 | |
| 509 | static char * |
| 510 | decoding_fgets(char *s, int size, struct tok_state *tok) |
| 511 | { |
Martin v. Löwis | 2863c10 | 2002-08-07 15:18:57 +0000 | [diff] [blame] | 512 | char *line = NULL; |
Martin v. Löwis | 6cba256 | 2006-02-28 22:41:29 +0000 | [diff] [blame] | 513 | int badchar = 0; |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 514 | for (;;) { |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 515 | if (tok->decoding_state == STATE_NORMAL) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 516 | /* We already have a codec associated with |
| 517 | this input. */ |
| 518 | line = fp_readl(s, size, tok); |
| 519 | break; |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 520 | } else if (tok->decoding_state == STATE_RAW) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 521 | /* We want a 'raw' read. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 522 | line = Py_UniversalNewlineFgets(s, size, |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 523 | tok->fp, NULL); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 524 | break; |
| 525 | } else { |
| 526 | /* We have not yet determined the encoding. |
| 527 | If an encoding is found, use the file-pointer |
| 528 | reader functions from now on. */ |
| 529 | if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok)) |
| 530 | return error_ret(tok); |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 531 | assert(tok->decoding_state != STATE_INIT); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 532 | } |
Martin v. Löwis | cd280fb | 2002-08-04 18:28:44 +0000 | [diff] [blame] | 533 | } |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 534 | if (line != NULL && tok->lineno < 2 && !tok->read_coding_spec) { |
| 535 | if (!check_coding_spec(line, strlen(line), tok, fp_setreadl)) { |
| 536 | return error_ret(tok); |
| 537 | } |
| 538 | } |
| 539 | #ifndef PGEN |
Martin v. Löwis | 447d33e | 2007-07-29 18:10:01 +0000 | [diff] [blame] | 540 | /* The default encoding is UTF-8, so make sure we don't have any |
| 541 | non-UTF-8 sequences in it. */ |
Martin v. Löwis | 6cba256 | 2006-02-28 22:41:29 +0000 | [diff] [blame] | 542 | if (line && !tok->encoding) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 543 | unsigned char *c; |
Martin v. Löwis | 447d33e | 2007-07-29 18:10:01 +0000 | [diff] [blame] | 544 | int length; |
| 545 | for (c = (unsigned char *)line; *c; c += length) |
| 546 | if (!(length = valid_utf8(c))) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 547 | badchar = *c; |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | if (badchar) { |
Marc-André Lemburg | 1fb1400 | 2003-02-17 18:31:57 +0000 | [diff] [blame] | 552 | char buf[500]; |
Martin v. Löwis | 725bb23 | 2002-08-05 01:49:16 +0000 | [diff] [blame] | 553 | /* Need to add 1 to the line number, since this line |
| 554 | has not been counted, yet. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 555 | sprintf(buf, |
Martin v. Löwis | 447d33e | 2007-07-29 18:10:01 +0000 | [diff] [blame] | 556 | "Non-UTF-8 code starting with '\\x%.2x' " |
Marc-André Lemburg | 1fb1400 | 2003-02-17 18:31:57 +0000 | [diff] [blame] | 557 | "in file %.200s on line %i, " |
| 558 | "but no encoding declared; " |
Guido van Rossum | 21b731f | 2007-08-30 00:10:46 +0000 | [diff] [blame] | 559 | "see http://python.org/dev/peps/pep-0263/ for details", |
Marc-André Lemburg | 1fb1400 | 2003-02-17 18:31:57 +0000 | [diff] [blame] | 560 | badchar, tok->filename, tok->lineno + 1); |
Martin v. Löwis | 6cba256 | 2006-02-28 22:41:29 +0000 | [diff] [blame] | 561 | PyErr_SetString(PyExc_SyntaxError, buf); |
| 562 | return error_ret(tok); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 563 | } |
| 564 | #endif |
| 565 | return line; |
| 566 | } |
| 567 | |
| 568 | static int |
| 569 | decoding_feof(struct tok_state *tok) |
| 570 | { |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 571 | if (tok->decoding_state != STATE_NORMAL) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 572 | return feof(tok->fp); |
| 573 | } else { |
| 574 | PyObject* buf = tok->decoding_buffer; |
| 575 | if (buf == NULL) { |
Walter Dörwald | c1f5fff | 2005-07-12 21:53:43 +0000 | [diff] [blame] | 576 | buf = PyObject_CallObject(tok->decoding_readline, NULL); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 577 | if (buf == NULL) { |
| 578 | error_ret(tok); |
| 579 | return 1; |
| 580 | } else { |
| 581 | tok->decoding_buffer = buf; |
| 582 | } |
| 583 | } |
| 584 | return PyObject_Length(buf) == 0; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /* Fetch a byte from TOK, using the string buffer. */ |
| 589 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 590 | static int |
| 591 | buf_getc(struct tok_state *tok) { |
Just van Rossum | f032f86 | 2003-02-09 20:38:48 +0000 | [diff] [blame] | 592 | return Py_CHARMASK(*tok->str++); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* Unfetch a byte from TOK, using the string buffer. */ |
| 596 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 597 | static void |
| 598 | buf_ungetc(int c, struct tok_state *tok) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 599 | tok->str--; |
Just van Rossum | f032f86 | 2003-02-09 20:38:48 +0000 | [diff] [blame] | 600 | 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] | 601 | } |
| 602 | |
| 603 | /* Set the readline function for TOK to ENC. For the string-based |
| 604 | tokenizer, this means to just record the encoding. */ |
| 605 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 606 | static int |
| 607 | buf_setreadl(struct tok_state *tok, const char* enc) { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 608 | tok->enc = enc; |
| 609 | return 1; |
| 610 | } |
| 611 | |
| 612 | /* Return a UTF-8 encoding Python string object from the |
| 613 | C byte string STR, which is encoded with ENC. */ |
| 614 | |
| 615 | static PyObject * |
| 616 | translate_into_utf8(const char* str, const char* enc) { |
| 617 | PyObject *utf8; |
| 618 | PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL); |
| 619 | if (buf == NULL) |
| 620 | return NULL; |
| 621 | utf8 = PyUnicode_AsUTF8String(buf); |
| 622 | Py_DECREF(buf); |
| 623 | return utf8; |
| 624 | } |
| 625 | |
| 626 | /* Decode a byte string STR for use as the buffer of TOK. |
| 627 | Look for encoding declarations inside STR, and record them |
| 628 | inside TOK. */ |
| 629 | |
| 630 | static const char * |
| 631 | decode_str(const char *str, struct tok_state *tok) |
| 632 | { |
| 633 | PyObject* utf8 = NULL; |
| 634 | const char *s; |
Christian Heimes | 1af737c | 2008-01-23 08:24:23 +0000 | [diff] [blame] | 635 | const char *newl[2] = {NULL, NULL}; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 636 | int lineno = 0; |
| 637 | tok->enc = NULL; |
| 638 | tok->str = str; |
| 639 | if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok)) |
Neal Norwitz | dee2fd5 | 2005-11-16 05:12:59 +0000 | [diff] [blame] | 640 | return error_ret(tok); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 641 | str = tok->str; /* string after BOM if any */ |
Tim Peters | 2c3f9c6 | 2002-08-04 17:58:34 +0000 | [diff] [blame] | 642 | assert(str); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 643 | if (tok->enc != NULL) { |
| 644 | utf8 = translate_into_utf8(str, tok->enc); |
| 645 | if (utf8 == NULL) |
Neal Norwitz | dee2fd5 | 2005-11-16 05:12:59 +0000 | [diff] [blame] | 646 | return error_ret(tok); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 647 | str = PyBytes_AsString(utf8); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 648 | } |
| 649 | for (s = str;; s++) { |
| 650 | if (*s == '\0') break; |
| 651 | else if (*s == '\n') { |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 652 | assert(lineno < 2); |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 653 | newl[lineno] = s; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 654 | lineno++; |
| 655 | if (lineno == 2) break; |
| 656 | } |
| 657 | } |
| 658 | tok->enc = NULL; |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 659 | /* need to check line 1 and 2 separately since check_coding_spec |
| 660 | assumes a single line as input */ |
| 661 | if (newl[0]) { |
| 662 | if (!check_coding_spec(str, newl[0] - str, tok, buf_setreadl)) |
| 663 | return error_ret(tok); |
| 664 | if (tok->enc == NULL && newl[1]) { |
| 665 | if (!check_coding_spec(newl[0]+1, newl[1] - newl[0], |
| 666 | tok, buf_setreadl)) |
| 667 | return error_ret(tok); |
| 668 | } |
| 669 | } |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 670 | if (tok->enc != NULL) { |
| 671 | assert(utf8 == NULL); |
| 672 | utf8 = translate_into_utf8(str, tok->enc); |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 673 | if (utf8 == NULL) { |
| 674 | PyErr_Format(PyExc_SyntaxError, |
| 675 | "unknown encoding: %s", tok->enc); |
Neal Norwitz | dee2fd5 | 2005-11-16 05:12:59 +0000 | [diff] [blame] | 676 | return error_ret(tok); |
Neal Norwitz | 40d3781 | 2005-10-02 01:48:49 +0000 | [diff] [blame] | 677 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 678 | str = PyBytes_AS_STRING(utf8); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 679 | } |
| 680 | assert(tok->decoding_buffer == NULL); |
| 681 | tok->decoding_buffer = utf8; /* CAUTION */ |
| 682 | return str; |
| 683 | } |
| 684 | |
| 685 | #endif /* PGEN */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 686 | |
| 687 | /* Set up tokenizer for string */ |
| 688 | |
| 689 | struct tok_state * |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 690 | PyTokenizer_FromString(const char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 691 | { |
| 692 | struct tok_state *tok = tok_new(); |
| 693 | if (tok == NULL) |
| 694 | return NULL; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 695 | str = (char *)decode_str(str, tok); |
Neal Norwitz | dee2fd5 | 2005-11-16 05:12:59 +0000 | [diff] [blame] | 696 | if (str == NULL) { |
| 697 | PyTokenizer_Free(tok); |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 698 | return NULL; |
Neal Norwitz | dee2fd5 | 2005-11-16 05:12:59 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Martin v. Löwis | 95292d6 | 2002-12-11 14:04:59 +0000 | [diff] [blame] | 701 | /* XXX: constify members. */ |
| 702 | tok->buf = tok->cur = tok->end = tok->inp = (char*)str; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 703 | return tok; |
| 704 | } |
| 705 | |
| 706 | |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 707 | /* Set up tokenizer for file */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 708 | |
| 709 | struct tok_state * |
Martin v. Löwis | 85bcc66 | 2007-09-04 09:18:06 +0000 | [diff] [blame] | 710 | PyTokenizer_FromFile(FILE *fp, char* enc, char *ps1, char *ps2) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 711 | { |
| 712 | struct tok_state *tok = tok_new(); |
| 713 | if (tok == NULL) |
| 714 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 715 | if ((tok->buf = (char *)PyMem_MALLOC(BUFSIZ)) == NULL) { |
Neal Norwitz | dee2fd5 | 2005-11-16 05:12:59 +0000 | [diff] [blame] | 716 | PyTokenizer_Free(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 717 | return NULL; |
| 718 | } |
| 719 | tok->cur = tok->inp = tok->buf; |
| 720 | tok->end = tok->buf + BUFSIZ; |
| 721 | tok->fp = fp; |
| 722 | tok->prompt = ps1; |
| 723 | tok->nextprompt = ps2; |
Martin v. Löwis | 85bcc66 | 2007-09-04 09:18:06 +0000 | [diff] [blame] | 724 | if (enc != NULL) { |
| 725 | /* Must copy encoding declaration since it |
| 726 | gets copied into the parse tree. */ |
| 727 | tok->encoding = PyMem_MALLOC(strlen(enc)+1); |
| 728 | if (!tok->encoding) { |
| 729 | PyTokenizer_Free(tok); |
| 730 | return NULL; |
| 731 | } |
| 732 | strcpy(tok->encoding, enc); |
Neil Schemenauer | 3f993c3 | 2007-09-21 20:50:26 +0000 | [diff] [blame] | 733 | tok->decoding_state = STATE_NORMAL; |
Martin v. Löwis | 85bcc66 | 2007-09-04 09:18:06 +0000 | [diff] [blame] | 734 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 735 | return tok; |
| 736 | } |
| 737 | |
| 738 | |
| 739 | /* Free a tok_state structure */ |
| 740 | |
| 741 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 742 | PyTokenizer_Free(struct tok_state *tok) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 743 | { |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 744 | if (tok->encoding != NULL) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 745 | PyMem_FREE(tok->encoding); |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 746 | #ifndef PGEN |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 747 | Py_XDECREF(tok->decoding_readline); |
| 748 | Py_XDECREF(tok->decoding_buffer); |
Martin v. Löwis | 1ee99d3 | 2002-08-04 20:10:29 +0000 | [diff] [blame] | 749 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 750 | if (tok->fp != NULL && tok->buf != NULL) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 751 | PyMem_FREE(tok->buf); |
| 752 | PyMem_FREE(tok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 755 | /* Get next char, updating state; error code goes into tok->done */ |
| 756 | |
| 757 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 758 | tok_nextc(register struct tok_state *tok) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 759 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 760 | for (;;) { |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 761 | if (tok->cur != tok->inp) { |
Guido van Rossum | cf57d8b | 1998-01-19 22:07:46 +0000 | [diff] [blame] | 762 | return Py_CHARMASK(*tok->cur++); /* Fast path */ |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 763 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 764 | if (tok->done != E_OK) |
| 765 | return EOF; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 766 | if (tok->fp == NULL) { |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 767 | char *end = strchr(tok->inp, '\n'); |
| 768 | if (end != NULL) |
| 769 | end++; |
| 770 | else { |
| 771 | end = strchr(tok->inp, '\0'); |
| 772 | if (end == tok->inp) { |
| 773 | tok->done = E_EOF; |
| 774 | return EOF; |
| 775 | } |
| 776 | } |
| 777 | if (tok->start == NULL) |
| 778 | tok->buf = tok->cur; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 779 | tok->line_start = tok->cur; |
Guido van Rossum | 1a817c0 | 1994-09-19 08:06:25 +0000 | [diff] [blame] | 780 | tok->lineno++; |
| 781 | tok->inp = end; |
Guido van Rossum | cf57d8b | 1998-01-19 22:07:46 +0000 | [diff] [blame] | 782 | return Py_CHARMASK(*tok->cur++); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 783 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 784 | if (tok->prompt != NULL) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 785 | char *newtok = PyOS_Readline(stdin, stdout, tok->prompt); |
Martin v. Löwis | 85bcc66 | 2007-09-04 09:18:06 +0000 | [diff] [blame] | 786 | #ifndef PGEN |
| 787 | if (tok->encoding && newtok && *newtok) { |
| 788 | /* Recode to UTF-8 */ |
| 789 | Py_ssize_t buflen; |
| 790 | const char* buf; |
| 791 | PyObject *u = translate_into_utf8(newtok, tok->encoding); |
| 792 | PyMem_FREE(newtok); |
| 793 | if (!u) { |
| 794 | tok->done = E_DECODE; |
| 795 | return EOF; |
| 796 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 797 | buflen = PyBytes_GET_SIZE(u); |
| 798 | buf = PyBytes_AS_STRING(u); |
Martin v. Löwis | 85bcc66 | 2007-09-04 09:18:06 +0000 | [diff] [blame] | 799 | if (!buf) { |
| 800 | Py_DECREF(u); |
| 801 | tok->done = E_DECODE; |
| 802 | return EOF; |
| 803 | } |
| 804 | newtok = PyMem_MALLOC(buflen+1); |
| 805 | strcpy(newtok, buf); |
| 806 | Py_DECREF(u); |
| 807 | } |
| 808 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 809 | if (tok->nextprompt != NULL) |
| 810 | tok->prompt = tok->nextprompt; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 811 | if (newtok == NULL) |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 812 | tok->done = E_INTR; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 813 | else if (*newtok == '\0') { |
| 814 | PyMem_FREE(newtok); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 815 | tok->done = E_EOF; |
| 816 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 817 | else if (tok->start != NULL) { |
Guido van Rossum | 6da3434 | 2000-06-28 22:00:02 +0000 | [diff] [blame] | 818 | size_t start = tok->start - tok->buf; |
| 819 | size_t oldlen = tok->cur - tok->buf; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 820 | size_t newlen = oldlen + strlen(newtok); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 821 | char *buf = tok->buf; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 822 | buf = (char *)PyMem_REALLOC(buf, newlen+1); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 823 | tok->lineno++; |
| 824 | if (buf == NULL) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 825 | PyMem_FREE(tok->buf); |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 826 | tok->buf = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 827 | PyMem_FREE(newtok); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 828 | tok->done = E_NOMEM; |
| 829 | return EOF; |
| 830 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 831 | tok->buf = buf; |
| 832 | tok->cur = tok->buf + oldlen; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 833 | tok->line_start = tok->cur; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 834 | strcpy(tok->buf + oldlen, newtok); |
| 835 | PyMem_FREE(newtok); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 836 | tok->inp = tok->buf + newlen; |
| 837 | tok->end = tok->inp + 1; |
| 838 | tok->start = tok->buf + start; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 839 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 840 | else { |
| 841 | tok->lineno++; |
| 842 | if (tok->buf != NULL) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 843 | PyMem_FREE(tok->buf); |
| 844 | tok->buf = newtok; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 845 | tok->line_start = tok->buf; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 846 | tok->cur = tok->buf; |
Martin v. Löwis | 49c5da1 | 2006-03-01 22:49:05 +0000 | [diff] [blame] | 847 | tok->line_start = tok->buf; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 848 | tok->inp = strchr(tok->buf, '\0'); |
| 849 | tok->end = tok->inp + 1; |
| 850 | } |
| 851 | } |
| 852 | else { |
| 853 | int done = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 854 | Py_ssize_t cur = 0; |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 855 | char *pt; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 856 | if (tok->start == NULL) { |
| 857 | if (tok->buf == NULL) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 858 | tok->buf = (char *) |
| 859 | PyMem_MALLOC(BUFSIZ); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 860 | if (tok->buf == NULL) { |
| 861 | tok->done = E_NOMEM; |
| 862 | return EOF; |
| 863 | } |
| 864 | tok->end = tok->buf + BUFSIZ; |
| 865 | } |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 866 | if (decoding_fgets(tok->buf, (int)(tok->end - tok->buf), |
| 867 | tok) == NULL) { |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 868 | tok->done = E_EOF; |
| 869 | done = 1; |
| 870 | } |
| 871 | else { |
| 872 | tok->done = E_OK; |
| 873 | tok->inp = strchr(tok->buf, '\0'); |
| 874 | done = tok->inp[-1] == '\n'; |
| 875 | } |
| 876 | } |
| 877 | else { |
| 878 | cur = tok->cur - tok->buf; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 879 | if (decoding_feof(tok)) { |
Guido van Rossum | 78c0535 | 1995-01-17 16:12:13 +0000 | [diff] [blame] | 880 | tok->done = E_EOF; |
| 881 | done = 1; |
| 882 | } |
| 883 | else |
| 884 | tok->done = E_OK; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 885 | } |
| 886 | tok->lineno++; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 887 | /* Read until '\n' or EOF */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 888 | while (!done) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 889 | Py_ssize_t curstart = tok->start == NULL ? -1 : |
| 890 | tok->start - tok->buf; |
| 891 | Py_ssize_t curvalid = tok->inp - tok->buf; |
| 892 | Py_ssize_t newsize = curvalid + BUFSIZ; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 893 | char *newbuf = tok->buf; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 894 | newbuf = (char *)PyMem_REALLOC(newbuf, |
| 895 | newsize); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 896 | if (newbuf == NULL) { |
| 897 | tok->done = E_NOMEM; |
| 898 | tok->cur = tok->inp; |
| 899 | return EOF; |
| 900 | } |
| 901 | tok->buf = newbuf; |
| 902 | tok->inp = tok->buf + curvalid; |
| 903 | tok->end = tok->buf + newsize; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 904 | tok->start = curstart < 0 ? NULL : |
| 905 | tok->buf + curstart; |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 906 | if (decoding_fgets(tok->inp, |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 907 | (int)(tok->end - tok->inp), |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 908 | tok) == NULL) { |
Thomas Wouters | 7eaf2aa | 2006-03-02 20:41:27 +0000 | [diff] [blame] | 909 | /* Break out early on decoding |
| 910 | errors, as tok->buf will be NULL |
| 911 | */ |
| 912 | if (tok->decoding_erred) |
| 913 | return EOF; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 914 | /* Last line does not end in \n, |
| 915 | fake one */ |
| 916 | strcpy(tok->inp, "\n"); |
| 917 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 918 | tok->inp = strchr(tok->inp, '\0'); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 919 | done = tok->inp[-1] == '\n'; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 920 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 921 | if (tok->buf != NULL) { |
| 922 | tok->cur = tok->buf + cur; |
| 923 | tok->line_start = tok->cur; |
| 924 | /* replace "\r\n" with "\n" */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 925 | /* For Mac leave the \r, giving a syntax error */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 926 | pt = tok->inp - 2; |
| 927 | if (pt >= tok->buf && *pt == '\r') { |
| 928 | *pt++ = '\n'; |
| 929 | *pt = '\0'; |
| 930 | tok->inp = pt; |
| 931 | } |
Guido van Rossum | 2e96eb9 | 1995-06-14 18:26:02 +0000 | [diff] [blame] | 932 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 933 | } |
| 934 | if (tok->done != E_OK) { |
| 935 | if (tok->prompt != NULL) |
Guido van Rossum | 6e73bf4 | 1998-08-25 18:13:04 +0000 | [diff] [blame] | 936 | PySys_WriteStderr("\n"); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 937 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 938 | return EOF; |
| 939 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 940 | } |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 941 | /*NOTREACHED*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | |
| 945 | /* Back-up one character */ |
| 946 | |
| 947 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 948 | tok_backup(register struct tok_state *tok, register int c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 949 | { |
| 950 | if (c != EOF) { |
Guido van Rossum | 588633d | 1994-12-30 15:46:02 +0000 | [diff] [blame] | 951 | if (--tok->cur < tok->buf) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 952 | Py_FatalError("tok_backup: begin of buffer"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 953 | if (*tok->cur != c) |
| 954 | *tok->cur = c; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | |
| 959 | /* Return the token corresponding to a single character */ |
| 960 | |
| 961 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 962 | PyToken_OneChar(int c) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 963 | { |
| 964 | switch (c) { |
| 965 | case '(': return LPAR; |
| 966 | case ')': return RPAR; |
| 967 | case '[': return LSQB; |
| 968 | case ']': return RSQB; |
| 969 | case ':': return COLON; |
| 970 | case ',': return COMMA; |
| 971 | case ';': return SEMI; |
| 972 | case '+': return PLUS; |
| 973 | case '-': return MINUS; |
| 974 | case '*': return STAR; |
| 975 | case '/': return SLASH; |
| 976 | case '|': return VBAR; |
| 977 | case '&': return AMPER; |
| 978 | case '<': return LESS; |
| 979 | case '>': return GREATER; |
| 980 | case '=': return EQUAL; |
| 981 | case '.': return DOT; |
| 982 | case '%': return PERCENT; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 983 | case '{': return LBRACE; |
| 984 | case '}': return RBRACE; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 985 | case '^': return CIRCUMFLEX; |
| 986 | case '~': return TILDE; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 987 | case '@': return AT; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 988 | default: return OP; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 993 | int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 994 | PyToken_TwoChars(int c1, int c2) |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 995 | { |
| 996 | switch (c1) { |
| 997 | case '=': |
| 998 | switch (c2) { |
| 999 | case '=': return EQEQUAL; |
| 1000 | } |
| 1001 | break; |
| 1002 | case '!': |
| 1003 | switch (c2) { |
| 1004 | case '=': return NOTEQUAL; |
| 1005 | } |
| 1006 | break; |
| 1007 | case '<': |
| 1008 | switch (c2) { |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1009 | case '=': return LESSEQUAL; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1010 | case '<': return LEFTSHIFT; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1011 | } |
| 1012 | break; |
| 1013 | case '>': |
| 1014 | switch (c2) { |
| 1015 | case '=': return GREATEREQUAL; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1016 | case '>': return RIGHTSHIFT; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1017 | } |
| 1018 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1019 | case '+': |
| 1020 | switch (c2) { |
| 1021 | case '=': return PLUSEQUAL; |
| 1022 | } |
| 1023 | break; |
| 1024 | case '-': |
| 1025 | switch (c2) { |
| 1026 | case '=': return MINEQUAL; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1027 | case '>': return RARROW; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1028 | } |
| 1029 | break; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1030 | case '*': |
| 1031 | switch (c2) { |
| 1032 | case '*': return DOUBLESTAR; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1033 | case '=': return STAREQUAL; |
| 1034 | } |
| 1035 | break; |
| 1036 | case '/': |
| 1037 | switch (c2) { |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1038 | case '/': return DOUBLESLASH; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1039 | case '=': return SLASHEQUAL; |
| 1040 | } |
| 1041 | break; |
| 1042 | case '|': |
| 1043 | switch (c2) { |
| 1044 | case '=': return VBAREQUAL; |
| 1045 | } |
| 1046 | break; |
| 1047 | case '%': |
| 1048 | switch (c2) { |
| 1049 | case '=': return PERCENTEQUAL; |
| 1050 | } |
| 1051 | break; |
| 1052 | case '&': |
| 1053 | switch (c2) { |
| 1054 | case '=': return AMPEREQUAL; |
| 1055 | } |
| 1056 | break; |
| 1057 | case '^': |
| 1058 | switch (c2) { |
| 1059 | case '=': return CIRCUMFLEXEQUAL; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1060 | } |
| 1061 | break; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1062 | } |
| 1063 | return OP; |
| 1064 | } |
| 1065 | |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1066 | int |
| 1067 | PyToken_ThreeChars(int c1, int c2, int c3) |
| 1068 | { |
| 1069 | switch (c1) { |
| 1070 | case '<': |
| 1071 | switch (c2) { |
| 1072 | case '<': |
| 1073 | switch (c3) { |
| 1074 | case '=': |
| 1075 | return LEFTSHIFTEQUAL; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1076 | } |
| 1077 | break; |
| 1078 | } |
| 1079 | break; |
| 1080 | case '>': |
| 1081 | switch (c2) { |
| 1082 | case '>': |
| 1083 | switch (c3) { |
| 1084 | case '=': |
| 1085 | return RIGHTSHIFTEQUAL; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1086 | } |
| 1087 | break; |
| 1088 | } |
| 1089 | break; |
| 1090 | case '*': |
| 1091 | switch (c2) { |
| 1092 | case '*': |
| 1093 | switch (c3) { |
| 1094 | case '=': |
| 1095 | return DOUBLESTAREQUAL; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1096 | } |
| 1097 | break; |
| 1098 | } |
| 1099 | break; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1100 | case '/': |
| 1101 | switch (c2) { |
| 1102 | case '/': |
| 1103 | switch (c3) { |
| 1104 | case '=': |
| 1105 | return DOUBLESLASHEQUAL; |
| 1106 | } |
| 1107 | break; |
| 1108 | } |
| 1109 | break; |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 1110 | case '.': |
| 1111 | switch (c2) { |
| 1112 | case '.': |
| 1113 | switch (c3) { |
| 1114 | case '.': |
| 1115 | return ELLIPSIS; |
| 1116 | } |
| 1117 | break; |
| 1118 | } |
| 1119 | break; |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1120 | } |
| 1121 | return OP; |
| 1122 | } |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1123 | |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1124 | static int |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 1125 | indenterror(struct tok_state *tok) |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1126 | { |
| 1127 | if (tok->alterror) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1128 | tok->done = E_TABSPACE; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1129 | tok->cur = tok->inp; |
| 1130 | return 1; |
| 1131 | } |
| 1132 | if (tok->altwarning) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1133 | PySys_WriteStderr("%s: inconsistent use of tabs and spaces " |
| 1134 | "in indentation\n", tok->filename); |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1135 | tok->altwarning = 0; |
| 1136 | } |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1140 | #ifdef PGEN |
| 1141 | #define verify_identifier(s,e) 1 |
| 1142 | #else |
| 1143 | /* Verify that the identifier follows PEP 3131. */ |
| 1144 | static int |
| 1145 | verify_identifier(char *start, char *end) |
| 1146 | { |
Guido van Rossum | e3e3701 | 2007-08-29 18:54:41 +0000 | [diff] [blame] | 1147 | PyObject *s; |
| 1148 | int result; |
| 1149 | s = PyUnicode_DecodeUTF8(start, end-start, NULL); |
| 1150 | if (s == NULL) { |
| 1151 | PyErr_Clear(); |
| 1152 | return 0; |
| 1153 | } |
| 1154 | result = PyUnicode_IsIdentifier(s); |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1155 | Py_DECREF(s); |
| 1156 | return result; |
| 1157 | } |
| 1158 | #endif |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1159 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1160 | /* Get next token, after space stripping etc. */ |
| 1161 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1162 | static int |
| 1163 | 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] | 1164 | { |
| 1165 | register int c; |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1166 | int blankline, nonascii; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1167 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1168 | *p_start = *p_end = NULL; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1169 | nextline: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1170 | tok->start = NULL; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1171 | blankline = 0; |
| 1172 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1173 | /* Get indentation level */ |
| 1174 | if (tok->atbol) { |
| 1175 | register int col = 0; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1176 | register int altcol = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1177 | tok->atbol = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1178 | for (;;) { |
| 1179 | c = tok_nextc(tok); |
| 1180 | if (c == ' ') |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1181 | col++, altcol++; |
| 1182 | else if (c == '\t') { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1183 | col = (col/tok->tabsize + 1) * tok->tabsize; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1184 | altcol = (altcol/tok->alttabsize + 1) |
| 1185 | * tok->alttabsize; |
| 1186 | } |
Guido van Rossum | 94d32b1 | 1995-07-07 22:27:27 +0000 | [diff] [blame] | 1187 | else if (c == '\014') /* Control-L (formfeed) */ |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1188 | col = altcol = 0; /* For Emacs users */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1189 | else |
| 1190 | break; |
| 1191 | } |
| 1192 | tok_backup(tok, c); |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1193 | if (c == '#' || c == '\n') { |
| 1194 | /* Lines with only whitespace and/or comments |
| 1195 | shouldn't affect the indentation and are |
| 1196 | not passed to the parser as NEWLINE tokens, |
| 1197 | except *totally* empty lines in interactive |
| 1198 | mode, which signal the end of a command group. */ |
| 1199 | if (col == 0 && c == '\n' && tok->prompt != NULL) |
| 1200 | blankline = 0; /* Let it through */ |
| 1201 | else |
| 1202 | blankline = 1; /* Ignore completely */ |
| 1203 | /* We can't jump back right here since we still |
| 1204 | may need to skip to the end of a comment */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1205 | } |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 1206 | if (!blankline && tok->level == 0) { |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1207 | if (col == tok->indstack[tok->indent]) { |
| 1208 | /* No change */ |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1209 | if (altcol != tok->altindstack[tok->indent]) { |
| 1210 | if (indenterror(tok)) |
| 1211 | return ERRORTOKEN; |
| 1212 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1213 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1214 | else if (col > tok->indstack[tok->indent]) { |
| 1215 | /* Indent -- always one */ |
| 1216 | if (tok->indent+1 >= MAXINDENT) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1217 | tok->done = E_TOODEEP; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1218 | tok->cur = tok->inp; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1219 | return ERRORTOKEN; |
| 1220 | } |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1221 | if (altcol <= tok->altindstack[tok->indent]) { |
| 1222 | if (indenterror(tok)) |
| 1223 | return ERRORTOKEN; |
| 1224 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1225 | tok->pendin++; |
| 1226 | tok->indstack[++tok->indent] = col; |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1227 | tok->altindstack[tok->indent] = altcol; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1228 | } |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1229 | else /* col < tok->indstack[tok->indent] */ { |
| 1230 | /* Dedent -- any number, must be consistent */ |
| 1231 | while (tok->indent > 0 && |
| 1232 | col < tok->indstack[tok->indent]) { |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1233 | tok->pendin--; |
Guido van Rossum | 54758fa | 1998-02-16 22:18:00 +0000 | [diff] [blame] | 1234 | tok->indent--; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1235 | } |
| 1236 | if (col != tok->indstack[tok->indent]) { |
Fred Drake | 85f3639 | 2000-07-11 17:53:00 +0000 | [diff] [blame] | 1237 | tok->done = E_DEDENT; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1238 | tok->cur = tok->inp; |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1239 | return ERRORTOKEN; |
| 1240 | } |
Guido van Rossum | 926f13a | 1998-04-09 21:38:06 +0000 | [diff] [blame] | 1241 | if (altcol != tok->altindstack[tok->indent]) { |
| 1242 | if (indenterror(tok)) |
| 1243 | return ERRORTOKEN; |
| 1244 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1245 | } |
| 1246 | } |
| 1247 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1248 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1249 | tok->start = tok->cur; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1250 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1251 | /* Return pending indents/dedents */ |
| 1252 | if (tok->pendin != 0) { |
| 1253 | if (tok->pendin < 0) { |
| 1254 | tok->pendin++; |
| 1255 | return DEDENT; |
| 1256 | } |
| 1257 | else { |
| 1258 | tok->pendin--; |
| 1259 | return INDENT; |
| 1260 | } |
| 1261 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1262 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1263 | again: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1264 | tok->start = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1265 | /* Skip spaces */ |
| 1266 | do { |
| 1267 | c = tok_nextc(tok); |
Guido van Rossum | 94d32b1 | 1995-07-07 22:27:27 +0000 | [diff] [blame] | 1268 | } while (c == ' ' || c == '\t' || c == '\014'); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1269 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1270 | /* Set start of current token */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1271 | tok->start = tok->cur - 1; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1272 | |
Thomas Wouters | 6caa07b | 2006-04-14 11:33:28 +0000 | [diff] [blame] | 1273 | /* Skip comment */ |
| 1274 | if (c == '#') |
Guido van Rossum | ab5ca15 | 2000-03-31 00:52:27 +0000 | [diff] [blame] | 1275 | while (c != EOF && c != '\n') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1276 | c = tok_nextc(tok); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1277 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1278 | /* Check for EOF and errors now */ |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1279 | if (c == EOF) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1280 | return tok->done == E_EOF ? ENDMARKER : ERRORTOKEN; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1281 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1282 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1283 | /* Identifier (most frequent token!) */ |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1284 | nonascii = 0; |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1285 | if (is_potential_identifier_start(c)) { |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1286 | /* Process b"", r"" and br"" */ |
| 1287 | if (c == 'b' || c == 'B') { |
Guido van Rossum | 5026cb4 | 1997-04-25 17:32:00 +0000 | [diff] [blame] | 1288 | c = tok_nextc(tok); |
| 1289 | if (c == '"' || c == '\'') |
| 1290 | goto letter_quote; |
| 1291 | } |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1292 | if (c == 'r' || c == 'R') { |
| 1293 | c = tok_nextc(tok); |
| 1294 | if (c == '"' || c == '\'') |
| 1295 | goto letter_quote; |
| 1296 | } |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 1297 | while (is_potential_identifier_char(c)) { |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1298 | if (c >= 128) |
| 1299 | nonascii = 1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1300 | c = tok_nextc(tok); |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 1301 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1302 | tok_backup(tok, c); |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1303 | if (nonascii && |
Martin v. Löwis | 4738340 | 2007-08-15 07:32:56 +0000 | [diff] [blame] | 1304 | !verify_identifier(tok->start, tok->cur)) { |
| 1305 | tok->done = E_IDENTIFIER; |
| 1306 | return ERRORTOKEN; |
| 1307 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1308 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1309 | *p_end = tok->cur; |
| 1310 | return NAME; |
| 1311 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1312 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1313 | /* Newline */ |
| 1314 | if (c == '\n') { |
| 1315 | tok->atbol = 1; |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 1316 | if (blankline || tok->level > 0) |
Guido van Rossum | 8c11a5c | 1991-07-27 21:42:56 +0000 | [diff] [blame] | 1317 | goto nextline; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1318 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1319 | *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] | 1320 | tok->cont_line = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1321 | return NEWLINE; |
| 1322 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1323 | |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1324 | /* Period or number starting with period? */ |
| 1325 | if (c == '.') { |
| 1326 | c = tok_nextc(tok); |
| 1327 | if (isdigit(c)) { |
| 1328 | goto fraction; |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 1329 | } else if (c == '.') { |
| 1330 | c = tok_nextc(tok); |
| 1331 | if (c == '.') { |
| 1332 | *p_start = tok->start; |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1333 | *p_end = tok->cur; |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 1334 | return ELLIPSIS; |
| 1335 | } else { |
| 1336 | tok_backup(tok, c); |
| 1337 | } |
| 1338 | tok_backup(tok, '.'); |
| 1339 | } else { |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1340 | tok_backup(tok, c); |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1341 | } |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 1342 | *p_start = tok->start; |
| 1343 | *p_end = tok->cur; |
| 1344 | return DOT; |
Guido van Rossum | baf0ebf | 1991-10-24 14:59:40 +0000 | [diff] [blame] | 1345 | } |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1346 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1347 | /* Number */ |
| 1348 | if (isdigit(c)) { |
| 1349 | if (c == '0') { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1350 | /* Hex, octal or binary -- maybe. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1351 | c = tok_nextc(tok); |
| 1352 | if (c == '.') |
| 1353 | goto fraction; |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1354 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | faa436c | 1996-01-26 18:59:07 +0000 | [diff] [blame] | 1355 | if (c == 'j' || c == 'J') |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1356 | goto imaginary; |
| 1357 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1358 | if (c == 'x' || c == 'X') { |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 1359 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1360 | /* Hex */ |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 1361 | c = tok_nextc(tok); |
| 1362 | if (!isxdigit(c)) { |
| 1363 | tok->done = E_TOKEN; |
| 1364 | tok_backup(tok, c); |
| 1365 | return ERRORTOKEN; |
| 1366 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1367 | do { |
| 1368 | c = tok_nextc(tok); |
| 1369 | } while (isxdigit(c)); |
| 1370 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1371 | else if (c == 'o' || c == 'O') { |
| 1372 | /* Octal */ |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 1373 | c = tok_nextc(tok); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 1374 | if (c < '0' || c >= '8') { |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 1375 | tok->done = E_TOKEN; |
| 1376 | tok_backup(tok, c); |
| 1377 | return ERRORTOKEN; |
| 1378 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1379 | do { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1380 | c = tok_nextc(tok); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1381 | } while ('0' <= c && c < '8'); |
| 1382 | } |
| 1383 | else if (c == 'b' || c == 'B') { |
| 1384 | /* Binary */ |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 1385 | c = tok_nextc(tok); |
| 1386 | if (c != '0' && c != '1') { |
| 1387 | tok->done = E_TOKEN; |
| 1388 | tok_backup(tok, c); |
| 1389 | return ERRORTOKEN; |
| 1390 | } |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1391 | do { |
| 1392 | c = tok_nextc(tok); |
| 1393 | } while (c == '0' || c == '1'); |
| 1394 | } |
| 1395 | else { |
| 1396 | int nonzero = 0; |
| 1397 | /* maybe old-style octal; c is first char of it */ |
| 1398 | /* in any case, allow '0' as a literal */ |
| 1399 | while (c == '0') |
| 1400 | c = tok_nextc(tok); |
| 1401 | while (isdigit(c)) { |
| 1402 | nonzero = 1; |
| 1403 | c = tok_nextc(tok); |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 1404 | } |
| 1405 | if (c == '.') |
| 1406 | goto fraction; |
| 1407 | else if (c == 'e' || c == 'E') |
| 1408 | goto exponent; |
| 1409 | #ifndef WITHOUT_COMPLEX |
| 1410 | else if (c == 'j' || c == 'J') |
| 1411 | goto imaginary; |
| 1412 | #endif |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1413 | else if (nonzero) { |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 1414 | tok->done = E_TOKEN; |
| 1415 | tok_backup(tok, c); |
| 1416 | return ERRORTOKEN; |
| 1417 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1418 | } |
| 1419 | } |
| 1420 | else { |
| 1421 | /* Decimal */ |
| 1422 | do { |
| 1423 | c = tok_nextc(tok); |
| 1424 | } while (isdigit(c)); |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 1425 | { |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 1426 | /* Accept floating point numbers. */ |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1427 | if (c == '.') { |
| 1428 | fraction: |
| 1429 | /* Fraction */ |
| 1430 | do { |
| 1431 | c = tok_nextc(tok); |
| 1432 | } while (isdigit(c)); |
| 1433 | } |
| 1434 | if (c == 'e' || c == 'E') { |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 1435 | exponent: |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1436 | /* Exponent part */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1437 | c = tok_nextc(tok); |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1438 | if (c == '+' || c == '-') |
| 1439 | c = tok_nextc(tok); |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 1440 | if (!isdigit(c)) { |
| 1441 | tok->done = E_TOKEN; |
| 1442 | tok_backup(tok, c); |
| 1443 | return ERRORTOKEN; |
Guido van Rossum | f023c46 | 1991-05-05 20:16:20 +0000 | [diff] [blame] | 1444 | } |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 1445 | do { |
| 1446 | c = tok_nextc(tok); |
| 1447 | } while (isdigit(c)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1448 | } |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1449 | #ifndef WITHOUT_COMPLEX |
Guido van Rossum | faa436c | 1996-01-26 18:59:07 +0000 | [diff] [blame] | 1450 | if (c == 'j' || c == 'J') |
Guido van Rossum | f595fde | 1996-01-12 01:31:58 +0000 | [diff] [blame] | 1451 | /* Imaginary part */ |
| 1452 | imaginary: |
| 1453 | c = tok_nextc(tok); |
| 1454 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | tok_backup(tok, c); |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1458 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1459 | *p_end = tok->cur; |
| 1460 | return NUMBER; |
| 1461 | } |
Guido van Rossum | 24dacb3 | 1997-04-06 03:46:20 +0000 | [diff] [blame] | 1462 | |
| 1463 | letter_quote: |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1464 | /* String */ |
| 1465 | if (c == '\'' || c == '"') { |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1466 | int quote = c; |
| 1467 | int quote_size = 1; /* 1 or 3 */ |
| 1468 | int end_quote_size = 0; |
| 1469 | |
| 1470 | /* Find the quote size and start of string */ |
| 1471 | c = tok_nextc(tok); |
| 1472 | if (c == quote) { |
| 1473 | c = tok_nextc(tok); |
| 1474 | if (c == quote) |
| 1475 | quote_size = 3; |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1476 | else |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1477 | end_quote_size = 1; /* empty string found */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1478 | } |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1479 | if (c != quote) |
| 1480 | tok_backup(tok, c); |
| 1481 | |
| 1482 | /* Get rest of string */ |
| 1483 | while (end_quote_size != quote_size) { |
| 1484 | c = tok_nextc(tok); |
| 1485 | if (c == EOF) { |
| 1486 | if (quote_size == 3) |
| 1487 | tok->done = E_EOFS; |
| 1488 | else |
| 1489 | tok->done = E_EOLS; |
| 1490 | tok->cur = tok->inp; |
| 1491 | return ERRORTOKEN; |
| 1492 | } |
| 1493 | if (quote_size == 1 && c == '\n') { |
| 1494 | tok->done = E_EOLS; |
| 1495 | tok->cur = tok->inp; |
| 1496 | return ERRORTOKEN; |
| 1497 | } |
| 1498 | if (c == quote) |
| 1499 | end_quote_size += 1; |
| 1500 | else { |
| 1501 | end_quote_size = 0; |
| 1502 | if (c == '\\') |
| 1503 | c = tok_nextc(tok); /* skip escaped char */ |
| 1504 | } |
| 1505 | } |
| 1506 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1507 | *p_start = tok->start; |
Guido van Rossum | 8054fad | 1993-10-26 15:19:44 +0000 | [diff] [blame] | 1508 | *p_end = tok->cur; |
| 1509 | return STRING; |
| 1510 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1511 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1512 | /* Line continuation */ |
| 1513 | if (c == '\\') { |
| 1514 | c = tok_nextc(tok); |
| 1515 | if (c != '\n') { |
Martin v. Löwis | 4bf108d | 2005-03-03 11:45:45 +0000 | [diff] [blame] | 1516 | tok->done = E_LINECONT; |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 1517 | tok->cur = tok->inp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1518 | return ERRORTOKEN; |
| 1519 | } |
Martin v. Löwis | f62a89b | 2002-09-03 11:52:44 +0000 | [diff] [blame] | 1520 | tok->cont_line = 1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1521 | goto again; /* Read next line */ |
| 1522 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1523 | |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1524 | /* Check for two-character token */ |
| 1525 | { |
| 1526 | int c2 = tok_nextc(tok); |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 1527 | int token = PyToken_TwoChars(c, c2); |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1528 | if (token != OP) { |
Thomas Wouters | 434d082 | 2000-08-24 20:11:32 +0000 | [diff] [blame] | 1529 | int c3 = tok_nextc(tok); |
| 1530 | int token3 = PyToken_ThreeChars(c, c2, c3); |
| 1531 | if (token3 != OP) { |
| 1532 | token = token3; |
| 1533 | } else { |
| 1534 | tok_backup(tok, c3); |
| 1535 | } |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1536 | *p_start = tok->start; |
Guido van Rossum | fbab905 | 1991-10-20 20:25:03 +0000 | [diff] [blame] | 1537 | *p_end = tok->cur; |
| 1538 | return token; |
| 1539 | } |
| 1540 | tok_backup(tok, c2); |
| 1541 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1542 | |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1543 | /* Keep track of parentheses nesting level */ |
Guido van Rossum | a849b83 | 1993-05-12 11:35:44 +0000 | [diff] [blame] | 1544 | switch (c) { |
| 1545 | case '(': |
| 1546 | case '[': |
| 1547 | case '{': |
| 1548 | tok->level++; |
| 1549 | break; |
| 1550 | case ')': |
| 1551 | case ']': |
| 1552 | case '}': |
| 1553 | tok->level--; |
| 1554 | break; |
| 1555 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1556 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1557 | /* Punctuation character */ |
Guido van Rossum | f4b1a64 | 1994-08-29 12:43:07 +0000 | [diff] [blame] | 1558 | *p_start = tok->start; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1559 | *p_end = tok->cur; |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 1560 | return PyToken_OneChar(c); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Martin v. Löwis | 00f1e3f | 2002-08-04 17:29:52 +0000 | [diff] [blame] | 1563 | int |
| 1564 | PyTokenizer_Get(struct tok_state *tok, char **p_start, char **p_end) |
| 1565 | { |
| 1566 | int result = tok_get(tok, p_start, p_end); |
| 1567 | if (tok->decoding_erred) { |
| 1568 | result = ERRORTOKEN; |
| 1569 | tok->done = E_DECODE; |
| 1570 | } |
| 1571 | return result; |
| 1572 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1573 | |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 1574 | /* Get -*- encoding -*- from a Python file. |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1575 | |
| 1576 | PyTokenizer_FindEncoding returns NULL when it can't find the encoding in |
Guido van Rossum | cf171a7 | 2007-11-16 00:51:45 +0000 | [diff] [blame] | 1577 | the first or second line of the file (in which case the encoding |
Brett Cannon | e453989 | 2007-10-20 03:46:49 +0000 | [diff] [blame] | 1578 | should be assumed to be PyUnicode_GetDefaultEncoding()). |
| 1579 | |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 1580 | The char * returned is malloc'ed via PyMem_MALLOC() and thus must be freed |
| 1581 | by the caller. |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1582 | */ |
| 1583 | char * |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 1584 | PyTokenizer_FindEncoding(int fd) |
| 1585 | { |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1586 | struct tok_state *tok; |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 1587 | FILE *fp; |
| 1588 | char *p_start =NULL , *p_end =NULL , *encoding = NULL; |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1589 | |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 1590 | fd = dup(fd); |
| 1591 | if (fd < 0) { |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1592 | return NULL; |
| 1593 | } |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 1594 | fp = fdopen(fd, "r"); |
| 1595 | if (fp == NULL) { |
| 1596 | return NULL; |
| 1597 | } |
| 1598 | tok = PyTokenizer_FromFile(fp, NULL, NULL, NULL); |
| 1599 | if (tok == NULL) { |
| 1600 | fclose(fp); |
| 1601 | return NULL; |
| 1602 | } |
| 1603 | while (tok->lineno < 2 && tok->done == E_OK) { |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1604 | PyTokenizer_Get(tok, &p_start, &p_end); |
| 1605 | } |
Guido van Rossum | 40d20bc | 2007-10-22 00:09:51 +0000 | [diff] [blame] | 1606 | fclose(fp); |
Brett Cannon | d5ec98c | 2007-10-20 02:54:14 +0000 | [diff] [blame] | 1607 | if (tok->encoding) { |
Brett Cannon | c2954e5 | 2007-10-21 02:45:33 +0000 | [diff] [blame] | 1608 | encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1); |
Brett Cannon | d5ec98c | 2007-10-20 02:54:14 +0000 | [diff] [blame] | 1609 | strcpy(encoding, tok->encoding); |
| 1610 | } |
| 1611 | PyTokenizer_Free(tok); |
Brett Cannon | d5ec98c | 2007-10-20 02:54:14 +0000 | [diff] [blame] | 1612 | return encoding; |
Guido van Rossum | ce3a72a | 2007-10-19 23:16:50 +0000 | [diff] [blame] | 1613 | } |
Thomas Wouters | 89d996e | 2007-09-08 17:39:28 +0000 | [diff] [blame] | 1614 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 1615 | #ifdef Py_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1616 | |
| 1617 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 1618 | tok_dump(int type, char *start, char *end) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1619 | { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 1620 | printf("%s", _PyParser_TokenNames[type]); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1621 | if (type == NAME || type == NUMBER || type == STRING || type == OP) |
| 1622 | printf("(%.*s)", (int)(end - start), start); |
| 1623 | } |
| 1624 | |
| 1625 | #endif |