diff --git a/src/lpython/parser/tokenizer.re b/src/lpython/parser/tokenizer.re index 83882cbf40..e9f13fc6f5 100644 --- a/src/lpython/parser/tokenizer.re +++ b/src/lpython/parser/tokenizer.re @@ -321,18 +321,25 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost } if (indent) { indent = false; - if (last_indent_length == 0) { - last_indent_type = tok[0]; - } - if (last_indent_type == tok[0]) { - indent_length.push_back(cur-tok); - last_indent_length = cur-tok; - RET(TK_INDENT); + if (cur[0] != ' ' && cur[0] != '\t' + && last_indent_length < cur-tok) { + if (last_indent_length == 0) { + last_indent_type = tok[0]; + } + if (last_indent_type == tok[0]) { + indent_length.push_back(cur-tok); + last_indent_length = cur-tok; + RET(TK_INDENT); + } else { + token_loc(loc); + throw parser_local::TokenizerError( + "Indentation should be of the same type " + "(either tabs or spaces)", {loc}); + } } else { - loc.first++; loc.last++; + token_loc(loc); throw parser_local::TokenizerError( - "Indentation should be of the same type " - "(either tabs or spaces)", {loc}); + "Expected an indented block.", {loc}); } } else { if(last_token == yytokentype::TK_NEWLINE @@ -347,7 +354,7 @@ int Tokenizer::lex(Allocator &al, YYSTYPE &yylval, Location &loc, diag::Diagnost RET(TK_DEDENT); } } else { - loc.first++; loc.last++; + token_loc(loc); throw parser_local::TokenizerError( "Indentation should be of the same type " "(either tabs or spaces)", {loc});