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

@ -11,7 +11,7 @@ $(TARGET): build/parser.o build/lexer.o build/main.o
build/parser.cpp build/parser.hpp: src/parser.y | build build/parser.cpp build/parser.hpp: src/parser.y | build
$(BISON) -d -o build/parser.cpp src/parser.y $(BISON) -d -o build/parser.cpp src/parser.y
build/lexer.cpp build/lexer.hpp: src/lexer.l build/parser.hpp src/scanner.hpp src/indent_helper.hpp| build build/lexer.cpp build/lexer.hpp: src/lexer.l build/parser.hpp src/scanner.hpp src/indent_stack.hpp| build
$(FLEX) --header-file=build/lexer.hpp -o build/lexer.cpp src/lexer.l $(FLEX) --header-file=build/lexer.hpp -o build/lexer.cpp src/lexer.l
build/%.o: build/%.cpp build/%.o: build/%.cpp

View file

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

View file

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

View file

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