minor fixes

This commit is contained in:
bronku 2025-11-25 16:06:03 +01:00
parent 60eaba951a
commit 0bc67209b9
4 changed files with 24 additions and 14 deletions

View file

@ -9,15 +9,15 @@ enum class IndentAction
constexpr int TAB_WIDTH = 8;
class IndentHelper
class IndentStack
{
std::stack<int> indent_stack;
int pending_dedents = 0;
public:
IndentHelper() { indent_stack.push(0); }
IndentStack() { indent_stack.push(0); }
IndentAction processLine(int spaces)
IndentAction updateIndentation(int spaces)
{
int current = indent_stack.top();
@ -32,7 +32,6 @@ public:
return IndentAction::Indent;
}
pending_dedents = 0;
while (indent_stack.top() > spaces)
{
indent_stack.pop();
@ -48,8 +47,7 @@ public:
return IndentAction::None;
}
// returns false on failure
bool consumeDedent()
bool popDedent()
{
if (pending_dedents <= 0)
{
@ -68,4 +66,14 @@ public:
}
return spaces;
}
bool hasRemainingDedents()
{
return pending_dedents > 0;
}
IndentAction closeBlock()
{
return updateIndentation(0);
}
};

View file

@ -5,7 +5,7 @@
%{
#include "scanner.hpp"
#include "indent_helper.hpp"
#include "indent_stack.hpp"
%}
@ -14,8 +14,8 @@
^[ \t]*#.*\n { /* Ignore comment-only lines */ }
^[ \t]* {
int col = indent_helper.calculateColumn(yytext, yyleng);
auto action = indent_helper.processLine(col);
int col = indent_stack.calculateColumn(yytext, yyleng);
auto action = indent_stack.updateIndentation(col);
if (action == IndentAction::Indent) return yy::parser::token::TOK_INDENT;
if (action == IndentAction::Dedent) return yy::parser::token::TOK_DEDENT;

View file

@ -5,18 +5,18 @@
#endif
#include "parser.hpp"
#include "indent_helper.hpp"
#include "indent_stack.hpp"
#include <string>
class Scanner : public yyFlexLexer
{
public:
IndentHelper indent_helper;
IndentStack indent_stack;
Scanner(std::istream &in) : yyFlexLexer(&in), yylval(nullptr) {}
int lex(yy::parser::semantic_type *lval)
{
if (indent_helper.consumeDedent())
if (indent_stack.popDedent())
{
return yy::parser::token::TOK_DEDENT;
}
@ -25,7 +25,9 @@ public:
if (token == yy::parser::token::TOK_EOF)
{
if (indent_helper.processLine(0) == IndentAction::Dedent)
auto result = indent_stack.closeBlock();
if (result == IndentAction::Dedent)
{
return yy::parser::token::TOK_DEDENT;
}