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