2023-10-11 12:14:44 -03:00
|
|
|
#ifndef _PY_LEXER_H_
|
|
|
|
#define _PY_LEXER_H_
|
1993-07-28 06:05:47 -03:00
|
|
|
|
2002-08-04 14:29:52 -03:00
|
|
|
#include "object.h"
|
1991-02-19 08:39:46 -04:00
|
|
|
|
2023-05-01 16:15:47 -03:00
|
|
|
#define MAXINDENT 100 /* Max indentation level */
|
|
|
|
#define MAXLEVEL 200 /* Max parentheses level */
|
|
|
|
#define MAXFSTRINGLEVEL 150 /* Max f-string nesting level */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2023-10-11 12:14:44 -03:00
|
|
|
#define INSIDE_FSTRING(tok) (tok->tok_mode_stack_index > 0)
|
|
|
|
#define INSIDE_FSTRING_EXPR(tok) (tok->curly_bracket_expr_start_depth >= 0)
|
|
|
|
|
2007-09-21 17:50:26 -03:00
|
|
|
enum decoding_state {
|
2010-05-09 12:52:27 -03:00
|
|
|
STATE_INIT,
|
2021-03-28 19:48:05 -03:00
|
|
|
STATE_SEEK_CODING,
|
|
|
|
STATE_NORMAL
|
2007-09-21 17:50:26 -03:00
|
|
|
};
|
|
|
|
|
2021-05-22 19:05:00 -03:00
|
|
|
enum interactive_underflow_t {
|
2023-05-25 23:50:33 -03:00
|
|
|
/* Normal mode of operation: return a new token when asked in interactive mode */
|
2021-05-22 19:05:00 -03:00
|
|
|
IUNDERFLOW_NORMAL,
|
|
|
|
/* Forcefully return ENDMARKER when asked for a new token in interactive mode. This
|
2021-10-06 20:57:10 -03:00
|
|
|
* can be used to prevent the tokenizer to prompt the user for new tokens */
|
2021-05-22 19:05:00 -03:00
|
|
|
IUNDERFLOW_STOP,
|
|
|
|
};
|
|
|
|
|
2022-10-06 20:07:17 -03:00
|
|
|
struct token {
|
|
|
|
int level;
|
|
|
|
int lineno, col_offset, end_lineno, end_col_offset;
|
|
|
|
const char *start, *end;
|
2023-04-26 22:33:31 -03:00
|
|
|
PyObject *metadata;
|
2022-10-06 20:07:17 -03:00
|
|
|
};
|
|
|
|
|
2023-04-19 13:18:16 -03:00
|
|
|
enum tokenizer_mode_kind_t {
|
|
|
|
TOK_REGULAR_MODE,
|
|
|
|
TOK_FSTRING_MODE,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_EXPR_NESTING 3
|
|
|
|
|
|
|
|
typedef struct _tokenizer_mode {
|
|
|
|
enum tokenizer_mode_kind_t kind;
|
|
|
|
|
2023-04-19 17:51:31 -03:00
|
|
|
int curly_bracket_depth;
|
|
|
|
int curly_bracket_expr_start_depth;
|
2023-04-19 13:18:16 -03:00
|
|
|
|
|
|
|
char f_string_quote;
|
|
|
|
int f_string_quote_size;
|
|
|
|
int f_string_raw;
|
|
|
|
const char* f_string_start;
|
|
|
|
const char* f_string_multi_line_start;
|
2023-05-20 10:07:05 -03:00
|
|
|
int f_string_line_start;
|
2023-04-19 13:18:16 -03:00
|
|
|
|
2023-04-23 16:06:10 -03:00
|
|
|
Py_ssize_t f_string_start_offset;
|
|
|
|
Py_ssize_t f_string_multi_line_start_offset;
|
|
|
|
|
2023-04-19 13:18:16 -03:00
|
|
|
Py_ssize_t last_expr_size;
|
|
|
|
Py_ssize_t last_expr_end;
|
|
|
|
char* last_expr_buffer;
|
2023-04-26 22:33:31 -03:00
|
|
|
int f_string_debug;
|
2024-07-16 15:57:22 -03:00
|
|
|
int in_format_spec;
|
2023-04-19 13:18:16 -03:00
|
|
|
} tokenizer_mode;
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Tokenizer state */
|
|
|
|
struct tok_state {
|
2010-05-09 12:52:27 -03:00
|
|
|
/* Input state; buf <= cur <= inp <= end */
|
|
|
|
/* NB an entire line is held in the buffer */
|
2023-06-13 12:18:11 -03:00
|
|
|
char *buf; /* Input buffer, or NULL; malloc'ed if fp != NULL or readline != NULL */
|
2010-05-09 12:52:27 -03:00
|
|
|
char *cur; /* Next character in buffer */
|
|
|
|
char *inp; /* End of data in buffer */
|
2021-03-13 23:38:40 -04:00
|
|
|
int fp_interactive; /* If the file descriptor is interactive */
|
|
|
|
char *interactive_src_start; /* The start of the source parsed so far in interactive mode */
|
|
|
|
char *interactive_src_end; /* The end of the source parsed so far in interactive mode */
|
2020-02-27 22:44:52 -04:00
|
|
|
const char *end; /* End of input buffer if buf != NULL */
|
|
|
|
const char *start; /* Start of current token if not NULL */
|
2010-05-09 12:52:27 -03:00
|
|
|
int done; /* E_OK normally, E_EOF at EOF, otherwise error code */
|
|
|
|
/* NB If done != E_OK, cur must be == inp!!! */
|
|
|
|
FILE *fp; /* Rest of input; NULL if tokenizing a string */
|
|
|
|
int tabsize; /* Tab spacing */
|
|
|
|
int indent; /* Current indentation index */
|
|
|
|
int indstack[MAXINDENT]; /* Stack of indents */
|
|
|
|
int atbol; /* Nonzero if at begin of new line */
|
|
|
|
int pendin; /* Pending indents (if > 0) or dedents (if < 0) */
|
2013-10-19 15:03:34 -03:00
|
|
|
const char *prompt, *nextprompt; /* For interactive prompting */
|
2010-05-09 12:52:27 -03:00
|
|
|
int lineno; /* Current line number */
|
2019-01-13 00:05:13 -04:00
|
|
|
int first_lineno; /* First line of a single line or multi line string
|
|
|
|
expression (cf. issue 16806) */
|
2022-10-07 18:38:35 -03:00
|
|
|
int starting_col_offset; /* The column offset at the beginning of a token */
|
|
|
|
int col_offset; /* Current col offset */
|
2010-05-09 12:52:27 -03:00
|
|
|
int level; /* () [] {} Parentheses nesting level */
|
|
|
|
/* Used to allow free continuations inside them */
|
2018-12-17 11:34:14 -04:00
|
|
|
char parenstack[MAXLEVEL];
|
|
|
|
int parenlinenostack[MAXLEVEL];
|
2021-01-19 19:59:33 -04:00
|
|
|
int parencolstack[MAXLEVEL];
|
2011-04-04 19:39:01 -03:00
|
|
|
PyObject *filename;
|
2018-12-17 11:34:14 -04:00
|
|
|
/* Stuff for checking on different tab sizes */
|
2010-05-09 12:52:27 -03:00
|
|
|
int altindstack[MAXINDENT]; /* Stack of alternate indents */
|
|
|
|
/* Stuff for PEP 0263 */
|
|
|
|
enum decoding_state decoding_state;
|
|
|
|
int decoding_erred; /* whether erred in decoding */
|
|
|
|
char *encoding; /* Source encoding. */
|
|
|
|
int cont_line; /* whether we are in a continuation line. */
|
|
|
|
const char* line_start; /* pointer to start of current line */
|
2019-01-13 00:05:13 -04:00
|
|
|
const char* multi_line_start; /* pointer to start of first line of
|
|
|
|
a single line or multi line string
|
|
|
|
expression (cf. issue 16806) */
|
2010-10-14 09:04:34 -03:00
|
|
|
PyObject *decoding_readline; /* open(...).readline */
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *decoding_buffer;
|
2023-05-30 18:43:34 -03:00
|
|
|
PyObject *readline; /* readline() function */
|
2010-05-09 12:52:27 -03:00
|
|
|
const char* enc; /* Encoding for the current str. */
|
2021-11-20 22:06:39 -04:00
|
|
|
char* str; /* Source string being tokenized (if tokenizing from a string)*/
|
2020-02-27 22:44:52 -04:00
|
|
|
char* input; /* Tokenizer's newline translated copy of the string. */
|
2019-01-31 07:40:27 -04:00
|
|
|
|
|
|
|
int type_comments; /* Whether to look for type comments */
|
2019-03-07 16:38:08 -04:00
|
|
|
|
2021-05-22 19:05:00 -03:00
|
|
|
/* How to proceed when asked for a new token in interactive mode */
|
2021-08-09 15:32:54 -03:00
|
|
|
enum interactive_underflow_t interactive_underflow;
|
2023-10-11 12:14:44 -03:00
|
|
|
int (*underflow)(struct tok_state *); /* Function to call when buffer is empty and we need to refill it*/
|
|
|
|
|
2022-11-30 07:36:06 -04:00
|
|
|
int report_warnings;
|
2023-04-19 13:18:16 -03:00
|
|
|
// TODO: Factor this into its own thing
|
2023-05-01 16:15:47 -03:00
|
|
|
tokenizer_mode tok_mode_stack[MAXFSTRINGLEVEL];
|
2023-04-19 13:18:16 -03:00
|
|
|
int tok_mode_stack_index;
|
2023-05-20 21:03:02 -03:00
|
|
|
int tok_extra_tokens;
|
|
|
|
int comment_newline;
|
2023-06-06 08:52:16 -03:00
|
|
|
int implicit_newline;
|
2022-05-24 17:35:08 -03:00
|
|
|
#ifdef Py_DEBUG
|
|
|
|
int debug;
|
|
|
|
#endif
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
2023-10-11 12:14:44 -03:00
|
|
|
int _PyLexer_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);
|
|
|
|
int _PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, const char *start, const char *end);
|
|
|
|
|
|
|
|
struct tok_state *_PyTokenizer_tok_new(void);
|
|
|
|
void _PyTokenizer_Free(struct tok_state *);
|
|
|
|
void _PyToken_Free(struct token *);
|
|
|
|
void _PyToken_Init(struct token *);
|
1993-07-28 06:05:47 -03:00
|
|
|
|
2019-04-13 13:05:14 -03:00
|
|
|
|
1993-07-28 06:05:47 -03:00
|
|
|
#endif
|