lexer: catch concatenation of f'' and '' strings

This turns the "edge case" into a parse-time error.
crypto-aes
Jeff Epler 3 years ago
parent acd7b8932f
commit 32647cd9b4
  1. 13
      py/lexer.c
  2. 6
      tests/basics/string_pep498_fstring.py

@ -583,6 +583,8 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
// MP_TOKEN_END is used to indicate that this is the first string token
lex->tok_kind = MP_TOKEN_END;
bool saw_normal = false, saw_fstring = false;
// Loop to accumulate string/bytes literals
do {
// parse type codes
@ -619,6 +621,17 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
is_fstring = true;
}
if (is_fstring) {
saw_fstring = true;
} else {
saw_normal = true;
}
if (saw_fstring && saw_normal) {
// Can't concatenate f-string with normal string
break;
}
// Set or check token kind
if (lex->tok_kind == MP_TOKEN_END) {
lex->tok_kind = kind;

@ -104,10 +104,10 @@ assert f'result={foo()}' == 'result={result}'.format(result=foo())
x = 10
y = 'hi'
assert (f'h' f'i') == 'hi'
assert (f'h' 'i') == 'hi'
assert ('h' f'i') == 'hi'
#assert (f'h' 'i') == 'hi'
#assert ('h' f'i') == 'hi'
assert f'{x:^4}' == ' 10 '
assert ('a' 'b' f'{x}' f'str<{y:^4}>' 'd' 'e') == 'ab10str< hi >de'
#assert ('a' 'b' f'{x}' f'str<{y:^4}>' 'd' 'e') == 'ab10str< hi >de'
# Other tests
assert f'{{{4*10}}}' == '{40}'

Loading…
Cancel
Save