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