From 38438904dcbec1a4b91ad768cfe486a20981038b Mon Sep 17 00:00:00 2001 From: bronku Date: Tue, 25 Nov 2025 16:24:09 +0100 Subject: [PATCH] small fixes --- src/indent_stack.hpp | 25 ++++++++++++++++--------- src/lexer.l | 43 +++++++++++++++++++++---------------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/indent_stack.hpp b/src/indent_stack.hpp index 4c6b2b5..f56e39a 100644 --- a/src/indent_stack.hpp +++ b/src/indent_stack.hpp @@ -8,11 +8,12 @@ enum class IndentAction }; constexpr int TAB_WIDTH = 8; +constexpr char TAB = '\t'; class IndentStack { std::stack indent_stack; - int pending_dedents = 0; + int dedent_queue = 0; public: IndentStack() { indent_stack.push(0); } @@ -32,15 +33,15 @@ public: return IndentAction::Indent; } - while (indent_stack.top() > spaces) + while (!indent_stack.empty() && indent_stack.top() > spaces) { indent_stack.pop(); - pending_dedents++; + dedent_queue++; } - if (pending_dedents > 0) + if (dedent_queue > 0) { - pending_dedents--; + dedent_queue--; return IndentAction::Dedent; } @@ -49,11 +50,11 @@ public: bool popDedent() { - if (pending_dedents <= 0) + if (dedent_queue <= 0) { return false; } - pending_dedents--; + dedent_queue--; return true; } @@ -62,18 +63,24 @@ public: int spaces = 0; for (int i = 0; i < length; i++) { - spaces += (text[i] == '\t') ? TAB_WIDTH : 1; + if (text[i] == TAB) + { + spaces += TAB_WIDTH - (spaces % TAB_WIDTH); + continue; + } + spaces++; } return spaces; } bool hasRemainingDedents() { - return pending_dedents > 0; + return dedent_queue > 0; } IndentAction closeBlock() { + // Force indentation reset at EOF return updateIndentation(0); } }; \ No newline at end of file diff --git a/src/lexer.l b/src/lexer.l index de29239..61b2a3e 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -10,10 +10,10 @@ %} %% -^[ \t]*\r?\n { /* Ignore lines containing whitespace only */ } -^[ \t]*#.*\n { /* Ignore comment-only lines */ } +^[ \t]*\r?\n { /* Ignore lines containing whitespace only */ } +^[ \t]*#.*\n { /* Ignore comment-only lines */ } -^[ \t]* { +^[ \t]+ { int col = indent_stack.calculateColumn(yytext, yyleng); auto action = indent_stack.updateIndentation(col); @@ -22,28 +22,27 @@ } -\n { 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; } -return { return yy::parser::token::TOK_KW_RETURN; } -for { return yy::parser::token::TOK_KW_FOR; } -in { return yy::parser::token::TOK_KW_IN; } +def { return yy::parser::token::TOK_KW_DEF; } +if { return yy::parser::token::TOK_KW_IF; } +return { return yy::parser::token::TOK_KW_RETURN; } +for { return yy::parser::token::TOK_KW_FOR; } +in { return yy::parser::token::TOK_KW_IN; } -\( { return yy::parser::token::TOK_L_PAREN; } -\) { return yy::parser::token::TOK_R_PAREN; } -\: { return yy::parser::token::TOK_COLON; } -\= { return yy::parser::token::TOK_ASSIGN; } -\+ { return yy::parser::token::TOK_PLUS; } -\- { return yy::parser::token::TOK_MINUS; } +\( { return yy::parser::token::TOK_L_PAREN; } +\) { return yy::parser::token::TOK_R_PAREN; } +\: { return yy::parser::token::TOK_COLON; } +\= { return yy::parser::token::TOK_ASSIGN; } +\+ { return yy::parser::token::TOK_PLUS; } +\- { return yy::parser::token::TOK_MINUS; } -\" { yymore(); BEGIN(STRING); } -\"\" { yymore(); } -\" { BEGIN(INITIAL); yylval->as() = yytext; return yy::parser::token::TOK_STRING; } -. { yymore(); } +\" { yymore(); BEGIN(STRING); } +\" { BEGIN(INITIAL); yylval->as() = yytext; return yy::parser::token::TOK_STRING; } +. { yymore(); } -[0-9]+ { yylval->as() = yytext; return yy::parser::token::TOK_NUMBER; } +[0-9]+ { yylval->as() = yytext; return yy::parser::token::TOK_NUMBER; } [A-Za-z_][A-Za-z0-9_]* { yylval->as() = yytext; return yy::parser::token::TOK_IDENTIFIER; } -<> { return yy::parser::token::TOK_EOF; } -. { /* ignore other characters */ } +<> { return yy::parser::token::TOK_EOF; } +. { }