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