Skip to content

Commit c9f747c

Browse files
committed
py/lexer: Move f-string completion code to more logical location.
This way, the use of `lex->fstring_args` is fully self contained within the string literal parsing section of `mp_lexer_to_next()`. Signed-off-by: Damien George <damien@micropython.org>
1 parent 7ea88f7 commit c9f747c

1 file changed

Lines changed: 24 additions & 24 deletions

File tree

py/lexer.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -558,30 +558,6 @@ static bool skip_whitespace(mp_lexer_t *lex, bool stop_at_newline) {
558558
}
559559

560560
void mp_lexer_to_next(mp_lexer_t *lex) {
561-
#if MICROPY_PY_FSTRINGS
562-
if (lex->fstring_args.len) {
563-
// moving onto the next token means the literal string is complete.
564-
// switch into injecting the format args.
565-
vstr_add_byte(&lex->fstring_args, ')');
566-
if (lex->inject_chrs_idx == 0) {
567-
// switch from stream to inject_chrs
568-
char *s = vstr_add_len(&lex->inject_chrs, 3);
569-
s[0] = lex->chr0;
570-
s[1] = lex->chr1;
571-
s[2] = lex->chr2;
572-
} else {
573-
// already consuming from inject_chrs, rewind cached chars to insert new ones
574-
assert(lex->inject_chrs_idx >= 3);
575-
lex->inject_chrs_idx -= 3;
576-
}
577-
vstr_ins_strn(&lex->inject_chrs, lex->inject_chrs_idx, lex->fstring_args.buf, lex->fstring_args.len);
578-
vstr_reset(&lex->fstring_args);
579-
lex->chr0 = lex->inject_chrs.buf[lex->inject_chrs_idx++];
580-
lex->chr1 = lex->inject_chrs.buf[lex->inject_chrs_idx++];
581-
lex->chr2 = lex->inject_chrs.buf[lex->inject_chrs_idx++];
582-
}
583-
#endif
584-
585561
// start new token text
586562
vstr_reset(&lex->vstr);
587563

@@ -706,6 +682,30 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
706682

707683
} while (is_string_or_bytes(lex));
708684

685+
#if MICROPY_PY_FSTRINGS
686+
if (lex->fstring_args.len) {
687+
// If there was an f-string then it's now complete.
688+
// Switch into injecting the format args.
689+
vstr_add_byte(&lex->fstring_args, ')');
690+
if (lex->inject_chrs_idx == 0) {
691+
// switch from stream to inject_chrs
692+
char *s = vstr_add_len(&lex->inject_chrs, 3);
693+
s[0] = lex->chr0;
694+
s[1] = lex->chr1;
695+
s[2] = lex->chr2;
696+
} else {
697+
// already consuming from inject_chrs, rewind cached chars to insert new ones
698+
assert(lex->inject_chrs_idx >= 3);
699+
lex->inject_chrs_idx -= 3;
700+
}
701+
vstr_ins_strn(&lex->inject_chrs, lex->inject_chrs_idx, lex->fstring_args.buf, lex->fstring_args.len);
702+
vstr_reset(&lex->fstring_args);
703+
lex->chr0 = lex->inject_chrs.buf[lex->inject_chrs_idx++];
704+
lex->chr1 = lex->inject_chrs.buf[lex->inject_chrs_idx++];
705+
lex->chr2 = lex->inject_chrs.buf[lex->inject_chrs_idx++];
706+
}
707+
#endif
708+
709709
} else if (is_head_of_identifier(lex)) {
710710
lex->tok_kind = MP_TOKEN_NAME;
711711

0 commit comments

Comments
 (0)