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