Coverage Report

Created: 2025-11-24 06:11

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