Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Parser/lexer/number.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_token.h"
3
4
#include "lexer_internal.h"
5
#include "../tokenizer/helpers.h"
6
7
105k
#define MAKE_TOKEN(token_type) _PyLexer_token_setup(tok, token, token_type, p_start, p_end)
8
9
static int
10
lookahead(struct tok_state *tok, const char *test)
11
8.88k
{
12
8.88k
    const char *s = test;
13
8.88k
    int res = 0;
14
23.6k
    while (1) {
15
23.6k
        int c = tok_nextc(tok);
16
23.6k
        if (*s == 0) {
17
8.77k
            res = !is_potential_identifier_char(c);
18
8.77k
        }
19
14.8k
        else if (c == *s) {
20
14.7k
            s++;
21
14.7k
            continue;
22
14.7k
        }
23
24
8.88k
        tok_backup(tok, c);
25
23.6k
        while (s != test) {
26
14.7k
            tok_backup(tok, *--s);
27
14.7k
        }
28
8.88k
        return res;
29
23.6k
    }
30
8.88k
}
31
32
static int
33
105k
verify_end_of_number(struct tok_state *tok, int c, const char *kind) {
34
105k
    if (tok->tok_extra_tokens) {
35
        // When we are parsing extra tokens, we don't want to emit warnings
36
        // about invalid literals, because we want to be a bit more liberal.
37
56
        return 1;
38
56
    }
39
    /* Emit a deprecation warning only if the numeric literal is immediately
40
     * followed by one of keywords which can occur after a numeric literal
41
     * in valid code: "and", "else", "for", "if", "in", "is" and "or".
42
     * It allows to gradually deprecate existing valid code without adding
43
     * warning before error in most cases of invalid numeric literal (which
44
     * would be confusing and break existing tests).
45
     * Raise a syntax error with slightly better message than plain
46
     * "invalid syntax" if the numeric literal is immediately followed by
47
     * other keyword or identifier.
48
     */
49
105k
    int r = 0;
50
105k
    if (c == 'a') {
51
974
        r = lookahead(tok, "nd");
52
974
    }
53
104k
    else if (c == 'e') {
54
1.00k
        r = lookahead(tok, "lse");
55
1.00k
    }
56
103k
    else if (c == 'f') {
57
2.82k
        r = lookahead(tok, "or");
58
2.82k
    }
59
100k
    else if (c == 'i') {
60
1.84k
        int c2 = tok_nextc(tok);
61
1.84k
        if (c2 == 'f' || c2 == 'n' || c2 == 's') {
62
1.82k
            r = 1;
63
1.82k
        }
64
1.84k
        tok_backup(tok, c2);
65
1.84k
    }
66
98.9k
    else if (c == 'o') {
67
3.76k
        r = lookahead(tok, "r");
68
3.76k
    }
69
95.1k
    else if (c == 'n') {
70
318
        r = lookahead(tok, "ot");
71
318
    }
72
105k
    if (r) {
73
10.5k
        tok_backup(tok, c);
74
10.5k
        if (_PyTokenizer_parser_warn(tok, PyExc_SyntaxWarning,
75
10.5k
                "invalid %s literal", kind))
76
0
        {
77
0
            return 0;
78
0
        }
79
10.5k
        tok_nextc(tok);
80
10.5k
    }
81
94.9k
    else /* In future releases, only error will remain. */
82
94.9k
    if (c < 128 && is_potential_identifier_char(c)) {
83
266
        tok_backup(tok, c);
84
266
        _PyTokenizer_syntaxerror(tok, "invalid %s literal", kind);
85
266
        return 0;
86
266
    }
87
105k
    return 1;
88
105k
}
89
90
static int
91
tok_decimal_tail(struct tok_state *tok)
92
92.4k
{
93
92.4k
    int c;
94
95
92.9k
    while (1) {
96
312k
        do {
97
312k
            c = tok_nextc(tok);
98
312k
        } while (Py_ISDIGIT(c));
99
92.9k
        if (c != '_') {
100
92.4k
            break;
101
92.4k
        }
102
504
        c = tok_nextc(tok);
103
504
        if (!Py_ISDIGIT(c)) {
104
14
            tok_backup(tok, c);
105
14
            _PyTokenizer_syntaxerror(tok, "invalid decimal literal");
106
14
            return 0;
107
14
        }
108
504
    }
109
92.4k
    return c;
110
92.4k
}
111
112
int
113
_PyLexer_scan_number(struct tok_state *tok, struct token *token, int c,
114
                     int leading_dot)
115
105k
{
116
105k
    _PyTok_Off p_start = -1;
117
105k
    _PyTok_Off p_end = -1;
118
119
105k
    if (leading_dot) {
120
4.38k
        goto fraction;
121
4.38k
    }
122
101k
    if (c == '0') {
123
        /* Hex, octal or binary -- maybe. */
124
31.5k
        c = tok_nextc(tok);
125
31.5k
        if (c == 'x' || c == 'X') {
126
            /* Hex */
127
14.2k
            c = tok_nextc(tok);
128
14.4k
            do {
129
14.4k
                if (c == '_') {
130
216
                    c = tok_nextc(tok);
131
216
                }
132
14.4k
                if (!Py_ISXDIGIT(c)) {
133
17
                    tok_backup(tok, c);
134
17
                    return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "invalid hexadecimal literal"));
135
17
                }
136
75.7k
                do {
137
75.7k
                    c = tok_nextc(tok);
138
75.7k
                } while (Py_ISXDIGIT(c));
139
14.4k
            } while (c == '_');
140
14.2k
            if (!verify_end_of_number(tok, c, "hexadecimal")) {
141
1
                return MAKE_TOKEN(ERRORTOKEN);
142
1
            }
143
14.2k
        }
144
17.2k
        else if (c == 'o' || c == 'O') {
145
            /* Octal */
146
1.38k
            c = tok_nextc(tok);
147
3.51k
            do {
148
3.51k
                if (c == '_') {
149
2.13k
                    c = tok_nextc(tok);
150
2.13k
                }
151
3.51k
                if (c < '0' || c >= '8') {
152
21
                    if (Py_ISDIGIT(c)) {
153
1
                        return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok,
154
1
                                "invalid digit '%c' in octal literal", c));
155
1
                    }
156
20
                    else {
157
20
                        tok_backup(tok, c);
158
20
                        return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "invalid octal literal"));
159
20
                    }
160
21
                }
161
21.3k
                do {
162
21.3k
                    c = tok_nextc(tok);
163
21.3k
                } while ('0' <= c && c < '8');
164
3.49k
            } while (c == '_');
165
1.36k
            if (Py_ISDIGIT(c)) {
166
2
                return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok,
167
2
                        "invalid digit '%c' in octal literal", c));
168
2
            }
169
1.36k
            if (!verify_end_of_number(tok, c, "octal")) {
170
1
                return MAKE_TOKEN(ERRORTOKEN);
171
1
            }
172
1.36k
        }
173
15.8k
        else if (c == 'b' || c == 'B') {
174
            /* Binary */
175
679
            c = tok_nextc(tok);
176
1.03k
            do {
177
1.03k
                if (c == '_') {
178
368
                    c = tok_nextc(tok);
179
368
                }
180
1.03k
                if (c != '0' && c != '1') {
181
27
                    if (Py_ISDIGIT(c)) {
182
1
                        return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "invalid digit '%c' in binary literal", c));
183
1
                    }
184
26
                    else {
185
26
                        tok_backup(tok, c);
186
26
                        return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "invalid binary literal"));
187
26
                    }
188
27
                }
189
6.28k
                do {
190
6.28k
                    c = tok_nextc(tok);
191
6.28k
                } while (c == '0' || c == '1');
192
1.01k
            } while (c == '_');
193
652
            if (Py_ISDIGIT(c)) {
194
1
                return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "invalid digit '%c' in binary literal", c));
195
1
            }
196
651
            if (!verify_end_of_number(tok, c, "binary")) {
197
3
                return MAKE_TOKEN(ERRORTOKEN);
198
3
            }
199
651
        }
200
15.1k
        else {
201
15.1k
            int nonzero = 0;
202
            /* maybe old-style octal; c is first char of it */
203
            /* in any case, allow '0' as a literal */
204
17.6k
            while (1) {
205
17.6k
                if (c == '_') {
206
294
                    c = tok_nextc(tok);
207
294
                    if (!Py_ISDIGIT(c)) {
208
4
                        tok_backup(tok, c);
209
4
                        return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "invalid decimal literal"));
210
4
                    }
211
294
                }
212
17.6k
                if (c != '0') {
213
15.1k
                    break;
214
15.1k
                }
215
2.44k
                c = tok_nextc(tok);
216
2.44k
            }
217
15.1k
            _PyTok_Off zeros_end = tok->cur;
218
15.1k
            if (Py_ISDIGIT(c)) {
219
751
                nonzero = 1;
220
751
                c = tok_decimal_tail(tok);
221
751
                if (c == 0) {
222
2
                    return MAKE_TOKEN(ERRORTOKEN);
223
2
                }
224
751
            }
225
15.1k
            if (c == '.') {
226
901
                c = tok_nextc(tok);
227
901
                goto fraction;
228
901
            }
229
14.2k
            else if (c == 'e' || c == 'E') {
230
934
                goto exponent;
231
934
            }
232
13.3k
            else if (c == 'j' || c == 'J') {
233
921
                goto imaginary;
234
921
            }
235
12.3k
            else if (nonzero && !tok->tok_extra_tokens) {
236
                /* Old-style octal: now disallowed. */
237
60
                tok_backup(tok, c);
238
60
                return MAKE_TOKEN(_PyTokenizer_syntaxerror_known_range(
239
60
                        tok, (int)(tok->start + 1 - tok->line_start),
240
60
                        (int)(zeros_end - tok->line_start),
241
60
                        "leading zeros in decimal integer "
242
60
                        "literals are not permitted; "
243
60
                        "use an 0o prefix for octal integers"));
244
60
            }
245
12.3k
            if (!verify_end_of_number(tok, c, "decimal")) {
246
34
                return MAKE_TOKEN(ERRORTOKEN);
247
34
            }
248
12.3k
        }
249
31.5k
    }
250
69.8k
    else {
251
        /* Decimal */
252
69.8k
        c = tok_decimal_tail(tok);
253
69.8k
        if (c == 0) {
254
10
            return MAKE_TOKEN(ERRORTOKEN);
255
10
        }
256
69.8k
        {
257
            /* Accept floating-point numbers. */
258
69.8k
            if (c == '.') {
259
5.47k
                c = tok_nextc(tok);
260
10.7k
    fraction:
261
                /* Fraction */
262
10.7k
                if (Py_ISDIGIT(c)) {
263
7.87k
                    c = tok_decimal_tail(tok);
264
7.87k
                    if (c == 0) {
265
1
                        return MAKE_TOKEN(ERRORTOKEN);
266
1
                    }
267
7.87k
                }
268
10.7k
            }
269
75.1k
            if (c == 'e' || c == 'E') {
270
14.0k
                int e;
271
14.9k
              exponent:
272
14.9k
                e = c;
273
                /* Exponent part */
274
14.9k
                c = tok_nextc(tok);
275
14.9k
                if (c == '+' || c == '-') {
276
6.16k
                    c = tok_nextc(tok);
277
6.16k
                    if (!Py_ISDIGIT(c)) {
278
11
                        tok_backup(tok, c);
279
11
                        return MAKE_TOKEN(_PyTokenizer_syntaxerror(tok, "invalid decimal literal"));
280
11
                    }
281
8.79k
                } else if (!Py_ISDIGIT(c)) {
282
1.00k
                    tok_backup(tok, c);
283
1.00k
                    if (!verify_end_of_number(tok, e, "decimal")) {
284
61
                        return MAKE_TOKEN(ERRORTOKEN);
285
61
                    }
286
940
                    tok_backup(tok, e);
287
940
                    p_start = tok->start;
288
940
                    p_end = tok->cur;
289
940
                    return MAKE_TOKEN(NUMBER);
290
1.00k
                }
291
13.9k
                c = tok_decimal_tail(tok);
292
13.9k
                if (c == 0) {
293
1
                    return MAKE_TOKEN(ERRORTOKEN);
294
1
                }
295
13.9k
            }
296
75.0k
            if (c == 'j' || c == 'J') {
297
                /* Imaginary part */
298
5.28k
    imaginary:
299
5.28k
                c = tok_nextc(tok);
300
5.28k
                if (!verify_end_of_number(tok, c, "imaginary")) {
301
18
                    return MAKE_TOKEN(ERRORTOKEN);
302
18
                }
303
5.28k
            }
304
70.7k
            else if (!verify_end_of_number(tok, c, "decimal")) {
305
148
                return MAKE_TOKEN(ERRORTOKEN);
306
148
            }
307
75.0k
        }
308
75.0k
    }
309
104k
    tok_backup(tok, c);
310
104k
    p_start = tok->start;
311
104k
    p_end = tok->cur;
312
104k
    return MAKE_TOKEN(NUMBER);
313
101k
}