1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Tokenizer implementation */
|
|
|
|
|
2020-02-12 06:17:00 -04:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
2002-04-14 17:12:41 -03:00
|
|
|
#include "Python.h"
|
2021-10-12 03:38:19 -03:00
|
|
|
#include "pycore_call.h" // _PyObject_CallNoArgs()
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2002-08-04 14:29:52 -03:00
|
|
|
#include <assert.h>
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
#include "tokenizer.h"
|
|
|
|
#include "errcode.h"
|
|
|
|
|
2017-11-17 05:25:47 -04:00
|
|
|
/* Alternate tab spacing */
|
|
|
|
#define ALTTABSIZE 1
|
|
|
|
|
2007-06-10 06:51:05 -03:00
|
|
|
#define is_potential_identifier_start(c) (\
|
2010-05-09 12:52:27 -03:00
|
|
|
(c >= 'a' && c <= 'z')\
|
|
|
|
|| (c >= 'A' && c <= 'Z')\
|
|
|
|
|| c == '_'\
|
|
|
|
|| (c >= 128))
|
2007-06-10 06:51:05 -03:00
|
|
|
|
|
|
|
#define is_potential_identifier_char(c) (\
|
2010-05-09 12:52:27 -03:00
|
|
|
(c >= 'a' && c <= 'z')\
|
|
|
|
|| (c >= 'A' && c <= 'Z')\
|
|
|
|
|| (c >= '0' && c <= '9')\
|
|
|
|
|| c == '_'\
|
|
|
|
|| (c >= 128))
|
2007-06-10 06:51:05 -03:00
|
|
|
|
1994-08-29 09:43:07 -03:00
|
|
|
|
1992-02-26 11:24:44 -04:00
|
|
|
/* Don't ever change this -- it would break the portability of Python code */
|
1990-10-14 09:07:46 -03:00
|
|
|
#define TABSIZE 8
|
|
|
|
|
2022-10-06 20:07:17 -03:00
|
|
|
#define MAKE_TOKEN(token_type) token_setup(tok, token, token_type, p_start, p_end)
|
2022-10-07 18:38:35 -03:00
|
|
|
#define MAKE_TYPE_COMMENT_TOKEN(token_type, col_offset, end_col_offset) (\
|
|
|
|
type_comment_token_setup(tok, token, token_type, col_offset, end_col_offset, p_start, p_end))
|
|
|
|
#define ADVANCE_LINENO() \
|
|
|
|
tok->lineno++; \
|
|
|
|
tok->col_offset = 0;
|
2022-10-06 20:07:17 -03:00
|
|
|
|
2023-04-19 17:51:31 -03:00
|
|
|
#define INSIDE_FSTRING(tok) (tok->tok_mode_stack_index > 0)
|
|
|
|
#define INSIDE_FSTRING_EXPR(tok) (tok->curly_bracket_expr_start_depth >= 0)
|
2023-04-19 13:18:16 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
static inline tokenizer_mode* TOK_GET_MODE(struct tok_state* tok) {
|
|
|
|
assert(tok->tok_mode_stack_index >= 0);
|
2023-05-01 16:15:47 -03:00
|
|
|
assert(tok->tok_mode_stack_index < MAXFSTRINGLEVEL);
|
2023-04-19 13:18:16 -03:00
|
|
|
return &(tok->tok_mode_stack[tok->tok_mode_stack_index]);
|
|
|
|
}
|
|
|
|
static inline tokenizer_mode* TOK_NEXT_MODE(struct tok_state* tok) {
|
|
|
|
assert(tok->tok_mode_stack_index >= 0);
|
2023-05-01 16:15:47 -03:00
|
|
|
assert(tok->tok_mode_stack_index + 1 < MAXFSTRINGLEVEL);
|
2023-04-19 13:18:16 -03:00
|
|
|
return &(tok->tok_mode_stack[++tok->tok_mode_stack_index]);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define TOK_GET_MODE(tok) (&(tok->tok_mode_stack[tok->tok_mode_stack_index]))
|
|
|
|
#define TOK_NEXT_MODE(tok) (&(tok->tok_mode_stack[++tok->tok_mode_stack_index]))
|
|
|
|
#endif
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* Forward */
|
2000-07-09 00:09:57 -03:00
|
|
|
static struct tok_state *tok_new(void);
|
|
|
|
static int tok_nextc(struct tok_state *tok);
|
|
|
|
static void tok_backup(struct tok_state *tok, int c);
|
2022-02-08 07:54:37 -04:00
|
|
|
static int syntaxerror(struct tok_state *tok, const char *format, ...);
|
2007-10-19 23:54:14 -03:00
|
|
|
|
2019-01-31 07:40:27 -04:00
|
|
|
/* Spaces in this constant are treated as "zero or more spaces or tabs" when
|
|
|
|
tokenizing. */
|
|
|
|
static const char* type_comment_prefix = "# type: ";
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Create and initialize a new tok_state structure */
|
|
|
|
|
|
|
|
static struct tok_state *
|
2000-07-22 16:20:54 -03:00
|
|
|
tok_new(void)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2020-12-01 04:56:42 -04:00
|
|
|
struct tok_state *tok = (struct tok_state *)PyMem_Malloc(
|
2010-05-09 12:52:27 -03:00
|
|
|
sizeof(struct tok_state));
|
|
|
|
if (tok == NULL)
|
|
|
|
return NULL;
|
2020-02-27 22:44:52 -04:00
|
|
|
tok->buf = tok->cur = tok->inp = NULL;
|
2021-03-13 23:38:40 -04:00
|
|
|
tok->fp_interactive = 0;
|
|
|
|
tok->interactive_src_start = NULL;
|
|
|
|
tok->interactive_src_end = NULL;
|
2020-02-27 22:44:52 -04:00
|
|
|
tok->start = NULL;
|
|
|
|
tok->end = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->done = E_OK;
|
|
|
|
tok->fp = NULL;
|
|
|
|
tok->input = NULL;
|
|
|
|
tok->tabsize = TABSIZE;
|
|
|
|
tok->indent = 0;
|
|
|
|
tok->indstack[0] = 0;
|
|
|
|
tok->atbol = 1;
|
|
|
|
tok->pendin = 0;
|
|
|
|
tok->prompt = tok->nextprompt = NULL;
|
|
|
|
tok->lineno = 0;
|
2022-10-07 18:38:35 -03:00
|
|
|
tok->starting_col_offset = -1;
|
|
|
|
tok->col_offset = -1;
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->level = 0;
|
|
|
|
tok->altindstack[0] = 0;
|
|
|
|
tok->decoding_state = STATE_INIT;
|
|
|
|
tok->decoding_erred = 0;
|
|
|
|
tok->enc = NULL;
|
|
|
|
tok->encoding = NULL;
|
|
|
|
tok->cont_line = 0;
|
2011-04-04 19:39:01 -03:00
|
|
|
tok->filename = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->decoding_readline = NULL;
|
|
|
|
tok->decoding_buffer = NULL;
|
2019-01-31 07:40:27 -04:00
|
|
|
tok->type_comments = 0;
|
2019-03-07 16:38:08 -04:00
|
|
|
tok->async_hacks = 0;
|
|
|
|
tok->async_def = 0;
|
|
|
|
tok->async_def_indent = 0;
|
|
|
|
tok->async_def_nl = 0;
|
2021-05-22 19:05:00 -03:00
|
|
|
tok->interactive_underflow = IUNDERFLOW_NORMAL;
|
2021-11-20 22:06:39 -04:00
|
|
|
tok->str = NULL;
|
2022-11-30 07:36:06 -04:00
|
|
|
tok->report_warnings = 1;
|
2023-04-26 22:33:31 -03:00
|
|
|
tok->tok_mode_stack[0] = (tokenizer_mode){.kind =TOK_REGULAR_MODE, .f_string_quote='\0', .f_string_quote_size = 0, .f_string_debug=0};
|
2023-04-19 13:18:16 -03:00
|
|
|
tok->tok_mode_stack_index = 0;
|
|
|
|
tok->tok_report_warnings = 1;
|
2022-05-24 17:35:08 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
tok->debug = _Py_GetConfig()->parser_debug;
|
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
return tok;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2009-11-12 20:17:59 -04:00
|
|
|
static char *
|
2013-07-15 23:15:34 -03:00
|
|
|
new_string(const char *s, Py_ssize_t len, struct tok_state *tok)
|
2009-11-12 20:17:59 -04:00
|
|
|
{
|
2020-12-01 04:56:42 -04:00
|
|
|
char* result = (char *)PyMem_Malloc(len + 1);
|
2013-07-15 23:15:34 -03:00
|
|
|
if (!result) {
|
|
|
|
tok->done = E_NOMEM;
|
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2013-07-15 23:15:34 -03:00
|
|
|
memcpy(result, s, len);
|
|
|
|
result[len] = '\0';
|
2010-05-09 12:52:27 -03:00
|
|
|
return result;
|
2009-11-12 20:17:59 -04:00
|
|
|
}
|
|
|
|
|
2002-08-04 14:29:52 -03:00
|
|
|
static char *
|
|
|
|
error_ret(struct tok_state *tok) /* XXX */
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->decoding_erred = 1;
|
2021-10-13 12:22:14 -03:00
|
|
|
if (tok->fp != NULL && tok->buf != NULL) /* see _PyTokenizer_Free */
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(tok->buf);
|
2020-02-27 22:44:52 -04:00
|
|
|
tok->buf = tok->cur = tok->inp = NULL;
|
|
|
|
tok->start = NULL;
|
|
|
|
tok->end = NULL;
|
2015-11-14 09:10:35 -04:00
|
|
|
tok->done = E_DECODE;
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL; /* as if it were EOF */
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-25 14:01:53 -04:00
|
|
|
static const char *
|
|
|
|
get_normal_name(const char *s) /* for utf-8 and latin-1 */
|
2002-08-04 14:29:52 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
char buf[13];
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 12; i++) {
|
|
|
|
int c = s[i];
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
else if (c == '_')
|
|
|
|
buf[i] = '-';
|
|
|
|
else
|
|
|
|
buf[i] = tolower(c);
|
|
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
if (strcmp(buf, "utf-8") == 0 ||
|
|
|
|
strncmp(buf, "utf-8-", 6) == 0)
|
|
|
|
return "utf-8";
|
|
|
|
else if (strcmp(buf, "latin-1") == 0 ||
|
|
|
|
strcmp(buf, "iso-8859-1") == 0 ||
|
|
|
|
strcmp(buf, "iso-latin-1") == 0 ||
|
|
|
|
strncmp(buf, "latin-1-", 8) == 0 ||
|
|
|
|
strncmp(buf, "iso-8859-1-", 11) == 0 ||
|
|
|
|
strncmp(buf, "iso-latin-1-", 12) == 0)
|
|
|
|
return "iso-8859-1";
|
|
|
|
else
|
|
|
|
return s;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the coding spec in S, or NULL if none is found. */
|
|
|
|
|
2013-07-15 23:15:34 -03:00
|
|
|
static int
|
|
|
|
get_coding_spec(const char *s, char **spec, Py_ssize_t size, struct tok_state *tok)
|
2002-08-04 14:29:52 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_ssize_t i;
|
2013-07-15 23:15:34 -03:00
|
|
|
*spec = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Coding spec must be in a comment, and that comment must be
|
|
|
|
* the only statement on the source code line. */
|
|
|
|
for (i = 0; i < size - 6; i++) {
|
|
|
|
if (s[i] == '#')
|
|
|
|
break;
|
|
|
|
if (s[i] != ' ' && s[i] != '\t' && s[i] != '\014')
|
2013-07-15 23:15:34 -03:00
|
|
|
return 1;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
for (; i < size - 6; i++) { /* XXX inefficient search */
|
|
|
|
const char* t = s + i;
|
2021-03-28 19:48:05 -03:00
|
|
|
if (memcmp(t, "coding", 6) == 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
const char* begin = NULL;
|
|
|
|
t += 6;
|
|
|
|
if (t[0] != ':' && t[0] != '=')
|
|
|
|
continue;
|
|
|
|
do {
|
|
|
|
t++;
|
2021-03-28 19:48:05 -03:00
|
|
|
} while (t[0] == ' ' || t[0] == '\t');
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
begin = t;
|
|
|
|
while (Py_ISALNUM(t[0]) ||
|
|
|
|
t[0] == '-' || t[0] == '_' || t[0] == '.')
|
|
|
|
t++;
|
|
|
|
|
|
|
|
if (begin < t) {
|
2013-07-15 23:15:34 -03:00
|
|
|
char* r = new_string(begin, t - begin, tok);
|
2015-12-25 14:01:53 -04:00
|
|
|
const char* q;
|
2013-07-15 23:15:34 -03:00
|
|
|
if (!r)
|
|
|
|
return 0;
|
2013-07-16 00:50:22 -03:00
|
|
|
q = get_normal_name(r);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (r != q) {
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(r);
|
2013-07-15 23:15:34 -03:00
|
|
|
r = new_string(q, strlen(q), tok);
|
|
|
|
if (!r)
|
|
|
|
return 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2013-07-15 23:15:34 -03:00
|
|
|
*spec = r;
|
2016-03-20 18:36:29 -03:00
|
|
|
break;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-15 23:15:34 -03:00
|
|
|
return 1;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether the line contains a coding spec. If it does,
|
|
|
|
invoke the set_readline function for the new encoding.
|
|
|
|
This function receives the tok_state and the new encoding.
|
|
|
|
Return 1 on success, 0 on failure. */
|
|
|
|
|
|
|
|
static int
|
2006-02-15 13:27:45 -04:00
|
|
|
check_coding_spec(const char* line, Py_ssize_t size, struct tok_state *tok,
|
2010-05-09 12:52:27 -03:00
|
|
|
int set_readline(struct tok_state *, const char *))
|
2002-08-04 14:29:52 -03:00
|
|
|
{
|
2013-07-15 23:15:34 -03:00
|
|
|
char *cs;
|
2014-01-09 12:36:09 -04:00
|
|
|
if (tok->cont_line) {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* It's a continuation line, so it can't be a coding spec. */
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->decoding_state = STATE_NORMAL;
|
2010-05-09 12:52:27 -03:00
|
|
|
return 1;
|
2014-01-09 12:36:09 -04:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
if (!get_coding_spec(line, &cs, size, tok)) {
|
2013-07-15 23:15:34 -03:00
|
|
|
return 0;
|
2021-03-28 19:48:05 -03:00
|
|
|
}
|
2014-01-09 12:36:09 -04:00
|
|
|
if (!cs) {
|
|
|
|
Py_ssize_t i;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
if (line[i] == '#' || line[i] == '\n' || line[i] == '\r')
|
|
|
|
break;
|
|
|
|
if (line[i] != ' ' && line[i] != '\t' && line[i] != '\014') {
|
|
|
|
/* Stop checking coding spec after a line containing
|
|
|
|
* anything except a comment. */
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->decoding_state = STATE_NORMAL;
|
2014-01-09 12:36:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-15 23:15:34 -03:00
|
|
|
return 1;
|
2014-01-09 12:36:09 -04:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->decoding_state = STATE_NORMAL;
|
2013-07-15 23:15:34 -03:00
|
|
|
if (tok->encoding == NULL) {
|
2021-03-28 19:48:05 -03:00
|
|
|
assert(tok->decoding_readline == NULL);
|
|
|
|
if (strcmp(cs, "utf-8") != 0 && !set_readline(tok, cs)) {
|
|
|
|
error_ret(tok);
|
|
|
|
PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
|
|
|
|
PyMem_Free(cs);
|
|
|
|
return 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->encoding = cs;
|
2013-07-15 23:15:34 -03:00
|
|
|
} else { /* then, compare cs with BOM */
|
2021-03-28 19:48:05 -03:00
|
|
|
if (strcmp(tok->encoding, cs) != 0) {
|
|
|
|
error_ret(tok);
|
2013-07-15 23:15:34 -03:00
|
|
|
PyErr_Format(PyExc_SyntaxError,
|
|
|
|
"encoding problem: %s with BOM", cs);
|
2021-03-28 19:48:05 -03:00
|
|
|
PyMem_Free(cs);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(cs);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
return 1;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* See whether the file starts with a BOM. If it does,
|
|
|
|
invoke the set_readline function with the new encoding.
|
|
|
|
Return 1 on success, 0 on failure. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
check_bom(int get_char(struct tok_state *),
|
2010-05-09 12:52:27 -03:00
|
|
|
void unget_char(int, struct tok_state *),
|
|
|
|
int set_readline(struct tok_state *, const char *),
|
|
|
|
struct tok_state *tok)
|
2002-08-04 14:29:52 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
int ch1, ch2, ch3;
|
|
|
|
ch1 = get_char(tok);
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->decoding_state = STATE_SEEK_CODING;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (ch1 == EOF) {
|
|
|
|
return 1;
|
|
|
|
} else if (ch1 == 0xEF) {
|
|
|
|
ch2 = get_char(tok);
|
|
|
|
if (ch2 != 0xBB) {
|
|
|
|
unget_char(ch2, tok);
|
|
|
|
unget_char(ch1, tok);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ch3 = get_char(tok);
|
|
|
|
if (ch3 != 0xBF) {
|
|
|
|
unget_char(ch3, tok);
|
|
|
|
unget_char(ch2, tok);
|
|
|
|
unget_char(ch1, tok);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unget_char(ch1, tok);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (tok->encoding != NULL)
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(tok->encoding);
|
2013-07-15 23:15:34 -03:00
|
|
|
tok->encoding = new_string("utf-8", 5, tok);
|
|
|
|
if (!tok->encoding)
|
|
|
|
return 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
/* No need to set_readline: input is already utf-8 */
|
|
|
|
return 1;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
2021-03-28 19:48:05 -03:00
|
|
|
static int
|
|
|
|
tok_concatenate_interactive_new_line(struct tok_state *tok, const char *line) {
|
2021-03-13 23:38:40 -04:00
|
|
|
assert(tok->fp_interactive);
|
|
|
|
|
|
|
|
if (!line) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_ssize_t current_size = tok->interactive_src_end - tok->interactive_src_start;
|
|
|
|
Py_ssize_t line_size = strlen(line);
|
2022-07-05 13:39:21 -03:00
|
|
|
char last_char = line[line_size > 0 ? line_size - 1 : line_size];
|
|
|
|
if (last_char != '\n') {
|
|
|
|
line_size += 1;
|
|
|
|
}
|
2021-03-13 23:38:40 -04:00
|
|
|
char* new_str = tok->interactive_src_start;
|
|
|
|
|
|
|
|
new_str = PyMem_Realloc(new_str, current_size + line_size + 1);
|
|
|
|
if (!new_str) {
|
|
|
|
if (tok->interactive_src_start) {
|
|
|
|
PyMem_Free(tok->interactive_src_start);
|
|
|
|
}
|
|
|
|
tok->interactive_src_start = NULL;
|
|
|
|
tok->interactive_src_end = NULL;
|
|
|
|
tok->done = E_NOMEM;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
strcpy(new_str + current_size, line);
|
2022-07-05 13:39:21 -03:00
|
|
|
if (last_char != '\n') {
|
|
|
|
/* Last line does not end in \n, fake one */
|
|
|
|
new_str[current_size + line_size - 1] = '\n';
|
|
|
|
new_str[current_size + line_size] = '\0';
|
|
|
|
}
|
2021-03-13 23:38:40 -04:00
|
|
|
tok->interactive_src_start = new_str;
|
|
|
|
tok->interactive_src_end = new_str + current_size + line_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-23 16:06:10 -03:00
|
|
|
/* Traverse and remember all f-string buffers, in order to be able to restore
|
|
|
|
them after reallocating tok->buf */
|
2023-04-19 13:18:16 -03:00
|
|
|
static void
|
2023-04-23 16:06:10 -03:00
|
|
|
remember_fstring_buffers(struct tok_state *tok)
|
2023-04-19 13:18:16 -03:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
tokenizer_mode *mode;
|
|
|
|
|
|
|
|
for (index = tok->tok_mode_stack_index; index >= 0; --index) {
|
|
|
|
mode = &(tok->tok_mode_stack[index]);
|
2023-04-24 22:31:21 -03:00
|
|
|
mode->f_string_start_offset = mode->f_string_start - tok->buf;
|
|
|
|
mode->f_string_multi_line_start_offset = mode->f_string_multi_line_start - tok->buf;
|
2023-04-23 16:06:10 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Traverse and restore all f-string buffers after reallocating tok->buf */
|
|
|
|
static void
|
|
|
|
restore_fstring_buffers(struct tok_state *tok)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
tokenizer_mode *mode;
|
|
|
|
|
|
|
|
for (index = tok->tok_mode_stack_index; index >= 0; --index) {
|
|
|
|
mode = &(tok->tok_mode_stack[index]);
|
2023-04-24 22:31:21 -03:00
|
|
|
mode->f_string_start = tok->buf + mode->f_string_start_offset;
|
|
|
|
mode->f_string_multi_line_start = tok->buf + mode->f_string_multi_line_start_offset;
|
2023-04-19 13:18:16 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 22:33:31 -03:00
|
|
|
static int
|
|
|
|
set_fstring_expr(struct tok_state* tok, struct token *token, char c) {
|
|
|
|
assert(token != NULL);
|
|
|
|
assert(c == '}' || c == ':' || c == '!');
|
|
|
|
tokenizer_mode *tok_mode = TOK_GET_MODE(tok);
|
|
|
|
|
|
|
|
if (!tok_mode->f_string_debug || token->metadata) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *res = PyUnicode_DecodeUTF8(
|
|
|
|
tok_mode->last_expr_buffer,
|
|
|
|
tok_mode->last_expr_size - tok_mode->last_expr_end,
|
|
|
|
NULL
|
|
|
|
);
|
|
|
|
if (!res) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
token->metadata = res;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:18:16 -03:00
|
|
|
static int
|
|
|
|
update_fstring_expr(struct tok_state *tok, char cur)
|
|
|
|
{
|
|
|
|
assert(tok->cur != NULL);
|
|
|
|
|
|
|
|
Py_ssize_t size = strlen(tok->cur);
|
|
|
|
tokenizer_mode *tok_mode = TOK_GET_MODE(tok);
|
|
|
|
|
|
|
|
switch (cur) {
|
2023-04-19 17:51:31 -03:00
|
|
|
case 0:
|
2023-04-19 13:18:16 -03:00
|
|
|
if (!tok_mode->last_expr_buffer || tok_mode->last_expr_end >= 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
char *new_buffer = PyMem_Realloc(
|
|
|
|
tok_mode->last_expr_buffer,
|
|
|
|
tok_mode->last_expr_size + size
|
|
|
|
);
|
|
|
|
if (new_buffer == NULL) {
|
|
|
|
PyMem_Free(tok_mode->last_expr_buffer);
|
2023-04-19 17:51:31 -03:00
|
|
|
goto error;
|
2023-04-19 13:18:16 -03:00
|
|
|
}
|
|
|
|
tok_mode->last_expr_buffer = new_buffer;
|
|
|
|
strncpy(tok_mode->last_expr_buffer + tok_mode->last_expr_size, tok->cur, size);
|
|
|
|
tok_mode->last_expr_size += size;
|
|
|
|
break;
|
2023-04-19 17:51:31 -03:00
|
|
|
case '{':
|
|
|
|
if (tok_mode->last_expr_buffer != NULL) {
|
|
|
|
PyMem_Free(tok_mode->last_expr_buffer);
|
|
|
|
}
|
|
|
|
tok_mode->last_expr_buffer = PyMem_Malloc(size);
|
|
|
|
if (tok_mode->last_expr_buffer == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
tok_mode->last_expr_size = size;
|
|
|
|
tok_mode->last_expr_end = -1;
|
|
|
|
strncpy(tok_mode->last_expr_buffer, tok->cur, size);
|
|
|
|
break;
|
2023-04-19 13:18:16 -03:00
|
|
|
case '}':
|
|
|
|
case '!':
|
|
|
|
case ':':
|
|
|
|
if (tok_mode->last_expr_end == -1) {
|
|
|
|
tok_mode->last_expr_end = strlen(tok->start);
|
|
|
|
}
|
|
|
|
break;
|
2023-04-19 17:51:31 -03:00
|
|
|
default:
|
|
|
|
Py_UNREACHABLE();
|
2023-04-19 13:18:16 -03:00
|
|
|
}
|
|
|
|
return 1;
|
2023-04-19 17:51:31 -03:00
|
|
|
error:
|
|
|
|
tok->done = E_NOMEM;
|
|
|
|
return 0;
|
2023-04-19 13:18:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_fstring_expressions(struct tok_state *tok)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
tokenizer_mode *mode;
|
|
|
|
|
|
|
|
for (index = tok->tok_mode_stack_index; index >= 0; --index) {
|
|
|
|
mode = &(tok->tok_mode_stack[index]);
|
|
|
|
if (mode->last_expr_buffer != NULL) {
|
|
|
|
PyMem_Free(mode->last_expr_buffer);
|
|
|
|
mode->last_expr_buffer = NULL;
|
|
|
|
mode->last_expr_size = 0;
|
|
|
|
mode->last_expr_end = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-04 14:29:52 -03:00
|
|
|
/* Read a line of text from TOK into S, using the stream in TOK.
|
2005-07-12 18:53:43 -03:00
|
|
|
Return NULL on failure, else S.
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2005-07-12 18:53:43 -03:00
|
|
|
On entry, tok->decoding_buffer will be one of:
|
|
|
|
1) NULL: need to call tok->decoding_readline to get a new line
|
|
|
|
2) PyUnicodeObject *: decoding_feof has called tok->decoding_readline and
|
2010-05-09 12:52:27 -03:00
|
|
|
stored the result in tok->decoding_buffer
|
2021-03-28 19:48:05 -03:00
|
|
|
3) PyByteArrayObject *: previous call to tok_readline_recode did not have enough room
|
2010-05-09 12:52:27 -03:00
|
|
|
(in the s buffer) to copy entire contents of the line read
|
|
|
|
by tok->decoding_readline. tok->decoding_buffer has the overflow.
|
2021-03-28 19:48:05 -03:00
|
|
|
In this case, tok_readline_recode is called in a loop (with an expanded buffer)
|
2010-05-09 12:52:27 -03:00
|
|
|
until the buffer ends with a '\n' (or until the end of the file is
|
2021-03-28 19:48:05 -03:00
|
|
|
reached): see tok_nextc and its calls to tok_reserve_buf.
|
2005-07-12 18:53:43 -03:00
|
|
|
*/
|
2002-08-04 14:29:52 -03:00
|
|
|
|
2021-03-28 19:48:05 -03:00
|
|
|
static int
|
|
|
|
tok_reserve_buf(struct tok_state *tok, Py_ssize_t size)
|
2002-08-04 14:29:52 -03:00
|
|
|
{
|
2021-03-28 19:48:05 -03:00
|
|
|
Py_ssize_t cur = tok->cur - tok->buf;
|
|
|
|
Py_ssize_t oldsize = tok->inp - tok->buf;
|
|
|
|
Py_ssize_t newsize = oldsize + Py_MAX(size, oldsize >> 1);
|
|
|
|
if (newsize > tok->end - tok->buf) {
|
|
|
|
char *newbuf = tok->buf;
|
|
|
|
Py_ssize_t start = tok->start == NULL ? -1 : tok->start - tok->buf;
|
2021-06-12 14:53:49 -03:00
|
|
|
Py_ssize_t line_start = tok->start == NULL ? -1 : tok->line_start - tok->buf;
|
|
|
|
Py_ssize_t multi_line_start = tok->multi_line_start - tok->buf;
|
2023-04-23 16:06:10 -03:00
|
|
|
remember_fstring_buffers(tok);
|
2021-03-28 19:48:05 -03:00
|
|
|
newbuf = (char *)PyMem_Realloc(newbuf, newsize);
|
|
|
|
if (newbuf == NULL) {
|
|
|
|
tok->done = E_NOMEM;
|
|
|
|
return 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->buf = newbuf;
|
|
|
|
tok->cur = tok->buf + cur;
|
|
|
|
tok->inp = tok->buf + oldsize;
|
|
|
|
tok->end = tok->buf + newsize;
|
|
|
|
tok->start = start < 0 ? NULL : tok->buf + start;
|
2021-06-12 14:53:49 -03:00
|
|
|
tok->line_start = line_start < 0 ? NULL : tok->buf + line_start;
|
|
|
|
tok->multi_line_start = multi_line_start < 0 ? NULL : tok->buf + multi_line_start;
|
2023-04-23 16:06:10 -03:00
|
|
|
restore_fstring_buffers(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
return 1;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2022-09-27 19:23:42 -03:00
|
|
|
static inline int
|
|
|
|
contains_null_bytes(const char* str, size_t size) {
|
|
|
|
return memchr(str, 0, size) != NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-28 19:48:05 -03:00
|
|
|
static int
|
|
|
|
tok_readline_recode(struct tok_state *tok) {
|
|
|
|
PyObject *line;
|
|
|
|
const char *buf;
|
|
|
|
Py_ssize_t buflen;
|
|
|
|
line = tok->decoding_buffer;
|
|
|
|
if (line == NULL) {
|
|
|
|
line = PyObject_CallNoArgs(tok->decoding_readline);
|
|
|
|
if (line == NULL) {
|
|
|
|
error_ret(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
goto error;
|
2021-03-28 19:48:05 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->decoding_buffer = NULL;
|
2021-03-28 19:48:05 -03:00
|
|
|
}
|
|
|
|
buf = PyUnicode_AsUTF8AndSize(line, &buflen);
|
|
|
|
if (buf == NULL) {
|
|
|
|
error_ret(tok);
|
|
|
|
goto error;
|
|
|
|
}
|
2022-11-20 16:20:03 -04:00
|
|
|
// Make room for the null terminator *and* potentially
|
|
|
|
// an extra newline character that we may need to artificially
|
|
|
|
// add.
|
|
|
|
size_t buffer_size = buflen + 2;
|
|
|
|
if (!tok_reserve_buf(tok, buffer_size)) {
|
2021-03-28 19:48:05 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memcpy(tok->inp, buf, buflen);
|
|
|
|
tok->inp += buflen;
|
|
|
|
*tok->inp = '\0';
|
|
|
|
if (tok->fp_interactive &&
|
|
|
|
tok_concatenate_interactive_new_line(tok, buf) == -1) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(line);
|
|
|
|
return 1;
|
2007-08-11 21:03:22 -03:00
|
|
|
error:
|
2021-03-28 19:48:05 -03:00
|
|
|
Py_XDECREF(line);
|
|
|
|
return 0;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the readline function for TOK to a StreamReader's
|
|
|
|
readline function. The StreamReader is named ENC.
|
|
|
|
|
|
|
|
This function is called from check_bom and check_coding_spec.
|
|
|
|
|
|
|
|
ENC is usually identical to the future value of tok->encoding,
|
|
|
|
except for the (currently unsupported) case of UTF-16.
|
|
|
|
|
|
|
|
Return 1 on success, 0 on failure. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
fp_setreadl(struct tok_state *tok, const char* enc)
|
|
|
|
{
|
2022-06-14 01:15:26 -03:00
|
|
|
PyObject *readline, *open, *stream;
|
2010-10-14 09:04:34 -03:00
|
|
|
int fd;
|
2014-01-09 12:36:09 -04:00
|
|
|
long pos;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2010-10-14 09:04:34 -03:00
|
|
|
fd = fileno(tok->fp);
|
2014-01-09 12:36:09 -04:00
|
|
|
/* Due to buffering the file offset for fd can be different from the file
|
2014-02-28 10:27:29 -04:00
|
|
|
* position of tok->fp. If tok->fp was opened in text mode on Windows,
|
|
|
|
* its file position counts CRLF as one char and can't be directly mapped
|
|
|
|
* to the file offset for fd. Instead we step back one byte and read to
|
|
|
|
* the end of line.*/
|
2014-01-09 12:36:09 -04:00
|
|
|
pos = ftell(tok->fp);
|
2014-02-28 10:27:29 -04:00
|
|
|
if (pos == -1 ||
|
|
|
|
lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) {
|
2010-10-14 09:04:34 -03:00
|
|
|
PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
|
2016-09-13 02:06:58 -03:00
|
|
|
return 0;
|
2010-10-14 09:04:34 -03:00
|
|
|
}
|
|
|
|
|
2022-06-14 01:15:26 -03:00
|
|
|
open = _PyImport_GetModuleAttrString("io", "open");
|
|
|
|
if (open == NULL) {
|
2016-09-13 02:06:58 -03:00
|
|
|
return 0;
|
2022-02-08 16:39:07 -04:00
|
|
|
}
|
2022-06-14 01:15:26 -03:00
|
|
|
stream = PyObject_CallFunction(open, "isisOOO",
|
2010-10-14 09:04:34 -03:00
|
|
|
fd, "r", -1, enc, Py_None, Py_None, Py_False);
|
2022-06-14 01:15:26 -03:00
|
|
|
Py_DECREF(open);
|
2022-02-08 16:39:07 -04:00
|
|
|
if (stream == NULL) {
|
2016-09-13 02:06:58 -03:00
|
|
|
return 0;
|
2022-02-08 16:39:07 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2022-02-08 16:39:07 -04:00
|
|
|
readline = PyObject_GetAttr(stream, &_Py_ID(readline));
|
2016-09-13 02:06:58 -03:00
|
|
|
Py_DECREF(stream);
|
2022-02-08 16:39:07 -04:00
|
|
|
if (readline == NULL) {
|
2016-09-13 02:06:58 -03:00
|
|
|
return 0;
|
2022-02-08 16:39:07 -04:00
|
|
|
}
|
2016-04-06 03:45:48 -03:00
|
|
|
Py_XSETREF(tok->decoding_readline, readline);
|
2016-09-13 02:06:58 -03:00
|
|
|
|
2014-02-28 10:27:29 -04:00
|
|
|
if (pos > 0) {
|
2021-10-11 19:42:23 -03:00
|
|
|
PyObject *bufobj = _PyObject_CallNoArgs(readline);
|
2022-02-08 16:39:07 -04:00
|
|
|
if (bufobj == NULL) {
|
2016-09-13 02:06:58 -03:00
|
|
|
return 0;
|
2022-02-08 16:39:07 -04:00
|
|
|
}
|
2016-09-13 02:06:58 -03:00
|
|
|
Py_DECREF(bufobj);
|
2014-02-28 10:27:29 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2016-09-13 02:06:58 -03:00
|
|
|
return 1;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fetch the next byte from TOK. */
|
|
|
|
|
|
|
|
static int fp_getc(struct tok_state *tok) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return getc(tok->fp);
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unfetch the last byte back into TOK. */
|
|
|
|
|
|
|
|
static void fp_ungetc(int c, struct tok_state *tok) {
|
2010-05-09 12:52:27 -03:00
|
|
|
ungetc(c, tok->fp);
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
2007-07-29 15:10:01 -03:00
|
|
|
/* Check whether the characters at s start a valid
|
|
|
|
UTF-8 sequence. Return the number of characters forming
|
2022-09-07 18:23:54 -03:00
|
|
|
the sequence if yes, 0 if not. The special cases match
|
|
|
|
those in stringlib/codecs.h:utf8_decode.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
valid_utf8(const unsigned char* s)
|
2007-07-29 15:10:01 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
int expected = 0;
|
|
|
|
int length;
|
2022-09-07 18:23:54 -03:00
|
|
|
if (*s < 0x80) {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* single-byte code */
|
|
|
|
return 1;
|
2022-09-07 18:23:54 -03:00
|
|
|
}
|
|
|
|
else if (*s < 0xE0) {
|
|
|
|
/* \xC2\x80-\xDF\xBF -- 0080-07FF */
|
|
|
|
if (*s < 0xC2) {
|
|
|
|
/* invalid sequence
|
|
|
|
\x80-\xBF -- continuation byte
|
|
|
|
\xC0-\xC1 -- fake 0000-007F */
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
expected = 1;
|
2022-09-07 18:23:54 -03:00
|
|
|
}
|
|
|
|
else if (*s < 0xF0) {
|
|
|
|
/* \xE0\xA0\x80-\xEF\xBF\xBF -- 0800-FFFF */
|
|
|
|
if (*s == 0xE0 && *(s + 1) < 0xA0) {
|
|
|
|
/* invalid sequence
|
|
|
|
\xE0\x80\x80-\xE0\x9F\xBF -- fake 0000-0800 */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (*s == 0xED && *(s + 1) >= 0xA0) {
|
|
|
|
/* Decoding UTF-8 sequences in range \xED\xA0\x80-\xED\xBF\xBF
|
|
|
|
will result in surrogates in range D800-DFFF. Surrogates are
|
|
|
|
not valid UTF-8 so they are rejected.
|
|
|
|
See https://www.unicode.org/versions/Unicode5.2.0/ch03.pdf
|
|
|
|
(table 3-7) and http://www.rfc-editor.org/rfc/rfc3629.txt */
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
expected = 2;
|
2022-09-07 18:23:54 -03:00
|
|
|
}
|
|
|
|
else if (*s < 0xF5) {
|
|
|
|
/* \xF0\x90\x80\x80-\xF4\x8F\xBF\xBF -- 10000-10FFFF */
|
|
|
|
if (*(s + 1) < 0x90 ? *s == 0xF0 : *s == 0xF4) {
|
|
|
|
/* invalid sequence -- one of:
|
|
|
|
\xF0\x80\x80\x80-\xF0\x8F\xBF\xBF -- fake 0000-FFFF
|
|
|
|
\xF4\x90\x80\x80- -- 110000- overflow */
|
|
|
|
return 0;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
expected = 3;
|
2022-09-07 18:23:54 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* invalid start byte */
|
2010-05-09 12:52:27 -03:00
|
|
|
return 0;
|
2022-09-07 18:23:54 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
length = expected + 1;
|
|
|
|
for (; expected; expected--)
|
|
|
|
if (s[expected] < 0x80 || s[expected] >= 0xC0)
|
|
|
|
return 0;
|
|
|
|
return length;
|
2007-07-29 15:10:01 -03:00
|
|
|
}
|
|
|
|
|
2021-03-28 19:48:05 -03:00
|
|
|
static int
|
|
|
|
ensure_utf8(char *line, struct tok_state *tok)
|
2002-08-04 14:29:52 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
int badchar = 0;
|
2021-03-28 19:48:05 -03:00
|
|
|
unsigned char *c;
|
|
|
|
int length;
|
|
|
|
for (c = (unsigned char *)line; *c; c += length) {
|
|
|
|
if (!(length = valid_utf8(c))) {
|
|
|
|
badchar = *c;
|
2010-05-09 12:52:27 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (badchar) {
|
2011-04-24 23:03:58 -03:00
|
|
|
PyErr_Format(PyExc_SyntaxError,
|
2021-03-28 19:48:05 -03:00
|
|
|
"Non-UTF-8 code starting with '\\x%.2x' "
|
|
|
|
"in file %U on line %i, "
|
|
|
|
"but no encoding declared; "
|
2022-03-30 08:00:27 -03:00
|
|
|
"see https://peps.python.org/pep-0263/ for details",
|
2022-09-07 18:23:54 -03:00
|
|
|
badchar, tok->filename, tok->lineno);
|
2021-03-28 19:48:05 -03:00
|
|
|
return 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
return 1;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fetch a byte from TOK, using the string buffer. */
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
static int
|
|
|
|
buf_getc(struct tok_state *tok) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return Py_CHARMASK(*tok->str++);
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Unfetch a byte from TOK, using the string buffer. */
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
static void
|
|
|
|
buf_ungetc(int c, struct tok_state *tok) {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->str--;
|
|
|
|
assert(Py_CHARMASK(*tok->str) == c); /* tok->cur may point to read-only segment */
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the readline function for TOK to ENC. For the string-based
|
|
|
|
tokenizer, this means to just record the encoding. */
|
|
|
|
|
2006-04-21 07:40:58 -03:00
|
|
|
static int
|
|
|
|
buf_setreadl(struct tok_state *tok, const char* enc) {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->enc = enc;
|
|
|
|
return 1;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a UTF-8 encoding Python string object from the
|
|
|
|
C byte string STR, which is encoded with ENC. */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
translate_into_utf8(const char* str, const char* enc) {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *utf8;
|
|
|
|
PyObject* buf = PyUnicode_Decode(str, strlen(str), enc, NULL);
|
|
|
|
if (buf == NULL)
|
|
|
|
return NULL;
|
|
|
|
utf8 = PyUnicode_AsUTF8String(buf);
|
|
|
|
Py_DECREF(buf);
|
|
|
|
return utf8;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
2009-11-12 20:17:59 -04:00
|
|
|
|
|
|
|
static char *
|
|
|
|
translate_newlines(const char *s, int exec_input, struct tok_state *tok) {
|
2013-06-04 19:44:00 -03:00
|
|
|
int skip_next_lf = 0;
|
|
|
|
size_t needed_length = strlen(s) + 2, final_length;
|
2010-05-09 12:52:27 -03:00
|
|
|
char *buf, *current;
|
|
|
|
char c = '\0';
|
2020-12-01 04:56:42 -04:00
|
|
|
buf = PyMem_Malloc(needed_length);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (buf == NULL) {
|
|
|
|
tok->done = E_NOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (current = buf; *s; s++, current++) {
|
|
|
|
c = *s;
|
|
|
|
if (skip_next_lf) {
|
|
|
|
skip_next_lf = 0;
|
|
|
|
if (c == '\n') {
|
|
|
|
c = *++s;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c == '\r') {
|
|
|
|
skip_next_lf = 1;
|
|
|
|
c = '\n';
|
|
|
|
}
|
|
|
|
*current = c;
|
|
|
|
}
|
|
|
|
/* If this is exec input, add a newline to the end of the string if
|
|
|
|
there isn't one already. */
|
|
|
|
if (exec_input && c != '\n') {
|
|
|
|
*current = '\n';
|
|
|
|
current++;
|
|
|
|
}
|
|
|
|
*current = '\0';
|
|
|
|
final_length = current - buf + 1;
|
2019-03-19 14:17:58 -03:00
|
|
|
if (final_length < needed_length && final_length) {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* should never fail */
|
2020-12-01 04:56:42 -04:00
|
|
|
char* result = PyMem_Realloc(buf, final_length);
|
2019-03-19 14:17:58 -03:00
|
|
|
if (result == NULL) {
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(buf);
|
2019-03-19 14:17:58 -03:00
|
|
|
}
|
|
|
|
buf = result;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return buf;
|
2009-11-12 20:17:59 -04:00
|
|
|
}
|
|
|
|
|
2002-08-04 14:29:52 -03:00
|
|
|
/* Decode a byte string STR for use as the buffer of TOK.
|
|
|
|
Look for encoding declarations inside STR, and record them
|
|
|
|
inside TOK. */
|
|
|
|
|
2020-02-27 22:44:52 -04:00
|
|
|
static char *
|
2009-11-12 20:17:59 -04:00
|
|
|
decode_str(const char *input, int single, struct tok_state *tok)
|
2002-08-04 14:29:52 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject* utf8 = NULL;
|
2020-02-27 22:44:52 -04:00
|
|
|
char *str;
|
2010-05-09 12:52:27 -03:00
|
|
|
const char *s;
|
|
|
|
const char *newl[2] = {NULL, NULL};
|
|
|
|
int lineno = 0;
|
|
|
|
tok->input = str = translate_newlines(input, single, tok);
|
|
|
|
if (str == NULL)
|
|
|
|
return NULL;
|
|
|
|
tok->enc = NULL;
|
|
|
|
tok->str = str;
|
|
|
|
if (!check_bom(buf_getc, buf_ungetc, buf_setreadl, tok))
|
|
|
|
return error_ret(tok);
|
|
|
|
str = tok->str; /* string after BOM if any */
|
|
|
|
assert(str);
|
|
|
|
if (tok->enc != NULL) {
|
|
|
|
utf8 = translate_into_utf8(str, tok->enc);
|
|
|
|
if (utf8 == NULL)
|
|
|
|
return error_ret(tok);
|
|
|
|
str = PyBytes_AsString(utf8);
|
|
|
|
}
|
|
|
|
for (s = str;; s++) {
|
|
|
|
if (*s == '\0') break;
|
|
|
|
else if (*s == '\n') {
|
|
|
|
assert(lineno < 2);
|
|
|
|
newl[lineno] = s;
|
|
|
|
lineno++;
|
|
|
|
if (lineno == 2) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tok->enc = NULL;
|
|
|
|
/* need to check line 1 and 2 separately since check_coding_spec
|
|
|
|
assumes a single line as input */
|
|
|
|
if (newl[0]) {
|
2021-03-28 19:48:05 -03:00
|
|
|
if (!check_coding_spec(str, newl[0] - str, tok, buf_setreadl)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (tok->enc == NULL && tok->decoding_state != STATE_NORMAL && newl[1]) {
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!check_coding_spec(newl[0]+1, newl[1] - newl[0],
|
|
|
|
tok, buf_setreadl))
|
2021-03-28 19:48:05 -03:00
|
|
|
return NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tok->enc != NULL) {
|
|
|
|
assert(utf8 == NULL);
|
|
|
|
utf8 = translate_into_utf8(str, tok->enc);
|
|
|
|
if (utf8 == NULL)
|
|
|
|
return error_ret(tok);
|
|
|
|
str = PyBytes_AS_STRING(utf8);
|
|
|
|
}
|
|
|
|
assert(tok->decoding_buffer == NULL);
|
|
|
|
tok->decoding_buffer = utf8; /* CAUTION */
|
|
|
|
return str;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Set up tokenizer for string */
|
|
|
|
|
|
|
|
struct tok_state *
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_FromString(const char *str, int exec_input)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
struct tok_state *tok = tok_new();
|
2020-02-27 22:44:52 -04:00
|
|
|
char *decoded;
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (tok == NULL)
|
|
|
|
return NULL;
|
2020-02-27 22:44:52 -04:00
|
|
|
decoded = decode_str(str, exec_input, tok);
|
|
|
|
if (decoded == NULL) {
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_Free(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:44:52 -04:00
|
|
|
tok->buf = tok->cur = tok->inp = decoded;
|
|
|
|
tok->end = decoded;
|
2010-05-09 12:52:27 -03:00
|
|
|
return tok;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2021-03-28 19:48:05 -03:00
|
|
|
/* Set up tokenizer for UTF-8 string */
|
|
|
|
|
2009-03-02 19:31:26 -04:00
|
|
|
struct tok_state *
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_FromUTF8(const char *str, int exec_input)
|
2009-03-02 19:31:26 -04:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
struct tok_state *tok = tok_new();
|
2020-02-27 22:44:52 -04:00
|
|
|
char *translated;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (tok == NULL)
|
|
|
|
return NULL;
|
2020-02-27 22:44:52 -04:00
|
|
|
tok->input = translated = translate_newlines(str, exec_input, tok);
|
|
|
|
if (translated == NULL) {
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_Free(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->decoding_state = STATE_NORMAL;
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->enc = NULL;
|
2020-02-27 22:44:52 -04:00
|
|
|
tok->str = translated;
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->encoding = new_string("utf-8", 5, tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!tok->encoding) {
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_Free(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:44:52 -04:00
|
|
|
tok->buf = tok->cur = tok->inp = translated;
|
|
|
|
tok->end = translated;
|
2010-05-09 12:52:27 -03:00
|
|
|
return tok;
|
2009-03-02 19:31:26 -04:00
|
|
|
}
|
|
|
|
|
1991-07-27 18:42:56 -03:00
|
|
|
/* Set up tokenizer for file */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
struct tok_state *
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_FromFile(FILE *fp, const char* enc,
|
|
|
|
const char *ps1, const char *ps2)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
struct tok_state *tok = tok_new();
|
|
|
|
if (tok == NULL)
|
|
|
|
return NULL;
|
2020-12-01 04:56:42 -04:00
|
|
|
if ((tok->buf = (char *)PyMem_Malloc(BUFSIZ)) == NULL) {
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_Free(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tok->cur = tok->inp = tok->buf;
|
|
|
|
tok->end = tok->buf + BUFSIZ;
|
|
|
|
tok->fp = fp;
|
|
|
|
tok->prompt = ps1;
|
|
|
|
tok->nextprompt = ps2;
|
|
|
|
if (enc != NULL) {
|
|
|
|
/* Must copy encoding declaration since it
|
|
|
|
gets copied into the parse tree. */
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->encoding = new_string(enc, strlen(enc), tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!tok->encoding) {
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_Free(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
tok->decoding_state = STATE_NORMAL;
|
|
|
|
}
|
|
|
|
return tok;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free a tok_state structure */
|
|
|
|
|
|
|
|
void
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_Free(struct tok_state *tok)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2021-03-13 23:38:40 -04:00
|
|
|
if (tok->encoding != NULL) {
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(tok->encoding);
|
2021-03-13 23:38:40 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_XDECREF(tok->decoding_readline);
|
|
|
|
Py_XDECREF(tok->decoding_buffer);
|
2011-04-04 19:39:01 -03:00
|
|
|
Py_XDECREF(tok->filename);
|
2021-03-13 23:38:40 -04:00
|
|
|
if (tok->fp != NULL && tok->buf != NULL) {
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(tok->buf);
|
2021-03-13 23:38:40 -04:00
|
|
|
}
|
|
|
|
if (tok->input) {
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(tok->input);
|
2021-03-13 23:38:40 -04:00
|
|
|
}
|
|
|
|
if (tok->interactive_src_start != NULL) {
|
|
|
|
PyMem_Free(tok->interactive_src_start);
|
|
|
|
}
|
2023-04-19 13:18:16 -03:00
|
|
|
free_fstring_expressions(tok);
|
2020-12-01 04:56:42 -04:00
|
|
|
PyMem_Free(tok);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2021-03-28 19:48:05 -03:00
|
|
|
static int
|
|
|
|
tok_readline_raw(struct tok_state *tok)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
if (!tok_reserve_buf(tok, BUFSIZ)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-09-27 19:23:42 -03:00
|
|
|
int n_chars = (int)(tok->end - tok->inp);
|
|
|
|
size_t line_size = 0;
|
|
|
|
char *line = _Py_UniversalNewlineFgetsWithSize(tok->inp, n_chars, tok->fp, NULL, &line_size);
|
2021-03-28 19:48:05 -03:00
|
|
|
if (line == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
2023-04-19 13:18:16 -03:00
|
|
|
if (tok->tok_mode_stack_index && !update_fstring_expr(tok, 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
if (tok->fp_interactive &&
|
|
|
|
tok_concatenate_interactive_new_line(tok, line) == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-09-27 19:23:42 -03:00
|
|
|
tok->inp += line_size;
|
2021-12-12 03:06:50 -04:00
|
|
|
if (tok->inp == tok->buf) {
|
2021-03-29 20:24:49 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
} while (tok->inp[-1] != '\n');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tok_underflow_string(struct tok_state *tok) {
|
|
|
|
char *end = strchr(tok->inp, '\n');
|
|
|
|
if (end != NULL) {
|
|
|
|
end++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
end = strchr(tok->inp, '\0');
|
|
|
|
if (end == tok->inp) {
|
|
|
|
tok->done = E_EOF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tok->start == NULL) {
|
|
|
|
tok->buf = tok->cur;
|
|
|
|
}
|
|
|
|
tok->line_start = tok->cur;
|
2022-10-07 18:38:35 -03:00
|
|
|
ADVANCE_LINENO();
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->inp = end;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tok_underflow_interactive(struct tok_state *tok) {
|
2021-05-22 19:05:00 -03:00
|
|
|
if (tok->interactive_underflow == IUNDERFLOW_STOP) {
|
|
|
|
tok->done = E_INTERACT_STOP;
|
|
|
|
return 1;
|
|
|
|
}
|
2022-02-01 18:33:52 -04:00
|
|
|
char *newtok = PyOS_Readline(tok->fp ? tok->fp : stdin, stdout, tok->prompt);
|
2021-03-28 19:48:05 -03:00
|
|
|
if (newtok != NULL) {
|
|
|
|
char *translated = translate_newlines(newtok, 0, tok);
|
|
|
|
PyMem_Free(newtok);
|
|
|
|
if (translated == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
newtok = translated;
|
|
|
|
}
|
|
|
|
if (tok->encoding && newtok && *newtok) {
|
|
|
|
/* Recode to UTF-8 */
|
|
|
|
Py_ssize_t buflen;
|
|
|
|
const char* buf;
|
|
|
|
PyObject *u = translate_into_utf8(newtok, tok->encoding);
|
|
|
|
PyMem_Free(newtok);
|
|
|
|
if (u == NULL) {
|
|
|
|
tok->done = E_DECODE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buflen = PyBytes_GET_SIZE(u);
|
|
|
|
buf = PyBytes_AS_STRING(u);
|
|
|
|
newtok = PyMem_Malloc(buflen+1);
|
|
|
|
if (newtok == NULL) {
|
|
|
|
Py_DECREF(u);
|
|
|
|
tok->done = E_NOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strcpy(newtok, buf);
|
|
|
|
Py_DECREF(u);
|
|
|
|
}
|
|
|
|
if (tok->fp_interactive &&
|
|
|
|
tok_concatenate_interactive_new_line(tok, newtok) == -1) {
|
|
|
|
PyMem_Free(newtok);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (tok->nextprompt != NULL) {
|
|
|
|
tok->prompt = tok->nextprompt;
|
|
|
|
}
|
|
|
|
if (newtok == NULL) {
|
|
|
|
tok->done = E_INTR;
|
|
|
|
}
|
|
|
|
else if (*newtok == '\0') {
|
|
|
|
PyMem_Free(newtok);
|
|
|
|
tok->done = E_EOF;
|
|
|
|
}
|
|
|
|
else if (tok->start != NULL) {
|
|
|
|
Py_ssize_t cur_multi_line_start = tok->multi_line_start - tok->buf;
|
2023-04-23 16:06:10 -03:00
|
|
|
remember_fstring_buffers(tok);
|
2021-03-28 19:48:05 -03:00
|
|
|
size_t size = strlen(newtok);
|
2022-10-07 18:38:35 -03:00
|
|
|
ADVANCE_LINENO();
|
2021-03-28 19:48:05 -03:00
|
|
|
if (!tok_reserve_buf(tok, size + 1)) {
|
|
|
|
PyMem_Free(tok->buf);
|
|
|
|
tok->buf = NULL;
|
|
|
|
PyMem_Free(newtok);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(tok->cur, newtok, size + 1);
|
|
|
|
PyMem_Free(newtok);
|
|
|
|
tok->inp += size;
|
|
|
|
tok->multi_line_start = tok->buf + cur_multi_line_start;
|
2023-04-23 16:06:10 -03:00
|
|
|
restore_fstring_buffers(tok);
|
2021-03-28 19:48:05 -03:00
|
|
|
}
|
|
|
|
else {
|
2023-04-24 22:31:21 -03:00
|
|
|
remember_fstring_buffers(tok);
|
2022-10-07 18:38:35 -03:00
|
|
|
ADVANCE_LINENO();
|
2021-03-28 19:48:05 -03:00
|
|
|
PyMem_Free(tok->buf);
|
|
|
|
tok->buf = newtok;
|
|
|
|
tok->cur = tok->buf;
|
|
|
|
tok->line_start = tok->buf;
|
|
|
|
tok->inp = strchr(tok->buf, '\0');
|
|
|
|
tok->end = tok->inp + 1;
|
2023-04-24 22:31:21 -03:00
|
|
|
restore_fstring_buffers(tok);
|
2021-03-28 19:48:05 -03:00
|
|
|
}
|
|
|
|
if (tok->done != E_OK) {
|
|
|
|
if (tok->prompt != NULL) {
|
|
|
|
PySys_WriteStderr("\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2023-04-19 13:18:16 -03:00
|
|
|
|
|
|
|
if (tok->tok_mode_stack_index && !update_fstring_expr(tok, 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tok_underflow_file(struct tok_state *tok) {
|
|
|
|
if (tok->start == NULL) {
|
|
|
|
tok->cur = tok->inp = tok->buf;
|
|
|
|
}
|
|
|
|
if (tok->decoding_state == STATE_INIT) {
|
|
|
|
/* We have not yet determined the encoding.
|
|
|
|
If an encoding is found, use the file-pointer
|
|
|
|
reader functions from now on. */
|
|
|
|
if (!check_bom(fp_getc, fp_ungetc, fp_setreadl, tok)) {
|
|
|
|
error_ret(tok);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert(tok->decoding_state != STATE_INIT);
|
|
|
|
}
|
|
|
|
/* Read until '\n' or EOF */
|
|
|
|
if (tok->decoding_readline != NULL) {
|
|
|
|
/* We already have a codec associated with this input. */
|
|
|
|
if (!tok_readline_recode(tok)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* We want a 'raw' read. */
|
|
|
|
if (!tok_readline_raw(tok)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tok->inp == tok->cur) {
|
|
|
|
tok->done = E_EOF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (tok->inp[-1] != '\n') {
|
2022-11-20 16:20:03 -04:00
|
|
|
assert(tok->inp + 1 < tok->end);
|
2021-03-28 19:48:05 -03:00
|
|
|
/* Last line does not end in \n, fake one */
|
|
|
|
*tok->inp++ = '\n';
|
|
|
|
*tok->inp = '\0';
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:38:35 -03:00
|
|
|
ADVANCE_LINENO();
|
2021-03-28 19:48:05 -03:00
|
|
|
if (tok->decoding_state != STATE_NORMAL) {
|
|
|
|
if (tok->lineno > 2) {
|
|
|
|
tok->decoding_state = STATE_NORMAL;
|
|
|
|
}
|
2021-03-29 20:24:49 -03:00
|
|
|
else if (!check_coding_spec(tok->cur, strlen(tok->cur),
|
2021-03-28 19:48:05 -03:00
|
|
|
tok, fp_setreadl))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* The default encoding is UTF-8, so make sure we don't have any
|
|
|
|
non-UTF-8 sequences in it. */
|
2021-12-12 03:06:50 -04:00
|
|
|
if (!tok->encoding && !ensure_utf8(tok->cur, tok)) {
|
|
|
|
error_ret(tok);
|
|
|
|
return 0;
|
2021-03-28 19:48:05 -03:00
|
|
|
}
|
|
|
|
assert(tok->done == E_OK);
|
|
|
|
return tok->done == E_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-22 18:57:24 -03:00
|
|
|
#if defined(Py_DEBUG)
|
2021-03-28 19:48:05 -03:00
|
|
|
static void
|
|
|
|
print_escape(FILE *f, const char *s, Py_ssize_t size)
|
|
|
|
{
|
|
|
|
if (s == NULL) {
|
|
|
|
fputs("NULL", f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
putc('"', f);
|
|
|
|
while (size-- > 0) {
|
|
|
|
unsigned char c = *s++;
|
|
|
|
switch (c) {
|
|
|
|
case '\n': fputs("\\n", f); break;
|
|
|
|
case '\r': fputs("\\r", f); break;
|
|
|
|
case '\t': fputs("\\t", f); break;
|
|
|
|
case '\f': fputs("\\f", f); break;
|
|
|
|
case '\'': fputs("\\'", f); break;
|
|
|
|
case '"': fputs("\\\"", f); break;
|
|
|
|
default:
|
|
|
|
if (0x20 <= c && c <= 0x7f)
|
|
|
|
putc(c, f);
|
|
|
|
else
|
|
|
|
fprintf(f, "\\x%02x", c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
putc('"', f);
|
|
|
|
}
|
2021-10-22 18:57:24 -03:00
|
|
|
#endif
|
2021-03-28 19:48:05 -03:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Get next char, updating state; error code goes into tok->done */
|
|
|
|
|
|
|
|
static int
|
2013-08-13 15:18:52 -03:00
|
|
|
tok_nextc(struct tok_state *tok)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2021-03-28 19:48:05 -03:00
|
|
|
int rc;
|
2010-05-09 12:52:27 -03:00
|
|
|
for (;;) {
|
|
|
|
if (tok->cur != tok->inp) {
|
2022-10-07 18:38:35 -03:00
|
|
|
tok->col_offset++;
|
2010-05-09 12:52:27 -03:00
|
|
|
return Py_CHARMASK(*tok->cur++); /* Fast path */
|
|
|
|
}
|
2022-02-08 07:54:37 -04:00
|
|
|
if (tok->done != E_OK) {
|
2023-04-19 13:18:16 -03:00
|
|
|
return EOF;
|
2022-02-08 07:54:37 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
if (tok->fp == NULL) {
|
2021-03-28 19:48:05 -03:00
|
|
|
rc = tok_underflow_string(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
else if (tok->prompt != NULL) {
|
|
|
|
rc = tok_underflow_interactive(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else {
|
2021-03-28 19:48:05 -03:00
|
|
|
rc = tok_underflow_file(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-10-22 05:52:24 -03:00
|
|
|
#if defined(Py_DEBUG)
|
2022-05-24 17:35:08 -03:00
|
|
|
if (tok->debug) {
|
2021-10-27 18:27:34 -03:00
|
|
|
fprintf(stderr, "line[%d] = ", tok->lineno);
|
2021-10-28 14:06:15 -03:00
|
|
|
print_escape(stderr, tok->cur, tok->inp - tok->cur);
|
2021-10-27 18:27:34 -03:00
|
|
|
fprintf(stderr, " tok->done = %d\n", tok->done);
|
2021-03-28 19:48:05 -03:00
|
|
|
}
|
2021-10-22 05:52:24 -03:00
|
|
|
#endif
|
2021-03-28 19:48:05 -03:00
|
|
|
if (!rc) {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->cur = tok->inp;
|
|
|
|
return EOF;
|
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
tok->line_start = tok->cur;
|
2022-09-27 19:23:42 -03:00
|
|
|
|
|
|
|
if (contains_null_bytes(tok->line_start, tok->inp - tok->line_start)) {
|
|
|
|
syntaxerror(tok, "source code cannot contain null bytes");
|
|
|
|
tok->cur = tok->inp;
|
|
|
|
return EOF;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-03-28 19:48:05 -03:00
|
|
|
Py_UNREACHABLE();
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Back-up one character */
|
|
|
|
|
|
|
|
static void
|
2013-08-13 15:18:52 -03:00
|
|
|
tok_backup(struct tok_state *tok, int c)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
if (c != EOF) {
|
2020-03-06 19:54:20 -04:00
|
|
|
if (--tok->cur < tok->buf) {
|
2020-03-25 15:27:36 -03:00
|
|
|
Py_FatalError("tokenizer beginning of buffer");
|
2020-03-06 19:54:20 -04:00
|
|
|
}
|
2023-04-19 13:18:16 -03:00
|
|
|
if ((int)(unsigned char)*tok->cur != Py_CHARMASK(c)) {
|
2021-03-28 19:48:05 -03:00
|
|
|
Py_FatalError("tok_backup: wrong character");
|
2020-03-06 19:54:20 -04:00
|
|
|
}
|
2022-10-07 18:38:35 -03:00
|
|
|
tok->col_offset--;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2018-07-09 09:09:35 -03:00
|
|
|
static int
|
2021-07-09 21:29:29 -03:00
|
|
|
_syntaxerror_range(struct tok_state *tok, const char *format,
|
|
|
|
int col_offset, int end_col_offset,
|
|
|
|
va_list vargs)
|
2018-07-09 09:09:35 -03:00
|
|
|
{
|
2023-05-04 11:15:26 -03:00
|
|
|
// In release builds, we don't want to overwrite a previous error, but in debug builds we
|
|
|
|
// want to fail if we are not doing it so we can fix it.
|
|
|
|
assert(tok->done != E_ERROR);
|
|
|
|
if (tok->done == E_ERROR) {
|
|
|
|
return ERRORTOKEN;
|
|
|
|
}
|
2020-02-12 06:17:00 -04:00
|
|
|
PyObject *errmsg, *errtext, *args;
|
|
|
|
errmsg = PyUnicode_FromFormatV(format, vargs);
|
|
|
|
if (!errmsg) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
errtext = PyUnicode_DecodeUTF8(tok->line_start, tok->cur - tok->line_start,
|
|
|
|
"replace");
|
|
|
|
if (!errtext) {
|
|
|
|
goto error;
|
|
|
|
}
|
2021-07-09 21:29:29 -03:00
|
|
|
|
|
|
|
if (col_offset == -1) {
|
|
|
|
col_offset = (int)PyUnicode_GET_LENGTH(errtext);
|
|
|
|
}
|
|
|
|
if (end_col_offset == -1) {
|
|
|
|
end_col_offset = col_offset;
|
|
|
|
}
|
|
|
|
|
2020-02-12 06:17:00 -04:00
|
|
|
Py_ssize_t line_len = strcspn(tok->line_start, "\n");
|
|
|
|
if (line_len != tok->cur - tok->line_start) {
|
|
|
|
Py_DECREF(errtext);
|
|
|
|
errtext = PyUnicode_DecodeUTF8(tok->line_start, line_len,
|
|
|
|
"replace");
|
|
|
|
}
|
|
|
|
if (!errtext) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2021-07-09 21:29:29 -03:00
|
|
|
args = Py_BuildValue("(O(OiiNii))", errmsg, tok->filename, tok->lineno,
|
|
|
|
col_offset, errtext, tok->lineno, end_col_offset);
|
2020-02-12 06:17:00 -04:00
|
|
|
if (args) {
|
|
|
|
PyErr_SetObject(PyExc_SyntaxError, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_XDECREF(errmsg);
|
2018-07-09 09:09:35 -03:00
|
|
|
tok->done = E_ERROR;
|
|
|
|
return ERRORTOKEN;
|
|
|
|
}
|
|
|
|
|
2021-07-09 21:29:29 -03:00
|
|
|
static int
|
|
|
|
syntaxerror(struct tok_state *tok, const char *format, ...)
|
|
|
|
{
|
2023-04-19 13:18:16 -03:00
|
|
|
// This errors are cleaned on startup. Todo: Fix it.
|
2021-07-09 21:29:29 -03:00
|
|
|
va_list vargs;
|
|
|
|
va_start(vargs, format);
|
|
|
|
int ret = _syntaxerror_range(tok, format, -1, -1, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
syntaxerror_known_range(struct tok_state *tok,
|
|
|
|
int col_offset, int end_col_offset,
|
|
|
|
const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list vargs;
|
|
|
|
va_start(vargs, format);
|
|
|
|
int ret = _syntaxerror_range(tok, format, col_offset, end_col_offset, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
1998-04-09 18:38:06 -03:00
|
|
|
static int
|
2000-07-22 16:20:54 -03:00
|
|
|
indenterror(struct tok_state *tok)
|
1998-04-09 18:38:06 -03:00
|
|
|
{
|
2017-11-17 05:25:47 -04:00
|
|
|
tok->done = E_TABSPACE;
|
|
|
|
tok->cur = tok->inp;
|
|
|
|
return ERRORTOKEN;
|
1998-04-09 18:38:06 -03:00
|
|
|
}
|
|
|
|
|
2021-06-08 20:31:10 -03:00
|
|
|
static int
|
2022-04-27 14:15:14 -03:00
|
|
|
parser_warn(struct tok_state *tok, PyObject *category, const char *format, ...)
|
2021-06-08 20:31:10 -03:00
|
|
|
{
|
2022-11-30 07:36:06 -04:00
|
|
|
if (!tok->report_warnings) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-08 20:31:10 -03:00
|
|
|
PyObject *errmsg;
|
|
|
|
va_list vargs;
|
|
|
|
va_start(vargs, format);
|
|
|
|
errmsg = PyUnicode_FromFormatV(format, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
if (!errmsg) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2022-04-27 14:15:14 -03:00
|
|
|
if (PyErr_WarnExplicitObject(category, errmsg, tok->filename,
|
2021-06-08 20:31:10 -03:00
|
|
|
tok->lineno, NULL, NULL) < 0) {
|
2022-04-27 14:15:14 -03:00
|
|
|
if (PyErr_ExceptionMatches(category)) {
|
2021-06-08 20:31:10 -03:00
|
|
|
/* Replace the DeprecationWarning exception with a SyntaxError
|
|
|
|
to get a more accurate error report */
|
|
|
|
PyErr_Clear();
|
|
|
|
syntaxerror(tok, "%U", errmsg);
|
|
|
|
}
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
Py_DECREF(errmsg);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_XDECREF(errmsg);
|
|
|
|
tok->done = E_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:18:16 -03:00
|
|
|
static int
|
|
|
|
warn_invalid_escape_sequence(struct tok_state *tok, int first_invalid_escape_char)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!tok->tok_report_warnings) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *msg = PyUnicode_FromFormat(
|
|
|
|
"invalid escape sequence '\\%c'",
|
|
|
|
(char) first_invalid_escape_char
|
|
|
|
);
|
|
|
|
|
|
|
|
if (msg == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning, msg, tok->filename,
|
|
|
|
tok->lineno, NULL, NULL) < 0) {
|
|
|
|
Py_DECREF(msg);
|
|
|
|
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
|
|
|
|
/* Replace the DeprecationWarning exception with a SyntaxError
|
|
|
|
to get a more accurate error report */
|
|
|
|
PyErr_Clear();
|
|
|
|
return syntaxerror(tok, "invalid escape sequence '\\%c'", (char) first_invalid_escape_char);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-08 20:31:10 -03:00
|
|
|
static int
|
|
|
|
lookahead(struct tok_state *tok, const char *test)
|
|
|
|
{
|
|
|
|
const char *s = test;
|
|
|
|
int res = 0;
|
|
|
|
while (1) {
|
|
|
|
int c = tok_nextc(tok);
|
|
|
|
if (*s == 0) {
|
|
|
|
res = !is_potential_identifier_char(c);
|
|
|
|
}
|
|
|
|
else if (c == *s) {
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tok_backup(tok, c);
|
|
|
|
while (s != test) {
|
|
|
|
tok_backup(tok, *--s);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
verify_end_of_number(struct tok_state *tok, int c, const char *kind)
|
|
|
|
{
|
|
|
|
/* Emit a deprecation warning only if the numeric literal is immediately
|
2021-06-12 23:47:44 -03:00
|
|
|
* followed by one of keywords which can occur after a numeric literal
|
2021-06-08 20:31:10 -03:00
|
|
|
* in valid code: "and", "else", "for", "if", "in", "is" and "or".
|
|
|
|
* It allows to gradually deprecate existing valid code without adding
|
|
|
|
* warning before error in most cases of invalid numeric literal (which
|
2021-06-12 23:47:44 -03:00
|
|
|
* would be confusing and break existing tests).
|
|
|
|
* Raise a syntax error with slightly better message than plain
|
2021-06-08 20:31:10 -03:00
|
|
|
* "invalid syntax" if the numeric literal is immediately followed by
|
|
|
|
* other keyword or identifier.
|
|
|
|
*/
|
|
|
|
int r = 0;
|
|
|
|
if (c == 'a') {
|
|
|
|
r = lookahead(tok, "nd");
|
|
|
|
}
|
|
|
|
else if (c == 'e') {
|
|
|
|
r = lookahead(tok, "lse");
|
|
|
|
}
|
|
|
|
else if (c == 'f') {
|
|
|
|
r = lookahead(tok, "or");
|
|
|
|
}
|
|
|
|
else if (c == 'i') {
|
|
|
|
int c2 = tok_nextc(tok);
|
|
|
|
if (c2 == 'f' || c2 == 'n' || c2 == 's') {
|
|
|
|
r = 1;
|
|
|
|
}
|
|
|
|
tok_backup(tok, c2);
|
|
|
|
}
|
|
|
|
else if (c == 'o') {
|
|
|
|
r = lookahead(tok, "r");
|
|
|
|
}
|
2022-02-22 03:51:51 -04:00
|
|
|
else if (c == 'n') {
|
|
|
|
r = lookahead(tok, "ot");
|
|
|
|
}
|
2021-06-08 20:31:10 -03:00
|
|
|
if (r) {
|
|
|
|
tok_backup(tok, c);
|
2022-04-27 14:15:14 -03:00
|
|
|
if (parser_warn(tok, PyExc_SyntaxWarning,
|
|
|
|
"invalid %s literal", kind))
|
|
|
|
{
|
2021-06-08 20:31:10 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
tok_nextc(tok);
|
|
|
|
}
|
|
|
|
else /* In future releases, only error will remain. */
|
|
|
|
if (is_potential_identifier_char(c)) {
|
|
|
|
tok_backup(tok, c);
|
|
|
|
syntaxerror(tok, "invalid %s literal", kind);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-09-28 02:41:54 -03:00
|
|
|
/* Verify that the identifier follows PEP 3131.
|
|
|
|
All identifier strings are guaranteed to be "ready" unicode objects.
|
|
|
|
*/
|
2007-08-15 04:32:56 -03:00
|
|
|
static int
|
2010-03-12 10:45:56 -04:00
|
|
|
verify_identifier(struct tok_state *tok)
|
2007-08-15 04:32:56 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *s;
|
2015-04-21 13:05:19 -03:00
|
|
|
if (tok->decoding_erred)
|
|
|
|
return 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL);
|
2018-09-10 03:27:31 -03:00
|
|
|
if (s == NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
|
2020-05-12 06:42:04 -03:00
|
|
|
tok->done = E_DECODE;
|
|
|
|
}
|
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->done = E_ERROR;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-12 06:42:04 -03:00
|
|
|
Py_ssize_t invalid = _PyUnicode_ScanIdentifier(s);
|
|
|
|
if (invalid < 0) {
|
|
|
|
Py_DECREF(s);
|
|
|
|
tok->done = E_ERROR;
|
|
|
|
return 0;
|
2020-02-11 09:29:33 -04:00
|
|
|
}
|
2020-05-12 06:42:04 -03:00
|
|
|
assert(PyUnicode_GET_LENGTH(s) > 0);
|
|
|
|
if (invalid < PyUnicode_GET_LENGTH(s)) {
|
|
|
|
Py_UCS4 ch = PyUnicode_READ_CHAR(s, invalid);
|
|
|
|
if (invalid + 1 < PyUnicode_GET_LENGTH(s)) {
|
|
|
|
/* Determine the offset in UTF-8 encoded input */
|
|
|
|
Py_SETREF(s, PyUnicode_Substring(s, 0, invalid + 1));
|
|
|
|
if (s != NULL) {
|
|
|
|
Py_SETREF(s, PyUnicode_AsUTF8String(s));
|
|
|
|
}
|
|
|
|
if (s == NULL) {
|
|
|
|
tok->done = E_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
tok->cur = (char *)tok->start + PyBytes_GET_SIZE(s);
|
|
|
|
}
|
|
|
|
Py_DECREF(s);
|
|
|
|
// PyUnicode_FromFormatV() does not support %X
|
|
|
|
char hex[9];
|
2020-06-15 16:59:47 -03:00
|
|
|
(void)PyOS_snprintf(hex, sizeof(hex), "%04X", ch);
|
2020-05-12 06:42:04 -03:00
|
|
|
if (Py_UNICODE_ISPRINTABLE(ch)) {
|
|
|
|
syntaxerror(tok, "invalid character '%c' (U+%s)", ch, hex);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
syntaxerror(tok, "invalid non-printable character U+%s", hex);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Py_DECREF(s);
|
|
|
|
return 1;
|
2007-08-15 04:32:56 -03:00
|
|
|
}
|
1998-04-09 18:38:06 -03:00
|
|
|
|
2016-09-09 18:57:09 -03:00
|
|
|
static int
|
|
|
|
tok_decimal_tail(struct tok_state *tok)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
do {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
} while (isdigit(c));
|
|
|
|
if (c != '_') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
if (!isdigit(c)) {
|
|
|
|
tok_backup(tok, c);
|
2018-07-09 09:09:35 -03:00
|
|
|
syntaxerror(tok, "invalid decimal literal");
|
2016-09-09 18:57:09 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2022-01-25 18:12:14 -04:00
|
|
|
static inline int
|
|
|
|
tok_continuation_line(struct tok_state *tok) {
|
|
|
|
int c = tok_nextc(tok);
|
|
|
|
if (c != '\n') {
|
|
|
|
tok->done = E_LINECONT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
if (c == EOF) {
|
|
|
|
tok->done = E_EOF;
|
|
|
|
tok->cur = tok->inp;
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
tok_backup(tok, c);
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2022-10-07 18:38:35 -03:00
|
|
|
static int
|
|
|
|
type_comment_token_setup(struct tok_state *tok, struct token *token, int type, int col_offset,
|
|
|
|
int end_col_offset, const char *start, const char *end)
|
|
|
|
{
|
|
|
|
token->level = tok->level;
|
|
|
|
token->lineno = token->end_lineno = tok->lineno;
|
|
|
|
token->col_offset = col_offset;
|
|
|
|
token->end_col_offset = end_col_offset;
|
|
|
|
token->start = start;
|
|
|
|
token->end = end;
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2002-08-04 14:29:52 -03:00
|
|
|
static int
|
2022-10-06 20:07:17 -03:00
|
|
|
token_setup(struct tok_state *tok, struct token *token, int type, const char *start, const char *end)
|
|
|
|
{
|
|
|
|
assert((start == NULL && end == NULL) || (start != NULL && end != NULL));
|
|
|
|
token->level = tok->level;
|
2023-04-19 13:18:16 -03:00
|
|
|
if (ISSTRINGLIT(type)) {
|
|
|
|
token->lineno = tok->first_lineno;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
token->lineno = tok->lineno;
|
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
token->end_lineno = tok->lineno;
|
2022-10-07 18:38:35 -03:00
|
|
|
token->col_offset = token->end_col_offset = -1;
|
2022-10-06 20:07:17 -03:00
|
|
|
token->start = start;
|
|
|
|
token->end = end;
|
2022-10-07 18:38:35 -03:00
|
|
|
|
2022-10-06 20:07:17 -03:00
|
|
|
if (start != NULL && end != NULL) {
|
2022-10-07 18:38:35 -03:00
|
|
|
token->col_offset = tok->starting_col_offset;
|
|
|
|
token->end_col_offset = tok->col_offset;
|
2022-10-06 20:07:17 -03:00
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2023-04-19 13:18:16 -03:00
|
|
|
tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct token *token)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2013-08-13 15:18:52 -03:00
|
|
|
int c;
|
2010-05-09 12:52:27 -03:00
|
|
|
int blankline, nonascii;
|
1991-07-27 18:42:56 -03:00
|
|
|
|
2022-10-06 20:07:17 -03:00
|
|
|
const char *p_start = NULL;
|
|
|
|
const char *p_end = NULL;
|
1991-07-27 18:42:56 -03:00
|
|
|
nextline:
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->start = NULL;
|
2022-10-07 18:38:35 -03:00
|
|
|
tok->starting_col_offset = -1;
|
2010-05-09 12:52:27 -03:00
|
|
|
blankline = 0;
|
|
|
|
|
|
|
|
/* Get indentation level */
|
|
|
|
if (tok->atbol) {
|
2013-08-13 15:18:52 -03:00
|
|
|
int col = 0;
|
|
|
|
int altcol = 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->atbol = 0;
|
2022-01-25 18:12:14 -04:00
|
|
|
int cont_line_col = 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
for (;;) {
|
|
|
|
c = tok_nextc(tok);
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == ' ') {
|
2010-05-09 12:52:27 -03:00
|
|
|
col++, altcol++;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
else if (c == '\t') {
|
2017-11-17 05:25:47 -04:00
|
|
|
col = (col / tok->tabsize + 1) * tok->tabsize;
|
|
|
|
altcol = (altcol / ALTTABSIZE + 1) * ALTTABSIZE;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
else if (c == '\014') {/* Control-L (formfeed) */
|
2010-05-09 12:52:27 -03:00
|
|
|
col = altcol = 0; /* For Emacs users */
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2022-01-25 18:12:14 -04:00
|
|
|
else if (c == '\\') {
|
|
|
|
// Indentation cannot be split over multiple physical lines
|
|
|
|
// using backslashes. This means that if we found a backslash
|
|
|
|
// preceded by whitespace, **the first one we find** determines
|
|
|
|
// the level of indentation of whatever comes next.
|
|
|
|
cont_line_col = cont_line_col ? cont_line_col : col;
|
|
|
|
if ((c = tok_continuation_line(tok)) == -1) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2022-01-25 18:12:14 -04:00
|
|
|
}
|
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
break;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
tok_backup(tok, c);
|
2022-01-25 18:12:14 -04:00
|
|
|
if (c == '#' || c == '\n') {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Lines with only whitespace and/or comments
|
|
|
|
shouldn't affect the indentation and are
|
|
|
|
not passed to the parser as NEWLINE tokens,
|
|
|
|
except *totally* empty lines in interactive
|
|
|
|
mode, which signal the end of a command group. */
|
2016-09-09 18:57:09 -03:00
|
|
|
if (col == 0 && c == '\n' && tok->prompt != NULL) {
|
2010-05-09 12:52:27 -03:00
|
|
|
blankline = 0; /* Let it through */
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2019-12-09 00:36:27 -04:00
|
|
|
else if (tok->prompt != NULL && tok->lineno == 1) {
|
|
|
|
/* In interactive mode, if the first line contains
|
|
|
|
only spaces and/or a comment, let it through. */
|
|
|
|
blankline = 0;
|
|
|
|
col = altcol = 0;
|
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
blankline = 1; /* Ignore completely */
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
/* We can't jump back right here since we still
|
|
|
|
may need to skip to the end of a comment */
|
|
|
|
}
|
|
|
|
if (!blankline && tok->level == 0) {
|
2022-01-25 18:12:14 -04:00
|
|
|
col = cont_line_col ? cont_line_col : col;
|
|
|
|
altcol = cont_line_col ? cont_line_col : altcol;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (col == tok->indstack[tok->indent]) {
|
|
|
|
/* No change */
|
|
|
|
if (altcol != tok->altindstack[tok->indent]) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(indenterror(tok));
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (col > tok->indstack[tok->indent]) {
|
|
|
|
/* Indent -- always one */
|
|
|
|
if (tok->indent+1 >= MAXINDENT) {
|
|
|
|
tok->done = E_TOODEEP;
|
|
|
|
tok->cur = tok->inp;
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
if (altcol <= tok->altindstack[tok->indent]) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(indenterror(tok));
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
tok->pendin++;
|
|
|
|
tok->indstack[++tok->indent] = col;
|
|
|
|
tok->altindstack[tok->indent] = altcol;
|
|
|
|
}
|
|
|
|
else /* col < tok->indstack[tok->indent] */ {
|
|
|
|
/* Dedent -- any number, must be consistent */
|
|
|
|
while (tok->indent > 0 &&
|
|
|
|
col < tok->indstack[tok->indent]) {
|
|
|
|
tok->pendin--;
|
|
|
|
tok->indent--;
|
|
|
|
}
|
|
|
|
if (col != tok->indstack[tok->indent]) {
|
|
|
|
tok->done = E_DEDENT;
|
|
|
|
tok->cur = tok->inp;
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
if (altcol != tok->altindstack[tok->indent]) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(indenterror(tok));
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tok->start = tok->cur;
|
2022-10-07 18:38:35 -03:00
|
|
|
tok->starting_col_offset = tok->col_offset;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* Return pending indents/dedents */
|
|
|
|
if (tok->pendin != 0) {
|
|
|
|
if (tok->pendin < 0) {
|
|
|
|
tok->pendin++;
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(DEDENT);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
tok->pendin--;
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(INDENT);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
2006-04-21 07:40:58 -03:00
|
|
|
|
2019-03-07 16:38:08 -04:00
|
|
|
/* Peek ahead at the next character */
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
tok_backup(tok, c);
|
|
|
|
/* Check if we are closing an async function */
|
|
|
|
if (tok->async_def
|
|
|
|
&& !blankline
|
|
|
|
/* Due to some implementation artifacts of type comments,
|
|
|
|
* a TYPE_COMMENT at the start of a function won't set an
|
|
|
|
* indentation level and it will produce a NEWLINE after it.
|
|
|
|
* To avoid spuriously ending an async function due to this,
|
|
|
|
* wait until we have some non-newline char in front of us. */
|
|
|
|
&& c != '\n'
|
|
|
|
&& tok->level == 0
|
|
|
|
/* There was a NEWLINE after ASYNC DEF,
|
|
|
|
so we're past the signature. */
|
|
|
|
&& tok->async_def_nl
|
|
|
|
/* Current indentation level is less than where
|
|
|
|
the async function was defined */
|
|
|
|
&& tok->async_def_indent >= tok->indent)
|
|
|
|
{
|
|
|
|
tok->async_def = 0;
|
|
|
|
tok->async_def_indent = 0;
|
|
|
|
tok->async_def_nl = 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
again:
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->start = NULL;
|
|
|
|
/* Skip spaces */
|
|
|
|
do {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
} while (c == ' ' || c == '\t' || c == '\014');
|
|
|
|
|
|
|
|
/* Set start of current token */
|
2022-09-13 10:14:35 -03:00
|
|
|
tok->start = tok->cur == NULL ? NULL : tok->cur - 1;
|
2022-10-07 18:38:35 -03:00
|
|
|
tok->starting_col_offset = tok->col_offset - 1;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2019-01-31 07:40:27 -04:00
|
|
|
/* Skip comment, unless it's a type comment */
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == '#') {
|
2023-04-19 13:18:16 -03:00
|
|
|
|
2023-04-19 17:51:31 -03:00
|
|
|
if (INSIDE_FSTRING(tok)) {
|
2023-04-19 13:18:16 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "f-string expression part cannot include '#'"));
|
|
|
|
}
|
|
|
|
|
2019-01-31 07:40:27 -04:00
|
|
|
const char *prefix, *p, *type_start;
|
2022-10-07 18:38:35 -03:00
|
|
|
int current_starting_col_offset;
|
2019-01-31 07:40:27 -04:00
|
|
|
|
2016-09-09 18:57:09 -03:00
|
|
|
while (c != EOF && c != '\n') {
|
2010-05-09 12:52:27 -03:00
|
|
|
c = tok_nextc(tok);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2019-01-31 07:40:27 -04:00
|
|
|
|
|
|
|
if (tok->type_comments) {
|
|
|
|
p = tok->start;
|
2022-10-07 18:38:35 -03:00
|
|
|
current_starting_col_offset = tok->starting_col_offset;
|
2019-01-31 07:40:27 -04:00
|
|
|
prefix = type_comment_prefix;
|
|
|
|
while (*prefix && p < tok->cur) {
|
|
|
|
if (*prefix == ' ') {
|
|
|
|
while (*p == ' ' || *p == '\t') {
|
|
|
|
p++;
|
2022-10-07 18:38:35 -03:00
|
|
|
current_starting_col_offset++;
|
2019-01-31 07:40:27 -04:00
|
|
|
}
|
|
|
|
} else if (*prefix == *p) {
|
|
|
|
p++;
|
2022-10-07 18:38:35 -03:00
|
|
|
current_starting_col_offset++;
|
2019-01-31 07:40:27 -04:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a type comment if we matched all of type_comment_prefix. */
|
|
|
|
if (!*prefix) {
|
|
|
|
int is_type_ignore = 1;
|
2022-10-07 18:38:35 -03:00
|
|
|
// +6 in order to skip the word 'ignore'
|
2019-05-22 11:54:20 -03:00
|
|
|
const char *ignore_end = p + 6;
|
2022-10-07 18:38:35 -03:00
|
|
|
const int ignore_end_col_offset = current_starting_col_offset + 6;
|
2019-01-31 07:40:27 -04:00
|
|
|
tok_backup(tok, c); /* don't eat the newline or EOF */
|
|
|
|
|
|
|
|
type_start = p;
|
|
|
|
|
2019-05-11 15:17:24 -03:00
|
|
|
/* A TYPE_IGNORE is "type: ignore" followed by the end of the token
|
2019-05-22 17:43:37 -03:00
|
|
|
* or anything ASCII and non-alphanumeric. */
|
2019-05-11 15:17:24 -03:00
|
|
|
is_type_ignore = (
|
2019-05-22 11:54:20 -03:00
|
|
|
tok->cur >= ignore_end && memcmp(p, "ignore", 6) == 0
|
2019-05-22 17:43:37 -03:00
|
|
|
&& !(tok->cur > ignore_end
|
|
|
|
&& ((unsigned char)ignore_end[0] >= 128 || Py_ISALNUM(ignore_end[0]))));
|
2019-01-31 07:40:27 -04:00
|
|
|
|
|
|
|
if (is_type_ignore) {
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = ignore_end;
|
|
|
|
p_end = tok->cur;
|
2019-05-22 11:54:20 -03:00
|
|
|
|
2019-01-31 07:40:27 -04:00
|
|
|
/* If this type ignore is the only thing on the line, consume the newline also. */
|
|
|
|
if (blankline) {
|
|
|
|
tok_nextc(tok);
|
|
|
|
tok->atbol = 1;
|
|
|
|
}
|
2022-10-07 18:38:35 -03:00
|
|
|
return MAKE_TYPE_COMMENT_TOKEN(TYPE_IGNORE, ignore_end_col_offset, tok->col_offset);
|
2019-01-31 07:40:27 -04:00
|
|
|
} else {
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = type_start;
|
|
|
|
p_end = tok->cur;
|
2022-10-07 18:38:35 -03:00
|
|
|
return MAKE_TYPE_COMMENT_TOKEN(TYPE_COMMENT, current_starting_col_offset, tok->col_offset);
|
2019-01-31 07:40:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2021-05-22 19:05:00 -03:00
|
|
|
if (tok->done == E_INTERACT_STOP) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ENDMARKER);
|
2021-05-22 19:05:00 -03:00
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Check for EOF and errors now */
|
|
|
|
if (c == EOF) {
|
2021-01-19 19:59:33 -04:00
|
|
|
if (tok->level) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2021-01-19 19:59:33 -04:00
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(tok->done == E_EOF ? ENDMARKER : ERRORTOKEN);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Identifier (most frequent token!) */
|
|
|
|
nonascii = 0;
|
|
|
|
if (is_potential_identifier_start(c)) {
|
2017-02-04 21:32:39 -04:00
|
|
|
/* Process the various legal combinations of b"", r"", u"", and f"". */
|
2015-09-19 15:51:32 -03:00
|
|
|
int saw_b = 0, saw_r = 0, saw_u = 0, saw_f = 0;
|
2012-01-12 17:46:19 -04:00
|
|
|
while (1) {
|
2015-09-19 15:51:32 -03:00
|
|
|
if (!(saw_b || saw_u || saw_f) && (c == 'b' || c == 'B'))
|
2012-01-12 17:46:19 -04:00
|
|
|
saw_b = 1;
|
2012-03-04 08:04:06 -04:00
|
|
|
/* Since this is a backwards compatibility support literal we don't
|
|
|
|
want to support it in arbitrary order like byte literals. */
|
2016-09-09 18:57:09 -03:00
|
|
|
else if (!(saw_b || saw_u || saw_r || saw_f)
|
|
|
|
&& (c == 'u'|| c == 'U')) {
|
2012-03-04 08:04:06 -04:00
|
|
|
saw_u = 1;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2012-06-20 06:17:58 -03:00
|
|
|
/* ur"" and ru"" are not supported */
|
2016-09-09 18:57:09 -03:00
|
|
|
else if (!(saw_r || saw_u) && (c == 'r' || c == 'R')) {
|
2012-01-12 17:46:19 -04:00
|
|
|
saw_r = 1;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
else if (!(saw_f || saw_b || saw_u) && (c == 'f' || c == 'F')) {
|
2015-09-19 15:51:32 -03:00
|
|
|
saw_f = 1;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
else {
|
2012-01-12 17:46:19 -04:00
|
|
|
break;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
c = tok_nextc(tok);
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == '"' || c == '\'') {
|
2023-04-19 13:18:16 -03:00
|
|
|
if (saw_f) {
|
|
|
|
goto f_string_quote;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
goto letter_quote;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
while (is_potential_identifier_char(c)) {
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c >= 128) {
|
2010-05-09 12:52:27 -03:00
|
|
|
nonascii = 1;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
c = tok_nextc(tok);
|
|
|
|
}
|
|
|
|
tok_backup(tok, c);
|
2016-09-09 18:57:09 -03:00
|
|
|
if (nonascii && !verify_identifier(tok)) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2020-04-20 21:53:04 -03:00
|
|
|
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
2020-04-20 21:53:04 -03:00
|
|
|
|
2019-03-07 16:38:08 -04:00
|
|
|
/* async/await parsing block. */
|
|
|
|
if (tok->cur - tok->start == 5 && tok->start[0] == 'a') {
|
|
|
|
/* May be an 'async' or 'await' token. For Python 3.7 or
|
|
|
|
later we recognize them unconditionally. For Python
|
|
|
|
3.5 or 3.6 we recognize 'async' in front of 'def', and
|
|
|
|
either one inside of 'async def'. (Technically we
|
|
|
|
shouldn't recognize these at all for 3.4 or earlier,
|
|
|
|
but there's no *valid* Python 3.4 code that would be
|
|
|
|
rejected, and async functions will be rejected in a
|
|
|
|
later phase.) */
|
|
|
|
if (!tok->async_hacks || tok->async_def) {
|
|
|
|
/* Always recognize the keywords. */
|
|
|
|
if (memcmp(tok->start, "async", 5) == 0) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ASYNC);
|
2019-03-07 16:38:08 -04:00
|
|
|
}
|
|
|
|
if (memcmp(tok->start, "await", 5) == 0) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(AWAIT);
|
2019-03-07 16:38:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (memcmp(tok->start, "async", 5) == 0) {
|
|
|
|
/* The current token is 'async'.
|
|
|
|
Look ahead one token to see if that is 'def'. */
|
|
|
|
|
|
|
|
struct tok_state ahead_tok;
|
2022-10-06 20:07:17 -03:00
|
|
|
struct token ahead_token;
|
2019-03-07 16:38:08 -04:00
|
|
|
int ahead_tok_kind;
|
|
|
|
|
|
|
|
memcpy(&ahead_tok, tok, sizeof(ahead_tok));
|
2023-04-19 13:18:16 -03:00
|
|
|
ahead_tok_kind = tok_get_normal_mode(&ahead_tok,
|
|
|
|
current_tok,
|
|
|
|
&ahead_token);
|
2019-03-07 16:38:08 -04:00
|
|
|
|
|
|
|
if (ahead_tok_kind == NAME
|
|
|
|
&& ahead_tok.cur - ahead_tok.start == 3
|
|
|
|
&& memcmp(ahead_tok.start, "def", 3) == 0)
|
|
|
|
{
|
|
|
|
/* The next token is going to be 'def', so instead of
|
|
|
|
returning a plain NAME token, return ASYNC. */
|
|
|
|
tok->async_def_indent = tok->indent;
|
|
|
|
tok->async_def = 1;
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ASYNC);
|
2019-03-07 16:38:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(NAME);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Newline */
|
|
|
|
if (c == '\n') {
|
|
|
|
tok->atbol = 1;
|
2016-09-09 18:57:09 -03:00
|
|
|
if (blankline || tok->level > 0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
goto nextline;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur - 1; /* Leave '\n' out of the string */
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->cont_line = 0;
|
2019-03-07 16:38:08 -04:00
|
|
|
if (tok->async_def) {
|
|
|
|
/* We're somewhere inside an 'async def' function, and
|
|
|
|
we've encountered a NEWLINE after its signature. */
|
|
|
|
tok->async_def_nl = 1;
|
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(NEWLINE);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Period or number starting with period? */
|
|
|
|
if (c == '.') {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
if (isdigit(c)) {
|
|
|
|
goto fraction;
|
|
|
|
} else if (c == '.') {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
if (c == '.') {
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(ELLIPSIS);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok_backup(tok, c);
|
|
|
|
}
|
|
|
|
tok_backup(tok, '.');
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok_backup(tok, c);
|
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(DOT);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Number */
|
|
|
|
if (isdigit(c)) {
|
|
|
|
if (c == '0') {
|
|
|
|
/* Hex, octal or binary -- maybe. */
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
if (c == 'x' || c == 'X') {
|
|
|
|
/* Hex */
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
do {
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == '_') {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
}
|
|
|
|
if (!isxdigit(c)) {
|
|
|
|
tok_backup(tok, c);
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "invalid hexadecimal literal"));
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
} while (isxdigit(c));
|
|
|
|
} while (c == '_');
|
2021-06-08 20:31:10 -03:00
|
|
|
if (!verify_end_of_number(tok, c, "hexadecimal")) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2021-06-08 20:31:10 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else if (c == 'o' || c == 'O') {
|
|
|
|
/* Octal */
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
do {
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == '_') {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
}
|
|
|
|
if (c < '0' || c >= '8') {
|
2018-07-09 09:09:35 -03:00
|
|
|
if (isdigit(c)) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok,
|
|
|
|
"invalid digit '%c' in octal literal", c));
|
2018-07-09 09:09:35 -03:00
|
|
|
}
|
|
|
|
else {
|
2021-07-09 21:29:29 -03:00
|
|
|
tok_backup(tok, c);
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "invalid octal literal"));
|
2018-07-09 09:09:35 -03:00
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
} while ('0' <= c && c < '8');
|
|
|
|
} while (c == '_');
|
2018-07-09 09:09:35 -03:00
|
|
|
if (isdigit(c)) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok,
|
|
|
|
"invalid digit '%c' in octal literal", c));
|
2018-07-09 09:09:35 -03:00
|
|
|
}
|
2021-06-08 20:31:10 -03:00
|
|
|
if (!verify_end_of_number(tok, c, "octal")) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2021-06-08 20:31:10 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else if (c == 'b' || c == 'B') {
|
|
|
|
/* Binary */
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
do {
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == '_') {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
}
|
|
|
|
if (c != '0' && c != '1') {
|
2018-07-09 09:09:35 -03:00
|
|
|
if (isdigit(c)) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "invalid digit '%c' in binary literal", c));
|
2018-07-09 09:09:35 -03:00
|
|
|
}
|
|
|
|
else {
|
2021-07-09 21:29:29 -03:00
|
|
|
tok_backup(tok, c);
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "invalid binary literal"));
|
2018-07-09 09:09:35 -03:00
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
} while (c == '0' || c == '1');
|
|
|
|
} while (c == '_');
|
2018-07-09 09:09:35 -03:00
|
|
|
if (isdigit(c)) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "invalid digit '%c' in binary literal", c));
|
2018-07-09 09:09:35 -03:00
|
|
|
}
|
2021-06-08 20:31:10 -03:00
|
|
|
if (!verify_end_of_number(tok, c, "binary")) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2021-06-08 20:31:10 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
int nonzero = 0;
|
|
|
|
/* maybe old-style octal; c is first char of it */
|
|
|
|
/* in any case, allow '0' as a literal */
|
2016-09-09 18:57:09 -03:00
|
|
|
while (1) {
|
|
|
|
if (c == '_') {
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
if (!isdigit(c)) {
|
|
|
|
tok_backup(tok, c);
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "invalid decimal literal"));
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c != '0') {
|
|
|
|
break;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
c = tok_nextc(tok);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2021-07-09 21:29:29 -03:00
|
|
|
char* zeros_end = tok->cur;
|
2016-09-09 18:57:09 -03:00
|
|
|
if (isdigit(c)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
nonzero = 1;
|
2016-09-09 18:57:09 -03:00
|
|
|
c = tok_decimal_tail(tok);
|
|
|
|
if (c == 0) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == '.') {
|
|
|
|
c = tok_nextc(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
goto fraction;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
else if (c == 'e' || c == 'E') {
|
2010-05-09 12:52:27 -03:00
|
|
|
goto exponent;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
else if (c == 'j' || c == 'J') {
|
2010-05-09 12:52:27 -03:00
|
|
|
goto imaginary;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
else if (nonzero) {
|
2016-09-09 18:57:09 -03:00
|
|
|
/* Old-style octal: now disallowed. */
|
2010-05-09 12:52:27 -03:00
|
|
|
tok_backup(tok, c);
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror_known_range(
|
2021-07-09 21:29:29 -03:00
|
|
|
tok, (int)(tok->start + 1 - tok->line_start),
|
|
|
|
(int)(zeros_end - tok->line_start),
|
|
|
|
"leading zeros in decimal integer "
|
|
|
|
"literals are not permitted; "
|
2022-10-06 20:07:17 -03:00
|
|
|
"use an 0o prefix for octal integers"));
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-06-08 20:31:10 -03:00
|
|
|
if (!verify_end_of_number(tok, c, "decimal")) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2021-06-08 20:31:10 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Decimal */
|
2016-09-09 18:57:09 -03:00
|
|
|
c = tok_decimal_tail(tok);
|
|
|
|
if (c == 0) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
{
|
|
|
|
/* Accept floating point numbers. */
|
|
|
|
if (c == '.') {
|
2016-09-09 18:57:09 -03:00
|
|
|
c = tok_nextc(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
fraction:
|
|
|
|
/* Fraction */
|
2016-09-09 18:57:09 -03:00
|
|
|
if (isdigit(c)) {
|
|
|
|
c = tok_decimal_tail(tok);
|
|
|
|
if (c == 0) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
if (c == 'e' || c == 'E') {
|
2014-06-07 16:36:39 -03:00
|
|
|
int e;
|
|
|
|
exponent:
|
|
|
|
e = c;
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Exponent part */
|
|
|
|
c = tok_nextc(tok);
|
2014-06-07 16:36:39 -03:00
|
|
|
if (c == '+' || c == '-') {
|
2010-05-09 12:52:27 -03:00
|
|
|
c = tok_nextc(tok);
|
2014-06-07 16:36:39 -03:00
|
|
|
if (!isdigit(c)) {
|
|
|
|
tok_backup(tok, c);
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "invalid decimal literal"));
|
2014-06-07 16:36:39 -03:00
|
|
|
}
|
|
|
|
} else if (!isdigit(c)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok_backup(tok, c);
|
2021-06-08 20:31:10 -03:00
|
|
|
if (!verify_end_of_number(tok, e, "decimal")) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2021-06-08 20:31:10 -03:00
|
|
|
}
|
2014-06-07 16:36:39 -03:00
|
|
|
tok_backup(tok, e);
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(NUMBER);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
c = tok_decimal_tail(tok);
|
|
|
|
if (c == 0) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == 'j' || c == 'J') {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Imaginary part */
|
|
|
|
imaginary:
|
|
|
|
c = tok_nextc(tok);
|
2021-06-08 20:31:10 -03:00
|
|
|
if (!verify_end_of_number(tok, c, "imaginary")) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2021-06-08 20:31:10 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!verify_end_of_number(tok, c, "decimal")) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tok_backup(tok, c);
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(NUMBER);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
1997-04-05 23:46:20 -04:00
|
|
|
|
2023-04-19 13:18:16 -03:00
|
|
|
f_string_quote:
|
|
|
|
if (((tolower(*tok->start) == 'f' || tolower(*tok->start) == 'r') && (c == '\'' || c == '"'))) {
|
|
|
|
int quote = c;
|
|
|
|
int quote_size = 1; /* 1 or 3 */
|
|
|
|
|
|
|
|
/* Nodes of type STRING, especially multi line strings
|
|
|
|
must be handled differently in order to get both
|
|
|
|
the starting line number and the column offset right.
|
|
|
|
(cf. issue 16806) */
|
|
|
|
tok->first_lineno = tok->lineno;
|
|
|
|
tok->multi_line_start = tok->line_start;
|
|
|
|
|
|
|
|
/* Find the quote size and start of string */
|
|
|
|
int after_quote = tok_nextc(tok);
|
|
|
|
if (after_quote == quote) {
|
|
|
|
int after_after_quote = tok_nextc(tok);
|
|
|
|
if (after_after_quote == quote) {
|
|
|
|
quote_size = 3;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// TODO: Check this
|
|
|
|
tok_backup(tok, after_after_quote);
|
|
|
|
tok_backup(tok, after_quote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (after_quote != quote) {
|
|
|
|
tok_backup(tok, after_quote);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
2023-05-01 16:15:47 -03:00
|
|
|
if (tok->tok_mode_stack_index + 1 >= MAXFSTRINGLEVEL) {
|
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "too many nested f-strings"));
|
|
|
|
}
|
2023-04-19 17:51:31 -03:00
|
|
|
tokenizer_mode *the_current_tok = TOK_NEXT_MODE(tok);
|
|
|
|
the_current_tok->kind = TOK_FSTRING_MODE;
|
|
|
|
the_current_tok->f_string_quote = quote;
|
|
|
|
the_current_tok->f_string_quote_size = quote_size;
|
|
|
|
the_current_tok->f_string_start = tok->start;
|
|
|
|
the_current_tok->f_string_multi_line_start = tok->line_start;
|
2023-04-23 16:06:10 -03:00
|
|
|
the_current_tok->f_string_start_offset = -1;
|
|
|
|
the_current_tok->f_string_multi_line_start_offset = -1;
|
2023-04-19 17:51:31 -03:00
|
|
|
the_current_tok->last_expr_buffer = NULL;
|
|
|
|
the_current_tok->last_expr_size = 0;
|
|
|
|
the_current_tok->last_expr_end = -1;
|
2023-04-26 22:33:31 -03:00
|
|
|
the_current_tok->f_string_debug = 0;
|
2023-04-19 13:18:16 -03:00
|
|
|
|
|
|
|
switch (*tok->start) {
|
|
|
|
case 'F':
|
|
|
|
case 'f':
|
2023-04-19 17:51:31 -03:00
|
|
|
the_current_tok->f_string_raw = tolower(*(tok->start + 1)) == 'r';
|
2023-04-19 13:18:16 -03:00
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
case 'r':
|
2023-04-19 17:51:31 -03:00
|
|
|
the_current_tok->f_string_raw = 1;
|
2023-04-19 13:18:16 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Py_UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2023-04-19 17:51:31 -03:00
|
|
|
the_current_tok->curly_bracket_depth = 0;
|
|
|
|
the_current_tok->curly_bracket_expr_start_depth = -1;
|
2023-04-19 13:18:16 -03:00
|
|
|
return MAKE_TOKEN(FSTRING_START);
|
|
|
|
}
|
|
|
|
|
1997-04-05 23:46:20 -04:00
|
|
|
letter_quote:
|
2010-05-09 12:52:27 -03:00
|
|
|
/* String */
|
|
|
|
if (c == '\'' || c == '"') {
|
|
|
|
int quote = c;
|
|
|
|
int quote_size = 1; /* 1 or 3 */
|
|
|
|
int end_quote_size = 0;
|
|
|
|
|
2019-01-13 00:05:13 -04:00
|
|
|
/* Nodes of type STRING, especially multi line strings
|
|
|
|
must be handled differently in order to get both
|
|
|
|
the starting line number and the column offset right.
|
|
|
|
(cf. issue 16806) */
|
|
|
|
tok->first_lineno = tok->lineno;
|
|
|
|
tok->multi_line_start = tok->line_start;
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Find the quote size and start of string */
|
|
|
|
c = tok_nextc(tok);
|
|
|
|
if (c == quote) {
|
|
|
|
c = tok_nextc(tok);
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == quote) {
|
2010-05-09 12:52:27 -03:00
|
|
|
quote_size = 3;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
end_quote_size = 1; /* empty string found */
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c != quote) {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok_backup(tok, c);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* Get rest of string */
|
|
|
|
while (end_quote_size != quote_size) {
|
|
|
|
c = tok_nextc(tok);
|
2023-05-04 09:26:23 -03:00
|
|
|
if (tok->done == E_ERROR) {
|
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
|
|
|
}
|
|
|
|
if (tok->done == E_DECODE) {
|
2022-09-06 20:12:16 -03:00
|
|
|
break;
|
2023-05-04 09:26:23 -03:00
|
|
|
}
|
2021-01-20 17:38:47 -04:00
|
|
|
if (c == EOF || (quote_size == 1 && c == '\n')) {
|
2021-06-12 14:53:49 -03:00
|
|
|
assert(tok->multi_line_start != NULL);
|
2021-01-20 17:38:47 -04:00
|
|
|
// shift the tok_state's location into
|
|
|
|
// the start of string, and report the error
|
|
|
|
// from the initial quote character
|
|
|
|
tok->cur = (char *)tok->start;
|
|
|
|
tok->cur++;
|
|
|
|
tok->line_start = tok->multi_line_start;
|
|
|
|
int start = tok->lineno;
|
|
|
|
tok->lineno = tok->first_lineno;
|
2023-04-19 13:18:16 -03:00
|
|
|
|
2023-04-19 17:51:31 -03:00
|
|
|
if (INSIDE_FSTRING(tok)) {
|
2023-04-19 13:18:16 -03:00
|
|
|
/* When we are in an f-string, before raising the
|
|
|
|
* unterminated string literal error, check whether
|
|
|
|
* does the initial quote matches with f-strings quotes
|
|
|
|
* and if it is, then this must be a missing '}' token
|
|
|
|
* so raise the proper error */
|
2023-04-19 17:51:31 -03:00
|
|
|
tokenizer_mode *the_current_tok = TOK_GET_MODE(tok);
|
|
|
|
if (the_current_tok->f_string_quote == quote &&
|
|
|
|
the_current_tok->f_string_quote_size == quote_size) {
|
2023-04-19 13:18:16 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "f-string: expecting '}'", start));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:57:09 -03:00
|
|
|
if (quote_size == 3) {
|
2022-02-08 07:54:37 -04:00
|
|
|
syntaxerror(tok, "unterminated triple-quoted string literal"
|
|
|
|
" (detected at line %d)", start);
|
|
|
|
if (c != '\n') {
|
|
|
|
tok->done = E_EOFS;
|
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
else {
|
2022-02-08 07:54:37 -04:00
|
|
|
syntaxerror(tok, "unterminated string literal (detected at"
|
|
|
|
" line %d)", start);
|
|
|
|
if (c != '\n') {
|
|
|
|
tok->done = E_EOLS;
|
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == quote) {
|
2010-05-09 12:52:27 -03:00
|
|
|
end_quote_size += 1;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
else {
|
|
|
|
end_quote_size = 0;
|
2016-09-09 18:57:09 -03:00
|
|
|
if (c == '\\') {
|
2016-09-08 19:09:45 -03:00
|
|
|
tok_nextc(tok); /* skip escaped char */
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(STRING);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Line continuation */
|
|
|
|
if (c == '\\') {
|
2022-01-25 18:12:14 -04:00
|
|
|
if ((c = tok_continuation_line(tok)) == -1) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
tok->cont_line = 1;
|
|
|
|
goto again; /* Read next line */
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:18:16 -03:00
|
|
|
/* Punctuation character */
|
|
|
|
int is_punctuation = (c == ':' || c == '}' || c == '!' || c == '{');
|
2023-04-19 17:51:31 -03:00
|
|
|
if (is_punctuation && INSIDE_FSTRING(tok) && INSIDE_FSTRING_EXPR(current_tok)) {
|
|
|
|
/* This code block gets executed before the curly_bracket_depth is incremented
|
2023-04-19 13:18:16 -03:00
|
|
|
* by the `{` case, so for ensuring that we are on the 0th level, we need
|
|
|
|
* to adjust it manually */
|
2023-04-19 17:51:31 -03:00
|
|
|
int cursor = current_tok->curly_bracket_depth - (c != '{');
|
2023-04-19 13:18:16 -03:00
|
|
|
if (cursor == 0 && !update_fstring_expr(tok, c)) {
|
|
|
|
return MAKE_TOKEN(ENDMARKER);
|
|
|
|
}
|
2023-04-26 22:33:31 -03:00
|
|
|
if (cursor == 0 && c != '{' && set_fstring_expr(tok, token, c)) {
|
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
|
|
|
}
|
2023-04-19 13:18:16 -03:00
|
|
|
|
2023-04-19 17:51:31 -03:00
|
|
|
if (c == ':' && cursor == current_tok->curly_bracket_expr_start_depth) {
|
2023-04-19 13:18:16 -03:00
|
|
|
current_tok->kind = TOK_FSTRING_MODE;
|
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(_PyToken_OneChar(c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Check for two-character token */
|
|
|
|
{
|
|
|
|
int c2 = tok_nextc(tok);
|
2022-10-06 20:07:17 -03:00
|
|
|
int current_token = _PyToken_TwoChars(c, c2);
|
|
|
|
if (current_token != OP) {
|
2010-05-09 12:52:27 -03:00
|
|
|
int c3 = tok_nextc(tok);
|
2022-10-06 20:07:17 -03:00
|
|
|
int current_token3 = _PyToken_ThreeChars(c, c2, c3);
|
|
|
|
if (current_token3 != OP) {
|
|
|
|
current_token = current_token3;
|
2016-09-09 18:57:09 -03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-09 12:52:27 -03:00
|
|
|
tok_backup(tok, c3);
|
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(current_token);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
tok_backup(tok, c2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep track of parentheses nesting level */
|
|
|
|
switch (c) {
|
|
|
|
case '(':
|
|
|
|
case '[':
|
|
|
|
case '{':
|
2018-12-17 11:34:14 -04:00
|
|
|
if (tok->level >= MAXLEVEL) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "too many nested parentheses"));
|
2018-12-17 11:34:14 -04:00
|
|
|
}
|
|
|
|
tok->parenstack[tok->level] = c;
|
|
|
|
tok->parenlinenostack[tok->level] = tok->lineno;
|
2021-01-20 08:53:52 -04:00
|
|
|
tok->parencolstack[tok->level] = (int)(tok->start - tok->line_start);
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->level++;
|
2023-04-19 17:51:31 -03:00
|
|
|
if (INSIDE_FSTRING(tok)) {
|
|
|
|
current_tok->curly_bracket_depth++;
|
2023-04-19 13:18:16 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
break;
|
|
|
|
case ')':
|
|
|
|
case ']':
|
|
|
|
case '}':
|
2018-12-17 11:34:14 -04:00
|
|
|
if (!tok->level) {
|
2023-04-19 17:51:31 -03:00
|
|
|
if (INSIDE_FSTRING(tok) && !current_tok->curly_bracket_depth && c == '}') {
|
2023-04-19 13:18:16 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "f-string: single '}' is not allowed"));
|
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "unmatched '%c'", c));
|
2018-12-17 11:34:14 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
tok->level--;
|
2018-12-17 11:34:14 -04:00
|
|
|
int opening = tok->parenstack[tok->level];
|
|
|
|
if (!((opening == '(' && c == ')') ||
|
|
|
|
(opening == '[' && c == ']') ||
|
|
|
|
(opening == '{' && c == '}')))
|
|
|
|
{
|
2023-04-19 13:18:16 -03:00
|
|
|
/* If the opening bracket belongs to an f-string's expression
|
|
|
|
part (e.g. f"{)}") and the closing bracket is an arbitrary
|
|
|
|
nested expression, then instead of matching a different
|
|
|
|
syntactical construct with it; we'll throw an unmatched
|
|
|
|
parentheses error. */
|
2023-04-19 17:51:31 -03:00
|
|
|
if (INSIDE_FSTRING(tok) && opening == '{') {
|
|
|
|
assert(current_tok->curly_bracket_depth >= 0);
|
|
|
|
int previous_bracket = current_tok->curly_bracket_depth - 1;
|
|
|
|
if (previous_bracket == current_tok->curly_bracket_expr_start_depth) {
|
2023-04-19 13:18:16 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "f-string: unmatched '%c'", c));
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 11:34:14 -04:00
|
|
|
if (tok->parenlinenostack[tok->level] != tok->lineno) {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok,
|
2018-12-17 11:34:14 -04:00
|
|
|
"closing parenthesis '%c' does not match "
|
|
|
|
"opening parenthesis '%c' on line %d",
|
2022-10-06 20:07:17 -03:00
|
|
|
c, opening, tok->parenlinenostack[tok->level]));
|
2018-12-17 11:34:14 -04:00
|
|
|
}
|
|
|
|
else {
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok,
|
2018-12-17 11:34:14 -04:00
|
|
|
"closing parenthesis '%c' does not match "
|
|
|
|
"opening parenthesis '%c'",
|
2022-10-06 20:07:17 -03:00
|
|
|
c, opening));
|
2018-12-17 11:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
2023-04-19 13:18:16 -03:00
|
|
|
|
2023-04-19 17:51:31 -03:00
|
|
|
if (INSIDE_FSTRING(tok)) {
|
|
|
|
current_tok->curly_bracket_depth--;
|
|
|
|
if (c == '}' && current_tok->curly_bracket_depth == current_tok->curly_bracket_expr_start_depth) {
|
|
|
|
current_tok->curly_bracket_expr_start_depth--;
|
2023-04-19 13:18:16 -03:00
|
|
|
current_tok->kind = TOK_FSTRING_MODE;
|
2023-04-26 22:33:31 -03:00
|
|
|
current_tok->f_string_debug = 0;
|
2023-04-19 13:18:16 -03:00
|
|
|
}
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
break;
|
2023-04-19 17:51:31 -03:00
|
|
|
default:
|
|
|
|
break;
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
|
2021-11-20 14:28:28 -04:00
|
|
|
if (!Py_UNICODE_ISPRINTABLE(c)) {
|
|
|
|
char hex[9];
|
|
|
|
(void)PyOS_snprintf(hex, sizeof(hex), "%04X", c);
|
2022-10-06 20:07:17 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "invalid non-printable character U+%s", hex));
|
2021-11-20 14:28:28 -04:00
|
|
|
}
|
|
|
|
|
2023-04-26 22:33:31 -03:00
|
|
|
if( c == '=' && INSIDE_FSTRING_EXPR(current_tok)) {
|
|
|
|
current_tok->f_string_debug = 1;
|
|
|
|
}
|
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Punctuation character */
|
2022-10-06 20:07:17 -03:00
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(_PyToken_OneChar(c));
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
2023-04-19 13:18:16 -03:00
|
|
|
static int
|
|
|
|
tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct token *token)
|
|
|
|
{
|
|
|
|
const char *p_start = NULL;
|
|
|
|
const char *p_end = NULL;
|
|
|
|
int end_quote_size = 0;
|
|
|
|
int unicode_escape = 0;
|
|
|
|
|
|
|
|
tok->start = tok->cur;
|
|
|
|
tok->first_lineno = tok->lineno;
|
|
|
|
tok->starting_col_offset = tok->col_offset;
|
|
|
|
|
|
|
|
// If we start with a bracket, we defer to the normal mode as there is nothing for us to tokenize
|
|
|
|
// before it.
|
|
|
|
int start_char = tok_nextc(tok);
|
2023-04-24 15:30:21 -03:00
|
|
|
if (start_char == '{') {
|
|
|
|
int peek1 = tok_nextc(tok);
|
|
|
|
tok_backup(tok, peek1);
|
|
|
|
tok_backup(tok, start_char);
|
|
|
|
if (peek1 != '{') {
|
2023-04-19 17:51:31 -03:00
|
|
|
current_tok->curly_bracket_expr_start_depth++;
|
|
|
|
if (current_tok->curly_bracket_expr_start_depth >= MAX_EXPR_NESTING) {
|
2023-04-19 13:18:16 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "f-string: expressions nested too deeply"));
|
|
|
|
}
|
2023-04-24 15:30:21 -03:00
|
|
|
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
|
|
|
|
return tok_get_normal_mode(tok, current_tok, token);
|
2023-04-19 13:18:16 -03:00
|
|
|
}
|
2023-04-24 15:30:21 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
tok_backup(tok, start_char);
|
2023-04-19 13:18:16 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we are at the end of the string
|
|
|
|
for (int i = 0; i < current_tok->f_string_quote_size; i++) {
|
|
|
|
int quote = tok_nextc(tok);
|
|
|
|
if (quote != current_tok->f_string_quote) {
|
|
|
|
tok_backup(tok, quote);
|
|
|
|
goto f_string_middle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_tok->last_expr_buffer != NULL) {
|
|
|
|
PyMem_Free(current_tok->last_expr_buffer);
|
|
|
|
current_tok->last_expr_buffer = NULL;
|
|
|
|
current_tok->last_expr_size = 0;
|
|
|
|
current_tok->last_expr_end = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
tok->tok_mode_stack_index--;
|
|
|
|
return MAKE_TOKEN(FSTRING_END);
|
|
|
|
|
|
|
|
f_string_middle:
|
|
|
|
|
|
|
|
while (end_quote_size != current_tok->f_string_quote_size) {
|
|
|
|
int c = tok_nextc(tok);
|
2023-05-04 09:26:23 -03:00
|
|
|
if (tok->done == E_ERROR) {
|
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
|
|
|
}
|
2023-04-19 13:18:16 -03:00
|
|
|
if (c == EOF || (current_tok->f_string_quote_size == 1 && c == '\n')) {
|
2023-05-01 12:26:43 -03:00
|
|
|
if (tok->decoding_erred) {
|
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
|
|
|
}
|
|
|
|
|
2023-04-19 13:18:16 -03:00
|
|
|
assert(tok->multi_line_start != NULL);
|
|
|
|
// shift the tok_state's location into
|
|
|
|
// the start of string, and report the error
|
|
|
|
// from the initial quote character
|
|
|
|
tok->cur = (char *)current_tok->f_string_start;
|
|
|
|
tok->cur++;
|
|
|
|
tok->line_start = current_tok->f_string_multi_line_start;
|
|
|
|
int start = tok->lineno;
|
|
|
|
tok->lineno = tok->first_lineno;
|
|
|
|
|
|
|
|
if (current_tok->f_string_quote_size == 3) {
|
|
|
|
return MAKE_TOKEN(syntaxerror(tok,
|
|
|
|
"unterminated triple-quoted f-string literal"
|
|
|
|
" (detected at line %d)", start));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return MAKE_TOKEN(syntaxerror(tok,
|
|
|
|
"unterminated f-string literal (detected at"
|
|
|
|
" line %d)", start));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == current_tok->f_string_quote) {
|
|
|
|
end_quote_size += 1;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
end_quote_size = 0;
|
|
|
|
}
|
|
|
|
|
2023-04-19 17:51:31 -03:00
|
|
|
int in_format_spec = (
|
|
|
|
current_tok->last_expr_end != -1
|
|
|
|
&&
|
|
|
|
INSIDE_FSTRING_EXPR(current_tok)
|
|
|
|
);
|
2023-04-19 13:18:16 -03:00
|
|
|
if (c == '{') {
|
|
|
|
int peek = tok_nextc(tok);
|
|
|
|
if (peek != '{' || in_format_spec) {
|
|
|
|
tok_backup(tok, peek);
|
|
|
|
tok_backup(tok, c);
|
2023-04-19 17:51:31 -03:00
|
|
|
current_tok->curly_bracket_expr_start_depth++;
|
|
|
|
if (current_tok->curly_bracket_expr_start_depth >= MAX_EXPR_NESTING) {
|
2023-04-19 13:18:16 -03:00
|
|
|
return MAKE_TOKEN(syntaxerror(tok, "f-string: expressions nested too deeply"));
|
|
|
|
}
|
|
|
|
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
|
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
} else {
|
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur - 1;
|
|
|
|
}
|
|
|
|
return MAKE_TOKEN(FSTRING_MIDDLE);
|
|
|
|
} else if (c == '}') {
|
|
|
|
if (unicode_escape) {
|
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(FSTRING_MIDDLE);
|
|
|
|
}
|
|
|
|
int peek = tok_nextc(tok);
|
|
|
|
|
|
|
|
// The tokenizer can only be in the format spec if we have already completed the expression
|
|
|
|
// scanning (indicated by the end of the expression being set) and we are not at the top level
|
|
|
|
// of the bracket stack (-1 is the top level). Since format specifiers can't legally use double
|
|
|
|
// brackets, we can bypass it here.
|
|
|
|
if (peek == '}' && !in_format_spec) {
|
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur - 1;
|
|
|
|
} else {
|
|
|
|
tok_backup(tok, peek);
|
|
|
|
tok_backup(tok, c);
|
|
|
|
TOK_GET_MODE(tok)->kind = TOK_REGULAR_MODE;
|
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
}
|
|
|
|
return MAKE_TOKEN(FSTRING_MIDDLE);
|
|
|
|
} else if (c == '\\') {
|
|
|
|
int peek = tok_nextc(tok);
|
|
|
|
// Special case when the backslash is right before a curly
|
|
|
|
// brace. We have to restore and return the control back
|
|
|
|
// to the loop for the next iteration.
|
|
|
|
if (peek == '{' || peek == '}') {
|
|
|
|
if (!current_tok->f_string_raw) {
|
|
|
|
if (warn_invalid_escape_sequence(tok, peek)) {
|
|
|
|
return MAKE_TOKEN(ERRORTOKEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tok_backup(tok, peek);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!current_tok->f_string_raw) {
|
|
|
|
if (peek == 'N') {
|
|
|
|
/* Handle named unicode escapes (\N{BULLET}) */
|
|
|
|
peek = tok_nextc(tok);
|
|
|
|
if (peek == '{') {
|
|
|
|
unicode_escape = 1;
|
|
|
|
} else {
|
|
|
|
tok_backup(tok, peek);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* else {
|
|
|
|
skip the escaped character
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backup the f-string quotes to emit a final FSTRING_MIDDLE and
|
|
|
|
// add the quotes to the FSTRING_END in the next tokenizer iteration.
|
|
|
|
for (int i = 0; i < current_tok->f_string_quote_size; i++) {
|
|
|
|
tok_backup(tok, current_tok->f_string_quote);
|
|
|
|
}
|
|
|
|
p_start = tok->start;
|
|
|
|
p_end = tok->cur;
|
|
|
|
return MAKE_TOKEN(FSTRING_MIDDLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
tok_get(struct tok_state *tok, struct token *token)
|
|
|
|
{
|
|
|
|
tokenizer_mode *current_tok = TOK_GET_MODE(tok);
|
|
|
|
if (current_tok->kind == TOK_REGULAR_MODE) {
|
|
|
|
return tok_get_normal_mode(tok, current_tok, token);
|
|
|
|
} else {
|
|
|
|
return tok_get_fstring_mode(tok, current_tok, token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-04 14:29:52 -03:00
|
|
|
int
|
2022-10-06 20:07:17 -03:00
|
|
|
_PyTokenizer_Get(struct tok_state *tok, struct token *token)
|
2002-08-04 14:29:52 -03:00
|
|
|
{
|
2022-10-06 20:07:17 -03:00
|
|
|
int result = tok_get(tok, token);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (tok->decoding_erred) {
|
|
|
|
result = ERRORTOKEN;
|
|
|
|
tok->done = E_DECODE;
|
|
|
|
}
|
|
|
|
return result;
|
2002-08-04 14:29:52 -03:00
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2022-04-02 18:11:38 -03:00
|
|
|
#if defined(__wasi__) || (defined(__EMSCRIPTEN__) && (__EMSCRIPTEN_major__ >= 3))
|
2022-03-22 13:08:51 -03:00
|
|
|
// fdopen() with borrowed fd. WASI does not provide dup() and Emscripten's
|
|
|
|
// dup() emulation with open() is slow.
|
|
|
|
typedef union {
|
|
|
|
void *cookie;
|
|
|
|
int fd;
|
|
|
|
} borrowed;
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
borrow_read(void *cookie, char *buf, size_t size)
|
|
|
|
{
|
|
|
|
borrowed b = {.cookie = cookie};
|
|
|
|
return read(b.fd, (void *)buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FILE *
|
|
|
|
fdopen_borrow(int fd) {
|
|
|
|
// supports only reading. seek fails. close and write are no-ops.
|
|
|
|
cookie_io_functions_t io_cb = {borrow_read, NULL, NULL, NULL};
|
|
|
|
borrowed b = {.fd = fd};
|
|
|
|
return fopencookie(b.cookie, "r", io_cb);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static FILE *
|
|
|
|
fdopen_borrow(int fd) {
|
|
|
|
fd = _Py_dup(fd);
|
|
|
|
if (fd < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return fdopen(fd, "r");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-04 20:48:03 -03:00
|
|
|
/* Get the encoding of a Python file. Check for the coding cookie and check if
|
|
|
|
the file starts with a BOM.
|
2007-10-19 20:16:50 -03:00
|
|
|
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_FindEncodingFilename() returns NULL when it can't find the
|
2011-04-04 20:48:03 -03:00
|
|
|
encoding in the first or second line of the file (in which case the encoding
|
|
|
|
should be assumed to be UTF-8).
|
|
|
|
|
2020-12-01 04:56:42 -04:00
|
|
|
The char* returned is malloc'ed via PyMem_Malloc() and thus must be freed
|
2011-04-04 20:48:03 -03:00
|
|
|
by the caller. */
|
2007-10-20 00:46:49 -03:00
|
|
|
|
2007-10-19 20:16:50 -03:00
|
|
|
char *
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_FindEncodingFilename(int fd, PyObject *filename)
|
2007-10-21 21:09:51 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
struct tok_state *tok;
|
|
|
|
FILE *fp;
|
2020-02-27 22:44:52 -04:00
|
|
|
char *encoding = NULL;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2022-03-22 13:08:51 -03:00
|
|
|
fp = fdopen_borrow(fd);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (fp == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-10-13 12:22:14 -03:00
|
|
|
tok = _PyTokenizer_FromFile(fp, NULL, NULL, NULL);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (tok == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-04 20:48:03 -03:00
|
|
|
if (filename != NULL) {
|
2022-11-10 10:30:05 -04:00
|
|
|
tok->filename = Py_NewRef(filename);
|
2011-04-04 20:48:03 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
tok->filename = PyUnicode_FromString("<string>");
|
|
|
|
if (tok->filename == NULL) {
|
|
|
|
fclose(fp);
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_Free(tok);
|
2011-04-04 20:48:03 -03:00
|
|
|
return encoding;
|
|
|
|
}
|
|
|
|
}
|
2022-10-06 20:07:17 -03:00
|
|
|
struct token token;
|
2022-11-30 07:36:06 -04:00
|
|
|
// We don't want to report warnings here because it could cause infinite recursion
|
|
|
|
// if fetching the encoding shows a warning.
|
|
|
|
tok->report_warnings = 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
while (tok->lineno < 2 && tok->done == E_OK) {
|
2022-10-06 20:07:17 -03:00
|
|
|
_PyTokenizer_Get(tok, &token);
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
if (tok->encoding) {
|
2020-12-01 04:56:42 -04:00
|
|
|
encoding = (char *)PyMem_Malloc(strlen(tok->encoding) + 1);
|
2021-03-28 19:48:05 -03:00
|
|
|
if (encoding) {
|
2019-08-15 13:19:07 -03:00
|
|
|
strcpy(encoding, tok->encoding);
|
2021-03-28 19:48:05 -03:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2021-10-13 12:22:14 -03:00
|
|
|
_PyTokenizer_Free(tok);
|
2010-05-09 12:52:27 -03:00
|
|
|
return encoding;
|
2007-10-19 20:16:50 -03:00
|
|
|
}
|
Merged revisions 57778-58052 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r57820 | georg.brandl | 2007-08-31 08:59:27 +0200 (Fri, 31 Aug 2007) | 2 lines
Document new shorthand notation for index entries.
........
r57827 | georg.brandl | 2007-08-31 10:47:51 +0200 (Fri, 31 Aug 2007) | 2 lines
Fix subitem markup.
........
r57833 | martin.v.loewis | 2007-08-31 12:01:07 +0200 (Fri, 31 Aug 2007) | 1 line
Mark registry components as 64-bit on Win64.
........
r57854 | bill.janssen | 2007-08-31 21:02:23 +0200 (Fri, 31 Aug 2007) | 1 line
deprecate use of FakeSocket
........
r57855 | bill.janssen | 2007-08-31 21:02:46 +0200 (Fri, 31 Aug 2007) | 1 line
remove mentions of socket.ssl in comments
........
r57856 | bill.janssen | 2007-08-31 21:03:31 +0200 (Fri, 31 Aug 2007) | 1 line
remove use of non-existent SSLFakeSocket in apparently untested code
........
r57859 | martin.v.loewis | 2007-09-01 08:36:03 +0200 (Sat, 01 Sep 2007) | 3 lines
Bug #1737210: Change Manufacturer of Windows installer to PSF.
Will backport to 2.5.
........
r57865 | georg.brandl | 2007-09-01 09:51:24 +0200 (Sat, 01 Sep 2007) | 2 lines
Fix RST link (backport from Py3k).
........
r57876 | georg.brandl | 2007-09-01 17:49:49 +0200 (Sat, 01 Sep 2007) | 2 lines
Document sets' ">" and "<" operations (backport from py3k).
........
r57878 | skip.montanaro | 2007-09-01 19:40:03 +0200 (Sat, 01 Sep 2007) | 4 lines
Added a note and examples to explain that re.split does not split on an
empty pattern match. (issue 852532).
........
r57879 | walter.doerwald | 2007-09-01 20:18:09 +0200 (Sat, 01 Sep 2007) | 2 lines
Fix wrong function names.
........
r57880 | walter.doerwald | 2007-09-01 20:34:05 +0200 (Sat, 01 Sep 2007) | 2 lines
Fix typo.
........
r57889 | andrew.kuchling | 2007-09-01 22:31:59 +0200 (Sat, 01 Sep 2007) | 1 line
Markup fix
........
r57892 | andrew.kuchling | 2007-09-01 22:43:36 +0200 (Sat, 01 Sep 2007) | 1 line
Add various items
........
r57895 | andrew.kuchling | 2007-09-01 23:17:58 +0200 (Sat, 01 Sep 2007) | 1 line
Wording change
........
r57896 | andrew.kuchling | 2007-09-01 23:18:31 +0200 (Sat, 01 Sep 2007) | 1 line
Add more items
........
r57904 | ronald.oussoren | 2007-09-02 11:46:07 +0200 (Sun, 02 Sep 2007) | 3 lines
Macosx: this patch ensures that the value of MACOSX_DEPLOYMENT_TARGET used
by the Makefile is also used at configure-time.
........
r57925 | georg.brandl | 2007-09-03 09:16:46 +0200 (Mon, 03 Sep 2007) | 2 lines
Fix #883466: don't allow Unicode as arguments to quopri and uu codecs.
........
r57936 | matthias.klose | 2007-09-04 01:33:04 +0200 (Tue, 04 Sep 2007) | 2 lines
- Added support for linking the bsddb module against BerkeleyDB 4.6.x.
........
r57954 | mark.summerfield | 2007-09-04 10:16:15 +0200 (Tue, 04 Sep 2007) | 3 lines
Added cross-references plus a note about dict & list shallow copying.
........
r57958 | martin.v.loewis | 2007-09-04 11:51:57 +0200 (Tue, 04 Sep 2007) | 3 lines
Document that we rely on the OS to release the crypto
context. Fixes #1626801.
........
r57960 | martin.v.loewis | 2007-09-04 15:13:14 +0200 (Tue, 04 Sep 2007) | 3 lines
Patch #1388440: Add set_completion_display_matches_hook and
get_completion_type to readline.
........
r57961 | martin.v.loewis | 2007-09-04 16:19:28 +0200 (Tue, 04 Sep 2007) | 3 lines
Patch #1031213: Decode source line in SyntaxErrors back to its original
source encoding. Will backport to 2.5.
........
r57972 | matthias.klose | 2007-09-04 20:17:36 +0200 (Tue, 04 Sep 2007) | 3 lines
- Makefile.pre.in(buildbottest): Run an optional script pybuildbot.identify
to include some information about the build environment.
........
r57973 | matthias.klose | 2007-09-04 21:05:38 +0200 (Tue, 04 Sep 2007) | 2 lines
- Makefile.pre.in(buildbottest): Remove whitespace at eol.
........
r57975 | matthias.klose | 2007-09-04 22:46:02 +0200 (Tue, 04 Sep 2007) | 2 lines
- Fix libffi configure for hppa*-*-linux* | parisc*-*-linux*.
........
r57980 | bill.janssen | 2007-09-05 02:46:27 +0200 (Wed, 05 Sep 2007) | 1 line
SSL certificate distinguished names should be represented by tuples
........
r57985 | martin.v.loewis | 2007-09-05 08:39:17 +0200 (Wed, 05 Sep 2007) | 3 lines
Patch #1105: Explain that one needs to build the solution
to get dependencies right.
........
r57987 | armin.rigo | 2007-09-05 09:51:21 +0200 (Wed, 05 Sep 2007) | 4 lines
PyDict_GetItem() returns a borrowed reference.
There are probably a number of places that are open to attacks
such as the following one, in bltinmodule.c:min_max().
........
r57991 | martin.v.loewis | 2007-09-05 13:47:34 +0200 (Wed, 05 Sep 2007) | 3 lines
Patch #786737: Allow building in a tree of symlinks pointing to
a readonly source.
........
r57993 | georg.brandl | 2007-09-05 15:36:44 +0200 (Wed, 05 Sep 2007) | 2 lines
Backport from Py3k: Bug #1684991: explain lookup semantics for __special__ methods (new-style classes only).
........
r58004 | armin.rigo | 2007-09-06 10:30:51 +0200 (Thu, 06 Sep 2007) | 4 lines
Patch #1733973 by peaker:
ptrace_enter_call() assumes no exception is currently set.
This assumption is broken when throwing into a generator.
........
r58006 | armin.rigo | 2007-09-06 11:30:38 +0200 (Thu, 06 Sep 2007) | 4 lines
PyDict_GetItem() returns a borrowed reference.
This attack is against ceval.c:IMPORT_NAME, which calls an
object (__builtin__.__import__) without holding a reference to it.
........
r58013 | georg.brandl | 2007-09-06 16:49:56 +0200 (Thu, 06 Sep 2007) | 2 lines
Backport from 3k: #1116: fix reference to old filename.
........
r58021 | thomas.heller | 2007-09-06 22:26:20 +0200 (Thu, 06 Sep 2007) | 1 line
Fix typo: c_float represents to C float type.
........
r58022 | skip.montanaro | 2007-09-07 00:29:06 +0200 (Fri, 07 Sep 2007) | 3 lines
If this is correct for py3k branch and it's already in the release25-maint
branch, seems like it ought to be on the trunk as well.
........
r58023 | gregory.p.smith | 2007-09-07 00:59:59 +0200 (Fri, 07 Sep 2007) | 4 lines
Apply the fix from Issue1112 to make this test more robust and keep
windows happy.
........
r58031 | brett.cannon | 2007-09-07 05:17:50 +0200 (Fri, 07 Sep 2007) | 4 lines
Make uuid1 and uuid4 tests conditional on whether ctypes can be imported;
implementation of either function depends on ctypes but uuid as a whole does
not.
........
r58032 | brett.cannon | 2007-09-07 06:18:30 +0200 (Fri, 07 Sep 2007) | 6 lines
Fix a crasher where Python code managed to infinitely recurse in C code without
ever going back out to Python code in PyObject_Call(). Required introducing a
static RuntimeError instance so that normalizing an exception there is no
reliance on a recursive call that would put the exception system over the
recursion check itself.
........
r58034 | thomas.heller | 2007-09-07 08:32:17 +0200 (Fri, 07 Sep 2007) | 1 line
Add a 'c_longdouble' type to the ctypes module.
........
r58035 | thomas.heller | 2007-09-07 11:30:40 +0200 (Fri, 07 Sep 2007) | 1 line
Remove unneeded #include.
........
r58036 | thomas.heller | 2007-09-07 11:33:24 +0200 (Fri, 07 Sep 2007) | 6 lines
Backport from py3k branch:
Add a workaround for a strange bug on win64, when _ctypes is compiled
with the SDK compiler. This should fix the failing
Lib\ctypes\test\test_as_parameter.py test.
........
r58037 | georg.brandl | 2007-09-07 16:14:40 +0200 (Fri, 07 Sep 2007) | 2 lines
Fix a wrong indentation for sublists.
........
r58043 | georg.brandl | 2007-09-07 22:10:49 +0200 (Fri, 07 Sep 2007) | 2 lines
#1095: ln -f doesn't work portably, fix in Makefile.
........
r58049 | skip.montanaro | 2007-09-08 02:34:17 +0200 (Sat, 08 Sep 2007) | 1 line
be explicit about the actual location of the missing file
........
2007-09-08 14:39:28 -03:00
|
|
|
|
1996-12-30 12:17:54 -04:00
|
|
|
#ifdef Py_DEBUG
|
1990-10-14 09:07:46 -03:00
|
|
|
void
|
2000-07-22 16:20:54 -03:00
|
|
|
tok_dump(int type, char *start, char *end)
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
2021-10-27 18:27:34 -03:00
|
|
|
fprintf(stderr, "%s", _PyParser_TokenNames[type]);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (type == NAME || type == NUMBER || type == STRING || type == OP)
|
2021-10-27 18:27:34 -03:00
|
|
|
fprintf(stderr, "(%.*s)", (int)(end - start), start);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
2021-10-13 12:22:14 -03:00
|
|
|
#endif // Py_DEBUG
|