Skip to content

Commit 2b00047

Browse files
committed
py/lexer: Properly classify floats that look like hex numbers.
Eg 0e0 almost looks like a hex number but in fact is a float.
1 parent 0be3c70 commit 2b00047

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

py/lexer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ STATIC bool is_following_digit(mp_lexer_t *lex) {
105105
return unichar_isdigit(lex->chr1);
106106
}
107107

108-
STATIC bool is_following_letter(mp_lexer_t *lex) {
109-
return unichar_isalpha(lex->chr1);
108+
STATIC bool is_following_base_char(mp_lexer_t *lex) {
109+
const unichar chr1 = lex->chr1 | 0x20;
110+
return chr1 == 'b' || chr1 == 'o' || chr1 == 'x';
110111
}
111112

112113
STATIC bool is_following_odigit(mp_lexer_t *lex) {
@@ -541,7 +542,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) {
541542
lex->tok_kind = MP_TOKEN_FLOAT_OR_IMAG;
542543
} else {
543544
lex->tok_kind = MP_TOKEN_INTEGER;
544-
if (is_char(lex, '0') && is_following_letter(lex)) {
545+
if (is_char(lex, '0') && is_following_base_char(lex)) {
545546
forced_integer = true;
546547
}
547548
}

tests/float/float1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
print(.12)
55
print(1.)
66
print(1.2)
7+
print(0e0)
8+
print(0e+0)
9+
print(0e-0)
710

811
# float construction
912
print(float(1.2))

0 commit comments

Comments
 (0)