/src/cpython/Parser/lexer/state.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include "Python.h" |
2 | | #include "pycore_pystate.h" |
3 | | #include "pycore_token.h" |
4 | | #include "errcode.h" |
5 | | |
6 | | #include "state.h" |
7 | | |
8 | | /* Never change this */ |
9 | 22.8k | #define TABSIZE 8 |
10 | | |
11 | | /* Create and initialize a new tok_state structure */ |
12 | | struct tok_state * |
13 | | _PyTokenizer_tok_new(void) |
14 | 22.8k | { |
15 | 22.8k | struct tok_state *tok = (struct tok_state *)PyMem_Calloc( |
16 | 22.8k | 1, |
17 | 22.8k | sizeof(struct tok_state)); |
18 | 22.8k | if (tok == NULL) |
19 | 0 | return NULL; |
20 | 22.8k | tok->buf = tok->cur = tok->inp = NULL; |
21 | 22.8k | tok->fp_interactive = 0; |
22 | 22.8k | tok->interactive_src_start = NULL; |
23 | 22.8k | tok->interactive_src_end = NULL; |
24 | 22.8k | tok->start = NULL; |
25 | 22.8k | tok->end = NULL; |
26 | 22.8k | tok->done = E_OK; |
27 | 22.8k | tok->fp = NULL; |
28 | 22.8k | tok->input = NULL; |
29 | 22.8k | tok->tabsize = TABSIZE; |
30 | 22.8k | tok->indent = 0; |
31 | 22.8k | tok->indstack[0] = 0; |
32 | 22.8k | tok->atbol = 1; |
33 | 22.8k | tok->pendin = 0; |
34 | 22.8k | tok->prompt = tok->nextprompt = NULL; |
35 | 22.8k | tok->lineno = 0; |
36 | 22.8k | tok->starting_col_offset = -1; |
37 | 22.8k | tok->col_offset = -1; |
38 | 22.8k | tok->level = 0; |
39 | 22.8k | tok->altindstack[0] = 0; |
40 | 22.8k | tok->decoding_state = STATE_INIT; |
41 | 22.8k | tok->decoding_erred = 0; |
42 | 22.8k | tok->enc = NULL; |
43 | 22.8k | tok->encoding = NULL; |
44 | 22.8k | tok->cont_line = 0; |
45 | 22.8k | tok->filename = NULL; |
46 | 22.8k | tok->decoding_readline = NULL; |
47 | 22.8k | tok->decoding_buffer = NULL; |
48 | 22.8k | tok->readline = NULL; |
49 | 22.8k | tok->type_comments = 0; |
50 | 22.8k | tok->interactive_underflow = IUNDERFLOW_NORMAL; |
51 | 22.8k | tok->underflow = NULL; |
52 | 22.8k | tok->str = NULL; |
53 | 22.8k | tok->report_warnings = 1; |
54 | 22.8k | tok->tok_extra_tokens = 0; |
55 | 22.8k | tok->comment_newline = 0; |
56 | 22.8k | tok->implicit_newline = 0; |
57 | 22.8k | tok->tok_mode_stack[0] = (tokenizer_mode){.kind =TOK_REGULAR_MODE, .quote='\0', .quote_size = 0, .in_debug=0}; |
58 | 22.8k | tok->tok_mode_stack_index = 0; |
59 | | #ifdef Py_DEBUG |
60 | | tok->debug = _Py_GetConfig()->parser_debug; |
61 | | #endif |
62 | 22.8k | return tok; |
63 | 22.8k | } |
64 | | |
65 | | static void |
66 | | free_fstring_expressions(struct tok_state *tok) |
67 | 22.8k | { |
68 | 22.8k | int index; |
69 | 22.8k | tokenizer_mode *mode; |
70 | | |
71 | 50.5k | for (index = tok->tok_mode_stack_index; index >= 0; --index) { |
72 | 27.7k | mode = &(tok->tok_mode_stack[index]); |
73 | 27.7k | if (mode->last_expr_buffer != NULL) { |
74 | 4.65k | PyMem_Free(mode->last_expr_buffer); |
75 | 4.65k | mode->last_expr_buffer = NULL; |
76 | 4.65k | mode->last_expr_size = 0; |
77 | 4.65k | mode->last_expr_end = -1; |
78 | 4.65k | mode->in_format_spec = 0; |
79 | 4.65k | } |
80 | 27.7k | } |
81 | 22.8k | } |
82 | | |
83 | | /* Free a tok_state structure */ |
84 | | void |
85 | | _PyTokenizer_Free(struct tok_state *tok) |
86 | 22.8k | { |
87 | 22.8k | if (tok->encoding != NULL) { |
88 | 4.21k | PyMem_Free(tok->encoding); |
89 | 4.21k | } |
90 | 22.8k | Py_XDECREF(tok->decoding_readline); |
91 | 22.8k | Py_XDECREF(tok->decoding_buffer); |
92 | 22.8k | Py_XDECREF(tok->readline); |
93 | 22.8k | Py_XDECREF(tok->filename); |
94 | 22.8k | if ((tok->readline != NULL || tok->fp != NULL ) && tok->buf != NULL) { |
95 | 0 | PyMem_Free(tok->buf); |
96 | 0 | } |
97 | 22.8k | if (tok->input) { |
98 | 22.8k | PyMem_Free(tok->input); |
99 | 22.8k | } |
100 | 22.8k | if (tok->interactive_src_start != NULL) { |
101 | 0 | PyMem_Free(tok->interactive_src_start); |
102 | 0 | } |
103 | 22.8k | free_fstring_expressions(tok); |
104 | 22.8k | PyMem_Free(tok); |
105 | 22.8k | } |
106 | | |
107 | | void |
108 | 8.65k | _PyToken_Free(struct token *token) { |
109 | 8.65k | Py_XDECREF(token->metadata); |
110 | 8.65k | } |
111 | | |
112 | | void |
113 | 1.70M | _PyToken_Init(struct token *token) { |
114 | 1.70M | token->metadata = NULL; |
115 | 1.70M | } |
116 | | |
117 | | int |
118 | | _PyLexer_type_comment_token_setup(struct tok_state *tok, struct token *token, int type, int col_offset, |
119 | | int end_col_offset, const char *start, const char *end) |
120 | 0 | { |
121 | 0 | token->level = tok->level; |
122 | 0 | token->lineno = token->end_lineno = tok->lineno; |
123 | 0 | token->col_offset = col_offset; |
124 | 0 | token->end_col_offset = end_col_offset; |
125 | 0 | token->start = start; |
126 | 0 | token->end = end; |
127 | 0 | return type; |
128 | 0 | } |
129 | | |
130 | | int |
131 | | _PyLexer_token_setup(struct tok_state *tok, struct token *token, int type, const char *start, const char *end) |
132 | 1.74M | { |
133 | 1.74M | assert((start == NULL && end == NULL) || (start != NULL && end != NULL)); |
134 | 1.74M | token->level = tok->level; |
135 | 1.74M | if (ISSTRINGLIT(type)) { |
136 | 88.1k | token->lineno = tok->first_lineno; |
137 | 88.1k | } |
138 | 1.65M | else { |
139 | 1.65M | token->lineno = tok->lineno; |
140 | 1.65M | } |
141 | 1.74M | token->end_lineno = tok->lineno; |
142 | 1.74M | token->col_offset = token->end_col_offset = -1; |
143 | 1.74M | token->start = start; |
144 | 1.74M | token->end = end; |
145 | | |
146 | 1.74M | if (start != NULL && end != NULL) { |
147 | 1.68M | token->col_offset = tok->starting_col_offset; |
148 | 1.68M | token->end_col_offset = tok->col_offset; |
149 | 1.68M | } |
150 | 1.74M | return type; |
151 | 1.74M | } |