Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Parser/lexer/layout.c
Line
Count
Source
1
#include "Python.h"
2
#include "errcode.h"
3
#include "pycore_token.h"
4
5
#include "lexer_internal.h"
6
#include "../tokenizer/helpers.h"
7
#include "../tokenizer/reader.h"
8
9
1.09k
#define TABSIZE 8
10
1.09k
#define ALTTABSIZE 1
11
12
int
13
_PyLexer_ContinueLine(struct tok_state *tok)
14
1.22k
{
15
1.22k
    int c = tok_nextc(tok);
16
1.22k
    if (c == '\r') {
17
0
        c = tok_nextc(tok);
18
0
    }
19
1.22k
    if (c != '\n') {
20
49
        tok->done = E_LINECONT;
21
49
        return -1;
22
49
    }
23
1.17k
    c = tok_nextc(tok);
24
1.17k
    if (tok->implicit_newline == _PyTokenizer_APPEND_NEWLINE) {
25
0
        tok->needs_newline |= _PyTokenizer_NEWLINE_CONTINUATION;
26
0
    }
27
1.17k
    if (c == EOF) {
28
56
        tok->done = E_EOF;
29
56
        tok->cur = tok->inp;
30
56
        return -1;
31
1.11k
    } else {
32
1.11k
        tok_backup(tok, c);
33
1.11k
    }
34
1.11k
    return c;
35
1.17k
}
36
37
38
static int
39
update_indentation(struct tok_state *tok, int col, int altcol)
40
193k
{
41
193k
    lexer_layout_state *layout = &tok->layout;
42
193k
    if (col == layout->stack[layout->depth].column) {
43
176k
        if (altcol != layout->stack[layout->depth].alternate_column) {
44
1
            _PyTokenizer_indenterror(tok);
45
1
            return -1;
46
1
        }
47
176k
    }
48
17.7k
    else if (col > layout->stack[layout->depth].column) {
49
9.72k
        if (layout->depth + 1 >= MAXINDENT) {
50
0
            tok->done = E_TOODEEP;
51
0
            tok->cur = tok->inp;
52
0
            return -1;
53
0
        }
54
9.72k
        if (altcol <= layout->stack[layout->depth].alternate_column) {
55
1
            _PyTokenizer_indenterror(tok);
56
1
            return -1;
57
1
        }
58
9.72k
        layout->pending++;
59
9.72k
        layout->stack[++layout->depth] = (indentation_level){col, altcol};
60
9.72k
    }
61
7.99k
    else {
62
17.1k
        while (layout->depth > 0 &&
63
12.7k
            col < layout->stack[layout->depth].column) {
64
9.19k
            layout->pending--;
65
9.19k
            layout->depth--;
66
9.19k
        }
67
7.99k
        if (col != layout->stack[layout->depth].column) {
68
7
            tok->done = E_DEDENT;
69
7
            tok->cur = tok->inp;
70
7
            return -1;
71
7
        }
72
7.99k
        if (altcol != layout->stack[layout->depth].alternate_column) {
73
1
            _PyTokenizer_indenterror(tok);
74
1
            return -1;
75
1
        }
76
7.99k
    }
77
193k
    return 0;
78
193k
}
79
80
int
81
_PyLexer_BeginLine(struct tok_state *tok)
82
316k
{
83
316k
    assert(tok->layout.at_bol);
84
316k
    int c;
85
316k
    int blankline = 0;
86
316k
    int col = 0;
87
316k
    int altcol = 0;
88
316k
    tok->layout.at_bol = 0;
89
316k
    int cont_line_col = 0;
90
549k
    for (;;) {
91
549k
        c = tok_nextc(tok);
92
549k
        if (c == ' ') {
93
230k
            col++, altcol++;
94
230k
        }
95
318k
        else if (c == '\t') {
96
546
            col = (col / TABSIZE + 1) * TABSIZE;
97
546
            altcol = (altcol / ALTTABSIZE + 1) * ALTTABSIZE;
98
546
        }
99
317k
        else if (c == '\014')  {/* Control-L (formfeed) */
100
999
            col = altcol = 0; /* For Emacs users */
101
999
        }
102
316k
        else if (c == '\\') {
103
            // Indentation cannot be split over multiple physical lines
104
            // using backslashes. This means that if we found a backslash
105
            // preceded by whitespace, **the first one we find** determines
106
            // the level of indentation of whatever comes next.
107
885
            cont_line_col = cont_line_col ? cont_line_col : col;
108
885
            if ((c = _PyLexer_ContinueLine(tok)) == -1) {
109
31
                return -1;
110
31
            }
111
885
        }
112
316k
        else if (c == EOF && PyErr_Occurred()) {
113
98
            return -1;
114
98
        }
115
315k
        else {
116
315k
            break;
117
315k
        }
118
549k
    }
119
315k
    tok_backup(tok, c);
120
315k
    if (c == '\n' && tok->implicit_newline == _PyTokenizer_IMPLY_NEWLINE &&
121
51
        tok->level == 0 && (cont_line_col || col)) {
122
        // Without the newline, a whitespace-only line affects indentation.
123
37
        tok->needs_newline |= _PyTokenizer_NEWLINE_INDENT;
124
37
    }
125
315k
    if (c == '#' || c == '\n' || c == '\r') {
126
86.6k
        int interactive = _PyTok_ReaderIsInteractive(tok);
127
        /* Lines with only whitespace and/or comments
128
           shouldn't affect the indentation and are
129
           not passed to the parser as NEWLINE tokens,
130
           except *totally* empty lines in interactive
131
           mode, which signal the end of a command group. */
132
86.6k
        if (col == 0 && c == '\n' && interactive) {
133
0
            blankline = 0; /* Let it through */
134
0
        }
135
86.6k
        else if (interactive && tok->lineno == 1) {
136
            /* In interactive mode, if the first line contains
137
               only spaces and/or a comment, let it through. */
138
0
            blankline = 0;
139
0
            col = altcol = 0;
140
0
        }
141
86.6k
        else {
142
86.6k
            blankline = 1; /* Ignore completely */
143
86.6k
        }
144
86.6k
    }
145
315k
    if (!blankline && tok->level == 0) {
146
193k
        col = cont_line_col ? cont_line_col : col;
147
193k
        altcol = cont_line_col ? cont_line_col : altcol;
148
193k
        if (update_indentation(tok, col, altcol) < 0) {
149
10
            return -1;
150
10
        }
151
193k
    }
152
315k
    return blankline;
153
315k
}
154
155
int
156
_PyLexer_IndentationToken(struct tok_state *tok, struct token *token)
157
18.8k
{
158
18.8k
    assert(tok->layout.pending != 0);
159
18.8k
    _PyTok_Off p_start = -1;
160
18.8k
    _PyTok_Off p_end = -1;
161
18.8k
    if (tok->layout.pending < 0) {
162
9.17k
        if (tok->tok_extra_tokens) {
163
60
            p_start = tok->cur;
164
60
            p_end = tok->cur;
165
60
        }
166
9.17k
        tok->layout.pending++;
167
9.17k
        return _PyLexer_token_setup(tok, token, DEDENT, p_start, p_end);
168
9.17k
    }
169
9.72k
    else {
170
9.72k
        if (tok->tok_extra_tokens) {
171
64
            p_start = tok->buf_offset;
172
64
            p_end = tok->cur;
173
64
        }
174
9.72k
        tok->layout.pending--;
175
9.72k
        return _PyLexer_token_setup(tok, token, INDENT, p_start, p_end);
176
9.72k
    }
177
18.8k
}
178
179
int
180
_PyLexer_Newline(struct tok_state *tok, struct token *token, int blankline)
181
251k
{
182
251k
    if (!blankline && tok->implicit_newline == _PyTokenizer_IMPLY_NEWLINE) {
183
        // The statement's terminating newline was not in the input.
184
59.2k
        tok->needs_newline |= _PyTokenizer_NEWLINE_STATEMENT;
185
59.2k
    }
186
251k
    tok->layout.at_bol = 1;
187
251k
    if (blankline || tok->level > 0) {
188
122k
        if (!tok->tok_extra_tokens) {
189
121k
            return 0;
190
121k
        }
191
122k
    }
192
129k
    else if (!tok->layout.comment_newline || !tok->tok_extra_tokens) {
193
129k
        return _PyLexer_token_setup(tok, token, NEWLINE,
194
129k
                                    tok->start, tok->cur - 1);
195
129k
    }
196
140
    tok->layout.comment_newline = 0;
197
140
    return _PyLexer_token_setup(tok, token, NL, tok->start, tok->cur);
198
251k
}
199
200
void
201
_PyLexer_ImplyDedents(struct tok_state *tok)
202
0
{
203
0
    if (tok->layout.depth != 0) {
204
0
        tok->layout.pending = -tok->layout.depth;
205
0
        tok->layout.depth = 0;
206
0
    }
207
0
}