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