Coverage Report

Created: 2026-10-31 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Parser/tokenizer/tokenizer.h
Line
Count
Source
1
#ifndef Py_TOKENIZER_H
2
#define Py_TOKENIZER_H
3
4
#include "Python.h"
5
#include "types.h"
6
7
struct tok_state;
8
9
typedef enum {
10
    _PyTokenizer_KEEP_NEWLINE,   // Preserve the original line terminator.
11
    _PyTokenizer_IMPLY_NEWLINE,  // Add a missing line terminator.
12
    _PyTokenizer_APPEND_NEWLINE, // Also append a blank line to terminated input.
13
} _PyTokenizer_Newline;
14
15
/* Reasons that the appended newline can change the token stream. */
16
enum {
17
    _PyTokenizer_NEWLINE_STATEMENT = 1,
18
    _PyTokenizer_NEWLINE_INDENT = 2,
19
    _PyTokenizer_NEWLINE_CONTINUATION = 4,
20
};
21
22
/* Initialize before use. metadata owns a reference released by _PyToken_Free;
23
   a consumer taking that reference must set metadata to NULL. */
24
struct token {
25
    int type;
26
    int level;
27
    unsigned char is_raw;
28
    unsigned char is_incomplete;
29
    _PyTok_Span span;
30
    _PyTok_Loc start_loc;
31
    _PyTok_Loc end_loc;
32
    PyObject *metadata;
33
};
34
35
typedef struct {
36
    const char *text;
37
    Py_ssize_t length;
38
    _PyTok_Span line_span;
39
    _PyTok_Off end_line_start;
40
    int implicit_newline;
41
    int at_eof;
42
} _PyToken_View;
43
44
/* Supplemental source context for a terminal error. location is the reporting
45
   cursor, independent of the scanner cursor; lineno == 0 means absent.
46
   The text span may cover multiple physical lines. */
47
typedef struct {
48
    _PyTok_Loc location;
49
    _PyTok_Span text_span;
50
} _PyTokenizer_Diagnostic;
51
52
typedef struct {
53
    int status;
54
    unsigned char implicit_newline;  // _PyTokenizer_Newline
55
    _PyTokenizer_Diagnostic diagnostic;
56
    _PyTok_Loc location;
57
    _PyTok_Off cursor;
58
    _PyTok_Span input_span;
59
    _PyTok_Span line_span;
60
    int level;
61
    char delimiter;
62
    _PyTok_Loc delimiter_loc;
63
    int in_formatted_string;
64
    int is_interactive;
65
    int is_file;
66
    PyObject *filename;
67
    PyObject *module;
68
    const char *encoding;
69
} _PyTokenizer_Info;
70
71
/* Get replaces the initialized token, releasing its previous metadata.
72
   Errors are returned as ERRORTOKEN, with or without a Python exception.
73
   In incomplete-input mode, an unfinished string retains its token type,
74
   sets is_incomplete and has E_INCOMPLETE status. Only f-string and t-string
75
   middle tokens retain their text span. No Python exception is set. */
76
void _PyTokenizer_Get(struct tok_state *, struct token *);
77
void _PyTokenizer_Free(struct tok_state *);
78
int _PyTokenizer_Traverse(struct tok_state *, visitproc, void *);
79
void _PyTokenizer_raise_init_error(PyObject *filename);
80
void _PyToken_Init(struct token *);
81
static inline void
82
_PyToken_Free(struct token *token)
83
3.82M
{
84
    Py_CLEAR(token->metadata);
85
3.82M
}
Unexecuted instantiation: errors.c:_PyToken_Free
Unexecuted instantiation: traceback.c:_PyToken_Free
Unexecuted instantiation: peg_api.c:_PyToken_Free
Unexecuted instantiation: reader.c:_PyToken_Free
Unexecuted instantiation: helpers.c:_PyToken_Free
Python-tokenize.c:_PyToken_Free
Line
Count
Source
83
2.00k
{
84
    Py_CLEAR(token->metadata);
85
2.00k
}
pegen.c:_PyToken_Free
Line
Count
Source
83
1.74M
{
84
    Py_CLEAR(token->metadata);
85
1.74M
}
pegen_errors.c:_PyToken_Free
Line
Count
Source
83
61.0k
{
84
    Py_CLEAR(token->metadata);
85
61.0k
}
Unexecuted instantiation: parser.c:_PyToken_Free
Unexecuted instantiation: parser_invalid.c:_PyToken_Free
lexer.c:_PyToken_Free
Line
Count
Source
83
2.01M
{
84
    Py_CLEAR(token->metadata);
85
2.01M
}
Unexecuted instantiation: layout.c:_PyToken_Free
Unexecuted instantiation: number.c:_PyToken_Free
Unexecuted instantiation: state.c:_PyToken_Free
Unexecuted instantiation: string.c:_PyToken_Free
Unexecuted instantiation: decoder.c:_PyToken_Free
Unexecuted instantiation: api.c:_PyToken_Free
Unexecuted instantiation: action_helpers.c:_PyToken_Free
Unexecuted instantiation: string_parser.c:_PyToken_Free
86
87
/* Views and borrowed snapshot references remain valid until the tokenizer is
88
   mutated or freed. Source spans may be discarded when reading more input. */
89
_PyTokenizer_Info _PyTokenizer_GetInfo(const struct tok_state *);
90
int _PyTokenizer_GetStatus(const struct tok_state *);
91
int _PyTokenizer_GetNewlineRequirements(const struct tok_state *);
92
/* An absent token span has a nonnull empty text view. */
93
const char *_PyToken_TextView(
94
    const struct tok_state *, const struct token *, Py_ssize_t *);
95
/* Use the token from the most recent Get. An absent span has nonnull empty text.
96
   line_span covers the token's complete physical line range. end_line_start
97
   is the logical offset of its final physical line. */
98
void _PyToken_GetView(
99
    const struct tok_state *tok, const struct token *token,
100
    _PyToken_View *view);
101
const char *_PyTokenizer_SpanView(
102
    const struct tok_state *, _PyTok_Span, Py_ssize_t *);
103
/* For retained input: 1-based lines clamp to the first or final line.
104
   The view excludes the newline and need not be NUL-terminated. */
105
const char *_PyTokenizer_LineView(
106
    const struct tok_state *, Py_ssize_t, Py_ssize_t *);
107
/* Return NUL-terminated retained input, or NULL without setting an exception
108
   for streaming input or interactive input before its first line. */
109
const char *_PyTokenizer_RetainedSource(const struct tok_state *);
110
void _PyTokenizer_SetContext(
111
    struct tok_state *tok, PyObject *filename, PyObject *module);
112
void _PyTokenizer_SetOptions(
113
    struct tok_state *tok, int extra_tokens, int type_comments);
114
void _PyTokenizer_AllowIncompleteInput(struct tok_state *, int);
115
void _PyTokenizer_ImplyDedents(struct tok_state *);
116
int _PyTokenizer_HasTrailingStatement(const struct tok_state *);
117
int _PyTokenizer_IsInteractive(const struct tok_state *);
118
void _PyTokenizer_StopInteractive(struct tok_state *);
119
120
struct tok_state *_PyTokenizer_FromString(
121
    const char *input, int exec_input, int preserve_crlf);
122
struct tok_state *_PyTokenizer_FromUTF8(
123
    const char *input, int exec_input, int preserve_crlf);
124
struct tok_state *_PyTokenizer_FromReadline(
125
    PyObject *readline, const char *encoding);
126
struct tok_state *_PyTokenizer_FromFile(
127
    FILE *fp, const char *encoding, const char *ps1, const char *ps2);
128
/* Return the declared encoding in PyMem-allocated storage, or NULL.
129
   An exception is set on error. */
130
char *_PyTokenizer_FindEncodingFilename(int, PyObject *);
131
132
#endif /* !Py_TOKENIZER_H */