Skip to content

Commit 98b3072

Browse files
committed
py/lexer: Simplify handling of indenting of very first token.
1 parent b0599de commit 98b3072

1 file changed

Lines changed: 10 additions & 15 deletions

File tree

py/lexer.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ STATIC bool get_hex(mp_lexer_t *lex, mp_uint_t num_digits, mp_uint_t *result) {
283283
return true;
284284
}
285285

286-
STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) {
286+
void mp_lexer_to_next(mp_lexer_t *lex) {
287287
// start new token text
288288
vstr_reset(&lex->vstr);
289289

@@ -322,14 +322,7 @@ STATIC void mp_lexer_next_token_into(mp_lexer_t *lex, bool first_token) {
322322
lex->tok_line = lex->line;
323323
lex->tok_column = lex->column;
324324

325-
if (first_token && lex->line == 1 && lex->column != 1) {
326-
// check that the first token is in the first column
327-
// if first token is not on first line, we get a physical newline and
328-
// this check is done as part of normal indent/dedent checking below
329-
// (done to get equivalence with CPython)
330-
lex->tok_kind = MP_TOKEN_INDENT;
331-
332-
} else if (lex->emit_dent < 0) {
325+
if (lex->emit_dent < 0) {
333326
lex->tok_kind = MP_TOKEN_DEDENT;
334327
lex->emit_dent += 1;
335328

@@ -705,7 +698,7 @@ mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {
705698
vstr_init(&lex->vstr, 32);
706699

707700
// check for memory allocation error
708-
// note: vstr_init above may fail on malloc, but so may mp_lexer_next_token_into below
701+
// note: vstr_init above may fail on malloc, but so may mp_lexer_to_next below
709702
if (lex->indent_level == NULL) {
710703
mp_lexer_free(lex);
711704
return NULL;
@@ -737,7 +730,13 @@ mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) {
737730
}
738731

739732
// preload first token
740-
mp_lexer_next_token_into(lex, true);
733+
mp_lexer_to_next(lex);
734+
735+
// Check that the first token is in the first column. If it's not then we
736+
// convert the token kind to INDENT so that the parser gives a syntax error.
737+
if (lex->tok_column != 1) {
738+
lex->tok_kind = MP_TOKEN_INDENT;
739+
}
741740

742741
return lex;
743742
}
@@ -785,10 +784,6 @@ void mp_lexer_free(mp_lexer_t *lex) {
785784
}
786785
}
787786

788-
void mp_lexer_to_next(mp_lexer_t *lex) {
789-
mp_lexer_next_token_into(lex, false);
790-
}
791-
792787
#if 0
793788
// This function is used to print the current token and should only be
794789
// needed to debug the lexer, so it's not available via a config option.

0 commit comments

Comments
 (0)