Coverage Report

Created: 2026-10-17 06:15

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
_PyTokenizer_Info
11
_PyTokenizer_GetInfo(const struct tok_state *tok)
12
224k
{
13
224k
    _PyTokenizer_Info info = {
14
224k
        .status = tok->done,
15
224k
        .diagnostic = tok->diagnostic,
16
224k
        .location = {tok->lineno, tok->line_start < 0
17
224k
            ? -1 : (int)(tok->cur - tok->line_start)},
18
224k
        .cursor = tok->cur,
19
224k
        .input_span = {tok->buf_offset, tok->inp},
20
224k
        .line_span = {tok->line_start, tok->inp},
21
224k
        .level = tok->level,
22
224k
        .delimiter_loc = {-1, -1},
23
224k
        .in_formatted_string = tok->ftstring_depth != 0,
24
224k
        .is_interactive = _PyTok_ReaderIsInteractive(tok),
25
224k
        .is_file = tok->reader->fp != NULL && tok->reader->fp != stdin,
26
224k
        .filename = tok->filename,
27
224k
        .module = tok->module,
28
224k
        .encoding = tok->reader->encoding,
29
224k
    };
30
224k
    if (tok->level > 0) {
31
25.4k
        int level = tok->level - 1;
32
25.4k
        info.delimiter = tok->parenstack[level];
33
25.4k
        info.delimiter_loc = (_PyTok_Loc){
34
25.4k
            tok->parenlinenostack[level], tok->parencolstack[level]};
35
25.4k
    }
36
224k
    return info;
37
224k
}
38
39
const char *
40
_PyToken_TextView(const struct tok_state *tok, const struct token *token,
41
                  Py_ssize_t *length)
42
1.64M
{
43
1.64M
    assert(length != NULL);
44
1.64M
    if (token->span.start < 0) {
45
31.1k
        assert(token->span.start == -1 && token->span.end == -1);
46
31.1k
        *length = 0;
47
31.1k
        return "";
48
31.1k
    }
49
1.60M
    return _PyTok_SourceSpanView(&tok->source, token->span, length);
50
1.64M
}
51
52
const char *
53
_PyTokenizer_SpanView(const struct tok_state *tok, _PyTok_Span span,
54
                      Py_ssize_t *length)
55
52.5k
{
56
52.5k
    return _PyTok_SourceSpanView(&tok->source, span, length);
57
52.5k
}
58
59
void
60
_PyToken_GetView(const struct tok_state *tok, const struct token *token,
61
                 _PyToken_View *view)
62
2.06k
{
63
2.06k
    assert(view != NULL);
64
2.06k
    view->text = _PyToken_TextView(tok, token, &view->length);
65
2.06k
    view->end_line_start = tok->line_start;
66
2.06k
    view->line_span = (_PyTok_Span){
67
2.06k
        ISSTRINGLIT(token->type)
68
2.06k
            ? token->span.start - token->start_loc.byte_col : tok->line_start,
69
2.06k
        tok->inp,
70
2.06k
    };
71
2.06k
    view->implicit_newline = tok->implicit_newline;
72
2.06k
    view->at_eof = tok->done == E_EOF;
73
2.06k
}
74
75
const char *
76
_PyTokenizer_LineView(const struct tok_state *tok, Py_ssize_t lineno,
77
                      Py_ssize_t *length)
78
257
{
79
257
    return _PyTok_SourceLineView(&tok->source, lineno, length);
80
257
}
81
82
void
83
_PyTokenizer_SetContext(struct tok_state *tok, PyObject *filename,
84
                        PyObject *module)
85
62.5k
{
86
62.5k
    Py_XINCREF(filename);
87
62.5k
    Py_XINCREF(module);
88
62.5k
    Py_XSETREF(tok->filename, filename);
89
62.5k
    Py_XSETREF(tok->module, module);
90
62.5k
}
91
92
void
93
_PyTokenizer_SetOptions(struct tok_state *tok, int extra_tokens,
94
                        int type_comments)
95
62.5k
{
96
62.5k
    tok->tok_extra_tokens = extra_tokens;
97
62.5k
    tok->type_comments = type_comments;
98
62.5k
}
99
100
void
101
_PyTokenizer_ImplyDedents(struct tok_state *tok)
102
0
{
103
0
    _PyLexer_ImplyDedents(tok);
104
0
}
105
106
int
107
_PyTokenizer_HasTrailingStatement(const struct tok_state *tok)
108
0
{
109
0
    _PyTok_Off cur = tok->cur;
110
0
    _PyTok_Off end = tok->source.base_offset + tok->source.len;
111
0
    while (cur < end) {
112
0
        int c = _PyTok_SourceByte(&tok->source, cur++);
113
0
        if (c == '\0') {
114
0
            return 0;
115
0
        }
116
0
        if (c == ' ' || c == '\t' || c == '\n' || c == '\014') {
117
0
            continue;
118
0
        }
119
0
        if (c != '#') {
120
0
            return 1;
121
0
        }
122
0
        while (cur < end) {
123
0
            c = _PyTok_SourceByte(&tok->source, cur);
124
0
            if (c == '\0' || c == '\n') {
125
0
                break;
126
0
            }
127
0
            cur++;
128
0
        }
129
0
    }
130
0
    return 0;
131
0
}
132
133
int
134
_PyTokenizer_IsInteractive(const struct tok_state *tok)
135
50.3k
{
136
50.3k
    return _PyTok_ReaderIsInteractive(tok);
137
50.3k
}
138
139
void
140
_PyTokenizer_StopInteractive(struct tok_state *tok)
141
54.2k
{
142
54.2k
    _PyTok_ReaderStopInteractive(tok);
143
54.2k
}