indent fix

This commit is contained in:
bronku 2025-11-25 15:36:51 +01:00
parent 5062a9b588
commit 4def48d206
3 changed files with 37 additions and 50 deletions

View file

@ -7,18 +7,13 @@
#include "scanner.hpp"
#include "indent_helper.hpp"
IndentHelper indent_helper;
bool at_line_start = true;
%}
%%
^[ \t]*\r?\n { /* Ignore lines containing whitespace only */ }
^[ \t]*#.*\n { /* Ignore comment-only lines */ }
^[ \t]* {
if (!at_line_start) {
REJECT;
}
at_line_start = false;
int spaces = 0;
for (int i = 0; i < yyleng; i++)
spaces += (yytext[i] == '\t') ? 8 : 1;
@ -28,10 +23,7 @@ bool at_line_start = true;
if (result < 0) return yy::parser::token::TOK_DEDENT;
}
\n {
at_line_start = true;
return yy::parser::token::TOK_NEWLINE;
}
\n { return yy::parser::token::TOK_NEWLINE; }
def { return yy::parser::token::TOK_KW_DEF; }
if { return yy::parser::token::TOK_KW_IF; }
@ -54,15 +46,5 @@ in { return yy::parser::token::TOK_KW_IN; }
[0-9]+ { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_NUMBER; }
[A-Za-z_][A-Za-z0-9_]* { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_IDENTIFIER; }
<<EOF>> {
if (indent_helper.hasPendingDedents()) {
indent_helper.consumeDedent();
return yy::parser::token::TOK_DEDENT;
}
int d;
if ((d = indent_helper.flushDedent()) != 0) return yy::parser::token::TOK_DEDENT;
return yy::parser::token::TOK_EOF;
}
<<EOF>> { return yy::parser::token::TOK_EOF; }
. { /* ignore other characters */ }