blob: 9b218d4c59c4f92a8415a4323190a9513b42db90 [file] [log] [blame]
Pablo Galindoc5fc1562020-04-22 23:29:27 +01001#ifndef PEGEN_H
2#define PEGEN_H
3
4#define PY_SSIZE_T_CLEAN
5#include <Python.h>
6#include <token.h>
Victor Stinner94faa072021-03-23 20:47:40 +01007#include <pycore_ast.h>
Pablo Galindoc5fc1562020-04-22 23:29:27 +01008
Pablo Galindo2b74c832020-04-27 18:02:07 +01009#if 0
10#define PyPARSE_YIELD_IS_KEYWORD 0x0001
11#endif
12
13#define PyPARSE_DONT_IMPLY_DEDENT 0x0002
14
15#if 0
16#define PyPARSE_WITH_IS_KEYWORD 0x0003
17#define PyPARSE_PRINT_IS_FUNCTION 0x0004
18#define PyPARSE_UNICODE_LITERALS 0x0008
19#endif
20
21#define PyPARSE_IGNORE_COOKIE 0x0010
22#define PyPARSE_BARRY_AS_BDFL 0x0020
23#define PyPARSE_TYPE_COMMENTS 0x0040
24#define PyPARSE_ASYNC_HACKS 0x0080
25
Pablo Galindoc5fc1562020-04-22 23:29:27 +010026typedef struct _memo {
27 int type;
28 void *node;
29 int mark;
30 struct _memo *next;
31} Memo;
32
33typedef struct {
34 int type;
35 PyObject *bytes;
Lysandros Nikolaou861efc62020-06-20 15:57:27 +030036 int lineno, col_offset, end_lineno, end_col_offset;
Pablo Galindoc5fc1562020-04-22 23:29:27 +010037 Memo *memo;
38} Token;
39
40typedef struct {
41 char *str;
42 int type;
43} KeywordToken;
44
Guido van Rossumc001c092020-04-30 12:12:19 -070045
46typedef struct {
47 struct {
48 int lineno;
49 char *comment; // The " <tag>" in "# type: ignore <tag>"
50 } *items;
51 size_t size;
52 size_t num_items;
53} growable_comment_array;
54
Pablo Galindoc5fc1562020-04-22 23:29:27 +010055typedef struct {
56 struct tok_state *tok;
57 Token **tokens;
58 int mark;
59 int fill, size;
60 PyArena *arena;
61 KeywordToken **keywords;
Pablo Galindob2802482021-04-15 21:38:45 +010062 char **soft_keywords;
Pablo Galindoc5fc1562020-04-22 23:29:27 +010063 int n_keyword_lists;
64 int start_rule;
65 int *errcode;
66 int parsing_started;
67 PyObject* normalize;
68 int starting_lineno;
69 int starting_col_offset;
70 int error_indicator;
Pablo Galindo2b74c832020-04-27 18:02:07 +010071 int flags;
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +030072 int feature_version;
Guido van Rossumc001c092020-04-30 12:12:19 -070073 growable_comment_array type_ignore_comments;
Lysandros Nikolaou2f37c352020-05-07 13:37:51 +030074 Token *known_err_token;
Pablo Galindo800a35c62020-05-25 18:38:45 +010075 int level;
Lysandros Nikolaoubca70142020-10-27 00:42:04 +020076 int call_invalid_rules;
Pablo Galindoc5fc1562020-04-22 23:29:27 +010077} Parser;
78
79typedef struct {
80 cmpop_ty cmpop;
81 expr_ty expr;
82} CmpopExprPair;
83
84typedef struct {
85 expr_ty key;
86 expr_ty value;
87} KeyValuePair;
88
89typedef struct {
90 arg_ty arg;
91 expr_ty value;
92} NameDefaultPair;
93
94typedef struct {
Pablo Galindoa5634c42020-09-16 19:42:00 +010095 asdl_arg_seq *plain_names;
Pablo Galindoc5fc1562020-04-22 23:29:27 +010096 asdl_seq *names_with_defaults; // asdl_seq* of NameDefaultsPair's
97} SlashWithDefault;
98
99typedef struct {
100 arg_ty vararg;
101 asdl_seq *kwonlyargs; // asdl_seq* of NameDefaultsPair's
102 arg_ty kwarg;
103} StarEtc;
104
Victor Stinner94faa072021-03-23 20:47:40 +0100105typedef struct { operator_ty kind; } AugOperator;
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100106typedef struct {
107 void *element;
108 int is_keyword;
109} KeywordOrStarred;
110
Pablo Galindo58bafe42021-04-09 01:17:31 +0100111#if defined(Py_DEBUG)
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100112void _PyPegen_clear_memo_statistics(void);
113PyObject *_PyPegen_get_memo_statistics(void);
Pablo Galindo58bafe42021-04-09 01:17:31 +0100114#endif
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100115
116int _PyPegen_insert_memo(Parser *p, int mark, int type, void *node);
117int _PyPegen_update_memo(Parser *p, int mark, int type, void *node);
118int _PyPegen_is_memoized(Parser *p, int type, void *pres);
119
Pablo Galindo58fb1562021-02-02 19:54:22 +0000120
Pablo Galindo1df5a9e2020-04-23 12:42:13 +0100121int _PyPegen_lookahead_with_name(int, expr_ty (func)(Parser *), Parser *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100122int _PyPegen_lookahead_with_int(int, Token *(func)(Parser *, int), Parser *, int);
Pablo Galindo404b23b2020-05-27 00:15:52 +0100123int _PyPegen_lookahead_with_string(int , expr_ty (func)(Parser *, const char*), Parser *, const char*);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100124int _PyPegen_lookahead(int, void *(func)(Parser *), Parser *);
125
126Token *_PyPegen_expect_token(Parser *p, int type);
Pablo Galindo58fb1562021-02-02 19:54:22 +0000127Token *_PyPegen_expect_forced_token(Parser *p, int type, const char* expected);
Guido van Rossumb45af1a2020-05-26 10:58:44 -0700128expr_ty _PyPegen_expect_soft_keyword(Parser *p, const char *keyword);
Pablo Galindob2802482021-04-15 21:38:45 +0100129expr_ty _PyPegen_soft_keyword_token(Parser *p);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100130Token *_PyPegen_get_last_nonnwhitespace_token(Parser *);
131int _PyPegen_fill_token(Parser *p);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100132expr_ty _PyPegen_name_token(Parser *p);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100133expr_ty _PyPegen_number_token(Parser *p);
134void *_PyPegen_string_token(Parser *p);
135const char *_PyPegen_get_expr_name(expr_ty);
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300136void *_PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...);
137void *_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
Pablo Galindo51c58962020-06-16 16:49:43 +0100138 Py_ssize_t lineno, Py_ssize_t col_offset,
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300139 const char *errmsg, va_list va);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100140void *_PyPegen_dummy_name(Parser *p, ...);
141
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300142Py_LOCAL_INLINE(void *)
Pablo Galindo96eeff52021-03-22 17:28:11 +0000143RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
144 Py_ssize_t lineno, Py_ssize_t col_offset,
145 const char *errmsg, ...)
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300146{
147 va_list va;
148 va_start(va, errmsg);
149 _PyPegen_raise_error_known_location(p, errtype, lineno, col_offset + 1,
150 errmsg, va);
151 va_end(va);
152 return NULL;
153}
154
155
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100156#define UNUSED(expr) do { (void)(expr); } while (0)
Pablo Galindo58bafe42021-04-09 01:17:31 +0100157#define EXTRA_EXPR(head, tail) head->lineno, (head)->col_offset, (tail)->end_lineno, (tail)->end_col_offset, p->arena
Pablo Galindoac7a92c2020-05-10 05:34:50 +0100158#define EXTRA _start_lineno, _start_col_offset, _end_lineno, _end_col_offset, p->arena
Lysandros Nikolaoua15c9b32020-05-13 22:36:27 +0300159#define RAISE_SYNTAX_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_SyntaxError, msg, ##__VA_ARGS__)
160#define RAISE_INDENTATION_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_IndentationError, msg, ##__VA_ARGS__)
161#define RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, msg, ...) \
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300162 RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, (a)->lineno, (a)->col_offset, msg, ##__VA_ARGS__)
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100163
164Py_LOCAL_INLINE(void *)
165CHECK_CALL(Parser *p, void *result)
166{
167 if (result == NULL) {
168 assert(PyErr_Occurred());
169 p->error_indicator = 1;
170 }
171 return result;
172}
173
174/* This is needed for helper functions that are allowed to
175 return NULL without an error. Example: _PyPegen_seq_extract_starred_exprs */
176Py_LOCAL_INLINE(void *)
177CHECK_CALL_NULL_ALLOWED(Parser *p, void *result)
178{
179 if (result == NULL && PyErr_Occurred()) {
180 p->error_indicator = 1;
181 }
182 return result;
183}
184
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300185#define CHECK(type, result) ((type) CHECK_CALL(p, result))
186#define CHECK_NULL_ALLOWED(type, result) ((type) CHECK_CALL_NULL_ALLOWED(p, result))
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100187
Guido van Rossumc001c092020-04-30 12:12:19 -0700188PyObject *_PyPegen_new_type_comment(Parser *, char *);
189
190Py_LOCAL_INLINE(PyObject *)
191NEW_TYPE_COMMENT(Parser *p, Token *tc)
192{
193 if (tc == NULL) {
194 return NULL;
195 }
196 char *bytes = PyBytes_AsString(tc->bytes);
197 if (bytes == NULL) {
198 goto error;
199 }
200 PyObject *tco = _PyPegen_new_type_comment(p, bytes);
201 if (tco == NULL) {
202 goto error;
203 }
204 return tco;
205 error:
206 p->error_indicator = 1; // Inline CHECK_CALL
207 return NULL;
208}
209
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300210Py_LOCAL_INLINE(void *)
211INVALID_VERSION_CHECK(Parser *p, int version, char *msg, void *node)
212{
213 if (node == NULL) {
214 p->error_indicator = 1; // Inline CHECK_CALL
215 return NULL;
216 }
217 if (p->feature_version < version) {
218 p->error_indicator = 1;
Batuhan Taskaya76c1b4d2020-05-01 16:13:43 +0300219 return RAISE_SYNTAX_ERROR("%s only supported in Python 3.%i and greater",
220 msg, version);
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300221 }
222 return node;
223}
224
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300225#define CHECK_VERSION(type, version, msg, node) ((type) INVALID_VERSION_CHECK(p, version, msg, node))
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300226
Guido van Rossumc001c092020-04-30 12:12:19 -0700227arg_ty _PyPegen_add_type_comment_to_arg(Parser *, arg_ty, Token *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100228PyObject *_PyPegen_new_identifier(Parser *, char *);
Lysandros Nikolaou3e0a6f32020-05-01 06:27:52 +0300229Parser *_PyPegen_Parser_New(struct tok_state *, int, int, int, int *, PyArena *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100230void _PyPegen_Parser_Free(Parser *);
231mod_ty _PyPegen_run_parser_from_file_pointer(FILE *, int, PyObject *, const char *,
Pablo Galindo2b74c832020-04-27 18:02:07 +0100232 const char *, const char *, PyCompilerFlags *, int *, PyArena *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100233void *_PyPegen_run_parser(Parser *);
Pablo Galindo2b74c832020-04-27 18:02:07 +0100234mod_ty _PyPegen_run_parser_from_string(const char *, int, PyObject *, PyCompilerFlags *, PyArena *);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100235asdl_stmt_seq *_PyPegen_interactive_exit(Parser *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100236asdl_seq *_PyPegen_singleton_seq(Parser *, void *);
237asdl_seq *_PyPegen_seq_insert_in_front(Parser *, void *, asdl_seq *);
Guido van Rossumc001c092020-04-30 12:12:19 -0700238asdl_seq *_PyPegen_seq_append_to_end(Parser *, asdl_seq *, void *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100239asdl_seq *_PyPegen_seq_flatten(Parser *, asdl_seq *);
240expr_ty _PyPegen_join_names_with_dot(Parser *, expr_ty, expr_ty);
241int _PyPegen_seq_count_dots(asdl_seq *);
Matthew Suozzo75a06f02021-04-10 16:56:28 -0400242alias_ty _PyPegen_alias_for_star(Parser *, int, int, int, int, PyArena *);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100243asdl_identifier_seq *_PyPegen_map_names_to_ids(Parser *, asdl_expr_seq *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100244CmpopExprPair *_PyPegen_cmpop_expr_pair(Parser *, cmpop_ty, expr_ty);
245asdl_int_seq *_PyPegen_get_cmpops(Parser *p, asdl_seq *);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100246asdl_expr_seq *_PyPegen_get_exprs(Parser *, asdl_seq *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100247expr_ty _PyPegen_set_expr_context(Parser *, expr_ty, expr_context_ty);
248KeyValuePair *_PyPegen_key_value_pair(Parser *, expr_ty, expr_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100249asdl_expr_seq *_PyPegen_get_keys(Parser *, asdl_seq *);
250asdl_expr_seq *_PyPegen_get_values(Parser *, asdl_seq *);
Guido van Rossumc001c092020-04-30 12:12:19 -0700251NameDefaultPair *_PyPegen_name_default_pair(Parser *, arg_ty, expr_ty, Token *);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100252SlashWithDefault *_PyPegen_slash_with_default(Parser *, asdl_arg_seq *, asdl_seq *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100253StarEtc *_PyPegen_star_etc(Parser *, arg_ty, asdl_seq *, arg_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100254arguments_ty _PyPegen_make_arguments(Parser *, asdl_arg_seq *, SlashWithDefault *,
255 asdl_arg_seq *, asdl_seq *, StarEtc *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100256arguments_ty _PyPegen_empty_arguments(Parser *);
257AugOperator *_PyPegen_augoperator(Parser*, operator_ty type);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100258stmt_ty _PyPegen_function_def_decorators(Parser *, asdl_expr_seq *, stmt_ty);
259stmt_ty _PyPegen_class_def_decorators(Parser *, asdl_expr_seq *, stmt_ty);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100260KeywordOrStarred *_PyPegen_keyword_or_starred(Parser *, void *, int);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100261asdl_expr_seq *_PyPegen_seq_extract_starred_exprs(Parser *, asdl_seq *);
262asdl_keyword_seq *_PyPegen_seq_delete_starred_exprs(Parser *, asdl_seq *);
263expr_ty _PyPegen_collect_call_seqs(Parser *, asdl_expr_seq *, asdl_seq *,
Pablo Galindo315a61f2020-09-03 15:29:32 +0100264 int lineno, int col_offset, int end_lineno,
265 int end_col_offset, PyArena *arena);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100266expr_ty _PyPegen_concatenate_strings(Parser *p, asdl_seq *);
267asdl_seq *_PyPegen_join_sequences(Parser *, asdl_seq *, asdl_seq *);
Pablo Galindo06f8c332020-10-30 23:48:42 +0000268int _PyPegen_check_barry_as_flufl(Parser *, Token *);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100269mod_ty _PyPegen_make_module(Parser *, asdl_stmt_seq *);
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100270
Pablo Galindo16ab0702020-05-15 02:04:52 +0100271// Error reporting helpers
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300272typedef enum {
273 STAR_TARGETS,
274 DEL_TARGETS,
275 FOR_TARGETS
276} TARGETS_TYPE;
277expr_ty _PyPegen_get_invalid_target(expr_ty e, TARGETS_TYPE targets_type);
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300278#define RAISE_SYNTAX_ERROR_INVALID_TARGET(type, e) _RAISE_SYNTAX_ERROR_INVALID_TARGET(p, type, e)
279
280Py_LOCAL_INLINE(void *)
281_RAISE_SYNTAX_ERROR_INVALID_TARGET(Parser *p, TARGETS_TYPE type, void *e)
282{
Lysandros Nikolaou2e5ca9e2020-10-21 22:53:14 +0300283 expr_ty invalid_target = CHECK_NULL_ALLOWED(expr_ty, _PyPegen_get_invalid_target(e, type));
Lysandros Nikolaou6c4e0bd2020-06-21 05:18:01 +0300284 if (invalid_target != NULL) {
285 const char *msg;
286 if (type == STAR_TARGETS || type == FOR_TARGETS) {
287 msg = "cannot assign to %s";
288 }
289 else {
290 msg = "cannot delete %s";
291 }
292 return RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
293 invalid_target,
294 msg,
295 _PyPegen_get_expr_name(invalid_target)
296 );
297 }
298 return RAISE_SYNTAX_ERROR("invalid syntax");
299}
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300300
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300301void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
Lysandros Nikolaouae145832020-05-22 03:56:52 +0300302void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args);
Lysandros Nikolaou75b863a2020-05-18 22:14:47 +0300303
Pablo Galindo16ab0702020-05-15 02:04:52 +0100304
Lysandros Nikolaou01ece632020-06-19 02:10:43 +0300305// Generated function in parse.c - function definition in python.gram
Pablo Galindoc5fc1562020-04-22 23:29:27 +0100306void *_PyPegen_parse(Parser *);
307
308#endif