xref: /third_party/python/Parser/string_parser.h (revision 7db96d56)
1#ifndef STRINGS_H
2#define STRINGS_H
3
4#include <Python.h>
5#include <pycore_ast.h>
6#include "pegen.h"
7
8#define EXPRLIST_N_CACHED  64
9
10typedef struct {
11    /* Incrementally build an array of expr_ty, so be used in an
12       asdl_seq. Cache some small but reasonably sized number of
13       expr_ty's, and then after that start dynamically allocating,
14       doubling the number allocated each time. Note that the f-string
15       f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one
16       Constant for the literal 'a'. So you add expr_ty's about twice as
17       fast as you add expressions in an f-string. */
18
19    Py_ssize_t allocated;  /* Number we've allocated. */
20    Py_ssize_t size;       /* Number we've used. */
21    expr_ty    *p;         /* Pointer to the memory we're actually
22                              using. Will point to 'data' until we
23                              start dynamically allocating. */
24    expr_ty    data[EXPRLIST_N_CACHED];
25} ExprList;
26
27/* The FstringParser is designed to add a mix of strings and
28   f-strings, and concat them together as needed. Ultimately, it
29   generates an expr_ty. */
30typedef struct {
31    PyObject *last_str;
32    ExprList expr_list;
33    int fmode;
34} FstringParser;
35
36void _PyPegen_FstringParser_Init(FstringParser *);
37int _PyPegen_parsestr(Parser *, int *, int *, PyObject **,
38                      const char **, Py_ssize_t *, Token *);
39int _PyPegen_FstringParser_ConcatFstring(Parser *, FstringParser *, const char **,
40                                const char *, int, int, Token *, Token *,
41                                Token *);
42int _PyPegen_FstringParser_ConcatAndDel(FstringParser *, PyObject *);
43expr_ty _PyPegen_FstringParser_Finish(Parser *, FstringParser *, Token *, Token *);
44void _PyPegen_FstringParser_Dealloc(FstringParser *);
45
46#endif
47