8000 py: f-strings are now opt-in compile flag · micropython/micropython@4ad8eb4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ad8eb4

Browse files
committed
py: f-strings are now opt-in compile flag
1 parent 6e0f83b commit 4ad8eb4

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed

ports/bare-arm/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define MICROPY_PY_ARRAY (0)
3838
#define MICROPY_PY_ATTRTUPLE (0)
3939
#define MICROPY_PY_COLLECTIONS (0)
40+
#define MICROPY_PY_FSTRING (0)
4041
#define MICROPY_PY_MATH (0)
4142
#define MICROPY_PY_CMATH (0)
4243
#define MICROPY_PY_IO (0)

ports/unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
#define MICROPY_PY_SYS_EXC_INFO (1)
123123
#define MICROPY_PY_COLLECTIONS_DEQUE (1)
124124
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)
125+
#define MICROPY_PY_FSTRING (1)
125126
#ifndef MICROPY_PY_MATH_SPECIAL_FUNCTIONS
126127
#define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (1)
127128
#endif

py/lexer.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,19 @@ STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw, bool is_fstring)
334334
}
335335

336336
size_t n_closing = 0;
337+
# if MICROPY_PY_FSTRING
337338
bool in_expression = false;
338339
bool expression_eat = true;
340+
# endif
339341

340342
while (!is_end(lex) && (num_quotes > 1 || !is_char(lex, '\n')) && n_closing < num_quotes) {
341343
if (is_char(lex, quote_char)) {
342344
n_closing += 1;
343345
vstr_add_char(&lex->vstr, CUR_CHAR(lex));
344346
} else {
345347
n_closing = 0;
348+
349+
# if MICROPY_PY_FSTRING
346350
if (is_fstring && is_char(lex, '{')) {
347351
vstr_add_char(&lex->vstr, CUR_CHAR(lex));
348352
in_expression = !in_expression;
@@ -387,6 +391,7 @@ STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw, bool is_fstring)
387391
next_char(lex);
388392
continue;
389393
}
394+
# endif
390395

391396
if (is_char(lex, '\\')) {
392397
next_char(lex);
@@ -621,18 +626,23 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
621626
kind = MP_TOKEN_BYTES;
622627
n_char = 2;
623628
}
629+
# if MICROPY_PY_FSTRING
624630
if (is_char_following(lex, 'f')) {
625631
lex->tok_kind = MP_TOKEN_FSTRING_RAW;
626632
break;
627633
}
628-
} else if (is_char(lex, 'f')) {
634+
# endif
635+
}
636+
# if MICROPY_PY_FSTRING
637+
else if (is_char(lex, 'f')) {
629638
if (is_char_following(lex, 'r')) {
630639
lex->tok_kind = MP_TOKEN_FSTRING_RAW;
631640
break;
632641
}
633642
n_char = 1;
634643
is_fstring = true;
635644
}
645+
# endif
636646

637647
// Set or check token kind
638648
if (lex->tok_kind == MP_TOKEN_END) {

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,12 @@ typedef double mp_float_t;
11081108
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (0)
11091109
#endif
11101110

1111+
// Whether to include support for PEP-498 f-strings
1112+
#ifndef MICROPY_PY_FSTRING
1113+
#define MICROPY_PY_FSTRING (0)
1114+
#endif
1115+
1116+
11111117
// Whether to provide "math" module
11121118
#ifndef MICROPY_PY_MATH
11131119
#define MICROPY_PY_MATH (1)

py/parse.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,12 +1155,14 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
11551155
} else if (lex->tok_kind == MP_TOKEN_DEDENT_MISMATCH) {
11561156
exc = mp_obj_new_exception_msg(&mp_type_IndentationError,
11571157
MP_ERROR_TEXT("unindent doesn't match any outer indent level"));
1158+
# if MICROPY_PY_FSTRING
11581159
} else if (lex->tok_kind == MP_TOKEN_MALFORMED_FSTRING) {
11591160
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
11601161
MP_ERROR_TEXT("malformed f-string"));
11611162
} else if (lex->tok_kind == MP_TOKEN_FSTRING_RAW) {
11621163
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
11631164
MP_ERROR_TEXT("raw f-strings are not supported"));
1165+
# endif
11641166
} else {
11651167
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
11661168
MP_ERROR_TEXT("invalid syntax"));

0 commit comments

Comments
 (0)
0