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