Skip to content

Commit

Permalink
py/lexer: Support raw f-strings.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dpgeorge committed Jun 6, 2024
1 parent a066f23 commit 3c8089d
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 22 deletions.
14 changes: 6 additions & 8 deletions py/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,21 +661,19 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
}
#if MICROPY_PY_FSTRINGS
if (is_char_following(lex, 'f')) {
// raw-f-strings unsupported, immediately return (invalid) token.
lex->tok_kind = MP_TOKEN_FSTRING_RAW;
break;
is_fstring = true;
n_char = 2;
}
#endif
}
#if MICROPY_PY_FSTRINGS
else if (is_char(lex, 'f')) {
is_fstring = true;
n_char = 1;
if (is_char_following(lex, 'r')) {
// raw-f-strings unsupported, immediately return (invalid) token.
lex->tok_kind = MP_TOKEN_FSTRING_RAW;
break;
is_raw = true;
n_char = 2;
}
n_char = 1;
is_fstring = true;
}
#endif

Expand Down
1 change: 0 additions & 1 deletion py/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ typedef enum _mp_token_kind_t {
MP_TOKEN_LONELY_STRING_OPEN,
#if MICROPY_PY_FSTRINGS
MP_TOKEN_MALFORMED_FSTRING,
MP_TOKEN_FSTRING_RAW,
#endif

MP_TOKEN_NEWLINE,
Expand Down
3 changes: 0 additions & 3 deletions py/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1351,9 +1351,6 @@ mp_parse_tree_t mp_parse(mp_lexer_t *lex, mp_parse_input_kind_t input_kind) {
} else if (lex->tok_kind == MP_TOKEN_MALFORMED_FSTRING) {
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
MP_ERROR_TEXT("malformed f-string"));
} else if (lex->tok_kind == MP_TOKEN_FSTRING_RAW) {
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
MP_ERROR_TEXT("raw f-strings are not supported"));
#endif
} else {
exc = mp_obj_new_exception_msg(&mp_type_SyntaxError,
Expand Down
4 changes: 4 additions & 0 deletions tests/basics/string_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,7 @@ def foo(a, b):
f"cd---------------------------------"
f"e{y}f---------------------------------"
)

# Raw f-strings.
print(rf"\r\a\w {'f'} \s\t\r\i\n\g")
print(fr"\r{x}")
4 changes: 2 additions & 2 deletions tests/cmdline/cmd_parsetree.py.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
----------------
[ 1] file_input_2(1) (n=10)
tok(6)
tok(5)
[ 4] \(rule\|for_stmt\)(22) (n=4)
id(i)
[ 4] \(rule\|atom_paren\)(45) (n=1)
Expand All @@ -9,7 +9,7 @@
NULL
[ 6] \(rule\|expr_stmt\)(5) (n=2)
id(a)
tok(16)
tok(15)
[ 7] \(rule\|expr_stmt\)(5) (n=2)
id(b)
str(str)
Expand Down
8 changes: 0 additions & 8 deletions tests/cpydiff/core_fstring_raw.py

This file was deleted.

0 comments on commit 3c8089d

Please sign in to comment.