extracted line couting

This commit is contained in:
bronku 2025-11-25 15:51:51 +01:00
parent 7f24975092
commit 60eaba951a
2 changed files with 17 additions and 6 deletions

View file

@ -7,6 +7,8 @@ enum class IndentAction
None None
}; };
constexpr int TAB_WIDTH = 8;
class IndentHelper class IndentHelper
{ {
std::stack<int> indent_stack; std::stack<int> indent_stack;
@ -56,4 +58,14 @@ public:
pending_dedents--; pending_dedents--;
return true; 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;
}
}; };

View file

@ -14,15 +14,14 @@
^[ \t]*#.*\n { /* Ignore comment-only lines */ } ^[ \t]*#.*\n { /* Ignore comment-only lines */ }
^[ \t]* { ^[ \t]* {
int spaces = 0; int col = indent_helper.calculateColumn(yytext, yyleng);
for (int i = 0; i < yyleng; i++) auto action = indent_helper.processLine(col);
spaces += (yytext[i] == '\t') ? 8 : 1;
IndentAction result = indent_helper.processLine(spaces); if (action == IndentAction::Indent) return yy::parser::token::TOK_INDENT;
if (result == IndentAction::Indent) return yy::parser::token::TOK_INDENT; if (action == IndentAction::Dedent) return yy::parser::token::TOK_DEDENT;
if (result == IndentAction::Dedent) return yy::parser::token::TOK_DEDENT;
} }
\n { return yy::parser::token::TOK_NEWLINE; } \n { return yy::parser::token::TOK_NEWLINE; }
def { return yy::parser::token::TOK_KW_DEF; } def { return yy::parser::token::TOK_KW_DEF; }