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