small fixes
This commit is contained in:
parent
0bc67209b9
commit
38438904dc
2 changed files with 37 additions and 31 deletions
|
|
@ -8,11 +8,12 @@ enum class IndentAction
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr int TAB_WIDTH = 8;
|
constexpr int TAB_WIDTH = 8;
|
||||||
|
constexpr char TAB = '\t';
|
||||||
|
|
||||||
class IndentStack
|
class IndentStack
|
||||||
{
|
{
|
||||||
std::stack<int> indent_stack;
|
std::stack<int> indent_stack;
|
||||||
int pending_dedents = 0;
|
int dedent_queue = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IndentStack() { indent_stack.push(0); }
|
IndentStack() { indent_stack.push(0); }
|
||||||
|
|
@ -32,15 +33,15 @@ public:
|
||||||
return IndentAction::Indent;
|
return IndentAction::Indent;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (indent_stack.top() > spaces)
|
while (!indent_stack.empty() && indent_stack.top() > spaces)
|
||||||
{
|
{
|
||||||
indent_stack.pop();
|
indent_stack.pop();
|
||||||
pending_dedents++;
|
dedent_queue++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pending_dedents > 0)
|
if (dedent_queue > 0)
|
||||||
{
|
{
|
||||||
pending_dedents--;
|
dedent_queue--;
|
||||||
return IndentAction::Dedent;
|
return IndentAction::Dedent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -49,11 +50,11 @@ public:
|
||||||
|
|
||||||
bool popDedent()
|
bool popDedent()
|
||||||
{
|
{
|
||||||
if (pending_dedents <= 0)
|
if (dedent_queue <= 0)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
pending_dedents--;
|
dedent_queue--;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,18 +63,24 @@ public:
|
||||||
int spaces = 0;
|
int spaces = 0;
|
||||||
for (int i = 0; i < length; i++)
|
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;
|
return spaces;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasRemainingDedents()
|
bool hasRemainingDedents()
|
||||||
{
|
{
|
||||||
return pending_dedents > 0;
|
return dedent_queue > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
IndentAction closeBlock()
|
IndentAction closeBlock()
|
||||||
{
|
{
|
||||||
|
// Force indentation reset at EOF
|
||||||
return updateIndentation(0);
|
return updateIndentation(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
^[ \t]*\r?\n { /* Ignore lines containing whitespace only */ }
|
^[ \t]*\r?\n { /* Ignore lines containing whitespace only */ }
|
||||||
^[ \t]*#.*\n { /* Ignore comment-only lines */ }
|
^[ \t]*#.*\n { /* Ignore comment-only lines */ }
|
||||||
|
|
||||||
^[ \t]* {
|
^[ \t]+ {
|
||||||
int col = indent_stack.calculateColumn(yytext, yyleng);
|
int col = indent_stack.calculateColumn(yytext, yyleng);
|
||||||
auto action = indent_stack.updateIndentation(col);
|
auto action = indent_stack.updateIndentation(col);
|
||||||
|
|
||||||
|
|
@ -38,7 +38,6 @@ in { return yy::parser::token::TOK_KW_IN; }
|
||||||
\- { return yy::parser::token::TOK_MINUS; }
|
\- { return yy::parser::token::TOK_MINUS; }
|
||||||
|
|
||||||
\" { yymore(); BEGIN(STRING); }
|
\" { yymore(); BEGIN(STRING); }
|
||||||
<STRING>\"\" { yymore(); }
|
|
||||||
<STRING>\" { BEGIN(INITIAL); yylval->as<std::string>() = yytext; return yy::parser::token::TOK_STRING; }
|
<STRING>\" { BEGIN(INITIAL); yylval->as<std::string>() = yytext; return yy::parser::token::TOK_STRING; }
|
||||||
<STRING>. { yymore(); }
|
<STRING>. { yymore(); }
|
||||||
|
|
||||||
|
|
@ -46,4 +45,4 @@ in { return yy::parser::token::TOK_KW_IN; }
|
||||||
[A-Za-z_][A-Za-z0-9_]* { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_IDENTIFIER; }
|
[A-Za-z_][A-Za-z0-9_]* { yylval->as<std::string>() = yytext; return yy::parser::token::TOK_IDENTIFIER; }
|
||||||
|
|
||||||
<<EOF>> { return yy::parser::token::TOK_EOF; }
|
<<EOF>> { return yy::parser::token::TOK_EOF; }
|
||||||
. { /* ignore other characters */ }
|
. { }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue