diff --git a/src/indent_helper.hpp b/src/indent_helper.hpp index fcc8ad0..e009ee0 100644 --- a/src/indent_helper.hpp +++ b/src/indent_helper.hpp @@ -7,6 +7,8 @@ enum class IndentAction None }; +constexpr int TAB_WIDTH = 8; + class IndentHelper { std::stack indent_stack; @@ -56,4 +58,14 @@ public: pending_dedents--; return true; } + + static int calculateColumn(const char *text, int length) + { + int spaces = 0; + for (int i = 0; i < length; i++) + { + spaces += (text[i] == '\t') ? TAB_WIDTH : 1; + } + return spaces; + } }; \ No newline at end of file diff --git a/src/lexer.l b/src/lexer.l index 3ec588b..b603b70 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -14,15 +14,14 @@ ^[ \t]*#.*\n { /* Ignore comment-only lines */ } ^[ \t]* { - int spaces = 0; - for (int i = 0; i < yyleng; i++) - spaces += (yytext[i] == '\t') ? 8 : 1; + int col = indent_helper.calculateColumn(yytext, yyleng); + auto action = indent_helper.processLine(col); - IndentAction result = indent_helper.processLine(spaces); - if (result == IndentAction::Indent) return yy::parser::token::TOK_INDENT; - if (result == IndentAction::Dedent) return yy::parser::token::TOK_DEDENT; + if (action == IndentAction::Indent) return yy::parser::token::TOK_INDENT; + if (action == IndentAction::Dedent) return yy::parser::token::TOK_DEDENT; } + \n { return yy::parser::token::TOK_NEWLINE; } def { return yy::parser::token::TOK_KW_DEF; }