Skip to content

Commit 773278e

Browse files
committed
py/lexer: Simplify handling of line-continuation error.
Previous to this patch there was an explicit check for errors with line continuation (where backslash was not immediately followed by a newline). But this check is not necessary: if there is an error then the remaining logic of the tokeniser will reject the backslash and correctly produce a syntax error.
1 parent ae43679 commit 773278e

2 files changed

Lines changed: 9 additions & 18 deletions

File tree

py/lexer.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,10 @@ void mp_lexer_to_next(mp_lexer_t *lex) {
290290
next_char(lex);
291291
}
292292
// had_physical_newline will be set on next loop
293-
} else if (is_char(lex, '\\')) {
294-
// backslash (outside string literals) must appear just before a physical newline
293+
} else if (is_char_and(lex, '\\', '\n')) {
294+
// line-continuation, so don't set had_physical_newline
295+
next_char(lex);
295296
next_char(lex);
296-
if (!is_physical_newline(lex)) {
297-
// SyntaxError: unexpected character after line continuation character
298-
lex->tok_line = lex->line;
299-
lex->tok_column = lex->column;
300-
lex->tok_kind = MP_TOKEN_BAD_LINE_CONTINUATION;
301-
return;
302-
} else {
303-
next_char(lex);
304-
}
305297
} else {
306298
break;
307299
}

py/lexer.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,25 @@
3939
*/
4040

4141
typedef enum _mp_token_kind_t {
42-
MP_TOKEN_END, // 0
42+
MP_TOKEN_END,
4343

4444
MP_TOKEN_INVALID,
4545
MP_TOKEN_DEDENT_MISMATCH,
4646
MP_TOKEN_LONELY_STRING_OPEN,
47-
MP_TOKEN_BAD_LINE_CONTINUATION,
4847

49-
MP_TOKEN_NEWLINE, // 5
50-
MP_TOKEN_INDENT, // 6
51-
MP_TOKEN_DEDENT, // 7
48+
MP_TOKEN_NEWLINE,
49+
MP_TOKEN_INDENT,
50+
MP_TOKEN_DEDENT,
5251

53-
MP_TOKEN_NAME, // 8
52+
MP_TOKEN_NAME,
5453
MP_TOKEN_INTEGER,
5554
MP_TOKEN_FLOAT_OR_IMAG,
5655
MP_TOKEN_STRING,
5756
MP_TOKEN_BYTES,
5857

5958
MP_TOKEN_ELLIPSIS,
6059

61-
MP_TOKEN_KW_FALSE, // 14
60+
MP_TOKEN_KW_FALSE,
6261
MP_TOKEN_KW_NONE,
6362
MP_TOKEN_KW_TRUE,
6463
MP_TOKEN_KW___DEBUG__,

0 commit comments

Comments
 (0)