Coverage Report

Created: 2025-08-26 06:26

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