Skip to content

Commit 32647cd

Browse files
committed
lexer: catch concatenation of f'' and '' strings
This turns the "edge case" into a parse-time error.
1 parent acd7b89 commit 32647cd

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

py/lexer.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
583583
// MP_TOKEN_END is used to indicate that this is the first string token
584584
lex->tok_kind = MP_TOKEN_END;
585585

586+
bool saw_normal = false, saw_fstring = false;
587+
586588
// Loop to accumulate string/bytes literals
587589
do {
588590
// parse type codes
@@ -619,6 +621,17 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
619621
is_fstring = true;
620622
}
621623

624+
if (is_fstring) {
625+
saw_fstring = true;
626+
} else {
627+
saw_normal = true;
628+
}
629+
630+
if (saw_fstring && saw_normal) {
631+
// Can't concatenate f-string with normal string
632+
break;
633+
}
634+
622635
// Set or check token kind
623636
if (lex->tok_kind == MP_TOKEN_END) {
624637
lex->tok_kind = kind;

tests/basics/string_pep498_fstring.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ def foo():
104104
x = 10
105105
y = 'hi'
106106
assert (f'h' f'i') == 'hi'
107-
assert (f'h' 'i') == 'hi'
108-
assert ('h' f'i') == 'hi'
107+
#assert (f'h' 'i') == 'hi'
108+
#assert ('h' f'i') == 'hi'
109109
assert f'{x:^4}' == ' 10 '
110-
assert ('a' 'b' f'{x}' f'str<{y:^4}>' 'd' 'e') == 'ab10str< hi >de'
110+
#assert ('a' 'b' f'{x}' f'str<{y:^4}>' 'd' 'e') == 'ab10str< hi >de'
111111

112112
# Other tests
113113
assert f'{{{4*10}}}' == '{40}'

0 commit comments

Comments
 (0)