@@ -64,9 +64,11 @@ STATIC bool is_char_or3(mp_lexer_t *lex, byte c1, byte c2, byte c3) {
64
64
return lex -> chr0 == c1 || lex -> chr0 == c2 || lex -> chr0 == c3 ;
65
65
}
66
66
67
+ #if MICROPY_COMP_FSTRING_LITERAL
67
68
STATIC bool is_char_or4 (mp_lexer_t * lex , byte c1 , byte c2 , byte c3 , byte c4 ) {
68
69
return lex -> chr0 == c1 || lex -> chr0 == c2 || lex -> chr0 == c3 || lex -> chr0 == c4 ;
69
70
}
71
+ #endif
70
72
71
73
STATIC bool is_char_following (mp_lexer_t * lex , byte c ) {
72
74
return lex -> chr1 == c ;
@@ -111,9 +113,13 @@ STATIC bool is_following_odigit(mp_lexer_t *lex) {
111
113
112
114
STATIC bool is_string_or_bytes (mp_lexer_t * lex ) {
113
115
return is_char_or (lex , '\'' , '\"' )
116
+ #if MICROPY_COMP_FSTRING_LITERAL
114
117
|| (is_char_or4 (lex , 'r' , 'u' , 'b' , 'f' ) && is_char_following_or (lex , '\'' , '\"' ))
115
118
|| ((is_char_and (lex , 'r' , 'f' ) || is_char_and (lex , 'f' , 'r' ))
116
119
&& is_char_following_following_or (lex , '\'' , '\"' ))
120
+ #else
121
+ || (is_char_or3 (lex , 'r' , 'u' , 'b' ) && is_char_following_or (lex , '\'' , '\"' ))
122
+ #endif
117
123
|| ((is_char_and (lex , 'r' , 'b' ) || is_char_and (lex , 'b' , 'r' ))
118
124
&& is_char_following_following_or (lex , '\'' , '\"' ));
119
125
}
@@ -127,6 +133,7 @@ STATIC bool is_tail_of_identifier(mp_lexer_t *lex) {
127
133
return is_head_of_identifier (lex ) || is_digit (lex );
128
134
}
129
135
136
+ #if MICROPY_COMP_FSTRING_LITERAL
130
137
STATIC void swap_char_banks (mp_lexer_t * lex ) {
131
138
if (lex -> vstr_postfix_processing ) {
132
139
lex -> chr3 = lex -> chr0 ;
@@ -149,6 +156,7 @@ STATIC void swap_char_banks(mp_lexer_t *lex) {
149
156
lex -> vstr_postfix_idx = 0 ;
150
157
}
151
158
}
159
+ #endif
152
160
153
161
STATIC void next_char (mp_lexer_t * lex ) {
154
162
if (lex -> chr0 == '\n' ) {
@@ -166,13 +174,16 @@ STATIC void next_char(mp_lexer_t *lex) {
166
174
lex -> chr0 = lex -> chr1 ;
167
175
lex -> chr1 = lex -> chr2 ;
168
176
177
+ #if MICROPY_COMP_FSTRING_LITERAL
169
178
if (lex -> vstr_postfix_processing ) {
170
179
if (lex -> vstr_postfix_idx == lex -> vstr_postfix .len ) {
171
180
lex -> chr2 = '\0' ;
172
181
} else {
173
182
lex -> chr2 = lex -> vstr_postfix .buf [lex -> vstr_postfix_idx ++ ];
174
183
}
175
- } else {
184
+ } else
185
+ #endif
186
+ {
176
187
lex -> chr2 = lex -> reader .readbyte (lex -> reader .data );
177
188
}
178
189
@@ -190,10 +201,12 @@ STATIC void next_char(mp_lexer_t *lex) {
190
201
lex -> chr2 = '\n' ;
191
202
}
192
203
204
+ #if MICROPY_COMP_FSTRING_LITERAL
193
205
if (lex -> vstr_postfix_processing && lex -> chr0 == '\0' ) {
194
206
lex -> vstr_postfix_processing = false;
195
207
swap_char_banks (lex );
196
208
}
209
+ #endif
197
210
}
198
211
199
212
STATIC void indent_push (mp_lexer_t * lex , size_t indent ) {
@@ -334,15 +347,18 @@ STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw, bool is_fstring)
334
347
}
335
348
336
349
size_t n_closing = 0 ;
350
+ #if MICROPY_COMP_FSTRING_LITERAL
337
351
bool in_expression = false;
338
352
bool expression_eat = true;
353
+ #endif
339
354
340
355
while (!is_end (lex ) && (num_quotes > 1 || !is_char (lex , '\n' )) && n_closing < num_quotes ) {
341
356
if (is_char (lex , quote_char )) {
342
357
n_closing += 1 ;
343
358
vstr_add_char (& lex -> vstr , CUR_CHAR (lex ));
344
359
} else {
345
360
n_closing = 0 ;
361
+ #if MICROPY_COMP_FSTRING_LITERAL
346
362
if (is_fstring && is_char (lex , '{' )) {
347
363
vstr_add_char (& lex -> vstr , CUR_CHAR (lex ));
348
364
in_expression = !in_expression ;
@@ -390,6 +406,7 @@ STATIC void parse_string_literal(mp_lexer_t *lex, bool is_raw, bool is_fstring)
390
406
next_char (lex );
391
407
continue ;
392
408
}
409
+ #endif
393
410
394
411
if (is_char (lex , '\\' )) {
395
412
next_char (lex );
@@ -525,12 +542,14 @@ STATIC bool skip_whitespace(mp_lexer_t *lex, bool stop_at_newline) {
525
542
}
526
543
527
544
void mp_lexer_to_next (mp_lexer_t * lex ) {
545
+ #if MICROPY_COMP_FSTRING_LITERAL
528
546
if (lex -> vstr_postfix .len && !lex -> vstr_postfix_processing ) {
529
547
// end format call injection
530
548
vstr_add_char (& lex -> vstr_postfix , ')' );
531
549
lex -> vstr_postfix_processing = true;
532
550
swap_char_banks (lex );
533
551
}
552
+ #endif
534
553
535
554
// start new token text
536
555
vstr_reset (& lex -> vstr );
@@ -583,13 +602,19 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
583
602
// MP_TOKEN_END is used to indicate that this is the first string token
584
603
lex -> tok_kind = MP_TOKEN_END ;
585
604
605
+ #if MICROPY_COMP_FSTRING_LITERAL
586
606
bool saw_normal = false, saw_fstring = false;
607
+ #endif
587
608
588
609
// Loop to accumulate string/bytes literals
589
610
do {
590
611
// parse type codes
591
612
bool is_raw = false;
613
+ #if MICROPY_COMP_FSTRING_LITERAL
592
614
bool is_fstring = false;
615
+ #else
616
+ const bool is_fstring = false;
617
+ #endif
593
618
mp_token_kind_t kind = MP_TOKEN_STRING ;
594
619
int n_char = 0 ;
595
620
if (is_char (lex , 'u' )) {
@@ -608,6 +633,7 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
608
633
kind = MP_TOKEN_BYTES ;
609
634
n_char = 2 ;
610
635
}
636
+ #if MICROPY_COMP_FSTRING_LITERAL
611
637
if (is_char_following (lex , 'f' )) {
612
638
lex -> tok_kind = MP_TOKEN_FSTRING_RAW ;
613
639
break ;
@@ -619,8 +645,10 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
619
645
}
620
646
n_char = 1 ;
621
647
is_fstring = true;
648
+ #endif
622
649
}
623
650
651
+ #if MICROPY_COMP_FSTRING_LITERAL
624
652
if (is_fstring ) {
625
653
saw_fstring = true;
626
654
} else {
@@ -631,6 +659,7 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
631
659
// Can't concatenate f-string with normal string
632
660
break ;
633
661
}
662
+ #endif
634
663
635
664
// Set or check token kind
636
665
if (lex -> tok_kind == MP_TOKEN_END ) {
@@ -808,7 +837,9 @@ mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {
808
837
lex -> num_indent_level = 1 ;
809
838
lex -> indent_level = m_new (uint16_t , lex -> alloc_indent_level );
810
839
vstr_init (& lex -> vstr , 32 );
840
+ #if MICROPY_COMP_FSTRING_LITERAL
811
841
vstr_init (& lex -> vstr_postfix , 0 );
842
+ #endif
812
843
813
844
// store sentinel for first indentation level
814
845
lex -> indent_level [0 ] = 0 ;
0 commit comments