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