Skip to content

Commit 3c8089d

Browse files
committed
py/lexer: Support raw f-strings.
Support for raw str/bytes already exists, and extending that to raw f-strings is easy. It also reduces code size because it eliminates an error message. Signed-off-by: Damien George <damien@micropython.org>
1 parent a066f23 commit 3c8089d

File tree

6 files changed

+12
-22
lines changed

6 files changed

+12
-22
lines changed

py/lexer.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -661,21 +661,19 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
661661
}
662662
#if MICROPY_PY_FSTRINGS
663663
if (is_char_following(lex, 'f')) {
664-
// raw-f-strings unsupported, immediately return (invalid) token.
665-
lex->tok_kind = MP_TOKEN_FSTRING_RAW;
666-
break;
664+
is_fstring = true;
665+
n_char = 2;
667666
}
668667
#endif
669668
}
670669
#if MICROPY_PY_FSTRINGS
671670
else if (is_char(lex, 'f')) {
671+
is_fstring = true;
672+
n_char = 1;
672673
if (is_char_following(lex, 'r')) {
673-
// raw-f-strings unsupported, immediately return (invalid) token.
674-
lex->tok_kind = MP_TOKEN_FSTRING_RAW;
675-
break;
674+
is_raw = true;
675+
n_char = 2;
676676
}
677-
n_char = 1;
678-
is_fstring = true;
679677
}
680678
#endif
681679

py/lexer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ typedef enum _mp_token_kind_t {
4646
MP_TOKEN_LONELY_STRING_OPEN,
4747
#if MICROPY_PY_FSTRINGS
4848
MP_TOKEN_MALFORMED_FSTRING,
49-
MP_TOKEN_FSTRING_RAW,
5049
#endif
5150

5251
MP_TOKEN_NEWLINE,

py/parse.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,9 +1351,6 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
13511351
} else if (lex->tok_kind == MP_TOKEN_MALFORMED_FSTRING) {
13521352
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
13531353
MP_ERROR_TEXT("malformed f-string"));
1354-
} else if (lex->tok_kind == MP_TOKEN_FSTRING_RAW) {
1355-
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
1356-
MP_ERROR_TEXT("raw f-strings are not supported"));
13571354
#endif
13581355
} else {
13591356
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,

tests/basics/string_fstring.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ def foo(a, b):
7575
f"cd---------------------------------"
7676
f"e{y}f---------------------------------"
7777
)
78+
79+
# Raw f-strings.
80+
print(rf"\r\a\w {'f'} \s\t\r\i\n\g")
81+
print(fr"\r{x}")

tests/cmdline/cmd_parsetree.py.exp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
----------------
22
[ 1] file_input_2(1) (n=10)
3-
tok(6)
3+
tok(5)
44
[ 4] \(rule\|for_stmt\)(22) (n=4)
55
id(i)
66
[ 4] \(rule\|atom_paren\)(45) (n=1)
@@ -9,7 +9,7 @@
99
NULL
1010
[ 6] \(rule\|expr_stmt\)(5) (n=2)
1111
id(a)
12-
tok(16)
12+
tok(15)
1313
[ 7] \(rule\|expr_stmt\)(5) (n=2)
1414
id(b)
1515
str(str)

tests/cpydiff/core_fstring_raw.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy