Coverage Report

Created: 2025-07-18 06:09

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