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