Coverage Report

Created: 2026-10-31 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Parser/tokenizer/api.c
Line
Count
Source
1
#include "Python.h"
2
#include "errcode.h"
3
#include "pycore_token.h"
4
5
#include "tokenizer.h"
6
#include "reader.h"
7
#include "reader_internal.h"
8
#include "../lexer/state.h"
9
10
int
11
_PyTokenizer_GetStatus(const struct tok_state *tok)
12
3.86k
{
13
3.86k
    return tok->done;
14
3.86k
}
15
16
int
17
_PyTokenizer_GetNewlineRequirements(const struct tok_state *tok)
18
0
{
19
0
    return tok->needs_newline;
20
0
}
21
22
_PyTokenizer_Info
23
_PyTokenizer_GetInfo(const struct tok_state *tok)
24
264k
{
25
264k
    _PyTokenizer_Info info = {
26
264k
        .status = tok->done,
27
264k
        .implicit_newline = tok->implicit_newline,
28
264k
        .diagnostic = tok->diagnostic,
29
264k
        .location = {tok->lineno, tok->line_start < 0
30
264k
            ? -1 : (int)(tok->cur - tok->line_start)},
31
264k
        .cursor = tok->cur,
32
264k
        .input_span = {tok->buf_offset, tok->inp},
33
264k
        .line_span = {tok->line_start, tok->inp},
34
264k
        .level = tok->level,
35
264k
        .delimiter_loc = {-1, -1},
36
264k
        .in_formatted_string = tok->ftstring_depth != 0,
37
264k
        .is_interactive = _PyTok_ReaderIsInteractive(tok),
38
264k
        .is_file = tok->reader->fp != NULL && tok->reader->fp != stdin,
39
264k
        .filename = tok->filename,
40
264k
        .module = tok->module,
41
264k
        .encoding = tok->reader->encoding,
42
264k
    };
43
264k
    if (tok->level > 0) {
44
24.1k
        int level = tok->level - 1;
45
24.1k
        info.delimiter = tok->parenstack[level];
46
24.1k
        info.delimiter_loc = (_PyTok_Loc){
47
24.1k
            tok->parenlinenostack[level], tok->parencolstack[level]};
48
24.1k
    }
49
264k
    return info;
50
264k
}
51
52
const char *
53
_PyToken_TextView(const struct tok_state *tok, const struct token *token,
54
                  Py_ssize_t *length)
55
767k
{
56
767k
    assert(length != NULL);
57
767k
    if (token->span.start < 0) {
58
4
        assert(token->span.start == -1 && token->span.end == -1);
59
4
        *length = 0;
60
4
        return "";
61
4
    }
62
767k
    return _PyTok_SourceSpanView(&tok->source, token->span, length);
63
767k
}
64
65
const char *
66
_PyTokenizer_SpanView(const struct tok_state *tok, _PyTok_Span span,
67
                      Py_ssize_t *length)
68
63.2k
{
69
63.2k
    return _PyTok_SourceSpanView(&tok->source, span, length);
70
63.2k
}
71
72
void
73
_PyToken_GetView(const struct tok_state *tok, const struct token *token,
74
                 _PyToken_View *view)
75
2.00k
{
76
2.00k
    assert(view != NULL);
77
2.00k
    view->text = _PyToken_TextView(tok, token, &view->length);
78
2.00k
    view->end_line_start = tok->line_start;
79
2.00k
    view->line_span = (_PyTok_Span){
80
2.00k
        ISSTRINGLIT(token->type)
81
2.00k
            ? token->span.start - token->start_loc.byte_col : tok->line_start,
82
2.00k
        tok->inp,
83
2.00k
    };
84
2.00k
    view->implicit_newline = tok->implicit_newline;
85
2.00k
    view->at_eof = tok->done == E_EOF;
86
2.00k
}
87
88
const char *
89
_PyTokenizer_LineView(const struct tok_state *tok, Py_ssize_t lineno,
90
                      Py_ssize_t *length)
91
310
{
92
310
    return _PyTok_SourceLineView(&tok->source, lineno, length);
93
310
}
94
95
void
96
_PyTokenizer_SetContext(struct tok_state *tok, PyObject *filename,
97
                        PyObject *module)
98
73.4k
{
99
73.4k
    Py_XINCREF(filename);
100
73.4k
    Py_XINCREF(module);
101
73.4k
    Py_XSETREF(tok->filename, filename);
102
73.4k
    Py_XSETREF(tok->module, module);
103
73.4k
}
104
105
void
106
_PyTokenizer_SetOptions(struct tok_state *tok, int extra_tokens,
107
                        int type_comments)
108
73.4k
{
109
73.4k
    tok->tok_extra_tokens = extra_tokens;
110
73.4k
    tok->type_comments = type_comments;
111
73.4k
}
112
113
void
114
_PyTokenizer_ImplyDedents(struct tok_state *tok)
115
0
{
116
0
    _PyLexer_ImplyDedents(tok);
117
0
}
118
119
void
120
_PyTokenizer_AllowIncompleteInput(struct tok_state *tok, int allow)
121
138k
{
122
138k
    tok->allow_incomplete_input = allow != 0;
123
138k
}
124
125
int
126
_PyTokenizer_HasTrailingStatement(const struct tok_state *tok)
127
0
{
128
0
    _PyTok_Off cur = tok->cur;
129
0
    _PyTok_Off end = tok->source.base_offset + tok->source.len;
130
0
    while (cur < end) {
131
0
        int c = _PyTok_SourceByte(&tok->source, cur++);
132
0
        if (c == '\0') {
133
0
            return 0;
134
0
        }
135
0
        if (c == ' ' || c == '\t' || c == '\n' || c == '\014') {
136
0
            continue;
137
0
        }
138
0
        if (c != '#') {
139
0
            return 1;
140
0
        }
141
0
        while (cur < end) {
142
0
            c = _PyTok_SourceByte(&tok->source, cur);
143
0
            if (c == '\0' || c == '\n') {
144
0
                break;
145
0
            }
146
0
            cur++;
147
0
        }
148
0
    }
149
0
    return 0;
150
0
}
151
152
int
153
_PyTokenizer_IsInteractive(const struct tok_state *tok)
154
61.0k
{
155
61.0k
    return _PyTok_ReaderIsInteractive(tok);
156
61.0k
}
157
158
void
159
_PyTokenizer_StopInteractive(struct tok_state *tok)
160
65.0k
{
161
65.0k
    _PyTok_ReaderStopInteractive(tok);
162
65.0k
}