pegen.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #ifndef PEGEN_H
  2. #define PEGEN_H
  3. #define PY_SSIZE_T_CLEAN
  4. #include <Python.h>
  5. #include <pycore_ast.h>
  6. #include <pycore_token.h>
  7. #if 0
  8. #define PyPARSE_YIELD_IS_KEYWORD 0x0001
  9. #endif
  10. #define PyPARSE_DONT_IMPLY_DEDENT 0x0002
  11. #if 0
  12. #define PyPARSE_WITH_IS_KEYWORD 0x0003
  13. #define PyPARSE_PRINT_IS_FUNCTION 0x0004
  14. #define PyPARSE_UNICODE_LITERALS 0x0008
  15. #endif
  16. #define PyPARSE_IGNORE_COOKIE 0x0010
  17. #define PyPARSE_BARRY_AS_BDFL 0x0020
  18. #define PyPARSE_TYPE_COMMENTS 0x0040
  19. #define PyPARSE_ASYNC_HACKS 0x0080
  20. #define PyPARSE_ALLOW_INCOMPLETE_INPUT 0x0100
  21. #define CURRENT_POS (-5)
  22. typedef struct _memo {
  23. int type;
  24. void *node;
  25. int mark;
  26. struct _memo *next;
  27. } Memo;
  28. typedef struct {
  29. int type;
  30. PyObject *bytes;
  31. int level;
  32. int lineno, col_offset, end_lineno, end_col_offset;
  33. Memo *memo;
  34. PyObject *metadata;
  35. } Token;
  36. typedef struct {
  37. const char *str;
  38. int type;
  39. } KeywordToken;
  40. typedef struct {
  41. struct {
  42. int lineno;
  43. char *comment; // The " <tag>" in "# type: ignore <tag>"
  44. } *items;
  45. size_t size;
  46. size_t num_items;
  47. } growable_comment_array;
  48. typedef struct {
  49. struct tok_state *tok;
  50. Token **tokens;
  51. int mark;
  52. int fill, size;
  53. PyArena *arena;
  54. KeywordToken **keywords;
  55. char **soft_keywords;
  56. int n_keyword_lists;
  57. int start_rule;
  58. int *errcode;
  59. int parsing_started;
  60. PyObject* normalize;
  61. int starting_lineno;
  62. int starting_col_offset;
  63. int error_indicator;
  64. int flags;
  65. int feature_version;
  66. growable_comment_array type_ignore_comments;
  67. Token *known_err_token;
  68. int level;
  69. int call_invalid_rules;
  70. int debug;
  71. } Parser;
  72. typedef struct {
  73. cmpop_ty cmpop;
  74. expr_ty expr;
  75. } CmpopExprPair;
  76. typedef struct {
  77. expr_ty key;
  78. expr_ty value;
  79. } KeyValuePair;
  80. typedef struct {
  81. expr_ty key;
  82. pattern_ty pattern;
  83. } KeyPatternPair;
  84. typedef struct {
  85. arg_ty arg;
  86. expr_ty value;
  87. } NameDefaultPair;
  88. typedef struct {
  89. asdl_arg_seq *plain_names;
  90. asdl_seq *names_with_defaults; // asdl_seq* of NameDefaultsPair's
  91. } SlashWithDefault;
  92. typedef struct {
  93. arg_ty vararg;
  94. asdl_seq *kwonlyargs; // asdl_seq* of NameDefaultsPair's
  95. arg_ty kwarg;
  96. } StarEtc;
  97. typedef struct { operator_ty kind; } AugOperator;
  98. typedef struct {
  99. void *element;
  100. int is_keyword;
  101. } KeywordOrStarred;
  102. typedef struct {
  103. void *result;
  104. PyObject *metadata;
  105. } ResultTokenWithMetadata;
  106. // Internal parser functions
  107. #if defined(Py_DEBUG)
  108. void _PyPegen_clear_memo_statistics(void);
  109. PyObject *_PyPegen_get_memo_statistics(void);
  110. #endif
  111. int _PyPegen_insert_memo(Parser *p, int mark, int type, void *node);
  112. int _PyPegen_update_memo(Parser *p, int mark, int type, void *node);
  113. int _PyPegen_is_memoized(Parser *p, int type, void *pres);
  114. int _PyPegen_lookahead_with_name(int, expr_ty (func)(Parser *), Parser *);
  115. int _PyPegen_lookahead_with_int(int, Token *(func)(Parser *, int), Parser *, int);
  116. int _PyPegen_lookahead_with_string(int , expr_ty (func)(Parser *, const char*), Parser *, const char*);
  117. int _PyPegen_lookahead(int, void *(func)(Parser *), Parser *);
  118. Token *_PyPegen_expect_token(Parser *p, int type);
  119. void* _PyPegen_expect_forced_result(Parser *p, void* result, const char* expected);
  120. Token *_PyPegen_expect_forced_token(Parser *p, int type, const char* expected);
  121. expr_ty _PyPegen_expect_soft_keyword(Parser *p, const char *keyword);
  122. expr_ty _PyPegen_soft_keyword_token(Parser *p);
  123. expr_ty _PyPegen_fstring_middle_token(Parser* p);
  124. Token *_PyPegen_get_last_nonnwhitespace_token(Parser *);
  125. int _PyPegen_fill_token(Parser *p);
  126. expr_ty _PyPegen_name_token(Parser *p);
  127. expr_ty _PyPegen_number_token(Parser *p);
  128. void *_PyPegen_string_token(Parser *p);
  129. Py_ssize_t _PyPegen_byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset);
  130. Py_ssize_t _PyPegen_byte_offset_to_character_offset_raw(const char*, Py_ssize_t col_offset);
  131. Py_ssize_t _PyPegen_calculate_display_width(PyObject *segment, Py_ssize_t character_offset);
  132. // Error handling functions and APIs
  133. typedef enum {
  134. STAR_TARGETS,
  135. DEL_TARGETS,
  136. FOR_TARGETS
  137. } TARGETS_TYPE;
  138. int _Pypegen_raise_decode_error(Parser *p);
  139. void _PyPegen_raise_tokenizer_init_error(PyObject *filename);
  140. int _Pypegen_tokenizer_error(Parser *p);
  141. void *_PyPegen_raise_error(Parser *p, PyObject *errtype, int use_mark, const char *errmsg, ...);
  142. void *_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
  143. Py_ssize_t lineno, Py_ssize_t col_offset,
  144. Py_ssize_t end_lineno, Py_ssize_t end_col_offset,
  145. const char *errmsg, va_list va);
  146. void _Pypegen_set_syntax_error(Parser* p, Token* last_token);
  147. void _Pypegen_stack_overflow(Parser *p);
  148. Py_LOCAL_INLINE(void *)
  149. RAISE_ERROR_KNOWN_LOCATION(Parser *p, PyObject *errtype,
  150. Py_ssize_t lineno, Py_ssize_t col_offset,
  151. Py_ssize_t end_lineno, Py_ssize_t end_col_offset,
  152. const char *errmsg, ...)
  153. {
  154. va_list va;
  155. va_start(va, errmsg);
  156. Py_ssize_t _col_offset = (col_offset == CURRENT_POS ? CURRENT_POS : col_offset + 1);
  157. Py_ssize_t _end_col_offset = (end_col_offset == CURRENT_POS ? CURRENT_POS : end_col_offset + 1);
  158. _PyPegen_raise_error_known_location(p, errtype, lineno, _col_offset, end_lineno, _end_col_offset, errmsg, va);
  159. va_end(va);
  160. return NULL;
  161. }
  162. #define RAISE_SYNTAX_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_SyntaxError, 0, msg, ##__VA_ARGS__)
  163. #define RAISE_INDENTATION_ERROR(msg, ...) _PyPegen_raise_error(p, PyExc_IndentationError, 0, msg, ##__VA_ARGS__)
  164. #define RAISE_SYNTAX_ERROR_ON_NEXT_TOKEN(msg, ...) _PyPegen_raise_error(p, PyExc_SyntaxError, 1, msg, ##__VA_ARGS__)
  165. #define RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, msg, ...) \
  166. RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, (a)->lineno, (a)->col_offset, (b)->end_lineno, (b)->end_col_offset, msg, ##__VA_ARGS__)
  167. #define RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, msg, ...) \
  168. RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, (a)->lineno, (a)->col_offset, (a)->end_lineno, (a)->end_col_offset, msg, ##__VA_ARGS__)
  169. #define RAISE_SYNTAX_ERROR_STARTING_FROM(a, msg, ...) \
  170. RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, (a)->lineno, (a)->col_offset, CURRENT_POS, CURRENT_POS, msg, ##__VA_ARGS__)
  171. #define RAISE_SYNTAX_ERROR_INVALID_TARGET(type, e) _RAISE_SYNTAX_ERROR_INVALID_TARGET(p, type, e)
  172. Py_LOCAL_INLINE(void *)
  173. CHECK_CALL(Parser *p, void *result)
  174. {
  175. if (result == NULL) {
  176. assert(PyErr_Occurred());
  177. p->error_indicator = 1;
  178. }
  179. return result;
  180. }
  181. /* This is needed for helper functions that are allowed to
  182. return NULL without an error. Example: _PyPegen_seq_extract_starred_exprs */
  183. Py_LOCAL_INLINE(void *)
  184. CHECK_CALL_NULL_ALLOWED(Parser *p, void *result)
  185. {
  186. if (result == NULL && PyErr_Occurred()) {
  187. p->error_indicator = 1;
  188. }
  189. return result;
  190. }
  191. #define CHECK(type, result) ((type) CHECK_CALL(p, result))
  192. #define CHECK_NULL_ALLOWED(type, result) ((type) CHECK_CALL_NULL_ALLOWED(p, result))
  193. expr_ty _PyPegen_get_invalid_target(expr_ty e, TARGETS_TYPE targets_type);
  194. const char *_PyPegen_get_expr_name(expr_ty);
  195. Py_LOCAL_INLINE(void *)
  196. _RAISE_SYNTAX_ERROR_INVALID_TARGET(Parser *p, TARGETS_TYPE type, void *e)
  197. {
  198. expr_ty invalid_target = CHECK_NULL_ALLOWED(expr_ty, _PyPegen_get_invalid_target(e, type));
  199. if (invalid_target != NULL) {
  200. const char *msg;
  201. if (type == STAR_TARGETS || type == FOR_TARGETS) {
  202. msg = "cannot assign to %s";
  203. }
  204. else {
  205. msg = "cannot delete %s";
  206. }
  207. return RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
  208. invalid_target,
  209. msg,
  210. _PyPegen_get_expr_name(invalid_target)
  211. );
  212. return RAISE_SYNTAX_ERROR_KNOWN_LOCATION(invalid_target, "invalid syntax");
  213. }
  214. return NULL;
  215. }
  216. // Action utility functions
  217. void *_PyPegen_dummy_name(Parser *p, ...);
  218. void * _PyPegen_seq_last_item(asdl_seq *seq);
  219. #define PyPegen_last_item(seq, type) ((type)_PyPegen_seq_last_item((asdl_seq*)seq))
  220. void * _PyPegen_seq_first_item(asdl_seq *seq);
  221. #define PyPegen_first_item(seq, type) ((type)_PyPegen_seq_first_item((asdl_seq*)seq))
  222. #define UNUSED(expr) do { (void)(expr); } while (0)
  223. #define EXTRA_EXPR(head, tail) head->lineno, (head)->col_offset, (tail)->end_lineno, (tail)->end_col_offset, p->arena
  224. #define EXTRA _start_lineno, _start_col_offset, _end_lineno, _end_col_offset, p->arena
  225. PyObject *_PyPegen_new_type_comment(Parser *, const char *);
  226. Py_LOCAL_INLINE(PyObject *)
  227. NEW_TYPE_COMMENT(Parser *p, Token *tc)
  228. {
  229. if (tc == NULL) {
  230. return NULL;
  231. }
  232. const char *bytes = PyBytes_AsString(tc->bytes);
  233. if (bytes == NULL) {
  234. goto error;
  235. }
  236. PyObject *tco = _PyPegen_new_type_comment(p, bytes);
  237. if (tco == NULL) {
  238. goto error;
  239. }
  240. return tco;
  241. error:
  242. p->error_indicator = 1; // Inline CHECK_CALL
  243. return NULL;
  244. }
  245. Py_LOCAL_INLINE(void *)
  246. INVALID_VERSION_CHECK(Parser *p, int version, char *msg, void *node)
  247. {
  248. if (node == NULL) {
  249. p->error_indicator = 1; // Inline CHECK_CALL
  250. return NULL;
  251. }
  252. if (p->feature_version < version) {
  253. p->error_indicator = 1;
  254. return RAISE_SYNTAX_ERROR("%s only supported in Python 3.%i and greater",
  255. msg, version);
  256. }
  257. return node;
  258. }
  259. #define CHECK_VERSION(type, version, msg, node) ((type) INVALID_VERSION_CHECK(p, version, msg, node))
  260. arg_ty _PyPegen_add_type_comment_to_arg(Parser *, arg_ty, Token *);
  261. PyObject *_PyPegen_new_identifier(Parser *, const char *);
  262. asdl_seq *_PyPegen_singleton_seq(Parser *, void *);
  263. asdl_seq *_PyPegen_seq_insert_in_front(Parser *, void *, asdl_seq *);
  264. asdl_seq *_PyPegen_seq_append_to_end(Parser *, asdl_seq *, void *);
  265. asdl_seq *_PyPegen_seq_flatten(Parser *, asdl_seq *);
  266. expr_ty _PyPegen_join_names_with_dot(Parser *, expr_ty, expr_ty);
  267. int _PyPegen_seq_count_dots(asdl_seq *);
  268. alias_ty _PyPegen_alias_for_star(Parser *, int, int, int, int, PyArena *);
  269. asdl_identifier_seq *_PyPegen_map_names_to_ids(Parser *, asdl_expr_seq *);
  270. CmpopExprPair *_PyPegen_cmpop_expr_pair(Parser *, cmpop_ty, expr_ty);
  271. asdl_int_seq *_PyPegen_get_cmpops(Parser *p, asdl_seq *);
  272. asdl_expr_seq *_PyPegen_get_exprs(Parser *, asdl_seq *);
  273. expr_ty _PyPegen_set_expr_context(Parser *, expr_ty, expr_context_ty);
  274. KeyValuePair *_PyPegen_key_value_pair(Parser *, expr_ty, expr_ty);
  275. asdl_expr_seq *_PyPegen_get_keys(Parser *, asdl_seq *);
  276. asdl_expr_seq *_PyPegen_get_values(Parser *, asdl_seq *);
  277. KeyPatternPair *_PyPegen_key_pattern_pair(Parser *, expr_ty, pattern_ty);
  278. asdl_expr_seq *_PyPegen_get_pattern_keys(Parser *, asdl_seq *);
  279. asdl_pattern_seq *_PyPegen_get_patterns(Parser *, asdl_seq *);
  280. NameDefaultPair *_PyPegen_name_default_pair(Parser *, arg_ty, expr_ty, Token *);
  281. SlashWithDefault *_PyPegen_slash_with_default(Parser *, asdl_arg_seq *, asdl_seq *);
  282. StarEtc *_PyPegen_star_etc(Parser *, arg_ty, asdl_seq *, arg_ty);
  283. arguments_ty _PyPegen_make_arguments(Parser *, asdl_arg_seq *, SlashWithDefault *,
  284. asdl_arg_seq *, asdl_seq *, StarEtc *);
  285. arguments_ty _PyPegen_empty_arguments(Parser *);
  286. expr_ty _PyPegen_formatted_value(Parser *, expr_ty, Token *, ResultTokenWithMetadata *, ResultTokenWithMetadata *, Token *,
  287. int, int, int, int, PyArena *);
  288. AugOperator *_PyPegen_augoperator(Parser*, operator_ty type);
  289. stmt_ty _PyPegen_function_def_decorators(Parser *, asdl_expr_seq *, stmt_ty);
  290. stmt_ty _PyPegen_class_def_decorators(Parser *, asdl_expr_seq *, stmt_ty);
  291. KeywordOrStarred *_PyPegen_keyword_or_starred(Parser *, void *, int);
  292. asdl_expr_seq *_PyPegen_seq_extract_starred_exprs(Parser *, asdl_seq *);
  293. asdl_keyword_seq *_PyPegen_seq_delete_starred_exprs(Parser *, asdl_seq *);
  294. expr_ty _PyPegen_collect_call_seqs(Parser *, asdl_expr_seq *, asdl_seq *,
  295. int lineno, int col_offset, int end_lineno,
  296. int end_col_offset, PyArena *arena);
  297. expr_ty _PyPegen_constant_from_token(Parser* p, Token* tok);
  298. expr_ty _PyPegen_decoded_constant_from_token(Parser* p, Token* tok);
  299. expr_ty _PyPegen_constant_from_string(Parser* p, Token* tok);
  300. expr_ty _PyPegen_concatenate_strings(Parser *p, asdl_expr_seq *, int, int, int, int, PyArena *);
  301. expr_ty _PyPegen_FetchRawForm(Parser *p, int, int, int, int);
  302. expr_ty _PyPegen_ensure_imaginary(Parser *p, expr_ty);
  303. expr_ty _PyPegen_ensure_real(Parser *p, expr_ty);
  304. asdl_seq *_PyPegen_join_sequences(Parser *, asdl_seq *, asdl_seq *);
  305. int _PyPegen_check_barry_as_flufl(Parser *, Token *);
  306. int _PyPegen_check_legacy_stmt(Parser *p, expr_ty t);
  307. ResultTokenWithMetadata *_PyPegen_check_fstring_conversion(Parser *p, Token *, expr_ty t);
  308. ResultTokenWithMetadata *_PyPegen_setup_full_format_spec(Parser *, Token *, asdl_expr_seq *, int, int,
  309. int, int, PyArena *);
  310. mod_ty _PyPegen_make_module(Parser *, asdl_stmt_seq *);
  311. void *_PyPegen_arguments_parsing_error(Parser *, expr_ty);
  312. expr_ty _PyPegen_get_last_comprehension_item(comprehension_ty comprehension);
  313. void *_PyPegen_nonparen_genexp_in_call(Parser *p, expr_ty args, asdl_comprehension_seq *comprehensions);
  314. // Parser API
  315. Parser *_PyPegen_Parser_New(struct tok_state *, int, int, int, int *, PyArena *);
  316. void _PyPegen_Parser_Free(Parser *);
  317. mod_ty _PyPegen_run_parser_from_file_pointer(FILE *, int, PyObject *, const char *,
  318. const char *, const char *, PyCompilerFlags *, int *, PyArena *);
  319. void *_PyPegen_run_parser(Parser *);
  320. mod_ty _PyPegen_run_parser_from_string(const char *, int, PyObject *, PyCompilerFlags *, PyArena *);
  321. asdl_stmt_seq *_PyPegen_interactive_exit(Parser *);
  322. // TODO: move to the correct place in this file
  323. expr_ty _PyPegen_joined_str(Parser *p, Token* a, asdl_expr_seq* expr, Token*b);
  324. // Generated function in parse.c - function definition in python.gram
  325. void *_PyPegen_parse(Parser *);
  326. #endif