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